Bug 7317: Overload Koha::Illrequest::TO_JSON
This patch implements an overloaded TO_JSON method, that introduces the option to (selectively) embed information required on the REST api. Tests are included for the new method. To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Illrequests.t => SUCCESS: Tests pass! - Sign off :-D Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Magnus Enger <magnus@libriotech.no> Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> Signed-off-by: Benjamin Rokseth <benjamin.rokseth@kul.oslo.kommune.no> Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
parent
2d971d15b5
commit
81df964bbd
|
|
@ -100,18 +100,10 @@ the API.
|
|||
The interface's request method returned saying that the desired item is not
|
||||
available for request.
|
||||
|
||||
=head2 Class Methods
|
||||
=head2 Class methods
|
||||
|
||||
=cut
|
||||
|
||||
=head3 type
|
||||
|
||||
=cut
|
||||
|
||||
sub _type {
|
||||
return 'Illrequest';
|
||||
}
|
||||
|
||||
sub illrequestattributes {
|
||||
my ( $self ) = @_;
|
||||
return Koha::Illrequestattributes->_new_from_dbic(
|
||||
|
|
@ -926,6 +918,60 @@ sub _censor {
|
|||
return $params;
|
||||
}
|
||||
|
||||
=head3 TO_JSON
|
||||
|
||||
$json = $illrequest->TO_JSON
|
||||
|
||||
Overloaded I<TO_JSON> method that takes care of inserting calculated values
|
||||
into the unblessed representation of the object.
|
||||
|
||||
=cut
|
||||
|
||||
sub TO_JSON {
|
||||
my ( $self, $embed ) = @_;
|
||||
|
||||
my $object = $self->SUPER::TO_JSON();
|
||||
$object->{id_prefix} = $self->id_prefix;
|
||||
|
||||
if ( scalar (keys %$embed) ) {
|
||||
# Augment the request response with patron details if appropriate
|
||||
if ( $embed->{patron} ) {
|
||||
my $patron = $self->patron;
|
||||
$object->{patron} = {
|
||||
firstname => $patron->firstname,
|
||||
surname => $patron->surname,
|
||||
cardnumber => $patron->cardnumber
|
||||
};
|
||||
}
|
||||
# Augment the request response with metadata details if appropriate
|
||||
if ( $embed->{metadata} ) {
|
||||
$object->{metadata} = $self->metadata;
|
||||
}
|
||||
# Augment the request response with status details if appropriate
|
||||
if ( $embed->{capabilities} ) {
|
||||
$object->{capabilities} = $self->capabilities;
|
||||
}
|
||||
# Augment the request response with library details if appropriate
|
||||
if ( $embed->{branch} ) {
|
||||
$object->{branch} = Koha::Libraries->find(
|
||||
$self->branchcode
|
||||
)->TO_JSON;
|
||||
}
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
=head2 Internal methods
|
||||
|
||||
=head3 _type
|
||||
|
||||
=cut
|
||||
|
||||
sub _type {
|
||||
return 'Illrequest';
|
||||
}
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
|
||||
|
|
|
|||
|
|
@ -26,9 +26,10 @@ use Koha::Patrons;
|
|||
use t::lib::Mocks;
|
||||
use t::lib::TestBuilder;
|
||||
use Test::MockObject;
|
||||
use Test::MockModule;
|
||||
use Test::Exception;
|
||||
|
||||
use Test::More tests => 10;
|
||||
use Test::More tests => 11;
|
||||
|
||||
my $schema = Koha::Database->new->schema;
|
||||
my $builder = t::lib::TestBuilder->new;
|
||||
|
|
@ -41,6 +42,7 @@ subtest 'Basic object tests' => sub {
|
|||
|
||||
$schema->storage->txn_begin;
|
||||
|
||||
Koha::Illrequests->search->delete;
|
||||
my $illrq = $builder->build({ source => 'Illrequest' });
|
||||
my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
|
||||
|
||||
|
|
@ -789,4 +791,58 @@ subtest 'Checking Limits' => sub {
|
|||
$schema->storage->txn_rollback;
|
||||
};
|
||||
|
||||
1;
|
||||
subtest 'TO_JSON() tests' => sub {
|
||||
|
||||
plan tests => 10;
|
||||
|
||||
my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
|
||||
|
||||
# Mock ->capabilities
|
||||
$illreqmodule->mock( 'capabilities', sub { return 'capable'; } );
|
||||
|
||||
# Mock ->metadata
|
||||
$illreqmodule->mock( 'metadata', sub { return 'metawhat?'; } );
|
||||
|
||||
$schema->storage->txn_begin;
|
||||
|
||||
my $library = $builder->build_object( { class => 'Koha::Libraries' } );
|
||||
my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
|
||||
my $illreq = $builder->build_object(
|
||||
{
|
||||
class => 'Koha::Illrequests',
|
||||
value => {
|
||||
branchcode => $library->branchcode,
|
||||
borrowernumber => $patron->borrowernumber
|
||||
}
|
||||
}
|
||||
);
|
||||
my $illreq_json = $illreq->TO_JSON;
|
||||
is( $illreq_json->{patron},
|
||||
undef, '%embed not passed, no \'patron\' attribute' );
|
||||
is( $illreq_json->{metadata},
|
||||
undef, '%embed not passed, no \'metadata\' attribute' );
|
||||
is( $illreq_json->{capabilities},
|
||||
undef, '%embed not passed, no \'capabilities\' attribute' );
|
||||
is( $illreq_json->{branch},
|
||||
undef, '%embed not passed, no \'branch\' attribute' );
|
||||
|
||||
$illreq_json = $illreq->TO_JSON(
|
||||
{ patron => 1, metadata => 1, capabilities => 1, branch => 1 } );
|
||||
is( $illreq_json->{patron}->{firstname},
|
||||
$patron->firstname,
|
||||
'%embed passed, \'patron\' attribute correct (firstname)' );
|
||||
is( $illreq_json->{patron}->{surname},
|
||||
$patron->surname,
|
||||
'%embed passed, \'patron\' attribute correct (surname)' );
|
||||
is( $illreq_json->{patron}->{cardnumber},
|
||||
$patron->cardnumber,
|
||||
'%embed passed, \'patron\' attribute correct (cardnumber)' );
|
||||
is( $illreq_json->{metadata},
|
||||
'metawhat?', '%embed passed, \'metadata\' attribute correct' );
|
||||
is( $illreq_json->{capabilities},
|
||||
'capable', '%embed passed, \'capabilities\' attribute correct' );
|
||||
is( $illreq_json->{branch}->{branchcode},
|
||||
$library->branchcode, '%embed not passed, no \'branch\' attribute' );
|
||||
|
||||
$schema->storage->txn_rollback;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue