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] [Versions 39 and 310]

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