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]

   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 forum 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 forum 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   * @coversDefaultClass \mod_forum\local\vaults\forum
  36   */
  37  class mod_forum_vaults_forum_testcase extends advanced_testcase {
  38      // Make use of the test generator trait.
  39      use mod_forum_tests_generator_trait;
  40  
  41      /**
  42       * Test get_from_id.
  43       *
  44       * @covers ::get_from_id
  45       */
  46      public function test_get_from_id() {
  47          $this->resetAfterTest();
  48  
  49          $vaultfactory = \mod_forum\local\container::get_vault_factory();
  50          $vault = $vaultfactory->get_forum_vault();
  51          $datagenerator = $this->getDataGenerator();
  52          $user = $datagenerator->create_user();
  53          $course = $datagenerator->create_course();
  54          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  55  
  56          $entity = $vault->get_from_id($forum->id);
  57  
  58          $this->assertEquals($forum->id, $entity->get_id());
  59      }
  60  
  61      /**
  62       * Test get_from_course_module_id.
  63       *
  64       * @covers ::get_from_course_module_id
  65       */
  66      public function test_get_from_course_module_id() {
  67          $this->resetAfterTest();
  68  
  69          $vaultfactory = \mod_forum\local\container::get_vault_factory();
  70          $vault = $vaultfactory->get_forum_vault();
  71          $datagenerator = $this->getDataGenerator();
  72          $user = $datagenerator->create_user();
  73          $course = $datagenerator->create_course();
  74          $forum1 = $datagenerator->create_module('forum', ['course' => $course->id]);
  75          $forum2 = $datagenerator->create_module('forum', ['course' => $course->id]);
  76          $coursemodule1 = get_coursemodule_from_instance('forum', $forum1->id);
  77          $coursemodule2 = get_coursemodule_from_instance('forum', $forum2->id);
  78  
  79          // Don't exist.
  80          $entity = $vault->get_from_course_module_id($coursemodule1->id + 100);
  81          $this->assertEquals(null, $entity);
  82  
  83          $entity = $vault->get_from_course_module_id($coursemodule1->id);
  84          $this->assertEquals($forum1->id, $entity->get_id());
  85      }
  86  
  87      /**
  88       * Test get_from_course_module_ids.
  89       *
  90       * @covers ::get_from_course_module_ids
  91       */
  92      public function test_get_from_course_module_ids() {
  93          $this->resetAfterTest();
  94  
  95          $vaultfactory = \mod_forum\local\container::get_vault_factory();
  96          $vault = $vaultfactory->get_forum_vault();
  97          $datagenerator = $this->getDataGenerator();
  98          $user = $datagenerator->create_user();
  99          $course = $datagenerator->create_course();
 100          $forum1 = $datagenerator->create_module('forum', ['course' => $course->id]);
 101          $forum2 = $datagenerator->create_module('forum', ['course' => $course->id]);
 102          $coursemodule1 = get_coursemodule_from_instance('forum', $forum1->id);
 103          $coursemodule2 = get_coursemodule_from_instance('forum', $forum2->id);
 104  
 105          // Don't exist.
 106          $entities = array_values($vault->get_from_course_module_ids([$coursemodule1->id + 100, $coursemodule1->id + 200]));
 107          $this->assertEquals([], $entities);
 108  
 109          $entities = array_values($vault->get_from_course_module_ids([$coursemodule1->id, $coursemodule2->id]));
 110          usort($entities, function($a, $b) {
 111              return $a->get_id() <=> $b->get_id();
 112          });
 113          $this->assertCount(2, $entities);
 114          $this->assertEquals($forum1->id, $entities[0]->get_id());
 115          $this->assertEquals($forum2->id, $entities[1]->get_id());
 116  
 117          $entities = array_values($vault->get_from_course_module_ids([$coursemodule1->id]));
 118          usort($entities, function($a, $b) {
 119              return $a->get_id() <=> $b->get_id();
 120          });
 121          $this->assertCount(1, $entities);
 122          $this->assertEquals($forum1->id, $entities[0]->get_id());
 123      }
 124  
 125      /**
 126       * Test get_from_post_id.
 127       *
 128       * @covers ::get_from_post_id
 129       */
 130      public function test_get_from_post_id() {
 131          $this->resetAfterTest();
 132  
 133          $vaultfactory = \mod_forum\local\container::get_vault_factory();
 134          $vault = $vaultfactory->get_forum_vault();
 135  
 136          $datagenerator = $this->getDataGenerator();
 137          $user = $datagenerator->create_user();
 138          $course = $datagenerator->create_course();
 139          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 140          [$discussion, $post] = $this->helper_post_to_forum($forum, $user);
 141          $reply = $this->helper_reply_to_post($post, $user);
 142  
 143          $otherforum = $datagenerator->create_module('forum', ['course' => $course->id]);
 144          [$otherdiscussion, $otherpost] = $this->helper_post_to_forum($otherforum, $user);
 145          $otherreply = $this->helper_reply_to_post($otherpost, $user);
 146  
 147          $entity = $vault->get_from_post_id($post->id);
 148          $this->assertEquals($forum->id, $entity->get_id());
 149  
 150          $entity = $vault->get_from_post_id($reply->id);
 151          $this->assertEquals($forum->id, $entity->get_id());
 152  
 153          $this->assertEmpty($vault->get_from_post_id(-1));
 154      }
 155  }