From 702851c071e774cbb37e359d7cabde1cdc936e6a Mon Sep 17 00:00:00 2001 From: David Monllao Date: Thu, 13 Feb 2014 11:32:49 +0800 Subject: [PATCH] MDL-44108 behat: Adding a roles data generator --- .../behat/tests/behat/data_generators.feature | 22 ++++- lib/testing/generator/data_generator.php | 88 +++++++++++++++++++ lib/tests/behat/behat_data_generators.php | 24 ++++- 3 files changed, 131 insertions(+), 3 deletions(-) diff --git a/admin/tool/behat/tests/behat/data_generators.feature b/admin/tool/behat/tests/behat/data_generators.feature index 558c848b264..b460b1a6dea 100644 --- a/admin/tool/behat/tests/behat/data_generators.feature +++ b/admin/tool/behat/tests/behat/data_generators.feature @@ -111,22 +111,32 @@ Feature: Set up contextual data for tests Then I should see "Topic 1" Scenario: Add role assigns - Given the following "users" exists: + Given the following "roles" exists: + | name | shortname | description | archetype | + | Custom editing teacher | custom1 | My custom role 1 | editingteacher | + | Custom student | custom2 | | | + And the following "users" exists: | username | firstname | lastname | email | | user1 | User | 1 | user1@moodlemoodle.com | | user2 | User | 2 | user2@moodlemoodle.com | | user3 | User | 3 | user3@moodlemoodle.com | + | user4 | User | 4 | user4@moodlemoodle.com | + | user5 | User | 5 | user5@moodlemoodle.com | And the following "categories" exists: | name | category | idnumber | | Cat 1 | 0 | CAT1 | And the following "courses" exists: | fullname | shortname | category | | Course 1 | C1 | CAT1 | + And the following "course enrolments" exists: + | user | course | role | + | user4 | C1 | custom1 | And the following "role assigns" exists: | user | role | contextlevel | reference | | user1 | manager | System | | | user2 | editingteacher | Category | CAT1 | | user3 | editingteacher | Course | C1 | + | user5 | custom2 | System | | When I log in as "user1" Then I should see "Front page settings" And I log out @@ -137,6 +147,16 @@ Feature: Set up contextual data for tests And I log in as "user3" And I follow "Course 1" And I should see "Turn editing on" + And I log out + And I log in as "user4" + And I follow "Course 1" + And I should see "Turn editing on" + And I log out + And I log in as "user5" + And I should see "You are logged in as" + And I follow "Course 1" + And I should see "You can not enrol yourself in this course." + Scenario: Add modules Given the following "courses" exists: diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index 20dcf5aab55..d9550cbc803 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -41,6 +41,7 @@ class testing_data_generator { protected $scalecount = 0; protected $groupcount = 0; protected $groupingcount = 0; + protected $rolecount = 0; /** @var array list of plugin generators */ protected $generators = array(); @@ -656,6 +657,93 @@ EOD; return $DB->get_record('scale', array('id'=>$id), '*', MUST_EXIST); } + /** + * Creates a new role in the system. + * + * You can fill $record with the role 'name', + * 'shortname', 'description' and 'archetype'. + * + * If an archetype is specified it's capabilities, + * context where the role can be assigned and + * all other properties are copied from the archetype; + * if no archetype is specified it will create an + * empty role. + * + * @param array|stdClass $record + * @return int The new role id + */ + public function create_role($record=null) { + global $DB; + + $this->rolecount++; + $i = $this->rolecount; + + $record = (array)$record; + + if (empty($record['shortname'])) { + $record['shortname'] = 'role-' . $i; + } + + if (empty($record['name'])) { + $record['name'] = 'Test role ' . $i; + } + + if (empty($record['description'])) { + $record['description'] = 'Test role ' . $i . ' description'; + } + + if (empty($record['archetype'])) { + $record['archetype'] = ''; + } else { + $archetypes = get_role_archetypes(); + if (empty($archetypes[$record['archetype']])) { + throw new coding_exception('\'role\' requires the field \'archetype\' to specify a ' . + 'valid archetype shortname (editingteacher, student...)'); + } + } + + // Creates the role. + if (!$newroleid = create_role($record['name'], $record['shortname'], $record['description'], $record['archetype'])) { + throw new coding_exception('There was an error creating \'' . $record['shortname'] . '\' role'); + } + + // If no archetype was specified we allow it to be added to all contexts, + // otherwise we allow it in the archetype contexts. + if (!$record['archetype']) { + $contextlevels = array_keys(context_helper::get_all_levels()); + } else { + // Copying from the archetype default rol. + $archetyperoleid = $DB->get_field( + 'role', + 'id', + array('shortname' => $record['archetype'], 'archetype' => $record['archetype']) + ); + $contextlevels = get_role_contextlevels($archetyperoleid); + } + set_role_contextlevels($newroleid, $contextlevels); + + if ($record['archetype']) { + + // We copy all the roles the archetype can assign, override and switch to. + if ($record['archetype']) { + $types = array('assign', 'override', 'switch'); + foreach ($types as $type) { + $rolestocopy = get_default_role_archetype_allows($type, $record['archetype']); + foreach ($rolestocopy as $tocopy) { + $functionname = 'allow_' . $type; + $functionname($newroleid, $tocopy); + } + } + } + + // Copying the archetype capabilities. + $sourcerole = $DB->get_record('role', array('id' => $archetyperoleid)); + role_cap_duplicate($sourcerole, $newroleid); + } + + return $newroleid; + } + /** * Helper method which combines $defaults with the values specified in $record. * If $record is an object, it is converted to an array. diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index 0a5b39007a7..749770bb287 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -130,6 +130,10 @@ class behat_data_generators extends behat_base { 'cohorts' => array( 'datagenerator' => 'cohort', 'required' => array('idnumber') + ), + 'roles' => array( + 'datagenerator' => 'role', + 'required' => array('shortname') ) ); @@ -377,6 +381,22 @@ class behat_data_generators extends behat_base { $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id); } + /** + * Creates a role. + * + * @param array $data + * @return void + */ + protected function process_role($data) { + + // We require the user to fill the role shortname. + if (empty($data['shortname'])) { + throw new Exception('\'role\' requires the field \'shortname\' to be specified'); + } + + $this->datagenerator->create_role($data); + } + /** * Gets the user id from it's username. * @throws Exception @@ -402,7 +422,7 @@ class behat_data_generators extends behat_base { global $DB; if (!$id = $DB->get_field('role', 'id', array('shortname' => $roleshortname))) { - throw new Exception('The specified role with shortname"' . $roleshortname . '" does not exist'); + throw new Exception('The specified role with shortname "' . $roleshortname . '" does not exist'); } return $id; @@ -439,7 +459,7 @@ class behat_data_generators extends behat_base { global $DB; if (!$id = $DB->get_field('course', 'id', array('shortname' => $shortname))) { - throw new Exception('The specified course with shortname"' . $shortname . '" does not exist'); + throw new Exception('The specified course with shortname "' . $shortname . '" does not exist'); } return $id; }