Bugzilla – Attachment 68563 Details for
Bug 7317
Add an Interlibrary Loan Module to Circulation and OPAC
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 7317: Overload Koha::Illrequest::TO_JSON
Bug-7317-Overload-KohaIllrequestTOJSON.patch (text/plain), 5.95 KB, created by
Benjamin Rokseth
on 2017-10-25 20:35:43 UTC
(
hide
)
Description:
Bug 7317: Overload Koha::Illrequest::TO_JSON
Filename:
MIME Type:
Creator:
Benjamin Rokseth
Created:
2017-10-25 20:35:43 UTC
Size:
5.95 KB
patch
obsolete
>From 9f9ef65f5d1ce6aae1afd3c16c960b8aac296a67 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 24 Oct 2017 13:43:51 -0300 >Subject: [PATCH] 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> >--- > Koha/Illrequest.pm | 64 +++++++++++++++++++++++++++++++++++++------- > t/db_dependent/Illrequests.t | 60 +++++++++++++++++++++++++++++++++++++++-- > 2 files changed, 113 insertions(+), 11 deletions(-) > >diff --git a/Koha/Illrequest.pm b/Koha/Illrequest.pm >index 12ef9aa..2b34b88 100644 >--- a/Koha/Illrequest.pm >+++ b/Koha/Illrequest.pm >@@ -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> >diff --git a/t/db_dependent/Illrequests.t b/t/db_dependent/Illrequests.t >index 34b9eda..b65dc08 100644 >--- a/t/db_dependent/Illrequests.t >+++ b/t/db_dependent/Illrequests.t >@@ -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; >+}; >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 7317
:
59893
|
59894
|
59895
|
59920
|
60485
|
60486
|
60487
|
60488
|
63081
|
63082
|
63083
|
63084
|
63085
|
63086
|
63087
|
63088
|
63089
|
63090
|
63113
|
63175
|
63176
|
63354
|
63421
|
63472
|
63475
|
65469
|
65470
|
65471
|
65472
|
65473
|
65474
|
65475
|
65476
|
66266
|
67242
|
67243
|
67244
|
67245
|
67246
|
67247
|
67248
|
67249
|
67250
|
67251
|
67252
|
67253
|
67254
|
67255
|
67256
|
67257
|
67258
|
67259
|
67260
|
67261
|
67262
|
67263
|
67264
|
67281
|
67282
|
67283
|
67284
|
67285
|
67286
|
67287
|
67288
|
67289
|
67290
|
67291
|
67292
|
67293
|
67294
|
67295
|
67296
|
67297
|
67298
|
67299
|
67300
|
67301
|
67302
|
67303
|
68185
|
68186
|
68328
|
68391
|
68406
|
68407
|
68408
|
68409
|
68418
|
68444
|
68445
|
68483
|
68484
|
68485
|
68487
|
68516
|
68517
|
68518
|
68519
|
68520
|
68521
|
68522
|
68523
|
68524
|
68525
|
68542
|
68543
|
68544
|
68545
|
68546
|
68547
|
68548
|
68549
|
68550
|
68551
|
68552
|
68558
|
68559
|
68560
|
68561
|
68562
| 68563 |
68564
|
68565
|
68566
|
68567
|
68858
|
68874
|
68877
|
68878
|
68885
|
68886
|
68921
|
68922
|
68926
|
68927
|
68928
|
68929
|
68931
|
68932
|
68933
|
68988
|
68989
|
68999
|
69000
|
69006
|
69008
|
69009
|
69012
|
69014
|
69044
|
69045
|
69046
|
69056
|
69059
|
69060
|
69061
|
69063
|
69064
|
69065
|
69066
|
69069
|
69070
|
69071
|
69074