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 core_ai\external;
18
 
19
/**
20
 * Test policy external api calls.
21
 *
22
 * @package    core_ai
23
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @covers     \core_ai\external\set_policy_status
26
 * @covers     \core_ai\external\get_policy_status
27
 */
28
final class policy_test extends \advanced_testcase {
29
    /**
30
     * Test get policy failure.
31
     */
32
    public function test_get_policy_failure(): void {
33
        $this->resetAfterTest();
34
 
35
        // Create a test user.
36
        $user = $this->getDataGenerator()->create_user();
37
        $this->setUser($user->id);
38
 
39
        $_POST['sesskey'] = sesskey();
40
        $params = [
41
            'userid' => $user->id,
42
        ];
43
 
44
        $result = \core_external\external_api::call_external_function(
45
            'core_ai_get_policy_status',
46
            $params,
47
        );
48
 
49
        $this->assertFalse($result['error']);
50
        $this->assertFalse($result['data']['status']);
51
    }
52
 
53
    /**
54
     * Test get policy success.
55
     */
56
    public function test_get_policy_success(): void {
57
        $this->resetAfterTest();
58
        global $DB;
59
 
60
        // Create a test user.
61
        $user = $this->getDataGenerator()->create_user();
62
        $this->setUser($user->id);
63
 
64
        // Get system context.
65
        $context = \core\context\system::instance();
66
 
67
        // Manually add record to the database.
68
        $record = new \stdClass();
69
        $record->userid = $user->id;
70
        $record->contextid = $context->id;
71
        $record->timeaccepted = time();
72
 
73
        $DB->insert_record('ai_policy_register', $record);
74
 
75
        $_POST['sesskey'] = sesskey();
76
        $params = [
77
            'userid' => $user->id,
78
        ];
79
 
80
        $result = \core_external\external_api::call_external_function(
81
            'core_ai_get_policy_status',
82
            $params
83
        );
84
 
85
        $this->assertFalse($result['error']);
86
        $this->assertTrue($result['data']['status']);
87
    }
88
 
89
    /**
90
     * Test set policy.
91
     */
92
    public function test_set_policy(): void {
93
        $this->resetAfterTest();
94
 
95
        // Create a test user.
96
        $user = $this->getDataGenerator()->create_user();
97
        $this->setUser($user->id);
98
 
99
        // Get system context.
100
        $context = \context_system::instance();
101
 
102
        $_POST['sesskey'] = sesskey();
103
        $params = [
104
            'contextid' => $context->id,
105
        ];
106
 
107
        $result = \core_external\external_api::call_external_function(
108
            'core_ai_set_policy_status',
109
            $params
110
        );
111
 
112
        $this->assertFalse($result['error']);
113
        $this->assertTrue($result['data']['success']);
114
    }
115
 
116
    /**
117
     * Test load_many_for_cache.
118
     */
119
    public function test_user_policy_caching(): void {
120
        global $DB;
121
        $this->resetAfterTest();
122
 
123
        $user1 = $this->getDataGenerator()->create_user();
124
        $user2 = $this->getDataGenerator()->create_user();
125
 
126
        // Manually add records to the database.
127
        $record1 = new \stdClass();
128
        $record1->userid = $user1->id;
129
        $record1->contextid = 1;
130
        $record1->timeaccepted = time();
131
 
132
        $record2 = new \stdClass();
133
        $record2->userid = $user2->id;
134
        $record2->contextid = 1;
135
        $record2->timeaccepted = time();
136
 
137
        $DB->insert_records('ai_policy_register', [
138
            $record1,
139
            $record2,
140
        ]);
141
 
142
        $policycache = \cache::make('core', 'ai_policy');
143
        $this->assertNotEmpty($policycache->get_many([$user1->id, $user2->id]));
144
    }
145
 
146
}