Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Barcodes.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 75;
21 use Test::Warn;
22 use Test::MockModule;
23 use t::lib::TestBuilder;
24
25 use Koha::Database;
26
27 $| = 1;
28
29 BEGIN {
30     use FindBin;
31     use lib $FindBin::Bin;
32     use_ok('C4::Barcodes', qw( value initial max db_max next_value next previous serial autoBarcode is_max ));
33 }
34
35 my $builder = t::lib::TestBuilder->new;
36
37 my $schema  = Koha::Database->new->schema;
38
39 subtest 'Test generation of annual barcodes from DB values' => sub {
40
41     plan tests => 4;
42
43     $schema->storage->txn_begin;
44     $builder->schema->resultset( 'Issue' )->delete_all;
45     $builder->schema->resultset( 'Item' )->delete_all;
46
47     my $barcodeobj;
48
49     warning_like { $barcodeobj = C4::Barcodes->new('annual'); } [ qr/No max barcode (.*) found\.  Using initial value\./ ], "(annual) Expected complaint regarding no max barcode found";
50
51     my $barcodevalue = $barcodeobj->value();
52
53     my $item_1 = $builder->build_sample_item(
54         {
55             barcode => $barcodevalue
56         }
57     );
58
59     is($barcodevalue,$barcodeobj->db_max(), "(annual) First barcode saved to db is equal to db_max" );
60
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);
67
68     my $item_2 = $builder->build_sample_item(
69         {
70             barcode => $barcodevalue
71         }
72     );
73
74     $barcodeobj = C4::Barcodes->new('annual');
75
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');
78
79     $schema->storage->txn_rollback;
80 };
81
82 subtest 'Test generation of hbyymmincr barcodes from DB values' => sub {
83
84     plan tests => 5;
85
86     $schema->storage->txn_begin;
87     $builder->schema->resultset( 'Issue' )->delete_all;
88     $builder->schema->resultset( 'Item' )->delete_all;
89
90     my $branchcode_1 = "LETTERS"; #NOTE: This barcode type supports only letters in the branchcode
91     my $barcodeobj;
92
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";
98
99
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";
103
104     my $barcodevalue = $barcodeobj->value();
105
106     my $item_1 = $builder->build_sample_item(
107         {
108             barcode => $barcodevalue
109         }
110     );
111
112     is($barcodevalue,$barcodeobj->db_max(), "(hbyymmincr) First barcode saved to db is equal to db_max" );
113
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);
120
121     my $item_2 = $builder->build_sample_item(
122         {
123             barcode => $barcodevalue
124         }
125     );
126
127     $barcodeobj = C4::Barcodes->new('hbyymmincr',$branchcode_1);
128
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');
131
132     $schema->storage->txn_rollback;
133 };
134
135
136 $schema->storage->txn_begin;
137
138 $builder->schema->resultset( 'Issue' )->delete_all;
139 $builder->schema->resultset( 'Item' )->delete_all;
140
141 my %thash = (
142     incremental => [],
143     annual => [],
144     hbyymmincr => ['MAIN'],
145     EAN13 => ['0000000695152','892685001928'],
146 );
147
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";
156     }
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";
161     }
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";
168     }
169     elsif ($_ eq 'incremental') {
170         $obj1 = C4::Barcodes->new($_);
171     }
172     else {
173         die "This should not happen! ($_)\n";
174     }
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";
187         }
188         else {
189             ok($initial= $obj1->initial(),              "$pre initial()        : " . ($initial|| 'FAILED') );
190             $temp = $obj1->db_max();
191         }
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()");
202         }
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'));
210     }
211 }
212
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'));
225         }
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')");
231         }
232         else {
233             ok($obj1 = C4::Barcodes->new($format,$testval),    "$pre Barcode Creation : new('$format','$testval')");
234         }
235     }
236 }
237
238 $schema->storage->txn_rollback;