Line 0
Link Here
|
0 |
- |
1 |
# Copyright 2016 KohaSuomi |
|
|
2 |
# |
3 |
# This file is part of Koha. |
4 |
# |
5 |
|
6 |
use Modern::Perl; |
7 |
use Test::More; |
8 |
|
9 |
use Scalar::Util qw(blessed); |
10 |
use Try::Tiny; |
11 |
|
12 |
use C4::Biblio; |
13 |
use Koha::Validation; |
14 |
|
15 |
use t::lib::TestObjects::SystemPreferenceFactory; |
16 |
use t::lib::TestObjects::ObjectFactory; |
17 |
|
18 |
|
19 |
my $globalContext = {}; |
20 |
my $spref = t::lib::TestObjects::SystemPreferenceFactory->createTestGroup([ |
21 |
{ |
22 |
preference => 'ValidatePhoneNumber', |
23 |
value => 'fin', |
24 |
}, |
25 |
{ |
26 |
preference => 'ValidateEmailAddress', |
27 |
value => 1, |
28 |
}, |
29 |
], undef, $globalContext); |
30 |
|
31 |
subtest "Validate email", \&simpleEmail; |
32 |
sub simpleEmail { |
33 |
is(Koha::Validation::use_validator('email', 'koha@example.com'), |
34 |
1, |
35 |
"Valid email"); |
36 |
is(Koha::Validation::use_validator('email', 'koha@examplecom'), |
37 |
0, |
38 |
"InValid email"); |
39 |
} |
40 |
|
41 |
subtest "Validate phone", \&simplePhone; |
42 |
sub simplePhone { |
43 |
is(Koha::Validation::use_validator('phone', '+358504497763'), |
44 |
1, |
45 |
"Valid phone"); |
46 |
is(Koha::Validation::use_validator('phone', '+35504497763'), |
47 |
0, |
48 |
"InValid phone"); |
49 |
} |
50 |
|
51 |
subtest "Validate array of something", \&arrayOf; |
52 |
sub arrayOf { |
53 |
my @array = ('+358504497763', '0451123123'); |
54 |
is(Koha::Validation->tries('phnbr', \@array, 'phone', 'a' ), |
55 |
1, |
56 |
"Valid phonenumber array"); |
57 |
|
58 |
push(@array, 'soita heti'); |
59 |
try { |
60 |
is(Koha::Validation->tries('phnbr', \@array, 'phone', 'a' ), |
61 |
'SHOULD THROW AN EXCEPTION!', |
62 |
"InValid phonenumber array"); |
63 |
} catch { |
64 |
is(ref($_), 'Koha::Exception::BadParameter', |
65 |
"InValid phonenumber array"); |
66 |
|
67 |
is($_->message, |
68 |
"'phnbr->2' 'soita heti' is not a valid 'phonenumber'", |
69 |
"Got a well formatted error message"); |
70 |
}; |
71 |
} |
72 |
|
73 |
subtest "Validate string", \&simpleString; |
74 |
sub simpleString { |
75 |
|
76 |
is(Koha::Validation->tries('key', '+358504497763', 'string'), |
77 |
1, |
78 |
"Valid string"); |
79 |
|
80 |
try { |
81 |
is(Koha::Validation->tries('key', 'A', 'string'), |
82 |
'SHOULD THROW AN EXCEPTION!', |
83 |
"Not a string, but a char"); |
84 |
} catch { |
85 |
is(ref($_), 'Koha::Exception::BadParameter', |
86 |
"Not a string, but a char"); |
87 |
|
88 |
is($_->message, |
89 |
"'key' 'A' is not a valid 'string', but a char", |
90 |
"Got a well formatted error message"); |
91 |
}; |
92 |
|
93 |
try { |
94 |
is(Koha::Validation->tries('key', '', 'string'), |
95 |
'SHOULD THROW AN EXCEPTION!', |
96 |
"Not a string, but a nothing"); |
97 |
} catch { |
98 |
is(ref($_), 'Koha::Exception::BadParameter', |
99 |
"Not a string, but a nothing"); |
100 |
}; |
101 |
|
102 |
is(Koha::Validation->tries('key', 'AB', 'string'), |
103 |
1, |
104 |
"Valid short String"); |
105 |
} |
106 |
|
107 |
subtest "Array of Array of doubles", \&nestedArrayDoubles; |
108 |
sub nestedArrayDoubles { |
109 |
|
110 |
my @array = ([0.4, 1.2], |
111 |
[4.5, 7.9]); |
112 |
is(Koha::Validation->tries('nay', \@array, 'double', 'aa'), |
113 |
1, |
114 |
"Valid nested array of doubles"); |
115 |
|
116 |
push(@array, [2, 'lol']); |
117 |
try { |
118 |
is(Koha::Validation->tries('nay', \@array, 'double', 'aa'), |
119 |
'SHOULD THROW AN EXCEPTION!', |
120 |
"InValid nested array of doubles"); |
121 |
} catch { |
122 |
is(ref($_), 'Koha::Exception::BadParameter', |
123 |
"InValid nested array of doubles"); |
124 |
|
125 |
is($_->message, |
126 |
"'nay->2->1' 'lol' is not a valid 'double'", |
127 |
"Got a well formatted error message"); |
128 |
}; |
129 |
} |
130 |
|
131 |
subtest "MARC Selectors", \&marcSelectors; |
132 |
sub marcSelectors { |
133 |
|
134 |
is(Koha::Validation->tries('mrc', '856', 'marcFieldSelector',), |
135 |
1, |
136 |
"Valid MARC Field selector"); |
137 |
is(Koha::Validation::getMARCFieldSelectorCache, '856', "Validated MARC Field remembered"); |
138 |
is(Koha::Validation::getMARCSubfieldSelectorCache, undef, "Not touched MARC Subfield not defined"); |
139 |
|
140 |
try { |
141 |
is(Koha::Validation->tries('mrc', '85u', 'marcFieldSelector',), |
142 |
'SHOULD THROW AN EXCEPTION!', |
143 |
"InValid MARC Field selector"); |
144 |
} catch { |
145 |
is(ref($_), 'Koha::Exception::BadParameter', |
146 |
"InValid MARC Field selector"); |
147 |
|
148 |
is($_->message, |
149 |
"'mrc' '85u' is not a MARC field selector", |
150 |
"Got a well formatted error message"); |
151 |
}; |
152 |
is(Koha::Validation::getMARCFieldSelectorCache, undef, "InValidated MARC Field forgot"); |
153 |
is(Koha::Validation::getMARCSubfieldSelectorCache, undef, "Not touched MARC Subfield not defined"); |
154 |
|
155 |
is(Koha::Validation->tries('mrc', '110a', 'marcSubfieldSelector',), |
156 |
1, |
157 |
"Valid MARC Subfield selector"); |
158 |
is(Koha::Validation::getMARCFieldSelectorCache, '110', "Validated MARC Field remembered"); |
159 |
is(Koha::Validation::getMARCSubfieldSelectorCache, 'a', "Validated MARC Subfield remembered"); |
160 |
|
161 |
try { |
162 |
is(Koha::Validation->tries('mrc', '110', 'marcSubfieldSelector',), |
163 |
'SHOULD THROW AN EXCEPTION!', |
164 |
"InValid MARC Subfield selector"); |
165 |
} catch { |
166 |
is(ref($_), 'Koha::Exception::BadParameter', |
167 |
"InValid MARC Subfield selector"); |
168 |
|
169 |
is($_->message, |
170 |
"'mrc' '110' is not a MARC subfield selector", |
171 |
"Got a well formatted error message"); |
172 |
}; |
173 |
is(Koha::Validation::getMARCFieldSelectorCache, undef, "InValidated MARC Field forgot"); |
174 |
is(Koha::Validation::getMARCSubfieldSelectorCache, undef, "InValidated MARC Subfield forgot"); |
175 |
|
176 |
is(Koha::Validation->tries('mrc', '110a', 'marcSelector',), |
177 |
1, |
178 |
"Valid MARC selector"); |
179 |
is(Koha::Validation::getMARCFieldSelectorCache, '110', "Validated MARC Field remembered"); |
180 |
is(Koha::Validation::getMARCSubfieldSelectorCache, 'a', "Validated MARC Subfield remembered"); |
181 |
|
182 |
is(Koha::Validation->tries('mrc', '245', 'marcSelector',), |
183 |
1, |
184 |
"Valid MARC selector"); |
185 |
is(Koha::Validation::getMARCFieldSelectorCache, '245', "Validated MARC Field remembered"); |
186 |
is(Koha::Validation::getMARCSubfieldSelectorCache, '', "Not given MARC Subfield forgot"); |
187 |
|
188 |
my ($f, $sf) = C4::Biblio::GetMarcFromKohaField('biblio.title', ''); |
189 |
is(Koha::Validation->tries('mrc', 'biblio.title', 'marcSelector'), |
190 |
1, |
191 |
"biblio.title is a valid MARC Selector"); |
192 |
is(Koha::Validation::getMARCFieldSelectorCache, $f, "Field from KohaToMarcMapping"); |
193 |
is(Koha::Validation::getMARCSubfieldSelectorCache, $sf, "Subfield from KohaToMarcMapping"); |
194 |
} |
195 |
|
196 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($globalContext); |
197 |
done_testing(); |