From b6fed3123663ce38a598a6e97a65d1ff95a639bb Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Sun, 18 May 2014 09:21:14 +0800 Subject: [PATCH] MDL-45602 behat: add members to cohorts --- .../behat/tests/behat/data_generators.feature | 30 ++++++++++++++----- lib/tests/behat/behat_data_generators.php | 30 +++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/admin/tool/behat/tests/behat/data_generators.feature b/admin/tool/behat/tests/behat/data_generators.feature index 4610112ec3a..1081477abe0 100644 --- a/admin/tool/behat/tests/behat/data_generators.feature +++ b/admin/tool/behat/tests/behat/data_generators.feature @@ -234,24 +234,40 @@ Feature: Set up contextual data for tests And I select "Group 2 (1)" from "groups" And the "members" select box should contain "Student 2" - Scenario: Add cohorts with data generator + Scenario: Add cohorts and cohort members with data generator Given the following "categories" exist: | name | category | idnumber | | Cat 1 | 0 | CAT1 | + And the following "users" exist: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@asd.com | + | student2 | Student | 2 | student2@asd.com | And the following "cohorts" exist: | name | idnumber | - | System cohort 1 | CH01 | + | System cohort A | CHSA | And the following "cohorts" exist: | name | idnumber | contextlevel | reference | - | System cohort 2 | CH02 | System | | - | Cohort in category 1 | CH1 | Category | CAT1 | + | System cohort B | CHSB | System | | + | Cohort in category | CHC | Category | CAT1 | + | Empty cohort | CHE | Category | CAT1 | + And the following "cohort members" exist: + | user | cohort | + | student1 | CHSA | + | student2 | CHSB | + | student1 | CHSB | + | student1 | CHC | When I log in as "admin" And I navigate to "Cohorts" node in "Site administration > Users > Accounts" - Then I should see "System cohort 1" - And I should see "System cohort 2" + Then the following should exist in the "cohorts" table: + | Name | Cohort size | + | System cohort A | 1 | + | System cohort B | 2 | And I should not see "Cohort in category" And I follow "Courses" And I follow "Cat 1" And I follow "Cohorts" - And I should see "Cohort in category 1" And I should not see "System cohort" + And the following should exist in the "cohorts" table: + | Name | Cohort size | + | Cohort in category | 1 | + | Empty cohort | 0 | diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index 45ecb097c7a..285bf5ed054 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -130,6 +130,11 @@ class behat_data_generators extends behat_base { 'cohorts' => array( 'datagenerator' => 'cohort', 'required' => array('idnumber') + ), + 'cohort members' => array( + 'datagenerator' => 'cohort_member', + 'required' => array('user', 'cohort'), + 'switchids' => array('user' => 'userid', 'cohort' => 'cohortid') ) ); @@ -413,6 +418,16 @@ class behat_data_generators extends behat_base { $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id); } + /** + * Adds members to cohorts + * + * @param array $data + * @return void + */ + protected function process_cohort_member($data) { + cohort_add_member($data['cohortid'], $data['userid']); + } + /** * Gets the user id from it's username. * @throws Exception @@ -510,6 +525,21 @@ class behat_data_generators extends behat_base { return $id; } + /** + * Gets the cohort id from it's idnumber. + * @throws Exception + * @param string $idnumber + * @return int + */ + protected function get_cohort_id($idnumber) { + global $DB; + + if (!$id = $DB->get_field('cohort', 'id', array('idnumber' => $idnumber))) { + throw new Exception('The specified cohort with idnumber "' . $idnumber . '" does not exist'); + } + return $id; + } + /** * Gets the internal context id from the context reference. *