From d2eca4cd0f2a99a77827f70862d79e7ea5bc898c Mon Sep 17 00:00:00 2001 From: David Monllao Date: Wed, 3 Apr 2013 10:07:13 +0800 Subject: [PATCH] MDL-38482 behat: Data generator for system assigns Chaning also the adapt_ prefix for non-direct data generator uses to process_ because not all of behat's data generators would have a lib/testing data generator to share with phpunit tests. --- lib/tests/behat/behat_data_generators.php | 42 ++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index bddba88057c..2d86aaa2516 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -37,10 +37,11 @@ use Behat\Behat\Exception\PendingException as PendingException; * Acceptance tests are block-boxed, so this steps definitions should only * be used to set up the test environment as we are not replicating user steps. * - * All data generators should be in lib/testing/generator/* and shared between phpunit + * All data generators should be in lib/testing/generator/*, shared between phpunit * and behat and they should be called from here, if possible using the standard - * 'create_$elementname($options)' and if not possible (data generators arguments will not be - * always the same) create an adapter 'adapt_$elementname($options)' that uses the data generator. + * 'create_$elementname($options)' and if it's not possible (data generators arguments will not be + * always the same) or the element is not suitable to be a data generator, create a + * 'process_$elementname($options)' method and use the data generator from there if possible. * * @todo If the available elements list grows too much this class must be split into smaller pieces * @package core @@ -93,6 +94,11 @@ class behat_data_generators extends behat_base { 'switchids' => array('user' => 'userid', 'course' => 'courseid', 'role' => 'roleid') ), + 'system role assigns' => array( + 'datagenerator' => 'role_assign', + 'required' => array('user', 'role'), + 'switchids' => array('user' => 'userid', 'role' => 'roleid') + ), 'group members' => array( 'datagenerator' => 'group_member', 'required' => array('user', 'group'), @@ -164,9 +170,9 @@ class behat_data_generators extends behat_base { // Using data generators directly. $this->datagenerator->{$methodname}($elementdata); - } else if (method_exists($this, 'adapt_' . $elementdatagenerator)) { - // Using an adaptor to use the data generator. - $this->{'adapt_' . $elementdatagenerator}($elementdata); + } else if (method_exists($this, 'process_' . $elementdatagenerator)) { + // Using an alternative to the direct data generator call. + $this->{'process_' . $elementdatagenerator}($elementdata); } else { throw new PendingException($elementname . ' data generator is not implemented'); } @@ -190,10 +196,10 @@ class behat_data_generators extends behat_base { /** * Adapter to enrol_user() data generator. * @throws Exception - * @param mixed $data + * @param array $data * @return void */ - protected function adapt_enrol_user($data) { + protected function process_enrol_user($data) { global $SITE; if (empty($data['roleid'])) { @@ -225,6 +231,26 @@ class behat_data_generators extends behat_base { } + /** + * Assigns a role to a user at system level. + * @throws Exception + * @param array $data + * @return void + */ + protected function process_role_assign($data) { + + if (empty($data['roleid'])) { + throw new Exception('\'system role assigns\' requires the field \'role\' to be specified'); + } + + if (!isset($data['userid'])) { + throw new Exception('\'system role assigns\' requires the field \'user\' to be specified'); + } + + $context = context_system::instance(); + role_assign($data['roleid'], $data['userid'], $context->id); + } + /** * Gets the user id from it's username. * @throws Exception