Bug 18292: Remove return 1 statements in tests
[koha.git] / t / db_dependent / Creators / Lib.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20 use Graphics::Magick;
21 use Test::More tests => 645;
22 use Test::MockModule;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25 use Koha::Database;
26 use Test::Warn;
27
28 BEGIN {
29     use_ok('C4::Creators::Lib');
30     use_ok('C4::Biblio');
31     use_ok('C4::Context');
32     use_ok('Koha::Patron');
33     use_ok('MARC::Record');
34 }
35
36 can_ok(
37     'C4::Creators::Lib', qw(
38       get_all_templates
39       get_all_layouts
40       get_all_profiles
41       get_all_image_names
42       get_batch_summary
43       get_label_summary
44       get_card_summary
45       get_barcode_types
46       get_label_types
47       get_font_types
48       get_text_justification_types
49       get_output_formats
50       get_table_names
51       get_unit_values
52       html_table )
53 );
54
55 my $schema = Koha::Database->schema;
56 $schema->storage->txn_begin;
57 my $builder = t::lib::TestBuilder->new;
58
59 my $dbh = C4::Context->dbh;
60 $dbh->do('DELETE FROM issues');
61 $dbh->do('DELETE FROM creator_templates');
62 $dbh->do('DELETE FROM creator_layouts');
63 $dbh->do('DELETE FROM creator_images');
64 $dbh->do('DELETE FROM creator_batches');
65 $dbh->do('DELETE FROM printers_profile');
66 $dbh->do('DELETE FROM borrowers');
67 $dbh->do('DELETE FROM items');
68 $dbh->do('DELETE FROM biblioitems');
69
70 ###########################################################
71 #                     Inserted data
72 ###########################################################
73
74 my $library1 = $builder->build({
75     source => 'Branch',
76 });
77 my $library2 = $builder->build({
78     source => 'Branch',
79 });
80 my $library3 = $builder->build({
81     source => 'Branch',
82 });
83
84 # ---------- Some Templates  --------------------
85 my $query = '
86   INSERT INTO creator_templates
87      (profile_id      , template_code, template_desc, page_width,
88       page_height     , label_width  , label_height , top_text_margin,
89       left_text_margin, top_margin   , left_margin  , cols,
90       rows            , col_gap      , row_gap      , units,
91       creator)
92   VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
93 my $insert_sth = $dbh->prepare($query);
94 $insert_sth->execute( 1, 'TEMPL1', 'Template 1', 100, 150, 10, 15, 2, 3, 1, 4, 9, 6, 0.1, 0.2, 'POINT', 'Labels' );
95
96 $insert_sth->execute( 2, 'TEMPL2', 'Template 2', 101, 151, 11, 16, 3, 4, 2, 5, 10, 7, 0.2, 0.3, 'POINT', 'Labels' );
97 $query = '
98   SELECT template_id, template_code
99   FROM   creator_templates
100   WHERE  profile_id = ?
101   ';
102 my ( $template_id1, $template_code1 ) = $dbh->selectrow_array( $query, {}, 1 );
103
104 # ---------- Some Layouts -----------------------
105 $query = '
106   INSERT INTO creator_layouts
107     (barcode_type  , start_label , printing_type, layout_name,
108      guidebox      , font        , font_size    , units      ,
109      callnum_split , text_justify, format_string, layout_xml ,
110      creator)
111   VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)';
112 $insert_sth = $dbh->prepare($query);
113 $insert_sth->execute( 'COOP2OF5', 1, 'BAR1', 'NAME1', 1, 'TR', 11, 'POINT', 1, 'L', 'barcode', 'layout_xml1', 'Labels' );
114
115 $insert_sth->execute( 'EAN13', 2, 'BAR2', 'NAME2', 2, 'TR', 12, 'POINT', 2, 'L', 'barcode', 'layout_xml2', 'Labels' );
116
117 # ---------- Some Printers  ---------------------
118 $query = '
119   INSERT INTO printers_profile
120     (printer_name, template_id, paper_bin,
121      offset_horz , offset_vert, creep_horz,
122      creep_vert  , units      , creator)
123   VALUES (?,?,?,?,?,?,?,?,?)';
124 $insert_sth = $dbh->prepare($query);
125 $insert_sth->execute( 'Layout1 Name', 1234, 'Bypass', 0.1, 0.2, 0.3, 0.4, 'POINT', 'Labels' );
126
127 $insert_sth->execute( 'Layout2 Name', 1235, 'Bypass', 0.2, 0.3, 0.4, 0.5, 'POINT', 'Labels' );
128
129 # ---------- Some Images  -----------------------
130 my $image1 = Graphics::Magick->new;
131 my $image2 = Graphics::Magick->new;
132
133 $query = '
134   INSERT INTO creator_images
135     (imagefile, image_name)
136   VALUES (?,?)';
137 $insert_sth = $dbh->prepare($query);
138 $insert_sth->execute( "$image1->ImageToBlob()", 'Image 1' );
139 $insert_sth->execute( "$image2->ImageToBlob()", 'Image 2' );
140
141 # ---------- Some biblios -----------------------
142 my $title1  = 'Title 1';
143 my $title2  = 'Title 2';
144 my $title3  = 'Title 3';
145 my $author1 = 'Author 1';
146 my $author2 = 'Author 2';
147 my $author3 = 'Author 3';
148
149 $query = '
150   INSERT INTO biblio
151     (title, author)
152   VALUES (?,?)';
153 $insert_sth = $dbh->prepare($query);
154 $insert_sth->execute( $title1, $author1 );
155 $insert_sth->execute( $title2, undef );
156 $insert_sth->execute( $title3, $author3 );
157
158 $query = '
159   SELECT biblionumber
160   FROM   biblio
161   WHERE  title = ?';
162 my $biblionumber1 = $dbh->selectrow_array( $query, {}, $title1 );
163 my $biblionumber2 = $dbh->selectrow_array( $query, {}, $title2 );
164 my $biblionumber3 = $dbh->selectrow_array( $query, {}, $title3 );
165
166 # ---------- Some biblio items  -------------------------
167 $query = '
168   INSERT INTO biblioitems
169     (biblionumber, itemtype)
170   VALUES (?,?)';
171 $insert_sth = $dbh->prepare($query);
172 $insert_sth->execute( $biblionumber1, 'Book' );
173 $insert_sth->execute( $biblionumber2, 'Music' );
174 $insert_sth->execute( $biblionumber3, 'Book' );
175
176 $query = '
177   SELECT biblioitemnumber
178   FROM   biblioitems
179   WHERE  biblionumber = ?';
180 my $biblioitemnumber1 = $dbh->selectrow_array( $query, {}, $biblionumber1 );
181 my $biblioitemnumber2 = $dbh->selectrow_array( $query, {}, $biblionumber2 );
182 my $biblioitemnumber3 = $dbh->selectrow_array( $query, {}, $biblionumber3 );
183
184 # ---------- Some items  -------------------------
185 my $barcode1 = '111111';
186 my $barcode2 = '222222';
187 my $barcode3 = '333333';
188
189 $query = '
190   INSERT INTO items
191     (biblionumber, biblioitemnumber, barcode, itype)
192   VALUES (?,?,?,?)';
193 $insert_sth = $dbh->prepare($query);
194 $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
195 $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
196 $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
197
198 $query = '
199   SELECT itemnumber
200   FROM   items
201   WHERE  barcode = ?';
202 my $item_number1 = $dbh->selectrow_array( $query, {}, $barcode1 );
203 my $item_number2 = $dbh->selectrow_array( $query, {}, $barcode2 );
204 my $item_number3 = $dbh->selectrow_array( $query, {}, $barcode3 );
205
206 # ---------- Some borrowers  ---------------------
207 my $surname1     = 'Borrower 1';
208 my $surname2     = 'Borrower 2';
209 my $surname3     = 'Borrower 3';
210 my $firstname1   = 'firstname 1';
211 my $firstname2   = 'firstname 2';
212 my $firstname3   = 'firstname 3';
213 my $cardnumber1  = '00001';
214 my $cardnumber2  = '00002';
215 my $cardnumber3  = '00003';
216 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
217 my $branchcode   = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
218
219 $query = '
220  INSERT INTO borrowers
221     (surname, firstname, cardnumber, branchcode, categorycode)
222   VALUES (?,?,?,?,?)';
223 $insert_sth = $dbh->prepare($query);
224 $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
225 $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
226 $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
227
228 $query = '
229   SELECT borrowernumber
230   FROM   borrowers
231   WHERE  surname = ?';
232 my $borrowernumber1 = $dbh->selectrow_array( $query, {}, $surname1 );
233 my $borrowernumber2 = $dbh->selectrow_array( $query, {}, $surname2 );
234 my $borrowernumber3 = $dbh->selectrow_array( $query, {}, $surname3 );
235
236 # ---------- Some batches  -----------------------
237 $query = '
238   INSERT INTO creator_batches
239     (batch_id , item_number, borrower_number,
240      timestamp, branch_code, creator)
241   VALUES (?,?,?,?,?,?)';
242 $insert_sth = $dbh->prepare($query);
243 $insert_sth->execute( 11, $item_number1, $borrowernumber1, 'now()', $library1->{branchcode}, 'Labels' );
244
245 $insert_sth->execute( 12, $item_number2, $borrowernumber2, 'now()', $library2->{branchcode}, 'Labels' );
246
247 $insert_sth->execute( 12, $item_number3, $borrowernumber3, 'now()', $library3->{branchcode}, 'Labels' );
248
249 ###########################################################
250 #                     Testing Subs
251 ###########################################################
252
253 # ---------- Testing get_all_templates --------------------
254 # Mocking $sth->err and $sth->errstr
255 {
256     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
257     $dbi_st->mock( 'err',    sub { return 1; } );
258     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
259     my $templates;
260     warning_is { $templates = get_all_templates() } 'Database returned the following error: something went wrong',
261       'get_all_templates() raises warning if something went wrong with the sql request execution';
262
263     is( $templates, -1, '$templates return -1' );
264 }
265
266 # Without params ----------------------
267 my $templates = get_all_templates();
268
269 $query = '
270   SELECT count(*)
271   FROM   creator_templates
272   ';
273 my $count = $dbh->selectrow_array($query);
274 is( $count,      2,      'There are 2 templates' );
275 is( @$templates, $count, 'There are 2 templates matching' );
276 isa_ok( $templates, 'ARRAY', '$templates is an ARRAY' );
277
278 isa_ok( $templates->[0], 'HASH', '$templates->[0]  is a HASH' );
279 is( $templates->[0]->{profile_id},       1,            'profile_id       is good' );
280 is( $templates->[0]->{template_code},    'TEMPL1',     'template_code    is good' );
281 is( $templates->[0]->{template_desc},    'Template 1', 'template_desc    is good' );
282 is( $templates->[0]->{page_width},       100,          'page_width       is good' );
283 is( $templates->[0]->{page_height},      150,          'page_height      is good' );
284 is( $templates->[0]->{label_width},      10,           'label_width      is good' );
285 is( $templates->[0]->{label_height},     15,           'label_height     is good' );
286 is( $templates->[0]->{top_text_margin},  2,            'top_text_margin  is good' );
287 is( $templates->[0]->{left_text_margin}, 3,            'left_text_margin is good' );
288 is( $templates->[0]->{top_margin},       1,            'top_margin       is good' );
289 is( $templates->[0]->{left_margin},      4,            'left_margin      is good' );
290 is( $templates->[0]->{cols},             9,            'cols             is good' );
291 is( $templates->[0]->{rows},             6,            'rows             is good' );
292 is( $templates->[0]->{col_gap},          0.1,          'col_gap          is good' );
293 is( $templates->[0]->{row_gap},          0.2,          'row_gap          is good' );
294 is( $templates->[0]->{units},            'POINT',      'units            is good' );
295 is( $templates->[0]->{creator},          'Labels',     'creator          is good' );
296
297 isa_ok( $templates->[1], 'HASH', '$templates->[1]  is a HASH' );
298 is( $templates->[1]->{profile_id},       2,            'profile_id       is good' );
299 is( $templates->[1]->{template_code},    'TEMPL2',     'template_code    is good' );
300 is( $templates->[1]->{template_desc},    'Template 2', 'template_desc    is good' );
301 is( $templates->[1]->{page_width},       101,          'page_width       is good' );
302 is( $templates->[1]->{page_height},      151,          'page_height      is good' );
303 is( $templates->[1]->{label_width},      11,           'label_width      is good' );
304 is( $templates->[1]->{label_height},     16,           'label_height     is good' );
305 is( $templates->[1]->{top_text_margin},  3,            'top_text_margin  is good' );
306 is( $templates->[1]->{left_text_margin}, 4,            'left_text_margin is good' );
307 is( $templates->[1]->{top_margin},       2,            'top_margin       is good' );
308 is( $templates->[1]->{left_margin},      5,            'left_margin      is good' );
309 is( $templates->[1]->{cols},             10,           'cols             is good' );
310 is( $templates->[1]->{rows},             7,            'rows             is good' );
311 is( $templates->[1]->{col_gap},          0.2,          'col_gap          is good' );
312 is( $templates->[1]->{row_gap},          0.3,          'row_gap          is good' );
313 is( $templates->[1]->{units},            'POINT',      'units            is good' );
314 is( $templates->[1]->{creator},          'Labels',     'creator          is good' );
315
316 # With field_list params --------------
317 $templates = get_all_templates( {fields=> [qw(units cols rows)] } );
318
319 $query = '
320   SELECT count(*)
321   FROM   creator_templates
322   ';
323 $count = $dbh->selectrow_array($query);
324 is( $count,      2,      'There are 2 templates' );
325 is( @$templates, $count, 'There are 2 templates matching' );
326 isa_ok( $templates, 'ARRAY', '$templates is an ARRAY' );
327
328 isa_ok( $templates->[0], 'HASH', '$templates->[0]  is a HASH' );
329 isnt( exists $templates->[0]->{profile_id},       1,            'profile_id       is good' );
330 isnt( exists $templates->[0]->{template_code},    'TEMPL1',     'template_code    is good' );
331 isnt( exists $templates->[0]->{template_desc},    'Template 1', 'template_desc    is good' );
332 isnt( exists $templates->[0]->{page_width},       100,          'page_width       is good' );
333 isnt( exists $templates->[0]->{page_height},      150,          'page_height      is good' );
334 isnt( exists $templates->[0]->{label_width},      10,           'label_width      is good' );
335 isnt( exists $templates->[0]->{label_height},     15,           'label_height     is good' );
336 isnt( exists $templates->[0]->{top_text_margin},  2,            'top_text_margin  is good' );
337 isnt( exists $templates->[0]->{left_text_margin}, 3,            'left_text_margin is good' );
338 isnt( exists $templates->[0]->{top_margin},       1,            'top_margin       is good' );
339 isnt( exists $templates->[0]->{left_margin},      4,            'left_margin      is good' );
340 is  (        $templates->[0]->{cols},             9,            'cols             is good' );
341 is  (        $templates->[0]->{rows},             6,            'rows             is good' );
342 isnt( exists $templates->[0]->{col_gap},          0.1,          'col_gap          is good' );
343 isnt( exists $templates->[0]->{row_gap},          0.2,          'row_gap          is good' );
344 is  (        $templates->[0]->{units},            'POINT',      'units            is good' );
345 isnt( exists $templates->[0]->{creator},          'Labels',     'creator          is good' );
346
347 isa_ok( $templates->[1], 'HASH', '$templates->[1]  is a HASH' );
348 isnt( exists $templates->[1]->{profile_id},       2,            'profile_id       is good' );
349 isnt( exists $templates->[1]->{template_code},    'TEMPL2',     'template_code    is good' );
350 isnt( exists $templates->[1]->{template_desc},    'Template 2', 'template_desc    is good' );
351 isnt( exists $templates->[1]->{page_width},       101,          'page_width       is good' );
352 isnt( exists $templates->[1]->{page_height},      151,          'page_height      is good' );
353 isnt( exists $templates->[1]->{label_width},      11,           'label_width      is good' );
354 isnt( exists $templates->[1]->{label_height},     16,           'label_height     is good' );
355 isnt( exists $templates->[1]->{top_text_margin},  3,            'top_text_margin  is good' );
356 isnt( exists $templates->[1]->{left_text_margin}, 4,            'left_text_margin is good' );
357 isnt( exists $templates->[1]->{top_margin},       2,            'top_margin       is good' );
358 isnt( exists $templates->[1]->{left_margin},      5,            'left_margin      is good' );
359 is  (        $templates->[1]->{cols},             10,           'cols             is good' );
360 is  (        $templates->[1]->{rows},             7,            'rows             is good' );
361 isnt( exists $templates->[1]->{col_gap},          0.2,          'col_gap          is good' );
362 isnt( exists $templates->[1]->{row_gap},          0.3,          'row_gap          is good' );
363 is  (        $templates->[1]->{units},            'POINT',      'units            is good' );
364 isnt( exists $templates->[1]->{creator},          'Labels',     'creator          is good' );
365
366 # With filters params ------------------
367 $templates = get_all_templates( { filters => { rows => 7} } );
368
369 $query = '
370   SELECT count(*)
371   FROM   creator_templates
372   WHERE  rows = 7
373   ';
374 $count = $dbh->selectrow_array($query);
375 is( $count,      1,      'There is 1 template matching' );
376 is( @$templates, $count, 'There is 1 template matching' );
377 isa_ok( $templates, 'ARRAY', '$templates is an ARRAY' );
378
379 isa_ok( $templates->[0], 'HASH', '$templates->[0]  is a HASH' );
380 is( $templates->[0]->{profile_id},       2,            'profile_id       is good' );
381 is( $templates->[0]->{template_code},    'TEMPL2',     'template_code    is good' );
382 is( $templates->[0]->{template_desc},    'Template 2', 'template_desc    is good' );
383 is( $templates->[0]->{page_width},       101,          'page_width       is good' );
384 is( $templates->[0]->{page_height},      151,          'page_height      is good' );
385 is( $templates->[0]->{label_width},      11,           'label_width      is good' );
386 is( $templates->[0]->{label_height},     16,           'label_height     is good' );
387 is( $templates->[0]->{top_text_margin},  3,            'top_text_margin  is good' );
388 is( $templates->[0]->{left_text_margin}, 4,            'left_text_margin is good' );
389 is( $templates->[0]->{top_margin},       2,            'top_margin       is good' );
390 is( $templates->[0]->{left_margin},      5,            'left_margin      is good' );
391 is( $templates->[0]->{cols},             10,           'cols             is good' );
392 is( $templates->[0]->{rows},             7,            'rows             is good' );
393 is( $templates->[0]->{col_gap},          0.2,          'col_gap          is good' );
394 is( $templates->[0]->{row_gap},          0.3,          'row_gap          is good' );
395 is( $templates->[0]->{units},            'POINT',      'units            is good' );
396 is( $templates->[0]->{creator},          'Labels',     'creator          is good' );
397
398 $templates = get_all_templates( { filters => { rows => [-42, 7]} } );
399 is( @$templates, $count, 'There is 1 template matching' );
400 # With orderby param ------------------
401 $templates = get_all_templates( { orderby => 'rows DESC' } );
402
403 $query = '
404   SELECT    count(*)
405   FROM      creator_templates
406   ORDER BY  rows DESC
407   ';
408 $count = $dbh->selectrow_array($query);
409 is( $count,      2,      'There are 2 templates' );
410 is( @$templates, $count, 'There are 2 templates matching' );
411 isa_ok( $templates, 'ARRAY', '$templates is an ARRAY' );
412
413 isa_ok( $templates->[0], 'HASH', '$templates->[0]  is a HASH' );
414 is( $templates->[0]->{profile_id},       2,            'profile_id       is good' );
415 is( $templates->[0]->{template_code},    'TEMPL2',     'template_code    is good' );
416 is( $templates->[0]->{template_desc},    'Template 2', 'template_desc    is good' );
417 is( $templates->[0]->{page_width},       101,          'page_width       is good' );
418 is( $templates->[0]->{page_height},      151,          'page_height      is good' );
419 is( $templates->[0]->{label_width},      11,           'label_width      is good' );
420 is( $templates->[0]->{label_height},     16,           'label_height     is good' );
421 is( $templates->[0]->{top_text_margin},  3,            'top_text_margin  is good' );
422 is( $templates->[0]->{left_text_margin}, 4,            'left_text_margin is good' );
423 is( $templates->[0]->{top_margin},       2,            'top_margin       is good' );
424 is( $templates->[0]->{left_margin},      5,            'left_margin      is good' );
425 is( $templates->[0]->{cols},             10,           'cols             is good' );
426 is( $templates->[0]->{rows},             7,            'rows             is good' );
427 is( $templates->[0]->{col_gap},          0.2,          'col_gap          is good' );
428 is( $templates->[0]->{row_gap},          0.3,          'row_gap          is good' );
429 is( $templates->[0]->{units},            'POINT',      'units            is good' );
430 is( $templates->[0]->{creator},          'Labels',     'creator          is good' );
431
432 isa_ok( $templates->[1], 'HASH', '$templates->[1]  is a HASH' );
433 is( $templates->[1]->{profile_id},       1,            'profile_id       is good' );
434 is( $templates->[1]->{template_code},    'TEMPL1',     'template_code    is good' );
435 is( $templates->[1]->{template_desc},    'Template 1', 'template_desc    is good' );
436 is( $templates->[1]->{page_width},       100,          'page_width       is good' );
437 is( $templates->[1]->{page_height},      150,          'page_height      is good' );
438 is( $templates->[1]->{label_width},      10,           'label_width      is good' );
439 is( $templates->[1]->{label_height},     15,           'label_height     is good' );
440 is( $templates->[1]->{top_text_margin},  2,            'top_text_margin  is good' );
441 is( $templates->[1]->{left_text_margin}, 3,            'left_text_margin is good' );
442 is( $templates->[1]->{top_margin},       1,            'top_margin       is good' );
443 is( $templates->[1]->{left_margin},      4,            'left_margin      is good' );
444 is( $templates->[1]->{cols},             9,            'cols             is good' );
445 is( $templates->[1]->{rows},             6,            'rows             is good' );
446 is( $templates->[1]->{col_gap},          0.1,          'col_gap          is good' );
447 is( $templates->[1]->{row_gap},          0.2,          'row_gap          is good' );
448 is( $templates->[1]->{units},            'POINT',      'units            is good' );
449 is( $templates->[1]->{creator},          'Labels',     'creator          is good' );
450
451 # ---------- Testing get_all_layouts ----------------------
452 # Mocking $sth->err and $sth->errstr
453 {
454     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
455     $dbi_st->mock( 'err',    sub { return 1; } );
456     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
457     my $layouts;
458     warning_is { $layouts = get_all_layouts() } 'Database returned the following error: something went wrong',
459       'get_all_layouts() raises warning if something went wrong with the sql request execution';
460
461     is( $layouts, -1, '$layouts return -1' );
462 }
463
464 # Without params ----------------------
465 my $layouts = get_all_layouts();
466
467 $query = '
468   SELECT count(*)
469   FROM   creator_layouts
470   ';
471 $count = $dbh->selectrow_array($query);
472 is( $count,    2,      'There are 2 layouts' );
473 is( @$layouts, $count, 'There are 2 layouts matching' );
474 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
475
476 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
477 is( $layouts->[0]->{barcode_type},  'COOP2OF5',    'barcode_type   is good' );
478 is( $layouts->[0]->{start_label},   1,             'start_label    is good' );
479 is( $layouts->[0]->{printing_type}, 'BAR1',        'printing_type  is good' );
480 is( $layouts->[0]->{layout_name},   'NAME1',       'layout_name    is good' );
481 is( $layouts->[0]->{guidebox},      1,             'guidebox       is good' );
482 is( $layouts->[0]->{font},          'TR',          'font           is good' );
483 is( $layouts->[0]->{font_size},     11,            'font_size      is good' );
484 is( $layouts->[0]->{units},         'POINT',       'units          is good' );
485 is( $layouts->[0]->{callnum_split}, 1,             'callnum_split  is good' );
486 is( $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
487 is( $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
488 is( $layouts->[0]->{layout_xml},    'layout_xml1', 'layout_xml     is good' );
489 is( $layouts->[0]->{creator},       'Labels',      'creator        is good' );
490
491 isa_ok( $layouts->[1], 'HASH', '$layouts->[1]  is a HASH' );
492 is( $layouts->[1]->{barcode_type},  'EAN13',       'barcode_type   is good' );
493 is( $layouts->[1]->{start_label},   2,             'start_label    is good' );
494 is( $layouts->[1]->{printing_type}, 'BAR2',        'printing_type  is good' );
495 is( $layouts->[1]->{layout_name},   'NAME2',       'layout_name    is good' );
496 is( $layouts->[1]->{guidebox},      2,             'guidebox       is good' );
497 is( $layouts->[1]->{font},          'TR',          'font           is good' );
498 is( $layouts->[1]->{font_size},     12,            'font_size      is good' );
499 is( $layouts->[1]->{units},         'POINT',       'units          is good' );
500 is( $layouts->[1]->{callnum_split}, 2,             'callnum_split  is good' );
501 is( $layouts->[1]->{text_justify},  'L',           'text_justify   is good' );
502 is( $layouts->[1]->{format_string}, 'barcode',     'format_string  is good' );
503 is( $layouts->[1]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
504 is( $layouts->[1]->{creator},       'Labels',      'creator        is good' );
505
506 # With field_list params --------------
507 $layouts = get_all_layouts( { fields => [qw(barcode_type layout_name font)] });
508
509 $query = '
510   SELECT count(*)
511   FROM   creator_layouts
512   ';
513 $count = $dbh->selectrow_array($query);
514 is( $count,    2,      'There are 2 layouts' );
515 is( @$layouts, $count, 'There are 2 layouts matching' );
516 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
517
518 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
519 is  (        $layouts->[0]->{barcode_type},  'COOP2OF5',    'barcode_type   is good' );
520 isnt( exists $layouts->[0]->{start_label},   1,             'start_label    is good' );
521 isnt( exists $layouts->[0]->{printing_type}, 'BAR1',        'printing_type  is good' );
522 is  (        $layouts->[0]->{layout_name},   'NAME1',       'layout_name    is good' );
523 isnt( exists $layouts->[0]->{guidebox},      1,             'guidebox       is good' );
524 is  (        $layouts->[0]->{font},         'TR',           'font           is good' );
525 isnt( exists $layouts->[0]->{font_size},     11,            'font_size      is good' );
526 isnt( exists $layouts->[0]->{units},         'POINT',       'units          is good' );
527 isnt( exists $layouts->[0]->{callnum_split}, 1,             'callnum_split  is good' );
528 isnt( exists $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
529 isnt( exists $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
530 isnt( exists $layouts->[0]->{layout_xml},    'layout_xml1', 'layout_xml     is good' );
531 isnt( exists $layouts->[0]->{creator},       'Labels',      'creator        is good' );
532
533 isa_ok( $layouts->[1], 'HASH', '$layouts->[1] is a HASH' );
534 is  (        $layouts->[1]->{barcode_type},   'EAN13',      'barcode_type   is good' );
535 isnt( exists $layouts->[1]->{start_label},    2,            'start_label    is good' );
536 isnt( exists $layouts->[1]->{printing_type},  'BAR2',       'printing_type  is good' );
537 is  (        $layouts->[1]->{layout_name},    'NAME2',      'layout_name    is good' );
538 isnt( exists $layouts->[1]->{guidebox},       2,            'guidebox       is good' );
539 is  (        $layouts->[1]->{font},          'TR',          'font           is good' );
540 isnt( exists $layouts->[1]->{font_size},      12,           'font_size      is good' );
541 isnt( exists $layouts->[1]->{units},         'POINT',       'units          is good' );
542 isnt( exists $layouts->[1]->{callnum_split},  2,            'callnum_split  is good' );
543 isnt( exists $layouts->[1]->{text_justify},  'L',           'text_justify   is good' );
544 isnt( exists $layouts->[1]->{format_string}, 'barcode',     'format_string  is good' );
545 isnt( exists $layouts->[1]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
546 isnt( exists $layouts->[1]->{creator},       'Labels',      'creator        is good' );
547
548 # With filters params ------------------
549 $layouts = get_all_layouts( { filters => { font_size => 12 } } );
550
551 $query = '
552   SELECT count(*)
553   FROM   creator_layouts
554   WHERE  font_size = 12
555   ';
556 $count = $dbh->selectrow_array($query);
557 is( $count,    1,      'There is 1 layout matching' );
558 is( @$layouts, $count, 'There is 1 layout matching' );
559 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
560
561 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
562 is( $layouts->[0]->{barcode_type},  'EAN13',       'barcode_type   is good' );
563 is( $layouts->[0]->{start_label},   2,             'start_label    is good' );
564 is( $layouts->[0]->{printing_type}, 'BAR2',        'printing_type  is good' );
565 is( $layouts->[0]->{layout_name},   'NAME2',       'layout_name    is good' );
566 is( $layouts->[0]->{guidebox},      2,             'guidebox       is good' );
567 is( $layouts->[0]->{font},          'TR',          'font           is good' );
568 is( $layouts->[0]->{font_size},     12,            'font_size      is good' );
569 is( $layouts->[0]->{units},         'POINT',       'units          is good' );
570 is( $layouts->[0]->{callnum_split}, 2,             'callnum_split  is good' );
571 is( $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
572 is( $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
573 is( $layouts->[0]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
574 is( $layouts->[0]->{creator},       'Labels',      'creator        is good' );
575
576 # With orderby param ------------------
577 $layouts = get_all_layouts( { orderby => 'font_size DESC' } );
578
579 $query = '
580   SELECT   count(*)
581   FROM     creator_layouts
582   ORDER BY font_size DESC
583   ';
584 $count = $dbh->selectrow_array($query);
585 is( $count,    2,      'There are layout matching' );
586 is( @$layouts, $count, 'There are 2 layouts matching' );
587 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
588
589 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
590 is( $layouts->[0]->{barcode_type},  'EAN13',       'barcode_type   is good' );
591 is( $layouts->[0]->{start_label},   2,             'start_label    is good' );
592 is( $layouts->[0]->{printing_type}, 'BAR2',        'printing_type  is good' );
593 is( $layouts->[0]->{layout_name},   'NAME2',       'layout_name    is good' );
594 is( $layouts->[0]->{guidebox},      2,             'guidebox       is good' );
595 is( $layouts->[0]->{font},          'TR',          'font           is good' );
596 is( $layouts->[0]->{font_size},     12,            'font_size      is good' );
597 is( $layouts->[0]->{units},         'POINT',       'units          is good' );
598 is( $layouts->[0]->{callnum_split}, 2,             'callnum_split  is good' );
599 is( $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
600 is( $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
601 is( $layouts->[0]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
602 is( $layouts->[0]->{creator},       'Labels',      'creator        is good' );
603
604 isa_ok( $layouts->[1], 'HASH', '$layouts->[1]  is a HASH' );
605 is( $layouts->[1]->{barcode_type},  'COOP2OF5',    'barcode_type   is good' );
606 is( $layouts->[1]->{start_label},   1,             'start_label    is good' );
607 is( $layouts->[1]->{printing_type}, 'BAR1',        'printing_type  is good' );
608 is( $layouts->[1]->{layout_name},   'NAME1',       'layout_name    is good' );
609 is( $layouts->[1]->{guidebox},      1,             'guidebox       is good' );
610 is( $layouts->[1]->{font},          'TR',          'font           is good' );
611 is( $layouts->[1]->{font_size},     11,            'font_size      is good' );
612 is( $layouts->[1]->{units},         'POINT',       'units          is good' );
613 is( $layouts->[1]->{callnum_split}, 1,             'callnum_split  is good' );
614 is( $layouts->[1]->{text_justify},  'L',           'text_justify   is good' );
615 is( $layouts->[1]->{format_string}, 'barcode',     'format_string  is good' );
616 is( $layouts->[1]->{layout_xml},    'layout_xml1', 'layout_xml     is good' );
617 is( $layouts->[1]->{creator},       'Labels',      'creator        is good' );
618
619 # ---------- Testing get_all_profiles ---------------------
620 # Mocking $sth->err and $sth->errstr
621 {
622     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
623     $dbi_st->mock( 'err',    sub { return 1; } );
624     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
625     my $profiles;
626     warning_is { $profiles = get_all_profiles() } 'Database returned the following error: something went wrong',
627       'get_all_profiles() raises warning if something went wrong with the sql request execution';
628
629     is( $profiles, -1, '$profiles return -1' );
630 }
631
632 # Without params ----------------------
633 my $profiles = get_all_profiles();
634
635 $query = '
636   SELECT count(*)
637   FROM   printers_profile
638   ';
639 $count = $dbh->selectrow_array($query);
640 is( $count,     2,      'There are 2 profiles' );
641 is( @$profiles, $count, 'There are 2 profiles matching' );
642 isa_ok( $profiles, 'ARRAY', '$profiles is an ARRAY' );
643
644 isa_ok( $profiles->[0], 'HASH', '$profiles->[0] is a HASH' );
645 is( $profiles->[0]->{printer_name}, 'Layout1 Name', 'printer_name   is good' );
646 is( $profiles->[0]->{template_id},  1234,           'template_id    is good' );
647 is( $profiles->[0]->{paper_bin},    'Bypass',       'paper_bin      is good' );
648 is( $profiles->[0]->{offset_horz},  0.1,            'offset_horz    is good' );
649 is( $profiles->[0]->{offset_vert},  0.2,            'offset_vert    is good' );
650 is( $profiles->[0]->{creep_horz},   0.3,            'creep_horz     is good' );
651 is( $profiles->[0]->{creep_vert},   0.4,            'creep_vert     is good' );
652 is( $profiles->[0]->{units},        'POINT',        'units          is good' );
653 is( $profiles->[0]->{creator},      'Labels',       'creator        is good' );
654
655 isa_ok( $profiles->[1], 'HASH', '$profiles->[1] is a HASH' );
656 is( $profiles->[1]->{printer_name}, 'Layout2 Name', 'printer_name   is good' );
657 is( $profiles->[1]->{template_id},  1235,           'template_id    is good' );
658 is( $profiles->[1]->{paper_bin},    'Bypass',       'paper_bin      is good' );
659 is( $profiles->[1]->{offset_horz},  0.2,            'offset_horz    is good' );
660 is( $profiles->[1]->{offset_vert},  0.3,            'offset_vert    is good' );
661 is( $profiles->[1]->{creep_horz},   0.4,            'creep_horz     is good' );
662 is( $profiles->[1]->{creep_vert},   0.5,            'creep_vert     is good' );
663 is( $profiles->[1]->{units},        'POINT',        'units          is good' );
664 is( $profiles->[1]->{creator},      'Labels',       'creator        is good' );
665
666 # With field_list params --------------
667 $profiles = get_all_profiles( { fields => [qw(printer_name template_id)] });
668
669 $query = '
670   SELECT count(*)
671   FROM   printers_profile
672   ';
673 $count = $dbh->selectrow_array($query);
674 is( $count,     2,      'There are 2 profiles' );
675 is( @$profiles, $count, 'There are 2 profiles matching' );
676 isa_ok( $profiles, 'ARRAY', '$profiles is an ARRAY' );
677
678 isa_ok( $profiles->[0], 'HASH', '$profiles->[0] is a HASH' );
679 is  (        $profiles->[0]->{printer_name},  'Layout1 Name', 'printer_name   is good' );
680 is  (        $profiles->[0]->{template_id},   1234,           'template_id    is good' );
681 isnt( exists $profiles->[0]->{paper_bin},     'Bypass',       'paper_bin      is good' );
682 isnt( exists $profiles->[0]->{offset_horz},   0.1,            'offset_horz    is good' );
683 isnt( exists $profiles->[0]->{offset_vert},   0.2,            'offset_vert    is good' );
684 isnt( exists $profiles->[0]->{creep_horz},    0.3,            'creep_horz     is good' );
685 isnt( exists $profiles->[0]->{creep_vert},    0.4,            'creep_vert     is good' );
686 isnt( exists $profiles->[0]->{units},         'POINT',        'units          is good' );
687 isnt( exists $profiles->[0]->{creator},       'Labels',       'creator        is good' );
688
689 isa_ok( $profiles->[1], 'HASH', '$profiles->[1] is a HASH' );
690 is  (        $profiles->[1]->{printer_name},  'Layout2 Name', 'printer_name   is good' );
691 is  (        $profiles->[1]->{template_id},   1235,           'template_id    is good' );
692 isnt( exists $profiles->[1]->{paper_bin},     'Bypass',       'paper_bin      is good' );
693 isnt( exists $profiles->[1]->{offset_horz},   0.2,            'offset_horz    is good' );
694 isnt( exists $profiles->[1]->{offset_vert},   0.3,            'offset_vert    is good' );
695 isnt( exists $profiles->[1]->{creep_horz},    0.4,            'creep_horz     is good' );
696 isnt( exists $profiles->[1]->{creep_vert},    0.5,            'creep_vert     is good' );
697 isnt( exists $profiles->[1]->{units},         'POINT',        'units          is good' );
698 isnt( exists $profiles->[1]->{creator},       'Labels',       'creator        is good' );
699
700 # With filters params ------------------
701 $profiles = get_all_profiles( { filters => { template_id => 1235 } } );
702
703 $query = '
704   SELECT count(*)
705   FROM   printers_profile
706   WHERE  template_id = 1235
707   ';
708 $count = $dbh->selectrow_array($query);
709 is( $count,     1,      'There is 1 profile matching' );
710 is( @$profiles, $count, 'There is 1 profile matching' );
711 isa_ok( $profiles, 'ARRAY', '$profiles is an ARRAY' );
712
713 isa_ok( $profiles->[0], 'HASH', '$profiles->[0] is a HASH' );
714 is  (        $profiles->[0]->{printer_name},  'Layout2 Name', 'printer_name   is good' );
715 is  (        $profiles->[0]->{template_id},   1235,           'template_id    is good' );
716 isnt( exists $profiles->[0]->{paper_bin},     'Bypass',       'paper_bin      is good' );
717 isnt( exists $profiles->[0]->{offset_horz},   0.2,            'offset_horz    is good' );
718 isnt( exists $profiles->[0]->{offset_vert},   0.3,            'offset_vert    is good' );
719 isnt( exists $profiles->[0]->{creep_horz},    0.4,            'creep_horz     is good' );
720 isnt( exists $profiles->[0]->{creep_vert},    0.5,            'creep_vert     is good' );
721 isnt( exists $profiles->[0]->{units},         'POINT',        'units          is good' );
722 isnt( exists $profiles->[0]->{creator},       'Labels',       'creator        is good' );
723
724 # ---------- Testing get_all_image_names ------------------
725
726 # Mocking $sth->err and $sth->errstr
727 {
728     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
729     $dbi_st->mock( 'err',    sub { return 1; } );
730     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
731     my $images;
732     warning_is { $images = get_all_image_names() } 'Database returned the following error: something went wrong',
733       'get_all_image_names() raises warning if something went wrong with the sql request execution';
734
735     is( $images, -1, '$images return -1' );
736 }
737
738 # Without params ----------------------
739 my $images = get_all_image_names();
740
741 $query = '
742   SELECT count(*)
743   FROM   creator_images
744   ';
745 $count = $dbh->selectrow_array($query);
746 is( $count,   2,      'There are 2 images' );
747 is( @$images, $count, 'There are 2 images matching' );
748 isa_ok( $images, 'ARRAY', '$images is an ARRAY' );
749
750 isa_ok( $images->[0], 'HASH', '$images->[0] is a HASH' );
751 is( $images->[0]->{name},     'Image 1',            'name         is good' );
752 is( $images->[0]->{selected}, 0,                    'selected     is good' );
753 is( $images->[0]->{type},     $images->[0]->{name}, 'type         is good' );
754
755 isa_ok( $images->[1], 'HASH', '$images->[1] is a HASH' );
756 is( $images->[1]->{name},     'Image 2',            'name         is good' );
757 is( $images->[1]->{selected}, 0,                    'selected     is good' );
758 is( $images->[1]->{type},     $images->[1]->{name}, 'type         is good' );
759
760 # ---------- Testing get_batch_summary --------------------
761
762 # Mocking $sth->err and $sth->errstr
763 {
764     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
765     $dbi_st->mock( 'err',    sub { return 1; } );
766     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
767     my $batches;
768     warning_is { $batches = get_batch_summary() } 'Database returned the following error on attempted SELECT: something went wrong',
769       'get_batch_summary() raises warning if something went wrong with the sql request execution';
770
771     is( $batches, -1, '$batches return -1' );
772 }
773
774 # Without creator params --------------
775 my $batches = get_batch_summary( { filters => { creator => 'Labels' } } );
776
777 $query = '
778   SELECT   batch_id, count(batch_id)
779   FROM     creator_batches
780   WHERE    creator = ?
781   GROUP BY batch_id
782   ';
783 my $sth = $dbh->prepare($query);
784 $sth->execute('Labels');
785 $count = $sth->rows;
786 is( $count,    2,      'There are 2 batches' );
787 is( @$batches, $count, 'There are 2 batches matching' );
788 isa_ok( $batches, 'ARRAY', '$batches is an ARRAY' );
789
790 $query = '
791   SELECT count(batch_id)
792   FROM   creator_batches
793   WHERE  creator = ?
794     AND  batch_id = ?
795   ';
796 $count = $dbh->selectrow_array( $query, {}, 'Labels', 11 );
797 is( $count, 1, 'There is 1 batch where batch_id = 11' );
798
799 isa_ok( $batches->[0], 'HASH', '$batches->[0] is a HASH' );
800 is( $batches->[0]->{batch_id},    11,     'batch_id      is good' );
801 is( $batches->[0]->{_item_count}, $count, 'item_number   is good for this batch_id' );
802
803 $count = $dbh->selectrow_array( $query, {}, 'Labels', 12 );
804 is( $count, 2, 'There are 2 batches where batch_id = 12' );
805
806 isa_ok( $batches->[1], 'HASH', '$batches->[1] is a HASH' );
807 is( $batches->[1]->{batch_id},    12,     'batch_id      is good' );
808 is( $batches->[1]->{_item_count}, $count, 'item_number   is good for this batch_id' );
809
810 # Without filters -----
811 $batches = get_batch_summary( { filters => { branch_code => $library1->{branchcode}, creator => 'Labels' } } );
812 is( @$batches, 1, 'There is 1 batch matching' );
813
814 $query = '
815   SELECT   batch_id, count(batch_id)
816   FROM     creator_batches
817   WHERE    creator = ?
818     AND    branch_code = ?
819   GROUP BY batch_id
820   ';
821 my ( $id, $nb ) = $dbh->selectrow_array( $query, {}, 'Labels', $library1->{branchcode} );
822
823 is( $batches->[0]->{batch_id},    $id, 'batch_id    is good' );
824 is( $batches->[0]->{_item_count}, $nb, 'item_number is good for this batch_id' );
825
826 # ---------- Testing get_label_summary --------------------
827 # Mocking $sth->err and $sth->errstr
828 {
829     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
830     $dbi_st->mock( 'err',    sub { return 1; } );
831     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
832     my $labels;
833     my @items = [ { item_number => $item_number1 } ];
834
835     warning_is { $labels = get_label_summary( items => @items ) } 'Database returned the following error on attempted SELECT: something went wrong',
836       'get_label_summary() raises warning if something went wrong with the sql request execution';
837
838     is( $labels, -1, '$labels return -1' );
839 }
840
841 # Without params ----------------------
842 my $labels = get_label_summary();
843
844 isa_ok( $labels, 'ARRAY', '$labels is an ARRAY' );
845 is( @$labels, 0, '$labels is empty' );
846
847 # With items param --------------------
848 $query = '
849   SELECT biblionumber, title, author
850   FROM   biblio
851   WHERE  biblionumber = ?
852   ';
853 my ( $b_biblionumber1, $b_title1, $b_author1 ) = $dbh->selectrow_array( $query, {}, $biblionumber1 );
854 my ( $b_biblionumber2, $b_title2, $b_author2 ) = $dbh->selectrow_array( $query, {}, $biblionumber2 );
855
856 $query = '
857   SELECT biblionumber, biblioitemnumber, itemtype
858   FROM   biblioitems
859   WHERE  biblioitemnumber = ?
860   ';
861 my ( $bi_biblionumber1, $bi_biblioitemnumber1, $bi_itemtype1 ) = $dbh->selectrow_array( $query, {}, $biblioitemnumber1 );
862 my ( $bi_biblionumber2, $bi_biblioitemnumber2, $bi_itemtype2 ) = $dbh->selectrow_array( $query, {}, $biblioitemnumber2 );
863
864 $query = '
865   SELECT biblionumber, biblioitemnumber, itemnumber, barcode, itype
866   FROM   items
867   WHERE  itemnumber = ?
868   ';
869 my ( $i_biblionumber1, $i_biblioitemnumber1, $i_itemnumber1, $i_barcode1, $i_itype1 ) = $dbh->selectrow_array( $query, {}, $item_number1 );
870 my ( $i_biblionumber2, $i_biblioitemnumber2, $i_itemnumber2, $i_barcode2, $i_itype2 ) = $dbh->selectrow_array( $query, {}, $item_number2 );
871
872 $query = '
873   SELECT label_id, batch_id, item_number
874   FROM   creator_batches
875   WHERE  item_number = ?
876   ';
877 my ( $c_label_id1, $c_batch_id1, $c_item_number1 ) = $dbh->selectrow_array( $query, {}, $item_number1 );
878 my ( $c_label_id2, $c_batch_id2, $c_item_number2 ) = $dbh->selectrow_array( $query, {}, $item_number2 );
879
880 is( $c_item_number1,      $i_itemnumber1,        'CREATOR_BATCHES.item_number == ITEMS.itemnumber' );
881 is( $i_biblioitemnumber1, $bi_biblioitemnumber1, 'ITEMS.biblioitemnumber      == BIBLIOITEMS.biblioitemnumber' );
882 is( $bi_biblionumber1,    $b_biblionumber1,      'BIBLIOITEMS.biblionumber    == BIBLIO.biblionumber' );
883
884 is( $c_item_number2,      $i_itemnumber2,        'CREATOR_BATCHES.item_number == ITEMS.itemnumber' );
885 is( $i_biblioitemnumber2, $bi_biblioitemnumber2, 'ITEMS.biblioitemnumber      == BIBLIOITEMS.biblioitemnumber' );
886 is( $bi_biblionumber2,    $b_biblionumber2,      'BIBLIOITEMS.biblionumber    == BIBLIO.biblionumber' );
887
888 my @items = [
889     {   item_number => $item_number1,
890         label_id    => $c_label_id1,
891     }
892 ];
893 $labels = get_label_summary( items => @items, batch_id => $c_batch_id1 );
894
895 is( @$labels, 1, 'There is 1 label for $item_number1' );
896 isa_ok( $labels,      'ARRAY', '$labels      is an array' );
897 isa_ok( $labels->[0], 'HASH',  '$labels->[0] is an hash' );
898
899 my $record_author = $b_author1;
900 my $record_title  = $b_title1;
901 $record_author =~ s/[^\.|\w]$//;
902 $record_title  =~ s/\W*$//;
903 $record_title = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $b_biblionumber1 . '"> ' . $b_title1 . '</a>';
904 my $summary1        = $record_title . " | " . ( $b_author1 ? $b_author1 : 'N/A' );
905 my $itemtypes_pref  = C4::Context->preference("item-level_itypes");
906 my $record_itemtype = $itemtypes_pref ? $i_itype1 : $bi_itemtype1;
907
908 is( $labels->[0]->{_label_number}, 1,                '_label_number  is good' );
909 is( $labels->[0]->{_summary},      $summary1,        '_summary       is good' );
910 is( $labels->[0]->{_item_type},    $record_itemtype, '_item_type     is good' );
911 is( $labels->[0]->{_barcode},      $i_barcode1,      '_barcode       is good' );
912 is( $labels->[0]->{_item_number},  $i_itemnumber1,   '_item_number   is good' );
913 is( $labels->[0]->{_label_id},     $c_label_id1,     '_label_id      is good' );
914
915 # record without author
916 @items = [
917     {   item_number => $item_number2,
918         label_id    => $c_label_id2,
919     }
920 ];
921 $labels = get_label_summary( items => @items, batch_id => $c_batch_id2 );
922
923 is( @$labels, 1, 'There is 1 label for $item_number2' );
924 isa_ok( $labels,      'ARRAY', '$labels      is an array' );
925 isa_ok( $labels->[0], 'HASH',  '$labels->[0] is an hash' );
926
927 $record_author = $b_author2;
928 $record_title  = $b_title2;
929 $record_author =~ s/[^\.|\w]$// if $b_author2;
930 $record_title =~ s/\W*$//;
931 $record_title = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $b_biblionumber2 . '"> ' . $b_title2 . '</a>';
932 my $summary2 = $record_title . " | " . ( $b_author2 ? $b_author2 : 'N/A' );
933 $itemtypes_pref = C4::Context->preference("item-level_itypes");
934 $record_itemtype = $itemtypes_pref ? $i_itype2 : $bi_itemtype2;
935
936 is( $labels->[0]->{_label_number}, 1,                '_label_number  is good' );
937 is( $labels->[0]->{_summary},      $summary2,        '_summary       is good' );
938 is( $labels->[0]->{_item_type},    $record_itemtype, '_item_type     is good' );
939 is( $labels->[0]->{_barcode},      $i_barcode2,      '_barcode       is good' );
940 is( $labels->[0]->{_item_number},  $i_itemnumber2,   '_item_number   is good' );
941 is( $labels->[0]->{_label_id},     $c_label_id2,     '_label_id      is good' );
942
943 #Mocking C4::Context->preference("item-level_itypes")
944 {
945     t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
946     my $h = C4::Context->preference("item-level_itypes");
947
948     my @items = [
949         {   item_number => $item_number1,
950             label_id    => $c_label_id1,
951         }
952     ];
953     $labels = get_label_summary( items => @items, batch_id => $c_batch_id1 );
954
955     is( @$labels, 1, 'There is 1 label for $item_number1' );
956     isa_ok( $labels,      'ARRAY', '$labels      is an array' );
957     isa_ok( $labels->[0], 'HASH',  '$labels->[0] is an hash' );
958
959     my $record_author = $b_author1;
960     my $record_title  = $b_title1;
961     $record_author =~ s/[^\.|\w]$//;
962     $record_title  =~ s/\W*$//;
963     $record_title = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $b_biblionumber1 . '"> ' . $b_title1 . '</a>';
964     my $summary1        = $record_title . " | " . ( $b_author1 ? $b_author1 : 'N/A' );
965     my $itemtypes_pref  = C4::Context->preference("item-level_itypes");
966     my $record_itemtype = $itemtypes_pref ? $i_itype1 : $bi_itemtype1;
967
968     is( $labels->[0]->{_label_number}, 1,                '_label_number  is good' );
969     is( $labels->[0]->{_summary},      $summary1,        '_summary       is good' );
970     is( $labels->[0]->{_item_type},    $record_itemtype, '_item_type     is good' );
971     is( $labels->[0]->{_barcode},      $i_barcode1,      '_barcode       is good' );
972     is( $labels->[0]->{_item_number},  $i_itemnumber1,   '_item_number   is good' );
973     is( $labels->[0]->{_label_id},     $c_label_id1,     '_label_id      is good' );
974 }
975
976 # ---------- Testing get_card_summary ---------------------
977 # Mocking $sth->err and $sth->errstr
978 {
979     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
980     $dbi_st->mock( 'err',    sub { return 1; } );
981     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
982     my $cards;
983     my @items = [ { item_number => $item_number1 } ];
984
985     warning_is { $cards = get_card_summary( items => @items ) } 'Database returned the following error on attempted SELECT: something went wrong',
986       'get_card_summary() raises warning if something went wrong with the sql request execution';
987
988     is( $cards, -1, '$cards return -1' );
989 }
990
991 # Without params ----------------------
992 my $cards = get_card_summary();
993
994 isa_ok( $cards, 'ARRAY', '$cards is an ARRAY' );
995 is( @$cards, 0, '$cards is empty' );
996
997 # With items param --------------------
998 $query = '
999   SELECT surname, firstname, cardnumber
1000   FROM   borrowers
1001   WHERE  borrowernumber = ?
1002   ';
1003 my ( $b_surname1, $b_firstname1, $b_cardnumber1 ) = $dbh->selectrow_array( $query, {}, $borrowernumber1 );
1004
1005 @items = [
1006     {   item_number     => $item_number1,
1007         label_id        => $c_label_id1,
1008         borrower_number => $borrowernumber1,
1009     }
1010 ];
1011 $cards = get_card_summary( items => @items );
1012
1013 is( @$cards, 1, 'There is 1 card for $item_number1' );
1014 isa_ok( $cards,      'ARRAY', '$cards      is an array' );
1015 isa_ok( $cards->[0], 'HASH',  '$cards->[0] is an hash' );
1016
1017 my $name1 = "$b_surname1, $b_firstname1";
1018 is( $cards->[0]->{_card_number},   1,                '_card_number   is good' );
1019 is( $cards->[0]->{_summary},       $name1,           '_summary       is good' );
1020 is( $cards->[0]->{borrowernumber}, $borrowernumber1, 'borrowernumber is good' );
1021 is( $cards->[0]->{_label_id},      $c_label_id1,     '_label_id      is good' );
1022
1023 # ---------- Testing get_barcode_types --------------------
1024 my $barcode_types = get_barcode_types();
1025
1026 is( @$barcode_types, 6, 'There are 6 barcodes types' );
1027 isa_ok( $barcode_types, 'ARRAY', '$barcode_types is an ARRAY' );
1028
1029 isa_ok( $barcode_types->[0], 'HASH', '$barcode_types->[0] is a HASH' );
1030 is( $barcode_types->[0]->{type},     'CODE39',                                                                                                              'type is good' );
1031 is( $barcode_types->[0]->{name},     'Code 39',                                                                                                             'name is good' );
1032 is( $barcode_types->[0]->{desc},     'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern.', 'desc is good' );
1033 is( $barcode_types->[0]->{selected}, 0,                                                                                                                     'selected is good' );
1034
1035 isa_ok( $barcode_types->[1], 'HASH', '$barcode_types->[1] is a HASH' );
1036 is( $barcode_types->[1]->{type}, 'CODE39MOD',          'type is good' );
1037 is( $barcode_types->[1]->{name}, 'Code 39 + Modulo43', 'name is good' );
1038 is( $barcode_types->[1]->{desc},
1039     'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 43 checksum.',
1040     'desc is good'
1041 );
1042 is( $barcode_types->[1]->{selected}, 0, 'selected is good' );
1043
1044 isa_ok( $barcode_types->[2], 'HASH', '$barcode_types->[2] is a HASH' );
1045 is( $barcode_types->[2]->{type},     'CODE39MOD10',        'type is good' );
1046 is( $barcode_types->[2]->{name},     'Code 39 + Modulo10', 'name is good' );
1047 is( $barcode_types->[2]->{desc},     'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 10 checksum.', 'desc is good');
1048 is( $barcode_types->[2]->{selected}, 0,                    'selected is good' );
1049
1050 isa_ok( $barcode_types->[3], 'HASH', '$barcode_types->[3] is a HASH' );
1051 is( $barcode_types->[3]->{type},     'COOP2OF5', 'type is good' );
1052 is( $barcode_types->[3]->{name},     'COOP2of5', 'name is good' );
1053 is( $barcode_types->[3]->{desc},     'Creates COOP2of5 barcodes from a string consisting of the numeric characters 0-9', 'desc is good' );
1054 is( $barcode_types->[3]->{selected}, 0,          'selected is good' );
1055
1056 isa_ok( $barcode_types->[4], 'HASH', '$barcode_types->[4] is a HASH' );
1057 is( $barcode_types->[4]->{type},     'EAN13',     'type is good' );
1058 is( $barcode_types->[4]->{name},     'EAN13',     'name is good' );
1059 is( $barcode_types->[4]->{desc},     'Creates EAN13 barcodes from a string of 12 or 13 digits. The check number (the 13:th digit) is calculated if not supplied.', 'desc is good' );
1060 is( $barcode_types->[4]->{selected}, 0,           'selected is good' );
1061
1062 isa_ok( $barcode_types->[5], 'HASH', '$barcode_types->[5] is a HASH' );
1063 is( $barcode_types->[5]->{type},     'INDUSTRIAL2OF5', 'type is good' );
1064 is( $barcode_types->[5]->{name},     'Industrial2of5', 'name is good' );
1065 is( $barcode_types->[5]->{desc},     'Creates Industrial2of5 barcodes from a string consisting of the numeric characters 0-9', 'desc is good' );
1066 is( $barcode_types->[5]->{selected},  0,               'selected is good' );
1067
1068 # ---------- Testing get_label_types ----------------------
1069 my $label_types = get_label_types();
1070
1071 is( @$label_types, 5, 'There are 5 label types' );
1072 isa_ok( $label_types, 'ARRAY', '$label_types is an ARRAY' );
1073
1074 isa_ok( $label_types->[0], 'HASH', '$label_types->[0] is a HASH' );
1075 is( $label_types->[0]->{type},     'BIB',                                     'type     is good' );
1076 is( $label_types->[0]->{name},     'Biblio',                                  'name     is good' );
1077 is( $label_types->[0]->{desc},     'Only the bibliographic data is printed.', 'desc     is good' );
1078 is( $label_types->[0]->{selected}, 0,                                         'selected is good' );
1079
1080 isa_ok( $label_types->[1], 'HASH', '$label_types->[1] is a HASH' );
1081 is( $label_types->[1]->{type},     'BARBIB',                               'type     is good' );
1082 is( $label_types->[1]->{name},     'Barcode/Biblio',                       'name     is good' );
1083 is( $label_types->[1]->{desc},     'Barcode proceeds bibliographic data.', 'desc     is good' );
1084 is( $label_types->[1]->{selected}, 0,                                      'selected is good' );
1085
1086 isa_ok( $label_types->[2], 'HASH', '$label_types->[2] is a HASH' );
1087 is( $label_types->[2]->{type},     'BIBBAR',                               'type     is good' );
1088 is( $label_types->[2]->{name},     'Biblio/Barcode',                       'name     is good' );
1089 is( $label_types->[2]->{desc},     'Bibliographic data proceeds barcode.', 'desc     is good' );
1090 is( $label_types->[2]->{selected}, 0,                                      'selected is good' );
1091
1092 isa_ok( $label_types->[3], 'HASH', '$label_types->[3] is a HASH' );
1093 is( $label_types->[3]->{type},     'ALT',                                                               'type     is good' );
1094 is( $label_types->[3]->{name},     'Alternating',                                                       'name     is good' );
1095 is( $label_types->[3]->{desc},     'Barcode and bibliographic data are printed on alternating labels.', 'desc     is good' );
1096 is( $label_types->[3]->{selected}, 0,                                                                   'selected is good' );
1097
1098 isa_ok( $label_types->[4], 'HASH', '$label_types->[4] is a HASH' );
1099 is( $label_types->[4]->{type},     'BAR',                          'type     is good' );
1100 is( $label_types->[4]->{name},     'Barcode',                      'name     is good' );
1101 is( $label_types->[4]->{desc},     'Only the barcode is printed.', 'desc     is good' );
1102 is( $label_types->[4]->{selected}, 0,                              'selected is good' );
1103
1104 # ---------- Testing get_font_types -----------------------
1105 my $font_types = get_font_types();
1106
1107 is( @$font_types, 12, 'There are 12 font types' );
1108 isa_ok( $font_types, 'ARRAY', '$font_types is an ARRAY' );
1109
1110 isa_ok( $font_types->[0], 'HASH', '$font_types->[0] is a HASH' );
1111 is( $font_types->[0]->{type},     'TR',                      'type     is good' );
1112 is( $font_types->[0]->{name},     'Times-Roman',             'name     is good' );
1113 is( $font_types->[0]->{selected}, 0,                         'selected is good' );
1114
1115 isa_ok( $font_types->[1], 'HASH', '$font_types->[1] is a HASH' );
1116 is( $font_types->[1]->{type},     'TB',                      'type     is good' );
1117 is( $font_types->[1]->{name},     'Times-Bold',              'name     is good' );
1118 is( $font_types->[1]->{selected}, 0,                         'selected is good' );
1119
1120 isa_ok( $font_types->[2], 'HASH', '$font_types->[2] is a HASH' );
1121 is( $font_types->[2]->{type},     'TI',                      'type     is good' );
1122 is( $font_types->[2]->{name},     'Times-Italic',            'name     is good' );
1123 is( $font_types->[2]->{selected}, 0,                         'selected is good' );
1124
1125 isa_ok( $font_types->[3], 'HASH', '$font_types->[3] is a HASH' );
1126 is( $font_types->[3]->{type},     'TBI',                     'type     is good' );
1127 is( $font_types->[3]->{name},     'Times-Bold-Italic',       'name     is good' );
1128 is( $font_types->[3]->{selected}, 0,                         'selected is good' );
1129
1130 isa_ok( $font_types->[4], 'HASH', '$font_types->[4] is a HASH' );
1131 is( $font_types->[4]->{type},     'C',                       'type     is good' );
1132 is( $font_types->[4]->{name},     'Courier',                 'name     is good' );
1133 is( $font_types->[4]->{selected}, 0,                         'selected is good' );
1134
1135 isa_ok( $font_types->[5], 'HASH', '$font_types->[5] is a HASH' );
1136 is( $font_types->[5]->{type},     'CB',                      'type     is good' );
1137 is( $font_types->[5]->{name},     'Courier-Bold',            'name     is good' );
1138 is( $font_types->[5]->{selected}, 0,                         'selected is good' );
1139
1140 isa_ok( $font_types->[6], 'HASH', '$font_types->[6] is a HASH' );
1141 is( $font_types->[6]->{type},     'CO',                      'type     is good' );
1142 is( $font_types->[6]->{name},     'Courier-Oblique',         'name     is good' );
1143 is( $font_types->[6]->{selected}, 0,                         'selected is good' );
1144
1145 isa_ok( $font_types->[7], 'HASH', '$font_types->[7] is a HASH' );
1146 is( $font_types->[7]->{type},     'CBO',                     'type     is good' );
1147 is( $font_types->[7]->{name},     'Courier-Bold-Oblique',    'name     is good' );
1148 is( $font_types->[7]->{selected}, 0,                         'selected is good' );
1149
1150 isa_ok( $font_types->[8], 'HASH', '$font_types->[8] is a HASH' );
1151 is( $font_types->[8]->{type},     'H',                       'type     is good' );
1152 is( $font_types->[8]->{name},     'Helvetica',               'name     is good' );
1153 is( $font_types->[8]->{selected}, 0,                         'selected is good' );
1154
1155 isa_ok( $font_types->[9], 'HASH', '$font_types->[9] is a HASH' );
1156 is( $font_types->[9]->{type},     'HO',                      'type     is good' );
1157 is( $font_types->[9]->{name},     'Helvetica-Oblique',       'name     is good' );
1158 is( $font_types->[9]->{selected}, 0,                         'selected is good' );
1159
1160 isa_ok( $font_types->[10], 'HASH', '$font_types->[10] is a HASH' );
1161 is( $font_types->[10]->{type},     'HB',                     'type     is good' );
1162 is( $font_types->[10]->{name},     'Helvetica-Bold',         'name     is good' );
1163 is( $font_types->[10]->{selected}, 0,                        'selected is good' );
1164
1165 isa_ok( $font_types->[11], 'HASH', '$font_types->[11] is a HASH' );
1166 is( $font_types->[11]->{type},     'HBO',                    'type     is good' );
1167 is( $font_types->[11]->{name},     'Helvetica-Bold-Oblique', 'name     is good' );
1168 is( $font_types->[11]->{selected}, 0,                        'selected is good' );
1169
1170 # ---------- Testing get_text_justification_types ---------
1171 my $text_justification_types = get_text_justification_types();
1172
1173 is( @$text_justification_types, 3, 'There are 3 text justification types' );
1174 isa_ok( $text_justification_types, 'ARRAY', '$text_justification_types is an ARRAY' );
1175
1176 isa_ok( $text_justification_types->[0], 'HASH', '$font_types->[0] is a HASH' );
1177 is( $text_justification_types->[0]->{type},     'L',         'type     is good' );
1178 is( $text_justification_types->[0]->{name},     'Left',      'name     is good' );
1179 is( $text_justification_types->[0]->{selected}, 0,           'selected is good' );
1180
1181 isa_ok( $text_justification_types->[1], 'HASH', '$font_types->[1] is a HASH' );
1182 is( $text_justification_types->[1]->{type},     'C',         'type     is good' );
1183 is( $text_justification_types->[1]->{name},     'Center',    'name     is good' );
1184 is( $text_justification_types->[1]->{selected}, 0,           'selected is good' );
1185
1186 isa_ok( $text_justification_types->[2], 'HASH', '$font_types->[2] is a HASH' );
1187 is( $text_justification_types->[2]->{type},     'R',         'type     is good' );
1188 is( $text_justification_types->[2]->{name},     'Right',     'name     is good' );
1189 is( $text_justification_types->[2]->{selected}, 0,           'selected is good' );
1190
1191 # ---------- Testing get_unit_values ----------------------
1192 my $unit_values = get_unit_values();
1193
1194 is( @$unit_values, 5, 'There are 5 unit values' );
1195 isa_ok( $unit_values, 'ARRAY', '$unit_values is an ARRAY' );
1196
1197 isa_ok( $unit_values->[0], 'HASH', '$unit_values->[0] is a HASH' );
1198 is( $unit_values->[0]->{type},     'POINT',                  'type     is good' );
1199 is( $unit_values->[0]->{desc},     'PostScript Points',      'desc     is good' );
1200 is( $unit_values->[0]->{value},    1,                        'value    is good' );
1201 is( $unit_values->[0]->{selected}, 0,                        'selected is good' );
1202
1203 isa_ok( $unit_values->[1], 'HASH', '$unit_values->[1] is a HASH' );
1204 is( $unit_values->[1]->{type},     'AGATE',                  'type     is good' );
1205 is( $unit_values->[1]->{desc},     'Adobe Agates',           'desc     is good' );
1206 is( $unit_values->[1]->{value},    5.1428571,                'value    is good' );
1207 is( $unit_values->[1]->{selected}, 0,                        'selected is good' );
1208
1209 isa_ok( $unit_values->[2], 'HASH', '$unit_values->[2] is a HASH' );
1210 is( $unit_values->[2]->{type},     'INCH',                   'type     is good' );
1211 is( $unit_values->[2]->{desc},     'US Inches',              'desc     is good' );
1212 is( $unit_values->[2]->{value},    72,                       'value    is good' );
1213 is( $unit_values->[2]->{selected}, 0,                        'selected is good' );
1214
1215 isa_ok( $unit_values->[3], 'HASH', '$unit_values->[3] is a HASH' );
1216 is( $unit_values->[3]->{type},     'MM',                     'type     is good' );
1217 is( $unit_values->[3]->{desc},     'SI Millimeters',         'desc     is good' );
1218 is( $unit_values->[3]->{value},    2.83464567,               'value    is good' );
1219 is( $unit_values->[3]->{selected}, 0,                        'selected is good' );
1220
1221 isa_ok( $unit_values->[4], 'HASH', '$unit_values->[4] is a HASH' );
1222 is( $unit_values->[4]->{type},     'CM',                     'type     is good' );
1223 is( $unit_values->[4]->{desc},     'SI Centimeters',         'desc     is good' );
1224 is( $unit_values->[4]->{value},    28.3464567,               'value    is good' );
1225 is( $unit_values->[4]->{selected}, 0,                        'selected is good' );
1226
1227 # ---------- Testing get_output_formats -------------------
1228 my $output_formats = get_output_formats();
1229
1230 is( @$output_formats, 2, 'There are 2 output format' );
1231 isa_ok( $output_formats, 'ARRAY', '$output_formats is an ARRAY' );
1232
1233 isa_ok( $output_formats->[0], 'HASH', '$output_formats->[0] is a HASH' );
1234 is( $output_formats->[0]->{type}, 'pdf',      'type is good' );
1235 is( $output_formats->[0]->{desc}, 'PDF File', 'name is good' );
1236
1237 isa_ok( $output_formats->[1], 'HASH', '$output_formats->[1] is a HASH' );
1238 is( $output_formats->[1]->{type}, 'csv',      'type is good' );
1239 is( $output_formats->[1]->{desc}, 'CSV File', 'name is good' );
1240
1241 # ---------- Testing get_table_names ----------------------
1242 my $table_names   = get_table_names("aq");
1243 my $KOHA_PATH     = C4::Context->config("intranetdir");
1244 my $kohastructure = "$KOHA_PATH/installer/data/mysql/kohastructure.sql";
1245
1246 open( my $fh, '<', $kohastructure ) or die $!;
1247
1248 my $tables_names_matching = [];
1249 while ( my $intext = <$fh> ) {
1250     while ( $intext =~ /CREATE TABLE `*\w*aq\w*`*/g ) {
1251         my @tables_names_matching = split( /\ /, $intext );
1252         if ( $tables_names_matching[2] =~ /`*`/ ) {
1253             $tables_names_matching[2] =~ s/`//g;
1254         }
1255         push( @$tables_names_matching, "$tables_names_matching[2]" );
1256     }
1257 }
1258 close $fh;
1259 @$tables_names_matching = sort @$tables_names_matching;
1260 is_deeply( $table_names, $tables_names_matching, 'get_table_names return all tables matching' );
1261
1262 # ---------- Testing html_table ---------------------------
1263 my $display_columns = [
1264     { _label_number  => { label => 'Label Number',  link_field => 0 } },
1265     { _summary       => { label => 'Summary',       link_field => 0 } },
1266     { _item_type     => { label => 'Item Type',     link_field => 0 } },
1267     { _barcode       => { label => 'Barcode',       link_field => 1 } },
1268     { _template_code => { label => 'Template Name', link_field => 0 } },
1269     { select         => { label => 'Select',        value      => '_label_id' } },
1270 ];
1271
1272 #without $data param ------------------
1273 my $db_rows = [];
1274 my $table = html_table( $display_columns, $db_rows );
1275 is( $table, undef, 'No need to generate a table if there is not data to display' );
1276
1277 #with $data param ---------------------
1278 $db_rows = [
1279     {   _label_number => 1,
1280         _summary      => $summary1,
1281         _item_type    => 'Book',
1282         _barcode      => $barcode1,
1283         _label_id     => 'Label ID',
1284         template_id   => $template_id1,
1285     }
1286 ];
1287
1288 $table = html_table( $display_columns, $db_rows );
1289
1290 isa_ok( $table, 'ARRAY', '$table is an ARRAY' );
1291
1292 #POPULATE HEADER
1293 isa_ok( $table->[0]->{header_fields}, 'ARRAY', '$table->[0]->{header_fields} is an ARRAY' );
1294 is( scalar( @{ $table->[0]->{header_fields} } ), 6, 'There are 7 header_fields' );
1295
1296 my $field_value = $display_columns->[0]->{_label_number}->{label};
1297 is( $table->[0]->{header_fields}->[0]->{hidden},       0,                '[Label Number]   hidden        field is good' );
1298 is( $table->[0]->{header_fields}->[0]->{select_field}, 0,                '[Label Number]   select_field  field is good' );
1299 is( $table->[0]->{header_fields}->[0]->{field_name},   '_label_number',  '[Label Number]   field_name    field is good' );
1300 is( $table->[0]->{header_fields}->[0]->{field_label},  $field_value,     '[Label Number]   field_label   field is good' );
1301
1302 $field_value = $display_columns->[1]->{_summary}->{label};
1303 is( $table->[0]->{header_fields}->[1]->{hidden},       0,                '[Summary]        hidden        field is good' );
1304 is( $table->[0]->{header_fields}->[1]->{select_field}, 0,                '[Summary]        select_field  field is good' );
1305 is( $table->[0]->{header_fields}->[1]->{field_name},   '_summary',       '[Summary]        field_name    field is good' );
1306 is( $table->[0]->{header_fields}->[1]->{field_label},  $field_value,     '[Summary]        field_label   field is good' );
1307
1308 $field_value = $display_columns->[2]->{_item_type}->{label};
1309 is( $table->[0]->{header_fields}->[2]->{hidden},       0,                '[Item Type]      hidden        field is good' );
1310 is( $table->[0]->{header_fields}->[2]->{select_field}, 0,                '[Item Type]      select_field  field is good' );
1311 is( $table->[0]->{header_fields}->[2]->{field_name},   '_item_type',     '[Item Type]      field_name    field is good' );
1312 is( $table->[0]->{header_fields}->[2]->{field_label},  $field_value,     '[Item Type]      field_label   field is good' );
1313
1314 $field_value = $display_columns->[3]->{_barcode}->{label};
1315 is( $table->[0]->{header_fields}->[3]->{hidden},       0,                '[Barcode]        hidden        field is good' );
1316 is( $table->[0]->{header_fields}->[3]->{select_field}, 0,                '[Barcode]        select_field  field is good' );
1317 is( $table->[0]->{header_fields}->[3]->{field_name},   '_barcode',       '[Barcode]        field_name    field is good' );
1318 is( $table->[0]->{header_fields}->[3]->{field_label},  $field_value,     '[Barcode]        field_label   field is good' );
1319
1320 $field_value = $display_columns->[4]->{_template_code}->{label};
1321 is( $table->[0]->{header_fields}->[4]->{hidden},       0,                '[Template Code]  hidden        field is good' );
1322 is( $table->[0]->{header_fields}->[4]->{select_field}, 0,                '[Template Code]  select_field  field is good' );
1323 is( $table->[0]->{header_fields}->[4]->{field_name},   '_template_code', '[Template Code]  field_name    field is good' );
1324 is( $table->[0]->{header_fields}->[4]->{field_label},  $field_value,     '[Template Code]  field_label   field is good' );
1325
1326 $field_value = $display_columns->[5]->{select}->{label};
1327 is( $table->[0]->{header_fields}->[5]->{hidden},       0,                '[Select]         hidden        field is good' );
1328 is( $table->[0]->{header_fields}->[5]->{select_field}, 0,                '[Select]         select_field  field is good' );
1329 is( $table->[0]->{header_fields}->[5]->{field_name},   'select',         '[Select]         field_name    field is good' );
1330 is( $table->[0]->{header_fields}->[5]->{field_label},  $field_value,     '[Select]         field_label   field is good' );
1331
1332 #POPULATE TABLE
1333 isa_ok( $table->[1]->{text_fields}, 'ARRAY', '$table->[0]->{text_fields} is an ARRAY' );
1334 is( scalar( @{ $table->[1]->{text_fields} } ), 6, 'There are 6 text_fields' );
1335
1336 #test : if (grep {$table_column eq $_} keys %$db_row)
1337 my $link_field = $display_columns->[0]->{_label_number}->{link_field};
1338 my $field_name = "$table->[0]->{header_fields}->[0]->{field_name}_tbl";
1339 $field_value = $db_rows->[0]->{_label_number};
1340 is( $table->[1]->{text_fields}->[0]->{hidden},       0,               '[Label Number]   hidden        field is good' );
1341 is( $table->[1]->{text_fields}->[0]->{link_field},   $link_field,     '[Label Number]   link_field    field is good' );
1342 is( $table->[1]->{text_fields}->[0]->{select_field}, 0,               '[Label Number]   select_field  field is good' );
1343 is( $table->[1]->{text_fields}->[0]->{field_name},   $field_name,     '[Label Number]   field_name    field is good' );
1344 is( $table->[1]->{text_fields}->[0]->{field_value},  $field_value,    '[Label Number]   field_value   field is good' );
1345
1346 $link_field  = $display_columns->[1]->{_summary}->{link_field};
1347 $field_value = $db_rows->[0]->{_summary};
1348 $field_name  = "$table->[0]->{header_fields}->[1]->{field_name}_tbl";
1349 is( $table->[1]->{text_fields}->[1]->{hidden},       0,               '[Summary]        hidden        field is good' );
1350 is( $table->[1]->{text_fields}->[1]->{link_field},   $link_field,     '[Summary]        link_field    field is good' );
1351 is( $table->[1]->{text_fields}->[1]->{select_field}, 0,               '[Summary]        select_field  field is good' );
1352 is( $table->[1]->{text_fields}->[1]->{field_name},   $field_name,     '[Summary]        field_name    field is good' );
1353 is( $table->[1]->{text_fields}->[1]->{field_value},  $field_value,    '[Summary]        field_value   field is good' );
1354
1355 $link_field  = $display_columns->[2]->{_item_type}->{link_field};
1356 $field_name  = "$table->[0]->{header_fields}->[2]->{field_name}_tbl";
1357 $field_value = $db_rows->[0]->{_item_type};
1358 is( $table->[1]->{text_fields}->[2]->{hidden},       0,               '[Item Type]      hidden        field is good' );
1359 is( $table->[1]->{text_fields}->[2]->{link_field},   $link_field,     '[Item Type]      link_field    field is good' );
1360 is( $table->[1]->{text_fields}->[2]->{select_field}, 0,               '[Item Type]      select_field  field is good' );
1361 is( $table->[1]->{text_fields}->[2]->{field_name},   $field_name,     '[Item Type]      field_name    field is good' );
1362 is( $table->[1]->{text_fields}->[2]->{field_value},  $field_value,    '[Item Type]      field_value   field is good' );
1363
1364 $link_field  = $display_columns->[3]->{_barcode}->{link_field};
1365 $field_name  = "$table->[0]->{header_fields}->[3]->{field_name}_tbl";
1366 $field_value = $db_rows->[0]->{_barcode};
1367 is( $table->[1]->{text_fields}->[3]->{hidden},       0,               '[Barcode]        hidden        field is good' );
1368 is( $table->[1]->{text_fields}->[3]->{link_field},   $link_field,     '[Barcode]        link_field    field is good' );
1369 is( $table->[1]->{text_fields}->[3]->{select_field}, 0,               '[Barcode]        select_field  field is good' );
1370 is( $table->[1]->{text_fields}->[3]->{field_name},   $field_name,     '[Barcode]        field_name    field is good' );
1371 is( $table->[1]->{text_fields}->[3]->{field_value},  $field_value,    '[Barcode]        field_value   field is good' );
1372
1373 #test : elsif ($table_column =~ m/^_((.*)_(.*$))/)
1374 $link_field = $display_columns->[4]->{_template_code}->{link_field};
1375 $field_name = "$table->[0]->{header_fields}->[4]->{field_name}_tbl";
1376 is( $table->[1]->{text_fields}->[4]->{hidden},       0,               '[Template Code]  hidden        field is good' );
1377 is( $table->[1]->{text_fields}->[4]->{link_field},   $link_field,     '[Template Code]  link_field    field is good' );
1378 is( $table->[1]->{text_fields}->[4]->{select_field}, 0,               '[Template Code]  select_field  field is good' );
1379 is( $table->[1]->{text_fields}->[4]->{field_name},   $field_name,     '[Template Code]  field_name    field is good' );
1380 is( $table->[1]->{text_fields}->[4]->{field_value},  $template_code1, '[Template Code]  field_value   field is good' );
1381
1382 #test : elsif ($table_column eq 'select')
1383 $field_value = $db_rows->[0]->{_label_id};
1384 is( $table->[1]->{text_fields}->[5]->{hidden},       0,               '[Select]         hidden        field is good' );
1385 is( $table->[1]->{text_fields}->[5]->{select_field}, 1,               '[Select]         select_field  field is good' );
1386 is( $table->[1]->{text_fields}->[5]->{field_name},   'select',        '[Select]         field_name    field is good' );
1387 is( $table->[1]->{text_fields}->[5]->{field_value},  $field_value,    '[Select]         field_value   field is good' );
1388
1389 # ---------- Testing _SELECT ---------------------------
1390 # Mocking $sth->err and $sth->errstr
1391 {
1392     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
1393     $dbi_st->mock( 'err',    sub { return 1; } );
1394     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
1395     my $records;
1396     warning_is { $records = C4::Creators::Lib::_SELECT( '*', 'borrowers', "borrowernumber = $borrowernumber1" ) } 'Database returned the following error: something went wrong',
1397       '_SELECT raises warning if something went wrong with the sql request execution';
1398
1399     is( $records, 1, '$record return 1' );
1400 }
1401
1402 #without $params[2] -------------------
1403 my $records = C4::Creators::Lib::_SELECT( 'surname, firstname, cardnumber, branchcode, categorycode', 'borrowers' );
1404
1405 is( @$records, 3, 'There are 3 borrowers' );
1406 isa_ok( $records, 'ARRAY', '$records is an ARRAY' );
1407
1408 isa_ok( $records->[0], 'HASH', '$records->[0] is a HASH' );
1409 is( $records->[0]->{surname},      $surname1,     'surname      is good' );
1410 is( $records->[0]->{firstname},    $firstname1,   'firstname    is good' );
1411 is( $records->[0]->{cardnumber},   $cardnumber1,  'cardnumber   is good' );
1412 is( $records->[0]->{branchcode},   $branchcode,   'branchcode   is good' );
1413 is( $records->[0]->{categorycode}, $categorycode, 'categorycode is good' );
1414
1415 isa_ok( $records->[1], 'HASH', '$records->[1] is a HASH' );
1416 is( $records->[1]->{surname},      $surname2,     'surname      is good' );
1417 is( $records->[1]->{firstname},    $firstname2,   'firstname    is good' );
1418 is( $records->[1]->{cardnumber},   $cardnumber2,  'cardnumber   is good' );
1419 is( $records->[1]->{branchcode},   $branchcode,   'branchcode   is good' );
1420 is( $records->[1]->{categorycode}, $categorycode, 'categorycode is good' );
1421
1422 isa_ok( $records->[2], 'HASH', '$records->[2] is a HASH' );
1423 is( $records->[2]->{surname},      $surname3,     'surname      is good' );
1424 is( $records->[2]->{firstname},    $firstname3,   'firstname    is good' );
1425 is( $records->[2]->{cardnumber},   $cardnumber3,  'cardnumber   is good' );
1426 is( $records->[2]->{branchcode},   $branchcode,   'branchcode   is good' );
1427 is( $records->[2]->{categorycode}, $categorycode, 'categorycode is good' );
1428
1429 #with $params[2] ----------------------
1430 $records = C4::Creators::Lib::_SELECT( 'surname, firstname, cardnumber, branchcode, categorycode', 'borrowers', "borrowernumber = $borrowernumber1" );
1431
1432 is( @$records, 1, 'There is 1 borrower where borrowernumber = $borrowernumber1' );
1433 isa_ok( $records, 'ARRAY', '$records is an ARRAY' );
1434
1435 isa_ok( $records->[0], 'HASH', '$records->[0] is a HASH' );
1436 is( $records->[0]->{surname},      $surname1,     'surname      is good' );
1437 is( $records->[0]->{firstname},    $firstname1,   'firstname    is good' );
1438 is( $records->[0]->{cardnumber},   $cardnumber1,  'cardnumber   is good' );
1439 is( $records->[0]->{branchcode},   $branchcode,   'branchcode   is good' );
1440 is( $records->[0]->{categorycode}, $categorycode, 'categorycode is good' );
1441
1442 # ---------- Sub ------------------------------------------
1443 my %preferences;
1444
1445 sub mock_preference {
1446     my $context = new Test::MockModule('C4::Context');
1447     my ( $pref, $value ) = @_;
1448     $preferences{$pref} = $value;
1449     $context->mock(
1450         'preference',
1451         sub {
1452             my ( $self, $pref ) = @_;
1453             if ( exists $preferences{$pref} ) {
1454                 return $preferences{$pref};
1455             } else {
1456                 my $method = $context->original('preference');
1457                 return $method->( $self, $pref );
1458             }
1459         }
1460     );
1461 }