Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Labels / t_Template.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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 54;
23 use C4::Context;
24
25 BEGIN {
26     use_ok('C4::Labels::Template');
27 }
28
29 my $expect_template = {
30         creator         =>      'Labels',
31         profile_id      =>      0,
32         template_code   =>      'DEFAULT TEMPLATE',
33         template_desc   =>      'Default description',
34         page_width      =>      8.5,
35         page_height     =>      0,
36         label_width     =>      0,
37         label_height    =>      0,
38         top_text_margin =>      0,
39         left_text_margin =>      0,
40         top_margin      =>      0,
41         left_margin     =>      0,
42         cols            =>      3,
43         rows            =>      0,
44         col_gap         =>      0,
45         row_gap         =>      0,
46         units           =>      'POINT',
47         template_stat   =>      0,
48 };
49
50 my $template;
51
52 # Testing Template->new()
53 ok($template = C4::Labels::Template->new(page_width => 8.5,cols => 3),
54     "Template->new() success.");
55 is_deeply($template, $expect_template,  "New template object verify success");
56
57 # Testing Template->get_attr()
58 foreach my $key (keys %{$expect_template}) {
59     ok($expect_template->{$key} eq $template->get_attr($key),
60         "Template->get_attr() success on attribute $key");
61 }
62
63 # Testing Template->set_attr()
64 my $new_attr = {
65     creator             => 'Labels',
66     profile_id          => 0,
67     template_code       => 'Avery 5160 | 1 x 2-5/8',
68     template_desc       => '3 columns, 10 rows of labels',
69     page_width          => 8.5,
70     page_height         => 11,
71     label_width         => 2.63,
72     label_height        => 1,
73     top_text_margin     => 0.139,
74     left_text_margin    => 0.0417,
75     top_margin          => 0.35,
76     left_margin         => 0.23,
77     cols                => 3,
78     rows                => 10,
79     col_gap             => 0.13,
80     row_gap             => 0,
81     units               => 'INCH',
82     template_stat       => 1,
83 };
84
85 foreach my $key (keys %{$new_attr}) {
86     next if ($key eq 'template_stat');
87     $template->set_attr($key, $new_attr->{$key});
88     ok($new_attr->{$key} eq $template->get_attr($key),
89        "Template->set_attr() success on attribute $key");
90 }
91
92 # Testing Template->save() with a new object
93 my $sav_results = $template->save();
94 ok($sav_results ne -1, "Template->save() success");
95
96 my $saved_template;
97 if ($sav_results ne -1) {
98     # Testing Template->retrieve()
99     $new_attr->{'template_id'} = $sav_results;
100     ok($saved_template = C4::Labels::Template->retrieve(template_id => $sav_results),
101        "Template->retrieve() success");
102     is_deeply($saved_template, $new_attr,
103               "Retrieved template object verify success");
104 }
105
106 # Testing Template->save with an updated object
107 $saved_template->set_attr(template_desc => 'A test template');
108 my $upd_results = $saved_template->save();
109 ok($upd_results ne -1, "Template->save() success");
110 my $updated_template = C4::Labels::Template->retrieve(template_id => $sav_results);
111 is_deeply($updated_template, $saved_template, "Updated template object verify success");
112
113 # Testing Template->retrieve() convert points option
114 my $conv_template = C4::Labels::Template->retrieve(template_id => $sav_results, convert => 1);
115 my $expect_conv = {
116     page_width          => 612,
117     page_height         => 792,
118     label_width         => 189.36,
119     label_height        => 72,
120     top_text_margin     => 10.008,
121     left_text_margin    => 3.0024,
122     top_margin          => 25.2,
123     left_margin         => 16.56,
124     col_gap             => 9.36,
125     row_gap             => 0,
126 };
127
128 foreach my $key (keys %{$expect_conv}) {
129     ok($expect_conv->{$key} eq $conv_template->get_attr($key),
130        "Template->retrieve() convert points option success ($expect_conv->{$key})")
131        || diag("Expected " . $expect_conv->{$key} . " but got " . $conv_template->get_attr($key) . ".");
132 }
133
134 # Testing Template->delete()
135 my $del_results = $updated_template->delete();
136 ok($del_results ne -1, "Template->delete() success");
137