Differences Between: [Versions 400 and 402] [Versions 401 and 402]
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 enrol_meta\external; 18 19 use core_external\external_api; 20 21 defined('MOODLE_INTERNAL') || die(); 22 global $CFG; 23 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 24 25 /** 26 * Tests for add_instances external class 27 * 28 * @package enrol_meta 29 * @group enrol_meta 30 * @category test 31 * @copyright 2021 WKS KV Bildung 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class add_instances_test extends \externallib_advanced_testcase { 35 36 /** 37 * Test setup 38 */ 39 public function setUp(): void { 40 $this->resetAfterTest(); 41 $this->setAdminUser(); 42 } 43 44 /** 45 * Test add_instances no instances. 46 */ 47 public function test_add_instances_no_instances() { 48 $this->expectException(\invalid_parameter_exception::class); 49 add_instances::execute([]); 50 } 51 52 /** 53 * Test add_instances missing courses. 54 */ 55 public function test_add_instances_missing_courses() { 56 $course = self::getDataGenerator()->create_course(); 57 58 // Missing meta course. 59 try { 60 add_instances::execute([['metacourseid' => 1000, 'courseid' => $course->id]]); 61 $this->fail('Exception expected'); 62 } catch (\moodle_exception $e) { 63 $this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', 1000), $e->getMessage()); 64 } 65 66 // Missing linked course. 67 try { 68 add_instances::execute([['metacourseid' => $course->id, 'courseid' => 1000]]); 69 $this->fail('Exception expected'); 70 } catch (\moodle_exception $e) { 71 $this->assertStringContainsString(get_string('wsinvalidcourse', 'enrol_meta', 1000), $e->getMessage()); 72 } 73 } 74 75 /** 76 * Test add_instances missing capabilities. 77 */ 78 public function test_add_instances_missing_capabilities() { 79 $metacourse = self::getDataGenerator()->create_course(); 80 $course = self::getDataGenerator()->create_course(); 81 $user = self::getDataGenerator()->create_user(); 82 $this::setUser($user); 83 84 // Missing rights in meta course. 85 try { 86 add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]); 87 $this->fail('Exception expected'); 88 } catch (\moodle_exception $e) { 89 $this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', $metacourse->id), $e->getMessage()); 90 } 91 92 // Add rights for metacourse. 93 $metacontext = \context_course::instance($metacourse->id); 94 $roleid = $this->assignUserCapability('enrol/meta:config', $metacontext->id); 95 $this->assignUserCapability('moodle/course:view', $metacontext->id, $roleid); 96 $this->assignUserCapability('moodle/course:enrolconfig', $metacontext->id, $roleid); 97 98 // Missing rights for linked course. 99 try { 100 add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]); 101 $this->fail('Exception expected'); 102 } catch (\moodle_exception $e) { 103 $this->assertStringContainsString(get_string('wsinvalidcourse', 'enrol_meta', $course->id), $e->getMessage()); 104 } 105 106 // Add rights for linked course. 107 $context = \context_course::instance($course->id); 108 $this->assignUserCapability('moodle/course:view', $context->id, $roleid); 109 $this->assignUserCapability('enrol/meta:selectaslinked', $context->id, $roleid); 110 111 $result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]); 112 $this->assertNotEmpty($result); 113 } 114 115 /** 116 * Test add_instances. 117 */ 118 public function test_add_instances() { 119 global $DB; 120 $metacourse = self::getDataGenerator()->create_course(); 121 $course = self::getDataGenerator()->create_course(); 122 123 // Sanity check. 124 $enrolrecords = $DB->count_records('enrol', 125 ['enrol' => 'meta', 'courseid' => $metacourse->id, 'customint1' => $course->id]); 126 $this->assertEquals(0, $enrolrecords); 127 128 // Add instance. 129 $result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]); 130 $result = external_api::clean_returnvalue(add_instances::execute_returns(), $result); 131 $this->assertEquals($result[0]['metacourseid'], $metacourse->id); 132 $this->assertEquals($result[0]['courseid'], $course->id); 133 $this->assertEquals($result[0]['status'], 1); 134 135 // Check instance was created. 136 $enrolrecords = $DB->count_records('enrol', 137 ['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]); 138 $this->assertEquals(1, $enrolrecords); 139 140 // Add same instance. 141 $result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]); 142 $result = external_api::clean_returnvalue(add_instances::execute_returns(), $result); 143 $this->assertEquals($result[0]['metacourseid'], $metacourse->id); 144 $this->assertEquals($result[0]['courseid'], $course->id); 145 $this->assertEquals($result[0]['status'], 0); 146 147 // Check no new instance was created. 148 $enrolrecords = $DB->count_records('enrol', 149 ['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]); 150 $this->assertEquals(1, $enrolrecords); 151 } 152 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body