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