| 1 | efrain | 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 | namespace mod_imscp;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use core_external\external_api;
 | 
        
           |  |  | 20 | use externallib_advanced_testcase;
 | 
        
           |  |  | 21 | use mod_imscp_external;
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | global $CFG;
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | require_once($CFG->dirroot . '/webservice/tests/helpers.php');
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * External mod_imscp functions unit tests
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @package    mod_imscp
 | 
        
           |  |  | 33 |  * @category   external
 | 
        
           |  |  | 34 |  * @copyright  2015 Juan Leyva <juan@moodle.com>
 | 
        
           |  |  | 35 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 36 |  * @since      Moodle 3.0
 | 
        
           |  |  | 37 |  */
 | 
        
           |  |  | 38 | class externallib_test extends externallib_advanced_testcase {
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Test view_imscp
 | 
        
           |  |  | 42 |      */
 | 
        
           | 11 | efrain | 43 |     public function test_view_imscp(): void {
 | 
        
           | 1 | efrain | 44 |         global $DB;
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |         $this->setAdminUser();
 | 
        
           |  |  | 49 |         // Setup test data.
 | 
        
           |  |  | 50 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 51 |         $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
 | 
        
           |  |  | 52 |         $context = \context_module::instance($imscp->cmid);
 | 
        
           |  |  | 53 |         $cm = get_coursemodule_from_instance('imscp', $imscp->id);
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |         // Test invalid instance id.
 | 
        
           |  |  | 56 |         try {
 | 
        
           |  |  | 57 |             mod_imscp_external::view_imscp(0);
 | 
        
           |  |  | 58 |             $this->fail('Exception expected due to invalid mod_imscp instance id.');
 | 
        
           |  |  | 59 |         } catch (\moodle_exception $e) {
 | 
        
           |  |  | 60 |             $this->assertEquals('invalidrecord', $e->errorcode);
 | 
        
           |  |  | 61 |         }
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         // Test not-enrolled user.
 | 
        
           |  |  | 64 |         $user = self::getDataGenerator()->create_user();
 | 
        
           |  |  | 65 |         $this->setUser($user);
 | 
        
           |  |  | 66 |         try {
 | 
        
           |  |  | 67 |             mod_imscp_external::view_imscp($imscp->id);
 | 
        
           |  |  | 68 |             $this->fail('Exception expected due to not enrolled user.');
 | 
        
           |  |  | 69 |         } catch (\moodle_exception $e) {
 | 
        
           |  |  | 70 |             $this->assertEquals('requireloginerror', $e->errorcode);
 | 
        
           |  |  | 71 |         }
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |         // Test user with full capabilities.
 | 
        
           |  |  | 74 |         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 | 
        
           |  |  | 75 |         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |         // Trigger and capture the event.
 | 
        
           |  |  | 78 |         $sink = $this->redirectEvents();
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         $result = mod_imscp_external::view_imscp($imscp->id);
 | 
        
           |  |  | 81 |         $result = external_api::clean_returnvalue(mod_imscp_external::view_imscp_returns(), $result);
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |         $events = $sink->get_events();
 | 
        
           |  |  | 84 |         $this->assertCount(1, $events);
 | 
        
           |  |  | 85 |         $event = array_shift($events);
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |         // Checking that the event contains the expected values.
 | 
        
           |  |  | 88 |         $this->assertInstanceOf('\mod_imscp\event\course_module_viewed', $event);
 | 
        
           |  |  | 89 |         $this->assertEquals($context, $event->get_context());
 | 
        
           |  |  | 90 |         $moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id));
 | 
        
           |  |  | 91 |         $this->assertEquals($moodleurl, $event->get_url());
 | 
        
           |  |  | 92 |         $this->assertEventContextNotUsed($event);
 | 
        
           |  |  | 93 |         $this->assertNotEmpty($event->get_name());
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |         // Test user with no capabilities.
 | 
        
           |  |  | 96 |         // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
 | 
        
           |  |  | 97 |         assign_capability('mod/imscp:view', CAP_PROHIBIT, $studentrole->id, $context->id);
 | 
        
           |  |  | 98 |         // Empty all the caches that may be affected by this change.
 | 
        
           |  |  | 99 |         accesslib_clear_all_caches_for_unit_testing();
 | 
        
           |  |  | 100 |         \course_modinfo::clear_instance_cache();
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |         try {
 | 
        
           |  |  | 103 |             mod_imscp_external::view_imscp($imscp->id);
 | 
        
           |  |  | 104 |             $this->fail('Exception expected due to missing capability.');
 | 
        
           |  |  | 105 |         } catch (\moodle_exception $e) {
 | 
        
           |  |  | 106 |             $this->assertEquals('requireloginerror', $e->errorcode);
 | 
        
           |  |  | 107 |         }
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |     }
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |     /**
 | 
        
           |  |  | 112 |      * Test get_imscps_by_courses
 | 
        
           |  |  | 113 |      */
 | 
        
           | 11 | efrain | 114 |     public function test_get_imscps_by_courses(): void {
 | 
        
           | 1 | efrain | 115 |         global $DB, $USER;
 | 
        
           |  |  | 116 |         $this->resetAfterTest(true);
 | 
        
           |  |  | 117 |         // As admin.
 | 
        
           |  |  | 118 |         $this->setAdminUser();
 | 
        
           |  |  | 119 |         $course1 = self::getDataGenerator()->create_course();
 | 
        
           |  |  | 120 |         $imscpoptions1 = array(
 | 
        
           |  |  | 121 |           'course' => $course1->id,
 | 
        
           |  |  | 122 |           'name' => 'First IMSCP'
 | 
        
           |  |  | 123 |         );
 | 
        
           |  |  | 124 |         $imscp1 = self::getDataGenerator()->create_module('imscp', $imscpoptions1);
 | 
        
           |  |  | 125 |         $course2 = self::getDataGenerator()->create_course();
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |         $imscpoptions2 = array(
 | 
        
           |  |  | 128 |           'course' => $course2->id,
 | 
        
           |  |  | 129 |           'name' => 'Second IMSCP'
 | 
        
           |  |  | 130 |         );
 | 
        
           |  |  | 131 |         $imscp2 = self::getDataGenerator()->create_module('imscp', $imscpoptions2);
 | 
        
           |  |  | 132 |         $student1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 | 
        
           |  |  | 135 |   | 
        
           |  |  | 136 |         // Enroll Student1 in Course1.
 | 
        
           |  |  | 137 |         self::getDataGenerator()->enrol_user($student1->id,  $course1->id, $studentrole->id);
 | 
        
           |  |  | 138 |   | 
        
           |  |  | 139 |         $this->setUser($student1);
 | 
        
           |  |  | 140 |         $imscps = mod_imscp_external::get_imscps_by_courses(array());
 | 
        
           |  |  | 141 |         $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 | 
        
           |  |  | 142 |         $this->assertCount(1, $imscps['imscps']);
 | 
        
           |  |  | 143 |         $this->assertEquals('First IMSCP', $imscps['imscps'][0]['name']);
 | 
        
           |  |  | 144 |         // As Student you cannot see some IMSCP properties like 'section'.
 | 
        
           |  |  | 145 |         $this->assertFalse(isset($imscps['imscps'][0]['section']));
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         // Student1 is not enrolled in this Course.
 | 
        
           |  |  | 148 |         // The webservice will give a warning!
 | 
        
           |  |  | 149 |         $imscps = mod_imscp_external::get_imscps_by_courses(array($course2->id));
 | 
        
           |  |  | 150 |         $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 | 
        
           |  |  | 151 |         $this->assertCount(0, $imscps['imscps']);
 | 
        
           |  |  | 152 |         $this->assertEquals(1, $imscps['warnings'][0]['warningcode']);
 | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 |         // Now as admin.
 | 
        
           |  |  | 155 |         $this->setAdminUser();
 | 
        
           |  |  | 156 |         // As Admin we can see this IMSCP.
 | 
        
           |  |  | 157 |         $imscps = mod_imscp_external::get_imscps_by_courses(array($course2->id));
 | 
        
           |  |  | 158 |         $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 | 
        
           |  |  | 159 |         $this->assertCount(1, $imscps['imscps']);
 | 
        
           |  |  | 160 |         $this->assertEquals('Second IMSCP', $imscps['imscps'][0]['name']);
 | 
        
           |  |  | 161 |         // As an Admin you can see some IMSCP properties like 'section'.
 | 
        
           |  |  | 162 |         $this->assertEquals(0, $imscps['imscps'][0]['section']);
 | 
        
           |  |  | 163 |   | 
        
           |  |  | 164 |         // Now, prohibit capabilities.
 | 
        
           |  |  | 165 |         $this->setUser($student1);
 | 
        
           |  |  | 166 |         $contextcourse1 = \context_course::instance($course1->id);
 | 
        
           |  |  | 167 |         // Prohibit capability = mod:imscp:view on Course1 for students.
 | 
        
           |  |  | 168 |         assign_capability('mod/imscp:view', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id);
 | 
        
           |  |  | 169 |         // Empty all the caches that may be affected by this change.
 | 
        
           |  |  | 170 |         accesslib_clear_all_caches_for_unit_testing();
 | 
        
           |  |  | 171 |         \course_modinfo::clear_instance_cache();
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 |         $imscps = mod_imscp_external::get_imscps_by_courses(array($course1->id));
 | 
        
           |  |  | 174 |         $imscps = external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 | 
        
           |  |  | 175 |         $this->assertCount(0, $imscps['imscps']);
 | 
        
           |  |  | 176 |     }
 | 
        
           |  |  | 177 | }
 |