Bug 24786: (QA follow-up) Remove trailing space in classes
[koha.git] / t / db_dependent / Barcodes.t
blob4253a615ebd831200376e5029268f3dc8de436d4
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_sample_item(
55 barcode => $barcodevalue
59 is($barcodevalue,$barcodeobj->db_max(), "(annual) First barcode saved to db is equal to db_max" );
61 #This is just setting the value ahead an arbitrary amount before adding a second barcode to db
62 $barcodevalue = $barcodeobj->next_value();
63 $barcodevalue = $barcodeobj->next_value($barcodevalue);
64 $barcodevalue = $barcodeobj->next_value($barcodevalue);
65 $barcodevalue = $barcodeobj->next_value($barcodevalue);
66 $barcodevalue = $barcodeobj->next_value($barcodevalue);
68 my $item_2 = $builder->build_sample_item(
70 barcode => $barcodevalue
74 $barcodeobj = C4::Barcodes->new('annual');
76 is($barcodevalue,$barcodeobj->db_max(), '(annual) db_max should equal the greatest barcode in the db when more than 1 present');
77 ok($barcodeobj->value() gt $barcodevalue, '(annual) new barcode object should be created with value greater and last value inserted into db');
79 $schema->storage->txn_rollback;
82 subtest 'Test generation of hbyymmincr barcodes from DB values' => sub {
84 plan tests => 5;
86 $schema->storage->txn_begin;
87 $builder->schema->resultset( 'Issue' )->delete_all;
88 $builder->schema->resultset( 'Item' )->delete_all;
90 my $branchcode_1 = "LETTERS"; #NOTE: This barcode type supports only letters in the branchcode
91 my $barcodeobj;
93 warnings_are { $barcodeobj = C4::Barcodes->new('hbyymmincr'); }
94 [ "HBYYMM Barcode created with no branchcode, default is blank",
95 "No existing hbyymmincr barcodes found. Reverting to initial value.",
96 "HBYYMM Barcode was not passed a branch, default is blank"]
97 , "(hbyymmincr) Expected complaint regarding no max barcode found";
100 warning_like { $barcodeobj = C4::Barcodes->new('hbyymmincr',$branchcode_1); }
101 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./],
102 "(hbyymmincr) Expected complaint regarding no max barcode found";
104 my $barcodevalue = $barcodeobj->value();
106 my $item_1 = $builder->build_sample_item(
108 barcode => $barcodevalue
112 is($barcodevalue,$barcodeobj->db_max(), "(hbyymmincr) First barcode saved to db is equal to db_max" );
114 #This is just setting the value ahead an arbitrary amount before adding a second barcode to db
115 $barcodevalue = $barcodeobj->next_value();
116 $barcodevalue = $barcodeobj->next_value($barcodevalue);
117 $barcodevalue = $barcodeobj->next_value($barcodevalue);
118 $barcodevalue = $barcodeobj->next_value($barcodevalue);
119 $barcodevalue = $barcodeobj->next_value($barcodevalue);
121 my $item_2 = $builder->build_sample_item(
123 barcode => $barcodevalue
127 $barcodeobj = C4::Barcodes->new('hbyymmincr',$branchcode_1);
129 is($barcodevalue,$barcodeobj->db_max(), '(hbyymmincr) db_max should equal the greatest barcode in the db when more than 1 present');
130 ok($barcodeobj->value() gt $barcodevalue, '(hbyymmincr) new barcode object should be created with value greater and last value inserted into db');
132 $schema->storage->txn_rollback;
136 $schema->storage->txn_begin;
138 $builder->schema->resultset( 'Issue' )->delete_all;
139 $builder->schema->resultset( 'Item' )->delete_all;
141 my %thash = (
142 incremental => [],
143 annual => [],
144 hbyymmincr => ['MAIN'],
145 EAN13 => ['0000000695152','892685001928'],
148 my ($obj1,$obj2,$format,$value,$initial,$serial,$next,$previous,$temp);
149 my @formats = sort keys %thash;
150 foreach (@formats) {
151 my $pre = sprintf '(%-12s)', $_;
152 if ($_ eq 'EAN13') {
153 warning_like { $obj1 = C4::Barcodes->new($_); }
154 [ qr/not valid EAN-13 barcode/ ],
155 "$pre Expected complaint regarding $_ not being a valid EAN-13 barcode";
157 elsif ($_ eq 'annual') {
158 warning_like { $obj1 = C4::Barcodes->new($_); }
159 [ qr/No max barcode (.*) found\. Using initial value\./ ],
160 "$pre Expected complaint regarding no max barcode found";
162 elsif ($_ eq 'hbyymmincr') {
163 warnings_are { $obj1 = C4::Barcodes->new($_); }
164 [ "HBYYMM Barcode created with no branchcode, default is blank",
165 "No existing hbyymmincr barcodes found. Reverting to initial value.",
166 "HBYYMM Barcode was not passed a branch, default is blank"]
167 , "$pre Expected complaint regarding no max barcode found";
169 elsif ($_ eq 'incremental') {
170 $obj1 = C4::Barcodes->new($_);
172 else {
173 die "This should not happen! ($_)\n";
175 ok($obj1, "$pre Barcode Creation : new($_)");
176 SKIP: {
177 skip "No Object Returned by new($_)", 17 unless $obj1;
178 ok($_ eq ($format = $obj1->autoBarcode()), "$pre autoBarcode() : " . ($format || 'FAILED') );
179 if ($_ eq 'hbyymmincr') {
180 warning_like { $initial= $obj1->initial(); }
181 [ qr/HBYYMM Barcode was not passed a branch, default is blank/ ]
182 ,"$pre Expected complaint regarding no branch passed when getting initial\n $pre initial() : " . $initial ;
183 warnings_are { $temp = $obj1->db_max(); }
184 [ "No existing hbyymmincr barcodes found. Reverting to initial value.",
185 "HBYYMM Barcode was not passed a branch, default is blank"]
186 ,"$pre Expected complaint regarding no hbyymmincr barcodes found";
188 else {
189 ok($initial= $obj1->initial(), "$pre initial() : " . ($initial|| 'FAILED') );
190 $temp = $obj1->db_max();
192 ok($temp = $obj1->max(), "$pre max() : " . ($temp || 'FAILED') );
193 ok($value = $obj1->value(), "$pre value() : " . ($value || 'FAILED') );
194 ok($serial = $obj1->serial(), "$pre serial() : " . ($serial || 'FAILED') );
195 ok($temp = $obj1->is_max(), "$pre obj1->is_max() [obj1 should currently be max]");
196 if ($_ eq 'hbyymmincr') {
197 warning_like { $obj2 = $obj1->new(); }
198 [ qr/HBYYMM Barcode created with no branchcode, default is blank/ ]
199 ,"$pre Expected complaint regarding no branch passed when getting initial\n $pre Barcode Creation : obj2 = obj1->new()" ;
200 } else {
201 ok($obj2 = $obj1->new(), "$pre Barcode Creation : obj2 = obj1->new()");
203 ok(not($obj1->is_max()), "$pre obj1->is_max() [obj1 should no longer be max]");
204 ok( $obj2->is_max(), "$pre obj2->is_max() [obj2 should currently be max]");
205 ok($obj2->serial == $obj1->serial + 1, "$pre obj2->serial() : " . ($obj2->serial || 'FAILED'));
206 ok($previous = $obj2->previous(), "$pre obj2->previous() : " . ($previous || 'FAILED'));
207 ok($next = $obj1->next(), "$pre obj1->next() : " . ($next || 'FAILED'));
208 ok($next->previous()->value() eq $obj1->value(), "$pre Roundtrip, value : " . ($obj1->value || 'FAILED'));
209 ok($previous->next()->value() eq $obj2->value(), "$pre Roundtrip, value : " . ($obj2->value || 'FAILED'));
213 foreach my $format (@formats) {
214 my $pre = sprintf '(%-12s)', $format;
215 foreach my $testval (@{$thash{ $format }}) {
216 if ($format eq 'hbyymmincr') {
217 warning_like { $obj1 = C4::Barcodes->new($format,$testval); }
218 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
219 "$pre Expected complaint regarding no hbyymmincr barcodes found";
220 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
221 $obj2 = $obj1->new();
222 my $branch;
223 ok($branch = $obj1->branch(), "$pre branch() : " . ($branch || 'FAILED') );
224 ok($branch eq $obj2->branch(), "$pre branch extended to derived object : " . ($obj2->branch || 'FAILED'));
226 elsif ($format eq 'EAN13') {
227 warning_like { $obj1 = C4::Barcodes->new($format,$testval) }
228 [ qr/not valid EAN-13 barcode/ ],
229 "$pre Expected complaint regarding $testval not being a valid EAN-13 barcode";
230 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
232 else {
233 ok($obj1 = C4::Barcodes->new($format,$testval), "$pre Barcode Creation : new('$format','$testval')");
238 $schema->storage->txn_rollback;