Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402]

   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      }
  39  
  40      /**
  41       * Test the behaviour of state::export_for_template().
  42       *
  43       * @dataProvider state_provider
  44       * @covers \core_courseformat\output\local\state
  45       *
  46       * @param string $format The course format of the course where the method will be executed.
  47       */
  48      public function test_state(string $format = 'topics') {
  49          global $PAGE;
  50  
  51          $this->resetAfterTest();
  52  
  53          // Create a course.
  54          $numsections = 6;
  55          $course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]);
  56          $hiddensections = [4, 6];
  57          foreach ($hiddensections as $section) {
  58              set_section_visible($course->id, $section, 0);
  59          }
  60  
  61          // Create and enrol user.
  62          $this->setAdminUser();
  63  
  64          // Add some activities to the course.
  65          $this->getDataGenerator()->create_module('page', ['course' => $course->id], ['section' => 1,
  66              'visible' => 1]);
  67          $this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['section' => 1,
  68              'visible' => 1]);
  69          $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 2,
  70              'visible' => 0]);
  71          $this->getDataGenerator()->create_module('glossary', ['course' => $course->id], ['section' => 4,
  72              'visible' => 1]);
  73          $this->getDataGenerator()->create_module('label', ['course' => $course->id], ['section' => 5,
  74              'visible' => 0]);
  75          $this->getDataGenerator()->create_module('feedback', ['course' => $course->id], ['section' => 5,
  76              'visible' => 1]);
  77  
  78          $courseformat = course_get_format($course->id);
  79          $modinfo = $courseformat->get_modinfo();
  80  
  81          $courseclass = $courseformat->get_output_classname('state\\course');
  82          $sectionclass = $courseformat->get_output_classname('state\\section');
  83          $cmclass = $courseformat->get_output_classname('state\\cm');
  84  
  85          // Get the proper renderer.
  86          $renderer = $courseformat->get_renderer($PAGE);
  87          $result = (object)[
  88              'course' => (object)[],
  89              'section' => [],
  90              'cm' => [],
  91          ];
  92  
  93          // General state.
  94          $coursestate = new $courseclass($courseformat);
  95          $result->course = $coursestate->export_for_template($renderer);
  96  
  97          if ($format == 'theunittest') {
  98              // These course format's hasn't the renderer file, so a debugging message will be displayed.
  99              $this->assertDebuggingCalled();
 100          }
 101  
 102          $this->assertEquals($course->id, $result->course->id);
 103          $this->assertEquals($numsections, $result->course->numsections);
 104          $this->assertFalse($result->course->editmode);
 105          $sections = $modinfo->get_section_info_all();
 106  
 107          foreach ($sections as $key => $section) {
 108              $this->assertEquals($section->id, $result->course->sectionlist[$key]);
 109  
 110              if (!empty($section->uservisible)) {
 111                  $sectionstate = new $sectionclass($courseformat, $section);
 112                  $result->section[$key] = $sectionstate->export_for_template($renderer);
 113                  $this->assertEquals($section->id, $result->section[$key]->id);
 114                  $this->assertEquals($section->section, $result->section[$key]->section);
 115                  $this->assertTrue($section->visible == $result->section[$key]->visible);
 116  
 117                  if ($key === 0 || $key === 3 || $key === 6) {
 118                      $this->assertEmpty($result->section[$key]->cmlist);
 119                  } else if ($key === 1) {
 120                      $this->assertEquals(2, count($result->section[$key]->cmlist));
 121                  } else if ($key === 2 || $key === 4) {
 122                      $this->assertEquals(1, count($result->section[$key]->cmlist));
 123                  } else if ($key === 5) {
 124                      $this->assertEquals(2, count($result->section[$key]->cmlist));
 125                  }
 126              }
 127          }
 128  
 129          foreach ($modinfo->cms as $key => $cm) {
 130              $section = $sections[$cm->sectionnum];
 131              $cmstate = new $cmclass($courseformat, $section, $cm);
 132              $result->cm[$key] = $cmstate->export_for_template($renderer);
 133              $this->assertEquals($cm->id, $result->cm[$key]->id);
 134              $this->assertEquals($cm->name, $result->cm[$key]->name);
 135              $this->assertTrue($cm->visible == $result->cm[$key]->visible);
 136          }
 137      }
 138  
 139      /**
 140       * Data provider for test_state().
 141       *
 142       * @return array
 143       */
 144      public function state_provider(): array {
 145          return [
 146              // COURSEFORMAT. Test behaviour depending on course formats.
 147              'Single activity format' => [
 148                  'format' => 'singleactivity',
 149              ],
 150              'Social format' => [
 151                  'format' => 'social',
 152              ],
 153              'Weeks format' => [
 154                  'format' => 'weeks',
 155              ],
 156              'The unit tests format' => [
 157                  'format' => 'theunittest',
 158              ],
 159          ];
 160      }
 161  }