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]

   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  declare(strict_types=1);
  18  
  19  namespace core\content\export;
  20  
  21  use advanced_testcase;
  22  use context_module;
  23  use context_system;
  24  use ZipArchive;
  25  
  26  /**
  27   * Unit tests for core\content\zipwriter.
  28   *
  29   * @package     core
  30   * @category    test
  31   * @copyright   2020 Simey Lameze <simey@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU Public License
  33   * @covers      \core\content\export\zipwriter
  34   */
  35  class zipwriter_test extends advanced_testcase {
  36  
  37      /**
  38       * Test add_file_from_stored_file().
  39       */
  40      public function test_add_file_from_stored_file(): void {
  41          $this->resetAfterTest(true);
  42          $this->setAdminUser();
  43  
  44          $course = $this->getDataGenerator()->create_course();
  45          $folder = $this->getDataGenerator()->create_module('folder', ['course' => $course->id]);
  46          $context = \context_course::instance($course->id);
  47  
  48          // Add a file to the intro.
  49          $fileintroname = "fileintro.txt";
  50          $filerecord = [
  51              'contextid' => context_module::instance($folder->cmid)->id,
  52              'component' => 'mod_folder',
  53              'filearea'  => 'intro',
  54              'itemid'    => 0,
  55              'filepath'  => '/',
  56              'filename'  => $fileintroname,
  57          ];
  58          $fs = get_file_storage();
  59          $storedfile = $fs->create_file_from_string($filerecord, 'image contents');
  60  
  61          $pathinfolder = $storedfile->get_filepath() . $storedfile->get_filename();
  62  
  63          $zipwriter = zipwriter::get_file_writer('test.zip');
  64          $zipwriter->add_file_from_stored_file($context, $pathinfolder, $storedfile);
  65          $zipwriter->finish();
  66  
  67          $zipfilepath = $zipwriter->get_file_path();
  68          $zip = new ZipArchive();
  69          $opened = $zip->open($zipfilepath);
  70          $this->assertTrue($opened);
  71  
  72          $pathinzip = $zipwriter->get_context_path($context, $pathinfolder);
  73          $this->assertEquals($storedfile->get_content(), $zip->getFromName($pathinzip));
  74      }
  75  
  76      /**
  77       * Test add_file_from_string().
  78       */
  79      public function test_add_file_from_string(): void {
  80          $context = context_system::instance();
  81  
  82          $pathinfolder = "/path/to/my/file.txt";
  83          $mycontent = "Zippidy do dah";
  84  
  85          $zipwriter = zipwriter::get_file_writer('test.zip');
  86          $zipwriter->add_file_from_string($context, $pathinfolder, $mycontent);
  87          $zipwriter->finish();
  88  
  89          $zipfilepath = $zipwriter->get_file_path();
  90          $zip = new ZipArchive();
  91          $opened = $zip->open($zipfilepath);
  92          $this->assertTrue($opened);
  93  
  94          $pathinzip = ltrim($zipwriter->get_context_path($context, $pathinfolder), '/');
  95          $this->assertEquals($mycontent, $zip->getFromName($pathinzip));
  96      }
  97  
  98      /**
  99       * Test get_file_writer().
 100       */
 101      public function test_get_file_writer(): void {
 102          $zipwriter = zipwriter::get_file_writer('test.zip');
 103          $this->assertInstanceOf(zipwriter::class, $zipwriter);
 104          $this->assertTrue(file_exists($zipwriter->get_file_path()));
 105      }
 106  
 107      /**
 108       * Test get_stream_writer().
 109       */
 110      public function test_get_stream_writer(): void {
 111          $zipwriter = zipwriter::get_stream_writer('test.zip');
 112          $this->assertInstanceOf(zipwriter::class, $zipwriter);
 113      }
 114  }