mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 12:32:08 +02:00
MDL-71571 enrol_meta: Add webservices for adding and deleting instances.
This commit is contained in:
parent
214adb7984
commit
80bcf0d740
164
enrol/meta/classes/external/add_instances.php
vendored
Normal file
164
enrol/meta/classes/external/add_instances.php
vendored
Normal file
@ -0,0 +1,164 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace enrol_meta\external;
|
||||
|
||||
use external_api;
|
||||
use external_function_parameters;
|
||||
use external_multiple_structure;
|
||||
use external_single_structure;
|
||||
use external_value;
|
||||
use invalid_parameter_exception;
|
||||
use context_course;
|
||||
use moodle_exception;
|
||||
|
||||
/**
|
||||
* Web service function relating to add enrol meta instances
|
||||
*
|
||||
* @package enrol_meta
|
||||
* @copyright 2021 WKS KV Bildung
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class add_instances extends external_api {
|
||||
|
||||
/**
|
||||
* Parameters for adding meta enrolment instances
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function execute_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'instances' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
[
|
||||
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is added.'),
|
||||
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
|
||||
'creategroup' => new external_value(PARAM_BOOL,
|
||||
'Creates group in meta course named after linked course and '
|
||||
. 'puts all enrolled users in this group', VALUE_DEFAULT, false),
|
||||
]
|
||||
), 'List of course meta enrolment instances to create.', VALUE_DEFAULT, []
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding meta enrolment instances
|
||||
*
|
||||
* @param array $instances
|
||||
* @return array
|
||||
*/
|
||||
public static function execute(array $instances): array {
|
||||
global $DB;
|
||||
// Parameter validation.
|
||||
$params = self::validate_parameters(self::execute_parameters(), [
|
||||
'instances' => $instances,
|
||||
]);
|
||||
|
||||
if (!count($params['instances'])) {
|
||||
throw new invalid_parameter_exception(get_string('wsnoinstancesspecified', 'enrol_meta'));
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($params['instances'] as $instance) {
|
||||
// Ensure the metacourse exists.
|
||||
$metacourserecord = $DB->get_record('course', ['id' => $instance['metacourseid']], 'id,visible');
|
||||
if (!$metacourserecord) {
|
||||
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
|
||||
}
|
||||
// Ensure the current user is allowed to access metacourse.
|
||||
$contextmeta = context_course::instance($instance['metacourseid'], IGNORE_MISSING);
|
||||
try {
|
||||
self::validate_context($contextmeta);
|
||||
require_all_capabilities(['moodle/course:enrolconfig', 'enrol/meta:config'], $contextmeta);
|
||||
} catch (moodle_exception $e) {
|
||||
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
|
||||
}
|
||||
|
||||
// Ensure the linked course exists.
|
||||
$courserecord = $DB->get_record('course', ['id' => $instance['courseid']], 'id,visible');
|
||||
if (!$courserecord) {
|
||||
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
|
||||
}
|
||||
|
||||
// Ensure the current user is allowed to access linked course.
|
||||
$context = context_course::instance($instance['courseid'], IGNORE_MISSING);
|
||||
try {
|
||||
self::validate_context($context);
|
||||
if (!$courserecord->visible) {
|
||||
require_capability('moodle/course:viewhiddencourses', $context);
|
||||
}
|
||||
require_capability('enrol/meta:selectaslinked', $context);
|
||||
} catch (moodle_exception $e) {
|
||||
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
|
||||
}
|
||||
|
||||
// Check for existing meta course link.
|
||||
$enrolrecord = $DB->get_record('enrol',
|
||||
['enrol' => 'meta', 'courseid' => $instance['metacourseid'], 'customint1' => $instance['courseid']]);
|
||||
if ($enrolrecord) {
|
||||
// Link exists.
|
||||
$result[] = [
|
||||
'metacourseid' => $instance['metacourseid'],
|
||||
'courseid' => $instance['courseid'],
|
||||
'status' => false,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for permission to create group.
|
||||
if ($instance['creategroup']) {
|
||||
try {
|
||||
require_capability('moodle/course:managegroups', $context);
|
||||
} catch (moodle_exception $e) {
|
||||
throw new invalid_parameter_exception(get_string('wscannotcreategroup', 'enrol_meta', $instance['courseid']));
|
||||
}
|
||||
}
|
||||
|
||||
// Create instance.
|
||||
$enrolplugin = enrol_get_plugin('meta');
|
||||
$fields = [
|
||||
'customint1' => $instance['courseid'],
|
||||
'customint2' => $instance['creategroup'] ? ENROL_META_CREATE_GROUP : 0,
|
||||
];
|
||||
$addresult = $enrolplugin->add_instance($metacourserecord, $fields);
|
||||
$result[] = [
|
||||
'metacourseid' => $instance['metacourseid'],
|
||||
'courseid' => $instance['courseid'],
|
||||
'status' => (bool) $addresult,
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return for adding enrolment instances.
|
||||
*
|
||||
* @return external_multiple_structure
|
||||
*/
|
||||
public static function execute_returns(): external_multiple_structure {
|
||||
return new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
[
|
||||
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is added.'),
|
||||
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'True on success, false if link already exists.'),
|
||||
]
|
||||
), 'List of course meta enrolment instances that were created.', VALUE_DEFAULT, []
|
||||
);
|
||||
}
|
||||
}
|
138
enrol/meta/classes/external/delete_instances.php
vendored
Normal file
138
enrol/meta/classes/external/delete_instances.php
vendored
Normal file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace enrol_meta\external;
|
||||
|
||||
use external_api;
|
||||
use external_function_parameters;
|
||||
use external_multiple_structure;
|
||||
use external_single_structure;
|
||||
use external_value;
|
||||
use invalid_parameter_exception;
|
||||
use context_course;
|
||||
use moodle_exception;
|
||||
|
||||
/**
|
||||
* Web service function relating to add enrol meta instances
|
||||
*
|
||||
* @package enrol_meta
|
||||
* @copyright 2021 WKS KV Bildung
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class delete_instances extends external_api {
|
||||
|
||||
/**
|
||||
* Parameters for deleting meta enrolment instances
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function execute_parameters(): external_function_parameters {
|
||||
return new external_function_parameters([
|
||||
'instances' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
[
|
||||
'metacourseid' => new external_value(PARAM_INT, 'ID of the course with meta enrolment.'),
|
||||
'courseid' => new external_value(PARAM_RAW, 'ID of the course where meta enrolment is linked to.'),
|
||||
]
|
||||
), 'List of course meta enrolment instances to delete.', VALUE_DEFAULT, []
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deleting meta enrolment instances
|
||||
*
|
||||
* @param array $instances
|
||||
* @return array
|
||||
*/
|
||||
public static function execute(array $instances): array {
|
||||
global $DB;
|
||||
// Parameter validation.
|
||||
$params = self::validate_parameters(self::execute_parameters(), [
|
||||
'instances' => $instances,
|
||||
]);
|
||||
|
||||
if (!count($params['instances'])) {
|
||||
throw new invalid_parameter_exception(get_string('wsnoinstancesspecified', 'enrol_meta'));
|
||||
}
|
||||
|
||||
$result = [];
|
||||
foreach ($params['instances'] as $instance) {
|
||||
// Ensure the metacourse exists.
|
||||
$metacourserecord = $DB->get_record('course', ['id' => $instance['metacourseid']], 'id,visible');
|
||||
if (!$metacourserecord) {
|
||||
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
|
||||
}
|
||||
// Ensure the current user is allowed to access metacourse.
|
||||
$contextmeta = context_course::instance($instance['metacourseid'], IGNORE_MISSING);
|
||||
try {
|
||||
self::validate_context($contextmeta);
|
||||
require_all_capabilities(['moodle/course:enrolconfig', 'enrol/meta:config'], $contextmeta);
|
||||
} catch (moodle_exception $e) {
|
||||
throw new invalid_parameter_exception(get_string('wsinvalidmetacourse', 'enrol_meta', $instance['metacourseid']));
|
||||
}
|
||||
|
||||
// Ensure the linked course exists.
|
||||
$courserecord = $DB->get_record('course', ['id' => $instance['courseid']], 'id,visible');
|
||||
if (!$courserecord) {
|
||||
throw new invalid_parameter_exception(get_string('wsinvalidcourse', 'enrol_meta', $instance['courseid']));
|
||||
}
|
||||
// It is probably needed to check if user is allowed to access linked course.
|
||||
// but can_delete_instance does not do that, so we stop permission check here.
|
||||
|
||||
// Check for existing meta course link.
|
||||
$enrolrecord = $DB->get_record('enrol',
|
||||
['enrol' => 'meta', 'courseid' => $instance['metacourseid'], 'customint1' => $instance['courseid']]);
|
||||
if ($enrolrecord) {
|
||||
// Link exists. Delete instance.
|
||||
$enrolplugin = enrol_get_plugin('meta');
|
||||
$enrolplugin->delete_instance($enrolrecord);
|
||||
|
||||
$result[] = [
|
||||
'metacourseid' => $instance['metacourseid'],
|
||||
'courseid' => $instance['courseid'],
|
||||
'status' => true,
|
||||
];
|
||||
continue;
|
||||
}
|
||||
$result[] = [
|
||||
'metacourseid' => $instance['metacourseid'],
|
||||
'courseid' => $instance['courseid'],
|
||||
'status' => false,
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return for deleting enrolment instances.
|
||||
*
|
||||
* @return external_multiple_structure
|
||||
*/
|
||||
public static function execute_returns(): external_multiple_structure {
|
||||
return new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
[
|
||||
'metacourseid' => new external_value(PARAM_INT, 'ID of the course where meta enrolment is deleted.'),
|
||||
'courseid' => new external_value(PARAM_RAW, 'ID of the course that was meta linked.'),
|
||||
'status' => new external_value(PARAM_BOOL, 'True on success, false if meta link did not exist.'),
|
||||
|
||||
]
|
||||
), 'List of course meta enrolment instances that were deleted.', VALUE_DEFAULT, []
|
||||
);
|
||||
}
|
||||
}
|
46
enrol/meta/db/services.php
Normal file
46
enrol/meta/db/services.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Meta enrol external functions and service definitions.
|
||||
*
|
||||
* @package enrol_meta
|
||||
* @copyright 2021 WKS KV Bildung
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
$functions = [
|
||||
'enrol_meta_add_instances' => [
|
||||
'classname' => \enrol_meta\external\add_instances::class,
|
||||
'methodname' => 'execute',
|
||||
'description' => 'Add meta enrolment instances',
|
||||
'capabilities' => 'enrol/meta:config',
|
||||
'type' => 'write',
|
||||
'ajax' => true,
|
||||
'loginrequired' => true,
|
||||
],
|
||||
'enrol_meta_delete_instances' => [
|
||||
'classname' => \enrol_meta\external\delete_instances::class,
|
||||
'methodname' => 'execute',
|
||||
'description' => 'Delete meta enrolment instances',
|
||||
'capabilities' => 'enrol/meta:config',
|
||||
'type' => 'write',
|
||||
'ajax' => true,
|
||||
'loginrequired' => true,
|
||||
],
|
||||
];
|
@ -39,3 +39,7 @@ $string['pluginname_desc'] = 'Course meta link enrolment plugin synchronises enr
|
||||
$string['syncall'] = 'Synchronise all enrolled users';
|
||||
$string['syncall_desc'] = 'If enabled all enrolled users are synchronised even if they have no role in parent course, if disabled only users that have at least one synchronised role are enrolled in child course.';
|
||||
$string['privacy:metadata:core_group'] = 'Enrol meta plugin can create a new group or use an existing group to add all the participants of the course linked.';
|
||||
$string['wscannotcreategroup'] = 'No permission to create group in linked course id = {$a}.';
|
||||
$string['wsinvalidcourse'] = 'Course id = {$a} does not exist or no permission to link in meta enrolment.';
|
||||
$string['wsinvalidmetacourse'] = 'Meta course id = {$a} does not exist or no permission to add enrolment instance.';
|
||||
$string['wsnoinstancesspecified'] = 'No instances specified';
|
||||
|
151
enrol/meta/tests/external_add_instances_test.php
Normal file
151
enrol/meta/tests/external_add_instances_test.php
Normal file
@ -0,0 +1,151 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace enrol_meta\external;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
|
||||
/**
|
||||
* Tests for add_instances external class
|
||||
*
|
||||
* @package enrol_meta
|
||||
* @group enrol_meta
|
||||
* @category test
|
||||
* @copyright 2021 WKS KV Bildung
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class add_instances_testcase extends \externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test setup
|
||||
*/
|
||||
public function setUp(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add_instances no instances.
|
||||
*/
|
||||
public function test_add_instances_no_instances() {
|
||||
$this->expectException(\invalid_parameter_exception::class);
|
||||
add_instances::execute([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add_instances missing courses.
|
||||
*/
|
||||
public function test_add_instances_missing_courses() {
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
|
||||
// Missing meta course.
|
||||
try {
|
||||
add_instances::execute([['metacourseid' => 1000, 'courseid' => $course->id]]);
|
||||
$this->fail('Exception expected');
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', 1000), $e->getMessage());
|
||||
}
|
||||
|
||||
// Missing linked course.
|
||||
try {
|
||||
add_instances::execute([['metacourseid' => $course->id, 'courseid' => 1000]]);
|
||||
$this->fail('Exception expected');
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertStringContainsString(get_string('wsinvalidcourse', 'enrol_meta', 1000), $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add_instances missing capabilities.
|
||||
*/
|
||||
public function test_add_instances_missing_capabilities() {
|
||||
$metacourse = self::getDataGenerator()->create_course();
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$user = self::getDataGenerator()->create_user();
|
||||
$this::setUser($user);
|
||||
|
||||
// Missing rights in meta course.
|
||||
try {
|
||||
add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$this->fail('Exception expected');
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', $metacourse->id), $e->getMessage());
|
||||
}
|
||||
|
||||
// Add rights for metacourse.
|
||||
$metacontext = \context_course::instance($metacourse->id);
|
||||
$roleid = $this->assignUserCapability('enrol/meta:config', $metacontext->id);
|
||||
$this->assignUserCapability('moodle/course:view', $metacontext->id, $roleid);
|
||||
$this->assignUserCapability('moodle/course:enrolconfig', $metacontext->id, $roleid);
|
||||
|
||||
// Missing rights for linked course.
|
||||
try {
|
||||
add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$this->fail('Exception expected');
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertStringContainsString(get_string('wsinvalidcourse', 'enrol_meta', $course->id), $e->getMessage());
|
||||
}
|
||||
|
||||
// Add rights for linked course.
|
||||
$context = \context_course::instance($course->id);
|
||||
$this->assignUserCapability('moodle/course:view', $context->id, $roleid);
|
||||
$this->assignUserCapability('enrol/meta:selectaslinked', $context->id, $roleid);
|
||||
|
||||
$result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$this->assertNotEmpty($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test add_instances.
|
||||
*/
|
||||
public function test_add_instances() {
|
||||
global $DB;
|
||||
$metacourse = self::getDataGenerator()->create_course();
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
|
||||
// Sanity check.
|
||||
$enrolrecords = $DB->count_records('enrol',
|
||||
['enrol' => 'meta', 'courseid' => $metacourse->id, 'customint1' => $course->id]);
|
||||
$this->assertEquals(0, $enrolrecords);
|
||||
|
||||
// Add instance.
|
||||
$result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
|
||||
$this->assertEquals($result[0]['metacourseid'], $metacourse->id);
|
||||
$this->assertEquals($result[0]['courseid'], $course->id);
|
||||
$this->assertEquals($result[0]['status'], 1);
|
||||
|
||||
// Check instance was created.
|
||||
$enrolrecords = $DB->count_records('enrol',
|
||||
['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]);
|
||||
$this->assertEquals(1, $enrolrecords);
|
||||
|
||||
// Add same instance.
|
||||
$result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
|
||||
$this->assertEquals($result[0]['metacourseid'], $metacourse->id);
|
||||
$this->assertEquals($result[0]['courseid'], $course->id);
|
||||
$this->assertEquals($result[0]['status'], 0);
|
||||
|
||||
// Check no new instance was created.
|
||||
$enrolrecords = $DB->count_records('enrol',
|
||||
['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]);
|
||||
$this->assertEquals(1, $enrolrecords);
|
||||
}
|
||||
}
|
141
enrol/meta/tests/external_delete_instances_test.php
Normal file
141
enrol/meta/tests/external_delete_instances_test.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace enrol_meta\external;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
|
||||
/**
|
||||
* Tests for delete_instances external class
|
||||
*
|
||||
* @package enrol_meta
|
||||
* @group enrol_meta
|
||||
* @category test
|
||||
* @copyright 2021 WKS KV Bildung
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class delete_instances_testcase extends \externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test setup
|
||||
*/
|
||||
public function setUp(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete_instances no instances.
|
||||
*/
|
||||
public function test_delete_instances_no_instances() {
|
||||
$this->expectException(\invalid_parameter_exception::class);
|
||||
delete_instances::execute([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete_instances missing courses.
|
||||
*/
|
||||
public function test_delete_instances_missing_courses() {
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
|
||||
// Missing meta course.
|
||||
try {
|
||||
delete_instances::execute([['metacourseid' => 1000, 'courseid' => $course->id]]);
|
||||
$this->fail('Exception expected');
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', 1000), $e->getMessage());
|
||||
}
|
||||
|
||||
// Missing linked course.
|
||||
try {
|
||||
delete_instances::execute([['metacourseid' => $course->id, 'courseid' => 1000]]);
|
||||
$this->fail('Exception expected');
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertStringContainsString(get_string('wsinvalidcourse', 'enrol_meta', 1000), $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete_instances missing capabilities.
|
||||
*/
|
||||
public function test_delete_instances_missing_capabilities() {
|
||||
$metacourse = self::getDataGenerator()->create_course();
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$user = self::getDataGenerator()->create_user();
|
||||
$this::setUser($user);
|
||||
|
||||
// Missing rights in meta course.
|
||||
try {
|
||||
delete_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$this->fail('Exception expected');
|
||||
} catch (\moodle_exception $e) {
|
||||
$this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', $metacourse->id), $e->getMessage());
|
||||
}
|
||||
|
||||
// Add rights for metacourse.
|
||||
$metacontext = \context_course::instance($metacourse->id);
|
||||
$roleid = $this->assignUserCapability('enrol/meta:config', $metacontext->id);
|
||||
$this->assignUserCapability('moodle/course:view', $metacontext->id, $roleid);
|
||||
$this->assignUserCapability('moodle/course:enrolconfig', $metacontext->id, $roleid);
|
||||
|
||||
$result = delete_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$this->assertNotEmpty($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete_instances.
|
||||
*/
|
||||
public function test_delete_instances() {
|
||||
global $DB;
|
||||
$metacourse = self::getDataGenerator()->create_course();
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
|
||||
// Create instance.
|
||||
$enrolplugin = enrol_get_plugin('meta');
|
||||
$fields = [
|
||||
'customint1' => $course->id,
|
||||
'customint2' => 0,
|
||||
];
|
||||
$enrolplugin->add_instance($metacourse, $fields);
|
||||
|
||||
// Sanity check.
|
||||
$enrolrecords = $DB->count_records('enrol',
|
||||
['enrol' => 'meta', 'courseid' => $metacourse->id, 'customint1' => $course->id]);
|
||||
$this->assertEquals(1, $enrolrecords);
|
||||
|
||||
// Delete instance.
|
||||
$result = delete_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
|
||||
$this->assertEquals($result[0]['metacourseid'], $metacourse->id);
|
||||
$this->assertEquals($result[0]['courseid'], $course->id);
|
||||
$this->assertEquals($result[0]['status'], 1);
|
||||
|
||||
// Check instance was deleted.
|
||||
$enrolrecords = $DB->count_records('enrol',
|
||||
['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]);
|
||||
$this->assertEquals(0, $enrolrecords);
|
||||
|
||||
// Delete same instance.
|
||||
$result = delete_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
|
||||
$result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
|
||||
$this->assertEquals($result[0]['metacourseid'], $metacourse->id);
|
||||
$this->assertEquals($result[0]['courseid'], $course->id);
|
||||
$this->assertEquals($result[0]['status'], 0);
|
||||
}
|
||||
}
|
@ -24,6 +24,6 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2021052500; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->version = 2021052501; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->requires = 2021052500; // Requires this Moodle version.
|
||||
$plugin->component = 'enrol_meta'; // Full name of the plugin (used for diagnostics)
|
||||
|
Loading…
x
Reference in New Issue
Block a user