Bug 17709 - Article request broken - 'internal server error'
[koha.git] / t / db_dependent / Barcodes.t
blob97c6e3e2df1929adc76fbb77a5aa06f0d6666ab3
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Test::More tests => 75;
21 use Test::Warn;
22 use Test::MockModule;
23 use t::lib::TestBuilder;
25 use Koha::Database;
27 $| = 1;
29 BEGIN {
30 use FindBin;
31 use lib $FindBin::Bin;
32 use_ok('C4::Barcodes');
35 my $builder = t::lib::TestBuilder->new;
37 my $schema = Koha::Database->new->schema;
39 subtest 'Test generation of annual barcodes from DB values' => sub {
41 plan tests => 4;
43 $schema->storage->txn_begin;
44 $builder->schema->resultset( 'Issue' )->delete_all;
45 $builder->schema->resultset( 'Item' )->delete_all;
47 my $barcodeobj;
49 warning_like { $barcodeobj = C4::Barcodes->new('annual'); } [ qr/No max barcode (.*) found\. Using initial value\./ ], "(annual) Expected complaint regarding no max barcode found";
51 my $barcodevalue = $barcodeobj->value();
53 my $item_1 = $builder->build({
54 source => 'Item',
55 value => {
56 barcode => $barcodevalue
58 });
60 is($barcodevalue,$barcodeobj->db_max(), "(annual) First barcode saved to db is equal to db_max" );
62 #This is just setting the value ahead an arbitrary amount before adding a second barcode to db
63 $barcodevalue = $barcodeobj->next_value();
64 $barcodevalue = $barcodeobj->next_value($barcodevalue);
65 $barcodevalue = $barcodeobj->next_value($barcodevalue);
66 $barcodevalue = $barcodeobj->next_value($barcodevalue);
67 $barcodevalue = $barcodeobj->next_value($barcodevalue);
69 my $item_2 = $builder->build({
70 source => 'Item',
71 value => {
72 barcode => $barcodevalue
74 });
76 $barcodeobj = C4::Barcodes->new('annual');
78 is($barcodevalue,$barcodeobj->db_max(), '(annual) db_max should equal the greatest barcode in the db when more than 1 present');
79 ok($barcodeobj->value() gt $barcodevalue, '(annual) new barcode object should be created with value greater and last value inserted into db');
81 $schema->storage->txn_rollback;
84 subtest 'Test generation of hbyymmincr barcodes from DB values' => sub {
86 plan tests => 5;
88 $schema->storage->txn_begin;
89 $builder->schema->resultset( 'Issue' )->delete_all;
90 $builder->schema->resultset( 'Item' )->delete_all;
92 my $branchcode_1 = "LETTERS"; #NOTE: This barcode type supports only letters in the branchcode
93 my $barcodeobj;
95 warnings_are { $barcodeobj = C4::Barcodes->new('hbyymmincr'); }
96 [ "HBYYMM Barcode created with no branchcode, default is blank",
97 "No existing hbyymmincr barcodes found. Reverting to initial value.",
98 "HBYYMM Barcode was not passed a branch, default is blank"]
99 , "(hbyymmincr) Expected complaint regarding no max barcode found";
102 warning_like { $barcodeobj = C4::Barcodes->new('hbyymmincr',$branchcode_1); }
103 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./],
104 "(hbyymmincr) Expected complaint regarding no max barcode found";
106 my $barcodevalue = $barcodeobj->value();
108 my $item_1 = $builder->build({
109 source => 'Item',
110 value => {
111 barcode => $barcodevalue
115 is($barcodevalue,$barcodeobj->db_max(), "(hbyymmincr) First barcode saved to db is equal to db_max" );
117 #This is just setting the value ahead an arbitrary amount before adding a second barcode to db
118 $barcodevalue = $barcodeobj->next_value();
119 $barcodevalue = $barcodeobj->next_value($barcodevalue);
120 $barcodevalue = $barcodeobj->next_value($barcodevalue);
121 $barcodevalue = $barcodeobj->next_value($barcodevalue);
122 $barcodevalue = $barcodeobj->next_value($barcodevalue);
124 my $item_2 = $builder->build({
125 source => 'Item',
126 value => {
127 barcode => $barcodevalue
131 $barcodeobj = C4::Barcodes->new('hbyymmincr',$branchcode_1);
133 is($barcodevalue,$barcodeobj->db_max(), '(hbyymmincr) db_max should equal the greatest barcode in the db when more than 1 present');
134 ok($barcodeobj->value() gt $barcodevalue, '(hbyymmincr) new barcode object should be created with value greater and last value inserted into db');
136 $schema->storage->txn_rollback;
140 $schema->storage->txn_begin;
142 $builder->schema->resultset( 'Issue' )->delete_all;
143 $builder->schema->resultset( 'Item' )->delete_all;
145 my %thash = (
146 incremental => [],
147 annual => [],
148 hbyymmincr => ['MAIN'],
149 EAN13 => ['0000000695152','892685001928'],
152 my ($obj1,$obj2,$format,$value,$initial,$serial,$re,$next,$previous,$temp);
153 my @formats = sort keys %thash;
154 foreach (@formats) {
155 my $pre = sprintf '(%-12s)', $_;
156 if ($_ eq 'EAN13') {
157 warning_like { $obj1 = C4::Barcodes->new($_); }
158 [ qr/not valid EAN-13 barcode/ ],
159 "$pre Expected complaint regarding $_ not being a valid EAN-13 barcode";
161 elsif ($_ eq 'annual') {
162 warning_like { $obj1 = C4::Barcodes->new($_); }
163 [ qr/No max barcode (.*) found\. Using initial value\./ ],
164 "$pre Expected complaint regarding no max barcode found";
166 elsif ($_ eq 'hbyymmincr') {
167 warnings_are { $obj1 = C4::Barcodes->new($_); }
168 [ "HBYYMM Barcode created with no branchcode, default is blank",
169 "No existing hbyymmincr barcodes found. Reverting to initial value.",
170 "HBYYMM Barcode was not passed a branch, default is blank"]
171 , "$pre Expected complaint regarding no max barcode found";
173 elsif ($_ eq 'incremental') {
174 $obj1 = C4::Barcodes->new($_);
176 else {
177 die "This should not happen! ($_)\n";
179 ok($obj1, "$pre Barcode Creation : new($_)");
180 SKIP: {
181 skip "No Object Returned by new($_)", 17 unless $obj1;
182 ok($_ eq ($format = $obj1->autoBarcode()), "$pre autoBarcode() : " . ($format || 'FAILED') );
183 if ($_ eq 'hbyymmincr') {
184 warning_like { $initial= $obj1->initial(); }
185 [ qr/HBYYMM Barcode was not passed a branch, default is blank/ ]
186 ,"$pre Expected complaint regarding no branch passed when getting initial\n $pre initial() : " . $initial ;
187 warnings_are { $temp = $obj1->db_max(); }
188 [ "No existing hbyymmincr barcodes found. Reverting to initial value.",
189 "HBYYMM Barcode was not passed a branch, default is blank"]
190 ,"$pre Expected complaint regarding no hbyymmincr barcodes found";
192 else {
193 ok($initial= $obj1->initial(), "$pre initial() : " . ($initial|| 'FAILED') );
194 $temp = $obj1->db_max();
196 ok($temp = $obj1->max(), "$pre max() : " . ($temp || 'FAILED') );
197 ok($value = $obj1->value(), "$pre value() : " . ($value || 'FAILED') );
198 ok($serial = $obj1->serial(), "$pre serial() : " . ($serial || 'FAILED') );
199 ok($temp = $obj1->is_max(), "$pre obj1->is_max() [obj1 should currently be max]");
200 if ($_ eq 'hbyymmincr') {
201 warning_like { $obj2 = $obj1->new(); }
202 [ qr/HBYYMM Barcode created with no branchcode, default is blank/ ]
203 ,"$pre Expected complaint regarding no branch passed when getting initial\n $pre Barcode Creation : obj2 = obj1->new()" ;
204 } else {
205 ok($obj2 = $obj1->new(), "$pre Barcode Creation : obj2 = obj1->new()");
207 ok(not($obj1->is_max()), "$pre obj1->is_max() [obj1 should no longer be max]");
208 ok( $obj2->is_max(), "$pre obj2->is_max() [obj2 should currently be max]");
209 ok($obj2->serial == $obj1->serial + 1, "$pre obj2->serial() : " . ($obj2->serial || 'FAILED'));
210 ok($previous = $obj2->previous(), "$pre obj2->previous() : " . ($previous || 'FAILED'));
211 ok($next = $obj1->next(), "$pre obj1->next() : " . ($next || 'FAILED'));
212 ok($next->previous()->value() eq $obj1->value(), "$pre Roundtrip, value : " . ($obj1->value || 'FAILED'));
213 ok($previous->next()->value() eq $obj2->value(), "$pre Roundtrip, value : " . ($obj2->value || 'FAILED'));
217 foreach $format (@formats) {
218 my $pre = sprintf '(%-12s)', $format;
219 foreach my $testval (@{$thash{ $format }}) {
220 if ($format eq 'hbyymmincr') {
221 warning_like { $obj1 = C4::Barcodes->new($format,$testval); }
222 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
223 "$pre Expected complaint regarding no hbyymmincr barcodes found";
224 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
225 $obj2 = $obj1->new();
226 my $branch;
227 ok($branch = $obj1->branch(), "$pre branch() : " . ($branch || 'FAILED') );
228 ok($branch eq $obj2->branch(), "$pre branch extended to derived object : " . ($obj2->branch || 'FAILED'));
230 elsif ($format eq 'EAN13') {
231 warning_like { $obj1 = C4::Barcodes->new($format,$testval) }
232 [ qr/not valid EAN-13 barcode/ ],
233 "$pre Expected complaint regarding $testval not being a valid EAN-13 barcode";
234 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
236 else {
237 ok($obj1 = C4::Barcodes->new($format,$testval), "$pre Barcode Creation : new('$format','$testval')");
242 $schema->storage->txn_rollback;