Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311]

   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', 'group' => 'groupid'],
  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 or name.
  51       *
  52       * @param string $idnumberorname The forum activity idnumber or name.
  53       * @return int The forum id
  54       */
  55      protected function get_forum_id(string $idnumberorname): int {
  56          return $this->get_cm_by_activity_name('forum', $idnumberorname)->instance;
  57      }
  58  
  59      /**
  60       * Gets the group id from it's idnumber. It allows using 'All participants' as idnumber.
  61       *
  62       * @throws Exception
  63       * @param string $idnumber
  64       * @return int
  65       */
  66      protected function get_group_id($idnumber): int {
  67          if ($idnumber === 'All participants') {
  68              return -1;
  69          }
  70  
  71          return parent::get_group_id($idnumber);
  72      }
  73  
  74      /**
  75       * Preprocess discussion data.
  76       *
  77       * @param array $data Raw data.
  78       * @return array Processed data.
  79       */
  80      protected function preprocess_discussion(array $data) {
  81          global $DB, $USER;
  82  
  83          $forum = $DB->get_record('forum', ['id' => $data['forumid']]);
  84  
  85          unset($data['course']);
  86          unset($data['forumid']);
  87  
  88          return array_merge([
  89              'course' => $forum->course,
  90              'forum' => $forum->id,
  91              'userid' => $USER->id,
  92          ], $data);
  93      }
  94  
  95      /**
  96       * Preprocess post data.
  97       *
  98       * @param array $data Raw data.
  99       * @return array Processed data.
 100       */
 101      protected function preprocess_post(array $data) {
 102          global $DB, $USER;
 103  
 104          // Get discussion from name.
 105          $discussionfilters = array_filter([
 106              'name' => $data['discussion'] ?? null,
 107              'forum' => $data['forumid'] ?? null,
 108          ]);
 109  
 110          if (!empty($discussionfilters)) {
 111              if (!$discussionid = $DB->get_field('forum_discussions', 'id', $discussionfilters)) {
 112                  throw new Exception('The specified discussion with name "' . $data['name'] . '" could not be found.');
 113              }
 114  
 115              $data['discussion'] = $discussionid;
 116  
 117              unset($data['forumid']);
 118          }
 119  
 120          // Get discussion from parent.
 121          $parentfilters = array_filter([
 122              'subject' => $data['parentsubject'] ?? null,
 123          ]);
 124  
 125          if (!empty($parentfilters)) {
 126              if (isset($discussionid)) {
 127                  $parentfilters['discussion'] = $discussionid;
 128              }
 129  
 130              if (!$parent = $DB->get_record('forum_posts', $parentfilters)) {
 131                  $parentdescription = implode(' and ', array_filter([
 132                      isset($parentfilters['subject']) ? 'subject "' . $parentfilters['subject'] . '"' : null,
 133                  ]));
 134  
 135                  throw new Exception('The specified post with ' . $parentdescription . ' could not be found.');
 136              }
 137  
 138              $data['parent'] = $parent->id;
 139              $data['discussion'] = $parent->discussion;
 140  
 141              unset($data['parentsubject']);
 142          }
 143  
 144          // Return processed data.
 145          if (!isset($data['discussion'])) {
 146              throw new Exception('It was not possible to find a discussion to create a post, '.
 147                  'please specify discussion or parentsubject.');
 148          }
 149  
 150          return array_merge(['userid' => $USER->id], $data);
 151      }
 152  }