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