Bug 24013: Add tests
[koha.git] / t / db_dependent / Labels / t_Label.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2020 Koha Development team
6 # Copyright (C) 2017  Mark Tompsett
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use Test::More tests => 4;
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use MARC::Record;
28 use MARC::Field;
29 use Data::Dumper;
30
31 use C4::Items;
32 use C4::Biblio;
33 use C4::Labels::Layout;
34
35 use Koha::Database;
36
37 use_ok('C4::Labels::Label');
38
39 my $database = Koha::Database->new();
40 my $schema   = $database->schema();
41 $schema->storage->txn_begin();
42
43 my $batch_id;
44 my ( $llx, $lly ) = ( 0, 0 );
45 my $frameworkcode = q{};
46
47 ## Setup Test
48 my $builder = t::lib::TestBuilder->new;
49
50 # Add branch
51 my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
52
53 # Add categories
54 my $category_1 = $builder->build( { source => 'Category' } )->{categorycode};
55
56 # Add an item type
57 my $itemtype =
58   $builder->build( { source => 'Itemtype', value => { notforloan => undef } } )
59   ->{itemtype};
60
61 t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
62
63 my $bibnum = $builder->build_sample_biblio({ frameworkcode => $frameworkcode })->biblionumber;
64
65 my $item = $builder->build_sample_item(
66     { library => $branch_1, itype => $itemtype, biblionumber => $bibnum, enumchron => "enum", copynumber => "copynum" } );
67 my $itemnumber = $item->itemnumber;
68
69 # Modify item; setting barcode.
70 my $testbarcode = '97531';
71 ModItem( { barcode => $testbarcode }, $bibnum, $itemnumber );
72
73 my $layout = C4::Labels::Layout->new( layout_name => 'TEST' );
74
75 my $dummy_template_values = {
76     creator          => 'Labels',
77     profile_id       => 0,
78     template_code    => 'Avery 5160 | 1 x 2-5/8',
79     template_desc    => '3 columns, 10 rows of labels',
80     page_width       => 8.5,
81     page_height      => 11,
82     label_width      => 2.63,
83     label_height     => 1,
84     top_text_margin  => 0.139,
85     left_text_margin => 0.0417,
86     top_margin       => 0.35,
87     left_margin      => 0.23,
88     cols             => 3,
89     rows             => 10,
90     col_gap          => 0.13,
91     row_gap          => 0,
92     units            => 'INCH',
93     template_stat    => 1,
94 };
95
96 my $label_info = {
97     batch_id         => $batch_id,
98     item_number      => $itemnumber,
99     llx              => $llx,
100     lly              => $lly,
101     width            => $dummy_template_values->{'label_width'},
102     height           => $dummy_template_values->{'label_height'},
103     top_text_margin  => $dummy_template_values->{'top_text_margin'},
104     left_text_margin => $dummy_template_values->{'left_text_margin'},
105     barcode_type     => $layout->get_attr('barcode_type'),
106     printing_type    => 'BIB',
107     guidebox         => $layout->get_attr('guidebox'),
108     oblique_title    => $layout->get_attr('oblique_title'),
109     font             => $layout->get_attr('font'),
110     font_size        => $layout->get_attr('font_size'),
111     callnum_split    => $layout->get_attr('callnum_split'),
112     justify          => $layout->get_attr('text_justify'),
113     text_wrap_cols   => $layout->get_text_wrap_cols(
114         label_width      => $dummy_template_values->{'label_width'},
115         left_text_margin => $dummy_template_values->{'left_text_margin'}
116     ),
117 };
118
119 my $format_string = '100a 245a';
120 my $label = C4::Labels::Label->new(%$label_info, format_string => $format_string);
121 my $label_text = $label->create_label();
122 ok( defined $label_text, 'Label Text Value defined.' );
123 my $label_csv_data = $label->csv_data();
124 is_deeply( $label_csv_data,
125     [ sprintf( "%s %s", $item->biblio->author, $item->biblio->title ) ] );
126
127 $format_string = '100a 245a,enumchron copynumber';
128 $label = C4::Labels::Label->new(%$label_info, format_string => $format_string);
129 $label_csv_data = $label->csv_data();
130 is_deeply(
131     $label_csv_data,
132     [
133         sprintf( "%s %s", $item->biblio->author, $item->biblio->title ),
134         sprintf( "%s %s", $item->enumchron,      $item->copynumber )
135     ]
136 );
137
138 $schema->storage->txn_rollback();
139
140 1;