e09b602204921257bf806e052a7415f6e3eb5560
[moodle.git] / mod / glossary / tests / external_test.php
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/>.
17 /**
18  * External glossary functions unit tests
19  *
20  * @package    mod_glossary
21  * @category   external
22  * @copyright  2015 Costantino Cito <ccito@cvaconsulting.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
30 /**
31  * External glossary functions unit tests
32  *
33  * @package    mod_glossary
34  * @category   external
35  * @copyright  2015 Costantino Cito <ccito@cvaconsulting.com>
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class mod_glossary_external_testcase extends externallib_advanced_testcase {
40     /**
41      * Test get_glossaries_by_courses
42      */
43     public function test_get_glossaries_by_courses() {
44         $this->resetAfterTest(true);
46         // As admin.
47         $this->setAdminUser();
48         $c1 = self::getDataGenerator()->create_course();
49         $c2 = self::getDataGenerator()->create_course();
50         $g1 = self::getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'name' => 'First Glossary'));
51         $g2 = self::getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'name' => 'Second Glossary'));
52         $g3 = self::getDataGenerator()->create_module('glossary', array('course' => $c2->id, 'name' => 'Third Glossary'));
54         $s1 = $this->getDataGenerator()->create_user();
55         self::getDataGenerator()->enrol_user($s1->id,  $c1->id);
57         // Check results where student is enrolled.
58         $this->setUser($s1);
59         $glossaries = mod_glossary_external::get_glossaries_by_courses(array());
60         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
62         $this->assertCount(2, $glossaries['glossaries']);
63         $this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']);
64         $this->assertEquals('Second Glossary', $glossaries['glossaries'][1]['name']);
66         // Check results with specific course IDs.
67         $glossaries = mod_glossary_external::get_glossaries_by_courses(array($c1->id, $c2->id));
68         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
70         $this->assertCount(2, $glossaries['glossaries']);
71         $this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']);
72         $this->assertEquals('Second Glossary', $glossaries['glossaries'][1]['name']);
74         $this->assertEquals('course', $glossaries['warnings'][0]['item']);
75         $this->assertEquals($c2->id, $glossaries['warnings'][0]['itemid']);
76         $this->assertEquals('1', $glossaries['warnings'][0]['warningcode']);
78         // Now as admin.
79         $this->setAdminUser();
81         $glossaries = mod_glossary_external::get_glossaries_by_courses(array($c2->id));
82         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
84         $this->assertCount(1, $glossaries['glossaries']);
85         $this->assertEquals('Third Glossary', $glossaries['glossaries'][0]['name']);
86     }
88     public function test_view_glossary() {
89         $this->resetAfterTest(true);
91         // Generate all the things.
92         $c1 = $this->getDataGenerator()->create_course();
93         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
94         $u1 = $this->getDataGenerator()->create_user();
95         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
97         $sink = $this->redirectEvents();
98         $this->setUser($u1);
99         $return = mod_glossary_external::view_glossary($g1->id, 'letter');
100         $return = external_api::clean_returnvalue(mod_glossary_external::view_glossary_returns(), $return);
101         $events = $sink->get_events();
103         // Assertion.
104         $this->assertTrue($return['status']);
105         $this->assertEmpty($return['warnings']);
106         $this->assertCount(1, $events);
107         $this->assertEquals('\mod_glossary\event\course_module_viewed', $events[0]->eventname);
108         $sink->close();
109     }
111     public function test_view_glossary_without_permission() {
112         $this->resetAfterTest(true);
114         // Generate all the things.
115         $c1 = $this->getDataGenerator()->create_course();
116         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
117         $u1 = $this->getDataGenerator()->create_user();
118         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
119         $ctx = context_module::instance($g1->cmid);
121         // Revoke permission.
122         $roles = get_archetype_roles('user');
123         $role = array_shift($roles);
124         assign_capability('mod/glossary:view', CAP_PROHIBIT, $role->id, $ctx, true);
125         accesslib_clear_all_caches_for_unit_testing();
127         // Assertion.
128         $this->setUser($u1);
129         $this->setExpectedException('require_login_exception', 'Activity is hidden');
130         mod_glossary_external::view_glossary($g1->id, 'letter');
131     }
133     public function test_view_entry() {
134         $this->resetAfterTest(true);
136         // Generate all the things.
137         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
138         $c1 = $this->getDataGenerator()->create_course();
139         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
140         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'visible' => false));
141         $u1 = $this->getDataGenerator()->create_user();
142         $e1 = $gg->create_content($g1, array('approved' => 1));
143         $e2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
144         $e3 = $gg->create_content($g1, array('approved' => 0, 'userid' => -1));
145         $e4 = $gg->create_content($g2, array('approved' => 1));
146         $ctx = context_module::instance($g1->cmid);
147         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
148         $this->setUser($u1);
150         // Test readable entry.
151         $sink = $this->redirectEvents();
152         $return = mod_glossary_external::view_entry($e1->id);
153         $return = external_api::clean_returnvalue(mod_glossary_external::view_entry_returns(), $return);
154         $events = $sink->get_events();
155         $this->assertTrue($return['status']);
156         $this->assertEmpty($return['warnings']);
157         $this->assertCount(1, $events);
158         $this->assertEquals('\mod_glossary\event\entry_viewed', $events[0]->eventname);
159         $sink->close();
161         // Test non-approved of self.
162         $return = mod_glossary_external::view_entry($e2->id);
163         $return = external_api::clean_returnvalue(mod_glossary_external::view_entry_returns(), $return);
164         $events = $sink->get_events();
165         $this->assertTrue($return['status']);
166         $this->assertEmpty($return['warnings']);
167         $this->assertCount(1, $events);
168         $this->assertEquals('\mod_glossary\event\entry_viewed', $events[0]->eventname);
169         $sink->close();
171         // Test non-approved of other.
172         try {
173             mod_glossary_external::view_entry($e3->id);
174             $this->fail('Cannot view non-approved entries of others.');
175         } catch (invalid_parameter_exception $e) {
176         }
178         // Test non-readable entry.
179         $this->setExpectedException('require_login_exception', 'Activity is hidden');
180         mod_glossary_external::view_entry($e4->id);
181     }
183     public function test_get_entries_by_letter() {
184         $this->resetAfterTest(true);
186         // Generate all the things.
187         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
188         $c1 = $this->getDataGenerator()->create_course();
189         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
190         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
191         $u1 = $this->getDataGenerator()->create_user();
192         $ctx = context_module::instance($g1->cmid);
193         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
195         $e1a = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Bob', 'userid' => 2));
196         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Jane', 'userid' => 2));
197         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Alice', 'userid' => $u1->id));
198         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id));
199         $e2a = $gg->create_content($g2);
201         $this->setAdminUser();
203         // Just a normal request from admin user.
204         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array());
205         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
206         $this->assertCount(3, $return['entries']);
207         $this->assertEquals(3, $return['count']);
208         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
209         $this->assertEquals($e1a->id, $return['entries'][1]['id']);
210         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
212         // An admin user requesting all the entries.
213         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array('includenotapproved' => 1));
214         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
215         $this->assertCount(4, $return['entries']);
216         $this->assertEquals(4, $return['count']);
217         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
218         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
219         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
220         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
222         // A normal user.
223         $this->setUser($u1);
224         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array());
225         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
226         $this->assertCount(3, $return['entries']);
227         $this->assertEquals(3, $return['count']);
228         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
229         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
230         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
232         // A normal user requesting to view all non approved entries.
233         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array('includenotapproved' => 1));
234         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
235         $this->assertCount(3, $return['entries']);
236         $this->assertEquals(3, $return['count']);
237         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
238         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
239         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
240     }
242     public function test_get_entries_by_letter_with_parameters() {
243         $this->resetAfterTest(true);
245         // Generate all the things.
246         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
247         $c1 = $this->getDataGenerator()->create_course();
248         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
249         $u1 = $this->getDataGenerator()->create_user();
250         $ctx = context_module::instance($g1->cmid);
251         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
253         $e1a = $gg->create_content($g1, array('approved' => 1, 'concept' => '0-day', 'userid' => $u1->id));
254         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Bob', 'userid' => 2));
255         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => '1-dayb', 'userid' => $u1->id));
257         $this->setUser($u1);
259         // Requesting a single letter.
260         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'b', 0, 20);
261         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
262         $this->assertCount(1, $return['entries']);
263         $this->assertEquals(1, $return['count']);
264         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
266         // Requesting special letters.
267         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'SPECIAL', 0, 20);
268         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
269         $this->assertCount(2, $return['entries']);
270         $this->assertEquals(2, $return['count']);
271         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
272         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
274         // Requesting with limit.
275         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 1);
276         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
277         $this->assertCount(1, $return['entries']);
278         $this->assertEquals(3, $return['count']);
279         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
280         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 1, 2);
281         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
282         $this->assertCount(2, $return['entries']);
283         $this->assertEquals(3, $return['count']);
284         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
285         $this->assertEquals($e1b->id, $return['entries'][1]['id']);
286     }
288     public function test_get_entries_by_date() {
289         global $DB;
290         $this->resetAfterTest(true);
292         // Generate all the things.
293         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
294         $c1 = $this->getDataGenerator()->create_course();
295         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
296         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
297         $u1 = $this->getDataGenerator()->create_user();
298         $ctx = context_module::instance($g1->cmid);
299         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
301         $now = time();
302         $e1a = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Bob', 'userid' => $u1->id,
303             'timecreated' => 1, 'timemodified' => $now + 3600));
304         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Jane', 'userid' => $u1->id,
305             'timecreated' => $now + 3600, 'timemodified' => 1));
306         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Alice', 'userid' => $u1->id,
307             'timecreated' => $now + 1, 'timemodified' => $now + 1));
308         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id,
309             'timecreated' => $now + 2, 'timemodified' => $now + 2));
310         $e2a = $gg->create_content($g2);
312         $this->setAdminUser($u1);
314         // Ordering by time modified descending.
315         $return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'DESC', 0, 20, array());
316         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
317         $this->assertCount(3, $return['entries']);
318         $this->assertEquals(3, $return['count']);
319         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
320         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
321         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
323         // Ordering by time modified ascending.
324         $return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'ASC', 0, 20, array());
325         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
326         $this->assertCount(3, $return['entries']);
327         $this->assertEquals(3, $return['count']);
328         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
329         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
330         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
332         // Ordering by time created asc.
333         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20, array());
334         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
335         $this->assertCount(3, $return['entries']);
336         $this->assertEquals(3, $return['count']);
337         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
338         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
339         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
341         // Ordering by time created descending.
342         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'DESC', 0, 20, array());
343         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
344         $this->assertCount(3, $return['entries']);
345         $this->assertEquals(3, $return['count']);
346         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
347         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
348         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
350         // Ordering including to approve.
351         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20,
352             array('includenotapproved' => true));
353         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
354         $this->assertCount(4, $return['entries']);
355         $this->assertEquals(4, $return['count']);
356         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
357         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
358         $this->assertEquals($e1d->id, $return['entries'][2]['id']);
359         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
361         // Ordering including to approve and pagination.
362         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 2,
363             array('includenotapproved' => true));
364         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
365         $this->assertCount(2, $return['entries']);
366         $this->assertEquals(4, $return['count']);
367         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
368         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
369         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 2, 2,
370             array('includenotapproved' => true));
371         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
372         $this->assertCount(2, $return['entries']);
373         $this->assertEquals(4, $return['count']);
374         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
375         $this->assertEquals($e1b->id, $return['entries'][1]['id']);
376     }
378     public function test_get_categories() {
379         $this->resetAfterTest(true);
380         $this->setAdminUser();
382         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
383         $c1 = $this->getDataGenerator()->create_course();
384         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
385         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
386         $cat1a = $gg->create_category($g1);
387         $cat1b = $gg->create_category($g1);
388         $cat1c = $gg->create_category($g1);
389         $cat2a = $gg->create_category($g2);
391         $return = mod_glossary_external::get_categories($g1->id, 0, 20);
392         $return = external_api::clean_returnvalue(mod_glossary_external::get_categories_returns(), $return);
393         $this->assertCount(3, $return['categories']);
394         $this->assertEquals(3, $return['count']);
395         $this->assertEquals($cat1a->id, $return['categories'][0]['id']);
396         $this->assertEquals($cat1b->id, $return['categories'][1]['id']);
397         $this->assertEquals($cat1c->id, $return['categories'][2]['id']);
399         $return = mod_glossary_external::get_categories($g1->id, 1, 2);
400         $return = external_api::clean_returnvalue(mod_glossary_external::get_categories_returns(), $return);
401         $this->assertCount(2, $return['categories']);
402         $this->assertEquals(3, $return['count']);
403         $this->assertEquals($cat1b->id, $return['categories'][0]['id']);
404         $this->assertEquals($cat1c->id, $return['categories'][1]['id']);
405     }
407     public function test_get_entries_by_category() {
408         $this->resetAfterTest(true);
410         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
411         $c1 = $this->getDataGenerator()->create_course();
412         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
413         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
414         $u1 = $this->getDataGenerator()->create_user();
415         $ctx = context_module::instance($g1->cmid);
417         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
418         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
419         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
420         $e1b1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
421         $e1b2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
422         $e1x1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
423         $e1x2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
424         $e2a1 = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
425         $e2a2 = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
427         $cat1a = $gg->create_category($g1, array('name' => 'Fish'), array($e1a1, $e1a2, $e1a3));
428         $cat1b = $gg->create_category($g1, array('name' => 'Cat'), array($e1b1, $e1b2));
429         $cat1c = $gg->create_category($g1, array('name' => 'Zebra'), array($e1b1));   // $e1b1 is in two categories.
430         $cat2a = $gg->create_category($g2, array(), array($e2a1, $e2a2));
432         $this->setAdminUser();
434         // Browse one category.
435         $return = mod_glossary_external::get_entries_by_category($g1->id, $cat1a->id, 0, 20, array());
436         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
437         $this->assertCount(3, $return['entries']);
438         $this->assertEquals(3, $return['count']);
439         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
440         $this->assertEquals($e1a2->id, $return['entries'][1]['id']);
441         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
443         // Browse all categories.
444         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 0, 20, array());
445         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
446         $this->assertCount(5, $return['entries']);
447         $this->assertEquals(5, $return['count']);
448         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
449         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
450         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
451         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
452         $this->assertEquals($e1b1->id, $return['entries'][4]['id']);
454         // Browse uncategorised.
455         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_NOT_CATEGORISED, 0, 20, array());
456         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
457         $this->assertCount(1, $return['entries']);
458         $this->assertEquals(1, $return['count']);
459         $this->assertEquals($e1x1->id, $return['entries'][0]['id']);
461         // Including to approve.
462         $return = mod_glossary_external::get_entries_by_category($g1->id, $cat1b->id, 0, 20,
463             array('includenotapproved' => true));
464         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
465         $this->assertCount(2, $return['entries']);
466         $this->assertEquals(2, $return['count']);
467         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
468         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
470         // Using limit.
471         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 0, 3,
472             array('includenotapproved' => true));
473         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
474         $this->assertCount(3, $return['entries']);
475         $this->assertEquals(6, $return['count']);
476         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
477         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
478         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
479         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 3, 2,
480             array('includenotapproved' => true));
481         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
482         $this->assertCount(2, $return['entries']);
483         $this->assertEquals(6, $return['count']);
484         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
485         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
486     }
488     public function test_get_authors() {
489         $this->resetAfterTest(true);
491         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
492         $c1 = $this->getDataGenerator()->create_course();
493         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
494         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
496         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon'));
497         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha'));
498         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Omega'));
500         $ctx = context_module::instance($g1->cmid);
502         $e1a = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
503         $e1b = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
504         $e1c = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
505         $e2a = $gg->create_content($g1, array('userid' => $u2->id, 'approved' => 1));
506         $e3a = $gg->create_content($g1, array('userid' => $u3->id, 'approved' => 0));
508         $this->setAdminUser();
510         // Simple request.
511         $return = mod_glossary_external::get_authors($g1->id, 0, 20, array());
512         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
513         $this->assertCount(2, $return['authors']);
514         $this->assertEquals(2, $return['count']);
515         $this->assertEquals($u2->id, $return['authors'][0]['id']);
516         $this->assertEquals($u1->id, $return['authors'][1]['id']);
518         // Include users with entries pending approval.
519         $return = mod_glossary_external::get_authors($g1->id, 0, 20, array('includenotapproved' => true));
520         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
521         $this->assertCount(3, $return['authors']);
522         $this->assertEquals(3, $return['count']);
523         $this->assertEquals($u2->id, $return['authors'][0]['id']);
524         $this->assertEquals($u3->id, $return['authors'][1]['id']);
525         $this->assertEquals($u1->id, $return['authors'][2]['id']);
527         // Pagination.
528         $return = mod_glossary_external::get_authors($g1->id, 1, 1, array('includenotapproved' => true));
529         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
530         $this->assertCount(1, $return['authors']);
531         $this->assertEquals(3, $return['count']);
532         $this->assertEquals($u3->id, $return['authors'][0]['id']);
533     }
535     public function test_get_entries_by_author() {
536         $this->resetAfterTest(true);
538         // Generate all the things.
539         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
540         $c1 = $this->getDataGenerator()->create_course();
541         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
542         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
543         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon', 'firstname' => 'Zac'));
544         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Ultra', 'firstname' => '1337'));
545         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha', 'firstname' => 'Omega'));
546         $u4 = $this->getDataGenerator()->create_user(array('lastname' => '0-day', 'firstname' => 'Zoe'));
547         $ctx = context_module::instance($g1->cmid);
548         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
550         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
551         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
552         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
553         $e1b1 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
554         $e1b2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u2->id));
555         $e1c1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u3->id));
556         $e1d1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u4->id));
557         $e2a = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
559         $this->setUser($u1);
561         // Requesting a single letter.
562         $return = mod_glossary_external::get_entries_by_author($g1->id, 'u', 'LASTNAME', 'ASC', 0, 20);
563         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
564         $this->assertCount(4, $return['entries']);
565         $this->assertEquals(4, $return['count']);
566         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
567         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
568         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
569         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
571         // Requesting special letters.
572         $return = mod_glossary_external::get_entries_by_author($g1->id, 'SPECIAL', 'LASTNAME', 'ASC', 0, 20);
573         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
574         $this->assertCount(1, $return['entries']);
575         $this->assertEquals(1, $return['count']);
576         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
578         // Requesting with limit.
579         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 0, 1);
580         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
581         $this->assertCount(1, $return['entries']);
582         $this->assertEquals(6, $return['count']);
583         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
584         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 1, 2);
585         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
586         $this->assertCount(2, $return['entries']);
587         $this->assertEquals(6, $return['count']);
588         $this->assertEquals($e1c1->id, $return['entries'][0]['id']);
589         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
591         // Including non-approved.
592         $this->setAdminUser();
593         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 0, 20,
594             array('includenotapproved' => true));
595         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
596         $this->assertCount(7, $return['entries']);
597         $this->assertEquals(7, $return['count']);
598         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
599         $this->assertEquals($e1c1->id, $return['entries'][1]['id']);
600         $this->assertEquals($e1b1->id, $return['entries'][2]['id']);
601         $this->assertEquals($e1b2->id, $return['entries'][3]['id']);
602         $this->assertEquals($e1a1->id, $return['entries'][4]['id']);
603         $this->assertEquals($e1a2->id, $return['entries'][5]['id']);
604         $this->assertEquals($e1a3->id, $return['entries'][6]['id']);
606         // Changing order.
607         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'DESC', 0, 1);
608         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
609         $this->assertCount(1, $return['entries']);
610         $this->assertEquals(6, $return['count']);
611         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
613         // Sorting by firstname.
614         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'FIRSTNAME', 'ASC', 0, 1);
615         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
616         $this->assertCount(1, $return['entries']);
617         $this->assertEquals(6, $return['count']);
618         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
620         // Sorting by firstname descending.
621         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'FIRSTNAME', 'DESC', 0, 1);
622         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
623         $this->assertCount(1, $return['entries']);
624         $this->assertEquals(6, $return['count']);
625         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
627         // Filtering by firstname descending.
628         $return = mod_glossary_external::get_entries_by_author($g1->id, 'z', 'FIRSTNAME', 'DESC', 0, 20);
629         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
630         $this->assertCount(4, $return['entries']);
631         $this->assertEquals(4, $return['count']);
632         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
633         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
634         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
635         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
637         // Test with a deleted user.
638         delete_user($u2);
639         $return = mod_glossary_external::get_entries_by_author($g1->id, 'u', 'LASTNAME', 'ASC', 0, 20);
640         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
641         $this->assertCount(4, $return['entries']);
642         $this->assertEquals(4, $return['count']);
643         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
644         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
645         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
646         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
647     }
649     public function test_get_entries_by_author_id() {
650         $this->resetAfterTest(true);
652         // Generate all the things.
653         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
654         $c1 = $this->getDataGenerator()->create_course();
655         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
656         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
657         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon', 'firstname' => 'Zac'));
658         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Ultra', 'firstname' => '1337'));
659         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha', 'firstname' => 'Omega'));
660         $u4 = $this->getDataGenerator()->create_user(array('lastname' => '0-day', 'firstname' => 'Zoe'));
661         $ctx = context_module::instance($g1->cmid);
662         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
664         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Zoom',
665             'timecreated' => 3600, 'timemodified' => time() - 3600));
666         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Alpha'));
667         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Dog',
668             'timecreated' => 1, 'timemodified' => time() - 1800));
669         $e1a4 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id, 'concept' => 'Bird'));
670         $e1b1 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
671         $e2a = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
673         $this->setAdminUser();
675         // Standard request.
676         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 20);
677         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
678         $this->assertCount(3, $return['entries']);
679         $this->assertEquals(3, $return['count']);
680         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
681         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
682         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
684         // Standard request descending.
685         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'DESC', 0, 20);
686         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
687         $this->assertCount(3, $return['entries']);
688         $this->assertEquals(3, $return['count']);
689         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
690         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
691         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
693         // Requesting ordering by time created.
694         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', 'ASC', 0, 20);
695         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
696         $this->assertCount(3, $return['entries']);
697         $this->assertEquals(3, $return['count']);
698         $this->assertEquals($e1a3->id, $return['entries'][0]['id']);
699         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
700         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
702         // Requesting ordering by time created descending.
703         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', 'DESC', 0, 20);
704         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
705         $this->assertCount(3, $return['entries']);
706         $this->assertEquals(3, $return['count']);
707         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
708         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
709         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
711         // Requesting ordering by time modified.
712         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'ASC', 0, 20);
713         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
714         $this->assertCount(3, $return['entries']);
715         $this->assertEquals(3, $return['count']);
716         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
717         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
718         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
720         // Requesting ordering by time modified descending.
721         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'DESC', 0, 20);
722         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
723         $this->assertCount(3, $return['entries']);
724         $this->assertEquals(3, $return['count']);
725         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
726         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
727         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
729         // Including non approved.
730         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 20,
731             array('includenotapproved' => true));
732         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
733         $this->assertCount(4, $return['entries']);
734         $this->assertEquals(4, $return['count']);
735         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
736         $this->assertEquals($e1a4->id, $return['entries'][1]['id']);
737         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
738         $this->assertEquals($e1a1->id, $return['entries'][3]['id']);
740         // Pagination.
741         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 2,
742             array('includenotapproved' => true));
743         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
744         $this->assertCount(2, $return['entries']);
745         $this->assertEquals(4, $return['count']);
746         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
747         $this->assertEquals($e1a4->id, $return['entries'][1]['id']);
748         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 1, 2,
749             array('includenotapproved' => true));
750         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
751         $this->assertCount(2, $return['entries']);
752         $this->assertEquals(4, $return['count']);
753         $this->assertEquals($e1a4->id, $return['entries'][0]['id']);
754         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
755     }
757     public function test_get_entries_by_search() {
758         $this->resetAfterTest(true);
760         // Generate all the things.
761         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
762         $c1 = $this->getDataGenerator()->create_course();
763         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
764         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
765         $u1 = $this->getDataGenerator()->create_user();
766         $ctx = context_module::instance($g1->cmid);
767         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
768         $this->setUser($u1);
770         $e1 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'House', 'timecreated' => time() + 3600));
771         $e2 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Mouse', 'timemodified' => 1));
772         $e3 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Hero'));
773         $e4 = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Toulouse'));
774         $e5 = $gg->create_content($g1, array('approved' => 1, 'definition' => 'Heroes', 'concept' => 'Abcd'));
775         $e6 = $gg->create_content($g1, array('approved' => 0, 'definition' => 'When used for heroes'));
776         $e7 = $gg->create_content($g1, array('approved' => 1, 'timecreated' => 1, 'timemodified' => time() + 3600, 'concept' => 'Z'),
777             array('Couscous'));
778         $e8 = $gg->create_content($g1, array('approved' => 0), array('Heroes'));
779         $e9 = $gg->create_content($g2, array('approved' => 0));
781         $this->setAdminUser();
783         // Test simple query.
784         $query = 'hero';
785         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20, array());
786         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
787         $this->assertCount(1, $return['entries']);
788         $this->assertEquals(1, $return['count']);
789         $this->assertEquals($e3->id, $return['entries'][0]['id']);
791         // Enabling full search.
792         $query = 'hero';
793         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20, array());
794         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
795         $this->assertCount(2, $return['entries']);
796         $this->assertEquals(2, $return['count']);
797         $this->assertEquals($e5->id, $return['entries'][0]['id']);
798         $this->assertEquals($e3->id, $return['entries'][1]['id']);
800         // Concept descending
801         $query = 'hero';
802         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'DESC', 0, 20, array());
803         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
804         $this->assertCount(2, $return['entries']);
805         $this->assertEquals(2, $return['count']);
806         $this->assertEquals($e3->id, $return['entries'][0]['id']);
807         $this->assertEquals($e5->id, $return['entries'][1]['id']);
809         // Search on alias.
810         $query = 'couscous';
811         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20, array());
812         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
813         $this->assertCount(1, $return['entries']);
814         $this->assertEquals(1, $return['count']);
815         $this->assertEquals($e7->id, $return['entries'][0]['id']);
816         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20, array());
817         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
818         $this->assertCount(1, $return['entries']);
819         $this->assertEquals(1, $return['count']);
820         $this->assertEquals($e7->id, $return['entries'][0]['id']);
822         // Pagination and ordering on created date.
823         $query = 'ou';
824         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CREATION', 'ASC', 0, 1, array());
825         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
826         $this->assertCount(1, $return['entries']);
827         $this->assertEquals(3, $return['count']);
828         $this->assertEquals($e7->id, $return['entries'][0]['id']);
829         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CREATION', 'DESC', 0, 1, array());
830         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
831         $this->assertCount(1, $return['entries']);
832         $this->assertEquals(3, $return['count']);
833         $this->assertEquals($e1->id, $return['entries'][0]['id']);
835         // Ordering on updated date.
836         $query = 'ou';
837         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'UPDATE', 'ASC', 0, 1, array());
838         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
839         $this->assertCount(1, $return['entries']);
840         $this->assertEquals(3, $return['count']);
841         $this->assertEquals($e2->id, $return['entries'][0]['id']);
842         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'UPDATE', 'DESC', 0, 1, array());
843         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
844         $this->assertCount(1, $return['entries']);
845         $this->assertEquals(3, $return['count']);
846         $this->assertEquals($e7->id, $return['entries'][0]['id']);
848         // Including not approved.
849         $query = 'ou';
850         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20,
851             array('includenotapproved' => true));
852         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
853         $this->assertCount(4, $return['entries']);
854         $this->assertEquals(4, $return['count']);
855         $this->assertEquals($e1->id, $return['entries'][0]['id']);
856         $this->assertEquals($e2->id, $return['entries'][1]['id']);
857         $this->assertEquals($e4->id, $return['entries'][2]['id']);
858         $this->assertEquals($e7->id, $return['entries'][3]['id']);
860         // Advanced query string.
861         $query = '+heroes -abcd';
862         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20,
863             array('includenotapproved' => true));
864         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
865         $this->assertCount(2, $return['entries']);
866         $this->assertEquals(2, $return['count']);
867         $this->assertEquals($e6->id, $return['entries'][0]['id']);
868         $this->assertEquals($e8->id, $return['entries'][1]['id']);
869     }
871     public function test_get_entries_by_term() {
872         $this->resetAfterTest(true);
874         // Generate all the things.
875         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
876         $c1 = $this->getDataGenerator()->create_course();
877         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
878         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
879         $u1 = $this->getDataGenerator()->create_user();
880         $ctx = context_module::instance($g1->cmid);
881         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
883         $this->setAdminUser();
885         $e1 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1, 'concept' => 'cat'));
886         $e2 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1), array('cat', 'dog'));
887         $e3 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1), array('dog'));
888         $e4 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 0, 'concept' => 'dog'));
889         $e5 = $gg->create_content($g2, array('userid' => $u1->id, 'approved' => 1, 'concept' => 'dog'), array('cat'));
891         // Search concept + alias.
892         $return = mod_glossary_external::get_entries_by_term($g1->id, 'cat', 0, 20, array('includenotapproved' => false));
893         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
894         $this->assertCount(2, $return['entries']);
895         $this->assertEquals(2, $return['count']);
896         $this->assertEquals($e1->id, $return['entries'][0]['id']);
897         $this->assertEquals($e2->id, $return['entries'][1]['id']);
899         // Search alias.
900         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 20, array('includenotapproved' => false));
901         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
903         $this->assertCount(2, $return['entries']);
904         $this->assertEquals(2, $return['count']);
905         $this->assertEquals($e2->id, $return['entries'][0]['id']);
906         $this->assertEquals($e3->id, $return['entries'][1]['id']);
908         // Search including not approved.
909         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 20, array('includenotapproved' => true));
910         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
911         $this->assertCount(3, $return['entries']);
912         $this->assertEquals(3, $return['count']);
913         $this->assertEquals($e4->id, $return['entries'][0]['id']);
914         $this->assertEquals($e2->id, $return['entries'][1]['id']);
915         $this->assertEquals($e3->id, $return['entries'][2]['id']);
917         // Pagination.
918         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 1, array('includenotapproved' => true));
919         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
920         $this->assertCount(1, $return['entries']);
921         $this->assertEquals(3, $return['count']);
922         $this->assertEquals($e4->id, $return['entries'][0]['id']);
923         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 1, 1, array('includenotapproved' => true));
924         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
925         $this->assertCount(1, $return['entries']);
926         $this->assertEquals(3, $return['count']);
927         $this->assertEquals($e2->id, $return['entries'][0]['id']);
928     }
930     public function test_get_entry_by_id() {
931         $this->resetAfterTest(true);
933         // Generate all the things.
934         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
935         $c1 = $this->getDataGenerator()->create_course();
936         $c2 = $this->getDataGenerator()->create_course();
937         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
938         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'visible' => 0));
939         $u1 = $this->getDataGenerator()->create_user();
940         $u2 = $this->getDataGenerator()->create_user();
941         $ctx = context_module::instance($g1->cmid);
942         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
944         $e1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
945         $e2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
946         $e3 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
947         $e4 = $gg->create_content($g2, array('approved' => 1));
949         $this->setUser($u1);
950         $return = mod_glossary_external::get_entry_by_id($e1->id);
951         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
952         $this->assertEquals($e1->id, $return['entry']['id']);
954         $return = mod_glossary_external::get_entry_by_id($e2->id);
955         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
956         $this->assertEquals($e2->id, $return['entry']['id']);
958         try {
959             $return = mod_glossary_external::get_entry_by_id($e3->id);
960             $this->fail('Cannot view unapproved entries of others.');
961         } catch (invalid_parameter_exception $e) {
962         }
964         try {
965             $return = mod_glossary_external::get_entry_by_id($e4->id);
966             $this->fail('Cannot view entries from another course.');
967         } catch (require_login_exception $e) {
968         }
970         // An admin can be other's entries to be approved.
971         $this->setAdminUser();
972         $return = mod_glossary_external::get_entry_by_id($e3->id);
973         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
974         $this->assertEquals($e3->id, $return['entry']['id']);
975     }