MDL-64656 core_tag: Return tags in modules and blog
[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']);
65         $this->assertEquals(1, $glossaries['glossaries'][0]['canaddentry']);
66         $this->assertEquals(1, $glossaries['glossaries'][1]['canaddentry']);
68         // Check results with specific course IDs.
69         $glossaries = mod_glossary_external::get_glossaries_by_courses(array($c1->id, $c2->id));
70         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
72         $this->assertCount(2, $glossaries['glossaries']);
73         $this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']);
74         $this->assertEquals('Second Glossary', $glossaries['glossaries'][1]['name']);
76         $this->assertEquals('course', $glossaries['warnings'][0]['item']);
77         $this->assertEquals($c2->id, $glossaries['warnings'][0]['itemid']);
78         $this->assertEquals('1', $glossaries['warnings'][0]['warningcode']);
79         $this->assertEquals(1, $glossaries['glossaries'][0]['canaddentry']);
81         // Now as admin.
82         $this->setAdminUser();
84         $glossaries = mod_glossary_external::get_glossaries_by_courses(array($c2->id));
85         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
87         $this->assertCount(1, $glossaries['glossaries']);
88         $this->assertEquals('Third Glossary', $glossaries['glossaries'][0]['name']);
89         $this->assertEquals(1, $glossaries['glossaries'][0]['canaddentry']);
90     }
92     public function test_view_glossary() {
93         $this->resetAfterTest(true);
95         // Generate all the things.
96         $c1 = $this->getDataGenerator()->create_course();
97         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
98         $u1 = $this->getDataGenerator()->create_user();
99         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
101         $sink = $this->redirectEvents();
102         $this->setUser($u1);
103         $return = mod_glossary_external::view_glossary($g1->id, 'letter');
104         $return = external_api::clean_returnvalue(mod_glossary_external::view_glossary_returns(), $return);
105         $events = $sink->get_events();
107         // Assertion.
108         $this->assertTrue($return['status']);
109         $this->assertEmpty($return['warnings']);
110         $this->assertCount(1, $events);
111         $this->assertEquals('\mod_glossary\event\course_module_viewed', $events[0]->eventname);
112         $sink->close();
113     }
115     /**
116      * @expectedException        require_login_exception
117      * @expectedExceptionMessage Activity is hidden
118      */
119     public function test_view_glossary_without_permission() {
120         $this->resetAfterTest(true);
122         // Generate all the things.
123         $c1 = $this->getDataGenerator()->create_course();
124         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
125         $u1 = $this->getDataGenerator()->create_user();
126         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
127         $ctx = context_module::instance($g1->cmid);
129         // Revoke permission.
130         $roles = get_archetype_roles('user');
131         $role = array_shift($roles);
132         assign_capability('mod/glossary:view', CAP_PROHIBIT, $role->id, $ctx, true);
133         accesslib_clear_all_caches_for_unit_testing();
135         // Assertion.
136         $this->setUser($u1);
137         mod_glossary_external::view_glossary($g1->id, 'letter');
138     }
140     /**
141      * @expectedException        require_login_exception
142      * @expectedExceptionMessage Activity is hidden
143      */
144     public function test_view_entry() {
145         $this->resetAfterTest(true);
147         // Generate all the things.
148         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
149         $c1 = $this->getDataGenerator()->create_course();
150         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
151         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'visible' => false));
152         $u1 = $this->getDataGenerator()->create_user();
153         $e1 = $gg->create_content($g1, array('approved' => 1));
154         $e2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
155         $e3 = $gg->create_content($g1, array('approved' => 0, 'userid' => -1));
156         $e4 = $gg->create_content($g2, array('approved' => 1));
157         $ctx = context_module::instance($g1->cmid);
158         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
159         $this->setUser($u1);
161         // Test readable entry.
162         $sink = $this->redirectEvents();
163         $return = mod_glossary_external::view_entry($e1->id);
164         $return = external_api::clean_returnvalue(mod_glossary_external::view_entry_returns(), $return);
165         $events = $sink->get_events();
166         $this->assertTrue($return['status']);
167         $this->assertEmpty($return['warnings']);
168         $this->assertCount(1, $events);
169         $this->assertEquals('\mod_glossary\event\entry_viewed', $events[0]->eventname);
170         $sink->close();
172         // Test non-approved of self.
173         $return = mod_glossary_external::view_entry($e2->id);
174         $return = external_api::clean_returnvalue(mod_glossary_external::view_entry_returns(), $return);
175         $events = $sink->get_events();
176         $this->assertTrue($return['status']);
177         $this->assertEmpty($return['warnings']);
178         $this->assertCount(1, $events);
179         $this->assertEquals('\mod_glossary\event\entry_viewed', $events[0]->eventname);
180         $sink->close();
182         // Test non-approved of other.
183         try {
184             mod_glossary_external::view_entry($e3->id);
185             $this->fail('Cannot view non-approved entries of others.');
186         } catch (invalid_parameter_exception $e) {
187             // All good.
188         }
190         // Test non-readable entry.
191         mod_glossary_external::view_entry($e4->id);
192     }
194     public function test_get_entries_by_letter() {
195         $this->resetAfterTest(true);
197         // Generate all the things.
198         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
199         $c1 = $this->getDataGenerator()->create_course();
200         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
201         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
202         $u1 = $this->getDataGenerator()->create_user();
203         $ctx = context_module::instance($g1->cmid);
204         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
206         $e1a = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Bob', 'userid' => 2, 'tags' => array('Cats', 'Dogs')));
207         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Jane', 'userid' => 2));
208         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Alice', 'userid' => $u1->id));
209         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id));
210         $e2a = $gg->create_content($g2);
212         $this->setAdminUser();
214         // Just a normal request from admin user.
215         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array());
216         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
217         $this->assertCount(3, $return['entries']);
218         $this->assertEquals(3, $return['count']);
219         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
220         $this->assertEquals($e1a->id, $return['entries'][1]['id']);
221         $this->assertEquals('Cats', $return['entries'][1]['tags'][0]['rawname']);
222         $this->assertEquals('Dogs', $return['entries'][1]['tags'][1]['rawname']);
223         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
225         // An admin user requesting all the entries.
226         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array('includenotapproved' => 1));
227         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
228         $this->assertCount(4, $return['entries']);
229         $this->assertEquals(4, $return['count']);
230         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
231         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
232         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
233         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
235         // A normal user.
236         $this->setUser($u1);
237         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array());
238         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
239         $this->assertCount(3, $return['entries']);
240         $this->assertEquals(3, $return['count']);
241         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
242         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
243         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
245         // A normal user requesting to view all non approved entries.
246         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 20, array('includenotapproved' => 1));
247         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
248         $this->assertCount(3, $return['entries']);
249         $this->assertEquals(3, $return['count']);
250         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
251         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
252         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
253     }
255     public function test_get_entries_by_letter_with_parameters() {
256         $this->resetAfterTest(true);
258         // Generate all the things.
259         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
260         $c1 = $this->getDataGenerator()->create_course();
261         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
262         $u1 = $this->getDataGenerator()->create_user();
263         $ctx = context_module::instance($g1->cmid);
264         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
266         $e1a = $gg->create_content($g1, array('approved' => 1, 'concept' => '0-day', 'userid' => $u1->id));
267         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Bob', 'userid' => 2));
268         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => '1-dayb', 'userid' => $u1->id));
270         $this->setUser($u1);
272         // Requesting a single letter.
273         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'b', 0, 20, array());
274         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
275         $this->assertCount(1, $return['entries']);
276         $this->assertEquals(1, $return['count']);
277         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
279         // Requesting special letters.
280         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'SPECIAL', 0, 20, array());
281         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
282         $this->assertCount(2, $return['entries']);
283         $this->assertEquals(2, $return['count']);
284         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
285         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
287         // Requesting with limit.
288         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 0, 1, array());
289         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
290         $this->assertCount(1, $return['entries']);
291         $this->assertEquals(3, $return['count']);
292         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
293         $return = mod_glossary_external::get_entries_by_letter($g1->id, 'ALL', 1, 2, array());
294         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_letter_returns(), $return);
295         $this->assertCount(2, $return['entries']);
296         $this->assertEquals(3, $return['count']);
297         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
298         $this->assertEquals($e1b->id, $return['entries'][1]['id']);
299     }
301     public function test_get_entries_by_date() {
302         global $DB;
303         $this->resetAfterTest(true);
305         // Generate all the things.
306         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
307         $c1 = $this->getDataGenerator()->create_course();
308         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
309         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
310         $u1 = $this->getDataGenerator()->create_user();
311         $ctx = context_module::instance($g1->cmid);
312         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
314         $now = time();
315         $e1a = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Bob', 'userid' => $u1->id,
316             'timecreated' => 1, 'timemodified' => $now + 3600, 'tags' => array('Cats', 'Dogs')));
317         $e1b = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Jane', 'userid' => $u1->id,
318             'timecreated' => $now + 3600, 'timemodified' => 1));
319         $e1c = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Alice', 'userid' => $u1->id,
320             'timecreated' => $now + 1, 'timemodified' => $now + 1));
321         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id,
322             'timecreated' => $now + 2, 'timemodified' => $now + 2));
323         $e2a = $gg->create_content($g2);
325         $this->setAdminUser($u1);
327         // Ordering by time modified descending.
328         $return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'DESC', 0, 20, array());
329         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
330         $this->assertCount(3, $return['entries']);
331         $this->assertEquals(3, $return['count']);
332         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
333         $this->assertEquals('Cats', $return['entries'][0]['tags'][0]['rawname']);
334         $this->assertEquals('Dogs', $return['entries'][0]['tags'][1]['rawname']);
335         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
336         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
338         // Ordering by time modified ascending.
339         $return = mod_glossary_external::get_entries_by_date($g1->id, 'UPDATE', 'ASC', 0, 20, array());
340         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
341         $this->assertCount(3, $return['entries']);
342         $this->assertEquals(3, $return['count']);
343         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
344         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
345         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
347         // Ordering by time created asc.
348         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20, array());
349         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
350         $this->assertCount(3, $return['entries']);
351         $this->assertEquals(3, $return['count']);
352         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
353         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
354         $this->assertEquals($e1b->id, $return['entries'][2]['id']);
356         // Ordering by time created descending.
357         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'DESC', 0, 20, array());
358         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
359         $this->assertCount(3, $return['entries']);
360         $this->assertEquals(3, $return['count']);
361         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
362         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
363         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
365         // Ordering including to approve.
366         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 20,
367             array('includenotapproved' => true));
368         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
369         $this->assertCount(4, $return['entries']);
370         $this->assertEquals(4, $return['count']);
371         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
372         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
373         $this->assertEquals($e1d->id, $return['entries'][2]['id']);
374         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
376         // Ordering including to approve and pagination.
377         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 0, 2,
378             array('includenotapproved' => true));
379         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
380         $this->assertCount(2, $return['entries']);
381         $this->assertEquals(4, $return['count']);
382         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
383         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
384         $return = mod_glossary_external::get_entries_by_date($g1->id, 'CREATION', 'ASC', 2, 2,
385             array('includenotapproved' => true));
386         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_date_returns(), $return);
387         $this->assertCount(2, $return['entries']);
388         $this->assertEquals(4, $return['count']);
389         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
390         $this->assertEquals($e1b->id, $return['entries'][1]['id']);
391     }
393     public function test_get_categories() {
394         $this->resetAfterTest(true);
395         $this->setAdminUser();
397         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
398         $c1 = $this->getDataGenerator()->create_course();
399         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
400         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
401         $cat1a = $gg->create_category($g1);
402         $cat1b = $gg->create_category($g1);
403         $cat1c = $gg->create_category($g1);
404         $cat2a = $gg->create_category($g2);
406         $return = mod_glossary_external::get_categories($g1->id, 0, 20);
407         $return = external_api::clean_returnvalue(mod_glossary_external::get_categories_returns(), $return);
408         $this->assertCount(3, $return['categories']);
409         $this->assertEquals(3, $return['count']);
410         $this->assertEquals($cat1a->id, $return['categories'][0]['id']);
411         $this->assertEquals($cat1b->id, $return['categories'][1]['id']);
412         $this->assertEquals($cat1c->id, $return['categories'][2]['id']);
414         $return = mod_glossary_external::get_categories($g1->id, 1, 2);
415         $return = external_api::clean_returnvalue(mod_glossary_external::get_categories_returns(), $return);
416         $this->assertCount(2, $return['categories']);
417         $this->assertEquals(3, $return['count']);
418         $this->assertEquals($cat1b->id, $return['categories'][0]['id']);
419         $this->assertEquals($cat1c->id, $return['categories'][1]['id']);
420     }
422     public function test_get_entries_by_category() {
423         $this->resetAfterTest(true);
425         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
426         $c1 = $this->getDataGenerator()->create_course();
427         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
428         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
429         $u1 = $this->getDataGenerator()->create_user();
430         $ctx = context_module::instance($g1->cmid);
432         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'tags' => array('Cats', 'Dogs')));
433         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
434         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
435         $e1b1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
436         $e1b2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
437         $e1x1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
438         $e1x2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
439         $e2a1 = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
440         $e2a2 = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
442         $cat1a = $gg->create_category($g1, array('name' => 'Fish'), array($e1a1, $e1a2, $e1a3));
443         $cat1b = $gg->create_category($g1, array('name' => 'Cat'), array($e1b1, $e1b2));
444         $cat1c = $gg->create_category($g1, array('name' => 'Zebra'), array($e1b1));   // Entry $e1b1 is in two categories.
445         $cat2a = $gg->create_category($g2, array(), array($e2a1, $e2a2));
447         $this->setAdminUser();
449         // Browse one category.
450         $return = mod_glossary_external::get_entries_by_category($g1->id, $cat1a->id, 0, 20, array());
451         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
452         $this->assertCount(3, $return['entries']);
453         $this->assertEquals(3, $return['count']);
454         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
455         $this->assertEquals('Cats', $return['entries'][0]['tags'][0]['rawname']);
456         $this->assertEquals('Dogs', $return['entries'][0]['tags'][1]['rawname']);
457         $this->assertEquals($e1a2->id, $return['entries'][1]['id']);
458         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
460         // Browse all categories.
461         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 0, 20, array());
462         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
463         $this->assertCount(5, $return['entries']);
464         $this->assertEquals(5, $return['count']);
465         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
466         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
467         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
468         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
469         $this->assertEquals($e1b1->id, $return['entries'][4]['id']);
471         // Browse uncategorised.
472         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_NOT_CATEGORISED, 0, 20, array());
473         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
474         $this->assertCount(1, $return['entries']);
475         $this->assertEquals(1, $return['count']);
476         $this->assertEquals($e1x1->id, $return['entries'][0]['id']);
478         // Including to approve.
479         $return = mod_glossary_external::get_entries_by_category($g1->id, $cat1b->id, 0, 20,
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(2, $return['count']);
484         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
485         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
487         // Using limit.
488         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 0, 3,
489             array('includenotapproved' => true));
490         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
491         $this->assertCount(3, $return['entries']);
492         $this->assertEquals(6, $return['count']);
493         $this->assertEquals($e1b1->id, $return['entries'][0]['id']);
494         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
495         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
496         $return = mod_glossary_external::get_entries_by_category($g1->id, GLOSSARY_SHOW_ALL_CATEGORIES, 3, 2,
497             array('includenotapproved' => true));
498         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_category_returns(), $return);
499         $this->assertCount(2, $return['entries']);
500         $this->assertEquals(6, $return['count']);
501         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
502         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
503     }
505     public function test_get_authors() {
506         $this->resetAfterTest(true);
508         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
509         $c1 = $this->getDataGenerator()->create_course();
510         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
511         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
513         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon'));
514         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha'));
515         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Omega'));
517         $ctx = context_module::instance($g1->cmid);
519         $e1a = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
520         $e1b = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
521         $e1c = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1));
522         $e2a = $gg->create_content($g1, array('userid' => $u2->id, 'approved' => 1));
523         $e3a = $gg->create_content($g1, array('userid' => $u3->id, 'approved' => 0));
525         $this->setAdminUser();
527         // Simple request.
528         $return = mod_glossary_external::get_authors($g1->id, 0, 20, array());
529         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
530         $this->assertCount(2, $return['authors']);
531         $this->assertEquals(2, $return['count']);
532         $this->assertEquals($u2->id, $return['authors'][0]['id']);
533         $this->assertEquals($u1->id, $return['authors'][1]['id']);
535         // Include users with entries pending approval.
536         $return = mod_glossary_external::get_authors($g1->id, 0, 20, array('includenotapproved' => true));
537         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
538         $this->assertCount(3, $return['authors']);
539         $this->assertEquals(3, $return['count']);
540         $this->assertEquals($u2->id, $return['authors'][0]['id']);
541         $this->assertEquals($u3->id, $return['authors'][1]['id']);
542         $this->assertEquals($u1->id, $return['authors'][2]['id']);
544         // Pagination.
545         $return = mod_glossary_external::get_authors($g1->id, 1, 1, array('includenotapproved' => true));
546         $return = external_api::clean_returnvalue(mod_glossary_external::get_authors_returns(), $return);
547         $this->assertCount(1, $return['authors']);
548         $this->assertEquals(3, $return['count']);
549         $this->assertEquals($u3->id, $return['authors'][0]['id']);
550     }
552     public function test_get_entries_by_author() {
553         $this->resetAfterTest(true);
555         // Generate all the things.
556         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
557         $c1 = $this->getDataGenerator()->create_course();
558         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
559         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
560         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon', 'firstname' => 'Zac'));
561         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Ultra', 'firstname' => '1337'));
562         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha', 'firstname' => 'Omega'));
563         $u4 = $this->getDataGenerator()->create_user(array('lastname' => '0-day', 'firstname' => 'Zoe'));
564         $ctx = context_module::instance($g1->cmid);
565         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
567         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
568         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
569         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id));
570         $e1b1 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
571         $e1b2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u2->id, 'tags' => array('Cats', 'Dogs')));
572         $e1c1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u3->id));
573         $e1d1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u4->id));
574         $e2a = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
576         $this->setUser($u1);
578         // Requesting a single letter.
579         $return = mod_glossary_external::get_entries_by_author($g1->id, 'u', 'LASTNAME', 'ASC', 0, 20, array());
580         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
581         $this->assertCount(4, $return['entries']);
582         $this->assertEquals(4, $return['count']);
583         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
584         $this->assertEquals('Cats', $return['entries'][0]['tags'][0]['rawname']);
585         $this->assertEquals('Dogs', $return['entries'][0]['tags'][1]['rawname']);
586         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
587         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
588         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
590         // Requesting special letters.
591         $return = mod_glossary_external::get_entries_by_author($g1->id, 'SPECIAL', 'LASTNAME', 'ASC', 0, 20, array());
592         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
593         $this->assertCount(1, $return['entries']);
594         $this->assertEquals(1, $return['count']);
595         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
597         // Requesting with limit.
598         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 0, 1, array());
599         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
600         $this->assertCount(1, $return['entries']);
601         $this->assertEquals(6, $return['count']);
602         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
603         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 1, 2, array());
604         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
605         $this->assertCount(2, $return['entries']);
606         $this->assertEquals(6, $return['count']);
607         $this->assertEquals($e1c1->id, $return['entries'][0]['id']);
608         $this->assertEquals($e1b2->id, $return['entries'][1]['id']);
610         // Including non-approved.
611         $this->setAdminUser();
612         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'ASC', 0, 20,
613             array('includenotapproved' => true));
614         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
615         $this->assertCount(7, $return['entries']);
616         $this->assertEquals(7, $return['count']);
617         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
618         $this->assertEquals($e1c1->id, $return['entries'][1]['id']);
619         $this->assertEquals($e1b1->id, $return['entries'][2]['id']);
620         $this->assertEquals($e1b2->id, $return['entries'][3]['id']);
621         $this->assertEquals($e1a1->id, $return['entries'][4]['id']);
622         $this->assertEquals($e1a2->id, $return['entries'][5]['id']);
623         $this->assertEquals($e1a3->id, $return['entries'][6]['id']);
625         // Changing order.
626         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'LASTNAME', 'DESC', 0, 1, array());
627         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
628         $this->assertCount(1, $return['entries']);
629         $this->assertEquals(6, $return['count']);
630         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
632         // Sorting by firstname.
633         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'FIRSTNAME', 'ASC', 0, 1, array());
634         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
635         $this->assertCount(1, $return['entries']);
636         $this->assertEquals(6, $return['count']);
637         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
639         // Sorting by firstname descending.
640         $return = mod_glossary_external::get_entries_by_author($g1->id, 'ALL', 'FIRSTNAME', 'DESC', 0, 1, array());
641         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
642         $this->assertCount(1, $return['entries']);
643         $this->assertEquals(6, $return['count']);
644         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
646         // Filtering by firstname descending.
647         $return = mod_glossary_external::get_entries_by_author($g1->id, 'z', 'FIRSTNAME', 'DESC', 0, 20, array());
648         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
649         $this->assertCount(4, $return['entries']);
650         $this->assertEquals(4, $return['count']);
651         $this->assertEquals($e1d1->id, $return['entries'][0]['id']);
652         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
653         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
654         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
656         // Test with a deleted user.
657         delete_user($u2);
658         $return = mod_glossary_external::get_entries_by_author($g1->id, 'u', 'LASTNAME', 'ASC', 0, 20, array());
659         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_returns(), $return);
660         $this->assertCount(4, $return['entries']);
661         $this->assertEquals(4, $return['count']);
662         $this->assertEquals($e1b2->id, $return['entries'][0]['id']);
663         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
664         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
665         $this->assertEquals($e1a3->id, $return['entries'][3]['id']);
666     }
668     public function test_get_entries_by_author_id() {
669         $this->resetAfterTest(true);
671         // Generate all the things.
672         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
673         $c1 = $this->getDataGenerator()->create_course();
674         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
675         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'displayformat' => 'entrylist'));
676         $u1 = $this->getDataGenerator()->create_user(array('lastname' => 'Upsilon', 'firstname' => 'Zac'));
677         $u2 = $this->getDataGenerator()->create_user(array('lastname' => 'Ultra', 'firstname' => '1337'));
678         $u3 = $this->getDataGenerator()->create_user(array('lastname' => 'Alpha', 'firstname' => 'Omega'));
679         $u4 = $this->getDataGenerator()->create_user(array('lastname' => '0-day', 'firstname' => 'Zoe'));
680         $ctx = context_module::instance($g1->cmid);
681         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
683         $e1a1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Zoom',
684             'timecreated' => 3600, 'timemodified' => time() - 3600));
685         $e1a2 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Alpha'));
686         $e1a3 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'concept' => 'Dog',
687             'timecreated' => 1, 'timemodified' => time() - 1800));
688         $e1a4 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id, 'concept' => 'Bird'));
689         $e1b1 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
690         $e2a = $gg->create_content($g2, array('approved' => 1, 'userid' => $u1->id));
692         $this->setAdminUser();
694         // Standard request.
695         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', '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($e1a2->id, $return['entries'][0]['id']);
700         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
701         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
703         // Standard request descending.
704         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', '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($e1a1->id, $return['entries'][0]['id']);
709         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
710         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
712         // Requesting ordering by time created.
713         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', '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($e1a3->id, $return['entries'][0]['id']);
718         $this->assertEquals($e1a1->id, $return['entries'][1]['id']);
719         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
721         // Requesting ordering by time created descending.
722         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CREATION', '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($e1a1->id, $return['entries'][1]['id']);
728         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
730         // Requesting ordering by time modified.
731         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'ASC', 0, 20, array());
732         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
733         $this->assertCount(3, $return['entries']);
734         $this->assertEquals(3, $return['count']);
735         $this->assertEquals($e1a1->id, $return['entries'][0]['id']);
736         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
737         $this->assertEquals($e1a2->id, $return['entries'][2]['id']);
739         // Requesting ordering by time modified descending.
740         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'UPDATE', 'DESC', 0, 20, array());
741         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
742         $this->assertCount(3, $return['entries']);
743         $this->assertEquals(3, $return['count']);
744         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
745         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
746         $this->assertEquals($e1a1->id, $return['entries'][2]['id']);
748         // Including non approved.
749         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 20,
750             array('includenotapproved' => true));
751         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
752         $this->assertCount(4, $return['entries']);
753         $this->assertEquals(4, $return['count']);
754         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
755         $this->assertEquals($e1a4->id, $return['entries'][1]['id']);
756         $this->assertEquals($e1a3->id, $return['entries'][2]['id']);
757         $this->assertEquals($e1a1->id, $return['entries'][3]['id']);
759         // Pagination.
760         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 0, 2,
761             array('includenotapproved' => true));
762         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
763         $this->assertCount(2, $return['entries']);
764         $this->assertEquals(4, $return['count']);
765         $this->assertEquals($e1a2->id, $return['entries'][0]['id']);
766         $this->assertEquals($e1a4->id, $return['entries'][1]['id']);
767         $return = mod_glossary_external::get_entries_by_author_id($g1->id, $u1->id, 'CONCEPT', 'ASC', 1, 2,
768             array('includenotapproved' => true));
769         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_author_id_returns(), $return);
770         $this->assertCount(2, $return['entries']);
771         $this->assertEquals(4, $return['count']);
772         $this->assertEquals($e1a4->id, $return['entries'][0]['id']);
773         $this->assertEquals($e1a3->id, $return['entries'][1]['id']);
774     }
776     public function test_get_entries_by_search() {
777         $this->resetAfterTest(true);
779         // Generate all the things.
780         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
781         $c1 = $this->getDataGenerator()->create_course();
782         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
783         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
784         $u1 = $this->getDataGenerator()->create_user();
785         $ctx = context_module::instance($g1->cmid);
786         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
787         $this->setUser($u1);
789         $e1 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'House', 'timecreated' => time() + 3600));
790         $e2 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Mouse', 'timemodified' => 1));
791         $e3 = $gg->create_content($g1, array('approved' => 1, 'concept' => 'Hero', 'tags' => array('Cats', 'Dogs')));
792         $e4 = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Toulouse'));
793         $e5 = $gg->create_content($g1, array('approved' => 1, 'definition' => 'Heroes', 'concept' => 'Abcd'));
794         $e6 = $gg->create_content($g1, array('approved' => 0, 'definition' => 'When used for Heroes'));
795         $e7 = $gg->create_content($g1, array('approved' => 1, 'timecreated' => 1, 'timemodified' => time() + 3600,
796             'concept' => 'Z'), array('Couscous'));
797         $e8 = $gg->create_content($g1, array('approved' => 0), array('Heroes'));
798         $e9 = $gg->create_content($g2, array('approved' => 0));
800         $this->setAdminUser();
802         // Test simple query.
803         $query = 'hero';
804         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20, array());
805         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
806         $this->assertCount(1, $return['entries']);
807         $this->assertEquals(1, $return['count']);
808         $this->assertEquals($e3->id, $return['entries'][0]['id']);
809         $this->assertEquals('Cats', $return['entries'][0]['tags'][0]['rawname']);
810         $this->assertEquals('Dogs', $return['entries'][0]['tags'][1]['rawname']);
812         // Enabling full search.
813         $query = 'hero';
814         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20, array());
815         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
816         $this->assertCount(2, $return['entries']);
817         $this->assertEquals(2, $return['count']);
818         $this->assertEquals($e5->id, $return['entries'][0]['id']);
819         $this->assertEquals($e3->id, $return['entries'][1]['id']);
821         // Concept descending.
822         $query = 'hero';
823         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'DESC', 0, 20, array());
824         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
825         $this->assertCount(2, $return['entries']);
826         $this->assertEquals(2, $return['count']);
827         $this->assertEquals($e3->id, $return['entries'][0]['id']);
828         $this->assertEquals($e5->id, $return['entries'][1]['id']);
830         // Search on alias.
831         $query = 'couscous';
832         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20, array());
833         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
834         $this->assertCount(1, $return['entries']);
835         $this->assertEquals(1, $return['count']);
836         $this->assertEquals($e7->id, $return['entries'][0]['id']);
837         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20, 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(1, $return['count']);
841         $this->assertEquals($e7->id, $return['entries'][0]['id']);
843         // Pagination and ordering on created date.
844         $query = 'ou';
845         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CREATION', 'ASC', 0, 1, array());
846         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
847         $this->assertCount(1, $return['entries']);
848         $this->assertEquals(3, $return['count']);
849         $this->assertEquals($e7->id, $return['entries'][0]['id']);
850         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CREATION', 'DESC', 0, 1, array());
851         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
852         $this->assertCount(1, $return['entries']);
853         $this->assertEquals(3, $return['count']);
854         $this->assertEquals($e1->id, $return['entries'][0]['id']);
856         // Ordering on updated date.
857         $query = 'ou';
858         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'UPDATE', 'ASC', 0, 1, array());
859         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
860         $this->assertCount(1, $return['entries']);
861         $this->assertEquals(3, $return['count']);
862         $this->assertEquals($e2->id, $return['entries'][0]['id']);
863         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'UPDATE', 'DESC', 0, 1, array());
864         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
865         $this->assertCount(1, $return['entries']);
866         $this->assertEquals(3, $return['count']);
867         $this->assertEquals($e7->id, $return['entries'][0]['id']);
869         // Including not approved.
870         $query = 'ou';
871         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, false, 'CONCEPT', 'ASC', 0, 20,
872             array('includenotapproved' => true));
873         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
874         $this->assertCount(4, $return['entries']);
875         $this->assertEquals(4, $return['count']);
876         $this->assertEquals($e1->id, $return['entries'][0]['id']);
877         $this->assertEquals($e2->id, $return['entries'][1]['id']);
878         $this->assertEquals($e4->id, $return['entries'][2]['id']);
879         $this->assertEquals($e7->id, $return['entries'][3]['id']);
881         // Advanced query string.
882         $query = '+Heroes -Abcd';
883         $return = mod_glossary_external::get_entries_by_search($g1->id, $query, true, 'CONCEPT', 'ASC', 0, 20,
884             array('includenotapproved' => true));
885         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_search_returns(), $return);
886         $this->assertCount(2, $return['entries']);
887         $this->assertEquals(2, $return['count']);
888         $this->assertEquals($e6->id, $return['entries'][0]['id']);
889         $this->assertEquals($e8->id, $return['entries'][1]['id']);
890     }
892     public function test_get_entries_by_term() {
893         $this->resetAfterTest(true);
895         // Generate all the things.
896         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
897         $c1 = $this->getDataGenerator()->create_course();
898         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
899         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
900         $u1 = $this->getDataGenerator()->create_user();
901         $ctx = context_module::instance($g1->cmid);
902         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
904         $this->setAdminUser();
906         $e1 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1, 'concept' => 'cat',
907             'tags' => array('Cats', 'Dogs')));
908         $e2 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1), array('cat', 'dog'));
909         $e3 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 1), array('dog'));
910         $e4 = $gg->create_content($g1, array('userid' => $u1->id, 'approved' => 0, 'concept' => 'dog'));
911         $e5 = $gg->create_content($g2, array('userid' => $u1->id, 'approved' => 1, 'concept' => 'dog'), array('cat'));
913         // Search concept + alias.
914         $return = mod_glossary_external::get_entries_by_term($g1->id, 'cat', 0, 20, array('includenotapproved' => false));
915         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
916         $this->assertCount(2, $return['entries']);
917         $this->assertEquals(2, $return['count']);
918         // Compare ids, ignore ordering of array, using canonicalize parameter of assertEquals.
919         $expected = array($e1->id, $e2->id);
920         $actual = array($return['entries'][0]['id'], $return['entries'][1]['id']);
921         $this->assertEquals($expected, $actual, '', 0.0, 10, true);
922         $this->assertEquals('Cats', $return['entries'][0]['tags'][0]['rawname']);
923         $this->assertEquals('Dogs', $return['entries'][0]['tags'][1]['rawname']);
925         // Search alias.
926         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 20, array('includenotapproved' => false));
927         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
929         $this->assertCount(2, $return['entries']);
930         $this->assertEquals(2, $return['count']);
931         // Compare ids, ignore ordering of array, using canonicalize parameter of assertEquals.
932         $expected = array($e2->id, $e3->id);
933         $actual = array($return['entries'][0]['id'], $return['entries'][1]['id']);
934         $this->assertEquals($expected, $actual, '', 0.0, 10, true);
936         // Search including not approved.
937         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 20, array('includenotapproved' => true));
938         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
939         $this->assertCount(3, $return['entries']);
940         $this->assertEquals(3, $return['count']);
941         // Compare ids, ignore ordering of array, using canonicalize parameter of assertEquals.
942         $expected = array($e4->id, $e2->id, $e3->id);
943         $actual = array($return['entries'][0]['id'], $return['entries'][1]['id'], $return['entries'][2]['id']);
944         $this->assertEquals($expected, $actual, '', 0.0, 10, true);
946         // Pagination.
947         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 0, 1, array('includenotapproved' => true));
948         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
949         $this->assertCount(1, $return['entries']);
950         // We don't compare the returned entry id because it may be different depending on the DBMS,
951         // for example, Postgres does a random sorting in this case.
952         $this->assertEquals(3, $return['count']);
953         $return = mod_glossary_external::get_entries_by_term($g1->id, 'dog', 1, 1, array('includenotapproved' => true));
954         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_by_term_returns(), $return);
955         $this->assertCount(1, $return['entries']);
956         $this->assertEquals(3, $return['count']);
957     }
959     public function test_get_entries_to_approve() {
960         $this->resetAfterTest(true);
962         // Generate all the things.
963         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
964         $c1 = $this->getDataGenerator()->create_course();
965         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
966         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
967         $u1 = $this->getDataGenerator()->create_user();
968         $ctx = context_module::instance($g1->cmid);
969         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
971         $e1a = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Bob', 'userid' => $u1->id,
972             'timecreated' => time() + 3600));
973         $e1b = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Jane', 'userid' => $u1->id, 'timecreated' => 1));
974         $e1c = $gg->create_content($g1, array('approved' => 0, 'concept' => 'Alice', 'userid' => $u1->id, 'timemodified' => 1));
975         $e1d = $gg->create_content($g1, array('approved' => 0, 'concept' => '0-day', 'userid' => $u1->id,
976             'timemodified' => time() + 3600));
977         $e1e = $gg->create_content($g1, array('approved' => 1, 'concept' => '1-day', 'userid' => $u1->id));
978         $e2a = $gg->create_content($g2);
980         $this->setAdminUser(true);
982         // Simple listing.
983         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 0, 20);
984         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
985         $this->assertCount(4, $return['entries']);
986         $this->assertEquals(4, $return['count']);
987         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
988         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
989         $this->assertEquals($e1a->id, $return['entries'][2]['id']);
990         $this->assertEquals($e1b->id, $return['entries'][3]['id']);
992         // Revert ordering of concept.
993         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'DESC', 0, 20);
994         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
995         $this->assertCount(4, $return['entries']);
996         $this->assertEquals(4, $return['count']);
997         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
998         $this->assertEquals($e1a->id, $return['entries'][1]['id']);
999         $this->assertEquals($e1c->id, $return['entries'][2]['id']);
1000         $this->assertEquals($e1d->id, $return['entries'][3]['id']);
1002         // Filtering by letter.
1003         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'a', 'CONCEPT', 'ASC', 0, 20);
1004         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1005         $this->assertCount(1, $return['entries']);
1006         $this->assertEquals(1, $return['count']);
1007         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
1009         // Filtering by special.
1010         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'SPECIAL', 'CONCEPT', 'ASC', 0, 20);
1011         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1012         $this->assertCount(1, $return['entries']);
1013         $this->assertEquals(1, $return['count']);
1014         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
1016         // Pagination.
1017         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 0, 2);
1018         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1019         $this->assertCount(2, $return['entries']);
1020         $this->assertEquals(4, $return['count']);
1021         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
1022         $this->assertEquals($e1c->id, $return['entries'][1]['id']);
1023         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 1, 2);
1024         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1025         $this->assertCount(2, $return['entries']);
1026         $this->assertEquals(4, $return['count']);
1027         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
1028         $this->assertEquals($e1a->id, $return['entries'][1]['id']);
1030         // Ordering by creation date.
1031         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CREATION', 'ASC', 0, 1);
1032         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1033         $this->assertCount(1, $return['entries']);
1034         $this->assertEquals(4, $return['count']);
1035         $this->assertEquals($e1b->id, $return['entries'][0]['id']);
1037         // Ordering by creation date desc.
1038         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CREATION', 'DESC', 0, 1);
1039         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1040         $this->assertCount(1, $return['entries']);
1041         $this->assertEquals(4, $return['count']);
1042         $this->assertEquals($e1a->id, $return['entries'][0]['id']);
1044         // Ordering by update date.
1045         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'UPDATE', 'ASC', 0, 1);
1046         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1047         $this->assertCount(1, $return['entries']);
1048         $this->assertEquals(4, $return['count']);
1049         $this->assertEquals($e1c->id, $return['entries'][0]['id']);
1051         // Ordering by update date desc.
1052         $return = mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'UPDATE', 'DESC', 0, 1);
1053         $return = external_api::clean_returnvalue(mod_glossary_external::get_entries_to_approve_returns(), $return);
1054         $this->assertCount(1, $return['entries']);
1055         $this->assertEquals(4, $return['count']);
1056         $this->assertEquals($e1d->id, $return['entries'][0]['id']);
1058         // Permissions are checked.
1059         $this->setUser($u1);
1060         $this->expectException('required_capability_exception');
1061         mod_glossary_external::get_entries_to_approve($g1->id, 'ALL', 'CONCEPT', 'ASC', 0, 1);
1062         $this->fail('Do not test anything else after this.');
1063     }
1065     public function test_get_entry_by_id() {
1066         $this->resetAfterTest(true);
1068         // Generate all the things.
1069         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
1070         $c1 = $this->getDataGenerator()->create_course();
1071         $c2 = $this->getDataGenerator()->create_course();
1072         $g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
1073         $g2 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id, 'visible' => 0));
1074         $u1 = $this->getDataGenerator()->create_user();
1075         $u2 = $this->getDataGenerator()->create_user();
1076         $ctx = context_module::instance($g1->cmid);
1077         $this->getDataGenerator()->enrol_user($u1->id, $c1->id);
1079         $e1 = $gg->create_content($g1, array('approved' => 1, 'userid' => $u1->id, 'tags' => array('Cats', 'Dogs')));
1080         // Add a fake inline image to the entry.
1081         $filename = 'shouldbeanimage.jpg';
1082         $filerecordinline = array(
1083             'contextid' => $ctx->id,
1084             'component' => 'mod_glossary',
1085             'filearea'  => 'entry',
1086             'itemid'    => $e1->id,
1087             'filepath'  => '/',
1088             'filename'  => $filename,
1089         );
1090         $fs = get_file_storage();
1091         $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
1093         $e2 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u1->id));
1094         $e3 = $gg->create_content($g1, array('approved' => 0, 'userid' => $u2->id));
1095         $e4 = $gg->create_content($g2, array('approved' => 1));
1097         $this->setUser($u1);
1098         $return = mod_glossary_external::get_entry_by_id($e1->id);
1099         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
1100         $this->assertEquals($e1->id, $return['entry']['id']);
1101         $this->assertEquals('Cats', $return['entry']['tags'][0]['rawname']);
1102         $this->assertEquals('Dogs', $return['entry']['tags'][1]['rawname']);
1103         $this->assertEquals($filename, $return['entry']['definitioninlinefiles'][0]['filename']);
1105         $return = mod_glossary_external::get_entry_by_id($e2->id);
1106         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
1107         $this->assertEquals($e2->id, $return['entry']['id']);
1109         try {
1110             $return = mod_glossary_external::get_entry_by_id($e3->id);
1111             $this->fail('Cannot view unapproved entries of others.');
1112         } catch (invalid_parameter_exception $e) {
1113             // All good.
1114         }
1116         try {
1117             $return = mod_glossary_external::get_entry_by_id($e4->id);
1118             $this->fail('Cannot view entries from another course.');
1119         } catch (require_login_exception $e) {
1120             // All good.
1121         }
1123         // An admin can be other's entries to be approved.
1124         $this->setAdminUser();
1125         $return = mod_glossary_external::get_entry_by_id($e3->id);
1126         $return = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $return);
1127         $this->assertEquals($e3->id, $return['entry']['id']);
1128     }
1130     public function test_add_entry_without_optional_settings() {
1131         global $CFG, $DB;
1132         $this->resetAfterTest(true);
1134         $course = $this->getDataGenerator()->create_course();
1135         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
1137         $this->setAdminUser();
1138         $concept = 'A concept';
1139         $definition = '<p>A definition</p>';
1140         $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML);
1141         $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
1143         // Get entry from DB.
1144         $entry = $DB->get_record('glossary_entries', array('id' => $return['entryid']));
1146         $this->assertEquals($concept, $entry->concept);
1147         $this->assertEquals($definition, $entry->definition);
1148         $this->assertEquals($CFG->glossary_linkentries, $entry->usedynalink);
1149         $this->assertEquals($CFG->glossary_casesensitive, $entry->casesensitive);
1150         $this->assertEquals($CFG->glossary_fullmatch, $entry->fullmatch);
1151         $this->assertEmpty($DB->get_records('glossary_alias', array('entryid' => $return['entryid'])));
1152         $this->assertEmpty($DB->get_records('glossary_entries_categories', array('entryid' => $return['entryid'])));
1153     }
1155     public function test_add_entry_with_aliases() {
1156         global $DB;
1157         $this->resetAfterTest(true);
1159         $course = $this->getDataGenerator()->create_course();
1160         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
1162         $this->setAdminUser();
1163         $concept = 'A concept';
1164         $definition = 'A definition';
1165         $paramaliases = 'abc, def, gez';
1166         $options = array(
1167             array(
1168                 'name' => 'aliases',
1169                 'value' => $paramaliases,
1170             )
1171         );
1172         $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
1173         $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
1175         $aliases = $DB->get_records('glossary_alias', array('entryid' => $return['entryid']));
1176         $this->assertCount(3, $aliases);
1177         foreach ($aliases as $alias) {
1178             $this->assertContains($alias->alias, $paramaliases);
1179         }
1180     }
1182     public function test_add_entry_in_categories() {
1183         global $DB;
1184         $this->resetAfterTest(true);
1186         $course = $this->getDataGenerator()->create_course();
1187         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
1188         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
1189         $cat1 = $gg->create_category($glossary);
1190         $cat2 = $gg->create_category($glossary);
1192         $this->setAdminUser();
1193         $concept = 'A concept';
1194         $definition = 'A definition';
1195         $paramcategories = "$cat1->id, $cat2->id";
1196         $options = array(
1197             array(
1198                 'name' => 'categories',
1199                 'value' => $paramcategories,
1200             )
1201         );
1202         $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
1203         $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
1205         $categories = $DB->get_records('glossary_entries_categories', array('entryid' => $return['entryid']));
1206         $this->assertCount(2, $categories);
1207         foreach ($categories as $category) {
1208             $this->assertContains($category->categoryid, $paramcategories);
1209         }
1210     }
1212     public function test_add_entry_with_attachments() {
1213         global $DB, $USER;
1214         $this->resetAfterTest(true);
1216         $course = $this->getDataGenerator()->create_course();
1217         $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
1218         $context = context_module::instance($glossary->cmid);
1220         $this->setAdminUser();
1221         $concept = 'A concept';
1222         $definition = 'A definition';
1224         // Draft files.
1225         $draftidinlineattach = file_get_unused_draft_itemid();
1226         $draftidattach = file_get_unused_draft_itemid();
1227         $usercontext = context_user::instance($USER->id);
1228         $filerecordinline = array(
1229             'contextid' => $usercontext->id,
1230             'component' => 'user',
1231             'filearea'  => 'draft',
1232             'itemid'    => $draftidinlineattach,
1233             'filepath'  => '/',
1234             'filename'  => 'shouldbeanimage.txt',
1235         );
1236         $fs = get_file_storage();
1238         // Create a file in a draft area for regular attachments.
1239         $filerecordattach = $filerecordinline;
1240         $attachfilename = 'attachment.txt';
1241         $filerecordattach['filename'] = $attachfilename;
1242         $filerecordattach['itemid'] = $draftidattach;
1243         $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
1244         $fs->create_file_from_string($filerecordattach, 'simple text attachment');
1246         $options = array(
1247             array(
1248                 'name' => 'inlineattachmentsid',
1249                 'value' => $draftidinlineattach,
1250             ),
1251             array(
1252                 'name' => 'attachmentsid',
1253                 'value' => $draftidattach,
1254             )
1255         );
1256         $return = mod_glossary_external::add_entry($glossary->id, $concept, $definition, FORMAT_HTML, $options);
1257         $return = external_api::clean_returnvalue(mod_glossary_external::add_entry_returns(), $return);
1259         $editorfiles = external_util::get_area_files($context->id, 'mod_glossary', 'entry', $return['entryid']);
1260         $attachmentfiles = external_util::get_area_files($context->id, 'mod_glossary', 'attachment', $return['entryid']);
1262         $this->assertCount(1, $editorfiles);
1263         $this->assertCount(1, $attachmentfiles);
1265         $this->assertEquals('shouldbeanimage.txt', $editorfiles[0]['filename']);
1266         $this->assertEquals('attachment.txt', $attachmentfiles[0]['filename']);
1267     }
1269     /**
1270      *   Test get entry including rating information.
1271      */
1272     public function test_get_entry_rating_information() {
1273         $this->resetAfterTest(true);
1275         global $DB, $CFG;
1276         require_once($CFG->dirroot . '/rating/lib.php');
1278         $this->resetAfterTest(true);
1280         $user1 = self::getDataGenerator()->create_user();
1281         $user2 = self::getDataGenerator()->create_user();
1282         $user3 = self::getDataGenerator()->create_user();
1283         $teacher = self::getDataGenerator()->create_user();
1285         // Create course to add the module.
1286         $course = self::getDataGenerator()->create_course();
1288         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
1289         $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
1290         $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual');
1291         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id, 'manual');
1292         $this->getDataGenerator()->enrol_user($user3->id, $course->id, $studentrole->id, 'manual');
1293         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
1295         // Create the glossary and contents.
1296         $record = new stdClass();
1297         $record->course = $course->id;
1298         $record->assessed = RATING_AGGREGATE_AVERAGE;
1299         $scale = $this->getDataGenerator()->create_scale(array('scale' => 'A,B,C,D'));
1300         $record->scale = "-$scale->id";
1301         $glossary = $this->getDataGenerator()->create_module('glossary', $record);
1302         $context = context_module::instance($glossary->cmid);
1304         $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
1305         $entry = $gg->create_content($glossary, array('approved' => 1, 'userid' => $user1->id));
1307         // Rate the entry as user2.
1308         $rating1 = new stdClass();
1309         $rating1->contextid = $context->id;
1310         $rating1->component = 'mod_glossary';
1311         $rating1->ratingarea = 'entry';
1312         $rating1->itemid = $entry->id;
1313         $rating1->rating = 1; // 1 is A.
1314         $rating1->scaleid = "-$scale->id";
1315         $rating1->userid = $user2->id;
1316         $rating1->timecreated = time();
1317         $rating1->timemodified = time();
1318         $rating1->id = $DB->insert_record('rating', $rating1);
1320         // Rate the entry as user3.
1321         $rating2 = new stdClass();
1322         $rating2->contextid = $context->id;
1323         $rating2->component = 'mod_glossary';
1324         $rating2->ratingarea = 'entry';
1325         $rating2->itemid = $entry->id;
1326         $rating2->rating = 3; // 3 is C.
1327         $rating2->scaleid = "-$scale->id";
1328         $rating2->userid = $user3->id;
1329         $rating2->timecreated = time() + 1;
1330         $rating2->timemodified = time() + 1;
1331         $rating2->id = $DB->insert_record('rating', $rating2);
1333         // As student, retrieve ratings information.
1334         $this->setUser($user1);
1335         $result = mod_glossary_external::get_entry_by_id($entry->id);
1336         $result = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $result);
1337         $this->assertCount(1, $result['ratinginfo']['ratings']);
1338         $this->assertFalse($result['ratinginfo']['ratings'][0]['canviewaggregate']);
1339         $this->assertFalse($result['ratinginfo']['canviewall']);
1340         $this->assertFalse($result['ratinginfo']['ratings'][0]['canrate']);
1341         $this->assertTrue(!isset($result['ratinginfo']['ratings'][0]['count']));
1343         // Now, as teacher, I should see the info correctly.
1344         $this->setUser($teacher);
1345         $result = mod_glossary_external::get_entry_by_id($entry->id);
1346         $result = external_api::clean_returnvalue(mod_glossary_external::get_entry_by_id_returns(), $result);
1347         $this->assertCount(1, $result['ratinginfo']['ratings']);
1348         $this->assertTrue($result['ratinginfo']['ratings'][0]['canviewaggregate']);
1349         $this->assertTrue($result['ratinginfo']['canviewall']);
1350         $this->assertTrue($result['ratinginfo']['ratings'][0]['canrate']);
1351         $this->assertEquals(2, $result['ratinginfo']['ratings'][0]['count']);
1352         $this->assertEquals(2, $result['ratinginfo']['ratings'][0]['aggregate']);   // 2 is B, that is the average of A + C.
1353     }