From c55473ad2e2695a868a9d9e9ca4652ceabd95c01 Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Thu, 16 Mar 2023 08:37:47 +0000 Subject: [PATCH] MDL-75301 question: Add behat generator for updating questions This adds "core_question > updated question" as an entity for `the following "X" exist` and calls the existing update_question() generator which will create a new question version with the supplied data. --- .../behat_core_question_generator.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/question/tests/generator/behat_core_question_generator.php b/question/tests/generator/behat_core_question_generator.php index 3a0eced04a4..fca9147e04e 100644 --- a/question/tests/generator/behat_core_question_generator.php +++ b/question/tests/generator/behat_core_question_generator.php @@ -41,6 +41,12 @@ class behat_core_question_generator extends behat_generator_base { 'required' => ['question', 'tag'], 'switchids' => ['question' => 'questionid'], ], + 'updated questions' => [ + 'singular' => 'question', + 'datagenerator' => 'updated_question', + 'required' => ['question', 'questioncategory'], + 'switchids' => ['question' => 'id', 'questioncategory' => 'category'], + ], ]; } @@ -58,4 +64,22 @@ class behat_core_question_generator extends behat_generator_base { } return $id; } + + /** + * Update a question + * + * This will update a question matching the supplied name with the provided data, creating a new version in the process. + * + * @param array $data the row of data from the behat script. + * @return void + */ + protected function process_updated_question(array $data): void { + global $DB; + $question = $DB->get_record('question', ['id' => $data['id']], '*', MUST_EXIST); + foreach ($data as $key => $value) { + $question->{$key} = $value; + } + + $this->datagenerator->get_plugin_generator('core_question')->update_question($question); + } }