Bug 28412: Add information about supported authentication methods
[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 => 7;
9 use MARC::Record;
10
11 use Koha::Patrons;
12 use Koha::Holds;
13 use C4::Biblio;
14 use C4::Items;
15 use Koha::Database;
16
17 use t::lib::TestBuilder;
18
19 BEGIN {
20     use FindBin;
21     use lib $FindBin::Bin;
22     use_ok('C4::Reserves', qw( AddReserve CheckReserves ));
23 }
24
25 my $schema = Koha::Database->schema;
26 $schema->storage->txn_begin;
27
28 my $builder = t::lib::TestBuilder->new;
29
30 my $library1 = $builder->build({ source => 'Branch', });
31 my $library2 = $builder->build({ source => 'Branch', });
32 my $library3 = $builder->build({ source => 'Branch', });
33 my $library4 = $builder->build({ source => 'Branch', });
34 my $itemtype = $builder->build(
35     {   source => 'Itemtype',
36         value  => { notforloan => undef, rentalcharge => 0 }
37     }
38 )->{itemtype};
39
40
41
42 my $borrowers_count = 5;
43
44 my $biblio = $builder->build_sample_biblio();
45 my $itemnumber = Koha::Item->new(
46     {
47         biblionumber  => $biblio->biblionumber,
48         homebranch    => $library4->{branchcode},
49         holdingbranch => $library3->{branchcode},
50         itype         => $itemtype,
51         exclude_from_local_holds_priority => 0,
52     },
53 )->store->itemnumber;
54
55 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library3->{branchcode}, $library4->{branchcode} );
56 my $patron_category = $builder->build({ source => 'Category', value => {exclude_from_local_holds_priority => 0} });
57 # Create some borrowers
58 my @borrowernumbers;
59 foreach ( 0 .. $borrowers_count-1 ) {
60     my $borrowernumber = Koha::Patron->new({
61         firstname    => 'my firstname',
62         surname      => 'my surname ' . $_,
63         categorycode => $patron_category->{categorycode},
64         branchcode   => $branchcodes[$_],
65     })->store->borrowernumber;
66     push @borrowernumbers, $borrowernumber;
67 }
68
69 # Create five item level holds
70 my $i = 1;
71 foreach my $borrowernumber (@borrowernumbers) {
72     AddReserve(
73         {
74             branchcode     => $branchcodes[$i],
75             borrowernumber => $borrowernumber,
76             biblionumber   => $biblio->biblionumber,
77             priority       => $i,
78         }
79     );
80
81     $i++;
82 }
83
84 my ($status, $reserve, $all_reserves);
85
86 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 0 );
87 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
88 ok( $reserve->{borrowernumber} eq $borrowernumbers[0], "Received expected results with LocalHoldsPriority disabled" );
89
90 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
91
92 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
93 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
94 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
95 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with PickupLibrary/homebranch" );
96
97 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
98 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
99 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
100 ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with PickupLibrary/holdingbranch" );
101
102 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
103 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
104 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
105 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with HomeLibrary/holdingbranch" );
106
107 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
108 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
109 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
110 ok( $reserve->{borrowernumber} eq $borrowernumbers[3], "Received expected results with HomeLibrary/homebranch" );
111
112 $schema->storage->txn_rollback;
113
114 subtest "exclude from local holds" => sub {
115     plan tests => 3;
116
117     $schema->storage->txn_begin;
118
119     t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
120     t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
121     t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
122
123     my $category_ex = $builder->build_object({ class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 1} });
124     my $category_nex = $builder->build_object({ class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
125
126     my $lib1 = $builder->build_object({ class => 'Koha::Libraries' });
127     my $lib2 = $builder->build_object({ class => 'Koha::Libraries' });
128
129     my $item1 = $builder->build_sample_item({exclude_from_local_holds_priority => 0, homebranch => $lib1->branchcode});
130     my $item2 = $builder->build_sample_item({exclude_from_local_holds_priority => 1, homebranch => $lib1->branchcode});
131
132     my $patron_ex_l2 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib2->branchcode, categorycode => $category_ex->categorycode}});
133     my $patron_ex_l1 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib1->branchcode, categorycode => $category_ex->categorycode}});
134     my $patron_nex_l2 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib2->branchcode, categorycode => $category_nex->categorycode}});
135     my $patron_nex_l1 = $builder->build_object({ class => 'Koha::Patrons', value => {branchcode => $lib1->branchcode, categorycode => $category_nex->categorycode}});
136
137     AddReserve(
138         {
139             branchcode     => $patron_nex_l2->branchcode,
140             borrowernumber => $patron_nex_l2->borrowernumber,
141             biblionumber   => $item1->biblionumber,
142             priority       => 1,
143         }
144     );
145     AddReserve(
146         {
147             branchcode     => $patron_ex_l1->branchcode,
148             borrowernumber => $patron_ex_l1->borrowernumber,
149             biblionumber   => $item1->biblionumber,
150             priority       => 2,
151         }
152     );
153     AddReserve(
154         {
155             branchcode     => $patron_nex_l1->branchcode,
156             borrowernumber => $patron_nex_l1->borrowernumber,
157             biblionumber   => $item1->biblionumber,
158             priority       => 3,
159         }
160     );
161
162     my ($status, $reserve, $all_reserves);
163     ($status, $reserve, $all_reserves) = CheckReserves($item1->itemnumber);
164     is($reserve->{borrowernumber}, $patron_nex_l1->borrowernumber, "Patron not excluded with local holds priorities is next checkout");
165
166     Koha::Holds->delete;
167
168     AddReserve(
169         {
170             branchcode     => $patron_nex_l2->branchcode,
171             borrowernumber => $patron_nex_l2->borrowernumber,
172             biblionumber   => $item1->biblionumber,
173             priority       => 1,
174         }
175     );
176     AddReserve(
177         {
178             branchcode     => $patron_ex_l1->branchcode,
179             borrowernumber => $patron_ex_l1->borrowernumber,
180             biblionumber   => $item1->biblionumber,
181             priority       => 2,
182         }
183     );
184
185     ($status, $reserve, $all_reserves) = CheckReserves($item1->itemnumber);
186     is($reserve->{borrowernumber}, $patron_nex_l2->borrowernumber, "Local patron is excluded from priority");
187
188     Koha::Holds->delete;
189
190     AddReserve(
191         {
192             branchcode     => $patron_nex_l2->branchcode,
193             borrowernumber => $patron_nex_l2->borrowernumber,
194             biblionumber   => $item2->biblionumber,
195             priority       => 1,
196         }
197     );
198     AddReserve(
199         {
200             branchcode     => $patron_nex_l1->branchcode,
201             borrowernumber => $patron_nex_l1->borrowernumber,
202             biblionumber   => $item2->biblionumber,
203             priority       => 2,
204         }
205     );
206
207     ($status, $reserve, $all_reserves) = CheckReserves($item2->itemnumber);
208     is($reserve->{borrowernumber}, $patron_nex_l2->borrowernumber, "Patron from other library is next checkout because item is excluded");
209
210     $schema->storage->txn_rollback;
211 };