From b7ebcf7e2c694bef4dbdbfe5f4c1b27569f37d48 Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Mon, 20 Nov 2023 09:49:11 +0100 Subject: [PATCH] MDL-79979 tool_generator: Use auto-enrol admin in new courses setting --- .../tool/generator/classes/course_backend.php | 8 ++++- .../tests/behat/make_test_course.feature | 33 +++++++++++++++++++ .../generator/tests/maketestcourse_test.php | 9 +++-- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 admin/tool/generator/tests/behat/make_test_course.feature diff --git a/admin/tool/generator/classes/course_backend.php b/admin/tool/generator/classes/course_backend.php index e5e7ec4e065..3379e591e51 100644 --- a/admin/tool/generator/classes/course_backend.php +++ b/admin/tool/generator/classes/course_backend.php @@ -205,7 +205,7 @@ class tool_generator_course_backend extends tool_generator_backend { * @return int Course id */ public function make() { - global $DB, $CFG; + global $DB, $CFG, $USER; require_once($CFG->dirroot . '/lib/phpunit/classes/util.php'); raise_memory_limit(MEMORY_EXTRA); @@ -231,6 +231,12 @@ class tool_generator_course_backend extends tool_generator_backend { $this->create_users(); $this->create_forum(); + // We are checking 'enroladminnewcourse' setting to decide to enrol admins or not. + if (!empty($CFG->creatornewroleid) && !empty($CFG->enroladminnewcourse) && is_siteadmin($USER->id)) { + // Deal with course creators - enrol them internally with default role. + enrol_try_internal_enrol($this->course->id, $USER->id, $CFG->creatornewroleid); + } + // Log total time. $this->log('coursecompleted', round(microtime(true) - $entirestart, 1)); diff --git a/admin/tool/generator/tests/behat/make_test_course.feature b/admin/tool/generator/tests/behat/make_test_course.feature new file mode 100644 index 00000000000..c76fc511128 --- /dev/null +++ b/admin/tool/generator/tests/behat/make_test_course.feature @@ -0,0 +1,33 @@ +@core @core_course @tool_generator +Feature: Admins can create test courses + In order to create testing information + As an admin + I need to create testing courses quickly + + @javascript + Scenario: 'Auto-enrol admin in new courses' setting when creating a test course as admin + Given I log in as "admin" + And the following config values are set as admin: + | enroladminnewcourse | 0 | + And I navigate to "Development > Make test course" in site administration + And I set the following fields to these values: + | Size of course | XS | + | Course full name | Fake course for testing | + | Course short name | fake | + And I press "Create course" + And I click on "Continue" "link" + And I navigate to course participants + Then I should not see "Teacher" + And I should not see "Nothing to display" + And the following config values are set as admin: + | enroladminnewcourse | 1 | + And I navigate to "Courses > Add a new course" in site administration + And I navigate to "Development > Make test course" in site administration + And I set the following fields to these values: + | Size of course | XS | + | Course full name | New fake course for testing | + | Course short name | newfake | + And I press "Create course" + And I click on "Continue" "link" + And I navigate to course participants + And I should see "Teacher" diff --git a/admin/tool/generator/tests/maketestcourse_test.php b/admin/tool/generator/tests/maketestcourse_test.php index f3109264e3c..6960dd2c294 100644 --- a/admin/tool/generator/tests/maketestcourse_test.php +++ b/admin/tool/generator/tests/maketestcourse_test.php @@ -68,9 +68,14 @@ class maketestcourse_test extends \advanced_testcase { $this->assertEquals(2, count($modinfo->get_section_info_all())); // Check user is enrolled. + // enroladminnewcourse is enabled by default, so admin is also enrolled as teacher. $users = get_enrolled_users($context); - $this->assertEquals(1, count($users)); - $this->assertEquals('tool_generator_000001', reset($users)->username); + $this->assertEquals(2, count($users)); + $usernames = array_map(function($user) { + return $user->username; + }, $users); + $this->assertTrue(in_array('admin', $usernames)); + $this->assertTrue(in_array('tool_generator_000001', $usernames)); // Check there's a page on the course. $pages = $modinfo->get_instances_of('page');