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 $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
38 my $builder = t::lib::TestBuilder->new;
40 my $dbh = C4::Context->dbh;
42 subtest 'Test generation of annual barcodes from DB values' => sub {
46 $builder->schema->resultset( 'Issue' )->delete_all;
47 $builder->schema->resultset( 'Item' )->delete_all;
51 warning_like { $barcodeobj = C4::Barcodes->new('annual'); } [ qr/No max barcode (.*) found\. Using initial value\./ ], "(annual) Expected complaint regarding no max barcode found";
53 my $barcodevalue = $barcodeobj->value();
55 my $item_1 = $builder->build({
58 barcode => $barcodevalue
62 is($barcodevalue,$barcodeobj->db_max(), "(annual) First barcode saved to db is equal to db_max" );
64 #This is just setting the value ahead an arbitrary amount before adding a second barcode to db
65 $barcodevalue = $barcodeobj->next_value();
66 $barcodevalue = $barcodeobj->next_value($barcodevalue);
67 $barcodevalue = $barcodeobj->next_value($barcodevalue);
68 $barcodevalue = $barcodeobj->next_value($barcodevalue);
69 $barcodevalue = $barcodeobj->next_value($barcodevalue);
71 my $item_2 = $builder->build({
74 barcode => $barcodevalue
78 $barcodeobj = C4::Barcodes->new('annual');
80 is($barcodevalue,$barcodeobj->db_max(), '(annual) db_max should equal the greatest barcode in the db when more than 1 present');
81 ok($barcodeobj->value() gt $barcodevalue, '(annual) new barcode object should be created with value greater and last value inserted into db');
83 $schema->storage->txn_rollback;
87 $builder->schema->resultset( 'Issue' )->delete_all;
88 $builder->schema->resultset( 'Item' )->delete_all;
93 hbyymmincr => ['MAIN'],
94 EAN13 => ['0000000695152','892685001928'],
97 my ($obj1,$obj2,$format,$value,$initial,$serial,$re,$next,$previous,$temp);
98 my @formats = sort keys %thash;
100 my $pre = sprintf '(%-12s)', $_;
102 warning_like { $obj1 = C4::Barcodes->new($_); }
103 [ qr/not valid EAN-13 barcode/ ],
104 "$pre Expected complaint regarding $_ not being a valid EAN-13 barcode";
106 elsif ($_ eq 'annual') {
107 warning_like { $obj1 = C4::Barcodes->new($_); }
108 [ qr/No max barcode (.*) found\. Using initial value\./ ],
109 "$pre Expected complaint regarding no max barcode found";
111 elsif ($_ eq 'hbyymmincr') {
112 warning_like { $obj1 = C4::Barcodes->new($_); }
113 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
114 "$pre Expected complaint regarding no hbyymmincr barcodes found";
116 elsif ($_ eq 'incremental') {
117 $obj1 = C4::Barcodes->new($_);
120 die "This should not happen! ($_)\n";
122 ok($obj1, "$pre Barcode Creation : new($_)");
124 skip "No Object Returned by new($_)", 17 unless $obj1;
125 ok($_ eq ($format = $obj1->autoBarcode()), "$pre autoBarcode() : " . ($format || 'FAILED') );
126 ok($initial= $obj1->initial(), "$pre initial() : " . ($initial|| 'FAILED') );
127 if ($_ eq 'hbyymmincr') {
128 warning_like { $temp = $obj1->db_max(); }
129 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
130 "$pre Expected complaint regarding no hbyymmincr barcodes found";
133 $temp = $obj1->db_max();
135 ok($temp = $obj1->max(), "$pre max() : " . ($temp || 'FAILED') );
136 ok($value = $obj1->value(), "$pre value() : " . ($value || 'FAILED') );
137 ok($serial = $obj1->serial(), "$pre serial() : " . ($serial || 'FAILED') );
138 ok($temp = $obj1->is_max(), "$pre obj1->is_max() [obj1 should currently be max]");
139 ok($obj2 = $obj1->new(), "$pre Barcode Creation : obj2 = obj1->new()");
140 ok(not($obj1->is_max()), "$pre obj1->is_max() [obj1 should no longer be max]");
141 ok( $obj2->is_max(), "$pre obj2->is_max() [obj2 should currently be max]");
142 ok($obj2->serial == $obj1->serial + 1, "$pre obj2->serial() : " . ($obj2->serial || 'FAILED'));
143 ok($previous = $obj2->previous(), "$pre obj2->previous() : " . ($previous || 'FAILED'));
144 ok($next = $obj1->next(), "$pre obj1->next() : " . ($next || 'FAILED'));
145 ok($next->previous()->value() eq $obj1->value(), "$pre Roundtrip, value : " . ($obj1->value || 'FAILED'));
146 ok($previous->next()->value() eq $obj2->value(), "$pre Roundtrip, value : " . ($obj2->value || 'FAILED'));
150 foreach $format (@formats) {
151 my $pre = sprintf '(%-12s)', $format;
152 foreach my $testval (@{$thash{ $format }}) {
153 if ($format eq 'hbyymmincr') {
154 warning_like { $obj1 = C4::Barcodes->new($format,$testval); }
155 [ qr/No existing hbyymmincr barcodes found\. Reverting to initial value\./ ],
156 "$pre Expected complaint regarding no hbyymmincr barcodes found";
157 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
158 $obj2 = $obj1->new();
160 ok($branch = $obj1->branch(), "$pre branch() : " . ($branch || 'FAILED') );
161 ok($branch eq $obj2->branch(), "$pre branch extended to derived object : " . ($obj2->branch || 'FAILED'));
163 elsif ($format eq 'EAN13') {
164 warning_like { $obj1 = C4::Barcodes->new($format,$testval) }
165 [ qr/not valid EAN-13 barcode/ ],
166 "$pre Expected complaint regarding $testval not being a valid EAN-13 barcode";
167 ok($obj1, "$pre Barcode Creation : new('$format','$testval')");
170 ok($obj1 = C4::Barcodes->new($format,$testval), "$pre Barcode Creation : new('$format','$testval')");