From 7cd408501b3db0cfe706b951af9f3fdc81d60440 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 8 Jul 2021 11:50:17 +0800 Subject: [PATCH] MDL-72125 testing: Set a default idnumber when creating activities The activity generator currently requires an idnumber when creating activities, but this is not a requirement when creating the same activity through the UI. The requirement comes because we want to provide a way to refer to activities in subsequent steps. This commit modifies the behaviour such that the generator uses the name of the activity as the default idnumber. This has two main benefits: 1. it simplfies generation of activities; and 2. it makes the language used when writing behat tests much more natural. With this change, steps will refer to the activity by its idnumber/title in all cases, rather than sometimes by an idnumber which bears no relevance to the title. --- lib/behat/classes/behat_core_generator.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/behat/classes/behat_core_generator.php b/lib/behat/classes/behat_core_generator.php index 7f900ae3ade..c39bf93099a 100644 --- a/lib/behat/classes/behat_core_generator.php +++ b/lib/behat/classes/behat_core_generator.php @@ -120,7 +120,7 @@ class behat_core_generator extends behat_generator_base { 'activities' => [ 'singular' => 'activity', 'datagenerator' => 'activity', - 'required' => ['activity', 'idnumber', 'course'], + 'required' => ['activity', 'course'], 'switchids' => ['course' => 'course', 'gradecategory' => 'gradecat', 'grouping' => 'groupingid'], ], 'blocks' => [ @@ -389,6 +389,17 @@ class behat_core_generator extends behat_generator_base { } } + if (!array_key_exists('idnumber', $data)) { + $data['idnumber'] = $data['name']; + if (strlen($data['name']) > 100) { + throw new Exception( + "Activity '{$activityname}' cannot be used as the default idnumber. " . + "The idnumber has a max length of 100 chars. " . + "Please manually specify an idnumber." + ); + } + } + // We split $data in the activity $record and the course module $options. $cmoptions = array(); $cmcolumns = $DB->get_columns('course_modules');