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 |
/**
|
|
|
18 |
* Tests for external function mod_chat_view_sessions.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_chat
|
|
|
21 |
* @category external
|
|
|
22 |
* @copyright 2022 Rodrigo Mady <rodrigo.mady@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @since Moodle 4.3
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
namespace mod_chat\external;
|
|
|
28 |
|
|
|
29 |
use externallib_advanced_testcase;
|
|
|
30 |
use moodle_exception;
|
|
|
31 |
|
|
|
32 |
defined('MOODLE_INTERNAL') || die();
|
|
|
33 |
|
|
|
34 |
global $CFG;
|
|
|
35 |
|
|
|
36 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Test Class for external function mod_chat_view_sessions.
|
|
|
40 |
*
|
|
|
41 |
* @package mod_chat
|
|
|
42 |
* @category external
|
|
|
43 |
* @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
|
|
|
44 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
45 |
* @since Moodle 4.3
|
|
|
46 |
* @coversDefaultClass \mod_chat\external\view_sessions
|
|
|
47 |
*/
|
|
|
48 |
class view_sessions_test extends externallib_advanced_testcase {
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Prepare the test.
|
|
|
52 |
*
|
|
|
53 |
* @return array
|
|
|
54 |
*/
|
|
|
55 |
private function prepare_test_data(): array {
|
|
|
56 |
global $DB;
|
|
|
57 |
|
|
|
58 |
$this->resetAfterTest(true);
|
|
|
59 |
|
|
|
60 |
// Chat module is disabled by default, enable it for testing.
|
|
|
61 |
$manager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
|
|
62 |
$manager::enable_plugin('chat', 1);
|
|
|
63 |
|
|
|
64 |
$course = $this->getDataGenerator()->create_course();
|
|
|
65 |
$student1 = $this->getDataGenerator()->create_and_enrol($course);
|
|
|
66 |
$chat = $this->getDataGenerator()->create_module('chat', ['course' => $course->id]);
|
|
|
67 |
$context = \context_module::instance($chat->cmid);
|
|
|
68 |
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
|
|
|
69 |
assign_capability('mod/chat:readlog', CAP_ALLOW, $studentroleid, $context, true);
|
|
|
70 |
$this->setUser($student1);
|
|
|
71 |
|
|
|
72 |
return [
|
|
|
73 |
'chat' => $chat,
|
|
|
74 |
];
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Helper to call view_sessions WS function.
|
|
|
79 |
*
|
|
|
80 |
* @param int $cmid
|
|
|
81 |
* @param int $sessionstart
|
|
|
82 |
* @param int $sessionend
|
|
|
83 |
* @return array
|
|
|
84 |
*/
|
|
|
85 |
protected function view_sessions(int $cmid, int $sessionstart = 0, int $sessionend = 0): array {
|
|
|
86 |
$result = view_sessions::execute($cmid, $sessionstart, $sessionend);
|
|
|
87 |
return \core_external\external_api::clean_returnvalue(view_sessions::execute_returns(), $result);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Test for webservice view sessions.
|
|
|
92 |
* @covers ::execute
|
|
|
93 |
*/
|
|
|
94 |
public function test_view_sessions(): void {
|
|
|
95 |
$data = $this->prepare_test_data();
|
|
|
96 |
$result = $this->view_sessions($data['chat']->cmid);
|
|
|
97 |
$this->assertArrayHasKey('status', $result);
|
|
|
98 |
$this->assertArrayHasKey('warnings', $result);
|
|
|
99 |
$this->assertTrue($result['status']);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Test for webservice view sessions without capability.
|
|
|
104 |
* @covers ::execute
|
|
|
105 |
*/
|
|
|
106 |
public function test_view_sessions_without_capability(): void {
|
|
|
107 |
global $DB;
|
|
|
108 |
$data = $this->prepare_test_data();
|
|
|
109 |
$context = \context_module::instance($data['chat']->cmid);
|
|
|
110 |
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
|
|
|
111 |
assign_capability('mod/chat:readlog', CAP_PROHIBIT, $studentroleid, $context, true);
|
|
|
112 |
$result = $this->view_sessions($data['chat']->cmid);
|
|
|
113 |
$this->assertArrayHasKey('status', $result);
|
|
|
114 |
$this->assertArrayHasKey('warnings', $result);
|
|
|
115 |
$this->assertFalse($result['status']);
|
|
|
116 |
$this->assertEquals(get_string('nopermissiontoseethechatlog', 'chat'), $result['warnings'][0]['message']);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Test for webservice view sessions with start and end dates.
|
|
|
121 |
* @covers ::execute
|
|
|
122 |
*/
|
|
|
123 |
public function test_view_sessions_with_start_end_dates(): void {
|
|
|
124 |
$data = $this->prepare_test_data();
|
|
|
125 |
$result = $this->view_sessions($data['chat']->cmid, strtotime('today'), strtotime('tomorrow'));
|
|
|
126 |
$this->assertArrayHasKey('status', $result);
|
|
|
127 |
$this->assertArrayHasKey('warnings', $result);
|
|
|
128 |
$this->assertTrue($result['status']);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Test execute with no valid instance of cmid.
|
|
|
133 |
* @covers ::execute
|
|
|
134 |
*/
|
|
|
135 |
public function test_view_sessions_no_instance(): void {
|
|
|
136 |
$this->expectException(moodle_exception::class);
|
|
|
137 |
$this->view_sessions(1234);
|
|
|
138 |
}
|
|
|
139 |
}
|