Bug 15956: New unit test SIP/Message.t
[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 use C4::Branch;
8
9 use Test::More tests => 6;
10 use MARC::Record;
11 use C4::Biblio;
12 use C4::Items;
13 use C4::Members;
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({
30     source => 'Branch',
31 });
32 my $library2 = $builder->build({
33     source => 'Branch',
34 });
35 my $library3 = $builder->build({
36     source => 'Branch',
37 });
38 my $library4 = $builder->build({
39     source => 'Branch',
40 });
41
42 my $borrowers_count = 5;
43
44 # Create a helper biblio
45 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio();
46 # Create a helper item for the biblio.
47 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
48   AddItem( { homebranch => $library4->{branchcode}, holdingbranch => $library3->{branchcode} }, $bibnum );
49
50 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode}, $library4->{branchcode}, $library3->{branchcode}, $library4->{branchcode} );
51
52 # Create some borrowers
53 my @borrowernumbers;
54 foreach ( 1 .. $borrowers_count ) {
55     my $borrowernumber = AddMember(
56         firstname    => 'my firstname',
57         surname      => 'my surname ' . $_,
58         categorycode => 'S',
59         branchcode   => $branchcodes[$_],
60     );
61     push @borrowernumbers, $borrowernumber;
62 }
63
64 my $biblionumber = $bibnum;
65
66 my @branches = GetBranchesLoop();
67 my $branch   = $branches[0][0]{value};
68
69 # Create five item level holds
70 my $i = 1;
71 foreach my $borrowernumber (@borrowernumbers) {
72     AddReserve(
73         $branchcodes[$i],
74         $borrowernumber,
75         $biblionumber,
76         my $bibitems   = q{},
77         my $priority = $i,
78         my $resdate,
79         my $expdate,
80         my $notes = q{},
81         $title,
82         my $checkitem,
83         my $found,
84     );
85
86     $i++;
87 }
88
89 my ($status, $reserve, $all_reserves);
90
91 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 0 );
92 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
93 ok( $reserve->{borrowernumber} eq $borrowernumbers[0], "Received expected results with LocalHoldsPriority disabled" );
94
95 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
96
97 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
98 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
99 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
100 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with PickupLibrary/homebranch" );
101
102 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
103 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
104 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
105 ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with PickupLibrary/holdingbranch" );
106
107 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
108 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
109 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
110 ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with HomeLibrary/holdingbranch" );
111
112 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
113 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
114 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
115 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with HomeLibrary/homebranch" );
116
117 # Helper method to set up a Biblio.
118 sub create_helper_biblio {
119     my $bib   = MARC::Record->new();
120     my $title = 'Silence in the library';
121     $bib->append_fields(
122         MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
123         MARC::Field->new( '245', ' ', ' ', a => $title ),
124     );
125     return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' );
126 }