Bug 6679: (follow-up) fix 9 perlcritic violations in C4/TmplTokenType.pm
[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 => 73;
21 use Test::Warn;
22
23 $| = 1;
24
25 BEGIN {
26     use FindBin;
27     use lib $FindBin::Bin;
28     use_ok('C4::Barcodes');
29 }
30
31 my %thash = (
32     incremental => [],
33     annual => [],
34     hbyymmincr => ['MAIN'],
35     EAN13 => ['0000000695152','892685001928'],
36 );
37
38 my ($obj1,$obj2,$format,$value,$initial,$serial,$re,$next,$previous,$temp);
39 my @formats = sort keys %thash;
40 foreach (@formats) {
41     my $pre = sprintf '(%-12s)', $_;
42     if ($_ eq 'EAN13') {
43         warning_like { $obj1 = C4::Barcodes->new($_); }
44                      [ qr/not valid EAN-13 barcode/ ],
45                      "$pre Expected complaint regarding $_ not being a valid EAN-13 barcode";
46     }
47     elsif ($_ eq 'annual') {
48         warning_like { $obj1 = C4::Barcodes->new($_); }
49                      [ qr/No max barcode (.*) found\.  Using initial value\./ ],
50                      "$pre Expected complaint regarding no max barcode found";
51     }
52     elsif ($_ eq 'hbyymmincr') {
53         warning_like { $obj1 = C4::Barcodes->new($_); }
54                      [ qr/No existing hbyymmincr barcodes found\.  Reverting to initial value\./ ],
55                      "$pre Expected complaint regarding no hbyymmincr barcodes found";
56     }
57     elsif ($_ eq 'incremental') {
58         $obj1 = C4::Barcodes->new($_);
59     }
60     else {
61         die "This should not happen! ($_)\n";
62     }
63     ok($obj1,           "$pre Barcode Creation : new($_)");
64     SKIP: {
65         skip "No Object Returned by new($_)", 17 unless $obj1;
66         ok($_ eq ($format = $obj1->autoBarcode()),  "$pre autoBarcode()    : " . ($format || 'FAILED') );
67         ok($initial= $obj1->initial(),              "$pre initial()        : " . ($initial|| 'FAILED') );
68         if ($_ eq 'hbyymmincr') {
69             warning_like { $temp = $obj1->db_max(); }
70                          [ qr/No existing hbyymmincr barcodes found\.  Reverting to initial value\./ ],
71                          "$pre Expected complaint regarding no hbyymmincr barcodes found";
72         }
73         else {
74             $temp = $obj1->db_max();
75         }
76         ok($temp   = $obj1->max(),                  "$pre max()            : " . ($temp   || 'FAILED') );
77         ok($value  = $obj1->value(),                "$pre value()          : " . ($value  || 'FAILED') );
78         ok($serial = $obj1->serial(),               "$pre serial()         : " . ($serial || 'FAILED') );
79         ok($temp   = $obj1->is_max(),               "$pre obj1->is_max() [obj1 should currently be max]");
80         ok($obj2   = $obj1->new(),                  "$pre Barcode Creation : obj2 = obj1->new()");
81         ok(not($obj1->is_max()),                    "$pre obj1->is_max() [obj1 should no longer be max]");
82         ok(    $obj2->is_max(),                     "$pre obj2->is_max() [obj2 should currently be max]");
83         ok($obj2->serial == $obj1->serial + 1,      "$pre obj2->serial()   : " . ($obj2->serial || 'FAILED'));
84         ok($previous = $obj2->previous(),           "$pre obj2->previous() : " . ($previous     || 'FAILED'));
85         ok($next     = $obj1->next(),               "$pre obj1->next()     : " . ($next         || 'FAILED'));
86         ok($next->previous()->value() eq $obj1->value(),  "$pre Roundtrip, value : " . ($obj1->value || 'FAILED'));
87         ok($previous->next()->value() eq $obj2->value(),  "$pre Roundtrip, value : " . ($obj2->value || 'FAILED'));
88     }
89 }
90
91 foreach $format (@formats) {
92     my $pre = sprintf '(%-12s)', $format;
93     foreach my $testval (@{$thash{ $format }}) {
94         if ($format eq 'hbyymmincr') {
95             warning_like { $obj1 = C4::Barcodes->new($format,$testval); }
96                          [ qr/No existing hbyymmincr barcodes found\.  Reverting to initial value\./ ],
97                          "$pre Expected complaint regarding no hbyymmincr barcodes found";
98             ok($obj1,    "$pre Barcode Creation : new('$format','$testval')");
99             $obj2 = $obj1->new();
100             my $branch;
101             ok($branch = $obj1->branch(),   "$pre branch() : " . ($branch || 'FAILED') );
102             ok($branch eq $obj2->branch(),  "$pre branch extended to derived object : " . ($obj2->branch || 'FAILED'));
103         }
104         elsif ($format eq 'EAN13') {
105             warning_like { $obj1 = C4::Barcodes->new($format,$testval) }
106                          [ qr/not valid EAN-13 barcode/ ],
107                          "$pre Expected complaint regarding $testval not being a valid EAN-13 barcode";
108             ok($obj1,    "$pre Barcode Creation : new('$format','$testval')");
109         }
110         else {
111             ok($obj1 = C4::Barcodes->new($format,$testval),    "$pre Barcode Creation : new('$format','$testval')");
112         }
113     }
114 }