Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 108; |
21 |
use t::lib::Mocks; |
22 |
|
23 |
BEGIN { |
24 |
t::lib::Mocks::mock_dbh; |
25 |
use_ok('Koha::Object'); |
26 |
use_ok('Koha::Category'); |
27 |
use_ok('Koha::Categories'); |
28 |
} |
29 |
|
30 |
# We want to test: |
31 |
# - Creating categories |
32 |
# - Accessors |
33 |
# - Setters |
34 |
# - Second Accessors |
35 |
# - Searching for categories [no, because db_dependent] |
36 |
# - Finding categories [no, because db_dependent] |
37 |
|
38 |
# Preliminaries: |
39 |
# The Category Accessor/Field names. |
40 |
my @xsors = qw/categorycode description enrolmentperiod enrolmentperioddate |
41 |
upperagelimit dateofbirthrequired finetype bulk enrolmentfee |
42 |
overduenoticerequired issuelimit reservefee hidelostitems |
43 |
category_type BlockExpiredPatronOpacActions default_privacy/; |
44 |
|
45 |
# Some Category creation data. |
46 |
my @cat_data = ( |
47 |
{ id => 'cat1', num => 21, bool => 1 }, |
48 |
{ id => 'cat2', num => 16, bool => 0 }, |
49 |
{ id => 'cat3', num => 35, bool => 1 }, |
50 |
); |
51 |
|
52 |
# Tests: Creating categories |
53 |
my @cats = map { |
54 |
my @data = ( |
55 |
$_->{id}, "Test category created (" . $_ . ")", |
56 |
365 + $_->{num}, '2016-01-01', $_->{num}, $_->{bool}, |
57 |
'BLA', $_->{bool}, $_->{num} + 0.53, $_->{bool}, |
58 |
$_->{num} - 5, $_->{num} - 5.06, $_->{bool}, 'A', |
59 |
-1, 'default', |
60 |
); |
61 |
my $props = {}; |
62 |
foreach my $xsor (@xsors) { |
63 |
$props->{$xsor} = shift @data; |
64 |
}; |
65 |
{ id => $_->{id}, obj => Koha::Category->new($props), data => $props } |
66 |
if (ok(Koha::Category->new($props), "Creating categories ['$_->{id}']")); |
67 |
} @cat_data; |
68 |
|
69 |
# Tests: Is a Category |
70 |
map { isa_ok($_->{obj}, 'Koha::Category', $_->{id}) } @cats; |
71 |
|
72 |
# Tests: Accessors |
73 |
map { |
74 |
my $cat = $_; |
75 |
map { |
76 |
is( |
77 |
$cat->{obj}->$_, $cat->{data}->{$_}, |
78 |
"Accessors ['$cat->{id}': '$_']" |
79 |
); |
80 |
} @xsors; |
81 |
} @cats; |
82 |
|
83 |
# Some new Category data. |
84 |
my @cat_data_new = ( |
85 |
{ id => 'cat1new', num => 31, bool => 0 }, |
86 |
{ id => 'cat2new', num => 76, bool => 1 }, |
87 |
{ id => 'cat3new', num => 55, bool => 0 }, |
88 |
); |
89 |
|
90 |
# Tests: Setters |
91 |
my @cats_new = map { |
92 |
my $dt = shift @cat_data_new; |
93 |
my @data = ( |
94 |
$dt->{id}, "Test category created (" . $dt . ")", |
95 |
365 + $dt->{num}, '2014-01-01', $dt->{num}, $dt->{bool}, |
96 |
'FOO', $dt->{bool}, $dt->{num} + 0.53, $dt->{bool}, |
97 |
$dt->{num} - 5, $dt->{num} - 5.06, $dt->{bool}, 'A', |
98 |
-1, 'default', |
99 |
); |
100 |
my $props = {}; |
101 |
foreach my $xsor (@xsors) { |
102 |
$props->{$xsor} = shift @data; |
103 |
}; |
104 |
{ id => $_->{id}, obj => $_->{obj}, data => $props } |
105 |
if (ok($_->{obj}->set($props), "Setters ['$_->{id}']")); |
106 |
} @cats; |
107 |
|
108 |
# Tests: Second Accessors |
109 |
map { |
110 |
my $cat = $_; |
111 |
map { |
112 |
is( |
113 |
$cat->{obj}->$_, $cat->{data}->{$_}, |
114 |
"Second Accessors ['$cat->{id}': '$_']" |
115 |
); |
116 |
} @xsors; |
117 |
} @cats_new; |
118 |
|
119 |
# These are db_dependent, and hence are not performed here. |
120 |
# # Search Tests |
121 |
# is( |
122 |
# Koha::Categories->search({ dateofbirthrequired => 1})->count, 2, |
123 |
# 'Search works' |
124 |
# ); |
125 |
|
126 |
# # Find Tests |
127 |
# map { |
128 |
# is( |
129 |
# Koha::Categories->find($_->{id})->enrolmentperiod, |
130 |
# $_->{data}->{enrolmentperiod}, $_->{id} . ' Found' |
131 |
# ); |
132 |
# } @cats; |
133 |
|
134 |
1; |