Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Behat data generator for mod_forum.
  19   *
  20   * @package   mod_forum
  21   * @category  test
  22   * @copyright 2021 Noel De Martin
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  class behat_mod_forum_generator extends behat_generator_base {
  26  
  27      /**
  28       * Get a list of the entities that Behat can create using the generator step.
  29       *
  30       * @return array
  31       */
  32      protected function get_creatable_entities(): array {
  33          return [
  34              'discussions' => [
  35                  'singular' => 'discussion',
  36                  'datagenerator' => 'discussion',
  37                  'required' => ['forum'],
  38                  'switchids' => ['forum' => 'forumid', 'user' => 'userid'],
  39              ],
  40              'posts' => [
  41                  'singular' => 'post',
  42                  'datagenerator' => 'post',
  43                  'required' => [],
  44                  'switchids' => ['forum' => 'forumid', 'user' => 'userid'],
  45              ],
  46          ];
  47      }
  48  
  49      /**
  50       * Get the forum id using an activity idnumber.
  51       *
  52       * @param string $idnumber
  53       * @return int The forum id
  54       */
  55      protected function get_forum_id(string $idnumber): int {
  56          global $DB;
  57  
  58          if (!$id = $DB->get_field('course_modules', 'instance', ['idnumber' => $idnumber])) {
  59              throw new Exception('The specified activity with idnumber "' . $idnumber . '" could not be found.');
  60          }
  61  
  62          return $id;
  63      }
  64  
  65      /**
  66       * Preprocess discussion data.
  67       *
  68       * @param array $data Raw data.
  69       * @return array Processed data.
  70       */
  71      protected function preprocess_discussion(array $data) {
  72          global $DB, $USER;
  73  
  74          $forum = $DB->get_record('forum', ['id' => $data['forumid']]);
  75  
  76          unset($data['course']);
  77          unset($data['forumid']);
  78  
  79          return array_merge([
  80              'course' => $forum->course,
  81              'forum' => $forum->id,
  82              'userid' => $USER->id,
  83          ], $data);
  84      }
  85  
  86      /**
  87       * Preprocess post data.
  88       *
  89       * @param array $data Raw data.
  90       * @return array Processed data.
  91       */
  92      protected function preprocess_post(array $data) {
  93          global $DB, $USER;
  94  
  95          // Get discussion from name.
  96          $discussionfilters = array_filter([
  97              'name' => $data['discussion'] ?? null,
  98              'forum' => $data['forumid'] ?? null,
  99          ]);
 100  
 101          if (!empty($discussionfilters)) {
 102              if (!$discussionid = $DB->get_field('forum_discussions', 'id', $discussionfilters)) {
 103                  throw new Exception('The specified discussion with name "' . $data['name'] . '" could not be found.');
 104              }
 105  
 106              $data['discussion'] = $discussionid;
 107  
 108              unset($data['forumid']);
 109          }
 110  
 111          // Get discussion from parent.
 112          $parentfilters = array_filter([
 113              'subject' => $data['parentsubject'] ?? null,
 114          ]);
 115  
 116          if (!empty($parentfilters)) {
 117              if (isset($discussionid)) {
 118                  $parentfilters['discussion'] = $discussionid;
 119              }
 120  
 121              if (!$parent = $DB->get_record('forum_posts', $parentfilters)) {
 122                  $parentdescription = implode(' and ', array_filter([
 123                      isset($parentfilters['subject']) ? 'subject "' . $parentfilters['subject'] . '"' : null,
 124                  ]));
 125  
 126                  throw new Exception('The specified post with ' . $parentdescription . ' could not be found.');
 127              }
 128  
 129              $data['parent'] = $parent->id;
 130              $data['discussion'] = $parent->discussion;
 131  
 132              unset($data['parentsubject']);
 133          }
 134  
 135          // Return processed data.
 136          if (!isset($data['discussion'])) {
 137              throw new Exception('It was not possible to find a discussion to create a post, '.
 138                  'please specify discussion or parentsubject.');
 139          }
 140  
 141          return array_merge(['userid' => $USER->id], $data);
 142      }
 143  }