Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Holds / LocalHoldsPriority.t
index ef40686..a125d3e 100755 (executable)
@@ -5,11 +5,13 @@ use Modern::Perl;
 use t::lib::Mocks;
 use C4::Context;
 
-use Test::More tests => 6;
+use Test::More tests => 7;
 use MARC::Record;
+
+use Koha::Patrons;
+use Koha::Holds;
 use C4::Biblio;
 use C4::Items;
-use C4::Members;
 use Koha::Database;
 
 use t::lib::TestBuilder;
@@ -17,7 +19,7 @@ use t::lib::TestBuilder;
 BEGIN {
     use FindBin;
     use lib $FindBin::Bin;
-    use_ok('C4::Reserves');
+    use_ok('C4::Reserves', qw( AddReserve CheckReserves ));
 }
 
 my $schema = Koha::Database->schema;
@@ -39,49 +41,41 @@ my $itemtype = $builder->build(
 
 my $borrowers_count = 5;
 
-# Create a helper biblio
-my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio();
-# Create a helper item for the biblio.
-my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
-    {   homebranch    => $library4->{branchcode},
+my $biblio = $builder->build_sample_biblio();
+my $itemnumber = Koha::Item->new(
+    {
+        biblionumber  => $biblio->biblionumber,
+        homebranch    => $library4->{branchcode},
         holdingbranch => $library3->{branchcode},
-        itype         => $itemtype
+        itype         => $itemtype,
+        exclude_from_local_holds_priority => 0,
     },
-    $bibnum
-);
-
+)->store->itemnumber;
 
 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library3->{branchcode}, $library4->{branchcode} );
-
+my $patron_category = $builder->build({ source => 'Category', value => {exclude_from_local_holds_priority => 0} });
 # Create some borrowers
 my @borrowernumbers;
-foreach ( 1 .. $borrowers_count ) {
-    my $borrowernumber = AddMember(
+foreach ( 0 .. $borrowers_count-1 ) {
+    my $borrowernumber = Koha::Patron->new({
         firstname    => 'my firstname',
         surname      => 'my surname ' . $_,
-        categorycode => 'S',
+        categorycode => $patron_category->{categorycode},
         branchcode   => $branchcodes[$_],
-    );
+    })->store->borrowernumber;
     push @borrowernumbers, $borrowernumber;
 }
 
-my $biblionumber = $bibnum;
-
 # Create five item level holds
 my $i = 1;
 foreach my $borrowernumber (@borrowernumbers) {
     AddReserve(
-        $branchcodes[$i],
-        $borrowernumber,
-        $biblionumber,
-        my $bibitems   = q{},
-        my $priority = $i,
-        my $resdate,
-        my $expdate,
-        my $notes = q{},
-        $title,
-        my $checkitem,
-        my $found,
+        {
+            branchcode     => $branchcodes[$i],
+            borrowernumber => $borrowernumber,
+            biblionumber   => $biblio->biblionumber,
+            priority       => $i,
+        }
     );
 
     $i++;
@@ -108,20 +102,110 @@ ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected result
 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
-ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with HomeLibrary/holdingbranch" );
+ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with HomeLibrary/holdingbranch" );
 
 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
-ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with HomeLibrary/homebranch" );
-
-# Helper method to set up a Biblio.
-sub create_helper_biblio {
-    my $bib   = MARC::Record->new();
-    my $title = 'Silence in the library';
-    $bib->append_fields(
-        MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
-        MARC::Field->new( '245', ' ', ' ', a => $title ),
+ok( $reserve->{borrowernumber} eq $borrowernumbers[3], "Received expected results with HomeLibrary/homebranch" );
+
+$schema->storage->txn_rollback;
+
+subtest "exclude from local holds" => sub {
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
+
+    my $category_ex = $builder->build_object({ class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 1} });
+    my $category_nex = $builder->build_object({ class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+
+    my $lib1 = $builder->build_object({ class => 'Koha::Libraries' });
+    my $lib2 = $builder->build_object({ class => 'Koha::Libraries' });
+
+    my $item1 = $builder->build_sample_item({exclude_from_local_holds_priority => 0, homebranch => $lib1->branchcode});
+    my $item2 = $builder->build_sample_item({exclude_from_local_holds_priority => 1, homebranch => $lib1->branchcode});
+
+    my $patron_ex_l2 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib2->branchcode, categorycode => $category_ex->categorycode}});
+    my $patron_ex_l1 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib1->branchcode, categorycode => $category_ex->categorycode}});
+    my $patron_nex_l2 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib2->branchcode, categorycode => $category_nex->categorycode}});
+    my $patron_nex_l1 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib1->branchcode, categorycode => $category_nex->categorycode}});
+
+    AddReserve(
+        {
+            branchcode     => $patron_nex_l2->branchcode,
+            borrowernumber => $patron_nex_l2->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 1,
+        }
     );
-    return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' );
-}
+    AddReserve(
+        {
+            branchcode     => $patron_ex_l1->branchcode,
+            borrowernumber => $patron_ex_l1->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 2,
+        }
+    );
+    AddReserve(
+        {
+            branchcode     => $patron_nex_l1->branchcode,
+            borrowernumber => $patron_nex_l1->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 3,
+        }
+    );
+
+    my ($status, $reserve, $all_reserves);
+    ($status, $reserve, $all_reserves) = CheckReserves($item1->itemnumber);
+    is($reserve->{borrowernumber}, $patron_nex_l1->borrowernumber, "Patron not excluded with local holds priorities is next checkout");
+
+    Koha::Holds->delete;
+
+    AddReserve(
+        {
+            branchcode     => $patron_nex_l2->branchcode,
+            borrowernumber => $patron_nex_l2->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 1,
+        }
+    );
+    AddReserve(
+        {
+            branchcode     => $patron_ex_l1->branchcode,
+            borrowernumber => $patron_ex_l1->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 2,
+        }
+    );
+
+    ($status, $reserve, $all_reserves) = CheckReserves($item1->itemnumber);
+    is($reserve->{borrowernumber}, $patron_nex_l2->borrowernumber, "Local patron is excluded from priority");
+
+    Koha::Holds->delete;
+
+    AddReserve(
+        {
+            branchcode     => $patron_nex_l2->branchcode,
+            borrowernumber => $patron_nex_l2->borrowernumber,
+            biblionumber   => $item2->biblionumber,
+            priority       => 1,
+        }
+    );
+    AddReserve(
+        {
+            branchcode     => $patron_nex_l1->branchcode,
+            borrowernumber => $patron_nex_l1->borrowernumber,
+            biblionumber   => $item2->biblionumber,
+            priority       => 2,
+        }
+    );
+
+    ($status, $reserve, $all_reserves) = CheckReserves($item2->itemnumber);
+    is($reserve->{borrowernumber}, $patron_nex_l2->borrowernumber, "Patron from other library is next checkout because item is excluded");
+
+    $schema->storage->txn_rollback;
+};