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>.
20 use Test::More tests => 74;
23 use t::lib::TestBuilder;
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 {
43 $schema->storage->txn_begin;
44 $builder->schema->resultset( 'Issue' )->delete_all;
45 $builder->schema->resultset( 'Item' )->delete_all;
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({
56 barcode => $barcodevalue
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({
72 barcode => $barcodevalue
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 $schema->storage->txn_begin;
86 $builder->schema->resultset( 'Issue' )->delete_all;
87 $builder->schema->resultset( 'Item' )->delete_all;
92 hbyymmincr => ['MAIN'],
93 EAN13 => ['0000000695152','892685001928'],
96 my ($obj1,$obj2,$format,$value,$initial,$serial,$re,$next,$previous,$temp);
97 my @formats = sort keys %thash;
99 my $pre = sprintf '(%-12s)', $_;
101 warning_like { $obj1 = C4::Barcodes->new($_); }
102 [ qr/not valid EAN-13 barcode/ ],
103 "$pre Expected complaint regarding $_ not being a valid EAN-13 barcode";
105 elsif ($_ eq 'annual') {
106 warning_like { $obj1 = C4::Barcodes->new($_); }
107 [ qr/No max barcode (.*) found\. Using initial value\./ ],
108 "$pre Expected complaint regarding no max barcode found";
110 elsif ($_ eq 'hbyymmincr') {
111 warning_like { $obj1 = C4::Barcodes->new($_); }
112 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
113 "$pre Expected complaint regarding no hbyymmincr barcodes found";
115 elsif ($_ eq 'incremental') {
116 $obj1 = C4::Barcodes->new($_);
119 die "This should not happen! ($_)\n";
121 ok($obj1, "$pre Barcode Creation : new($_)");
123 skip "No Object Returned by new($_)", 17 unless $obj1;
124 ok($_ eq ($format = $obj1->autoBarcode()), "$pre autoBarcode() : " . ($format || 'FAILED') );
125 ok($initial= $obj1->initial(), "$pre initial() : " . ($initial|| 'FAILED') );
126 if ($_ eq 'hbyymmincr') {
127 warning_like { $temp = $obj1->db_max(); }
128 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
129 "$pre Expected complaint regarding no hbyymmincr barcodes found";
132 $temp = $obj1->db_max();
134 ok($temp = $obj1->max(), "$pre max() : " . ($temp || 'FAILED') );
135 ok($value = $obj1->value(), "$pre value() : " . ($value || 'FAILED') );
136 ok($serial = $obj1->serial(), "$pre serial() : " . ($serial || 'FAILED') );
137 ok($temp = $obj1->is_max(), "$pre obj1->is_max() [obj1 should currently be max]");
138 ok($obj2 = $obj1->new(), "$pre Barcode Creation : obj2 = obj1->new()");
139 ok(not($obj1->is_max()), "$pre obj1->is_max() [obj1 should no longer be max]");
140 ok( $obj2->is_max(), "$pre obj2->is_max() [obj2 should currently be max]");
141 ok($obj2->serial == $obj1->serial + 1, "$pre obj2->serial() : " . ($obj2->serial || 'FAILED'));
142 ok($previous = $obj2->previous(), "$pre obj2->previous() : " . ($previous || 'FAILED'));
143 ok($next = $obj1->next(), "$pre obj1->next() : " . ($next || 'FAILED'));
144 ok($next->previous()->value() eq $obj1->value(), "$pre Roundtrip, value : " . ($obj1->value || 'FAILED'));
145 ok($previous->next()->value() eq $obj2->value(), "$pre Roundtrip, value : " . ($obj2->value || 'FAILED'));
149 foreach $format (@formats) {
150 my $pre = sprintf '(%-12s)', $format;
151 foreach my $testval (@{$thash{ $format }}) {
152 if ($format eq 'hbyymmincr') {
153 warning_like { $obj1 = C4::Barcodes->new($format,$testval); }
154 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
155 "$pre Expected complaint regarding no hbyymmincr barcodes found";
156 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
157 $obj2 = $obj1->new();
159 ok($branch = $obj1->branch(), "$pre branch() : " . ($branch || 'FAILED') );
160 ok($branch eq $obj2->branch(), "$pre branch extended to derived object : " . ($obj2->branch || 'FAILED'));
162 elsif ($format eq 'EAN13') {
163 warning_like { $obj1 = C4::Barcodes->new($format,$testval) }
164 [ qr/not valid EAN-13 barcode/ ],
165 "$pre Expected complaint regarding $testval not being a valid EAN-13 barcode";
166 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
169 ok($obj1 = C4::Barcodes->new($format,$testval), "$pre Barcode Creation : new('$format','$testval')");
174 $schema->storage->txn_rollback;