Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  18   * Unit tests for core\content\export\exporters\course_exporter.
  19   *
  20   * @package     core
  21   * @category    test
  22   * @copyright   2020 Simey Lameze <simey@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  declare(strict_types=1);
  27  
  28  namespace core\content\export\exporters;
  29  
  30  use advanced_testcase;
  31  use context_course;
  32  use context_module;
  33  use ZipArchive;
  34  use core\content\export\zipwriter;
  35  
  36  /**
  37   * Unit tests for activity exporter.
  38   *
  39   * @coversDefaultClass \core\content\export\exporters\course_exporter
  40   */
  41  class course_exporter_test extends advanced_testcase {
  42  
  43      /**
  44       * The course_exporter should still export a module intro when no exportables are passed.
  45       */
  46      public function test_no_exportables_exported(): void {
  47          $this->resetAfterTest(true);
  48  
  49          $generator = $this->getDataGenerator();
  50  
  51          $course = $generator->create_course();
  52          $coursecontext = context_course::instance($course->id);
  53  
  54          $intro = 'XX Some introduction should go here XX';
  55          $content = 'YY Some content should go here YY';
  56          $module = $generator->create_module('page', [
  57              'course' => $course->id,
  58              'intro' => $intro,
  59              'content' => $content,
  60          ]);
  61          $modcontext = context_module::instance($module->cmid);
  62  
  63          $user = $generator->create_user();
  64          $generator->enrol_user($user->id, $course->id);
  65  
  66          // Only the module index should be added.
  67          $archive = $this->get_mocked_zipwriter(['add_file_from_string']);
  68          $archive->expects($this->once())
  69              ->method('add_file_from_string')
  70              ->with(
  71                  $modcontext,
  72                  'index.html',
  73                  $this->callback(function($html) use ($intro, $content): bool {
  74                      if (strpos($html, $intro) === false) {
  75                          return false;
  76                      }
  77  
  78                      if (strpos($html, $content) !== false) {
  79                          // The content as not exported.
  80                          return false;
  81                      }
  82  
  83                      return true;
  84                  })
  85              );
  86          $archive->set_root_context($coursecontext);
  87  
  88          $coursecontroller = new course_exporter($modcontext->get_course_context(), $user, $archive);
  89          $coursecontroller->export_mod_content($modcontext, []);
  90      }
  91  
  92      /**
  93       * The course_exporter should still export exportables as well as module intro.
  94       */
  95      public function test_exportables_exported(): void {
  96          $this->resetAfterTest(true);
  97  
  98          $generator = $this->getDataGenerator();
  99  
 100          $course = $generator->create_course();
 101          $coursecontext = context_course::instance($course->id);
 102  
 103          $intro = 'XX Some introduction should go here XX';
 104          $content = 'YY Some content should go here YY';
 105          $module = $generator->create_module('page', [
 106              'course' => $course->id,
 107              'intro' => $intro,
 108              'content' => $content,
 109          ]);
 110          $modcontext = context_module::instance($module->cmid);
 111  
 112          $user = $generator->create_user();
 113          $generator->enrol_user($user->id, $course->id);
 114  
 115          // Only the module index should be added.
 116          $archive = $this->get_mocked_zipwriter(['add_file_from_string']);
 117          $archive->expects($this->once())
 118              ->method('add_file_from_string')
 119              ->with(
 120                  $modcontext,
 121                  'index.html',
 122                  $this->callback(function($html) use ($intro, $content): bool {
 123                      if (strpos($html, $intro) === false) {
 124                          return false;
 125                      }
 126  
 127                      if (strpos($html, $content) === false) {
 128                          // Content was exported.
 129                          return false;
 130                      }
 131  
 132                      return true;
 133                  })
 134              );
 135          $archive->set_root_context($coursecontext);
 136  
 137          $pagecontroller = new \mod_page\content\exporter($modcontext, "mod_page", $user, $archive);
 138  
 139          $coursecontroller = new course_exporter($modcontext->get_course_context(), $user, $archive);
 140          $coursecontroller->export_mod_content($modcontext, $pagecontroller->get_exportables());
 141      }
 142  
 143      /**
 144       * Get a mocked zipwriter instance, stubbing the supplieid classes.
 145       *
 146       * @param   string[] $methods
 147       * @return  zipwriter
 148       */
 149      protected function get_mocked_zipwriter(?array $methods = []): zipwriter {
 150          return $this->getMockBuilder(zipwriter::class)
 151              ->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
 152              ->setMethods($methods)
 153              ->getMock();
 154      }
 155  }