replace syslog with warns
[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 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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use Test::More tests => 52;
24 use C4::Context;
25 use Data::Dumper;
26
27 BEGIN {
28     use_ok('C4::Labels::Template');
29 }
30
31 my $expect_template = {
32         profile_id      =>      0,
33         template_code   =>      'DEFAULT TEMPLATE',
34         template_desc   =>      'Default description',
35         page_width      =>      8.5,
36         page_height     =>      0,
37         label_width     =>      0,
38         label_height    =>      0,
39         top_text_margin =>      0,
40         left_text_margin =>      0,
41         top_margin      =>      0,
42         left_margin     =>      0,
43         cols            =>      3,
44         rows            =>      0,
45         col_gap         =>      0,
46         row_gap         =>      0,
47         units           =>      'POINT',
48         template_stat   =>      0,
49 };
50
51 my $template;
52
53 diag "Testing Template->new() method.";
54 ok($template = C4::Labels::Template->new(page_width => 8.5,cols => 3))  || diag "Template->new() FAILED.";
55 is_deeply($template, $expect_template) || diag "New template object FAILED to verify.";
56
57 diag "Testing Template->get_attr() method.";
58 foreach my $key (keys %{$expect_template}) {
59     ok($expect_template->{$key} eq $template->get_attr($key)) || diag "Template->get_attr() FAILED on attribute $key.";
60 }
61
62 diag "Testing Template->set_attr() method.";
63 my $new_attr = {
64     profile_id          => 0,
65     template_code       => 'Avery 5160 | 1 x 2-5/8',
66     template_desc       => '3 columns, 10 rows of labels',
67     page_width          => 8.5,
68     page_height         => 11,
69     label_width         => 2.63,
70     label_height        => 1,
71     top_text_margin     => 0.139,
72     left_text_margin    => 0.0417,
73     top_margin          => 0.35,
74     left_margin         => 0.23,
75     cols                => 3,
76     rows                => 10,
77     col_gap             => 0.13,
78     row_gap             => 0,
79     units               => 'INCH',
80     template_stat       => 1,
81 };
82
83 foreach my $key (keys %{$new_attr}) {
84     next if ($key eq 'template_stat');
85     $template->set_attr($key, $new_attr->{$key});
86     ok($new_attr->{$key} eq $template->get_attr($key)) || diag "Template->set_attr() FAILED on attribute $key.";
87 }
88
89 diag "Testing Template->save() method with a new object.";
90
91 my $sav_results = $template->save();
92 ok($sav_results ne -1) || diag "Template->save() FAILED.";
93
94 my $saved_template;
95 if ($sav_results ne -1) {
96     diag "Testing Template->retrieve() method.";
97     $new_attr->{'template_id'} = $sav_results;
98     ok($saved_template = C4::Labels::Template->retrieve(template_id => $sav_results))  || diag "Template->retrieve() FAILED.";
99     is_deeply($saved_template, $new_attr) || diag "Retrieved template object FAILED to verify.";
100 }
101
102 diag "Testing Template->save method with an updated object.";
103
104 $saved_template->set_attr(template_desc => 'A test template');
105 my $upd_results = $saved_template->save();
106 ok($upd_results ne -1) || diag "Template->save() FAILED.";
107 my $updated_template = C4::Labels::Template->retrieve(template_id => $sav_results);
108 is_deeply($updated_template, $saved_template) || diag "Updated template object FAILED to verify.";
109
110 diag "Testing Template->retrieve() convert points option.";
111
112 my $conv_template = C4::Labels::Template->retrieve(template_id => $sav_results, convert => 1);
113 my $expect_conv = {
114     page_width          => 612,
115     page_height         => 792,
116     label_width         => 189.36,
117     label_height        => 72,
118     top_text_margin     => 10.008,
119     left_text_margin    => 3.0024,
120     top_margin          => 25.2,
121     left_margin         => 16.56,
122     col_gap             => 9.36,
123     row_gap             => 0,
124 };
125
126 foreach my $key (keys %{$expect_conv}) {
127     ok($expect_conv->{$key} eq $conv_template->get_attr($key)) || diag "Template->retrieve() convert points option FAILED. Expected " . $expect_conv->{$key} . " but got " . $conv_template->get_attr($key) . ".";
128 }
129
130 diag "Testing Template->delete() method.";
131
132 my $del_results = $updated_template->delete();
133 ok($del_results ne -1) || diag "Template->delete() FAILED.";