Commit f3936438 authored by Andrew Nicols's avatar Andrew Nicols
Browse files

MDL-69068 behat: New step to support singular generators

parent 952a894a
......@@ -39,99 +39,119 @@ defined('MOODLE_INTERNAL') || die();
class behat_core_generator extends behat_generator_base {
protected function get_creatable_entities(): array {
return [
$entities = [
'users' => [
'singular' => 'user',
'datagenerator' => 'user',
'required' => ['username'],
],
'categories' => [
'singular' => 'category',
'datagenerator' => 'category',
'required' => ['idnumber'],
'switchids' => ['category' => 'parent'],
],
'courses' => [
'singular' => 'course',
'datagenerator' => 'course',
'required' => ['shortname'],
'switchids' => ['category' => 'category'],
],
'groups' => [
'singular' => 'group',
'datagenerator' => 'group',
'required' => ['idnumber', 'course'],
'switchids' => ['course' => 'courseid'],
],
'groupings' => [
'singular' => 'grouping',
'datagenerator' => 'grouping',
'required' => ['idnumber', 'course'],
'switchids' => ['course' => 'courseid'],
],
'course enrolments' => [
'singular' => 'course enrolment',
'datagenerator' => 'enrol_user',
'required' => ['user', 'course', 'role'],
'switchids' => ['user' => 'userid', 'course' => 'courseid', 'role' => 'roleid'],
],
'custom field categories' => [
'singular' => 'custom field category',
'datagenerator' => 'custom_field_category',
'required' => ['name', 'component', 'area', 'itemid'],
'switchids' => [],
],
'custom fields' => [
'singular' => 'custom field',
'datagenerator' => 'custom_field',
'required' => ['name', 'category', 'type', 'shortname'],
'switchids' => [],
],
'permission overrides' => [
'singular' => 'permission override',
'datagenerator' => 'permission_override',
'required' => ['capability', 'permission', 'role', 'contextlevel', 'reference'],
'switchids' => ['role' => 'roleid'],
],
'system role assigns' => [
'singular' => 'system role assignment',
'datagenerator' => 'system_role_assign',
'required' => ['user', 'role'],
'switchids' => ['user' => 'userid', 'role' => 'roleid'],
],
'role assigns' => [
'singular' => 'role assignment',
'datagenerator' => 'role_assign',
'required' => ['user', 'role', 'contextlevel', 'reference'],
'switchids' => ['user' => 'userid', 'role' => 'roleid'],
],
'activities' => [
'singular' => 'activity',
'datagenerator' => 'activity',
'required' => ['activity', 'idnumber', 'course'],
'switchids' => ['course' => 'course', 'gradecategory' => 'gradecat', 'grouping' => 'groupingid'],
],
'blocks' => [
'singular' => 'block',
'datagenerator' => 'block_instance',
'required' => ['blockname', 'contextlevel', 'reference'],
],
'group members' => [
'singular' => 'group member',
'datagenerator' => 'group_member',
'required' => ['user', 'group'],
'switchids' => ['user' => 'userid', 'group' => 'groupid'],
],
'grouping groups' => [
'singular' => 'grouping group',
'datagenerator' => 'grouping_group',
'required' => ['grouping', 'group'],
'switchids' => ['grouping' => 'groupingid', 'group' => 'groupid'],
],
'cohorts' => [
'singular' => 'cohort',
'datagenerator' => 'cohort',
'required' => ['idnumber'],
],
'cohort members' => [
'singular' => 'cohort member',
'datagenerator' => 'cohort_member',
'required' => ['user', 'cohort'],
'switchids' => ['user' => 'userid', 'cohort' => 'cohortid'],
],
'roles' => [
'singular' => 'role',
'datagenerator' => 'role',
'required' => ['shortname'],
],
'grade categories' => [
'singular' => 'grade category',
'datagenerator' => 'grade_category',
'required' => ['fullname', 'course'],
'switchids' => ['course' => 'courseid', 'gradecategory' => 'parent'],
],
'grade items' => [
'singular' => 'grade item',
'datagenerator' => 'grade_item',
'required' => ['course'],
'switchids' => [
......@@ -142,30 +162,36 @@ class behat_core_generator extends behat_generator_base {
],
],
'grade outcomes' => [
'singular' => 'grade outcome',
'datagenerator' => 'grade_outcome',
'required' => ['shortname', 'scale'],
'switchids' => ['course' => 'courseid', 'gradecategory' => 'categoryid', 'scale' => 'scaleid'],
],
'scales' => [
'singular' => 'scale',
'datagenerator' => 'scale',
'required' => ['name', 'scale'],
'switchids' => ['course' => 'courseid'],
],
'question categories' => [
'singular' => 'question category',
'datagenerator' => 'question_category',
'required' => ['name', 'contextlevel', 'reference'],
'switchids' => ['questioncategory' => 'parent'],
],
'questions' => [
'singular' => 'question',
'datagenerator' => 'question',
'required' => ['qtype', 'questioncategory', 'name'],
'switchids' => ['questioncategory' => 'category', 'user' => 'createdby'],
],
'tags' => [
'singular' => 'tag',
'datagenerator' => 'tag',
'required' => ['name'],
],
'events' => [
'singular' => 'event',
'datagenerator' => 'event',
'required' => ['name', 'eventtype'],
'switchids' => [
......@@ -175,49 +201,60 @@ class behat_core_generator extends behat_generator_base {
],
],
'message contacts' => [
'singular' => 'message contact',
'datagenerator' => 'message_contacts',
'required' => ['user', 'contact'],
'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
],
'private messages' => [
'singular' => 'private message',
'datagenerator' => 'private_messages',
'required' => ['user', 'contact', 'message'],
'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
],
'favourite conversations' => [
'singular' => 'favourite conversation',
'datagenerator' => 'favourite_conversations',
'required' => ['user', 'contact'],
'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
],
'group messages' => [
'singular' => 'group message',
'datagenerator' => 'group_messages',
'required' => ['user', 'group', 'message'],
'switchids' => ['user' => 'userid', 'group' => 'groupid'],
],
'muted group conversations' => [
'singular' => 'muted group conversation',
'datagenerator' => 'mute_group_conversations',
'required' => ['user', 'group', 'course'],
'switchids' => ['user' => 'userid', 'group' => 'groupid', 'course' => 'courseid'],
],
'muted private conversations' => [
'singular' => 'muted private conversation',
'datagenerator' => 'mute_private_conversations',
'required' => ['user', 'contact'],
'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
],
'language customisations' => [
'singular' => 'language customisation',
'datagenerator' => 'customlang',
'required' => ['component', 'stringid', 'value'],
],
'analytics model' => [
'analytics models' => [
'singular' => 'analytics model',
'datagenerator' => 'analytics_model',
'required' => ['target', 'indicators', 'timesplitting', 'enabled'],
],
'user preferences' => [
'singular' => 'user preference',
'datagenerator' => 'user_preferences',
'required' => array('user', 'preference', 'value'),
'switchids' => array('user' => 'userid')
'switchids' => array('user' => 'userid'),
],
];
return $entities;
}
/**
......
......@@ -171,13 +171,23 @@ abstract class behat_generator_base {
*
* @param string $generatortype The name of the entity to create.
* @param TableNode $data from the step.
* @param bool $singular Whether there is only one record and it is pivotted
*/
public function generate_items(string $generatortype, TableNode $data) {
public function generate_items(string $generatortype, TableNode $data, bool $singular = false) {
// Now that we need them require the data generators.
require_once(__DIR__ . '/../../testing/generator/lib.php');
$elements = $this->get_creatable_entities();
foreach ($elements as $key => $configuration) {
if (array_key_exists('singular', $configuration)) {
$singularverb = $configuration['singular'];
unset($configuration['singular']);
unset($elements[$key]['singular']);
$elements[$singularverb] = $configuration;
}
}
if (!isset($elements[$generatortype])) {
throw new PendingException($this->name_for_errors($generatortype) .
' is not a known type of entity that can be generated.');
......@@ -193,8 +203,17 @@ abstract class behat_generator_base {
$generatortype = $entityinfo['datagenerator'];
foreach ($data->getHash() as $elementdata) {
if ($singular) {
// There is only one record to generate, and the table has been pivotted.
// The rows each represent a single field.
$rows = [$data->getRowsHash()];
} else {
// There are multiple records to generate.
// The rows represent an item to create.
$rows = $data->getHash();
}
foreach ($rows as $elementdata) {
// Check if all the required fields are there.
foreach ($entityinfo['required'] as $requiredfield) {
if (!isset($elementdata[$requiredfield])) {
......
......@@ -75,7 +75,7 @@ class behat_data_generators extends behat_base {
];
/**
* Creates the specified element.
* Creates the specified elements.
*
* See the class comment for an overview.
*
......@@ -92,6 +92,24 @@ class behat_data_generators extends behat_base {
$this->get_instance_for_component($component)->generate_items($entity, $data);
}
/**
* Creates the specified element.
*
* See the class comment for an overview.
*
* @Given the following :entitytype exists:
*
* @param string $entitytype The name of the type entity to add
* @param TableNode $data
*/
public function the_following_entity_exists($entitytype, TableNode $data) {
if (isset($this->movedentitytypes[$entitytype])) {
$entitytype = $this->movedentitytypes[$entitytype];
}
list($component, $entity) = $this->parse_entity_type($entitytype);
$this->get_instance_for_component($component)->generate_items($entity, $data, true);
}
/**
* Parse a full entity type like 'users' or 'mod_forum > subscription'.
*
......
......@@ -37,11 +37,13 @@ class behat_mod_quiz_generator extends behat_generator_base {
protected function get_creatable_entities(): array {
return [
'group overrides' => [
'singular' => 'group override',
'datagenerator' => 'override',
'required' => ['quiz', 'group'],
'switchids' => ['quiz' => 'quiz', 'group' => 'groupid'],
],
'user overrides' => [
'singular' => 'user override',
'datagenerator' => 'override',
'required' => ['quiz', 'user'],
'switchids' => ['quiz' => 'quiz', 'user' => 'userid'],
......
......@@ -36,6 +36,7 @@ class behat_core_question_generator extends behat_generator_base {
// are generated by behat_core_generator.
return [
'Tags' => [
'singular' => 'Tag',
'datagenerator' => 'question_tag',
'required' => ['question', 'tag'],
'switchids' => ['question' => 'questionid'],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment