From aa8ae42d2448e734c66f56b4d24bcc2f2ff69410 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 24 Jul 2014 10:03:36 +0800 Subject: [PATCH] MDL-46503 behat: cohort generator allows to create cohort in category --- .../behat/tests/behat/data_generators.feature | 22 +++++++++++++++++++ lib/tests/behat/behat_data_generators.php | 19 ++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/admin/tool/behat/tests/behat/data_generators.feature b/admin/tool/behat/tests/behat/data_generators.feature index 35614194d06..2b9dfdfb14a 100644 --- a/admin/tool/behat/tests/behat/data_generators.feature +++ b/admin/tool/behat/tests/behat/data_generators.feature @@ -252,3 +252,25 @@ Feature: Set up contextual data for tests And the "members" select box should contain "Student 1" And I set the field "groups" to "Group 2 (1)" And the "members" select box should contain "Student 2" + + Scenario: Add cohorts with data generator + Given the following "categories" exist: + | name | category | idnumber | + | Cat 1 | 0 | CAT1 | + And the following "cohorts" exist: + | name | idnumber | + | System cohort 1 | CH01 | + And the following "cohorts" exist: + | name | idnumber | contextlevel | reference | + | System cohort 2 | CH02 | System | | + | Cohort in category 1 | CH1 | Category | CAT1 | + 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" + 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" diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index 0a18e4bbd86..aba7b669d41 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -217,6 +217,25 @@ class behat_data_generators extends behat_base { return $data; } + /** + * If contextlevel and reference are specified for cohort, transform them to the contextid. + * + * @param array $data + * @return array + */ + protected function preprocess_cohort($data) { + if (isset($data['contextlevel'])) { + if (!isset($data['reference'])) { + throw new Exception('If field contextlevel is specified, field reference must also be present'); + } + $context = $this->get_context($data['contextlevel'], $data['reference']); + unset($data['contextlevel']); + unset($data['reference']); + $data['contextid'] = $context->id; + } + return $data; + } + /** * Adapter to modules generator * @throws Exception Custom exception for test writers