first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,249 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core\content\export\exportable_items;
use advanced_testcase;
use context;
use context_system;
use core\content\export\zipwriter;
use core\content\export\exported_item;
use moodle_url;
use stdClass;
/**
* Unit tests for the `exportable_filearea` export item class.
*
* @package core
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core\content\exportable_items\exportable_filearea
*/
class exportable_filearea_test extends advanced_testcase {
/**
* Ensure that the the exportable_filearea does not fetch files when none exist.
*/
public function test_no_files(): void {
$exportable = new exportable_filearea(
context_system::instance(),
'fake',
'Some fake filearea',
'filearea',
1
);
$this->assertInstanceOf(exportable_filearea::class, $exportable);
}
/**
* Ensure that the exportable_filearea returns all stored_file items for only the specified itemid, but those which
* are not included in the archive receive a pluginfile URL.
*/
public function test_specified_itemid_excluded_from_zip(): void {
$this->resetAfterTest(true);
// Setup for test.
$user = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$component = 'fake';
$filearea = 'myfirstfilearea';
$files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
$files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
$files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
$otherfiles2 = $this->create_files(context_system::instance(), $component, "other{$filearea}", 2);
$exportable = new exportable_filearea(
$context,
$component,
'Some filearea description',
$filearea,
2
);
// There is only one exportable.
$this->assertInstanceOf(exportable_filearea::class, $exportable);
$file2 = reset($files2);
$item = $this->assert_exportable_matches_file($component, $user, $context, $filearea, '', $files2, false, $exportable);
$this->assertCount(count($files2), $item->get_all_files());
$comparisonurl = new moodle_url('/tokenpluginfile.php/');
foreach ($item->get_all_files() as $url) {
$this->assertStringStartsWith($comparisonurl->out(false), $url->filepath);
}
}
/**
* Ensure that the exportable_filearea returns all stored_file items for only the specified itemid.
*/
public function test_specified_itemid(): void {
$this->resetAfterTest(true);
// Setup for test.
$user = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$component = 'fake';
$filearea = 'myfirstfilearea';
$files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
$files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
$files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
$otherfiles2 = $this->create_files(context_system::instance(), $component, "other{$filearea}", 2);
$exportable = new exportable_filearea(
$context,
$component,
'Some filearea description',
$filearea,
2
);
// There is only one exportable.
$this->assertInstanceOf(exportable_filearea::class, $exportable);
$file2 = reset($files2);
$item = $this->assert_exportable_matches_file($component, $user, $context, $filearea, '', $files2, true, $exportable);
$this->assertCount(count($files2), $item->get_all_files());
}
/**
* Ensure that the exportable_filearea returns all stored_files into the correct file location.
*/
public function test_in_subdir(): void {
$this->resetAfterTest(true);
// Setup for test.
$user = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$component = 'fake';
$filearea = 'myfirstfilearea';
$subdir = 'a/path/to/my/subdir';
$files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
$files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
$files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
$exportable = new exportable_filearea(
$context,
$component,
'Some filearea description',
$filearea,
2,
2,
$subdir
);
// There is only one exportable.
$this->assertInstanceOf(exportable_filearea::class, $exportable);
$item = $this->assert_exportable_matches_file($component, $user, $context, $filearea, $subdir, $files2, true, $exportable);
$this->assertCount(count($files2), $item->get_all_files());
}
/**
* Create files for use in testing.
*
* @param context $context
* @param string $component
* @param string $filearea
* @param int $itemid
* @param int $count
* @return filearea[]
*/
protected function create_files(context $context, string $component, string $filearea, int $itemid, int $count = 1): array {
$fs = get_file_storage();
$files = [];
for ($i = 0; $i < $count; $i++) {
$filepath = '/';
for ($j = 0; $j < $i; $j++) {
$filepath .= "{$j}/";
}
$files[] = $fs->create_file_from_string(
(object) [
'contextid' => $context->id,
'component' => $component,
'filearea' => $filearea,
'filepath' => $filepath,
'filename' => "file.txt",
'itemid' => $itemid,
],
"File content: {$i}"
);
}
return $files;
}
/**
* Assert that the supplied expotable matches the supplied file.
*
* @param string $component
* @param stdClass $user
* @param context $context
* @param string $filearea
* @param string $subdir
* @param stored_file[] $expectedfiles
* @param bool $addfilestozip Whether to allow files to be added to the archive
* @param exportable_filearea $exportable
* @return exported_item
*/
protected function assert_exportable_matches_file(
string $component,
stdClass $user,
context $context,
string $filearea,
string $subdir,
array $expectedfiles,
bool $addfilestozip,
exportable_filearea $exportable
): exported_item {
$archive = $this->getMockBuilder(zipwriter::class)
->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
->onlyMethods([
'add_file_from_stored_file',
'is_file_in_archive',
])
->getMock();
$archive->expects($this->any())
->method('is_file_in_archive')
->willReturn($addfilestozip);
$storedfileargs = [];
foreach ($expectedfiles as $file) {
$filepathinzip = $subdir . '/' . $file->get_filearea() . '/' . $file->get_filepath() . $file->get_filename();
$filepathinzip = ltrim(preg_replace('#/+#', '/', $filepathinzip), '/');
$storedfileargs[] = [
$this->equalTo($context),
$this->equalTo($filepathinzip),
$this->equalTo($file),
];
}
$archive->expects($this->exactly(count($expectedfiles)))
->method('add_file_from_stored_file')
->withConsecutive(...$storedfileargs);
return $exportable->add_to_archive($archive);
}
}
@@ -0,0 +1,227 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core\content\export\exportable_items;
use advanced_testcase;
use context;
use context_system;
use core\content\export\zipwriter;
use stdClass;
use stored_file;
/**
* Unit tests for the `exportable_stored_file` export item class.
*
* @package core
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core\content\exportable_items\exportable_stored_file
*/
class exportable_stored_file_test extends advanced_testcase {
/**
* Ensure that the create_from_area_params function returns an array.
*/
public function test_create_from_area_params_no_files(): void {
$exportables = exportable_stored_file::create_from_area_params(
context_system::instance(),
'fake',
'filearea',
null
);
$this->assertIsArray($exportables);
$this->assertCount(0, $exportables);
}
/**
* Ensure that the create_from_area_params function returns a set of exportable_stored_file items, for all itemids.
*/
public function test_create_from_area_params_no_itemid(): void {
$this->resetAfterTest(true);
// Setup for test.
$user = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$component = 'fake';
$filearea = 'myfirstfilearea';
$files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
$files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
$files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
$files = array_values(array_merge($files1, $files2, $files3));
$exportables = exportable_stored_file::create_from_area_params($context, $component, $filearea, null);
$this->assertIsArray($exportables);
$this->assertCount(3, $exportables);
// There should be three exportables. These are listed in order of itemid.
for ($i = 0; $i < 3; $i++) {
$exportable = $exportables[$i];
$file = $files[$i];
$this->assertInstanceOf(exportable_stored_file::class, $exportable);
$this->assert_exportable_matches_file($component, $user, $context, $filearea, '', $file, $exportable);
}
}
/**
* Ensure that the create_from_area_params function returns a set of exportable_stored_file items, for the requested
* itemid
*/
public function test_create_from_area_params_specified_itemid(): void {
$this->resetAfterTest(true);
// Setup for test.
$user = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$component = 'fake';
$filearea = 'myfirstfilearea';
$files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
$files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
$files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
$exportables = exportable_stored_file::create_from_area_params($context, $component, $filearea, 2);
$this->assertIsArray($exportables);
$this->assertCount(1, $exportables);
// There is only one exportable.
$exportable = array_shift($exportables);
$this->assertInstanceOf(exportable_stored_file::class, $exportable);
$file2 = reset($files2);
$this->assert_exportable_matches_file($component, $user, $context, $filearea, '', $file2, $exportable);
}
/**
* Ensure that the create_from_area_params function returns a set of exportable_stored_file items, for the requested
* itemid
*/
public function test_create_from_area_params_in_subdir(): void {
$this->resetAfterTest(true);
// Setup for test.
$user = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$component = 'fake';
$filearea = 'myfirstfilearea';
$subdir = 'a/path/to/my/subdir';
$files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
$files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
$files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
$exportables = exportable_stored_file::create_from_area_params($context, $component, $filearea, 2, 2, $subdir);
$this->assertIsArray($exportables);
$this->assertCount(1, $exportables);
// There is only one exportable.
$exportable = array_shift($exportables);
$this->assertInstanceOf(exportable_stored_file::class, $exportable);
$file2 = reset($files2);
$this->assert_exportable_matches_file($component, $user, $context, $filearea, $subdir, $file2, $exportable);
}
/**
* Create files for use in testing.
*
* @param context $context
* @param string $component
* @param string $filearea
* @param int $itemid
* @param int $count
* @return stored_file[]
*/
protected function create_files(context $context, string $component, string $filearea, int $itemid, int $count = 1): array {
$fs = get_file_storage();
$files = [];
for ($i = 0; $i < $count; $i++) {
$filepath = '/';
for ($j = 0; $j < $i; $j++) {
$filepath .= "{$j}/";
}
$files[] = $fs->create_file_from_string(
(object) [
'contextid' => $context->id,
'component' => $component,
'filearea' => $filearea,
'filepath' => $filepath,
'filename' => "file.txt",
'itemid' => $itemid,
],
"File content: {$i}"
);
}
return $files;
}
/**
* Assert that the supplied expotable matches the supplied file.
*
* @param string $component
* @param stdClass $user
* @param context $context
* @param string $filearea
* @param string $subdir
* @param stored_file $file
* @param exportable_stored_file $exportable
*/
protected function assert_exportable_matches_file(
string $component,
stdClass $user,
context $context,
string $filearea,
string $subdir,
stored_file $file,
exportable_stored_file $exportable
): void {
$archive = $this->getMockBuilder(zipwriter::class)
->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
->onlyMethods([
'add_file_from_stored_file',
])
->getMock();
$this->assertEquals($file->get_filepath() . $file->get_filename(), $exportable->get_user_visible_name());
$expectedfilepath = implode('/', array_filter([$subdir, $filearea, $file->get_filepath(), $file->get_filename()]));
$expectedfilepath = preg_replace('#/+#', '/', $expectedfilepath);
$archive->expects($this->once())
->method('add_file_from_stored_file')
->with(
$this->equalTo($context),
$this->equalTo($expectedfilepath),
$this->equalTo($file)
);
$exportable->add_to_archive($archive);
}
}
@@ -0,0 +1,371 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core\content\export\exportable_items;
use advanced_testcase;
use context;
use context_module;
use context_system;
use core\content\export\zipwriter;
use moodle_url;
use stdClass;
/**
* Unit tests for the `exportable_textarea` export item class.
*
* @package core
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core\content\exportable_items\exportable_textarea
*/
class exportable_textarea_test extends advanced_testcase {
/**
* Ensure that an exportable textarea which does not relate to any content, does not attempt to export any content.
*/
public function test_valid_table_without_content(): void {
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$context = context_system::instance();
$component = 'page';
$uservisiblename = 'Page content';
$tablename = 'page';
$fieldname = 'content';
$fieldid = -1;
$formatfieldname = 'contentformat';
$filearea = 'content';
$exportable = new exportable_textarea(
$context,
$component,
$uservisiblename,
$tablename,
$fieldname,
$fieldid,
$formatfieldname
);
$this->assertInstanceOf(exportable_textarea::class, $exportable);
$this->assert_exportable_empty($component, $user, $context, $exportable);
}
/**
* Ensure that the an exportable textarea exports content from the appropriate locations, but without any files.
*/
public function test_valid_table_with_content_no_filearea_specified(): void {
$this->resetAfterTest(true);
$content = '<h1>Hello</h1><p>World!</p>';
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', (object) [
'course' => $course,
'content' => $content,
'contentformat' => FORMAT_HTML,
]);
$context = context_module::instance($page->cmid);
$expectedfiles = $this->create_files($context, 'mod_page', 'content', (int) $page->id, 5);
// Unexpected files.
$this->create_files($context, 'mod_page', 'content', (int) $page->id + 1, 5);
$this->create_files($context, 'mod_page', 'othercontent', (int) $page->id, 5);
$this->create_files($context, 'mod_foo', 'content', (int) $page->id, 5);
$component = 'page';
$uservisiblename = 'Page content';
$tablename = 'page';
$fieldname = 'content';
$fieldid = (int) $page->id;
$formatfieldname = 'contentformat';
$exportable = new exportable_textarea(
$context,
$component,
$uservisiblename,
$tablename,
$fieldname,
$fieldid,
$formatfieldname
);
$this->assertInstanceOf(exportable_textarea::class, $exportable);
// Although files exist, the filearea and itemid were not included.
$this->assert_exportable_matches_file($component, $user, $context, null, $content, [], '', $exportable);
}
/**
* Ensure that the an exportable textarea exports content from the appropriate locations, but without any files.
*/
public function test_valid_table_with_content_no_itemid_specified(): void {
$this->resetAfterTest(true);
$content = '<h1>Hello</h1><p>World!</p>';
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', (object) [
'course' => $course,
'content' => $content,
'contentformat' => FORMAT_HTML,
]);
$context = context_module::instance($page->cmid);
$expectedfiles = $this->create_files($context, 'mod_page', 'content', (int) $page->id, 5);
// Unexpected files.
$this->create_files($context, 'mod_page', 'content', (int) $page->id + 1, 5);
$this->create_files($context, 'mod_page', 'othercontent', (int) $page->id, 5);
$this->create_files($context, 'mod_foo', 'content', (int) $page->id, 5);
$component = 'page';
$uservisiblename = 'Page content';
$tablename = 'page';
$fieldname = 'content';
$fieldid = (int) $page->id;
$formatfieldname = 'contentformat';
$filearea = 'content';
$exportable = new exportable_textarea(
$context,
$component,
$uservisiblename,
$tablename,
$fieldname,
$fieldid,
$formatfieldname,
$filearea
);
$this->assertInstanceOf(exportable_textarea::class, $exportable);
// Although files exist, the filearea and itemid were not included.
$this->assert_exportable_matches_file($component, $user, $context, null, $content, [], '', $exportable);
}
/**
* Ensure that the an exportable textarea exports content from the appropriate locations, with files.
*/
public function test_valid_table_with_content_and_files(): void {
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$contentin = <<<EOF
<h1>Hello</h1><p>World!</p>
<img src='@@PLUGINFILE@@/file.txt'>
<img src='@@PLUGINFILE@@/other/file.txt'>
EOF;
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', (object) [
'course' => $course,
'content' => $contentin,
'contentformat' => FORMAT_HTML,
]);
$this->setUser($user);
$context = context_module::instance($page->cmid);
$expectedfiles = $this->create_files(
$context,
'mod_page',
'content',
(int) $page->id,
5,
'contentformat',
'content',
(int) $page->id,
5
);
// Unexpected files.
$this->create_files($context, 'mod_page', 'content', (int) $page->id + 1, 5);
$this->create_files($context, 'mod_page', 'othercontent', (int) $page->id, 5);
$this->create_files($context, 'mod_foo', 'content', (int) $page->id, 5);
$component = 'mod_page';
$uservisiblename = 'Page content';
$tablename = 'page';
$fieldname = 'content';
$fieldid = (int) $page->id;
$formatfieldname = 'contentformat';
$filearea = 'content';
$itemid = (int) $page->id;
$exportable = new exportable_textarea(
$context,
$component,
$uservisiblename,
$tablename,
$fieldname,
$fieldid,
$formatfieldname,
$filearea,
$itemid,
null
);
$this->assertInstanceOf(exportable_textarea::class, $exportable);
$pluginfilebase = moodle_url::make_pluginfile_url(
$context->id, $component, $filearea, null, '', '', false, true
)->out(false);
$expectedcontent = <<<EOF
<h1>Hello</h1><p>World!</p>
<img src='content/file.txt'>
<img src='{$pluginfilebase}/other/file.txt'>
EOF;
// Although files exist, the filearea and itemid were not included.
$this->assert_exportable_matches_file(
$component, $user, $context, $filearea, $expectedcontent, $expectedfiles, '', $exportable
);
}
/**
* Create files for use in testing.
*
* @param context $context
* @param string $component
* @param string $filearea
* @param int $itemid
* @param int $count
* @return stored_file[]
*/
protected function create_files(context $context, string $component, string $filearea, int $itemid, int $count = 1): array {
$fs = get_file_storage();
$files = [];
for ($i = 0; $i < $count; $i++) {
$filepath = '/';
for ($j = 0; $j < $i; $j++) {
$filepath .= "{$j}/";
}
$files[] = $fs->create_file_from_string(
(object) [
'contextid' => $context->id,
'component' => $component,
'filearea' => $filearea,
'filepath' => $filepath,
'filename' => "file.txt",
'itemid' => $itemid,
],
"File content: {$i}"
);
}
return $files;
}
/**
* Assert that the supplied expotable matches the supplied file.
*
* @param string $component
* @param stdClass $user
* @param context $context
* @param string $filearea
* @param string $content
* @param stored_file[] $expectedfiles
* @param string $subdir
* @param exportable_textarea $exportable
*/
protected function assert_exportable_matches_file(
string $component,
stdClass $user,
context $context,
?string $filearea,
string $content,
array $expectedfiles,
string $subdir,
exportable_textarea $exportable
): void {
$archive = $this->getMockBuilder(zipwriter::class)
->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
->onlyMethods([
'is_file_in_archive',
'add_file_from_string',
'add_file_from_stored_file',
])
->getMock();
$archive->expects($this->any())
->method('is_file_in_archive')
->willReturn(true);
$storedfileargs = [];
foreach ($expectedfiles as $file) {
$filepathinzip = dirname($subdir) . $file->get_filearea() . '/' . $file->get_filepath() . $file->get_filename();
$filepathinzip = ltrim(preg_replace('#/+#', '/', $filepathinzip), '/');
$storedfileargs[] = [
$this->equalTo($context),
$this->equalTo($filepathinzip),
$this->equalTo($file),
];
}
$archive->expects($this->exactly(count($expectedfiles)))
->method('add_file_from_stored_file')
->withConsecutive(...$storedfileargs);
$archive->expects($this->never())
->method('add_file_from_string');
$exportable->add_to_archive($archive);
}
/**
* Assert that the supplied expotable matches the supplied file.
*
* @param string $component
* @param stdClass $user
* @param context $context
* @param exportable_textarea $exportable
*/
protected function assert_exportable_empty(
string $component,
stdClass $user,
context $context,
exportable_textarea $exportable
): void {
$archive = $this->getMockBuilder(zipwriter::class)
->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
->onlyMethods([
'add_file_from_stored_file',
'add_file_from_string',
'add_file_from_template',
])
->getMock();
$archive->expects($this->never())
->method('add_file_from_stored_file');
$archive->expects($this->never())
->method('add_file_from_string');
$archive->expects($this->never())
->method('add_file_from_template');
$exportable->add_to_archive($archive);
}
}
@@ -0,0 +1,150 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core\content\export\exporters;
use advanced_testcase;
use context_course;
use context_module;
use ZipArchive;
use core\content\export\zipwriter;
/**
* Unit tests for activity exporter.
*
* @package core
* @category test
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @covers \core\content\export\exporters\course_exporter
*/
class course_exporter_test extends advanced_testcase {
/**
* The course_exporter should still export a module intro when no exportables are passed.
*/
public function test_no_exportables_exported(): void {
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$intro = 'XX Some introduction should go here XX';
$content = 'YY Some content should go here YY';
$module = $generator->create_module('page', [
'course' => $course->id,
'intro' => $intro,
'content' => $content,
]);
$modcontext = context_module::instance($module->cmid);
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id);
// Only the module index should be added.
$archive = $this->get_mocked_zipwriter(['add_file_from_string']);
$archive->expects($this->once())
->method('add_file_from_string')
->with(
$modcontext,
'index.html',
$this->callback(function($html) use ($intro, $content): bool {
if (strpos($html, $intro) === false) {
return false;
}
if (strpos($html, $content) !== false) {
// The content as not exported.
return false;
}
return true;
})
);
$archive->set_root_context($coursecontext);
$coursecontroller = new course_exporter($modcontext->get_course_context(), $user, $archive);
$coursecontroller->export_mod_content($modcontext, []);
}
/**
* The course_exporter should still export exportables as well as module intro.
*/
public function test_exportables_exported(): void {
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$intro = 'XX Some introduction should go here XX';
$content = 'YY Some content should go here YY';
$module = $generator->create_module('page', [
'course' => $course->id,
'intro' => $intro,
'content' => $content,
]);
$modcontext = context_module::instance($module->cmid);
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id);
// Only the module index should be added.
$archive = $this->get_mocked_zipwriter(['add_file_from_string']);
$archive->expects($this->once())
->method('add_file_from_string')
->with(
$modcontext,
'index.html',
$this->callback(function($html) use ($intro, $content): bool {
if (strpos($html, $intro) === false) {
return false;
}
if (strpos($html, $content) === false) {
// Content was exported.
return false;
}
return true;
})
);
$archive->set_root_context($coursecontext);
$pagecontroller = new \mod_page\content\exporter($modcontext, "mod_page", $user, $archive);
$coursecontroller = new course_exporter($modcontext->get_course_context(), $user, $archive);
$coursecontroller->export_mod_content($modcontext, $pagecontroller->get_exportables());
}
/**
* Get a mocked zipwriter instance, stubbing the supplieid classes.
*
* @param string[] $methods
* @return zipwriter
*/
protected function get_mocked_zipwriter(?array $methods = []): zipwriter {
return $this->getMockBuilder(zipwriter::class)
->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
->onlyMethods($methods)
->getMock();
}
}
+114
View File
@@ -0,0 +1,114 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core\content\export;
use advanced_testcase;
use context_module;
use context_system;
use ZipArchive;
/**
* Unit tests for core\content\zipwriter.
*
* @package core
* @category test
* @copyright 2020 Simey Lameze <simey@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @covers \core\content\export\zipwriter
*/
class zipwriter_test extends advanced_testcase {
/**
* Test add_file_from_stored_file().
*/
public function test_add_file_from_stored_file(): void {
$this->resetAfterTest(true);
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$folder = $this->getDataGenerator()->create_module('folder', ['course' => $course->id]);
$context = \context_course::instance($course->id);
// Add a file to the intro.
$fileintroname = "fileintro.txt";
$filerecord = [
'contextid' => context_module::instance($folder->cmid)->id,
'component' => 'mod_folder',
'filearea' => 'intro',
'itemid' => 0,
'filepath' => '/',
'filename' => $fileintroname,
];
$fs = get_file_storage();
$storedfile = $fs->create_file_from_string($filerecord, 'image contents');
$pathinfolder = $storedfile->get_filepath() . $storedfile->get_filename();
$zipwriter = zipwriter::get_file_writer('test.zip');
$zipwriter->add_file_from_stored_file($context, $pathinfolder, $storedfile);
$zipwriter->finish();
$zipfilepath = $zipwriter->get_file_path();
$zip = new ZipArchive();
$opened = $zip->open($zipfilepath);
$this->assertTrue($opened);
$pathinzip = $zipwriter->get_context_path($context, $pathinfolder);
$this->assertEquals($storedfile->get_content(), $zip->getFromName($pathinzip));
}
/**
* Test add_file_from_string().
*/
public function test_add_file_from_string(): void {
$context = context_system::instance();
$pathinfolder = "/path/to/my/file.txt";
$mycontent = "Zippidy do dah";
$zipwriter = zipwriter::get_file_writer('test.zip');
$zipwriter->add_file_from_string($context, $pathinfolder, $mycontent);
$zipwriter->finish();
$zipfilepath = $zipwriter->get_file_path();
$zip = new ZipArchive();
$opened = $zip->open($zipfilepath);
$this->assertTrue($opened);
$pathinzip = ltrim($zipwriter->get_context_path($context, $pathinfolder), '/');
$this->assertEquals($mycontent, $zip->getFromName($pathinzip));
}
/**
* Test get_file_writer().
*/
public function test_get_file_writer(): void {
$zipwriter = zipwriter::get_file_writer('test.zip');
$this->assertInstanceOf(zipwriter::class, $zipwriter);
$this->assertTrue(file_exists($zipwriter->get_file_path()));
}
/**
* Test get_stream_writer().
*/
public function test_get_stream_writer(): void {
$zipwriter = zipwriter::get_stream_writer('test.zip');
$this->assertInstanceOf(zipwriter::class, $zipwriter);
}
}