Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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 core_courseformat\output\local\state;
  18  
  19  /**
  20   * Tests for state classes (course, section, cm).
  21   *
  22   * @package    core
  23   * @subpackage course
  24   * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class state_test extends \advanced_testcase {
  28  
  29      /**
  30       * Setup to ensure that fixtures are loaded.
  31       */
  32      public static function setupBeforeClass(): void {
  33          global $CFG;
  34  
  35          require_once($CFG->dirroot . '/course/lib.php');
  36          require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
  37          require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_output_course_format_state.php');
  38          require_once($CFG->dirroot . '/lib/externallib.php');
  39      }
  40  
  41      /**
  42       * Test the behaviour of state::export_for_template().
  43       *
  44       * @dataProvider state_provider
  45       * @covers \core_courseformat\output\local\state
  46       *
  47       * @param string $format The course format of the course where the method will be executed.
  48       */
  49      public function test_state(string $format = 'topics') {
  50          global $PAGE;
  51  
  52          $this->resetAfterTest();
  53  
  54          // Create a course.
  55          $numsections = 6;
  56          $course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]);
  57          $hiddensections = [4, 6];
  58          foreach ($hiddensections as $section) {
  59              set_section_visible($course->id, $section, 0);
  60          }
  61  
  62          // Create and enrol user.
  63          $this->setAdminUser();
  64  
  65          // Add some activities to the course.
  66          $this->getDataGenerator()->create_module('page', ['course' => $course->id], ['section' => 1,
  67              'visible' => 1]);
  68          $this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['section' => 1,
  69              'visible' => 1]);
  70          $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 2,
  71              'visible' => 0]);
  72          $this->getDataGenerator()->create_module('glossary', ['course' => $course->id], ['section' => 4,
  73              'visible' => 1]);
  74          $this->getDataGenerator()->create_module('label', ['course' => $course->id], ['section' => 5,
  75              'visible' => 0]);
  76          $this->getDataGenerator()->create_module('feedback', ['course' => $course->id], ['section' => 5,
  77              'visible' => 1]);
  78  
  79          $courseformat = course_get_format($course->id);
  80          $modinfo = $courseformat->get_modinfo();
  81  
  82          $courseclass = $courseformat->get_output_classname('state\\course');
  83          $sectionclass = $courseformat->get_output_classname('state\\section');
  84          $cmclass = $courseformat->get_output_classname('state\\cm');
  85  
  86          // Get the proper renderer.
  87          $renderer = $courseformat->get_renderer($PAGE);
  88          $result = (object)[
  89              'course' => (object)[],
  90              'section' => [],
  91              'cm' => [],
  92          ];
  93  
  94          // General state.
  95          $coursestate = new $courseclass($courseformat);
  96          $result->course = $coursestate->export_for_template($renderer);
  97  
  98          if ($format == 'theunittest') {
  99              // These course format's hasn't the renderer file, so a debugging message will be displayed.
 100              $this->assertDebuggingCalled();
 101          }
 102  
 103          $this->assertEquals($course->id, $result->course->id);
 104          $this->assertEquals($numsections, $result->course->numsections);
 105          $this->assertFalse($result->course->editmode);
 106          $sections = $modinfo->get_section_info_all();
 107  
 108          foreach ($sections as $key => $section) {
 109              $this->assertEquals($section->id, $result->course->sectionlist[$key]);
 110  
 111              if (!empty($section->uservisible)) {
 112                  $sectionstate = new $sectionclass($courseformat, $section);
 113                  $result->section[$key] = $sectionstate->export_for_template($renderer);
 114                  $this->assertEquals($section->id, $result->section[$key]->id);
 115                  $this->assertEquals($section->section, $result->section[$key]->section);
 116                  $this->assertTrue($section->visible == $result->section[$key]->visible);
 117  
 118                  if ($key === 0 || $key === 3 || $key === 6) {
 119                      $this->assertEmpty($result->section[$key]->cmlist);
 120                  } else if ($key === 1) {
 121                      $this->assertEquals(2, count($result->section[$key]->cmlist));
 122                  } else if ($key === 2 || $key === 4) {
 123                      $this->assertEquals(1, count($result->section[$key]->cmlist));
 124                  } else if ($key === 5) {
 125                      $this->assertEquals(2, count($result->section[$key]->cmlist));
 126                  }
 127              }
 128          }
 129  
 130          foreach ($modinfo->cms as $key => $cm) {
 131              $section = $sections[$cm->sectionnum];
 132              $cmstate = new $cmclass($courseformat, $section, $cm);
 133              $result->cm[$key] = $cmstate->export_for_template($renderer);
 134              $this->assertEquals($cm->id, $result->cm[$key]->id);
 135              $this->assertEquals($cm->name, $result->cm[$key]->name);
 136              $this->assertTrue($cm->visible == $result->cm[$key]->visible);
 137          }
 138      }
 139  
 140      /**
 141       * Data provider for test_state().
 142       *
 143       * @return array
 144       */
 145      public function state_provider(): array {
 146          return [
 147              // COURSEFORMAT. Test behaviour depending on course formats.
 148              'Single activity format' => [
 149                  'format' => 'singleactivity',
 150              ],
 151              'Social format' => [
 152                  'format' => 'social',
 153              ],
 154              'Weeks format' => [
 155                  'format' => 'weeks',
 156              ],
 157              'The unit tests format' => [
 158                  'format' => 'theunittest',
 159              ],
 160          ];
 161      }
 162  }