Bug 16561 - Bug 15877 followup to fix tests
[srvgit] / 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 => 74;
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 $schema->storage->txn_begin;
85
86 $builder->schema->resultset( 'Issue' )->delete_all;
87 $builder->schema->resultset( 'Item' )->delete_all;
88
89 my %thash = (
90     incremental => [],
91     annual => [],
92     hbyymmincr => ['MAIN'],
93     EAN13 => ['0000000695152','892685001928'],
94 );
95
96 my ($obj1,$obj2,$format,$value,$initial,$serial,$re,$next,$previous,$temp);
97 my @formats = sort keys %thash;
98 foreach (@formats) {
99     my $pre = sprintf '(%-12s)', $_;
100     if ($_ eq 'EAN13') {
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";
104     }
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";
109     }
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";
114     }
115     elsif ($_ eq 'incremental') {
116         $obj1 = C4::Barcodes->new($_);
117     }
118     else {
119         die "This should not happen! ($_)\n";
120     }
121     ok($obj1,           "$pre Barcode Creation : new($_)");
122     SKIP: {
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";
130         }
131         else {
132             $temp = $obj1->db_max();
133         }
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'));
146     }
147 }
148
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();
158             my $branch;
159             ok($branch = $obj1->branch(),   "$pre branch() : " . ($branch || 'FAILED') );
160             ok($branch eq $obj2->branch(),  "$pre branch extended to derived object : " . ($obj2->branch || 'FAILED'));
161         }
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')");
167         }
168         else {
169             ok($obj1 = C4::Barcodes->new($format,$testval),    "$pre Barcode Creation : new('$format','$testval')");
170         }
171     }
172 }
173
174 $schema->storage->txn_rollback;