1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace factor_sms;
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Tests for sms factor.
|
|
|
22 |
*
|
|
|
23 |
* @covers \factor_sms\factor
|
|
|
24 |
* @package factor_sms
|
|
|
25 |
* @copyright 2023 Raquel Ortega <raquel.ortega@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class factor_test extends \advanced_testcase {
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Data provider for test_format_number().
|
|
|
32 |
*
|
|
|
33 |
* @return array of different country codes and phone numbers.
|
|
|
34 |
*/
|
|
|
35 |
public function format_number_provider(): array {
|
|
|
36 |
|
|
|
37 |
return [
|
|
|
38 |
'Phone number with local format' => [
|
|
|
39 |
'phonenumber' => '0123456789',
|
|
|
40 |
'expected' => '+34123456789',
|
|
|
41 |
'countrycode' => '34',
|
|
|
42 |
],
|
|
|
43 |
'Phone number without international format' => [
|
|
|
44 |
'phonenumber' => '123456789',
|
|
|
45 |
'expected' => '+34123456789',
|
|
|
46 |
'countrycode' => '34',
|
|
|
47 |
],
|
|
|
48 |
'Phone number with international format' => [
|
|
|
49 |
'phonenumber' => '+39123456789',
|
|
|
50 |
'expected' => '+39123456789',
|
|
|
51 |
],
|
|
|
52 |
'Phone number with spaces using international format' => [
|
|
|
53 |
'phonenumber' => '+34 123 456 789',
|
|
|
54 |
'expected' => '+34123456789',
|
|
|
55 |
],
|
|
|
56 |
'Phone number with spaces using local format with country code' => [
|
|
|
57 |
'phonenumber' => '0 123 456 789',
|
|
|
58 |
'expected' => '+34123456789',
|
|
|
59 |
'countrycode' => '34',
|
|
|
60 |
],
|
|
|
61 |
'Phone number with spaces using local format without country code' => [
|
|
|
62 |
'phonenumber' => '0 123 456 789',
|
|
|
63 |
'expected' => '123456789',
|
|
|
64 |
],
|
|
|
65 |
];
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Test format number with different phones and different country codes
|
|
|
70 |
* @covers \factor_sms\helper::format_number
|
|
|
71 |
* @dataProvider format_number_provider
|
|
|
72 |
*
|
|
|
73 |
* @param string $phonenumber Phone number.
|
|
|
74 |
* @param string $expected Expected value.
|
|
|
75 |
* @param string|null $countrycode Country code.
|
|
|
76 |
*/
|
|
|
77 |
public function test_format_number(string $phonenumber, string $expected, ?string $countrycode = null): void {
|
|
|
78 |
|
|
|
79 |
$this->resetAfterTest(true);
|
|
|
80 |
|
|
|
81 |
set_config('countrycode', $countrycode, 'factor_sms');
|
|
|
82 |
|
|
|
83 |
$this->assertEquals($expected, \factor_sms\helper::format_number($phonenumber));
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Data provider for test_is_valid__phonenumber().
|
|
|
88 |
*
|
|
|
89 |
* @return array with different phone numebr tests
|
|
|
90 |
*/
|
|
|
91 |
public function is_valid_phonenumber_provider(): array {
|
|
|
92 |
return [
|
|
|
93 |
['+919367788755', true],
|
|
|
94 |
['8989829304', false],
|
|
|
95 |
['+16308520397', true],
|
|
|
96 |
['786-307-3615', false],
|
|
|
97 |
['+14155552671', true],
|
|
|
98 |
['+551155256325', true],
|
|
|
99 |
['649709233', false],
|
|
|
100 |
['+34649709233', true],
|
|
|
101 |
['+aaasss', false],
|
|
|
102 |
];
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Test is valid phone number in E.164 format (https://en.wikipedia.org/wiki/E.164)
|
|
|
107 |
* @covers \factor_sms\helper::is_valid_phonenumber
|
|
|
108 |
* @dataProvider is_valid_phonenumber_provider
|
|
|
109 |
*
|
|
|
110 |
* @param string $phonenumber
|
|
|
111 |
* @param bool $valid True if the given phone number is valid, false if is invalid
|
|
|
112 |
*/
|
|
|
113 |
public function test_is_valid_phonenumber(string $phonenumber, bool $valid): void {
|
|
|
114 |
$this->resetAfterTest(true);
|
|
|
115 |
if ($valid) {
|
|
|
116 |
$this->assertTrue(\factor_sms\helper::is_valid_phonenumber($phonenumber));
|
|
|
117 |
} else {
|
|
|
118 |
$this->assertFalse(\factor_sms\helper::is_valid_phonenumber($phonenumber));
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Test set up user factor and verification code with a random phone number
|
|
|
124 |
* @covers ::setup_user_factor
|
|
|
125 |
* @covers ::check_verification_code
|
|
|
126 |
* @covers ::revoke_user_factor
|
|
|
127 |
*/
|
|
|
128 |
public function test_check_verification_code(): void {
|
|
|
129 |
global $SESSION;
|
|
|
130 |
|
|
|
131 |
$this->resetAfterTest(true);
|
|
|
132 |
|
|
|
133 |
// Create and login a user and set up the phone number.
|
|
|
134 |
$user = $this->getDataGenerator()->create_user();
|
|
|
135 |
$this->setUser($user);
|
|
|
136 |
|
|
|
137 |
// Generate a fake phone number and save it in session.
|
|
|
138 |
$phonenumber = '+34' . (string)random_int(100000000, 999999999);
|
|
|
139 |
$SESSION->tool_mfa_sms_number = $phonenumber;
|
|
|
140 |
|
|
|
141 |
$smsfactor = \tool_mfa\plugininfo\factor::get_factor('sms');
|
|
|
142 |
$rc = new \ReflectionClass($smsfactor::class);
|
|
|
143 |
|
|
|
144 |
$smsdata = [];
|
|
|
145 |
$factorinstance = $smsfactor->setup_user_factor((object) $smsdata);
|
|
|
146 |
|
|
|
147 |
// Check if user factor was created successful.
|
|
|
148 |
$this->assertNotEmpty($factorinstance);
|
|
|
149 |
$this->assertEquals(1, count($smsfactor->get_active_user_factors($user)));
|
|
|
150 |
|
|
|
151 |
// Create the secret code.
|
|
|
152 |
$secretmanager = new \tool_mfa\local\secret_manager('sms');
|
|
|
153 |
$secretcode = $secretmanager->create_secret(1800, true);
|
|
|
154 |
|
|
|
155 |
// Check verification code.
|
|
|
156 |
$rcm = $rc->getMethod('check_verification_code');
|
|
|
157 |
$this->assertTrue($rcm->invoke($smsfactor, $secretcode));
|
|
|
158 |
|
|
|
159 |
// Test that calling the revoke on the generic type revokes all.
|
|
|
160 |
$smsfactor->revoke_user_factor($factorinstance->id);
|
|
|
161 |
$this->assertEquals(0, count($smsfactor->get_active_user_factors($user)));
|
|
|
162 |
|
|
|
163 |
unset($SESSION->tool_mfa_sms_number);
|
|
|
164 |
}
|
|
|
165 |
}
|