Bug 21896: (QA follow-up) Document and Test for FIFO behaviour
[koha.git] / t / db_dependent / Holds / LocalHoldsPriority.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks;
6 use C4::Context;
7
8 use Test::More tests => 6;
9 use MARC::Record;
10
11 use Koha::Patrons;
12 use C4::Biblio;
13 use C4::Items;
14 use Koha::Database;
15
16 use t::lib::TestBuilder;
17
18 BEGIN {
19     use FindBin;
20     use lib $FindBin::Bin;
21     use_ok('C4::Reserves');
22 }
23
24 my $schema = Koha::Database->schema;
25 $schema->storage->txn_begin;
26
27 my $builder = t::lib::TestBuilder->new;
28
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 }
36     }
37 )->{itemtype};
38
39
40
41 my $borrowers_count = 5;
42
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},
49         itype         => $itemtype
50     },
51     $bibnum
52 );
53
54
55 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library3->{branchcode}, $library4->{branchcode} );
56
57 my $patron_category = $builder->build({ source => 'Category' });
58 # Create some borrowers
59 my @borrowernumbers;
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;
68 }
69
70 my $biblionumber = $bibnum;
71
72 # Create five item level holds
73 my $i = 1;
74 foreach my $borrowernumber (@borrowernumbers) {
75     AddReserve(
76         $branchcodes[$i],
77         $borrowernumber,
78         $biblionumber,
79         my $bibitems   = q{},
80         my $priority = $i,
81         my $resdate,
82         my $expdate,
83         my $notes = q{},
84         $title,
85         my $checkitem,
86         my $found,
87     );
88
89     $i++;
90 }
91
92 my ($status, $reserve, $all_reserves);
93
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" );
97
98 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
99
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" );
104
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" );
109
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" );
114
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" );
119
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';
124     $bib->append_fields(
125         MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
126         MARC::Field->new( '245', ' ', ' ', a => $title ),
127     );
128     return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' );
129 }