Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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  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       * Test set up function.
  32       */
  33      public function setUp(): void {
  34          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  35          // tests using these functions.
  36          \mod_forum\subscriptions::reset_forum_cache();
  37  
  38          $builderfactory = \mod_forum\local\container::get_builder_factory();
  39          $this->builder = $builderfactory->get_exported_posts_builder();
  40      }
  41  
  42      /**
  43       * Test tear down function.
  44       */
  45      public function tearDown(): void {
  46          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  47          // tests using these functions.
  48          \mod_forum\subscriptions::reset_forum_cache();
  49      }
  50  
  51      /**
  52       * Test the export function returns expected values.
  53       */
  54      public function test_export() {
  55          global $PAGE;
  56          $this->resetAfterTest();
  57  
  58          $renderer = $PAGE->get_renderer('core');
  59          $datagenerator = $this->getDataGenerator();
  60          $user = $datagenerator->create_user();
  61          $course = $datagenerator->create_course();
  62          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  63          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  64          $context = \context_module::instance($coursemodule->id);
  65          $entityfactory = \mod_forum\local\container::get_entity_factory();
  66          $forum = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
  67          $group = $datagenerator->create_group(['courseid' => $course->id]);
  68          $now = time();
  69          $discussion = new discussion_entity(
  70              1,
  71              $course->id,
  72              $forum->get_id(),
  73              'test discussion',
  74              1,
  75              $user->id,
  76              $group->id,
  77              false,
  78              $now,
  79              $now,
  80              0,
  81              0,
  82              false,
  83              0
  84          );
  85  
  86          $exporter = new discussion_exporter($discussion, [
  87              'legacydatamapperfactory' => \mod_forum\local\container::get_legacy_data_mapper_factory(),
  88              'urlfactory' => \mod_forum\local\container::get_url_factory(),
  89              'capabilitymanager' => (\mod_forum\local\container::get_manager_factory())->get_capability_manager($forum),
  90              'context' => $context,
  91              'forum' => $forum,
  92              'user' => $user,
  93              'groupsbyid' => [$group->id => $group],
  94              'latestpostid' => 7
  95          ]);
  96  
  97          $exporteddiscussion = $exporter->export($renderer);
  98  
  99          $this->assertEquals(1, $exporteddiscussion->id);
 100          $this->assertEquals($forum->get_id(), $exporteddiscussion->forumid);
 101          $this->assertEquals(false, $exporteddiscussion->pinned);
 102          $this->assertEquals('test discussion', $exporteddiscussion->name);
 103          $this->assertEquals($now, $exporteddiscussion->times['modified']);
 104          $this->assertEquals(0, $exporteddiscussion->times['start']);
 105          $this->assertEquals(0, $exporteddiscussion->times['end']);
 106          $this->assertEquals($group->name, $exporteddiscussion->group['name']);
 107      }
 108  }