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