Bug 16550: Add test to NewsChannels.t
[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 => 644;
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( field_list => '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 filter params ------------------
367 $templates = get_all_templates( filter => '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 # With orderby param ------------------
399 $templates = get_all_templates( orderby => 'rows DESC' );
400
401 $query = '
402   SELECT    count(*)
403   FROM      creator_templates
404   ORDER BY  rows DESC
405   ';
406 $count = $dbh->selectrow_array($query);
407 is( $count,      2,      'There are 2 templates' );
408 is( @$templates, $count, 'There are 2 templates matching' );
409 isa_ok( $templates, 'ARRAY', '$templates is an ARRAY' );
410
411 isa_ok( $templates->[0], 'HASH', '$templates->[0]  is a HASH' );
412 is( $templates->[0]->{profile_id},       2,            'profile_id       is good' );
413 is( $templates->[0]->{template_code},    'TEMPL2',     'template_code    is good' );
414 is( $templates->[0]->{template_desc},    'Template 2', 'template_desc    is good' );
415 is( $templates->[0]->{page_width},       101,          'page_width       is good' );
416 is( $templates->[0]->{page_height},      151,          'page_height      is good' );
417 is( $templates->[0]->{label_width},      11,           'label_width      is good' );
418 is( $templates->[0]->{label_height},     16,           'label_height     is good' );
419 is( $templates->[0]->{top_text_margin},  3,            'top_text_margin  is good' );
420 is( $templates->[0]->{left_text_margin}, 4,            'left_text_margin is good' );
421 is( $templates->[0]->{top_margin},       2,            'top_margin       is good' );
422 is( $templates->[0]->{left_margin},      5,            'left_margin      is good' );
423 is( $templates->[0]->{cols},             10,           'cols             is good' );
424 is( $templates->[0]->{rows},             7,            'rows             is good' );
425 is( $templates->[0]->{col_gap},          0.2,          'col_gap          is good' );
426 is( $templates->[0]->{row_gap},          0.3,          'row_gap          is good' );
427 is( $templates->[0]->{units},            'POINT',      'units            is good' );
428 is( $templates->[0]->{creator},          'Labels',     'creator          is good' );
429
430 isa_ok( $templates->[1], 'HASH', '$templates->[1]  is a HASH' );
431 is( $templates->[1]->{profile_id},       1,            'profile_id       is good' );
432 is( $templates->[1]->{template_code},    'TEMPL1',     'template_code    is good' );
433 is( $templates->[1]->{template_desc},    'Template 1', 'template_desc    is good' );
434 is( $templates->[1]->{page_width},       100,          'page_width       is good' );
435 is( $templates->[1]->{page_height},      150,          'page_height      is good' );
436 is( $templates->[1]->{label_width},      10,           'label_width      is good' );
437 is( $templates->[1]->{label_height},     15,           'label_height     is good' );
438 is( $templates->[1]->{top_text_margin},  2,            'top_text_margin  is good' );
439 is( $templates->[1]->{left_text_margin}, 3,            'left_text_margin is good' );
440 is( $templates->[1]->{top_margin},       1,            'top_margin       is good' );
441 is( $templates->[1]->{left_margin},      4,            'left_margin      is good' );
442 is( $templates->[1]->{cols},             9,            'cols             is good' );
443 is( $templates->[1]->{rows},             6,            'rows             is good' );
444 is( $templates->[1]->{col_gap},          0.1,          'col_gap          is good' );
445 is( $templates->[1]->{row_gap},          0.2,          'row_gap          is good' );
446 is( $templates->[1]->{units},            'POINT',      'units            is good' );
447 is( $templates->[1]->{creator},          'Labels',     'creator          is good' );
448
449 # ---------- Testing get_all_layouts ----------------------
450 # Mocking $sth->err and $sth->errstr
451 {
452     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
453     $dbi_st->mock( 'err',    sub { return 1; } );
454     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
455     my $layouts;
456     warning_is { $layouts = get_all_layouts() } 'Database returned the following error: something went wrong',
457       'get_all_layouts() raises warning if something went wrong with the sql request execution';
458
459     is( $layouts, -1, '$layouts return -1' );
460 }
461
462 # Without params ----------------------
463 my $layouts = get_all_layouts();
464
465 $query = '
466   SELECT count(*)
467   FROM   creator_layouts
468   ';
469 $count = $dbh->selectrow_array($query);
470 is( $count,    2,      'There are 2 layouts' );
471 is( @$layouts, $count, 'There are 2 layouts matching' );
472 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
473
474 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
475 is( $layouts->[0]->{barcode_type},  'COOP2OF5',    'barcode_type   is good' );
476 is( $layouts->[0]->{start_label},   1,             'start_label    is good' );
477 is( $layouts->[0]->{printing_type}, 'BAR1',        'printing_type  is good' );
478 is( $layouts->[0]->{layout_name},   'NAME1',       'layout_name    is good' );
479 is( $layouts->[0]->{guidebox},      1,             'guidebox       is good' );
480 is( $layouts->[0]->{font},          'TR',          'font           is good' );
481 is( $layouts->[0]->{font_size},     11,            'font_size      is good' );
482 is( $layouts->[0]->{units},         'POINT',       'units          is good' );
483 is( $layouts->[0]->{callnum_split}, 1,             'callnum_split  is good' );
484 is( $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
485 is( $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
486 is( $layouts->[0]->{layout_xml},    'layout_xml1', 'layout_xml     is good' );
487 is( $layouts->[0]->{creator},       'Labels',      'creator        is good' );
488
489 isa_ok( $layouts->[1], 'HASH', '$layouts->[1]  is a HASH' );
490 is( $layouts->[1]->{barcode_type},  'EAN13',       'barcode_type   is good' );
491 is( $layouts->[1]->{start_label},   2,             'start_label    is good' );
492 is( $layouts->[1]->{printing_type}, 'BAR2',        'printing_type  is good' );
493 is( $layouts->[1]->{layout_name},   'NAME2',       'layout_name    is good' );
494 is( $layouts->[1]->{guidebox},      2,             'guidebox       is good' );
495 is( $layouts->[1]->{font},          'TR',          'font           is good' );
496 is( $layouts->[1]->{font_size},     12,            'font_size      is good' );
497 is( $layouts->[1]->{units},         'POINT',       'units          is good' );
498 is( $layouts->[1]->{callnum_split}, 2,             'callnum_split  is good' );
499 is( $layouts->[1]->{text_justify},  'L',           'text_justify   is good' );
500 is( $layouts->[1]->{format_string}, 'barcode',     'format_string  is good' );
501 is( $layouts->[1]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
502 is( $layouts->[1]->{creator},       'Labels',      'creator        is good' );
503
504 # With field_list params --------------
505 $layouts = get_all_layouts( field_list => 'barcode_type, layout_name, font' );
506
507 $query = '
508   SELECT count(*)
509   FROM   creator_layouts
510   ';
511 $count = $dbh->selectrow_array($query);
512 is( $count,    2,      'There are 2 layouts' );
513 is( @$layouts, $count, 'There are 2 layouts matching' );
514 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
515
516 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
517 is  (        $layouts->[0]->{barcode_type},  'COOP2OF5',    'barcode_type   is good' );
518 isnt( exists $layouts->[0]->{start_label},   1,             'start_label    is good' );
519 isnt( exists $layouts->[0]->{printing_type}, 'BAR1',        'printing_type  is good' );
520 is  (        $layouts->[0]->{layout_name},   'NAME1',       'layout_name    is good' );
521 isnt( exists $layouts->[0]->{guidebox},      1,             'guidebox       is good' );
522 is  (        $layouts->[0]->{font},         'TR',           'font           is good' );
523 isnt( exists $layouts->[0]->{font_size},     11,            'font_size      is good' );
524 isnt( exists $layouts->[0]->{units},         'POINT',       'units          is good' );
525 isnt( exists $layouts->[0]->{callnum_split}, 1,             'callnum_split  is good' );
526 isnt( exists $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
527 isnt( exists $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
528 isnt( exists $layouts->[0]->{layout_xml},    'layout_xml1', 'layout_xml     is good' );
529 isnt( exists $layouts->[0]->{creator},       'Labels',      'creator        is good' );
530
531 isa_ok( $layouts->[1], 'HASH', '$layouts->[1] is a HASH' );
532 is  (        $layouts->[1]->{barcode_type},   'EAN13',      'barcode_type   is good' );
533 isnt( exists $layouts->[1]->{start_label},    2,            'start_label    is good' );
534 isnt( exists $layouts->[1]->{printing_type},  'BAR2',       'printing_type  is good' );
535 is  (        $layouts->[1]->{layout_name},    'NAME2',      'layout_name    is good' );
536 isnt( exists $layouts->[1]->{guidebox},       2,            'guidebox       is good' );
537 is  (        $layouts->[1]->{font},          'TR',          'font           is good' );
538 isnt( exists $layouts->[1]->{font_size},      12,           'font_size      is good' );
539 isnt( exists $layouts->[1]->{units},         'POINT',       'units          is good' );
540 isnt( exists $layouts->[1]->{callnum_split},  2,            'callnum_split  is good' );
541 isnt( exists $layouts->[1]->{text_justify},  'L',           'text_justify   is good' );
542 isnt( exists $layouts->[1]->{format_string}, 'barcode',     'format_string  is good' );
543 isnt( exists $layouts->[1]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
544 isnt( exists $layouts->[1]->{creator},       'Labels',      'creator        is good' );
545
546 # With filter params ------------------
547 $layouts = get_all_layouts( filter => 'font_size = 12' );
548
549 $query = '
550   SELECT count(*)
551   FROM   creator_layouts
552   WHERE  font_size = 12
553   ';
554 $count = $dbh->selectrow_array($query);
555 is( $count,    1,      'There is 1 layout matching' );
556 is( @$layouts, $count, 'There is 1 layout matching' );
557 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
558
559 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
560 is( $layouts->[0]->{barcode_type},  'EAN13',       'barcode_type   is good' );
561 is( $layouts->[0]->{start_label},   2,             'start_label    is good' );
562 is( $layouts->[0]->{printing_type}, 'BAR2',        'printing_type  is good' );
563 is( $layouts->[0]->{layout_name},   'NAME2',       'layout_name    is good' );
564 is( $layouts->[0]->{guidebox},      2,             'guidebox       is good' );
565 is( $layouts->[0]->{font},          'TR',          'font           is good' );
566 is( $layouts->[0]->{font_size},     12,            'font_size      is good' );
567 is( $layouts->[0]->{units},         'POINT',       'units          is good' );
568 is( $layouts->[0]->{callnum_split}, 2,             'callnum_split  is good' );
569 is( $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
570 is( $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
571 is( $layouts->[0]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
572 is( $layouts->[0]->{creator},       'Labels',      'creator        is good' );
573
574 # With orderby param ------------------
575 $layouts = get_all_layouts( orderby => 'font_size DESC' );
576
577 $query = '
578   SELECT   count(*)
579   FROM     creator_layouts
580   ORDER BY font_size DESC
581   ';
582 $count = $dbh->selectrow_array($query);
583 is( $count,    2,      'There are layout matching' );
584 is( @$layouts, $count, 'There are 2 layouts matching' );
585 isa_ok( $layouts, 'ARRAY', '$layouts is an ARRAY' );
586
587 isa_ok( $layouts->[0], 'HASH', '$layouts->[0]  is a HASH' );
588 is( $layouts->[0]->{barcode_type},  'EAN13',       'barcode_type   is good' );
589 is( $layouts->[0]->{start_label},   2,             'start_label    is good' );
590 is( $layouts->[0]->{printing_type}, 'BAR2',        'printing_type  is good' );
591 is( $layouts->[0]->{layout_name},   'NAME2',       'layout_name    is good' );
592 is( $layouts->[0]->{guidebox},      2,             'guidebox       is good' );
593 is( $layouts->[0]->{font},          'TR',          'font           is good' );
594 is( $layouts->[0]->{font_size},     12,            'font_size      is good' );
595 is( $layouts->[0]->{units},         'POINT',       'units          is good' );
596 is( $layouts->[0]->{callnum_split}, 2,             'callnum_split  is good' );
597 is( $layouts->[0]->{text_justify},  'L',           'text_justify   is good' );
598 is( $layouts->[0]->{format_string}, 'barcode',     'format_string  is good' );
599 is( $layouts->[0]->{layout_xml},    'layout_xml2', 'layout_xml     is good' );
600 is( $layouts->[0]->{creator},       'Labels',      'creator        is good' );
601
602 isa_ok( $layouts->[1], 'HASH', '$layouts->[1]  is a HASH' );
603 is( $layouts->[1]->{barcode_type},  'COOP2OF5',    'barcode_type   is good' );
604 is( $layouts->[1]->{start_label},   1,             'start_label    is good' );
605 is( $layouts->[1]->{printing_type}, 'BAR1',        'printing_type  is good' );
606 is( $layouts->[1]->{layout_name},   'NAME1',       'layout_name    is good' );
607 is( $layouts->[1]->{guidebox},      1,             'guidebox       is good' );
608 is( $layouts->[1]->{font},          'TR',          'font           is good' );
609 is( $layouts->[1]->{font_size},     11,            'font_size      is good' );
610 is( $layouts->[1]->{units},         'POINT',       'units          is good' );
611 is( $layouts->[1]->{callnum_split}, 1,             'callnum_split  is good' );
612 is( $layouts->[1]->{text_justify},  'L',           'text_justify   is good' );
613 is( $layouts->[1]->{format_string}, 'barcode',     'format_string  is good' );
614 is( $layouts->[1]->{layout_xml},    'layout_xml1', 'layout_xml     is good' );
615 is( $layouts->[1]->{creator},       'Labels',      'creator        is good' );
616
617 # ---------- Testing get_all_profiles ---------------------
618 # Mocking $sth->err and $sth->errstr
619 {
620     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
621     $dbi_st->mock( 'err',    sub { return 1; } );
622     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
623     my $profiles;
624     warning_is { $profiles = get_all_profiles() } 'Database returned the following error: something went wrong',
625       'get_all_profiles() raises warning if something went wrong with the sql request execution';
626
627     is( $profiles, -1, '$profiles return -1' );
628 }
629
630 # Without params ----------------------
631 my $profiles = get_all_profiles();
632
633 $query = '
634   SELECT count(*)
635   FROM   printers_profile
636   ';
637 $count = $dbh->selectrow_array($query);
638 is( $count,     2,      'There are 2 profiles' );
639 is( @$profiles, $count, 'There are 2 profiles matching' );
640 isa_ok( $profiles, 'ARRAY', '$profiles is an ARRAY' );
641
642 isa_ok( $profiles->[0], 'HASH', '$profiles->[0] is a HASH' );
643 is( $profiles->[0]->{printer_name}, 'Layout1 Name', 'printer_name   is good' );
644 is( $profiles->[0]->{template_id},  1234,           'template_id    is good' );
645 is( $profiles->[0]->{paper_bin},    'Bypass',       'paper_bin      is good' );
646 is( $profiles->[0]->{offset_horz},  0.1,            'offset_horz    is good' );
647 is( $profiles->[0]->{offset_vert},  0.2,            'offset_vert    is good' );
648 is( $profiles->[0]->{creep_horz},   0.3,            'creep_horz     is good' );
649 is( $profiles->[0]->{creep_vert},   0.4,            'creep_vert     is good' );
650 is( $profiles->[0]->{units},        'POINT',        'units          is good' );
651 is( $profiles->[0]->{creator},      'Labels',       'creator        is good' );
652
653 isa_ok( $profiles->[1], 'HASH', '$profiles->[1] is a HASH' );
654 is( $profiles->[1]->{printer_name}, 'Layout2 Name', 'printer_name   is good' );
655 is( $profiles->[1]->{template_id},  1235,           'template_id    is good' );
656 is( $profiles->[1]->{paper_bin},    'Bypass',       'paper_bin      is good' );
657 is( $profiles->[1]->{offset_horz},  0.2,            'offset_horz    is good' );
658 is( $profiles->[1]->{offset_vert},  0.3,            'offset_vert    is good' );
659 is( $profiles->[1]->{creep_horz},   0.4,            'creep_horz     is good' );
660 is( $profiles->[1]->{creep_vert},   0.5,            'creep_vert     is good' );
661 is( $profiles->[1]->{units},        'POINT',        'units          is good' );
662 is( $profiles->[1]->{creator},      'Labels',       'creator        is good' );
663
664 # With field_list params --------------
665 $profiles = get_all_profiles( field_list => 'printer_name, template_id' );
666
667 $query = '
668   SELECT count(*)
669   FROM   printers_profile
670   ';
671 $count = $dbh->selectrow_array($query);
672 is( $count,     2,      'There are 2 profiles' );
673 is( @$profiles, $count, 'There are 2 profiles matching' );
674 isa_ok( $profiles, 'ARRAY', '$profiles is an ARRAY' );
675
676 isa_ok( $profiles->[0], 'HASH', '$profiles->[0] is a HASH' );
677 is  (        $profiles->[0]->{printer_name},  'Layout1 Name', 'printer_name   is good' );
678 is  (        $profiles->[0]->{template_id},   1234,           'template_id    is good' );
679 isnt( exists $profiles->[0]->{paper_bin},     'Bypass',       'paper_bin      is good' );
680 isnt( exists $profiles->[0]->{offset_horz},   0.1,            'offset_horz    is good' );
681 isnt( exists $profiles->[0]->{offset_vert},   0.2,            'offset_vert    is good' );
682 isnt( exists $profiles->[0]->{creep_horz},    0.3,            'creep_horz     is good' );
683 isnt( exists $profiles->[0]->{creep_vert},    0.4,            'creep_vert     is good' );
684 isnt( exists $profiles->[0]->{units},         'POINT',        'units          is good' );
685 isnt( exists $profiles->[0]->{creator},       'Labels',       'creator        is good' );
686
687 isa_ok( $profiles->[1], 'HASH', '$profiles->[1] is a HASH' );
688 is  (        $profiles->[1]->{printer_name},  'Layout2 Name', 'printer_name   is good' );
689 is  (        $profiles->[1]->{template_id},   1235,           'template_id    is good' );
690 isnt( exists $profiles->[1]->{paper_bin},     'Bypass',       'paper_bin      is good' );
691 isnt( exists $profiles->[1]->{offset_horz},   0.2,            'offset_horz    is good' );
692 isnt( exists $profiles->[1]->{offset_vert},   0.3,            'offset_vert    is good' );
693 isnt( exists $profiles->[1]->{creep_horz},    0.4,            'creep_horz     is good' );
694 isnt( exists $profiles->[1]->{creep_vert},    0.5,            'creep_vert     is good' );
695 isnt( exists $profiles->[1]->{units},         'POINT',        'units          is good' );
696 isnt( exists $profiles->[1]->{creator},       'Labels',       'creator        is good' );
697
698 # With filter params ------------------
699 $profiles = get_all_profiles( filter => 'template_id = 1235' );
700
701 $query = '
702   SELECT count(*)
703   FROM   printers_profile
704   WHERE  template_id = 1235
705   ';
706 $count = $dbh->selectrow_array($query);
707 is( $count,     1,      'There is 1 profile matching' );
708 is( @$profiles, $count, 'There is 1 profile matching' );
709 isa_ok( $profiles, 'ARRAY', '$profiles is an ARRAY' );
710
711 isa_ok( $profiles->[0], 'HASH', '$profiles->[0] is a HASH' );
712 is  (        $profiles->[0]->{printer_name},  'Layout2 Name', 'printer_name   is good' );
713 is  (        $profiles->[0]->{template_id},   1235,           'template_id    is good' );
714 isnt( exists $profiles->[0]->{paper_bin},     'Bypass',       'paper_bin      is good' );
715 isnt( exists $profiles->[0]->{offset_horz},   0.2,            'offset_horz    is good' );
716 isnt( exists $profiles->[0]->{offset_vert},   0.3,            'offset_vert    is good' );
717 isnt( exists $profiles->[0]->{creep_horz},    0.4,            'creep_horz     is good' );
718 isnt( exists $profiles->[0]->{creep_vert},    0.5,            'creep_vert     is good' );
719 isnt( exists $profiles->[0]->{units},         'POINT',        'units          is good' );
720 isnt( exists $profiles->[0]->{creator},       'Labels',       'creator        is good' );
721
722 # ---------- Testing get_all_image_names ------------------
723
724 # Mocking $sth->err and $sth->errstr
725 {
726     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
727     $dbi_st->mock( 'err',    sub { return 1; } );
728     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
729     my $images;
730     warning_is { $images = get_all_image_names() } 'Database returned the following error: something went wrong',
731       'get_all_image_names() raises warning if something went wrong with the sql request execution';
732
733     is( $images, -1, '$images return -1' );
734 }
735
736 # Without params ----------------------
737 my $images = get_all_image_names();
738
739 $query = '
740   SELECT count(*)
741   FROM   creator_images
742   ';
743 $count = $dbh->selectrow_array($query);
744 is( $count,   2,      'There are 2 images' );
745 is( @$images, $count, 'There are 2 images matching' );
746 isa_ok( $images, 'ARRAY', '$images is an ARRAY' );
747
748 isa_ok( $images->[0], 'HASH', '$images->[0] is a HASH' );
749 is( $images->[0]->{name},     'Image 1',            'name         is good' );
750 is( $images->[0]->{selected}, 0,                    'selected     is good' );
751 is( $images->[0]->{type},     $images->[0]->{name}, 'type         is good' );
752
753 isa_ok( $images->[1], 'HASH', '$images->[1] is a HASH' );
754 is( $images->[1]->{name},     'Image 2',            'name         is good' );
755 is( $images->[1]->{selected}, 0,                    'selected     is good' );
756 is( $images->[1]->{type},     $images->[1]->{name}, 'type         is good' );
757
758 # ---------- Testing get_batch_summary --------------------
759
760 # Mocking $sth->err and $sth->errstr
761 {
762     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
763     $dbi_st->mock( 'err',    sub { return 1; } );
764     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
765     my $batches;
766     warning_is { $batches = get_batch_summary() } 'Database returned the following error on attempted SELECT: something went wrong',
767       'get_batch_summary() raises warning if something went wrong with the sql request execution';
768
769     is( $batches, -1, '$batches return -1' );
770 }
771
772 # Without creator params --------------
773 my $batches = get_batch_summary( creator => 'Labels' );
774
775 $query = '
776   SELECT   batch_id, count(batch_id)
777   FROM     creator_batches
778   WHERE    creator = ?
779   GROUP BY batch_id
780   ';
781 my $sth = $dbh->prepare($query);
782 $sth->execute('Labels');
783 $count = $sth->rows;
784 is( $count,    2,      'There are 2 batches' );
785 is( @$batches, $count, 'There are 2 batches matching' );
786 isa_ok( $batches, 'ARRAY', '$batches is an ARRAY' );
787
788 $query = '
789   SELECT count(batch_id)
790   FROM   creator_batches
791   WHERE  creator = ?
792     AND  batch_id = ?
793   ';
794 $count = $dbh->selectrow_array( $query, {}, 'Labels', 11 );
795 is( $count, 1, 'There is 1 batch where batch_id = 11' );
796
797 isa_ok( $batches->[0], 'HASH', '$batches->[0] is a HASH' );
798 is( $batches->[0]->{batch_id},    11,     'batch_id      is good' );
799 is( $batches->[0]->{_item_count}, $count, 'item_number   is good for this batch_id' );
800
801 $count = $dbh->selectrow_array( $query, {}, 'Labels', 12 );
802 is( $count, 2, 'There are 2 batches where batch_id = 12' );
803
804 isa_ok( $batches->[1], 'HASH', '$batches->[1] is a HASH' );
805 is( $batches->[1]->{batch_id},    12,     'batch_id      is good' );
806 is( $batches->[1]->{_item_count}, $count, 'item_number   is good for this batch_id' );
807
808 # Without filter & creator params -----
809 $batches = get_batch_summary( filter => "branch_code='$library1->{branchcode}'", creator => 'Labels' );
810 is( @$batches, 1, 'There is 1 batch matching' );
811
812 $query = '
813   SELECT   batch_id, count(batch_id)
814   FROM     creator_batches
815   WHERE    creator = ?
816     AND    branch_code = ?
817   GROUP BY batch_id
818   ';
819 my ( $id, $nb ) = $dbh->selectrow_array( $query, {}, 'Labels', $library1->{branchcode} );
820
821 is( $batches->[0]->{batch_id},    $id, 'batch_id    is good' );
822 is( $batches->[0]->{_item_count}, $nb, 'item_number is good for this batch_id' );
823
824 # ---------- Testing get_label_summary --------------------
825 # Mocking $sth->err and $sth->errstr
826 {
827     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
828     $dbi_st->mock( 'err',    sub { return 1; } );
829     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
830     my $labels;
831     my @items = [ { item_number => $item_number1 } ];
832
833     warning_is { $labels = get_label_summary( items => @items ) } 'Database returned the following error on attempted SELECT: something went wrong',
834       'get_label_summary() raises warning if something went wrong with the sql request execution';
835
836     is( $labels, -1, '$labels return -1' );
837 }
838
839 # Without params ----------------------
840 my $labels = get_label_summary();
841
842 isa_ok( $labels, 'ARRAY', '$labels is an ARRAY' );
843 is( @$labels, 0, '$labels is empty' );
844
845 # With items param --------------------
846 $query = '
847   SELECT biblionumber, title, author
848   FROM   biblio
849   WHERE  biblionumber = ?
850   ';
851 my ( $b_biblionumber1, $b_title1, $b_author1 ) = $dbh->selectrow_array( $query, {}, $biblionumber1 );
852 my ( $b_biblionumber2, $b_title2, $b_author2 ) = $dbh->selectrow_array( $query, {}, $biblionumber2 );
853
854 $query = '
855   SELECT biblionumber, biblioitemnumber, itemtype
856   FROM   biblioitems
857   WHERE  biblioitemnumber = ?
858   ';
859 my ( $bi_biblionumber1, $bi_biblioitemnumber1, $bi_itemtype1 ) = $dbh->selectrow_array( $query, {}, $biblioitemnumber1 );
860 my ( $bi_biblionumber2, $bi_biblioitemnumber2, $bi_itemtype2 ) = $dbh->selectrow_array( $query, {}, $biblioitemnumber2 );
861
862 $query = '
863   SELECT biblionumber, biblioitemnumber, itemnumber, barcode, itype
864   FROM   items
865   WHERE  itemnumber = ?
866   ';
867 my ( $i_biblionumber1, $i_biblioitemnumber1, $i_itemnumber1, $i_barcode1, $i_itype1 ) = $dbh->selectrow_array( $query, {}, $item_number1 );
868 my ( $i_biblionumber2, $i_biblioitemnumber2, $i_itemnumber2, $i_barcode2, $i_itype2 ) = $dbh->selectrow_array( $query, {}, $item_number2 );
869
870 $query = '
871   SELECT label_id, batch_id, item_number
872   FROM   creator_batches
873   WHERE  item_number = ?
874   ';
875 my ( $c_label_id1, $c_batch_id1, $c_item_number1 ) = $dbh->selectrow_array( $query, {}, $item_number1 );
876 my ( $c_label_id2, $c_batch_id2, $c_item_number2 ) = $dbh->selectrow_array( $query, {}, $item_number2 );
877
878 is( $c_item_number1,      $i_itemnumber1,        'CREATOR_BATCHES.item_number == ITEMS.itemnumber' );
879 is( $i_biblioitemnumber1, $bi_biblioitemnumber1, 'ITEMS.biblioitemnumber      == BIBLIOITEMS.biblioitemnumber' );
880 is( $bi_biblionumber1,    $b_biblionumber1,      'BIBLIOITEMS.biblionumber    == BIBLIO.biblionumber' );
881
882 is( $c_item_number2,      $i_itemnumber2,        'CREATOR_BATCHES.item_number == ITEMS.itemnumber' );
883 is( $i_biblioitemnumber2, $bi_biblioitemnumber2, 'ITEMS.biblioitemnumber      == BIBLIOITEMS.biblioitemnumber' );
884 is( $bi_biblionumber2,    $b_biblionumber2,      'BIBLIOITEMS.biblionumber    == BIBLIO.biblionumber' );
885
886 my @items = [
887     {   item_number => $item_number1,
888         label_id    => $c_label_id1,
889     }
890 ];
891 $labels = get_label_summary( items => @items, batch_id => $c_batch_id1 );
892
893 is( @$labels, 1, 'There is 1 label for $item_number1' );
894 isa_ok( $labels,      'ARRAY', '$labels      is an array' );
895 isa_ok( $labels->[0], 'HASH',  '$labels->[0] is an hash' );
896
897 my $record_author = $b_author1;
898 my $record_title  = $b_title1;
899 $record_author =~ s/[^\.|\w]$//;
900 $record_title  =~ s/\W*$//;
901 $record_title = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $b_biblionumber1 . '"> ' . $b_title1 . '</a>';
902 my $summary1        = $record_title . " | " . ( $b_author1 ? $b_author1 : 'N/A' );
903 my $itemtypes_pref  = C4::Context->preference("item-level_itypes");
904 my $record_itemtype = $itemtypes_pref ? $i_itype1 : $bi_itemtype1;
905
906 is( $labels->[0]->{_label_number}, 1,                '_label_number  is good' );
907 is( $labels->[0]->{_summary},      $summary1,        '_summary       is good' );
908 is( $labels->[0]->{_item_type},    $record_itemtype, '_item_type     is good' );
909 is( $labels->[0]->{_barcode},      $i_barcode1,      '_barcode       is good' );
910 is( $labels->[0]->{_item_number},  $i_itemnumber1,   '_item_number   is good' );
911 is( $labels->[0]->{_label_id},     $c_label_id1,     '_label_id      is good' );
912
913 # record without author
914 @items = [
915     {   item_number => $item_number2,
916         label_id    => $c_label_id2,
917     }
918 ];
919 $labels = get_label_summary( items => @items, batch_id => $c_batch_id2 );
920
921 is( @$labels, 1, 'There is 1 label for $item_number2' );
922 isa_ok( $labels,      'ARRAY', '$labels      is an array' );
923 isa_ok( $labels->[0], 'HASH',  '$labels->[0] is an hash' );
924
925 $record_author = $b_author2;
926 $record_title  = $b_title2;
927 $record_author =~ s/[^\.|\w]$// if $b_author2;
928 $record_title =~ s/\W*$//;
929 $record_title = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $b_biblionumber2 . '"> ' . $b_title2 . '</a>';
930 my $summary2 = $record_title . " | " . ( $b_author2 ? $b_author2 : 'N/A' );
931 $itemtypes_pref = C4::Context->preference("item-level_itypes");
932 $record_itemtype = $itemtypes_pref ? $i_itype2 : $bi_itemtype2;
933
934 is( $labels->[0]->{_label_number}, 1,                '_label_number  is good' );
935 is( $labels->[0]->{_summary},      $summary2,        '_summary       is good' );
936 is( $labels->[0]->{_item_type},    $record_itemtype, '_item_type     is good' );
937 is( $labels->[0]->{_barcode},      $i_barcode2,      '_barcode       is good' );
938 is( $labels->[0]->{_item_number},  $i_itemnumber2,   '_item_number   is good' );
939 is( $labels->[0]->{_label_id},     $c_label_id2,     '_label_id      is good' );
940
941 #Mocking C4::Context->preference("item-level_itypes")
942 {
943     t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
944     my $h = C4::Context->preference("item-level_itypes");
945
946     my @items = [
947         {   item_number => $item_number1,
948             label_id    => $c_label_id1,
949         }
950     ];
951     $labels = get_label_summary( items => @items, batch_id => $c_batch_id1 );
952
953     is( @$labels, 1, 'There is 1 label for $item_number1' );
954     isa_ok( $labels,      'ARRAY', '$labels      is an array' );
955     isa_ok( $labels->[0], 'HASH',  '$labels->[0] is an hash' );
956
957     my $record_author = $b_author1;
958     my $record_title  = $b_title1;
959     $record_author =~ s/[^\.|\w]$//;
960     $record_title  =~ s/\W*$//;
961     $record_title = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $b_biblionumber1 . '"> ' . $b_title1 . '</a>';
962     my $summary1        = $record_title . " | " . ( $b_author1 ? $b_author1 : 'N/A' );
963     my $itemtypes_pref  = C4::Context->preference("item-level_itypes");
964     my $record_itemtype = $itemtypes_pref ? $i_itype1 : $bi_itemtype1;
965
966     is( $labels->[0]->{_label_number}, 1,                '_label_number  is good' );
967     is( $labels->[0]->{_summary},      $summary1,        '_summary       is good' );
968     is( $labels->[0]->{_item_type},    $record_itemtype, '_item_type     is good' );
969     is( $labels->[0]->{_barcode},      $i_barcode1,      '_barcode       is good' );
970     is( $labels->[0]->{_item_number},  $i_itemnumber1,   '_item_number   is good' );
971     is( $labels->[0]->{_label_id},     $c_label_id1,     '_label_id      is good' );
972 }
973
974 # ---------- Testing get_card_summary ---------------------
975 # Mocking $sth->err and $sth->errstr
976 {
977     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
978     $dbi_st->mock( 'err',    sub { return 1; } );
979     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
980     my $cards;
981     my @items = [ { item_number => $item_number1 } ];
982
983     warning_is { $cards = get_card_summary( items => @items ) } 'Database returned the following error on attempted SELECT: something went wrong',
984       'get_card_summary() raises warning if something went wrong with the sql request execution';
985
986     is( $cards, -1, '$cards return -1' );
987 }
988
989 # Without params ----------------------
990 my $cards = get_card_summary();
991
992 isa_ok( $cards, 'ARRAY', '$cards is an ARRAY' );
993 is( @$cards, 0, '$cards is empty' );
994
995 # With items param --------------------
996 $query = '
997   SELECT surname, firstname, cardnumber
998   FROM   borrowers
999   WHERE  borrowernumber = ?
1000   ';
1001 my ( $b_surname1, $b_firstname1, $b_cardnumber1 ) = $dbh->selectrow_array( $query, {}, $borrowernumber1 );
1002
1003 @items = [
1004     {   item_number     => $item_number1,
1005         label_id        => $c_label_id1,
1006         borrower_number => $borrowernumber1,
1007     }
1008 ];
1009 $cards = get_card_summary( items => @items );
1010
1011 is( @$cards, 1, 'There is 1 card for $item_number1' );
1012 isa_ok( $cards,      'ARRAY', '$cards      is an array' );
1013 isa_ok( $cards->[0], 'HASH',  '$cards->[0] is an hash' );
1014
1015 my $name1 = "$b_surname1, $b_firstname1";
1016 is( $cards->[0]->{_card_number},   1,                '_card_number   is good' );
1017 is( $cards->[0]->{_summary},       $name1,           '_summary       is good' );
1018 is( $cards->[0]->{borrowernumber}, $borrowernumber1, 'borrowernumber is good' );
1019 is( $cards->[0]->{_label_id},      $c_label_id1,     '_label_id      is good' );
1020
1021 # ---------- Testing get_barcode_types --------------------
1022 my $barcode_types = get_barcode_types();
1023
1024 is( @$barcode_types, 6, 'There are 6 barcodes types' );
1025 isa_ok( $barcode_types, 'ARRAY', '$barcode_types is an ARRAY' );
1026
1027 isa_ok( $barcode_types->[0], 'HASH', '$barcode_types->[0] is a HASH' );
1028 is( $barcode_types->[0]->{type},     'CODE39',                                                                                                              'type is good' );
1029 is( $barcode_types->[0]->{name},     'Code 39',                                                                                                             'name is good' );
1030 is( $barcode_types->[0]->{desc},     'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern.', 'desc is good' );
1031 is( $barcode_types->[0]->{selected}, 0,                                                                                                                     'selected is good' );
1032
1033 isa_ok( $barcode_types->[1], 'HASH', '$barcode_types->[1] is a HASH' );
1034 is( $barcode_types->[1]->{type}, 'CODE39MOD',          'type is good' );
1035 is( $barcode_types->[1]->{name}, 'Code 39 + Modulo43', 'name is good' );
1036 is( $barcode_types->[1]->{desc},
1037     'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 43 checksum.',
1038     'desc is good'
1039 );
1040 is( $barcode_types->[1]->{selected}, 0, 'selected is good' );
1041
1042 isa_ok( $barcode_types->[2], 'HASH', '$barcode_types->[2] is a HASH' );
1043 is( $barcode_types->[2]->{type},     'CODE39MOD10',        'type is good' );
1044 is( $barcode_types->[2]->{name},     'Code 39 + Modulo10', 'name is good' );
1045 is( $barcode_types->[2]->{desc},     'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 10 checksum.', 'desc is good');
1046 is( $barcode_types->[2]->{selected}, 0,                    'selected is good' );
1047
1048 isa_ok( $barcode_types->[3], 'HASH', '$barcode_types->[3] is a HASH' );
1049 is( $barcode_types->[3]->{type},     'COOP2OF5', 'type is good' );
1050 is( $barcode_types->[3]->{name},     'COOP2of5', 'name is good' );
1051 is( $barcode_types->[3]->{desc},     'Creates COOP2of5 barcodes from a string consisting of the numeric characters 0-9', 'desc is good' );
1052 is( $barcode_types->[3]->{selected}, 0,          'selected is good' );
1053
1054 isa_ok( $barcode_types->[4], 'HASH', '$barcode_types->[4] is a HASH' );
1055 is( $barcode_types->[4]->{type},     'EAN13',     'type is good' );
1056 is( $barcode_types->[4]->{name},     'EAN13',     'name is good' );
1057 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' );
1058 is( $barcode_types->[4]->{selected}, 0,           'selected is good' );
1059
1060 isa_ok( $barcode_types->[5], 'HASH', '$barcode_types->[5] is a HASH' );
1061 is( $barcode_types->[5]->{type},     'INDUSTRIAL2OF5', 'type is good' );
1062 is( $barcode_types->[5]->{name},     'Industrial2of5', 'name is good' );
1063 is( $barcode_types->[5]->{desc},     'Creates Industrial2of5 barcodes from a string consisting of the numeric characters 0-9', 'desc is good' );
1064 is( $barcode_types->[5]->{selected},  0,               'selected is good' );
1065
1066 # ---------- Testing get_label_types ----------------------
1067 my $label_types = get_label_types();
1068
1069 is( @$label_types, 5, 'There are 5 label types' );
1070 isa_ok( $label_types, 'ARRAY', '$label_types is an ARRAY' );
1071
1072 isa_ok( $label_types->[0], 'HASH', '$label_types->[0] is a HASH' );
1073 is( $label_types->[0]->{type},     'BIB',                                     'type     is good' );
1074 is( $label_types->[0]->{name},     'Biblio',                                  'name     is good' );
1075 is( $label_types->[0]->{desc},     'Only the bibliographic data is printed.', 'desc     is good' );
1076 is( $label_types->[0]->{selected}, 0,                                         'selected is good' );
1077
1078 isa_ok( $label_types->[1], 'HASH', '$label_types->[1] is a HASH' );
1079 is( $label_types->[1]->{type},     'BARBIB',                               'type     is good' );
1080 is( $label_types->[1]->{name},     'Barcode/Biblio',                       'name     is good' );
1081 is( $label_types->[1]->{desc},     'Barcode proceeds bibliographic data.', 'desc     is good' );
1082 is( $label_types->[1]->{selected}, 0,                                      'selected is good' );
1083
1084 isa_ok( $label_types->[2], 'HASH', '$label_types->[2] is a HASH' );
1085 is( $label_types->[2]->{type},     'BIBBAR',                               'type     is good' );
1086 is( $label_types->[2]->{name},     'Biblio/Barcode',                       'name     is good' );
1087 is( $label_types->[2]->{desc},     'Bibliographic data proceeds barcode.', 'desc     is good' );
1088 is( $label_types->[2]->{selected}, 0,                                      'selected is good' );
1089
1090 isa_ok( $label_types->[3], 'HASH', '$label_types->[3] is a HASH' );
1091 is( $label_types->[3]->{type},     'ALT',                                                               'type     is good' );
1092 is( $label_types->[3]->{name},     'Alternating',                                                       'name     is good' );
1093 is( $label_types->[3]->{desc},     'Barcode and bibliographic data are printed on alternating labels.', 'desc     is good' );
1094 is( $label_types->[3]->{selected}, 0,                                                                   'selected is good' );
1095
1096 isa_ok( $label_types->[4], 'HASH', '$label_types->[4] is a HASH' );
1097 is( $label_types->[4]->{type},     'BAR',                          'type     is good' );
1098 is( $label_types->[4]->{name},     'Barcode',                      'name     is good' );
1099 is( $label_types->[4]->{desc},     'Only the barcode is printed.', 'desc     is good' );
1100 is( $label_types->[4]->{selected}, 0,                              'selected is good' );
1101
1102 # ---------- Testing get_font_types -----------------------
1103 my $font_types = get_font_types();
1104
1105 is( @$font_types, 12, 'There are 12 font types' );
1106 isa_ok( $font_types, 'ARRAY', '$font_types is an ARRAY' );
1107
1108 isa_ok( $font_types->[0], 'HASH', '$font_types->[0] is a HASH' );
1109 is( $font_types->[0]->{type},     'TR',                      'type     is good' );
1110 is( $font_types->[0]->{name},     'Times-Roman',             'name     is good' );
1111 is( $font_types->[0]->{selected}, 0,                         'selected is good' );
1112
1113 isa_ok( $font_types->[1], 'HASH', '$font_types->[1] is a HASH' );
1114 is( $font_types->[1]->{type},     'TB',                      'type     is good' );
1115 is( $font_types->[1]->{name},     'Times-Bold',              'name     is good' );
1116 is( $font_types->[1]->{selected}, 0,                         'selected is good' );
1117
1118 isa_ok( $font_types->[2], 'HASH', '$font_types->[2] is a HASH' );
1119 is( $font_types->[2]->{type},     'TI',                      'type     is good' );
1120 is( $font_types->[2]->{name},     'Times-Italic',            'name     is good' );
1121 is( $font_types->[2]->{selected}, 0,                         'selected is good' );
1122
1123 isa_ok( $font_types->[3], 'HASH', '$font_types->[3] is a HASH' );
1124 is( $font_types->[3]->{type},     'TBI',                     'type     is good' );
1125 is( $font_types->[3]->{name},     'Times-Bold-Italic',       'name     is good' );
1126 is( $font_types->[3]->{selected}, 0,                         'selected is good' );
1127
1128 isa_ok( $font_types->[4], 'HASH', '$font_types->[4] is a HASH' );
1129 is( $font_types->[4]->{type},     'C',                       'type     is good' );
1130 is( $font_types->[4]->{name},     'Courier',                 'name     is good' );
1131 is( $font_types->[4]->{selected}, 0,                         'selected is good' );
1132
1133 isa_ok( $font_types->[5], 'HASH', '$font_types->[5] is a HASH' );
1134 is( $font_types->[5]->{type},     'CB',                      'type     is good' );
1135 is( $font_types->[5]->{name},     'Courier-Bold',            'name     is good' );
1136 is( $font_types->[5]->{selected}, 0,                         'selected is good' );
1137
1138 isa_ok( $font_types->[6], 'HASH', '$font_types->[6] is a HASH' );
1139 is( $font_types->[6]->{type},     'CO',                      'type     is good' );
1140 is( $font_types->[6]->{name},     'Courier-Oblique',         'name     is good' );
1141 is( $font_types->[6]->{selected}, 0,                         'selected is good' );
1142
1143 isa_ok( $font_types->[7], 'HASH', '$font_types->[7] is a HASH' );
1144 is( $font_types->[7]->{type},     'CBO',                     'type     is good' );
1145 is( $font_types->[7]->{name},     'Courier-Bold-Oblique',    'name     is good' );
1146 is( $font_types->[7]->{selected}, 0,                         'selected is good' );
1147
1148 isa_ok( $font_types->[8], 'HASH', '$font_types->[8] is a HASH' );
1149 is( $font_types->[8]->{type},     'H',                       'type     is good' );
1150 is( $font_types->[8]->{name},     'Helvetica',               'name     is good' );
1151 is( $font_types->[8]->{selected}, 0,                         'selected is good' );
1152
1153 isa_ok( $font_types->[9], 'HASH', '$font_types->[9] is a HASH' );
1154 is( $font_types->[9]->{type},     'HO',                      'type     is good' );
1155 is( $font_types->[9]->{name},     'Helvetica-Oblique',       'name     is good' );
1156 is( $font_types->[9]->{selected}, 0,                         'selected is good' );
1157
1158 isa_ok( $font_types->[10], 'HASH', '$font_types->[10] is a HASH' );
1159 is( $font_types->[10]->{type},     'HB',                     'type     is good' );
1160 is( $font_types->[10]->{name},     'Helvetica-Bold',         'name     is good' );
1161 is( $font_types->[10]->{selected}, 0,                        'selected is good' );
1162
1163 isa_ok( $font_types->[11], 'HASH', '$font_types->[11] is a HASH' );
1164 is( $font_types->[11]->{type},     'HBO',                    'type     is good' );
1165 is( $font_types->[11]->{name},     'Helvetica-Bold-Oblique', 'name     is good' );
1166 is( $font_types->[11]->{selected}, 0,                        'selected is good' );
1167
1168 # ---------- Testing get_text_justification_types ---------
1169 my $text_justification_types = get_text_justification_types();
1170
1171 is( @$text_justification_types, 3, 'There are 3 text justification types' );
1172 isa_ok( $text_justification_types, 'ARRAY', '$text_justification_types is an ARRAY' );
1173
1174 isa_ok( $text_justification_types->[0], 'HASH', '$font_types->[0] is a HASH' );
1175 is( $text_justification_types->[0]->{type},     'L',         'type     is good' );
1176 is( $text_justification_types->[0]->{name},     'Left',      'name     is good' );
1177 is( $text_justification_types->[0]->{selected}, 0,           'selected is good' );
1178
1179 isa_ok( $text_justification_types->[1], 'HASH', '$font_types->[1] is a HASH' );
1180 is( $text_justification_types->[1]->{type},     'C',         'type     is good' );
1181 is( $text_justification_types->[1]->{name},     'Center',    'name     is good' );
1182 is( $text_justification_types->[1]->{selected}, 0,           'selected is good' );
1183
1184 isa_ok( $text_justification_types->[2], 'HASH', '$font_types->[2] is a HASH' );
1185 is( $text_justification_types->[2]->{type},     'R',         'type     is good' );
1186 is( $text_justification_types->[2]->{name},     'Right',     'name     is good' );
1187 is( $text_justification_types->[2]->{selected}, 0,           'selected is good' );
1188
1189 # ---------- Testing get_unit_values ----------------------
1190 my $unit_values = get_unit_values();
1191
1192 is( @$unit_values, 5, 'There are 5 unit values' );
1193 isa_ok( $unit_values, 'ARRAY', '$unit_values is an ARRAY' );
1194
1195 isa_ok( $unit_values->[0], 'HASH', '$unit_values->[0] is a HASH' );
1196 is( $unit_values->[0]->{type},     'POINT',                  'type     is good' );
1197 is( $unit_values->[0]->{desc},     'PostScript Points',      'desc     is good' );
1198 is( $unit_values->[0]->{value},    1,                        'value    is good' );
1199 is( $unit_values->[0]->{selected}, 0,                        'selected is good' );
1200
1201 isa_ok( $unit_values->[1], 'HASH', '$unit_values->[1] is a HASH' );
1202 is( $unit_values->[1]->{type},     'AGATE',                  'type     is good' );
1203 is( $unit_values->[1]->{desc},     'Adobe Agates',           'desc     is good' );
1204 is( $unit_values->[1]->{value},    5.1428571,                'value    is good' );
1205 is( $unit_values->[1]->{selected}, 0,                        'selected is good' );
1206
1207 isa_ok( $unit_values->[2], 'HASH', '$unit_values->[2] is a HASH' );
1208 is( $unit_values->[2]->{type},     'INCH',                   'type     is good' );
1209 is( $unit_values->[2]->{desc},     'US Inches',              'desc     is good' );
1210 is( $unit_values->[2]->{value},    72,                       'value    is good' );
1211 is( $unit_values->[2]->{selected}, 0,                        'selected is good' );
1212
1213 isa_ok( $unit_values->[3], 'HASH', '$unit_values->[3] is a HASH' );
1214 is( $unit_values->[3]->{type},     'MM',                     'type     is good' );
1215 is( $unit_values->[3]->{desc},     'SI Millimeters',         'desc     is good' );
1216 is( $unit_values->[3]->{value},    2.83464567,               'value    is good' );
1217 is( $unit_values->[3]->{selected}, 0,                        'selected is good' );
1218
1219 isa_ok( $unit_values->[4], 'HASH', '$unit_values->[4] is a HASH' );
1220 is( $unit_values->[4]->{type},     'CM',                     'type     is good' );
1221 is( $unit_values->[4]->{desc},     'SI Centimeters',         'desc     is good' );
1222 is( $unit_values->[4]->{value},    28.3464567,               'value    is good' );
1223 is( $unit_values->[4]->{selected}, 0,                        'selected is good' );
1224
1225 # ---------- Testing get_output_formats -------------------
1226 my $output_formats = get_output_formats();
1227
1228 is( @$output_formats, 2, 'There are 2 output format' );
1229 isa_ok( $output_formats, 'ARRAY', '$output_formats is an ARRAY' );
1230
1231 isa_ok( $output_formats->[0], 'HASH', '$output_formats->[0] is a HASH' );
1232 is( $output_formats->[0]->{type}, 'pdf',      'type is good' );
1233 is( $output_formats->[0]->{desc}, 'PDF File', 'name is good' );
1234
1235 isa_ok( $output_formats->[1], 'HASH', '$output_formats->[1] is a HASH' );
1236 is( $output_formats->[1]->{type}, 'csv',      'type is good' );
1237 is( $output_formats->[1]->{desc}, 'CSV File', 'name is good' );
1238
1239 # ---------- Testing get_table_names ----------------------
1240 my $table_names   = get_table_names("aq");
1241 my $KOHA_PATH     = C4::Context->config("intranetdir");
1242 my $kohastructure = "$KOHA_PATH/installer/data/mysql/kohastructure.sql";
1243
1244 open( my $fh, '<', $kohastructure ) or die $!;
1245
1246 my $tables_names_matching = [];
1247 while ( my $intext = <$fh> ) {
1248     while ( $intext =~ /CREATE TABLE `*\w*aq\w*`*/g ) {
1249         my @tables_names_matching = split( /\ /, $intext );
1250         if ( $tables_names_matching[2] =~ /`*`/ ) {
1251             $tables_names_matching[2] =~ s/`//g;
1252         }
1253         push( @$tables_names_matching, "$tables_names_matching[2]" );
1254     }
1255 }
1256 close $fh;
1257 @$tables_names_matching = sort @$tables_names_matching;
1258 is_deeply( $table_names, $tables_names_matching, 'get_table_names return all tables matching' );
1259
1260 # ---------- Testing html_table ---------------------------
1261 my $display_columns = [
1262     { _label_number  => { label => 'Label Number',  link_field => 0 } },
1263     { _summary       => { label => 'Summary',       link_field => 0 } },
1264     { _item_type     => { label => 'Item Type',     link_field => 0 } },
1265     { _barcode       => { label => 'Barcode',       link_field => 1 } },
1266     { _template_code => { label => 'Template Name', link_field => 0 } },
1267     { select         => { label => 'Select',        value      => '_label_id' } },
1268 ];
1269
1270 #without $data param ------------------
1271 my $db_rows = [];
1272 my $table = html_table( $display_columns, $db_rows );
1273 is( $table, undef, 'No need to generate a table if there is not data to display' );
1274
1275 #with $data param ---------------------
1276 $db_rows = [
1277     {   _label_number => 1,
1278         _summary      => $summary1,
1279         _item_type    => 'Book',
1280         _barcode      => $barcode1,
1281         _label_id     => 'Label ID',
1282         template_id   => $template_id1,
1283     }
1284 ];
1285
1286 $table = html_table( $display_columns, $db_rows );
1287
1288 isa_ok( $table, 'ARRAY', '$table is an ARRAY' );
1289
1290 #POPULATE HEADER
1291 isa_ok( $table->[0]->{header_fields}, 'ARRAY', '$table->[0]->{header_fields} is an ARRAY' );
1292 is( scalar( @{ $table->[0]->{header_fields} } ), 6, 'There are 7 header_fields' );
1293
1294 my $field_value = $display_columns->[0]->{_label_number}->{label};
1295 is( $table->[0]->{header_fields}->[0]->{hidden},       0,                '[Label Number]   hidden        field is good' );
1296 is( $table->[0]->{header_fields}->[0]->{select_field}, 0,                '[Label Number]   select_field  field is good' );
1297 is( $table->[0]->{header_fields}->[0]->{field_name},   '_label_number',  '[Label Number]   field_name    field is good' );
1298 is( $table->[0]->{header_fields}->[0]->{field_label},  $field_value,     '[Label Number]   field_label   field is good' );
1299
1300 $field_value = $display_columns->[1]->{_summary}->{label};
1301 is( $table->[0]->{header_fields}->[1]->{hidden},       0,                '[Summary]        hidden        field is good' );
1302 is( $table->[0]->{header_fields}->[1]->{select_field}, 0,                '[Summary]        select_field  field is good' );
1303 is( $table->[0]->{header_fields}->[1]->{field_name},   '_summary',       '[Summary]        field_name    field is good' );
1304 is( $table->[0]->{header_fields}->[1]->{field_label},  $field_value,     '[Summary]        field_label   field is good' );
1305
1306 $field_value = $display_columns->[2]->{_item_type}->{label};
1307 is( $table->[0]->{header_fields}->[2]->{hidden},       0,                '[Item Type]      hidden        field is good' );
1308 is( $table->[0]->{header_fields}->[2]->{select_field}, 0,                '[Item Type]      select_field  field is good' );
1309 is( $table->[0]->{header_fields}->[2]->{field_name},   '_item_type',     '[Item Type]      field_name    field is good' );
1310 is( $table->[0]->{header_fields}->[2]->{field_label},  $field_value,     '[Item Type]      field_label   field is good' );
1311
1312 $field_value = $display_columns->[3]->{_barcode}->{label};
1313 is( $table->[0]->{header_fields}->[3]->{hidden},       0,                '[Barcode]        hidden        field is good' );
1314 is( $table->[0]->{header_fields}->[3]->{select_field}, 0,                '[Barcode]        select_field  field is good' );
1315 is( $table->[0]->{header_fields}->[3]->{field_name},   '_barcode',       '[Barcode]        field_name    field is good' );
1316 is( $table->[0]->{header_fields}->[3]->{field_label},  $field_value,     '[Barcode]        field_label   field is good' );
1317
1318 $field_value = $display_columns->[4]->{_template_code}->{label};
1319 is( $table->[0]->{header_fields}->[4]->{hidden},       0,                '[Template Code]  hidden        field is good' );
1320 is( $table->[0]->{header_fields}->[4]->{select_field}, 0,                '[Template Code]  select_field  field is good' );
1321 is( $table->[0]->{header_fields}->[4]->{field_name},   '_template_code', '[Template Code]  field_name    field is good' );
1322 is( $table->[0]->{header_fields}->[4]->{field_label},  $field_value,     '[Template Code]  field_label   field is good' );
1323
1324 $field_value = $display_columns->[5]->{select}->{label};
1325 is( $table->[0]->{header_fields}->[5]->{hidden},       0,                '[Select]         hidden        field is good' );
1326 is( $table->[0]->{header_fields}->[5]->{select_field}, 0,                '[Select]         select_field  field is good' );
1327 is( $table->[0]->{header_fields}->[5]->{field_name},   'select',         '[Select]         field_name    field is good' );
1328 is( $table->[0]->{header_fields}->[5]->{field_label},  $field_value,     '[Select]         field_label   field is good' );
1329
1330 #POPULATE TABLE
1331 isa_ok( $table->[1]->{text_fields}, 'ARRAY', '$table->[0]->{text_fields} is an ARRAY' );
1332 is( scalar( @{ $table->[1]->{text_fields} } ), 6, 'There are 6 text_fields' );
1333
1334 #test : if (grep {$table_column eq $_} keys %$db_row)
1335 my $link_field = $display_columns->[0]->{_label_number}->{link_field};
1336 my $field_name = "$table->[0]->{header_fields}->[0]->{field_name}_tbl";
1337 $field_value = $db_rows->[0]->{_label_number};
1338 is( $table->[1]->{text_fields}->[0]->{hidden},       0,               '[Label Number]   hidden        field is good' );
1339 is( $table->[1]->{text_fields}->[0]->{link_field},   $link_field,     '[Label Number]   link_field    field is good' );
1340 is( $table->[1]->{text_fields}->[0]->{select_field}, 0,               '[Label Number]   select_field  field is good' );
1341 is( $table->[1]->{text_fields}->[0]->{field_name},   $field_name,     '[Label Number]   field_name    field is good' );
1342 is( $table->[1]->{text_fields}->[0]->{field_value},  $field_value,    '[Label Number]   field_value   field is good' );
1343
1344 $link_field  = $display_columns->[1]->{_summary}->{link_field};
1345 $field_value = $db_rows->[0]->{_summary};
1346 $field_name  = "$table->[0]->{header_fields}->[1]->{field_name}_tbl";
1347 is( $table->[1]->{text_fields}->[1]->{hidden},       0,               '[Summary]        hidden        field is good' );
1348 is( $table->[1]->{text_fields}->[1]->{link_field},   $link_field,     '[Summary]        link_field    field is good' );
1349 is( $table->[1]->{text_fields}->[1]->{select_field}, 0,               '[Summary]        select_field  field is good' );
1350 is( $table->[1]->{text_fields}->[1]->{field_name},   $field_name,     '[Summary]        field_name    field is good' );
1351 is( $table->[1]->{text_fields}->[1]->{field_value},  $field_value,    '[Summary]        field_value   field is good' );
1352
1353 $link_field  = $display_columns->[2]->{_item_type}->{link_field};
1354 $field_name  = "$table->[0]->{header_fields}->[2]->{field_name}_tbl";
1355 $field_value = $db_rows->[0]->{_item_type};
1356 is( $table->[1]->{text_fields}->[2]->{hidden},       0,               '[Item Type]      hidden        field is good' );
1357 is( $table->[1]->{text_fields}->[2]->{link_field},   $link_field,     '[Item Type]      link_field    field is good' );
1358 is( $table->[1]->{text_fields}->[2]->{select_field}, 0,               '[Item Type]      select_field  field is good' );
1359 is( $table->[1]->{text_fields}->[2]->{field_name},   $field_name,     '[Item Type]      field_name    field is good' );
1360 is( $table->[1]->{text_fields}->[2]->{field_value},  $field_value,    '[Item Type]      field_value   field is good' );
1361
1362 $link_field  = $display_columns->[3]->{_barcode}->{link_field};
1363 $field_name  = "$table->[0]->{header_fields}->[3]->{field_name}_tbl";
1364 $field_value = $db_rows->[0]->{_barcode};
1365 is( $table->[1]->{text_fields}->[3]->{hidden},       0,               '[Barcode]        hidden        field is good' );
1366 is( $table->[1]->{text_fields}->[3]->{link_field},   $link_field,     '[Barcode]        link_field    field is good' );
1367 is( $table->[1]->{text_fields}->[3]->{select_field}, 0,               '[Barcode]        select_field  field is good' );
1368 is( $table->[1]->{text_fields}->[3]->{field_name},   $field_name,     '[Barcode]        field_name    field is good' );
1369 is( $table->[1]->{text_fields}->[3]->{field_value},  $field_value,    '[Barcode]        field_value   field is good' );
1370
1371 #test : elsif ($table_column =~ m/^_((.*)_(.*$))/)
1372 $link_field = $display_columns->[4]->{_template_code}->{link_field};
1373 $field_name = "$table->[0]->{header_fields}->[4]->{field_name}_tbl";
1374 is( $table->[1]->{text_fields}->[4]->{hidden},       0,               '[Template Code]  hidden        field is good' );
1375 is( $table->[1]->{text_fields}->[4]->{link_field},   $link_field,     '[Template Code]  link_field    field is good' );
1376 is( $table->[1]->{text_fields}->[4]->{select_field}, 0,               '[Template Code]  select_field  field is good' );
1377 is( $table->[1]->{text_fields}->[4]->{field_name},   $field_name,     '[Template Code]  field_name    field is good' );
1378 is( $table->[1]->{text_fields}->[4]->{field_value},  $template_code1, '[Template Code]  field_value   field is good' );
1379
1380 #test : elsif ($table_column eq 'select')
1381 $field_value = $db_rows->[0]->{_label_id};
1382 is( $table->[1]->{text_fields}->[5]->{hidden},       0,               '[Select]         hidden        field is good' );
1383 is( $table->[1]->{text_fields}->[5]->{select_field}, 1,               '[Select]         select_field  field is good' );
1384 is( $table->[1]->{text_fields}->[5]->{field_name},   'select',        '[Select]         field_name    field is good' );
1385 is( $table->[1]->{text_fields}->[5]->{field_value},  $field_value,    '[Select]         field_value   field is good' );
1386
1387 # ---------- Testing _SELECT ---------------------------
1388 # Mocking $sth->err and $sth->errstr
1389 {
1390     my $dbi_st = Test::MockModule->new( 'DBI::st', no_auto => 1 );
1391     $dbi_st->mock( 'err',    sub { return 1; } );
1392     $dbi_st->mock( 'errstr', sub { return 'something went wrong'; } );
1393     my $records;
1394     warning_is { $records = C4::Creators::Lib::_SELECT( '*', 'borrowers', "borrowernumber = $borrowernumber1" ) } 'Database returned the following error: something went wrong',
1395       '_SELECT raises warning if something went wrong with the sql request execution';
1396
1397     is( $records, 1, '$record return 1' );
1398 }
1399
1400 #without $params[2] -------------------
1401 my $records = C4::Creators::Lib::_SELECT( 'surname, firstname, cardnumber, branchcode, categorycode', 'borrowers' );
1402
1403 is( @$records, 3, 'There are 3 borrowers' );
1404 isa_ok( $records, 'ARRAY', '$records is an ARRAY' );
1405
1406 isa_ok( $records->[0], 'HASH', '$records->[0] is a HASH' );
1407 is( $records->[0]->{surname},      $surname1,     'surname      is good' );
1408 is( $records->[0]->{firstname},    $firstname1,   'firstname    is good' );
1409 is( $records->[0]->{cardnumber},   $cardnumber1,  'cardnumber   is good' );
1410 is( $records->[0]->{branchcode},   $branchcode,   'branchcode   is good' );
1411 is( $records->[0]->{categorycode}, $categorycode, 'categorycode is good' );
1412
1413 isa_ok( $records->[1], 'HASH', '$records->[1] is a HASH' );
1414 is( $records->[1]->{surname},      $surname2,     'surname      is good' );
1415 is( $records->[1]->{firstname},    $firstname2,   'firstname    is good' );
1416 is( $records->[1]->{cardnumber},   $cardnumber2,  'cardnumber   is good' );
1417 is( $records->[1]->{branchcode},   $branchcode,   'branchcode   is good' );
1418 is( $records->[1]->{categorycode}, $categorycode, 'categorycode is good' );
1419
1420 isa_ok( $records->[2], 'HASH', '$records->[2] is a HASH' );
1421 is( $records->[2]->{surname},      $surname3,     'surname      is good' );
1422 is( $records->[2]->{firstname},    $firstname3,   'firstname    is good' );
1423 is( $records->[2]->{cardnumber},   $cardnumber3,  'cardnumber   is good' );
1424 is( $records->[2]->{branchcode},   $branchcode,   'branchcode   is good' );
1425 is( $records->[2]->{categorycode}, $categorycode, 'categorycode is good' );
1426
1427 #with $params[2] ----------------------
1428 $records = C4::Creators::Lib::_SELECT( 'surname, firstname, cardnumber, branchcode, categorycode', 'borrowers', "borrowernumber = $borrowernumber1" );
1429
1430 is( @$records, 1, 'There is 1 borrower where borrowernumber = $borrowernumber1' );
1431 isa_ok( $records, 'ARRAY', '$records is an ARRAY' );
1432
1433 isa_ok( $records->[0], 'HASH', '$records->[0] is a HASH' );
1434 is( $records->[0]->{surname},      $surname1,     'surname      is good' );
1435 is( $records->[0]->{firstname},    $firstname1,   'firstname    is good' );
1436 is( $records->[0]->{cardnumber},   $cardnumber1,  'cardnumber   is good' );
1437 is( $records->[0]->{branchcode},   $branchcode,   'branchcode   is good' );
1438 is( $records->[0]->{categorycode}, $categorycode, 'categorycode is good' );
1439
1440 # ---------- Sub ------------------------------------------
1441 my %preferences;
1442
1443 sub mock_preference {
1444     my $context = new Test::MockModule('C4::Context');
1445     my ( $pref, $value ) = @_;
1446     $preferences{$pref} = $value;
1447     $context->mock(
1448         'preference',
1449         sub {
1450             my ( $self, $pref ) = @_;
1451             if ( exists $preferences{$pref} ) {
1452                 return $preferences{$pref};
1453             } else {
1454                 my $method = $context->original('preference');
1455                 return $method->( $self, $pref );
1456             }
1457         }
1458     );
1459 }