MDL-49231 mod_glossary: Minor coding style improvements
[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             // All good.
177         }
179         // Test non-readable entry.
180         $this->setExpectedException('require_login_exception', 'Activity is hidden');
181         mod_glossary_external::view_entry($e4->id);
182     }
184     public function test_get_entries_by_letter() {
185         $this->resetAfterTest(true);
187         // Generate all the things.
188         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
189         $c1 = $this->getDataGenerator()->create_course();
190         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
191         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
192         $u1 = $this->getDataGenerator()->create_user();
193         $ctx = context_module::instance($g1->cmid);
194         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
196         $e1a = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Bob', 'userid' => 2));
197         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Jane', 'userid' => 2));
198         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Alice', 'userid' => $u1->id));
199         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id));
200         $e2a = $gg->create_content($g2);
202         $this->setAdminUser();
204         // Just a normal request from admin user.
205         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array());
206         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
207         $this->assertCount(3, $return['entries']);
208         $this->assertEquals(3, $return['count']);
209         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
210         $this->assertEquals($e1a->id, $return['entries'][1]['id']);
211         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
213         // An admin user requesting all the entries.
214         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array('includenotapproved' => 1));
215         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
216         $this->assertCount(4, $return['entries']);
217         $this->assertEquals(4, $return['count']);
218         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
219         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
220         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
221         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
223         // A normal user.
224         $this->setUser($u1);
225         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array());
226         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
227         $this->assertCount(3, $return['entries']);
228         $this->assertEquals(3, $return['count']);
229         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
230         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
231         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
233         // A normal user requesting to view all non approved entries.
234         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array('includenotapproved' => 1));
235         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
236         $this->assertCount(3, $return['entries']);
237         $this->assertEquals(3, $return['count']);
238         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
239         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
240         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
241     }
243     public function test_get_entries_by_letter_with_parameters() {
244         $this->resetAfterTest(true);
246         // Generate all the things.
247         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
248         $c1 = $this->getDataGenerator()->create_course();
249         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
250         $u1 = $this->getDataGenerator()->create_user();
251         $ctx = context_module::instance($g1->cmid);
252         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
254         $e1a = $gg->create_content($g1, array('approved' => 1, 'concept' => '0-day', 'userid' => $u1->id));
255         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Bob', 'userid' => 2));
256         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => '1-dayb', 'userid' => $u1->id));
258         $this->setUser($u1);
260         // Requesting a single letter.
261         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'b', 0, 20, array());
262         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
263         $this->assertCount(1, $return['entries']);
264         $this->assertEquals(1, $return['count']);
265         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
267         // Requesting special letters.
268         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'SPECIAL', 0, 20, array());
269         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
270         $this->assertCount(2, $return['entries']);
271         $this->assertEquals(2, $return['count']);
272         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
273         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
275         // Requesting with limit.
276         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 1, array());
277         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
278         $this->assertCount(1, $return['entries']);
279         $this->assertEquals(3, $return['count']);
280         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
281         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 1, 2, array());
282         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
283         $this->assertCount(2, $return['entries']);
284         $this->assertEquals(3, $return['count']);
285         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
286         $this->assertEquals($e1b->id, $return['entries'][1]['id']);
287     }
289     public function test_get_entries_by_date() {
290         global $DB;
291         $this->resetAfterTest(true);
293         // Generate all the things.
294         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
295         $c1 = $this->getDataGenerator()->create_course();
296         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
297         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
298         $u1 = $this->getDataGenerator()->create_user();
299         $ctx = context_module::instance($g1->cmid);
300         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
302         $now = time();
303         $e1a = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Bob', 'userid' => $u1->id,
304             'timecreated' => 1, 'timemodified' => $now + 3600));
305         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Jane', 'userid' => $u1->id,
306             'timecreated' => $now + 3600, 'timemodified' => 1));
307         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Alice', 'userid' => $u1->id,
308             'timecreated' => $now + 1, 'timemodified' => $now + 1));
309         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id,
310             'timecreated' => $now + 2, 'timemodified' => $now + 2));
311         $e2a = $gg->create_content($g2);
313         $this->setAdminUser($u1);
315         // Ordering by time modified descending.
316         $return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'DESC', 0, 20, array());
317         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
318         $this->assertCount(3, $return['entries']);
319         $this->assertEquals(3, $return['count']);
320         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
321         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
322         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
324         // Ordering by time modified ascending.
325         $return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'ASC', 0, 20, array());
326         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
327         $this->assertCount(3, $return['entries']);
328         $this->assertEquals(3, $return['count']);
329         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
330         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
331         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
333         // Ordering by time created asc.
334         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20, array());
335         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
336         $this->assertCount(3, $return['entries']);
337         $this->assertEquals(3, $return['count']);
338         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
339         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
340         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
342         // Ordering by time created descending.
343         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'DESC', 0, 20, array());
344         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
345         $this->assertCount(3, $return['entries']);
346         $this->assertEquals(3, $return['count']);
347         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
348         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
349         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
351         // Ordering including to approve.
352         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20,
353             array('includenotapproved' => true));
354         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
355         $this->assertCount(4, $return['entries']);
356         $this->assertEquals(4, $return['count']);
357         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
358         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
359         $this->assertEquals($e1d->id, $return['entries'][2]['id']);
360         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
362         // Ordering including to approve and pagination.
363         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 2,
364             array('includenotapproved' => true));
365         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
366         $this->assertCount(2, $return['entries']);
367         $this->assertEquals(4, $return['count']);
368         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
369         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
370         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 2, 2,
371             array('includenotapproved' => true));
372         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
373         $this->assertCount(2, $return['entries']);
374         $this->assertEquals(4, $return['count']);
375         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
376         $this->assertEquals($e1b->id, $return['entries'][1]['id']);
377     }
379     public function test_get_categories() {
380         $this->resetAfterTest(true);
381         $this->setAdminUser();
383         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
384         $c1 = $this->getDataGenerator()->create_course();
385         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
386         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
387         $cat1a = $gg->create_category($g1);
388         $cat1b = $gg->create_category($g1);
389         $cat1c = $gg->create_category($g1);
390         $cat2a = $gg->create_category($g2);
392         $return = mod_glossary_external::get_categories($g1->id, 0, 20);
393         $return = external_api::clean_returnvalue(mod_glossary_external::get_categories_returns(), $return);
394         $this->assertCount(3, $return['categories']);
395         $this->assertEquals(3, $return['count']);
396         $this->assertEquals($cat1a->id, $return['categories'][0]['id']);
397         $this->assertEquals($cat1b->id, $return['categories'][1]['id']);
398         $this->assertEquals($cat1c->id, $return['categories'][2]['id']);
400         $return = mod_glossary_external::get_categories($g1->id, 1, 2);
401         $return = external_api::clean_returnvalue(mod_glossary_external::get_categories_returns(), $return);
402         $this->assertCount(2, $return['categories']);
403         $this->assertEquals(3, $return['count']);
404         $this->assertEquals($cat1b->id, $return['categories'][0]['id']);
405         $this->assertEquals($cat1c->id, $return['categories'][1]['id']);
406     }
408     public function test_get_entries_by_category() {
409         $this->resetAfterTest(true);
411         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
412         $c1 = $this->getDataGenerator()->create_course();
413         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
414         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
415         $u1 = $this->getDataGenerator()->create_user();
416         $ctx = context_module::instance($g1->cmid);
418         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
419         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
420         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
421         $e1b1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
422         $e1b2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
423         $e1x1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
424         $e1x2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
425         $e2a1 = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
426         $e2a2 = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
428         $cat1a = $gg->create_category($g1, array('name' => 'Fish'), array($e1a1, $e1a2, $e1a3));
429         $cat1b = $gg->create_category($g1, array('name' => 'Cat'), array($e1b1, $e1b2));
430         $cat1c = $gg->create_category($g1, array('name' => 'Zebra'), array($e1b1));   // Entry $e1b1 is in two categories.
431         $cat2a = $gg->create_category($g2, array(), array($e2a1, $e2a2));
433         $this->setAdminUser();
435         // Browse one category.
436         $return = mod_glossary_external::get_entries_by_category($g1->id, $cat1a->id, 0, 20, array());
437         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
438         $this->assertCount(3, $return['entries']);
439         $this->assertEquals(3, $return['count']);
440         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
441         $this->assertEquals($e1a2->id, $return['entries'][1]['id']);
442         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
444         // Browse all categories.
445         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 0, 20, array());
446         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
447         $this->assertCount(5, $return['entries']);
448         $this->assertEquals(5, $return['count']);
449         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
450         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
451         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
452         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
453         $this->assertEquals($e1b1->id, $return['entries'][4]['id']);
455         // Browse uncategorised.
456         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_NOT_CATEGORISED, 0, 20, array());
457         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
458         $this->assertCount(1, $return['entries']);
459         $this->assertEquals(1, $return['count']);
460         $this->assertEquals($e1x1->id, $return['entries'][0]['id']);
462         // Including to approve.
463         $return = mod_glossary_external::get_entries_by_category($g1->id, $cat1b->id, 0, 20,
464             array('includenotapproved' => true));
465         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
466         $this->assertCount(2, $return['entries']);
467         $this->assertEquals(2, $return['count']);
468         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
469         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
471         // Using limit.
472         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 0, 3,
473             array('includenotapproved' => true));
474         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
475         $this->assertCount(3, $return['entries']);
476         $this->assertEquals(6, $return['count']);
477         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
478         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
479         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
480         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 3, 2,
481             array('includenotapproved' => true));
482         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
483         $this->assertCount(2, $return['entries']);
484         $this->assertEquals(6, $return['count']);
485         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
486         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
487     }
489     public function test_get_authors() {
490         $this->resetAfterTest(true);
492         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
493         $c1 = $this->getDataGenerator()->create_course();
494         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
495         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
497         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon'));
498         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha'));
499         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Omega'));
501         $ctx = context_module::instance($g1->cmid);
503         $e1a = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
504         $e1b = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
505         $e1c = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
506         $e2a = $gg->create_content($g1, array('userid' => $u2->id, 'approved' => 1));
507         $e3a = $gg->create_content($g1, array('userid' => $u3->id, 'approved' => 0));
509         $this->setAdminUser();
511         // Simple request.
512         $return = mod_glossary_external::get_authors($g1->id, 0, 20, array());
513         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
514         $this->assertCount(2, $return['authors']);
515         $this->assertEquals(2, $return['count']);
516         $this->assertEquals($u2->id, $return['authors'][0]['id']);
517         $this->assertEquals($u1->id, $return['authors'][1]['id']);
519         // Include users with entries pending approval.
520         $return = mod_glossary_external::get_authors($g1->id, 0, 20, array('includenotapproved' => true));
521         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
522         $this->assertCount(3, $return['authors']);
523         $this->assertEquals(3, $return['count']);
524         $this->assertEquals($u2->id, $return['authors'][0]['id']);
525         $this->assertEquals($u3->id, $return['authors'][1]['id']);
526         $this->assertEquals($u1->id, $return['authors'][2]['id']);
528         // Pagination.
529         $return = mod_glossary_external::get_authors($g1->id, 1, 1, array('includenotapproved' => true));
530         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
531         $this->assertCount(1, $return['authors']);
532         $this->assertEquals(3, $return['count']);
533         $this->assertEquals($u3->id, $return['authors'][0]['id']);
534     }
536     public function test_get_entries_by_author() {
537         $this->resetAfterTest(true);
539         // Generate all the things.
540         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
541         $c1 = $this->getDataGenerator()->create_course();
542         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
543         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
544         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon', 'firstname' => 'Zac'));
545         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Ultra', 'firstname' => '1337'));
546         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha', 'firstname' => 'Omega'));
547         $u4 = $this->getDataGenerator()->create_user(array('lastname' => '0-day', 'firstname' => 'Zoe'));
548         $ctx = context_module::instance($g1->cmid);
549         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
551         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
552         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
553         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
554         $e1b1 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
555         $e1b2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u2->id));
556         $e1c1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u3->id));
557         $e1d1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u4->id));
558         $e2a = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
560         $this->setUser($u1);
562         // Requesting a single letter.
563         $return = mod_glossary_external::get_entries_by_author($g1->id, 'u', 'LASTNAME', 'ASC', 0, 20, array());
564         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
565         $this->assertCount(4, $return['entries']);
566         $this->assertEquals(4, $return['count']);
567         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
568         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
569         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
570         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
572         // Requesting special letters.
573         $return = mod_glossary_external::get_entries_by_author($g1->id, 'SPECIAL', 'LASTNAME', 'ASC', 0, 20, array());
574         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
575         $this->assertCount(1, $return['entries']);
576         $this->assertEquals(1, $return['count']);
577         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
579         // Requesting with limit.
580         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 0, 1, array());
581         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
582         $this->assertCount(1, $return['entries']);
583         $this->assertEquals(6, $return['count']);
584         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
585         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 1, 2, array());
586         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
587         $this->assertCount(2, $return['entries']);
588         $this->assertEquals(6, $return['count']);
589         $this->assertEquals($e1c1->id, $return['entries'][0]['id']);
590         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
592         // Including non-approved.
593         $this->setAdminUser();
594         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 0, 20,
595             array('includenotapproved' => true));
596         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
597         $this->assertCount(7, $return['entries']);
598         $this->assertEquals(7, $return['count']);
599         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
600         $this->assertEquals($e1c1->id, $return['entries'][1]['id']);
601         $this->assertEquals($e1b1->id, $return['entries'][2]['id']);
602         $this->assertEquals($e1b2->id, $return['entries'][3]['id']);
603         $this->assertEquals($e1a1->id, $return['entries'][4]['id']);
604         $this->assertEquals($e1a2->id, $return['entries'][5]['id']);
605         $this->assertEquals($e1a3->id, $return['entries'][6]['id']);
607         // Changing order.
608         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'DESC', 0, 1, array());
609         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
610         $this->assertCount(1, $return['entries']);
611         $this->assertEquals(6, $return['count']);
612         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
614         // Sorting by firstname.
615         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'FIRSTNAME', 'ASC', 0, 1, array());
616         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
617         $this->assertCount(1, $return['entries']);
618         $this->assertEquals(6, $return['count']);
619         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
621         // Sorting by firstname descending.
622         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'FIRSTNAME', 'DESC', 0, 1, array());
623         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
624         $this->assertCount(1, $return['entries']);
625         $this->assertEquals(6, $return['count']);
626         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
628         // Filtering by firstname descending.
629         $return = mod_glossary_external::get_entries_by_author($g1->id, 'z', 'FIRSTNAME', 'DESC', 0, 20, array());
630         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
631         $this->assertCount(4, $return['entries']);
632         $this->assertEquals(4, $return['count']);
633         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
634         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
635         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
636         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
638         // Test with a deleted user.
639         delete_user($u2);
640         $return = mod_glossary_external::get_entries_by_author($g1->id, 'u', 'LASTNAME', 'ASC', 0, 20, array());
641         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
642         $this->assertCount(4, $return['entries']);
643         $this->assertEquals(4, $return['count']);
644         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
645         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
646         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
647         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
648     }
650     public function test_get_entries_by_author_id() {
651         $this->resetAfterTest(true);
653         // Generate all the things.
654         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
655         $c1 = $this->getDataGenerator()->create_course();
656         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
657         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
658         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon', 'firstname' => 'Zac'));
659         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Ultra', 'firstname' => '1337'));
660         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha', 'firstname' => 'Omega'));
661         $u4 = $this->getDataGenerator()->create_user(array('lastname' => '0-day', 'firstname' => 'Zoe'));
662         $ctx = context_module::instance($g1->cmid);
663         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
665         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Zoom',
666             'timecreated' => 3600, 'timemodified' => time() - 3600));
667         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Alpha'));
668         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Dog',
669             'timecreated' => 1, 'timemodified' => time() - 1800));
670         $e1a4 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id, 'concept' => 'Bird'));
671         $e1b1 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
672         $e2a = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
674         $this->setAdminUser();
676         // Standard request.
677         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 20, array());
678         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
679         $this->assertCount(3, $return['entries']);
680         $this->assertEquals(3, $return['count']);
681         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
682         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
683         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
685         // Standard request descending.
686         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'DESC', 0, 20, array());
687         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
688         $this->assertCount(3, $return['entries']);
689         $this->assertEquals(3, $return['count']);
690         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
691         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
692         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
694         // Requesting ordering by time created.
695         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', 'ASC', 0, 20, array());
696         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
697         $this->assertCount(3, $return['entries']);
698         $this->assertEquals(3, $return['count']);
699         $this->assertEquals($e1a3->id, $return['entries'][0]['id']);
700         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
701         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
703         // Requesting ordering by time created descending.
704         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', 'DESC', 0, 20, array());
705         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
706         $this->assertCount(3, $return['entries']);
707         $this->assertEquals(3, $return['count']);
708         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
709         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
710         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
712         // Requesting ordering by time modified.
713         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'ASC', 0, 20, array());
714         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
715         $this->assertCount(3, $return['entries']);
716         $this->assertEquals(3, $return['count']);
717         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
718         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
719         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
721         // Requesting ordering by time modified descending.
722         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'DESC', 0, 20, array());
723         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
724         $this->assertCount(3, $return['entries']);
725         $this->assertEquals(3, $return['count']);
726         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
727         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
728         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
730         // Including non approved.
731         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 20,
732             array('includenotapproved' => true));
733         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
734         $this->assertCount(4, $return['entries']);
735         $this->assertEquals(4, $return['count']);
736         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
737         $this->assertEquals($e1a4->id, $return['entries'][1]['id']);
738         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
739         $this->assertEquals($e1a1->id, $return['entries'][3]['id']);
741         // Pagination.
742         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 2,
743             array('includenotapproved' => true));
744         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
745         $this->assertCount(2, $return['entries']);
746         $this->assertEquals(4, $return['count']);
747         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
748         $this->assertEquals($e1a4->id, $return['entries'][1]['id']);
749         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 1, 2,
750             array('includenotapproved' => true));
751         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
752         $this->assertCount(2, $return['entries']);
753         $this->assertEquals(4, $return['count']);
754         $this->assertEquals($e1a4->id, $return['entries'][0]['id']);
755         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
756     }
758     public function test_get_entries_by_search() {
759         $this->resetAfterTest(true);
761         // Generate all the things.
762         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
763         $c1 = $this->getDataGenerator()->create_course();
764         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
765         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
766         $u1 = $this->getDataGenerator()->create_user();
767         $ctx = context_module::instance($g1->cmid);
768         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
769         $this->setUser($u1);
771         $e1 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'House', 'timecreated' => time() + 3600));
772         $e2 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Mouse', 'timemodified' => 1));
773         $e3 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Hero'));
774         $e4 = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Toulouse'));
775         $e5 = $gg->create_content($g1, array('approved' => 1, 'definition' => 'Heroes', 'concept' => 'Abcd'));
776         $e6 = $gg->create_content($g1, array('approved' => 0, 'definition' => 'When used for heroes'));
777         $e7 = $gg->create_content($g1, array('approved' => 1, 'timecreated' => 1, 'timemodified' => time() + 3600,
778             'concept' => 'Z'), array('Couscous'));
779         $e8 = $gg->create_content($g1, array('approved' => 0), array('Heroes'));
780         $e9 = $gg->create_content($g2, array('approved' => 0));
782         $this->setAdminUser();
784         // Test simple query.
785         $query = 'hero';
786         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20, array());
787         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
788         $this->assertCount(1, $return['entries']);
789         $this->assertEquals(1, $return['count']);
790         $this->assertEquals($e3->id, $return['entries'][0]['id']);
792         // Enabling full search.
793         $query = 'hero';
794         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20, array());
795         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
796         $this->assertCount(2, $return['entries']);
797         $this->assertEquals(2, $return['count']);
798         $this->assertEquals($e5->id, $return['entries'][0]['id']);
799         $this->assertEquals($e3->id, $return['entries'][1]['id']);
801         // Concept descending.
802         $query = 'hero';
803         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'DESC', 0, 20, array());
804         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
805         $this->assertCount(2, $return['entries']);
806         $this->assertEquals(2, $return['count']);
807         $this->assertEquals($e3->id, $return['entries'][0]['id']);
808         $this->assertEquals($e5->id, $return['entries'][1]['id']);
810         // Search on alias.
811         $query = 'couscous';
812         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20, array());
813         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
814         $this->assertCount(1, $return['entries']);
815         $this->assertEquals(1, $return['count']);
816         $this->assertEquals($e7->id, $return['entries'][0]['id']);
817         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20, array());
818         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
819         $this->assertCount(1, $return['entries']);
820         $this->assertEquals(1, $return['count']);
821         $this->assertEquals($e7->id, $return['entries'][0]['id']);
823         // Pagination and ordering on created date.
824         $query = 'ou';
825         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CREATION', 'ASC', 0, 1, array());
826         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
827         $this->assertCount(1, $return['entries']);
828         $this->assertEquals(3, $return['count']);
829         $this->assertEquals($e7->id, $return['entries'][0]['id']);
830         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CREATION', 'DESC', 0, 1, array());
831         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
832         $this->assertCount(1, $return['entries']);
833         $this->assertEquals(3, $return['count']);
834         $this->assertEquals($e1->id, $return['entries'][0]['id']);
836         // Ordering on updated date.
837         $query = 'ou';
838         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'UPDATE', 'ASC', 0, 1, array());
839         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
840         $this->assertCount(1, $return['entries']);
841         $this->assertEquals(3, $return['count']);
842         $this->assertEquals($e2->id, $return['entries'][0]['id']);
843         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'UPDATE', 'DESC', 0, 1, array());
844         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
845         $this->assertCount(1, $return['entries']);
846         $this->assertEquals(3, $return['count']);
847         $this->assertEquals($e7->id, $return['entries'][0]['id']);
849         // Including not approved.
850         $query = 'ou';
851         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20,
852             array('includenotapproved' => true));
853         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
854         $this->assertCount(4, $return['entries']);
855         $this->assertEquals(4, $return['count']);
856         $this->assertEquals($e1->id, $return['entries'][0]['id']);
857         $this->assertEquals($e2->id, $return['entries'][1]['id']);
858         $this->assertEquals($e4->id, $return['entries'][2]['id']);
859         $this->assertEquals($e7->id, $return['entries'][3]['id']);
861         // Advanced query string.
862         $query = '+heroes -abcd';
863         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20,
864             array('includenotapproved' => true));
865         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
866         $this->assertCount(2, $return['entries']);
867         $this->assertEquals(2, $return['count']);
868         $this->assertEquals($e6->id, $return['entries'][0]['id']);
869         $this->assertEquals($e8->id, $return['entries'][1]['id']);
870     }
872     public function test_get_entries_by_term() {
873         $this->resetAfterTest(true);
875         // Generate all the things.
876         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
877         $c1 = $this->getDataGenerator()->create_course();
878         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
879         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
880         $u1 = $this->getDataGenerator()->create_user();
881         $ctx = context_module::instance($g1->cmid);
882         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
884         $this->setAdminUser();
886         $e1 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1, 'concept' => 'cat'));
887         $e2 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1), array('cat', 'dog'));
888         $e3 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1), array('dog'));
889         $e4 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 0, 'concept' => 'dog'));
890         $e5 = $gg->create_content($g2, array('userid' => $u1->id, 'approved' => 1, 'concept' => 'dog'), array('cat'));
892         // Search concept + alias.
893         $return = mod_glossary_external::get_entries_by_term($g1->id, 'cat', 0, 20, array('includenotapproved' => false));
894         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
895         $this->assertCount(2, $return['entries']);
896         $this->assertEquals(2, $return['count']);
897         $this->assertEquals($e1->id, $return['entries'][0]['id']);
898         $this->assertEquals($e2->id, $return['entries'][1]['id']);
900         // Search alias.
901         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 20, array('includenotapproved' => false));
902         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
904         $this->assertCount(2, $return['entries']);
905         $this->assertEquals(2, $return['count']);
906         $this->assertEquals($e2->id, $return['entries'][0]['id']);
907         $this->assertEquals($e3->id, $return['entries'][1]['id']);
909         // Search including not approved.
910         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 20, array('includenotapproved' => true));
911         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
912         $this->assertCount(3, $return['entries']);
913         $this->assertEquals(3, $return['count']);
914         $this->assertEquals($e4->id, $return['entries'][0]['id']);
915         $this->assertEquals($e2->id, $return['entries'][1]['id']);
916         $this->assertEquals($e3->id, $return['entries'][2]['id']);
918         // Pagination.
919         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 1, array('includenotapproved' => true));
920         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
921         $this->assertCount(1, $return['entries']);
922         $this->assertEquals(3, $return['count']);
923         $this->assertEquals($e4->id, $return['entries'][0]['id']);
924         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 1, 1, array('includenotapproved' => true));
925         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
926         $this->assertCount(1, $return['entries']);
927         $this->assertEquals(3, $return['count']);
928         $this->assertEquals($e2->id, $return['entries'][0]['id']);
929     }
931     public function test_get_entries_to_approve() {
932         $this->resetAfterTest(true);
934         // Generate all the things.
935         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
936         $c1 = $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));
939         $u1 = $this->getDataGenerator()->create_user();
940         $ctx = context_module::instance($g1->cmid);
941         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
943         $e1a = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Bob', 'userid' => $u1->id,
944             'timecreated' => time() + 3600));
945         $e1b = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Jane', 'userid' => $u1->id, 'timecreated' => 1));
946         $e1c = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Alice', 'userid' => $u1->id, 'timemodified' => 1));
947         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id,
948             'timemodified' => time() + 3600));
949         $e1e = $gg->create_content($g1, array('approved' => 1, 'concept' => '1-day', 'userid' => $u1->id));
950         $e2a = $gg->create_content($g2);
952         $this->setAdminUser(true);
954         // Simple listing.
955         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 0, 20);
956         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
957         $this->assertCount(4, $return['entries']);
958         $this->assertEquals(4, $return['count']);
959         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
960         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
961         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
962         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
964         // Revert ordering of concept.
965         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'DESC', 0, 20);
966         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
967         $this->assertCount(4, $return['entries']);
968         $this->assertEquals(4, $return['count']);
969         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
970         $this->assertEquals($e1a->id, $return['entries'][1]['id']);
971         $this->assertEquals($e1c->id, $return['entries'][2]['id']);
972         $this->assertEquals($e1d->id, $return['entries'][3]['id']);
974         // Filtering by letter.
975         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'a', 'CONCEPT', 'ASC', 0, 20);
976         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
977         $this->assertCount(1, $return['entries']);
978         $this->assertEquals(1, $return['count']);
979         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
981         // Filtering by special.
982         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'SPECIAL', 'CONCEPT', 'ASC', 0, 20);
983         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
984         $this->assertCount(1, $return['entries']);
985         $this->assertEquals(1, $return['count']);
986         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
988         // Pagination.
989         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 0, 2);
990         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
991         $this->assertCount(2, $return['entries']);
992         $this->assertEquals(4, $return['count']);
993         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
994         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
995         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 1, 2);
996         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
997         $this->assertCount(2, $return['entries']);
998         $this->assertEquals(4, $return['count']);
999         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
1000         $this->assertEquals($e1a->id, $return['entries'][1]['id']);
1002         // Ordering by creation date.
1003         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CREATION', 'ASC', 0, 1);
1004         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1005         $this->assertCount(1, $return['entries']);
1006         $this->assertEquals(4, $return['count']);
1007         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
1009         // Ordering by creation date desc.
1010         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CREATION', 'DESC', 0, 1);
1011         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1012         $this->assertCount(1, $return['entries']);
1013         $this->assertEquals(4, $return['count']);
1014         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
1016         // Ordering by update date.
1017         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'UPDATE', 'ASC', 0, 1);
1018         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1019         $this->assertCount(1, $return['entries']);
1020         $this->assertEquals(4, $return['count']);
1021         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
1023         // Ordering by update date desc.
1024         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'UPDATE', 'DESC', 0, 1);
1025         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1026         $this->assertCount(1, $return['entries']);
1027         $this->assertEquals(4, $return['count']);
1028         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
1030         // Permissions are checked.
1031         $this->setUser($u1);
1032         $this->setExpectedException('required_capability_exception');
1033         mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 0, 1);
1034         $this->fail('Do not test anything else after this.');
1035     }
1037     public function test_get_entry_by_id() {
1038         $this->resetAfterTest(true);
1040         // Generate all the things.
1041         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
1042         $c1 = $this->getDataGenerator()->create_course();
1043         $c2 = $this->getDataGenerator()->create_course();
1044         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
1045         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'visible' => 0));
1046         $u1 = $this->getDataGenerator()->create_user();
1047         $u2 = $this->getDataGenerator()->create_user();
1048         $ctx = context_module::instance($g1->cmid);
1049         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
1051         $e1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
1052         $e2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
1053         $e3 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
1054         $e4 = $gg->create_content($g2, array('approved' => 1));
1056         $this->setUser($u1);
1057         $return = mod_glossary_external::get_entry_by_id($e1->id);
1058         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
1059         $this->assertEquals($e1->id, $return['entry']['id']);
1061         $return = mod_glossary_external::get_entry_by_id($e2->id);
1062         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
1063         $this->assertEquals($e2->id, $return['entry']['id']);
1065         try {
1066             $return = mod_glossary_external::get_entry_by_id($e3->id);
1067             $this->fail('Cannot view unapproved entries of others.');
1068         } catch (invalid_parameter_exception $e) {
1069             // All good.
1070         }
1072         try {
1073             $return = mod_glossary_external::get_entry_by_id($e4->id);
1074             $this->fail('Cannot view entries from another course.');
1075         } catch (require_login_exception $e) {
1076             // All good.
1077         }
1079         // An admin can be other's entries to be approved.
1080         $this->setAdminUser();
1081         $return = mod_glossary_external::get_entry_by_id($e3->id);
1082         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
1083         $this->assertEquals($e3->id, $return['entry']['id']);
1084     }