Merge remote-tracking branch 'kc/new/bug_5995' into kcmaster
[koha.git] / t / db_dependent / Labels / t_Profile.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 => 27;
24 use C4::Context;
25 use Data::Dumper;
26
27 BEGIN {
28     use_ok('C4::Labels::Profile');
29 }
30
31 my $expected_profile = {
32         creator         => 'Labels',
33         printer_name    => 'Circulation Desk',
34         template_id     => '',
35         paper_bin       => 'bypass',
36         offset_horz     => 0,
37         offset_vert     => 0,
38         creep_horz      => 0,
39         creep_vert      => 0,
40         units           => 'POINT',
41 };
42
43 my $err = 0;
44
45 diag "Testing Profile->new() method.";
46 ok(my $profile = C4::Labels::Profile->new(printer_name => 'Circulation Desk',paper_bin => 'bypass')) || diag"Profile->new() FAILED.";
47 is_deeply($profile, $expected_profile) || diag "New profile object FAILED to verify.";
48
49 diag "Testing Profile->get_attr() method.";
50 foreach my $key (keys %{$expected_profile}) {
51     ok($expected_profile->{$key} eq $profile->get_attr($key)) || diag "Profile->get_attr() FAILED on attribute $key.";
52 }
53
54 diag "Testing Profile->set_attr() method.";
55 my $new_attr = {
56     printer_name    => 'Cataloging Desk',
57     template_id     => '1',
58     paper_bin       => 'tray 1',
59     offset_horz     => 0.3,
60     offset_vert     => 0.85,
61     creep_horz      => 0.156,
62     creep_vert      => 0.67,
63     units           => 'INCH',
64     creator         => 'Labels',
65 };
66
67 foreach my $key (keys %{$new_attr}) {
68     $err = $profile->set_attr($key, $new_attr->{$key});
69     ok(($new_attr->{$key} eq $profile->get_attr($key)) && ($err lt 1)) || diag "Profile->set_attr() FAILED on attribute $key.";
70 }
71
72 diag "Testing Profile->save() method with a new object.";
73
74 my $sav_results = $profile->save();
75 ok($sav_results ne -1) || diag "Profile->save() FAILED.";
76
77 my $saved_profile;
78 if ($sav_results ne -1) {
79     diag "Testing Profile->retrieve() method.";
80     $new_attr->{'profile_id'} = $sav_results;
81     ok($saved_profile = C4::Labels::Profile->retrieve(profile_id => $sav_results)) || diag "Profile->retrieve() FAILED.";
82     is_deeply($saved_profile, $new_attr) || diag "Retrieved profile object FAILED to verify.";
83 }
84
85 diag "Testing Profile->save() method with an updated object.";
86
87 $err = 0; # Reset error code
88 $err = $saved_profile->set_attr(units => 'CM');
89 my $upd_results = $saved_profile->save();
90 ok(($upd_results ne -1) && ($err lt 1)) || diag "Profile->save() FAILED.";
91 my $updated_profile = C4::Labels::Profile->retrieve(profile_id => $sav_results);
92 is_deeply($updated_profile, $saved_profile) || diag "Updated layout object FAILED to verify.";
93
94 diag "Testing Profile->delete() method.";
95
96 my $del_results = $updated_profile->delete();
97 ok($del_results ne -1) || diag "Profile->delete() FAILED.";