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   * The discussion vault tests.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once (__DIR__ . '/generator_trait.php');
  28  
  29  /**
  30   * The discussion vault tests.
  31   *
  32   * @package    mod_forum
  33   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class mod_forum_vaults_discussion_testcase extends advanced_testcase {
  37      // Make use of the test generator trait.
  38      use mod_forum_tests_generator_trait;
  39  
  40      /** @var \mod_forum\local\vaults\discussion */
  41      private $vault;
  42  
  43      /**
  44       * Set up function for tests.
  45       */
  46      public function setUp(): void {
  47          $vaultfactory = \mod_forum\local\container::get_vault_factory();
  48          $this->vault = $vaultfactory->get_discussion_vault();
  49      }
  50  
  51      /**
  52       * Test get_from_id.
  53       */
  54      public function test_get_from_id() {
  55          $this->resetAfterTest();
  56  
  57          $vault = $this->vault;
  58          $datagenerator = $this->getDataGenerator();
  59          $user = $datagenerator->create_user();
  60          $course = $datagenerator->create_course();
  61          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  62          [$discussionrecord, $post] = $this->helper_post_to_forum($forum, $user);
  63  
  64          $discussion = $vault->get_from_id($discussionrecord->id);
  65  
  66          $this->assertEquals($discussionrecord->id, $discussion->get_id());
  67      }
  68  
  69      /**
  70       * Test get_first_discussion_in_forum.
  71       */
  72      public function test_get_first_discussion_in_forum() {
  73          $this->resetAfterTest();
  74  
  75          $vault = $this->vault;
  76          $entityfactory = \mod_forum\local\container::get_entity_factory();
  77          $datagenerator = $this->getDataGenerator();
  78          $user = $datagenerator->create_user();
  79          $course = $datagenerator->create_course();
  80          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  81          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  82          $context = context_module::instance($coursemodule->id);
  83          $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $course);
  84  
  85          $this->assertEquals(null, $vault->get_first_discussion_in_forum($forumentity));
  86  
  87          [$discussion1, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 2]);
  88          [$discussion2, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 1]);
  89          [$discussion3, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 3]);
  90  
  91          $discussionentity = $vault->get_first_discussion_in_forum($forumentity);
  92          $this->assertEquals($discussion2->id, $discussionentity->get_id());
  93      }
  94  
  95      /**
  96       * Test get_all_discussions_in_forum
  97       */
  98      public function test_get_all_discussions_in_forum() {
  99          $this->resetAfterTest();
 100  
 101          $vault = $this->vault;
 102          $entityfactory = \mod_forum\local\container::get_entity_factory();
 103          $datagenerator = $this->getDataGenerator();
 104          $user = $datagenerator->create_user();
 105          $course = $datagenerator->create_course();
 106          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 107          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
 108          $context = context_module::instance($coursemodule->id);
 109          $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $course);
 110  
 111          $this->assertEquals([], $vault->get_all_discussions_in_forum($forumentity));
 112  
 113          [$discussion1, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 2]);
 114          [$discussion2, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 1]);
 115          [$discussion3, $post] = $this->helper_post_to_forum($forum, $user, ['timemodified' => 3]);
 116  
 117          $discussionentity = $vault->get_all_discussions_in_forum($forumentity);
 118          $this->assertArrayHasKey($discussion1->id, $discussionentity); // Order is not guaranteed, so just verify element existence.
 119          $this->assertArrayHasKey($discussion2->id, $discussionentity);
 120          $this->assertArrayHasKey($discussion3->id, $discussionentity);
 121      }
 122  }