Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 39 and 402]

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