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