Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 block_html;
  18  
  19  /**
  20   * Unit test for block_html class.
  21   *
  22   * @package   block_html
  23   * @copyright 2022 Open LMS (https://www.openlms.net/)
  24   * @author    Petr Skoda
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   *
  27   * @coversDefaultClass \block_html
  28   */
  29  class block_html_test extends \advanced_testcase {
  30      /**
  31       * Tests instance files copying.
  32       * @covers ::instance_copy
  33       */
  34      public function test_instance_copy() {
  35          global $USER;
  36          $this->resetAfterTest();
  37  
  38          $this->setAdminUser();
  39          $fs = get_file_storage();
  40  
  41          $course = $this->getDataGenerator()->create_course();
  42          $block1 = $this->create_block($course);
  43          $itemid = file_get_unused_draft_itemid();
  44          $fs = get_file_storage();
  45          $usercontext = \context_user::instance($USER->id);
  46          $fs->create_file_from_string(['component' => 'user', 'filearea' => 'draft',
  47              'contextid' => $usercontext->id, 'itemid' => $itemid, 'filepath' => '/',
  48              'filename' => 'file.txt'], 'File content');
  49          $data = (object)['title' => 'Block title', 'text' => ['text' => 'Block text',
  50              'itemid' => $itemid, 'format' => FORMAT_HTML]];
  51          $block1->instance_config_save($data);
  52          $this->assertTrue($fs->file_exists($block1->context->id, 'block_html', 'content', 0, '/', 'file.txt'));
  53  
  54          $block2 = $this->create_block($course);
  55          $this->assertFalse($fs->file_exists($block2->context->id, 'block_html', 'content', 0, '/', 'file.txt'));
  56  
  57          $this->setUser(null);
  58          $block2->instance_copy($block1->instance->id);
  59          $this->assertTrue($fs->file_exists($block2->context->id, 'block_html', 'content', 0, '/', 'file.txt'));
  60      }
  61  
  62      /**
  63       * Constructs a page object for the test course.
  64       *
  65       * @param \stdClass $course Moodle course object
  66       * @return \moodle_page Page object representing course view
  67       */
  68      protected static function construct_page($course): \moodle_page {
  69          $context = \context_course::instance($course->id);
  70          $page = new \moodle_page();
  71          $page->set_context($context);
  72          $page->set_course($course);
  73          $page->set_pagelayout('standard');
  74          $page->set_pagetype('course-view');
  75          $page->blocks->load_blocks();
  76          return $page;
  77      }
  78  
  79      /**
  80       * Creates an HTML block on a course.
  81       *
  82       * @param \stdClass $course Course object
  83       * @return \block_html Block instance object
  84       */
  85      protected function create_block($course): \block_html {
  86          $page = self::construct_page($course);
  87          $page->blocks->add_block_at_end_of_default_region('html');
  88  
  89          // Load the block.
  90          $page = self::construct_page($course);
  91          $page->blocks->load_blocks();
  92          $blocks = $page->blocks->get_blocks_for_region($page->blocks->get_default_region());
  93          $block = end($blocks);
  94          return $block;
  95      }
  96  }