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 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 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\local\entities\discussion as discussion_entity;
  20  use mod_forum\local\exporters\discussion as discussion_exporter;
  21  
  22  /**
  23   * The discussion exporter tests.
  24   *
  25   * @package    mod_forum
  26   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class exporters_discussion_test extends \advanced_testcase {
  30  
  31      /** @var \mod_forum\local\builders\exported_posts */
  32      private $builder;
  33  
  34      /**
  35       * Test set up function.
  36       */
  37      public function setUp(): void {
  38          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  39          // tests using these functions.
  40          \mod_forum\subscriptions::reset_forum_cache();
  41  
  42          $builderfactory = \mod_forum\local\container::get_builder_factory();
  43          $this->builder = $builderfactory->get_exported_posts_builder();
  44      }
  45  
  46      /**
  47       * Test tear down function.
  48       */
  49      public function tearDown(): void {
  50          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  51          // tests using these functions.
  52          \mod_forum\subscriptions::reset_forum_cache();
  53      }
  54  
  55      /**
  56       * Test the export function returns expected values.
  57       */
  58      public function test_export() {
  59          global $PAGE;
  60          $this->resetAfterTest();
  61  
  62          $renderer = $PAGE->get_renderer('core');
  63          $datagenerator = $this->getDataGenerator();
  64          $user = $datagenerator->create_user();
  65          $course = $datagenerator->create_course();
  66          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  67          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  68          $context = \context_module::instance($coursemodule->id);
  69          $entityfactory = \mod_forum\local\container::get_entity_factory();
  70          $forum = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
  71          $group = $datagenerator->create_group(['courseid' => $course->id]);
  72          $now = time();
  73          $discussion = new discussion_entity(
  74              1,
  75              $course->id,
  76              $forum->get_id(),
  77              'test discussion',
  78              1,
  79              $user->id,
  80              $group->id,
  81              false,
  82              $now,
  83              $now,
  84              0,
  85              0,
  86              false,
  87              0
  88          );
  89  
  90          $exporter = new discussion_exporter($discussion, [
  91              'legacydatamapperfactory' => \mod_forum\local\container::get_legacy_data_mapper_factory(),
  92              'urlfactory' => \mod_forum\local\container::get_url_factory(),
  93              'capabilitymanager' => (\mod_forum\local\container::get_manager_factory())->get_capability_manager($forum),
  94              'context' => $context,
  95              'forum' => $forum,
  96              'user' => $user,
  97              'groupsbyid' => [$group->id => $group],
  98              'latestpostid' => 7
  99          ]);
 100  
 101          $exporteddiscussion = $exporter->export($renderer);
 102  
 103          $this->assertEquals(1, $exporteddiscussion->id);
 104          $this->assertEquals($forum->get_id(), $exporteddiscussion->forumid);
 105          $this->assertEquals(false, $exporteddiscussion->pinned);
 106          $this->assertEquals('test discussion', $exporteddiscussion->name);
 107          $this->assertEquals($now, $exporteddiscussion->times['modified']);
 108          $this->assertEquals(0, $exporteddiscussion->times['start']);
 109          $this->assertEquals(0, $exporteddiscussion->times['end']);
 110          $this->assertEquals($group->name, $exporteddiscussion->group['name']);
 111      }
 112  }