4e94bf4dab2d6ed69192333ca340c5c7477ca254
[moodle.git] / mod / glossary / tests / externallib_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/>.
16 /**
17  * External glossary functions unit tests
18  *
19  * @package    mod_glossary
20  * @category   external
21  * @copyright  2015 Costantino Cito <ccito@cvaconsulting.com>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
24 defined('MOODLE_INTERNAL') || die();
25 global $CFG;
26 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
27 /**
28  * External glossary functions unit tests
29  *
30  * @package    mod_glossary
31  * @category   external
32  * @copyright  2015 Costantino Cito <ccito@cvaconsulting.com>
33  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  */
35 class mod_glossary_externallib_testcase extends externallib_advanced_testcase {
36     /**
37      * Test get_glossaries_by_courses
38      */
39     public function test_get_glossaries_by_courses() {
40         global $DB, $USER;
41         $this->resetAfterTest(true);
42         // As admin.
43         $this->setAdminUser();
44         $course1 = self::getDataGenerator()->create_course();
45         $glossaryoptions1 = array(
46                               'course' => $course1->id,
47                               'name' => 'First Glossary'
48                              );
49         $glossary1 = self::getDataGenerator()->create_module('glossary', $glossaryoptions1);
50         $course2 = self::getDataGenerator()->create_course();
51         $glossaryoptions2 = array(
52                               'course' => $course2->id,
53                               'name' => 'Second Glossary'
54                              );
55         $glossary2 = self::getDataGenerator()->create_module('glossary', $glossaryoptions2);
56         $student1 = $this->getDataGenerator()->create_user();
57         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
58         // Enroll Student1 in Course1.
59         self::getDataGenerator()->enrol_user($student1->id,  $course1->id, $studentrole->id);
60         $this->setUser($student1);
61         $glossaries = mod_glossary_external::get_glossaries_by_courses(array());
62         // We need to execute the return values cleaning process to simulate the web service server.
63         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
64         $this->assertCount(1, $glossaries['glossaries']);
65         $this->assertEquals('First Glossary', $glossaries['glossaries'][0]['name']);
66         // As Student you cannot see some glossary properties like 'showunanswered'.
67         $this->assertFalse(isset($glossaries['glossaries'][0]['section']));
68         // Student1 is not enrolled in this Course.
69         // The webservice will give a warning!
70         $glossaries = mod_glossary_external::get_glossaries_by_courses(array($course2->id));
71         // We need to execute the return values cleaning process to simulate the web service server.
72         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
73         $this->assertCount(0, $glossaries['glossaries']);
74         $this->assertEquals(1, $glossaries['warnings'][0]['warningcode']);
75         // Now as admin.
76         $this->setAdminUser();
77         // As Admin we can see this glossary.
78         $glossaries = mod_glossary_external::get_glossaries_by_courses(array($course2->id));
79         // We need to execute the return values cleaning process to simulate the web service server.
80         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
81         $this->assertCount(1, $glossaries['glossaries']);
82         $this->assertEquals('Second Glossary', $glossaries['glossaries'][0]['name']);
83         // As an Admin you can see some glossary properties like 'section'.
84         $this->assertEquals(0, $glossaries['glossaries'][0]['section']);
85         $this->setUser($student1);
86         // Prohibit capability = mod:glossary:view on Course1 for students.
87         $contextcourse1 = context_course::instance($course1->id);
88         assign_capability('mod/glossary:view', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id);
89         accesslib_clear_all_caches_for_unit_testing();
90         $glossaries = mod_glossary_external::get_glossaries_by_courses(array($course1->id));
91         $glossaries = external_api::clean_returnvalue(mod_glossary_external::get_glossaries_by_courses_returns(), $glossaries);
92         $this->assertEquals(2, $glossaries['warnings'][0]['warningcode']);
93     }
94 }