From d5e1463c4be982e8858774d24a6035f02e8c76f0 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Mon, 29 Nov 2021 15:22:14 +0100 Subject: [PATCH] MDL-73202 mod_forum: Add behat generators --- .../tests/behat/discussion_display.feature | 24 ++- .../generator/behat_mod_forum_generator.php | 143 ++++++++++++++++++ 2 files changed, 152 insertions(+), 15 deletions(-) create mode 100644 mod/forum/tests/generator/behat_mod_forum_generator.php diff --git a/mod/forum/tests/behat/discussion_display.feature b/mod/forum/tests/behat/discussion_display.feature index 252f396ae26..335bc3334f6 100644 --- a/mod/forum/tests/behat/discussion_display.feature +++ b/mod/forum/tests/behat/discussion_display.feature @@ -18,21 +18,15 @@ Feature: Students can choose from 4 discussion display options and their choice | course | C1 | | activity | forum | | name | Test forum name | - And I am on the "Course 1" course page logged in as admin - And I add a new discussion to "Test forum name" forum with: - | Subject | Discussion 1 | - | Message | Discussion contents 1, first message | - And I reply "Discussion 1" post from "Test forum name" forum with: - | Subject | Reply 1 to discussion 1 | - | Message | Discussion contents 1, second message | - And I am on the "Course 1" course page - And I add a new discussion to "Test forum name" forum with: - | Subject | Discussion 2 | - | Message | Discussion contents 2, first message | - And I reply "Discussion 2" post from "Test forum name" forum with: - | Subject | Reply 1 to discussion 2 | - | Message | Discussion contents 2, second message | - And I log out + | idnumber | forum | + And the following "mod_forum > discussions" exist: + | forum | name | subject | message | + | forum | Discussion 1 | Discussion 1 | Discussion contents 1, first message | + | forum | Discussion 2 | Discussion 2 | Discussion contents 2, first message | + And the following "mod_forum > posts" exist: + | parentsubject | subject | message | + | Discussion 1 | Reply 1 to discussion 1 | Discussion contents 1, second message | + | Discussion 2 | Reply 1 to discussion 2 | Discussion contents 2, second message | Scenario: Display replies flat, with oldest first Given I am on the "Course 1" course page logged in as student1 diff --git a/mod/forum/tests/generator/behat_mod_forum_generator.php b/mod/forum/tests/generator/behat_mod_forum_generator.php new file mode 100644 index 00000000000..eb7aa79fe88 --- /dev/null +++ b/mod/forum/tests/generator/behat_mod_forum_generator.php @@ -0,0 +1,143 @@ +. + +/** + * Behat data generator for mod_forum. + * + * @package mod_forum + * @category test + * @copyright 2021 Noel De Martin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_forum_generator extends behat_generator_base { + + /** + * Get a list of the entities that Behat can create using the generator step. + * + * @return array + */ + protected function get_creatable_entities(): array { + return [ + 'discussions' => [ + 'singular' => 'discussion', + 'datagenerator' => 'discussion', + 'required' => ['forum'], + 'switchids' => ['forum' => 'forumid', 'user' => 'userid'], + ], + 'posts' => [ + 'singular' => 'post', + 'datagenerator' => 'post', + 'required' => [], + 'switchids' => ['forum' => 'forumid', 'user' => 'userid'], + ], + ]; + } + + /** + * Get the forum id using an activity idnumber. + * + * @param string $idnumber + * @return int The forum id + */ + protected function get_forum_id(string $idnumber): int { + global $DB; + + if (!$id = $DB->get_field('course_modules', 'instance', ['idnumber' => $idnumber])) { + throw new Exception('The specified activity with idnumber "' . $idnumber . '" could not be found.'); + } + + return $id; + } + + /** + * Preprocess discussion data. + * + * @param array $data Raw data. + * @return array Processed data. + */ + protected function preprocess_discussion(array $data) { + global $DB, $USER; + + $forum = $DB->get_record('forum', ['id' => $data['forumid']]); + + unset($data['course']); + unset($data['forumid']); + + return array_merge([ + 'course' => $forum->course, + 'forum' => $forum->id, + 'userid' => $USER->id, + ], $data); + } + + /** + * Preprocess post data. + * + * @param array $data Raw data. + * @return array Processed data. + */ + protected function preprocess_post(array $data) { + global $DB, $USER; + + // Get discussion from name. + $discussionfilters = array_filter([ + 'name' => $data['discussion'] ?? null, + 'forum' => $data['forumid'] ?? null, + ]); + + if (!empty($discussionfilters)) { + if (!$discussionid = $DB->get_field('forum_discussions', 'id', $discussionfilters)) { + throw new Exception('The specified discussion with name "' . $data['name'] . '" could not be found.'); + } + + $data['discussion'] = $discussionid; + + unset($data['forumid']); + } + + // Get discussion from parent. + $parentfilters = array_filter([ + 'subject' => $data['parentsubject'] ?? null, + ]); + + if (!empty($parentfilters)) { + if (isset($discussionid)) { + $parentfilters['discussion'] = $discussionid; + } + + if (!$parent = $DB->get_record('forum_posts', $parentfilters)) { + $parentdescription = implode(' and ', array_filter([ + isset($parentfilters['subject']) ? 'subject "' . $parentfilters['subject'] . '"' : null, + ])); + + throw new Exception('The specified post with ' . $parentdescription . ' could not be found.'); + } + + $data['parent'] = $parent->id; + $data['discussion'] = $parent->discussion; + + unset($data['parentsubject']); + } + + // Return processed data. + if (!isset($data['discussion'])) { + throw new Exception('It was not possible to find a discussion to create a post, '. + 'please specify discussion or parentsubject.'); + } + + return array_merge(['userid' => $USER->id], $data); + } +}