Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_courseassist;
18
 
19
use core_ai\aiactions\generate_text;
20
use core_ai\aiactions\summarise_text;
21
use core_ai\manager;
22
 
23
/**
24
 * AI Placement course assist utils test.
25
 *
26
 * @package    aiplacement_courseassist
27
 * @copyright  2024 Huong Nguyen <huongnv13@gmail.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers     \aiplacement_courseassist\utils
30
 */
31
final class utils_test extends \advanced_testcase {
32
 
33
    /**
34
     * Test is_course_assist_available method.
35
     */
36
    public function test_is_course_assist_available(): void {
37
        global $DB;
38
        $this->resetAfterTest();
39
        $user1 = $this->getDataGenerator()->create_user();
40
        $user2 = $this->getDataGenerator()->create_user();
41
        $course = $this->getDataGenerator()->create_course();
42
        $context = \context_course::instance($course->id);
43
        $teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
44
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'manager');
45
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
46
 
47
        // Provider is not enabled.
48
        $this->setUser($user1);
49
        $this->assertFalse(utils::is_course_assist_available($context));
50
 
51
        // Provider is enabled, but plugin is not enabled.
52
        set_config('enabled', 1, 'aiprovider_openai');
53
        set_config('apikey', '123', 'aiprovider_openai');
54
        set_config('enabled', 0, 'aiplacement_courseassist');
55
        $this->assertFalse(utils::is_course_assist_available($context));
56
 
57
        // Plugin is enabled but user does not have capability.
58
        assign_capability('aiplacement/courseassist:summarise_text', CAP_PROHIBIT, $teacherrole->id, $context);
59
        assign_capability('aiplacement/courseassist:explain_text', CAP_PROHIBIT, $teacherrole->id, $context);
60
        $this->setUser($user2);
61
        set_config('enabled', 1, 'aiplacement_courseassist');
62
        $this->assertFalse(utils::is_course_assist_available($context));
63
 
64
        // Plugin is enabled, user has capability and placement action is not available.
65
        $this->setUser($user1);
66
        set_config('summarise_text', 0, 'aiplacement_courseassist');
67
        set_config('explain_text', 0, 'aiplacement_courseassist');
68
        $this->assertFalse(utils::is_course_assist_available($context));
69
 
70
        // Plugin is enabled, user has capability and provider action is not available.
71
        $this->setUser($user1);
72
        set_config('summarise_text', 0, 'aiprovider_openai');
73
        set_config('summarise_text', 1, 'aiplacement_courseassist');
74
        set_config('explain_text', 0, 'aiprovider_openai');
75
        set_config('explain_text', 1, 'aiplacement_courseassist');
76
        $this->assertFalse(utils::is_course_assist_available($context));
77
 
78
        // Plugin is enabled, user has capability, placement action is available and provider action is available.
79
        $mockmanager = $this->createMock(manager::class);
80
        $mockmanager->method('is_action_available')->willReturn(true);
81
        $mockmanager->method('is_action_enabled')->willReturn(true);
82
        $mockmanager->method('get_providers_for_actions')->willReturn([
83
            summarise_text::class => ['aiprovider_openai'],
84
        ]);
85
 
86
        \core\di::set(manager::class, function() use ($mockmanager) {
87
            return $mockmanager;
88
        });
89
 
90
        $this->setUser($user1);
91
        set_config('summarise_text', 1, 'aiplacement_courseassist');
92
        set_config('explain_text', 1, 'aiprovider_openai');
93
        set_config('explain_text', 1, 'aiplacement_courseassist');
94
        $this->assertTrue(utils::is_course_assist_available($context));
95
    }
96
 
97
    /**
98
     * Test get_actions_available method.
99
     */
100
    public function test_get_actions_available(): void {
101
        global $DB;
102
        $this->resetAfterTest();
103
        $user1 = $this->getDataGenerator()->create_user();
104
        $course = $this->getDataGenerator()->create_course();
105
        $context = \context_course::instance($course->id);
106
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'manager');
107
        $this->setUser($user1);
108
 
109
        // Two actions enabled.
110
        set_config('enabled', 1, 'aiprovider_openai');
111
        set_config('apikey', '123', 'aiprovider_openai');
112
        set_config('explain_text', 1, 'aiplacement_courseassist');
113
        set_config('summarise_text', 1, 'aiplacement_courseassist');
114
        $manager = \core\di::get(manager::class);
115
        $manager->create_provider_instance(
116
            classname: '\aiprovider_openai\provider',
117
            name: 'dummy',
118
            enabled: true,
119
            config: ['apikey' => '123'],
120
        );
121
        $this->assertCount(2, utils::get_actions_available($context));
122
 
123
        // One action enabled.
124
        set_config('summarise_text', 0, 'aiplacement_courseassist');
125
        $this->assertCount(1, utils::get_actions_available($context));
126
 
127
        // No actions enabled.
128
        set_config('explain_text', 0, 'aiplacement_courseassist');
129
        $this->assertCount(0, utils::get_actions_available($context));
130
    }
131
}