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 $this->resetAfterTest(true); 58 $course = $this->getDataGenerator()->create_course(); 59 $student1 = $this->getDataGenerator()->create_and_enrol($course); 60 $chat = $this->getDataGenerator()->create_module('chat', ['course' => $course->id]); 61 $context = \context_module::instance($chat->cmid); 62 $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']); 63 assign_capability('mod/chat:readlog', CAP_ALLOW, $studentroleid, $context, true); 64 $this->setUser($student1); 65 66 return [ 67 'chat' => $chat, 68 ]; 69 } 70 71 /** 72 * Helper to call view_sessions WS function. 73 * 74 * @param int $cmid 75 * @param int $sessionstart 76 * @param int $sessionend 77 * @return array 78 */ 79 protected function view_sessions(int $cmid, int $sessionstart = 0, int $sessionend = 0): array { 80 $result = view_sessions::execute($cmid, $sessionstart, $sessionend); 81 return \core_external\external_api::clean_returnvalue(view_sessions::execute_returns(), $result); 82 } 83 84 /** 85 * Test for webservice view sessions. 86 * @covers ::execute 87 */ 88 public function test_view_sessions(): void { 89 $data = $this->prepare_test_data(); 90 $result = $this->view_sessions($data['chat']->cmid); 91 $this->assertArrayHasKey('status', $result); 92 $this->assertArrayHasKey('warnings', $result); 93 $this->assertTrue($result['status']); 94 } 95 96 /** 97 * Test for webservice view sessions without capability. 98 * @covers ::execute 99 */ 100 public function test_view_sessions_without_capability(): void { 101 global $DB; 102 $data = $this->prepare_test_data(); 103 $context = \context_module::instance($data['chat']->cmid); 104 $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']); 105 assign_capability('mod/chat:readlog', CAP_PROHIBIT, $studentroleid, $context, true); 106 $result = $this->view_sessions($data['chat']->cmid); 107 $this->assertArrayHasKey('status', $result); 108 $this->assertArrayHasKey('warnings', $result); 109 $this->assertFalse($result['status']); 110 $this->assertEquals(get_string('nopermissiontoseethechatlog', 'chat'), $result['warnings'][0]['message']); 111 } 112 113 /** 114 * Test for webservice view sessions with start and end dates. 115 * @covers ::execute 116 */ 117 public function test_view_sessions_with_start_end_dates(): void { 118 $data = $this->prepare_test_data(); 119 $result = $this->view_sessions($data['chat']->cmid, strtotime('today'), strtotime('tomorrow')); 120 $this->assertArrayHasKey('status', $result); 121 $this->assertArrayHasKey('warnings', $result); 122 $this->assertTrue($result['status']); 123 } 124 125 /** 126 * Test execute with no valid instance of cmid. 127 * @covers ::execute 128 */ 129 public function test_view_sessions_no_instance(): void { 130 $this->expectException(moodle_exception::class); 131 $this->view_sessions(1234); 132 } 133 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body