Bug 18928: (follow-up) Make DB update idempotent
[koha.git] / t / db_dependent / ShelfBrowser.t
blobf4dd9b9d78ea9ae4d93b7b2641c61b0e6197bcb3
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 74;
5 use List::Util qw( shuffle );
6 use MARC::Field;
7 use MARC::Record;
9 use C4::Context;
10 use C4::Items;
11 use C4::Biblio;
12 use Koha::Database;
14 use t::lib::TestBuilder;
16 use_ok('C4::ShelfBrowser');
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $builder = t::lib::TestBuilder->new;
21 my $dbh = C4::Context->dbh;
22 $dbh->{RaiseError} = 1;
24 $dbh->do(q|DELETE FROM reserves|);
25 $dbh->do(q|DELETE FROM issues|);
26 $dbh->do(q|DELETE FROM items|);
28 my $library = $builder->build({
29 source => 'Branch',
30 });
32 my $cn;
34 # 100.100 150.100 200.100 210.100 300.000 320.000 400.100 410.100 500.100 510.100 520.100 600.000 610.000 700.100 710.100 720.100 730.100 740.100 750.100
35 my @callnumbers = qw(
36 100.100
37 150.100
38 200.100
39 210.100
40 300.000
41 320.000
42 400.100
43 410.100
44 500.100
45 510.100
46 520.100
47 600.000
48 610.000
49 700.100
50 710.100
51 720.100
52 730.100
53 740.100
54 750.100
57 my $record = MARC::Record->new();
58 $record->append_fields(
59 MARC::Field->new('100', ' ', ' ', a => 'Donald E. Knuth.'),
60 MARC::Field->new('245', ' ', ' ', a => 'The art of computer programming'),
62 my ( $biblionumber ) = C4::Biblio::AddBiblio($record, '');
64 for my $callnumber ( shuffle @callnumbers ) {
65 my ( $biblionumber, undef, $itemnumber ) = C4::Items::AddItem({
66 homebranch => $library->{branchcode},
67 holdingbranch => $library->{branchcode},
68 itemcallnumber => $callnumber,
69 }, $biblionumber);
70 $cn->{$callnumber} = {
71 biblionumber => $biblionumber,
72 itemnumber => $itemnumber,
73 itemcallnumber => $callnumber,
77 my $nearby;
79 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'500.100'}{itemnumber} );
80 # We have
81 # < 320.000 400.100 410.100 500.100 510.100 520.100 600.000 >
82 # 6 7 8 [9] 10 11 12
83 # Clicking on previous, we want a link to 150.100
84 is( $nearby->{prev_item}{itemcallnumber}, '150.100', "Simple case: previous link 1/2" );
85 is( $nearby->{prev_item}{itemnumber}, $cn->{'150.100'}{itemnumber}, "Simple case: previous link 2/2" );
86 # Clicking on next, we want a link to 730.100
87 is( $nearby->{next_item}{itemcallnumber}, '720.100', "Simple case: next link 1/2" );
88 is( $nearby->{next_item}{itemnumber}, $cn->{'720.100'}{itemnumber}, "Simple case: next link 2/2" );
90 is( $nearby->{items}[0]{itemcallnumber}, '320.000', "Simple case: item 1");
91 is( $nearby->{items}[1]{itemcallnumber}, '400.100', "Simple case: item 2");
92 is( $nearby->{items}[2]{itemcallnumber}, '410.100', "Simple case: item 3");
93 is( $nearby->{items}[3]{itemcallnumber}, '500.100', "Simple case: item 4");
94 is( $nearby->{items}[4]{itemcallnumber}, '510.100', "Simple case: item 5");
95 is( $nearby->{items}[5]{itemcallnumber}, '520.100', "Simple case: item 6");
96 is( $nearby->{items}[6]{itemcallnumber}, '600.000', "Simple case: item 7");
98 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'500.100'}{itemnumber}, 2, 3 );
99 # We have
100 # < 400.100 410.100 500.100 510.100 520.100 >
101 # 7 8 [9] 10 11
102 # Clicking on previous, we want a link to 320.000
103 is( $nearby->{prev_item}{itemcallnumber}, '320.000', "Test gap: previous link 1/2" );
104 is( $nearby->{prev_item}{itemnumber}, $cn->{'320.000'}{itemnumber}, "Test gap: previous link 2/2" );
105 # Clicking on next, we want a link to 600.000
106 is( $nearby->{next_item}{itemcallnumber}, '600.000', "Test gap: next link 1/2" );
107 is( $nearby->{next_item}{itemnumber}, $cn->{'600.000'}{itemnumber}, "Test gap: next link 2/2" );
109 is( scalar( @{$nearby->{items}} ), 5, "Test gap: got 5 items" );
110 is( $nearby->{items}[0]{itemcallnumber}, '400.100', "Test gap: item 1");
111 is( $nearby->{items}[1]{itemcallnumber}, '410.100', "Test gap: item 2");
112 is( $nearby->{items}[2]{itemcallnumber}, '500.100', "Test gap: item 3");
113 is( $nearby->{items}[3]{itemcallnumber}, '510.100', "Test gap: item 4");
114 is( $nearby->{items}[4]{itemcallnumber}, '520.100', "Test gap: item 5");
116 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'300.000'}{itemnumber} );
117 # We have
118 # < 150.100 200.100 210.100 300.000 320.000 400.100 410.100 >
119 # 2 3 4 [5] 6 7 8
120 # Clicking on previous, we want a link to 100.100
121 is( $nearby->{prev_item}{itemcallnumber}, '100.100', "Test start shelf: previous link 1/2" );
122 is( $nearby->{prev_item}{itemnumber}, $cn->{'100.100'}{itemnumber}, "Test start shelf: previous link 2/2" );
123 # Clicking on next, we want a link to 600.000
124 is( $nearby->{next_item}{itemcallnumber}, '600.000', "Test start shelf: next link 1/2" );
125 is( $nearby->{next_item}{itemnumber}, $cn->{'600.000'}{itemnumber}, "Test start shelf: next link 2/2" );
127 is( $nearby->{items}[0]{itemcallnumber}, '150.100', "Test start shelf: item 1");
128 is( $nearby->{items}[1]{itemcallnumber}, '200.100', "Test start shelf: item 2");
129 is( $nearby->{items}[2]{itemcallnumber}, '210.100', "Test start shelf: item 3");
130 is( $nearby->{items}[3]{itemcallnumber}, '300.000', "Test start shelf: item 4");
131 is( $nearby->{items}[4]{itemcallnumber}, '320.000', "Test start shelf: item 5");
132 is( $nearby->{items}[5]{itemcallnumber}, '400.100', "Test start shelf: item 6");
133 is( $nearby->{items}[6]{itemcallnumber}, '410.100', "Test start shelf: item 7");
137 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'100.100'}{itemnumber} );
138 # We have
139 # 100.100 150.100 200.100 210.100 >
140 # [1] 2 3 4
141 # There is no previous link
142 is( $nearby->{prev_item}, undef, "Test first item on a shelf: no previous link" );
143 # Clicking on next, we want a link to 410.100
144 is( $nearby->{next_item}{itemcallnumber}, '410.100', "Test first item on a shelf: next link 1/2" );
145 is( $nearby->{next_item}{itemnumber}, $cn->{'410.100'}{itemnumber}, "Test first item on a shelf: next link 2/2" );
147 is( scalar( @{$nearby->{items}} ), 4, "Test first item on a shelf: There are 4 items displayed" );
148 is( $nearby->{items}[0]{itemcallnumber}, '100.100', "Test first item on a shelf: item 1");
149 is( $nearby->{items}[1]{itemcallnumber}, '150.100', "Test first item on a shelf: item 2");
150 is( $nearby->{items}[2]{itemcallnumber}, '200.100', "Test first item on a shelf: item 3");
151 is( $nearby->{items}[3]{itemcallnumber}, '210.100', "Test first item on a shelf: item 4");
154 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'150.100'}{itemnumber} );
155 # We have
156 # 100.100 150.100 200.100 210.100 300.000 >
157 # 1 [2] 3 4 5
158 # There is no previous link
159 is( $nearby->{prev_item}, undef, "Test second item on a shelf: no previous link" );
160 # Clicking on next, we want a link to 500.100
161 is( $nearby->{next_item}{itemcallnumber}, '500.100', "Test second item on a shelf: next link 1/2" );
162 is( $nearby->{next_item}{itemnumber}, $cn->{'500.100'}{itemnumber}, "Test second item on a shelf: next link 2/2" );
164 is( scalar( @{$nearby->{items}} ), 5, "Test second item on a shelf: got 5 items" );
165 is( $nearby->{items}[0]{itemcallnumber}, '100.100', "Test second item on a shelf: item 1");
166 is( $nearby->{items}[1]{itemcallnumber}, '150.100', "Test second item on a shelf: item 2");
167 is( $nearby->{items}[2]{itemcallnumber}, '200.100', "Test second item on a shelf: item 3");
168 is( $nearby->{items}[3]{itemcallnumber}, '210.100', "Test second item on a shelf: item 4");
169 is( $nearby->{items}[4]{itemcallnumber}, '300.000', "Test second item on a shelf: item 5");
172 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'710.100'}{itemnumber} );
173 # We have
174 # < 600.000 610.000 700.100 710.100 720.100 730.100 740.100 >
175 # 12 13 14 [15] 16 17 18
176 # Clicking on previous, we want a link to 410.100
177 is( $nearby->{prev_item}{itemcallnumber}, '410.100', "Test end shelf: previous link 1/2" );
178 is( $nearby->{prev_item}{itemnumber}, $cn->{'410.100'}{itemnumber}, "Test end shelf: previous link 2/2" );
179 # Clicking on next, we want a link to 730.100
180 is( $nearby->{next_item}{itemcallnumber}, '750.100', "Test end shelf: next link is a link to the last item 1/2" );
181 is( $nearby->{next_item}{itemnumber}, $cn->{'750.100'}{itemnumber}, "Test end shelf: next link is a link to the last item 2/2" );
183 is( $nearby->{items}[0]{itemcallnumber}, '600.000', "Test end shelf: item 1");
184 is( $nearby->{items}[1]{itemcallnumber}, '610.000', "Test end shelf: item 2");
185 is( $nearby->{items}[2]{itemcallnumber}, '700.100', "Test end shelf: item 3");
186 is( $nearby->{items}[3]{itemcallnumber}, '710.100', "Test end shelf: item 4");
187 is( $nearby->{items}[4]{itemcallnumber}, '720.100', "Test end shelf: item 5");
188 is( $nearby->{items}[5]{itemcallnumber}, '730.100', "Test end shelf: item 6");
189 is( $nearby->{items}[6]{itemcallnumber}, '740.100', "Test end shelf: item 7");
192 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'740.100'}{itemnumber} );
193 # We have
194 # < 710.100 720.100 730.100 740.100 750.100
195 # 15 16 17 [18] 19
196 # Clicking on previous, we want a link to
197 is( $nearby->{prev_item}{itemcallnumber}, '520.100', "Test end of the shelf: previous link 1/2" );
198 is( $nearby->{prev_item}{itemnumber}, $cn->{'520.100'}{itemnumber}, "Test end of the shelf: previous link 2/2" );
199 # No next link
200 is( $nearby->{next_item}, undef, "Test end of the shelf: no next link" );
202 is( scalar( @{$nearby->{items}} ), 5, "Test end of the shelf: got 5 items" );
203 is( $nearby->{items}[0]{itemcallnumber}, '710.100', "Test end of the shelf: item 1");
204 is( $nearby->{items}[1]{itemcallnumber}, '720.100', "Test end of the shelf: item 2");
205 is( $nearby->{items}[2]{itemcallnumber}, '730.100', "Test end of the shelf: item 3");
206 is( $nearby->{items}[3]{itemcallnumber}, '740.100', "Test end of the shelf: item 4");
207 is( $nearby->{items}[4]{itemcallnumber}, '750.100', "Test end of the shelf: item 5");
209 $nearby = C4::ShelfBrowser::GetNearbyItems( $cn->{'750.100'}{itemnumber} );
210 # We have
211 # < 720.100 730.100 740.100 750.100
212 # 16 17 18 [19]
213 # Clicking on previous, we want a link to
214 is( $nearby->{prev_item}{itemcallnumber}, '600.000', "Test last item of the shelf: previous link 1/2" );
215 is( $nearby->{prev_item}{itemnumber}, $cn->{'600.000'}{itemnumber}, "Test last item of the shelf: previous link 2/2" );
216 # No next link
217 is( $nearby->{next_item}, undef, "Test end of the shelf: no next link" );
219 is( scalar( @{$nearby->{items}} ), 4, "Test last item of the shelf: got 4 items" );