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 311 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  /**
  18   * Contains the tests for the course_content_item_exporter class.
  19   *
  20   * @package    core
  21   * @subpackage course
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_course;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_course\local\exporters\course_content_item_exporter;
  30  use core_course\local\repository\content_item_readonly_repository;
  31  
  32  /**
  33   * The tests for the course_content_item_exporter class.
  34   *
  35   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class exporters_content_item_test extends \advanced_testcase {
  39  
  40      /**
  41       * Test confirming a content_item can be exported for a course.
  42       */
  43      public function test_export_course_content_item() {
  44          $this->resetAfterTest();
  45          global $PAGE;
  46  
  47          $course = $this->getDataGenerator()->create_course();
  48          $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  49          $cir = new content_item_readonly_repository();
  50          $contentitems = $cir->find_all_for_course($course, $user);
  51          $contentitem = array_shift($contentitems);
  52  
  53          $ciexporter = new course_content_item_exporter($contentitem, ['context' => \context_course::instance($course->id)]);
  54          $renderer = $PAGE->get_renderer('core');
  55          $exporteditem = $ciexporter->export($renderer);
  56  
  57          $this->assertObjectHasAttribute('id', $exporteditem);
  58          $this->assertEquals($exporteditem->id, $contentitem->get_id());
  59          $this->assertObjectHasAttribute('name', $exporteditem);
  60          $this->assertEquals($exporteditem->name, $contentitem->get_name());
  61          $this->assertObjectHasAttribute('title', $exporteditem);
  62          $this->assertEquals($exporteditem->title, $contentitem->get_title()->get_value());
  63          $this->assertObjectHasAttribute('link', $exporteditem);
  64          $this->assertEquals($exporteditem->link, $contentitem->get_link()->out(false));
  65          $this->assertObjectHasAttribute('icon', $exporteditem);
  66          $this->assertEquals($exporteditem->icon, $contentitem->get_icon());
  67          $this->assertObjectHasAttribute('help', $exporteditem);
  68          $this->assertEquals($exporteditem->help, format_text($contentitem->get_help(), FORMAT_MARKDOWN));
  69          $this->assertObjectHasAttribute('archetype', $exporteditem);
  70          $this->assertEquals($exporteditem->archetype, $contentitem->get_archetype());
  71          $this->assertObjectHasAttribute('componentname', $exporteditem);
  72          $this->assertEquals($exporteditem->componentname, $contentitem->get_component_name());
  73          $this->assertObjectHasAttribute('legacyitem', $exporteditem);
  74          $this->assertFalse($exporteditem->legacyitem);
  75      }
  76  
  77      /**
  78       * Test that legacy items (with id of -1) are exported correctly.
  79       */
  80      public function test_export_course_content_item_legacy() {
  81          $this->resetAfterTest();
  82          global $PAGE;
  83  
  84          $course = $this->getDataGenerator()->create_course();
  85  
  86          $contentitem = new \core_course\local\entity\content_item(
  87              -1,
  88              'test_name',
  89              new \core_course\local\entity\string_title('test_title'),
  90              new \moodle_url(''),
  91              '',
  92              '* First point
  93              * Another point',
  94              MOD_ARCHETYPE_OTHER,
  95              'core_test',
  96              MOD_PURPOSE_CONTENT
  97          );
  98  
  99          $ciexporter = new course_content_item_exporter($contentitem, ['context' => \context_course::instance($course->id)]);
 100          $renderer = $PAGE->get_renderer('core');
 101          $exporteditem = $ciexporter->export($renderer);
 102  
 103          $this->assertObjectHasAttribute('id', $exporteditem);
 104          $this->assertEquals($exporteditem->id, $contentitem->get_id());
 105          $this->assertObjectHasAttribute('name', $exporteditem);
 106          $this->assertEquals($exporteditem->name, $contentitem->get_name());
 107          $this->assertObjectHasAttribute('title', $exporteditem);
 108          $this->assertEquals($exporteditem->title, $contentitem->get_title()->get_value());
 109          $this->assertObjectHasAttribute('link', $exporteditem);
 110          $this->assertEquals($exporteditem->link, $contentitem->get_link()->out(false));
 111          $this->assertObjectHasAttribute('icon', $exporteditem);
 112          $this->assertEquals($exporteditem->icon, $contentitem->get_icon());
 113          $this->assertObjectHasAttribute('help', $exporteditem);
 114          $this->assertEquals($exporteditem->help, format_text($contentitem->get_help(), FORMAT_MARKDOWN));
 115          $this->assertObjectHasAttribute('archetype', $exporteditem);
 116          $this->assertEquals($exporteditem->archetype, $contentitem->get_archetype());
 117          $this->assertObjectHasAttribute('componentname', $exporteditem);
 118          $this->assertEquals($exporteditem->componentname, $contentitem->get_component_name());
 119          // Most important, is this a legacy item?
 120          $this->assertObjectHasAttribute('legacyitem', $exporteditem);
 121          $this->assertTrue($exporteditem->legacyitem);
 122      }
 123  }