Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# |
3 |
# Copyright 2017 Koha-Suomi Oy |
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 3 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 Modern::Perl; |
21 |
|
22 |
use Test::More tests => 3; |
23 |
|
24 |
use t::lib::Mocks; |
25 |
|
26 |
BEGIN { |
27 |
use_ok('Koha::Validation'); |
28 |
} |
29 |
|
30 |
subtest 'email() tests' => sub { |
31 |
plan tests => 2; |
32 |
|
33 |
is(Koha::Validation::email('test'), 0, "'test' is invalid e-mail address'"); |
34 |
is(Koha::Validation::email('test@example.com'), 1, '\'test@example.com\' is ' |
35 |
.'valid e-mail address'); |
36 |
}; |
37 |
|
38 |
subtest 'phone() tests' => sub { |
39 |
plan tests => 3; |
40 |
|
41 |
t::lib::Mocks::mock_preference('ValidatePhoneNumber', ''); |
42 |
|
43 |
is(Koha::Validation::phone('test'), 1, 'Phone number validation is switched ' |
44 |
.'off, so \'test\' is a valid phone number'); |
45 |
|
46 |
# An example: Finnish Phone number validation regex |
47 |
subtest 'Finnish phone number validation regex' => sub { |
48 |
t::lib::Mocks::mock_preference('ValidatePhoneNumber', |
49 |
'^((90[0-9]{3})?0|\+358\s?)(?!(100|20(0|2(0|[2-3])|9[8-9])|300|600|70' |
50 |
.'0|708|75(00[0-3]|(1|2)\d{2}|30[0-2]|32[0-2]|75[0-2]|98[0-2])))(4|50|' |
51 |
.'10[1-9]|20(1|2(1|[4-9])|[3-9])|29|30[1-9]|71|73|75(00[3-9]|30[3-9]|3' |
52 |
.'2[3-9]|53[3-9]|83[3-9])|2|3|5|6|8|9|1[3-9])\s?(\d\s?){4,19}\d$' |
53 |
); |
54 |
|
55 |
is(Koha::Validation::phone('1234'), 0, '1234 is invalid phone number.'); |
56 |
is(Koha::Validation::phone('+358501234567'), 1, '+358501234567 is valid ' |
57 |
.'phone number.'); |
58 |
is(Koha::Validation::phone('+1-202-555-0198'), 0, '+1-202-555-0198 is ' |
59 |
.'invalid phone number.'); |
60 |
}; |
61 |
|
62 |
subtest 'International phone number validation regex' => sub { |
63 |
t::lib::Mocks::mock_preference('ValidatePhoneNumber', |
64 |
'^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]' |
65 |
.'{1,12}){1,2}$' |
66 |
); |
67 |
|
68 |
is(Koha::Validation::phone('nope'), 0, 'nope is invalid phone number.'); |
69 |
is(Koha::Validation::phone('1234'), 1, '1234 is valid phone number.'); |
70 |
is(Koha::Validation::phone('+358501234567'), 1, '+358501234567 is valid ' |
71 |
.'phone number.'); |
72 |
is(Koha::Validation::phone('+1-202-555-0198'), 1, '+1-202-555-0198 is ' |
73 |
.'valid phone number.'); |
74 |
}; |
75 |
|
76 |
}; |