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
declare(strict_types=1);
18
 
19
namespace tiny_aiplacement;
20
 
21
use advanced_testcase;
22
 
23
/**
24
 * Unit tests for the \tiny_aiplacement\plugininfo class.
25
 *
26
 * @package     tiny_aiplacement
27
 * @covers      \tiny_aiplacement\plugininfo::is_enabled_for_external
28
 * @covers      \tiny_aiplacement\plugininfo::get_plugin_configuration_for_external
29
 * @copyright   2025 Moodle Pty Ltd
30
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
final class plugininfo_test extends advanced_testcase {
33
 
34
    /**
35
     * Basic setup for tests.
36
     */
37
    public function setUp(): void {
38
        parent::setUp();
39
        $this->resetAfterTest(true);
40
 
41
        $aimanager = \core\di::get(\core_ai\manager::class);
42
        $aiprovider = $aimanager->create_provider_instance(
43
            classname: '\aiprovider_openai\provider',
44
            name: 'test_provider',
45
            enabled: true,
46
            config: ['apikey' => 'test_api_key'],
47
        );
48
        $aimanager->set_action_state(
49
            plugin: $aiprovider->provider,
50
            actionbasename: \core_ai\aiactions\generate_text::class::get_basename(),
51
            enabled: 1,
52
            instanceid: $aiprovider->id
53
        );
54
        $aimanager->set_action_state(
55
            plugin: $aiprovider->provider,
56
            actionbasename: \core_ai\aiactions\generate_image::class::get_basename(),
57
            enabled: 1,
58
            instanceid: $aiprovider->id
59
        );
60
    }
61
 
62
    /**
63
     * Test the is_enabled_for_external and get_plugin_configuration_for_external methods.
64
     *
65
     * @dataProvider for_external_provider
66
     * @param ?string $role Role name to assign to the user. If null, no role is assigned.
67
     * @param bool $enabled True if the aiplacement_editor must be enabled.
68
     * @param bool $expectedenabled Expected result for is_enabled_for_external.
69
     * @param array $expectedconfiguration Expected result for get_plugin_configuration_for_external.
70
     * @return void
71
     */
72
    public function test_for_external(?string $role, bool $enabled, bool $expectedenabled, array $expectedconfiguration): void {
73
        global $CFG;
74
 
75
        set_config('enabled', (int) $enabled, 'aiplacement_editor');
76
 
77
        $generator = $this->getDataGenerator();
78
        $user = $generator->create_user();
79
        $course = $generator->create_course();
80
        $context = \context_course::instance($course->id);
81
        if ($role) {
82
            $generator->enrol_user($user->id, $course->id, $role);
83
        }
84
        $this->setUser($user);
85
 
86
        $this->assertEquals($expectedenabled, plugininfo::is_enabled_for_external($context, ['pluginname' => 'aiplacement']));
87
        $this->assertEquals($expectedconfiguration, plugininfo::get_plugin_configuration_for_external($context));
88
    }
89
 
90
    /**
91
     * Data provider for test_for_external.
92
     *
93
     * @return array
94
     */
95
    public static function for_external_provider(): array {
96
        return [
97
            [
98
                'role' => null,
99
                'enabled' => true,
100
                'expectedenabled' => false,
101
                'expectedconfiguration' => [
102
                    'policyagreed' => '0',
103
                    'generate_text' => '0',
104
                    'generate_image' => '0',
105
                ],
106
            ],
107
            [
108
                'role' => 'guest',
109
                'enabled' => true,
110
                'expectedenabled' => false,
111
                'expectedconfiguration' => [
112
                    'policyagreed' => '0',
113
                    'generate_text' => '0',
114
                    'generate_image' => '0',
115
                ],
116
            ],
117
            [
118
                'role' => 'student',
119
                'enabled' => true,
120
                'expectedenabled' => true,
121
                'expectedconfiguration' => [
122
                    'policyagreed' => '0',
123
                    'generate_text' => '1',
124
                    'generate_image' => '1',
125
                ],
126
            ],
127
            [
128
                'role' => 'teacher',
129
                'enabled' => true,
130
                'expectedenabled' => true,
131
                'expectedconfiguration' => [
132
                    'policyagreed' => '0',
133
                    'generate_text' => '1',
134
                    'generate_image' => '1',
135
                ],
136
            ],
137
            [
138
                'role' => 'teacher',
139
                'enabled' => false,
140
                'expectedenabled' => false,
141
                'expectedconfiguration' => [
142
                    'policyagreed' => '0',
143
                    'generate_text' => '0',
144
                    'generate_image' => '0',
145
                ],
146
            ],
147
        ];
148
    }
149
}