mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 08:26:37 +02:00
MDL-43820 enrol_guest: Override find_instance
For guest enrol method there can be just one instance. So we can just match by type
This commit is contained in:
parent
bf7ff76bcd
commit
3ea15bcef1
2 changed files with 81 additions and 0 deletions
|
@ -497,6 +497,28 @@ class enrol_guest_plugin extends enrol_plugin {
|
||||||
public function is_csv_upload_supported(): bool {
|
public function is_csv_upload_supported(): bool {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds matching instances for a given course.
|
||||||
|
*
|
||||||
|
* @param array $enrolmentdata enrolment data.
|
||||||
|
* @param int $courseid Course ID.
|
||||||
|
* @return stdClass|null Matching instance
|
||||||
|
*/
|
||||||
|
public function find_instance(array $enrolmentdata, int $courseid) : ?stdClass {
|
||||||
|
|
||||||
|
$instances = enrol_get_instances($courseid, false);
|
||||||
|
$instance = null;
|
||||||
|
foreach ($instances as $i) {
|
||||||
|
if ($i->enrol == 'guest') {
|
||||||
|
// There can be only one guest enrol instance so find first available.
|
||||||
|
$instance = $i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $instance;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
59
enrol/guest/tests/lib_test.php
Normal file
59
enrol/guest/tests/lib_test.php
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
<?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/>.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guest enrolment tests.
|
||||||
|
*
|
||||||
|
* @package enrol_guest
|
||||||
|
* @category phpunit
|
||||||
|
* @copyright 2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
|
||||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
|
*/
|
||||||
|
namespace enrol_guest;
|
||||||
|
|
||||||
|
class lib_test extends \advanced_testcase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the behaviour of find_instance().
|
||||||
|
*
|
||||||
|
* @covers ::find_instance
|
||||||
|
*/
|
||||||
|
public function test_find_instance() {
|
||||||
|
global $DB;
|
||||||
|
$this->resetAfterTest();
|
||||||
|
|
||||||
|
$cat = $this->getDataGenerator()->create_category();
|
||||||
|
// When we create a course, a guest enrolment instance is also created.
|
||||||
|
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
|
||||||
|
|
||||||
|
$guestplugin = enrol_get_plugin('guest');
|
||||||
|
|
||||||
|
$expected = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'guest']);
|
||||||
|
|
||||||
|
// Let's try to add second instance - only 1 guest instance is possible.
|
||||||
|
$instanceid2 = null;
|
||||||
|
// Have to do this check since add_instance doesn't block adding second instance for guest plugin.
|
||||||
|
if ($guestplugin->can_add_instance($course->id)) {
|
||||||
|
$instanceid2 = $guestplugin->add_instance($course, []);
|
||||||
|
}
|
||||||
|
$this->assertNull($instanceid2);
|
||||||
|
|
||||||
|
$enrolmentdata = [];
|
||||||
|
$actual = $guestplugin->find_instance($enrolmentdata, $course->id);
|
||||||
|
$this->assertEquals($expected->id, $actual->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue