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 aiplacement_editor;
|
|
|
18 |
|
|
|
19 |
use core_ai\aiactions\generate_image;
|
|
|
20 |
use core_ai\aiactions\generate_text;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Text editor placement utils test.
|
|
|
24 |
*
|
|
|
25 |
* @package aiplacement_editor
|
|
|
26 |
* @copyright 2024 Huong Nguyen <huongnv13@gmail.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @covers \aiplacement_courseassist\utils
|
|
|
29 |
*/
|
|
|
30 |
final class utils_test extends \advanced_testcase {
|
|
|
31 |
/** @var array List of users. */
|
|
|
32 |
private array $users;
|
|
|
33 |
/** @var \stdClass Course object. */
|
|
|
34 |
private \stdClass $course;
|
|
|
35 |
/** @var \context_course Course context. */
|
|
|
36 |
private \context_course $context;
|
|
|
37 |
/** @var \stdClass Teacher role. */
|
|
|
38 |
private \stdClass $teacherrole;
|
|
|
39 |
|
|
|
40 |
public function setUp(): void {
|
|
|
41 |
global $DB;
|
|
|
42 |
parent::setUp();
|
|
|
43 |
|
|
|
44 |
$this->resetAfterTest();
|
|
|
45 |
$this->users[1] = $this->getDataGenerator()->create_user();
|
|
|
46 |
$this->users[2] = $this->getDataGenerator()->create_user();
|
|
|
47 |
$this->course = $this->getDataGenerator()->create_course();
|
|
|
48 |
$this->context = \context_course::instance($this->course->id);
|
|
|
49 |
$this->teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
|
|
|
50 |
$this->getDataGenerator()->enrol_user($this->users[1]->id, $this->course->id, 'manager');
|
|
|
51 |
$this->getDataGenerator()->enrol_user($this->users[2]->id, $this->course->id, 'editingteacher');
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Test is_html_editor_placement_action_available method.
|
|
|
56 |
*
|
|
|
57 |
* @param string $actionname Action name.
|
|
|
58 |
* @param string $actionclass Action class.
|
|
|
59 |
* @dataProvider html_editor_placement_action_available_provider
|
|
|
60 |
*/
|
|
|
61 |
public function test_is_html_editor_placement_action_available(
|
|
|
62 |
string $actionname,
|
|
|
63 |
string $actionclass,
|
|
|
64 |
): void {
|
|
|
65 |
// Provider is not enabled.
|
|
|
66 |
$this->setUser($this->users[1]);
|
|
|
67 |
$this->assertFalse(utils::is_html_editor_placement_action_available(
|
|
|
68 |
context: $this->context,
|
|
|
69 |
actionname: $actionname,
|
|
|
70 |
actionclass: $actionclass
|
|
|
71 |
));
|
|
|
72 |
|
|
|
73 |
// Plugin is not enabled.
|
|
|
74 |
$this->setUser($this->users[1]);
|
|
|
75 |
set_config('enabled', 0, 'aiplacement_editor');
|
|
|
76 |
$this->assertFalse(utils::is_html_editor_placement_action_available(
|
|
|
77 |
context: $this->context,
|
|
|
78 |
actionname: $actionname,
|
|
|
79 |
actionclass: $actionclass
|
|
|
80 |
));
|
|
|
81 |
|
|
|
82 |
// Plugin is enabled but user does not have capability.
|
|
|
83 |
assign_capability("aiplacement/editor:{$actionname}", CAP_PROHIBIT, $this->teacherrole->id, $this->context);
|
|
|
84 |
$this->setUser($this->users[2]);
|
|
|
85 |
set_config('enabled', 1, 'aiplacement_editor');
|
|
|
86 |
$this->assertFalse(utils::is_html_editor_placement_action_available(
|
|
|
87 |
context: $this->context,
|
|
|
88 |
actionname: $actionname,
|
|
|
89 |
actionclass: $actionclass
|
|
|
90 |
));
|
|
|
91 |
|
|
|
92 |
// Plugin is enabled, user has capability and placement action is not available.
|
|
|
93 |
$this->setUser($this->users[1]);
|
|
|
94 |
set_config($actionname, 0, 'aiplacement_editor');
|
|
|
95 |
$this->assertFalse(utils::is_html_editor_placement_action_available(
|
|
|
96 |
context: $this->context,
|
|
|
97 |
actionname: $actionname,
|
|
|
98 |
actionclass: $actionclass
|
|
|
99 |
));
|
|
|
100 |
|
|
|
101 |
// Plugin is enabled, user has capability and provider action is not available.
|
|
|
102 |
$this->setUser($this->users[1]);
|
|
|
103 |
set_config($actionname, 1, 'aiplacement_editor');
|
|
|
104 |
$this->assertFalse(utils::is_html_editor_placement_action_available(
|
|
|
105 |
context: $this->context,
|
|
|
106 |
actionname: $actionname,
|
|
|
107 |
actionclass: $actionclass
|
|
|
108 |
));
|
|
|
109 |
|
|
|
110 |
// Plugin is enabled, user has capability, placement action is available and provider action is available.
|
|
|
111 |
$mockmanager = $this->createMock(\core_ai\manager::class);
|
|
|
112 |
$mockmanager->method('is_action_available')->willReturn(true);
|
|
|
113 |
$mockmanager->method('is_action_enabled')->willReturn(true);
|
|
|
114 |
|
|
|
115 |
\core\di::set(\core_ai\manager::class, function() use ($mockmanager) {
|
|
|
116 |
return $mockmanager;
|
|
|
117 |
});
|
|
|
118 |
$this->setUser($this->users[1]);
|
|
|
119 |
$this->assertTrue(utils::is_html_editor_placement_action_available(
|
|
|
120 |
context: $this->context,
|
|
|
121 |
actionname: $actionname,
|
|
|
122 |
actionclass: $actionclass
|
|
|
123 |
));
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Data provider for {@see test_is_html_editor_placement_action_available}
|
|
|
128 |
*
|
|
|
129 |
* @return array
|
|
|
130 |
*/
|
|
|
131 |
public static function html_editor_placement_action_available_provider(): array {
|
|
|
132 |
return [
|
|
|
133 |
'Text generation' => [
|
|
|
134 |
'generate_text',
|
|
|
135 |
generate_text::class,
|
|
|
136 |
],
|
|
|
137 |
'Image generation' => [
|
|
|
138 |
'generate_image',
|
|
|
139 |
generate_image::class,
|
|
|
140 |
],
|
|
|
141 |
];
|
|
|
142 |
}
|
|
|
143 |
}
|