Skip to content

Commit

Permalink
MDL-73202 mod_forum: Add behat generators
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Nov 30, 2021
1 parent 18b2af6 commit d5e1463
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 15 deletions.
24 changes: 9 additions & 15 deletions mod/forum/tests/behat/discussion_display.feature
Expand Up @@ -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
Expand Down
143 changes: 143 additions & 0 deletions mod/forum/tests/generator/behat_mod_forum_generator.php
@@ -0,0 +1,143 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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);
}
}

0 comments on commit d5e1463

Please sign in to comment.