Skip to content

Commit

Permalink
MDL-79979 tool_generator: Use auto-enrol admin in new courses setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Nov 21, 2023
1 parent b58d1fd commit f7fa5cb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
8 changes: 7 additions & 1 deletion admin/tool/generator/classes/course_backend.php
Expand Up @@ -216,7 +216,7 @@ public static function check_shortname_available($shortname) {
* @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);
Expand Down Expand Up @@ -252,6 +252,12 @@ public function make() {
}
}

// 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));

Expand Down
33 changes: 33 additions & 0 deletions 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"
9 changes: 7 additions & 2 deletions admin/tool/generator/tests/maketestcourse_test.php
Expand Up @@ -68,9 +68,14 @@ public function test_make_xs_course() {
$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');
Expand Down

0 comments on commit f7fa5cb

Please sign in to comment.