1441 |
ariadna |
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 core_sms;
|
|
|
18 |
|
|
|
19 |
use ValueError;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Tests for SMS Messages.
|
|
|
23 |
*
|
|
|
24 |
* @package core_sms
|
|
|
25 |
* @category test
|
|
|
26 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @covers \core_sms\message
|
|
|
29 |
*/
|
|
|
30 |
final class message_test extends \advanced_testcase {
|
|
|
31 |
public function test_create(): void {
|
|
|
32 |
$message = new message(
|
|
|
33 |
recipientnumber: '1234567890',
|
|
|
34 |
content: 'Hello, world!',
|
|
|
35 |
component: 'core',
|
|
|
36 |
messagetype: 'test',
|
|
|
37 |
recipientuserid: null,
|
|
|
38 |
issensitive: false,
|
|
|
39 |
);
|
|
|
40 |
|
|
|
41 |
$this->assertInstanceOf(message::class, $message);
|
|
|
42 |
$this->assertFalse($message->is_sent());
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function test_timecreated(): void {
|
|
|
46 |
$clock = $this->mock_clock_with_incrementing(55555);
|
|
|
47 |
|
|
|
48 |
$timecreated = 12345;
|
|
|
49 |
$message = new message(
|
|
|
50 |
recipientnumber: '1234567890',
|
|
|
51 |
content: 'Hello, world!',
|
|
|
52 |
component: 'core',
|
|
|
53 |
messagetype: 'test',
|
|
|
54 |
recipientuserid: null,
|
|
|
55 |
issensitive: false,
|
|
|
56 |
timecreated: $timecreated,
|
|
|
57 |
);
|
|
|
58 |
|
|
|
59 |
$this->assertEquals($timecreated, $message->timecreated);
|
|
|
60 |
|
|
|
61 |
$starttime = $clock->now();
|
|
|
62 |
$message = new message(
|
|
|
63 |
recipientnumber: '1234567890',
|
|
|
64 |
content: 'Hello, world!',
|
|
|
65 |
component: 'core',
|
|
|
66 |
messagetype: 'test',
|
|
|
67 |
recipientuserid: null,
|
|
|
68 |
issensitive: false,
|
|
|
69 |
);
|
|
|
70 |
|
|
|
71 |
$this->assertGreaterThan($starttime->getTimestamp(), $message->timecreated);
|
|
|
72 |
$this->assertLessThan($clock->now()->getTimestamp(), $message->timecreated);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public function test_id_not_updatable(): void {
|
|
|
76 |
$message = new message(
|
|
|
77 |
recipientnumber: '1234567890',
|
|
|
78 |
content: 'Hello, world!',
|
|
|
79 |
component: 'core',
|
|
|
80 |
messagetype: 'test',
|
|
|
81 |
recipientuserid: null,
|
|
|
82 |
issensitive: false,
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
$this->assertFalse(isset($message->id));
|
|
|
86 |
|
|
|
87 |
$message = $message->with(id: 123);
|
|
|
88 |
$this->assertEquals(123, $message->id);
|
|
|
89 |
|
|
|
90 |
$this->expectException(\coding_exception::class);
|
|
|
91 |
$message->with(id: 987);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
public function test_get_region_invalid(): void {
|
|
|
95 |
$message = new message(
|
|
|
96 |
recipientnumber: '1234567890',
|
|
|
97 |
content: 'Hello, world!',
|
|
|
98 |
component: 'core',
|
|
|
99 |
messagetype: 'test',
|
|
|
100 |
recipientuserid: null,
|
|
|
101 |
issensitive: false,
|
|
|
102 |
);
|
|
|
103 |
|
|
|
104 |
$this->expectException(ValueError::class);
|
|
|
105 |
$message->get_region();
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Test that get_region returns regions with valid numbers.
|
|
|
110 |
*
|
|
|
111 |
* @dataProvider get_region_provider
|
|
|
112 |
* @param string $recipientnumber
|
|
|
113 |
* @param string $expectedregion
|
|
|
114 |
*/
|
|
|
115 |
public function test_get_region_valid(
|
|
|
116 |
string $recipientnumber,
|
|
|
117 |
string $expectedregion,
|
|
|
118 |
): void {
|
|
|
119 |
$message = new message(
|
|
|
120 |
recipientnumber: $recipientnumber,
|
|
|
121 |
content: 'Hello, world!',
|
|
|
122 |
component: 'core',
|
|
|
123 |
messagetype: 'test',
|
|
|
124 |
recipientuserid: null,
|
|
|
125 |
issensitive: false,
|
|
|
126 |
);
|
|
|
127 |
|
|
|
128 |
$this->assertEquals($expectedregion, $message->get_region());
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Data provider for test_get_region_valid.
|
|
|
133 |
*
|
|
|
134 |
* @return array
|
|
|
135 |
*/
|
|
|
136 |
public static function get_region_provider(): array {
|
|
|
137 |
return [
|
|
|
138 |
// Authorised fictional numbers only.
|
|
|
139 |
// Australia: https://www.acma.gov.au/phone-numbers-use-tv-shows-films-and-creative-works.
|
|
|
140 |
['+61491570006', 'AU'],
|
|
|
141 |
|
|
|
142 |
// UK: https://www.ofcom.org.uk/phones-telecoms-and-internet/information-for-industry/numbering/numbers-for-drama.
|
|
|
143 |
['+447400123456', 'GB'],
|
|
|
144 |
];
|
|
|
145 |
}
|
|
|
146 |
}
|