Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 author 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\author as author_entity;
  28  use mod_forum\local\exporters\author as author_exporter;
  29  global $CFG;
  30  
  31  /**
  32   * The author exporter tests.
  33   *
  34   * @package    mod_forum
  35   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class mod_forum_exporters_author_testcase extends advanced_testcase {
  39      /**
  40       * Test the export function returns expected values.
  41       */
  42      public function test_export_author() {
  43          global $PAGE;
  44          $this->resetAfterTest();
  45  
  46          $renderer = $PAGE->get_renderer('core');
  47          $datagenerator = $this->getDataGenerator();
  48          $course = $datagenerator->create_course();
  49          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  50          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  51          $context = context_module::instance($coursemodule->id);
  52          $entityfactory = \mod_forum\local\container::get_entity_factory();
  53          $forum = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $course);
  54          $author = new author_entity(
  55              1,
  56              1,
  57              'test',
  58              'user',
  59              'test user',
  60              'test@example.com',
  61              false
  62          );
  63  
  64          $exporter = new author_exporter($author, 1, [], true, [
  65              'urlfactory' => \mod_forum\local\container::get_url_factory(),
  66              'context' => $context,
  67              'forum' => $forum,
  68          ]);
  69  
  70          $exportedauthor = $exporter->export($renderer);
  71  
  72          $this->assertEquals(1, $exportedauthor->id);
  73          $this->assertEquals('test user', $exportedauthor->fullname);
  74          $this->assertEquals([], $exportedauthor->groups);
  75          $this->assertNotEquals(null, $exportedauthor->urls['profile']);
  76          $this->assertNotEquals(null, $exportedauthor->urls['profileimage']);
  77      }
  78  
  79      /**
  80       * Test the export function with groups.
  81       */
  82      public function test_export_author_with_groups() {
  83          global $PAGE;
  84          $this->resetAfterTest();
  85  
  86          $renderer = $PAGE->get_renderer('core');
  87          $datagenerator = $this->getDataGenerator();
  88          $course = $datagenerator->create_course();
  89          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  90          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  91          $context = context_module::instance($coursemodule->id);
  92          $entityfactory = \mod_forum\local\container::get_entity_factory();
  93          $forum = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $course);
  94          $author = new author_entity(
  95              1,
  96              1,
  97              'test',
  98              'user',
  99              'test user',
 100              'test@example.com',
 101              false
 102          );
 103  
 104          $group = $datagenerator->create_group(['courseid' => $course->id]);
 105  
 106          $exporter = new author_exporter($author, 1, [$group], true, [
 107              'urlfactory' => \mod_forum\local\container::get_url_factory(),
 108              'context' => $context,
 109              'forum' => $forum,
 110          ]);
 111  
 112          $exportedauthor = $exporter->export($renderer);
 113  
 114          $this->assertCount(1, $exportedauthor->groups);
 115          $this->assertEquals($group->id, $exportedauthor->groups[0]['id']);
 116      }
 117  
 118      /**
 119       * Test the export function with no view capability.
 120       */
 121      public function test_export_author_no_view_capability() {
 122          global $PAGE;
 123          $this->resetAfterTest();
 124  
 125          $renderer = $PAGE->get_renderer('core');
 126          $datagenerator = $this->getDataGenerator();
 127          $course = $datagenerator->create_course();
 128          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 129          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
 130          $context = context_module::instance($coursemodule->id);
 131          $entityfactory = \mod_forum\local\container::get_entity_factory();
 132          $forum = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $course);
 133          $author = new author_entity(
 134              1,
 135              1,
 136              'test',
 137              'user',
 138              'test user',
 139              'test@example.com',
 140              false
 141          );
 142  
 143          $group = $datagenerator->create_group(['courseid' => $course->id]);
 144  
 145          $exporter = new author_exporter($author, 1, [$group], false, [
 146              'urlfactory' => \mod_forum\local\container::get_url_factory(),
 147              'context' => $context,
 148              'forum' => $forum,
 149          ]);
 150  
 151          $exportedauthor = $exporter->export($renderer);
 152  
 153          $this->assertEquals(null, $exportedauthor->id);
 154          $this->assertNotEquals('test user', $exportedauthor->fullname);
 155          $this->assertEquals([], $exportedauthor->groups);
 156          $this->assertEquals(null, $exportedauthor->urls['profile']);
 157          $this->assertEquals(null, $exportedauthor->urls['profileimage']);
 158      }
 159  }