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]

   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\author as author_entity;
  20  use mod_forum\local\exporters\author as author_exporter;
  21  
  22  /**
  23   * The author 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_author_test extends \advanced_testcase {
  30      /**
  31       * Test the export function returns expected values.
  32       */
  33      public function test_export_author() {
  34          global $PAGE;
  35          $this->resetAfterTest();
  36  
  37          $renderer = $PAGE->get_renderer('core');
  38          $datagenerator = $this->getDataGenerator();
  39          $course = $datagenerator->create_course();
  40          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  41          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  42          $context = \context_module::instance($coursemodule->id);
  43          $entityfactory = \mod_forum\local\container::get_entity_factory();
  44          $forum = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
  45          $author = new author_entity(
  46              1,
  47              1,
  48              'test',
  49              'user',
  50              'test user',
  51              'test@example.com',
  52              false
  53          );
  54  
  55          $exporter = new author_exporter($author, 1, [], true, [
  56              'urlfactory' => \mod_forum\local\container::get_url_factory(),
  57              'context' => $context,
  58              'forum' => $forum,
  59          ]);
  60  
  61          $exportedauthor = $exporter->export($renderer);
  62  
  63          $this->assertEquals(1, $exportedauthor->id);
  64          $this->assertEquals('test user', $exportedauthor->fullname);
  65          $this->assertEquals([], $exportedauthor->groups);
  66          $this->assertNotEquals(null, $exportedauthor->urls['profile']);
  67          $this->assertNotEquals(null, $exportedauthor->urls['profileimage']);
  68      }
  69  
  70      /**
  71       * Test the export function with groups.
  72       */
  73      public function test_export_author_with_groups() {
  74          global $PAGE;
  75          $this->resetAfterTest();
  76  
  77          $renderer = $PAGE->get_renderer('core');
  78          $datagenerator = $this->getDataGenerator();
  79          $course = $datagenerator->create_course();
  80          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  81          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  82          $context = \context_module::instance($coursemodule->id);
  83          $entityfactory = \mod_forum\local\container::get_entity_factory();
  84          $forum = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
  85          $author = new author_entity(
  86              1,
  87              1,
  88              'test',
  89              'user',
  90              'test user',
  91              'test@example.com',
  92              false
  93          );
  94  
  95          $group = $datagenerator->create_group(['courseid' => $course->id]);
  96  
  97          $exporter = new author_exporter($author, 1, [$group], true, [
  98              'urlfactory' => \mod_forum\local\container::get_url_factory(),
  99              'context' => $context,
 100              'forum' => $forum,
 101          ]);
 102  
 103          $exportedauthor = $exporter->export($renderer);
 104  
 105          $this->assertCount(1, $exportedauthor->groups);
 106          $this->assertEquals($group->id, $exportedauthor->groups[0]['id']);
 107      }
 108  
 109      /**
 110       * Test the export function with no view capability.
 111       */
 112      public function test_export_author_no_view_capability() {
 113          global $PAGE;
 114          $this->resetAfterTest();
 115  
 116          $renderer = $PAGE->get_renderer('core');
 117          $datagenerator = $this->getDataGenerator();
 118          $course = $datagenerator->create_course();
 119          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
 120          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
 121          $context = \context_module::instance($coursemodule->id);
 122          $entityfactory = \mod_forum\local\container::get_entity_factory();
 123          $forum = $entityfactory->get_forum_from_stdClass($forum, $context, $coursemodule, $course);
 124          $author = new author_entity(
 125              1,
 126              1,
 127              'test',
 128              'user',
 129              'test user',
 130              'test@example.com',
 131              false
 132          );
 133  
 134          $group = $datagenerator->create_group(['courseid' => $course->id]);
 135  
 136          $exporter = new author_exporter($author, 1, [$group], false, [
 137              'urlfactory' => \mod_forum\local\container::get_url_factory(),
 138              'context' => $context,
 139              'forum' => $forum,
 140          ]);
 141  
 142          $exportedauthor = $exporter->export($renderer);
 143  
 144          $this->assertEquals(null, $exportedauthor->id);
 145          $this->assertNotEquals('test user', $exportedauthor->fullname);
 146          $this->assertEquals([], $exportedauthor->groups);
 147          $this->assertEquals(null, $exportedauthor->urls['profile']);
 148          $this->assertEquals(null, $exportedauthor->urls['profileimage']);
 149      }
 150  }