MDL-68793 core_table: fix WS name to comply with guidelines
[moodle.git] / lib / table / tests / external / dynamic / get_test.php
CommitLineData
a020b020
SL
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/**
1179f07d 18 * Unit tests for core_table\external\dynamic\get;
a020b020
SL
19 *
20 * @package core_table
21 * @category test
22 * @copyright 2020 Simey Lameze <simey@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
24 */
25
26declare(strict_types = 1);
27
28namespace core_table\external\dynamic;
29
30use core_table\local\filter\filter;
31use advanced_testcase;
32
33/**
1179f07d 34 * Unit tests for core_table\external\dynamic\get;
a020b020
SL
35 *
36 * @package core_table
37 * @category test
38 * @copyright 2020 Simey Lameze <simey@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
1179f07d 41class get_test extends advanced_testcase {
a020b020
SL
42
43 /**
44 * Setup before class.
45 */
46 public static function setUpBeforeClass(): void {
47 global $CFG;
48 require_once("{$CFG->libdir}/externallib.php");
49 }
50
ad176d6f
SL
51 /**
52 * Test execute invalid component format.
53 */
54 public function test_execute_invalid_component_format(): void {
55 $this->resetAfterTest();
56
57 $this->expectException(\invalid_parameter_exception::class);
1179f07d 58 get::execute(
a31a2b6d
AN
59 "core-user",
60 "participants",
61 "",
62 $this->get_sort_array(['email' => SORT_ASC]),
63 [],
64 (string) filter::JOINTYPE_ANY,
65 null,
66 null,
67 null,
68 null,
69 [],
70 null
71
72 );
ad176d6f
SL
73 }
74
75 /**
76 * Test execute invalid component.
77 */
78 public function test_execute_invalid_component(): void {
79 $this->resetAfterTest();
80
81 $this->expectException(\UnexpectedValueException::class);
1179f07d 82 get::execute(
a31a2b6d
AN
83 "core_users",
84 "participants",
85 "",
86 $this->get_sort_array(['email' => SORT_ASC]),
87 [],
88 (string) filter::JOINTYPE_ANY,
89 null,
90 null,
91 null,
92 null,
93 [],
94 null
95 );
ad176d6f
SL
96 }
97
a020b020
SL
98 /**
99 * Test execute invalid handler.
100 */
101 public function test_execute_invalid_handler(): void {
102 $this->resetAfterTest();
103
104 $this->expectException('UnexpectedValueException');
4cf97b7f
SL
105 $handler = "\\core_user\\table\\users_participants_table";
106 $this->expectExceptionMessage("Table handler class {$handler} not found. Please make sure that your table handler class is under the \\core_user\\table namespace.");
a020b020
SL
107
108 // Tests that invalid users_participants_table class gets an exception.
1179f07d 109 get::execute(
a31a2b6d
AN
110 "core_user",
111 "users_participants_table",
112 "",
113 $this->get_sort_array(['email' => SORT_ASC]),
114 [],
115 (string) filter::JOINTYPE_ANY,
116 null,
117 null,
118 null,
119 null,
120 [],
121 null
122
123 );
a020b020
SL
124 }
125
126 /**
127 * Test execute invalid filter.
128 */
129 public function test_execute_invalid_filter(): void {
130 $this->resetAfterTest();
131
132 $course = $this->getDataGenerator()->create_course();
133
134 // Filter with an invalid name.
135 $filter = [
136 [
137 'fullname' => 'courseid',
138 'jointype' => filter::JOINTYPE_ANY,
a31a2b6d 139 'values' => [(int) $course->id]
a020b020
SL
140 ]
141 ];
ad176d6f 142 $this->expectException(\invalid_parameter_exception::class);
a020b020
SL
143 $this->expectExceptionMessage("Invalid parameter value detected (filters => Invalid parameter value detected " .
144 "(Missing required key in single structure: name): Missing required key in single structure: name");
145
1179f07d 146 get::execute(
a31a2b6d
AN
147 "core_user",
148 "participants", "user-index-participants-{$course->id}",
149 $this->get_sort_array(['firstname' => SORT_ASC]),
150 $filter,
151 (string) filter::JOINTYPE_ANY
152 );
a020b020
SL
153 }
154
155 /**
1179f07d 156 * Test execute method.
a020b020 157 */
1179f07d 158 public function test_table_get_execute(): void {
a020b020
SL
159 $this->resetAfterTest();
160
161 $course = $this->getDataGenerator()->create_course();
162 $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's1@moodle.com']);
163 $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's2@moodle.com']);
164 $user3 = $this->getDataGenerator()->create_user(['email' => 's3@moodle.com']);
165 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher', ['email' => 't1@moodle.com']);
166
167 $this->setUser($teacher);
168
a31a2b6d
AN
169 $this->get_sort_array(['email' => SORT_ASC]);
170
a020b020
SL
171 $filter = [
172 [
173 'name' => 'courseid',
174 'jointype' => filter::JOINTYPE_ANY,
a31a2b6d 175 'values' => [(int) $course->id]
a020b020
SL
176 ]
177 ];
178
1179f07d 179 $participantstable = get::execute(
a31a2b6d
AN
180 "core_user",
181 "participants",
182 "user-index-participants-{$course->id}",
183 $this->get_sort_array(['firstname' => SORT_ASC]),
184 $filter,
185 (string) filter::JOINTYPE_ANY,
186 null,
187 null,
188 null,
189 null,
190 [],
191 null
192 );
a020b020
SL
193 $html = $participantstable['html'];
194
195 $this->assertStringContainsString($user1->email, $html);
196 $this->assertStringContainsString($user2->email, $html);
197 $this->assertStringContainsString($teacher->email, $html);
198 $this->assertStringNotContainsString($user3->email, $html);
199 }
a31a2b6d
AN
200
201
202 /**
203 * Convert a traditional sort order into a sortorder for the web service.
204 *
205 * @param array $sortdata
206 * @return array
207 */
208 protected function get_sort_array(array $sortdata): array {
209 $newsortorder = [];
210 foreach ($sortdata as $sortby => $sortorder) {
211 $newsortorder[] = [
212 'sortby' => $sortby,
213 'sortorder' => $sortorder,
214 ];
215 }
216
217 return $newsortorder;
218 }
a020b020 219}