Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 message_airnotifier;
18
 
19
use message_airnotifier_manager;
20
 
21
/**
22
 * Unit tests for message_airnotifier_manager.
23
 *
24
 * @package     message_airnotifier
25
 * @category    test
26
 * @copyright   2020 Juan Leyva <juan@moodle.com>
27
 * @license     http://www.gnu.org/copyleft/gpl.html GNU Public License
28
 */
29
class manager_test extends \advanced_testcase {
30
 
31
    /** Test check_configuration by default **/
11 efrain 32
    public function test_check_configuration_default(): void {
1 efrain 33
        global $CFG;
34
        $this->resetAfterTest(true);
35
 
36
        $manager = new message_airnotifier_manager();
37
 
38
        // Mock server responses.
39
        $CFG->airnotifierurl = 'localhost';
40
        \curl::mock_response(json_encode(['error' => 'Invalid access key']));  // Mock request to check access key.
41
        $checks = $manager->check_configuration();
42
 
43
        $this->assertEquals(\core\check\result::OK, $checks[0]->get_status());   // Mobile service enabled.
44
        $this->assertEquals(\core\check\result::OK, $checks[1]->get_status());   // Message output not disabled in config.php.
45
        $this->assertEquals(\core\check\result::OK, $checks[2]->get_status());   // Mobile notifications enabled.
46
        $this->assertEquals(\core\check\result::ERROR, $checks[3]->get_status());    // Airnotifier NOT configured, missing key.
47
        $this->assertEquals(\core\check\result::OK, $checks[4]->get_status());   // Airnotifier URL available.
48
        $this->assertEquals(\core\check\result::ERROR, $checks[5]->get_status());    // Missing access key.
49
        $this->assertEquals(\core\check\result::ERROR, $checks[7]->get_status());  // No registered devices yet.
50
    }
51
 
52
    /** Test check_configuration with token **/
11 efrain 53
    public function test_check_configuration_with_token(): void {
1 efrain 54
        global $CFG;
55
        $this->resetAfterTest(true);
56
 
57
        $manager = new message_airnotifier_manager();
58
 
59
        // Mock server responses.
60
        $CFG->airnotifierurl = 'localhost';
61
        \curl::mock_response(json_encode(['status' => 'ok']));   // Mock first request to check URL.
62
        \curl::mock_response(json_encode(['error' => 'Invalid access key']));  // Mock second request to check acces key.
63
        $CFG->airnotifieraccesskey = 'test';    // For enabling Airnotifier.
64
        $checks = $manager->check_configuration();
65
 
66
        $this->assertEquals(\core\check\result::OK, $checks[0]->get_status());   // Mobile service enabled.
67
        $this->assertEquals(\core\check\result::OK, $checks[1]->get_status());   // Message output not disabled in config.php.
68
        $this->assertEquals(\core\check\result::OK, $checks[2]->get_status());   // Mobile notifications enabled.
69
        $this->assertEquals(\core\check\result::OK, $checks[3]->get_status());    // Airnotifier configured.
70
        $this->assertEquals(\core\check\result::OK, $checks[4]->get_status());   // Airnotifier URL available.
71
        // The original function fourth check (access key valid in the remote Airnotifier server) is not mockable.
72
        $this->assertEquals(\core\check\result::ERROR, $checks[6]->get_status());  // No registered devices yet.
73
    }
74
 
75
    /** Test check_configuration bad settings **/
11 efrain 76
    public function test_check_configuration_incorrect_settings(): void {
1 efrain 77
        global $CFG;
78
        $this->resetAfterTest(true);
79
 
80
        $manager = new message_airnotifier_manager();
81
 
82
        // Mock server responses.
83
        $CFG->airnotifierurl = 'localhost';
84
        \curl::mock_response(json_encode(['status' => 'ok']));   // Mock first request to check URL.
85
        \curl::mock_response(json_encode(['error' => 'Invalid access key']));  // Mock second request to check acces key.
86
        $CFG->airnotifieraccesskey = 'test';    // For enabling Airnotifier.
87
        $CFG->airnotifierappname .= ' ';
88
 
89
        $CFG->noemailever = true;
90
        $checks = $manager->check_configuration();
91
 
92
        $this->assertEquals(\core\check\result::OK, $checks[0]->get_status());   // Mobile service enabled.
93
        $this->assertEquals(\core\check\result::CRITICAL, $checks[1]->get_status());   // Message output disabled in config.php.
94
        $this->assertEquals(\core\check\result::OK, $checks[2]->get_status());   // Mobile notifications enabled.
95
        $this->assertEquals(\core\check\result::OK, $checks[3]->get_status());    // Airnotifier configured.
96
        $this->assertEquals(\core\check\result::ERROR, $checks[4]->get_status());   // Airnotifier URL available.
97
        $this->assertEquals(\core\check\result::OK, $checks[5]->get_status());   // Invalid setting (empty space).
98
        // The original function fifth check (access key valid in the remote Airnotifier server) is not mockable.
99
        $this->assertEquals(\core\check\result::ERROR, $checks[7]->get_status());  // No registered devices yet.
100
    }
101
 
102
    /** Test has_enabled_devices **/
11 efrain 103
    public function test_has_enabled_devices(): void {
1 efrain 104
        global $CFG, $DB, $USER;
105
        $this->resetAfterTest(true);
106
 
107
        $CFG->airnotifieraccesskey = 'test';    // For mocking the request.
108
        $manager = new message_airnotifier_manager();
109
 
110
        // No devices yet for current user.
111
        $this->assertFalse($manager->has_enabled_devices($CFG->airnotifiermobileappname));
112
 
113
        // Add devices.
114
        \curl::mock_response(json_encode(['status' => 'ok']));
115
        $DB->insert_record('user_devices',
116
            ['userid' => $USER->id, 'appid' => $CFG->airnotifiermobileappname, 'platform' => 'ios',
117
            'timecreated' => time(), 'timemodified' => time()]);
118
        $this->assertTrue($manager->has_enabled_devices($CFG->airnotifiermobileappname));
119
    }
120
}