mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
manager_test.php
115 lines (96 loc) · 5.76 KB
/
manager_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace message_airnotifier;
use message_airnotifier_manager;
/**
* Unit tests for message_airnotifier_manager.
*
* @package message_airnotifier
* @category test
* @copyright 2020 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class manager_test extends \advanced_testcase {
/** Test check_configuration by default **/
public function test_check_configuration_default() {
global $CFG;
$this->resetAfterTest(true);
$manager = new message_airnotifier_manager();
// Mock server responses.
$CFG->airnotifierurl = 'localhost';
\curl::mock_response(json_encode(['error' => 'Invalid access key'])); // Mock request to check access key.
$checks = $manager->check_configuration();
$this->assertEquals(\core\check\result::OK, $checks[0]->get_status()); // Mobile service enabled.
$this->assertEquals(\core\check\result::OK, $checks[1]->get_status()); // Mobile notifications enabled.
$this->assertEquals(\core\check\result::ERROR, $checks[2]->get_status()); // Airnotifier NOT configured, missing key.
$this->assertEquals(\core\check\result::OK, $checks[3]->get_status()); // Airnotifier URL available.
$this->assertEquals(\core\check\result::ERROR, $checks[4]->get_status()); // Missing access key.
$this->assertEquals(\core\check\result::WARNING, $checks[5]->get_status()); // Only a few of default mobile notifications.
}
/** Test check_configuration with token **/
public function test_check_configuration_with_token() {
global $CFG;
$this->resetAfterTest(true);
$manager = new message_airnotifier_manager();
// Mock server responses.
$CFG->airnotifierurl = 'localhost';
\curl::mock_response(json_encode(['status' => 'ok'])); // Mock first request to check URL.
\curl::mock_response(json_encode(['error' => 'Invalid access key'])); // Mock second request to check acces key.
$CFG->airnotifieraccesskey = 'test'; // For enabling Airnotifier.
$checks = $manager->check_configuration();
$this->assertEquals(\core\check\result::OK, $checks[0]->get_status()); // Mobile service enabled.
$this->assertEquals(\core\check\result::OK, $checks[1]->get_status()); // Mobile notifications enabled.
$this->assertEquals(\core\check\result::OK, $checks[2]->get_status()); // Airnotifier configured.
$this->assertEquals(\core\check\result::OK, $checks[3]->get_status()); // Airnotifier URL available.
// The original function fourth check (access key valid in the remote Airnotifier server) is not mockable.
$this->assertEquals(\core\check\result::WARNING, $checks[4]->get_status()); // Only a few of default mobile notifications.
}
/** Test check_configuration bad settings **/
public function test_check_configuration_incorrect_settings() {
global $CFG;
$this->resetAfterTest(true);
$manager = new message_airnotifier_manager();
// Mock server responses.
$CFG->airnotifierurl = 'localhost';
\curl::mock_response(json_encode(['status' => 'ok'])); // Mock first request to check URL.
\curl::mock_response(json_encode(['error' => 'Invalid access key'])); // Mock second request to check acces key.
$CFG->airnotifieraccesskey = 'test'; // For enabling Airnotifier.
$CFG->airnotifierappname .= ' ';
$checks = $manager->check_configuration();
$this->assertEquals(\core\check\result::OK, $checks[0]->get_status()); // Mobile service enabled.
$this->assertEquals(\core\check\result::OK, $checks[1]->get_status()); // Mobile notifications enabled.
$this->assertEquals(\core\check\result::OK, $checks[2]->get_status()); // Airnotifier configured.
$this->assertEquals(\core\check\result::ERROR, $checks[3]->get_status()); // Airnotifier URL available.
$this->assertEquals(\core\check\result::OK, $checks[4]->get_status()); // Invalid setting (empty space).
// The original function fifth check (access key valid in the remote Airnotifier server) is not mockable.
$this->assertEquals(\core\check\result::WARNING, $checks[5]->get_status()); // Only a few of default mobile notifications.
}
/** Test has_enabled_devices **/
public function test_has_enabled_devices() {
global $CFG, $DB, $USER;
$this->resetAfterTest(true);
$CFG->airnotifieraccesskey = 'test'; // For mocking the request.
$manager = new message_airnotifier_manager();
// No devices yet for current user.
$this->assertFalse($manager->has_enabled_devices($CFG->airnotifiermobileappname));
// Add devices.
\curl::mock_response(json_encode(['status' => 'ok']));
$DB->insert_record('user_devices',
['userid' => $USER->id, 'appid' => $CFG->airnotifiermobileappname, 'platform' => 'ios',
'timecreated' => time(), 'timemodified' => time()]);
$this->assertTrue($manager->has_enabled_devices($CFG->airnotifiermobileappname));
}
}