Added unit tests using a test database for XISBN.
[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
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 => 54;
24 use C4::Context;
25 use Data::Dumper;
26
27 BEGIN {
28     use_ok('C4::Labels::Template');
29 }
30
31 my $expect_template = {
32         creator         =>      'Labels',
33         profile_id      =>      0,
34         template_code   =>      'DEFAULT TEMPLATE',
35         template_desc   =>      'Default description',
36         page_width      =>      8.5,
37         page_height     =>      0,
38         label_width     =>      0,
39         label_height    =>      0,
40         top_text_margin =>      0,
41         left_text_margin =>      0,
42         top_margin      =>      0,
43         left_margin     =>      0,
44         cols            =>      3,
45         rows            =>      0,
46         col_gap         =>      0,
47         row_gap         =>      0,
48         units           =>      'POINT',
49         template_stat   =>      0,
50 };
51
52 my $template;
53
54 diag "Testing Template->new() method.";
55 ok($template = C4::Labels::Template->new(page_width => 8.5,cols => 3))  || diag "Template->new() FAILED.";
56 is_deeply($template, $expect_template) || diag "New template object FAILED to verify.";
57
58 diag "Testing Template->get_attr() method.";
59 foreach my $key (keys %{$expect_template}) {
60     ok($expect_template->{$key} eq $template->get_attr($key)) || diag "Template->get_attr() FAILED on attribute $key.";
61 }
62
63 diag "Testing Template->set_attr() method.";
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)) || diag "Template->set_attr() FAILED on attribute $key.";
89 }
90
91 diag "Testing Template->save() method with a new object.";
92
93 my $sav_results = $template->save();
94 ok($sav_results ne -1) || diag "Template->save() FAILED.";
95
96 my $saved_template;
97 if ($sav_results ne -1) {
98     diag "Testing Template->retrieve() method.";
99     $new_attr->{'template_id'} = $sav_results;
100     ok($saved_template = C4::Labels::Template->retrieve(template_id => $sav_results))  || diag "Template->retrieve() FAILED.";
101     is_deeply($saved_template, $new_attr) || diag "Retrieved template object FAILED to verify.";
102 }
103
104 diag "Testing Template->save method with an updated object.";
105
106 $saved_template->set_attr(template_desc => 'A test template');
107 my $upd_results = $saved_template->save();
108 ok($upd_results ne -1) || diag "Template->save() FAILED.";
109 my $updated_template = C4::Labels::Template->retrieve(template_id => $sav_results);
110 is_deeply($updated_template, $saved_template) || diag "Updated template object FAILED to verify.";
111
112 diag "Testing Template->retrieve() convert points option.";
113
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)) || diag "Template->retrieve() convert points option FAILED. Expected " . $expect_conv->{$key} . " but got " . $conv_template->get_attr($key) . ".";
130 }
131
132 diag "Testing Template->delete() method.";
133
134 my $del_results = $updated_template->delete();
135 ok($del_results ne -1) || diag "Template->delete() FAILED.";