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