Bug 27589: Unit test
[srvgit] / t / db_dependent / ILSDI_Services.t
old mode 100644 (file)
new mode 100755 (executable)
index 4726c76..3818762
@@ -19,7 +19,7 @@ use Modern::Perl;
 
 use CGI qw ( -utf8 );
 
-use Test::More tests => 9;
+use Test::More tests => 10;
 use Test::MockModule;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
@@ -60,7 +60,7 @@ subtest 'AuthenticatePatron test' => sub {
         }
     });
 
-    my $query = new CGI;
+    my $query = CGI->new;
     $query->param( 'username', $borrower->{userid});
     $query->param( 'password', $plain_password);
 
@@ -200,7 +200,6 @@ subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes
             source => 'Accountline',
             value  => {
                 borrowernumber    => $brwr->{borrowernumber},
-                accounttype       => undef,
                 debit_type_code   => 'OVERDUE',
                 amountoutstanding => 10
             }
@@ -208,7 +207,7 @@ subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes
     );
 
     # Prepare and send web request for IL-SDI server:
-    my $query = new CGI;
+    my $query = CGI->new;
     $query->param( 'service', 'GetPatronInfo' );
     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
     $query->param( 'show_attributes', '1' );
@@ -218,13 +217,11 @@ subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes
 
     # Build a structure for comparison:
     my $cmp = {
-        category_code     => $attr_type_visible->{'category_code'},
-        class             => $attr_type_visible->{'class'},
-        code              => $attr_shown->{'code'},
-        description       => $attr_type_visible->{'description'},
-        display_checkout  => $attr_type_visible->{'display_checkout'},
+        borrowernumber    => $brwr->{borrowernumber},
         value             => $attr_shown->{'attribute'},
-        value_description => undef,
+        value_description => $attr_shown->{'attribute'},
+        %$attr_type_visible,
+        %$attr_shown,
     };
 
     is( $reply->{'charges'}, '10.00',
@@ -303,7 +300,7 @@ subtest 'LookupPatron test' => sub {
 
 subtest 'Holds test' => sub {
 
-    plan tests => 5;
+    plan tests => 8;
 
     $schema->storage->txn_begin;
 
@@ -313,148 +310,163 @@ subtest 'Holds test' => sub {
         source => 'Borrower',
     });
 
-    my $biblio = $builder->build({
-        source => 'Biblio',
-    });
-
-    my $biblioitems = $builder->build({
-        source => 'Biblioitem',
-        value => {
-            biblionumber => $biblio->{biblionumber},
-        }
-    });
-
-    my $item = $builder->build({
-        source => 'Item',
-        value => {
-            biblionumber => $biblio->{biblionumber},
+    my $item = $builder->build_sample_item(
+        {
             damaged => 1
         }
-    });
+    );
 
-    my $query = new CGI;
+    my $query = CGI->new;
     $query->param( 'patron_id', $patron->{borrowernumber});
-    $query->param( 'bib_id', $biblio->{biblionumber});
+    $query->param( 'bib_id', $item->biblionumber);
 
     my $reply = C4::ILSDI::Services::HoldTitle( $query );
     is( $reply->{code}, 'damaged', "Item damaged" );
 
-    my $item_o = Koha::Items->find($item->{itemnumber});
-    $item_o->damaged(0)->store;
+    $item->damaged(0)->store;
 
     my $hold = $builder->build({
         source => 'Reserve',
         value => {
             borrowernumber => $patron->{borrowernumber},
-            biblionumber => $biblio->{biblionumber},
-            itemnumber => $item->{itemnumber}
+            biblionumber => $item->biblionumber,
+            itemnumber => $item->itemnumber
         }
     });
 
     $reply = C4::ILSDI::Services::HoldTitle( $query );
     is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
 
-    my $biblio_with_no_item = $builder->build({
-        source => 'Biblio',
-    });
+    my $biblio_with_no_item = $builder->build_sample_biblio;
 
-    $query = new CGI;
+    $query = CGI->new;
     $query->param( 'patron_id', $patron->{borrowernumber});
-    $query->param( 'bib_id', $biblio_with_no_item->{biblionumber});
+    $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
 
     $reply = C4::ILSDI::Services::HoldTitle( $query );
     is( $reply->{code}, 'NoItems', 'Biblio has no item' );
 
-    my $biblio2 = $builder->build({
-        source => 'Biblio',
-    });
-
-    my $biblioitems2 = $builder->build({
-        source => 'Biblioitem',
-        value => {
-            biblionumber => $biblio2->{biblionumber},
-        }
-    });
-
-    my $item2 = $builder->build({
-        source => 'Item',
-        value => {
-            biblionumber => $biblio2->{biblionumber},
-            damaged => 0
+    my $item2 = $builder->build_sample_item(
+        {
+            damaged => 0,
         }
-    });
+    );
 
     t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
-    my $issuingrule = $builder->build({
-        source => 'Issuingrule',
-        value => {
+    Koha::CirculationRules->set_rule(
+        {
             categorycode => $patron->{categorycode},
-            itemtype => $item2->{itype},
-            branchcode => $patron->{branchcode},
-            reservesallowed => 0,
+            itemtype     => $item2->{itype},
+            branchcode   => $patron->{branchcode},
+            rule_name    => 'reservesallowed',
+            rule_value   => 0,
         }
-    });
+    );
 
-    $query = new CGI;
+    $query = CGI->new;
     $query->param( 'patron_id', $patron->{borrowernumber});
-    $query->param( 'bib_id', $biblio2->{biblionumber});
-    $query->param( 'item_id', $item2->{itemnumber});
+    $query->param( 'bib_id', $item2->biblionumber);
+    $query->param( 'item_id', $item2->itemnumber);
 
     $reply = C4::ILSDI::Services::HoldItem( $query );
     is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
 
-    my $biblio3 = $builder->build({
-        source => 'Biblio',
-    });
-
-    my $biblioitems3 = $builder->build({
-        source => 'Biblioitem',
-        value => {
-            biblionumber => $biblio3->{biblionumber},
+    my $origin_branch = $builder->build(
+        {
+            source => 'Branch',
+            value  => {
+                pickup_location => 1,
+            }
         }
-    });
+    );
 
-    # Adding a holdable item to biblio 3.
-    my $item3 = $builder->build({
-        source => 'Item',
-        value => {
-            biblionumber => $biblio3->{biblionumber},
-            damaged => 0,
-        }
-    });
+    # Adding a holdable item.
+    my $item3 = $builder->build_sample_item(
+       {
+           barcode => '123456789',
+           library => $origin_branch->{branchcode}
+       });
 
-    my $item4 = $builder->build({
-        source => 'Item',
-        value => {
-            biblionumber => $biblio3->{biblionumber},
-            damaged => 1,
-        }
-    });
+    my $item4 = $builder->build_sample_item(
+        {
+           biblionumber => $item3->biblionumber,
+           damaged => 1,
+           library => $origin_branch->{branchcode}
+       });
 
-    my $issuingrule2 = $builder->build({
-        source => 'Issuingrule',
-        value => {
+    Koha::CirculationRules->set_rule(
+        {
             categorycode => $patron->{categorycode},
-            itemtype => $item3->{itype},
-            branchcode => $patron->{branchcode},
-            reservesallowed => 10,
+            itemtype     => $item3->{itype},
+            branchcode   => $patron->{branchcode},
+            rule_name    => 'reservesallowed',
+            rule_value   => 10,
         }
-    });
+    );
 
-    $query = new CGI;
+    $query = CGI->new;
     $query->param( 'patron_id', $patron->{borrowernumber});
-    $query->param( 'bib_id', $biblio3->{biblionumber});
-    $query->param( 'item_id', $item4->{itemnumber});
+    $query->param( 'bib_id', $item4->biblionumber);
+    $query->param( 'item_id', $item4->itemnumber);
 
     $reply = C4::ILSDI::Services::HoldItem( $query );
     is( $reply->{code}, 'damaged', "Item is damaged" );
 
+    my $module = Test::MockModule->new('C4::Context');
+    $module->mock('userenv', sub { { patron => $patron } });
+    my $issue = C4::Circulation::AddIssue($patron, $item3->barcode);
+    t::lib::Mocks::mock_preference( 'AllowHoldsOnPatronsPossessions', '0' );
+
+    $query = CGI->new;
+    $query->param( 'patron_id', $patron->{borrowernumber});
+    $query->param( 'bib_id', $item3->biblionumber);
+    $query->param( 'item_id', $item3->itemnumber);
+    $query->param( 'pickup_location', $origin_branch->{branchcode});
+    $reply = C4::ILSDI::Services::HoldItem( $query );
+
+    is( $reply->{code}, 'alreadypossession', "Patron has issued same book" );
+    is( $reply->{pickup_location}, undef, "No reserve placed");
+
+    # Test Patron cannot reserve if expired and BlockExpiredPatronOpacActions
+    my $category = $builder->build({
+        source => 'Category',
+        value => { BlockExpiredPatronOpacActions => -1 }
+        });
+
+    my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
+
+    my $expired_borrowernumber = Koha::Patron->new({
+        firstname =>  'Expired',
+        surname => 'Patron',
+        categorycode => $category->{categorycode},
+        branchcode => $branch_1,
+        dateexpiry => '2000-01-01',
+    })->store->borrowernumber;
+
+    t::lib::Mocks::mock_preference('BlockExpiredPatronOpacActions', 1);
+
+    my $item5 = $builder->build({
+        source => 'Item',
+        value => {
+            biblionumber => $biblio_with_no_item->biblionumber,
+            damaged => 0,
+        }
+    });
+
+    $query = CGI->new;
+    $query->param( 'patron_id', $expired_borrowernumber);
+    $query->param( 'bib_id', $biblio_with_no_item->biblionumber);
+    $query->param( 'item_id', $item5->{itemnumber});
+
+    $reply = C4::ILSDI::Services::HoldItem( $query );
+    is( $reply->{code}, 'PatronExpired', "Patron is expired" );
+
     $schema->storage->txn_rollback;
 };
 
 subtest 'Holds test for branch transfer limits' => sub {
 
-    plan tests => 4;
+    plan tests => 6;
 
     $schema->storage->txn_begin;
 
@@ -483,48 +495,33 @@ subtest 'Holds test for branch transfer limits' => sub {
         }
     );
 
-    my $biblio = $builder->build({
-        source => 'Biblio',
-    });
-    my $biblioitem = $builder->build({
-        source => 'Biblioitem',
-        value => {
-            biblionumber => $biblio->{biblionumber},
-        }
-    });
-    my $item = $builder->build({
-        source => 'Item',
-        value => {
-            homebranch => $origin_branch->{branchcode},
-            holdingbranch => $origin_branch->{branchcode},
-            biblionumber => $biblio->{biblionumber},
-            damaged => 0,
-            itemlost => 0,
+    my $item = $builder->build_sample_item(
+        {
+            library => $origin_branch->{branchcode},
         }
-    });
+    );
 
-    Koha::IssuingRules->search()->delete();
-    my $issuingrule = $builder->build({
-        source => 'Issuingrule',
-        value => {
-            categorycode => '*',
-            itemtype => '*',
-            branchcode => '*',
-            reservesallowed => 99,
+    Koha::CirculationRules->set_rule(
+        {
+            categorycode => undef,
+            itemtype     => undef,
+            branchcode   => undef,
+            rule_name    => 'reservesallowed',
+            rule_value   => 99,
         }
-    });
+    );
 
     my $limit = Koha::Item::Transfer::Limit->new({
         toBranch => $pickup_branch->{branchcode},
-        fromBranch => $item->{holdingbranch},
-        itemtype => $item->{itype},
+        fromBranch => $item->holdingbranch,
+        itemtype => $item->effective_itemtype,
     })->store();
 
-    my $query = new CGI;
+    my $query = CGI->new;
     $query->param( 'pickup_location', $pickup_branch->{branchcode} );
     $query->param( 'patron_id', $patron->{borrowernumber});
-    $query->param( 'bib_id', $biblio->{biblionumber});
-    $query->param( 'item_id', $item->{itemnumber});
+    $query->param( 'bib_id', $item->biblionumber);
+    $query->param( 'item_id', $item->itemnumber);
 
     my $reply = C4::ILSDI::Services::HoldItem( $query );
     is( $reply->{code}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
@@ -536,18 +533,80 @@ subtest 'Holds test for branch transfer limits' => sub {
 
     $reply = C4::ILSDI::Services::HoldItem( $query );
     is( $reply->{code}, undef, "Item hold, Item can be transferred" );
+    my $hold = Koha::Holds->search({ itemnumber => $item->itemnumber, borrowernumber => $patron->{borrowernumber} })->next;
+    is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
 
     Koha::Holds->search()->delete();
 
     $reply = C4::ILSDI::Services::HoldTitle( $query );
     is( $reply->{code}, undef, "Record hold, Item con be transferred" );
+    $hold = Koha::Holds->search({ biblionumber => $item->biblionumber, borrowernumber => $patron->{borrowernumber} })->next;
+    is( $hold->branchcode, $pickup_branch->{branchcode}, 'The library id is correctly set' );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'Holds test with start_date and end_date' => sub {
+
+    plan tests => 8;
+
+    $schema->storage->txn_begin;
+
+    my $pickup_library = $builder->build_object(
+        {
+            class  => 'Koha::Libraries',
+            value  => {
+                pickup_location => 1,
+            }
+        }
+    );
+
+    my $patron = $builder->build_object({
+        class => 'Koha::Patrons',
+    });
+
+    my $item = $builder->build_sample_item({ library => $pickup_library->branchcode });
+
+    Koha::CirculationRules->set_rule(
+        {
+            categorycode => undef,
+            itemtype     => undef,
+            branchcode   => undef,
+            rule_name    => 'reservesallowed',
+            rule_value   => 99,
+        }
+    );
+
+    my $query = CGI->new;
+    $query->param( 'pickup_location', $pickup_library->branchcode );
+    $query->param( 'patron_id', $patron->borrowernumber);
+    $query->param( 'bib_id', $item->biblionumber);
+    $query->param( 'item_id', $item->itemnumber);
+    $query->param( 'start_date', '2020-03-20');
+    $query->param( 'expiry_date', '2020-04-22');
+
+    my $reply = C4::ILSDI::Services::HoldItem( $query );
+    is ($reply->{pickup_location}, $pickup_library->branchname, "Item hold with date parameters was placed");
+    my $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
+    is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
+    is( $hold->reservedate, '2020-03-20', "Item hold has correct start date" );
+    is( $hold->expirationdate, '2020-04-22', "Item hold has correct end date" );
+
+    $hold->delete();
+
+    $reply = C4::ILSDI::Services::HoldTitle( $query );
+    is ($reply->{pickup_location}, $pickup_library->branchname, "Record hold with date parameters was placed");
+    $hold = Koha::Holds->search({ biblionumber => $item->biblionumber})->next();
+    is( $hold->biblionumber, $item->biblionumber, "correct biblionumber");
+    is( $hold->reservedate, '2020-03-20', "Record hold has correct start date" );
+    is( $hold->expirationdate, '2020-04-22', "Record hold has correct end date" );
 
     $schema->storage->txn_rollback;
 };
 
 subtest 'GetRecords' => sub {
 
-    plan tests => 1;
+    plan tests => 8;
 
     $schema->storage->txn_begin;
 
@@ -560,30 +619,35 @@ subtest 'GetRecords' => sub {
         source => 'Branch',
     });
 
-    my $biblio = $builder->build({
-        source => 'Biblio',
+    my $item = $builder->build_sample_item(
+        {
+            library => $branch1->{branchcode},
+        }
+    );
+
+    my $patron = $builder->build({
+        source => 'Borrower',
     });
-    my $biblioitem = $builder->build({
-        source => 'Biblioitem',
+
+    my $issue = $builder->build({
+        source => 'Issue',
         value => {
-            biblionumber => $biblio->{biblionumber},
-        },
+            itemnumber => $item->itemnumber,
+        }
     });
-    my $item = $builder->build_object({
-        class => 'Koha::Items',
+
+    my $hold = $builder->build({
+        source => 'Reserve',
         value => {
-            biblionumber => $biblio->{biblionumber},
-            biblioitemnumber => $biblioitem->{biblioitemnumber},
-            homebranch => $branch1->{branchcode},
-            holdingbranch => $branch1->{branchcode},
-        },
+            biblionumber => $item->biblionumber,
+        }
     });
 
     ModItemTransfer($item->itemnumber, $branch1->{branchcode}, $branch2->{branchcode});
 
-    my $cgi = new CGI;
+    my $cgi = CGI->new;
     $cgi->param(service => 'GetRecords');
-    $cgi->param(id => $biblio->{biblionumber});
+    $cgi->param(id => $item->biblionumber);
 
     my $reply = C4::ILSDI::Services::GetRecords($cgi);
 
@@ -596,6 +660,17 @@ subtest 'GetRecords' => sub {
     is_deeply($reply->{record}->[0]->{items}->{item}->[0]->{transfer}, $expected,
         'GetRecords returns transfer informations');
 
+    # Check informations exposed
+    my $reply_issue = $reply->{record}->[0]->{issues}->{issue}->[0];
+    is($reply_issue->{itemnumber}, $item->itemnumber, 'GetRecords has an issue tag');
+    is($reply_issue->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in issue tag');
+    is($reply_issue->{surname}, undef, 'GetRecords does not expose surname in issue tag');
+    is($reply_issue->{firstname}, undef, 'GetRecords does not expose firstname in issue tag');
+    is($reply_issue->{cardnumber}, undef, 'GetRecords does not expose cardnumber in issue tag');
+    my $reply_reserve = $reply->{record}->[0]->{reserves}->{reserve}->[0];
+    is($reply_reserve->{biblionumber}, $item->biblionumber, 'GetRecords has a reserve tag');
+    is($reply_reserve->{borrowernumber}, undef, 'GetRecords does not expose borrowernumber in reserve tag');
+
     $schema->storage->txn_rollback;
 };
 
@@ -604,9 +679,9 @@ subtest 'RenewHold' => sub {
 
     $schema->storage->txn_begin;
 
-    my $cgi    = new CGI;
+    my $cgi    = CGI->new;
     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
-    my $item   = $builder->build_object( { class => 'Koha::Items' } );
+    my $item   = $builder->build_sample_item;
     $cgi->param( patron_id => $patron->borrowernumber );
     $cgi->param( item_id   => $item->itemnumber );
 
@@ -652,9 +727,9 @@ subtest 'GetPatronInfo paginated loans' => sub {
             branchcode => $library->branchcode,
         },
     });
-    my $module = new Test::MockModule('C4::Context');
+    my $module = Test::MockModule->new('C4::Context');
     $module->mock('userenv', sub { { branch => $library->branchcode } });
-    my $date_due = DateTime->now->add(weeks => 2);
+    my $date_due = Koha::DateUtils::dt_from_string()->add(weeks => 2);
     my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
     my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
     my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
@@ -662,7 +737,7 @@ subtest 'GetPatronInfo paginated loans' => sub {
     my $issue3 = C4::Circulation::AddIssue($patron->unblessed, $item3->barcode, $date_due);
     my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
 
-    my $cgi = new CGI;
+    my $cgi = CGI->new;
 
     $cgi->param( 'service', 'GetPatronInfo' );
     $cgi->param( 'patron_id', $patron->borrowernumber );