Bug 14868: Use x-koha-authorization in current routes
[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');
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({
54         source => 'Item',
55         value => {
56             barcode => $barcodevalue
57         }
58     });
59
60     is($barcodevalue,$barcodeobj->db_max(), "(annual) First barcode saved to db is equal to db_max" );
61
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);
68
69     my $item_2 = $builder->build({
70         source => 'Item',
71         value => {
72             barcode => $barcodevalue
73         }
74     });
75
76     $barcodeobj = C4::Barcodes->new('annual');
77
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');
80
81     $schema->storage->txn_rollback;
82 };
83
84 subtest 'Test generation of hbyymmincr barcodes from DB values' => sub {
85
86     plan tests => 5;
87
88     $schema->storage->txn_begin;
89     $builder->schema->resultset( 'Issue' )->delete_all;
90     $builder->schema->resultset( 'Item' )->delete_all;
91
92     my $branchcode_1 = "LETTERS"; #NOTE: This barcode type supports only letters in the branchcode
93     my $barcodeobj;
94
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";
100
101
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";
105
106     my $barcodevalue = $barcodeobj->value();
107
108     my $item_1 = $builder->build({
109         source => 'Item',
110         value => {
111             barcode => $barcodevalue
112         }
113     });
114
115     is($barcodevalue,$barcodeobj->db_max(), "(hbyymmincr) First barcode saved to db is equal to db_max" );
116
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);
123
124     my $item_2 = $builder->build({
125         source => 'Item',
126         value => {
127             barcode => $barcodevalue
128         }
129     });
130
131     $barcodeobj = C4::Barcodes->new('hbyymmincr',$branchcode_1);
132
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');
135
136     $schema->storage->txn_rollback;
137 };
138
139
140 $schema->storage->txn_begin;
141
142 $builder->schema->resultset( 'Issue' )->delete_all;
143 $builder->schema->resultset( 'Item' )->delete_all;
144
145 my %thash = (
146     incremental => [],
147     annual => [],
148     hbyymmincr => ['MAIN'],
149     EAN13 => ['0000000695152','892685001928'],
150 );
151
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";
160     }
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";
165     }
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";
172     }
173     elsif ($_ eq 'incremental') {
174         $obj1 = C4::Barcodes->new($_);
175     }
176     else {
177         die "This should not happen! ($_)\n";
178     }
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";
191         }
192         else {
193             ok($initial= $obj1->initial(),              "$pre initial()        : " . ($initial|| 'FAILED') );
194             $temp = $obj1->db_max();
195         }
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()");
206         }
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'));
214     }
215 }
216
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'));
229         }
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')");
235         }
236         else {
237             ok($obj1 = C4::Barcodes->new($format,$testval),    "$pre Barcode Creation : new('$format','$testval')");
238         }
239     }
240 }
241
242 $schema->storage->txn_rollback;