Merge remote-tracking branch 'kc/new/bug_6755' into kcmaster
[koha.git] / t / db_dependent / Labels / t_Layout.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2007 Foundations Bible College.
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 2 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, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use Test::More tests => 36;
24 use C4::Context;
25 use Data::Dumper;
26
27 BEGIN {
28     use_ok('C4::Labels::Layout');
29 }
30
31 my $default_layout = {
32         creator         =>      'Labels',
33         layout_xml      =>      '',
34         units           =>      'POINT',
35         start_label     =>      1,
36         barcode_type    =>      'CODE39',
37         printing_type   =>      'BAR',
38         layout_name     =>      'TEST',
39         guidebox        =>      0,
40         font            =>      'TR',
41         font_size       =>      3,
42         callnum_split   =>      0,
43         text_justify    =>      'L',
44         format_string   =>      'title, author, isbn, issn, itemtype, barcode, callnumber',
45     };
46
47 my $layout;
48
49 diag "Testing Layout->new() method.";
50 ok($layout = C4::Labels::Layout->new(layout_name => 'TEST')) || diag "Layout->new() FAILED";
51 is_deeply($layout, $default_layout) || diag "New layout object FAILED to verify.";
52
53 diag "Testing Layout->get_attr() method.";
54 foreach my $key (keys %{$default_layout}) {
55     ok($default_layout->{$key} eq $layout->get_attr($key)) || diag "Layout->get_attr() FAILED on attribute $key.";
56 }
57
58 diag "Testing Layout->set_attr() method.";
59 my $new_attr = {
60         creator         =>      'Labels',
61         layout_xml      =>      '',
62         units           =>      'POINT',
63         start_label     =>      1,
64         barcode_type    =>      'CODE39',
65         printing_type   =>      'BIBBAR',
66         layout_name     =>      'TEST',
67         guidebox        =>      1,
68         font            =>      'TR',
69         font_size       =>      10,
70         callnum_split   =>      1,
71         text_justify    =>      'L',
72         format_string   =>      'callnumber, title, author, barcode',
73     };
74
75 foreach my $key (keys %{$new_attr}) {
76     $layout->set_attr($key => $new_attr->{$key});
77     ok($new_attr->{$key} eq $layout->get_attr($key)) || diag "Layout->set_attr() FAILED on attribute $key.";
78 }
79
80 diag "Testing Layout->save() method with a new object.";
81
82 my $sav_results = $layout->save();
83 ok($sav_results ne -1) || diag "Layout->save() FAILED";
84
85 my $saved_layout;
86 if ($sav_results ne -1) {
87     diag "Testing Layout->retrieve() method.";
88     $new_attr->{'layout_id'} = $sav_results;
89     ok($saved_layout = C4::Labels::Layout->retrieve(layout_id => $sav_results)) || diag "Layout->retrieve() FAILED";
90     is_deeply($saved_layout, $new_attr) || diag "Retrieved layout object FAILED to verify.";
91 }
92
93 diag "Testing Layout->save() method with an updated object.";
94
95 $saved_layout->set_attr(font => 'C');
96 my $upd_results = $saved_layout->save();
97 ok($upd_results ne -1) || diag "Layout->save() FAILED";
98 my $updated_layout = C4::Labels::Layout->retrieve(layout_id => $sav_results);
99 is_deeply($updated_layout, $saved_layout) || diag "Updated layout object FAILED to verify.";
100
101 diag "Testing Layout->get_text_wrap_cols() method.";
102
103 ok($updated_layout->get_text_wrap_cols(label_width => 180, left_text_margin => 18) eq 21) || diag "Layout->get_text_wrap_cols() FAILED.";
104
105 diag "Testing Layout->delete() method.";
106
107 my $del_results = $updated_layout->delete();
108 ok($del_results ne -1) || diag "Layout->delete() FAILED";