8 use Test::More tests => 6;
16 use t::lib::TestBuilder;
20 use lib $FindBin::Bin;
21 use_ok('C4::Reserves');
24 my $schema = Koha::Database->schema;
25 $schema->storage->txn_begin;
27 my $builder = t::lib::TestBuilder->new;
29 my $library1 = $builder->build({ source => 'Branch', });
30 my $library2 = $builder->build({ source => 'Branch', });
31 my $library3 = $builder->build({ source => 'Branch', });
32 my $library4 = $builder->build({ source => 'Branch', });
33 my $itemtype = $builder->build(
34 { source => 'Itemtype',
35 value => { notforloan => undef, rentalcharge => 0 }
41 my $borrowers_count = 5;
43 # Create a helper biblio
44 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio();
45 # Create a helper item for the biblio.
46 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = AddItem(
47 { homebranch => $library4->{branchcode},
48 holdingbranch => $library3->{branchcode},
55 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library3->{branchcode}, $library4->{branchcode} );
57 my $patron_category = $builder->build({ source => 'Category' });
58 # Create some borrowers
60 foreach ( 1 .. $borrowers_count ) {
61 my $borrowernumber = Koha::Patron->new({
62 firstname => 'my firstname',
63 surname => 'my surname ' . $_,
64 categorycode => $patron_category->{categorycode},
65 branchcode => $branchcodes[$_],
66 })->store->borrowernumber;
67 push @borrowernumbers, $borrowernumber;
70 my $biblionumber = $bibnum;
72 # Create five item level holds
74 foreach my $borrowernumber (@borrowernumbers) {
92 my ($status, $reserve, $all_reserves);
94 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 0 );
95 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
96 ok( $reserve->{borrowernumber} eq $borrowernumbers[0], "Received expected results with LocalHoldsPriority disabled" );
98 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
100 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
101 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
102 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
103 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with PickupLibrary/homebranch" );
105 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
106 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
107 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
108 ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with PickupLibrary/holdingbranch" );
110 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
111 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
112 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
113 ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with HomeLibrary/holdingbranch" );
115 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
116 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
117 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
118 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with HomeLibrary/homebranch" );
120 # Helper method to set up a Biblio.
121 sub create_helper_biblio {
122 my $bib = MARC::Record->new();
123 my $title = 'Silence in the library';
125 MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
126 MARC::Field->new( '245', ' ', ' ', a => $title ),
128 return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' );