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,70 @@
<?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/>.
namespace tool_moodlenet\local;
use tool_moodlenet\local\import_handler_info;
use tool_moodlenet\local\import_strategy;
use tool_moodlenet\local\import_strategy_file;
/**
* Class tool_moodlenet_import_handler_info_testcase, providing test cases for the import_handler_info class.
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_handler_info_test extends \advanced_testcase {
/**
* Test init and the getters.
*
* @dataProvider handler_info_data_provider
* @param string $modname the name of the mod.
* @param string $description description of the mod.
* @param bool $expectexception whether we expect an exception during init or not.
*/
public function test_initialisation($modname, $description, $expectexception): void {
$this->resetAfterTest();
// Skip those cases we cannot init.
if ($expectexception) {
$this->expectException(\coding_exception::class);
$handlerinfo = new import_handler_info($modname, $description, new import_strategy_file());
}
$handlerinfo = new import_handler_info($modname, $description, new import_strategy_file());
$this->assertEquals($modname, $handlerinfo->get_module_name());
$this->assertEquals($description, $handlerinfo->get_description());
$this->assertInstanceOf(import_strategy::class, $handlerinfo->get_strategy());
}
/**
* Data provider for creation of import_handler_info objects.
*
* @return array the data for creation of the info object.
*/
public function handler_info_data_provider() {
return [
'All data present' => ['label', 'Add a label to the course', false],
'Empty module name' => ['', 'Add a file resource to the course', true],
'Empty description' => ['resource', '', true],
];
}
}
@@ -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/>.
namespace tool_moodlenet\local;
use tool_moodlenet\local\import_handler_registry;
use tool_moodlenet\local\import_handler_info;
use tool_moodlenet\local\import_strategy_file;
use tool_moodlenet\local\import_strategy_link;
use tool_moodlenet\local\remote_resource;
use tool_moodlenet\local\url;
/**
* Class tool_moodlenet_import_handler_registry_testcase, providing test cases for the import_handler_registry class.
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_handler_registry_test extends \advanced_testcase {
/**
* Test confirming the behaviour of get_resource_handlers_for_strategy with different params.
*/
public function test_get_resource_handlers_for_strategy(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$ihr = new import_handler_registry($course, $teacher);
$resource = new remote_resource(
new \curl(),
new url('http://example.org'),
(object) [
'name' => 'Resource name',
'description' => 'Resource description'
]
);
$handlers = $ihr->get_resource_handlers_for_strategy($resource, new import_strategy_file());
$this->assertIsArray($handlers);
foreach ($handlers as $handler) {
$this->assertInstanceOf(import_handler_info::class, $handler);
}
}
/**
* Test confirming that the results are scoped to the provided user.
*/
public function test_get_resource_handlers_for_strategy_user_scoping(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$studentihr = new import_handler_registry($course, $student);
$teacherihr = new import_handler_registry($course, $teacher);
$resource = new remote_resource(
new \curl(),
new url('http://example.org'),
(object) [
'name' => 'Resource name',
'description' => 'Resource description'
]
);
$this->assertEmpty($studentihr->get_resource_handlers_for_strategy($resource, new import_strategy_file()));
$this->assertNotEmpty($teacherihr->get_resource_handlers_for_strategy($resource, new import_strategy_file()));
}
/**
* Test confirming that we can find a unique handler based on the module and strategy name.
*/
public function test_get_resource_handler_for_module_and_strategy(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$ihr = new import_handler_registry($course, $teacher);
$resource = new remote_resource(
new \curl(),
new url('http://example.org'),
(object) [
'name' => 'Resource name',
'description' => 'Resource description'
]
);
// Resource handles every file type, so we'll always be able to find that unique handler when looking.
$handler = $ihr->get_resource_handler_for_mod_and_strategy($resource, 'resource', new import_strategy_file());
$this->assertInstanceOf(import_handler_info::class, $handler);
// URL handles every resource, so we'll always be able to find that unique handler when looking with a link strategy.
$handler = $ihr->get_resource_handler_for_mod_and_strategy($resource, 'url', new import_strategy_link());
$this->assertInstanceOf(import_handler_info::class, $handler);
$this->assertEquals('url', $handler->get_module_name());
$this->assertInstanceOf(import_strategy_link::class, $handler->get_strategy());
}
}
@@ -0,0 +1,100 @@
<?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/>.
namespace tool_moodlenet\local;
use tool_moodlenet\local\import_info;
use tool_moodlenet\local\remote_resource;
use tool_moodlenet\local\url;
/**
* Class tool_moodlenet_import_info_testcase, providing test cases for the import_info class.
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_info_test extends \advanced_testcase {
/**
* Create some test objects.
*
* @return array
*/
protected function create_test_info(): array {
$user = $this->getDataGenerator()->create_user();
$resource = new remote_resource(new \curl(),
new url('http://example.org'),
(object) [
'name' => 'Resource name',
'description' => 'Resource summary'
]
);
$importinfo = new import_info($user->id, $resource, (object)[]);
return [$user, $resource, $importinfo];
}
/**
* Test for creation and getters.
*/
public function test_getters(): void {
$this->resetAfterTest();
[$user, $resource, $importinfo] = $this->create_test_info();
$this->assertEquals($resource, $importinfo->get_resource());
$this->assertEquals(new \stdClass(), $importinfo->get_config());
$this->assertNotEmpty($importinfo->get_id());
}
/**
* Test for setters.
*/
public function test_set_config(): void {
$this->resetAfterTest();
[$user, $resource, $importinfo] = $this->create_test_info();
$config = $importinfo->get_config();
$this->assertEquals(new \stdClass(), $config);
$config->course = 3;
$config->section = 1;
$importinfo->set_config($config);
$this->assertEquals((object) ['course' => 3, 'section' => 1], $importinfo->get_config());
}
/**
* Verify the object can be stored and loaded.
*/
public function test_persistence(): void {
$this->resetAfterTest();
[$user, $resource, $importinfo] = $this->create_test_info();
// Nothing to load initially since nothing has been saved.
$loadedinfo = import_info::load($importinfo->get_id());
$this->assertNull($loadedinfo);
// Now, save and confirm we can load the data into a new object.
$importinfo->save();
$loadedinfo2 = import_info::load($importinfo->get_id());
$this->assertEquals($importinfo, $loadedinfo2);
// Purge and confirm the load returns null now.
$importinfo->purge();
$loadedinfo3 = import_info::load($importinfo->get_id());
$this->assertNull($loadedinfo3);
}
}
@@ -0,0 +1,151 @@
<?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/>.
namespace tool_moodlenet\local;
use tool_moodlenet\local\import_handler_registry;
use tool_moodlenet\local\import_processor;
use tool_moodlenet\local\import_strategy_file;
use tool_moodlenet\local\import_strategy_link;
use tool_moodlenet\local\remote_resource;
use tool_moodlenet\local\url;
/**
* Class tool_moodlenet_import_processor_testcase, providing test cases for the import_processor class.
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_processor_test extends \advanced_testcase {
/**
* An integration test, this confirms the ability to construct an import processor and run the import for the current user.
*/
public function test_process_valid_resource(): void {
$this->resetAfterTest();
// Set up a user as a teacher in a course.
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$section = 0;
$this->setUser($teacher);
// Set up the import, using a mod_resource handler for the html extension.
$resourceurl = $this->getExternalTestFileUrl('/test.html');
$remoteresource = new remote_resource(
new \curl(),
new url($resourceurl),
(object) [
'name' => 'Resource name',
'description' => 'Resource description'
]
);
$handlerregistry = new import_handler_registry($course, $teacher);
$handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'resource',
new import_strategy_file());
$importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry);
// Import the file.
$importproc->process();
// Verify there is a new mod_resource created with correct name, description and containing the test.html file.
$modinfo = get_fast_modinfo($course, $teacher->id);
$cms = $modinfo->get_instances();
$this->assertArrayHasKey('resource', $cms);
$cminfo = array_shift($cms['resource']);
$this->assertEquals('Resource name', $cminfo->get_formatted_name());
$cm = get_coursemodule_from_id('', $cminfo->id, 0, false, MUST_EXIST);
list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cminfo, $course);
$this->assertEquals($remoteresource->get_description(), $data->intro);
$fs = get_file_storage();
$files = $fs->get_area_files(\context_module::instance($cminfo->id)->id, 'mod_resource', 'content', false,
'sortorder DESC, id ASC', false);
$file = reset($files);
$this->assertEquals('test.html', $file->get_filename());
$this->assertEquals('text/html', $file->get_mimetype());
}
/**
* Test confirming that an exception is thrown when trying to process a resource which does not exist.
*/
public function test_process_invalid_resource(): void {
$this->resetAfterTest();
// Set up a user as a teacher in a course.
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$section = 0;
$this->setUser($teacher);
// Set up the import, using a mod_resource handler for the html extension.
$resourceurl = $this->getExternalTestFileUrl('/test.htmlzz');
$remoteresource = new remote_resource(
new \curl(),
new url($resourceurl),
(object) [
'name' => 'Resource name',
'description' => 'Resource description'
]
);
$handlerregistry = new import_handler_registry($course, $teacher);
$handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'resource',
new import_strategy_file());
$importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry);
// Import the file.
$this->expectException(\coding_exception::class);
$importproc->process();
}
/**
* Test confirming that imports can be completed using alternative import strategies.
*/
public function test_process_alternative_import_strategies(): void {
$this->resetAfterTest();
// Set up a user as a teacher in a course.
$course = $this->getDataGenerator()->create_course();
$teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
$section = 0;
$this->setUser($teacher);
// Set up the import, using a mod_url handler and the link import strategy.
$remoteresource = new remote_resource(
new \curl(),
new url('http://example.com/cats.pdf'),
(object) [
'name' => 'Resource name',
'description' => 'Resource description'
]
);
$handlerregistry = new import_handler_registry($course, $teacher);
$handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'url',
new import_strategy_link());
$importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry);
// Import the resource as a link.
$importproc->process();
// Verify there is a new mod_url created with name 'cats' and containing the URL of the resource.
$modinfo = get_fast_modinfo($course, $teacher->id);
$cms = $modinfo->get_instances();
$this->assertArrayHasKey('url', $cms);
$cminfo = array_shift($cms['url']);
$this->assertEquals('Resource name', $cminfo->get_formatted_name());
}
}
@@ -0,0 +1,118 @@
<?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/>.
namespace tool_moodlenet\local;
use tool_moodlenet\local\remote_resource;
use tool_moodlenet\local\url;
/**
* Class tool_moodlenet_remote_resource_testcase, providing test cases for the remote_resource class.
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class remote_resource_test extends \advanced_testcase {
/**
* Test getters.
*
* @dataProvider remote_resource_data_provider
* @param string $url the url of the resource.
* @param string $metadata the resource metadata like name, description, etc.
* @param string $expectedextension the extension we expect to find when querying the remote resource.
*/
public function test_getters($url, $metadata, $expectedextension): void {
$this->resetAfterTest();
$remoteres = new remote_resource(new \curl(), new url($url), $metadata);
$this->assertEquals(new url($url), $remoteres->get_url());
$this->assertEquals($metadata->name, $remoteres->get_name());
$this->assertEquals($metadata->description, $remoteres->get_description());
$this->assertEquals($expectedextension, $remoteres->get_extension());
}
/**
* Data provider generating remote urls.
*
* @return array
*/
public function remote_resource_data_provider() {
return [
'With filename and extension' => [
$this->getExternalTestFileUrl('/test.html'),
(object) [
'name' => 'Test html file',
'description' => 'Full description of the html file'
],
'html'
],
'With filename only' => [
'http://example.com/path/file',
(object) [
'name' => 'Test html file',
'description' => 'Full description of the html file'
],
''
]
];
}
/**
* Test confirming the network based operations of a remote_resource.
*/
public function test_network_features(): void {
$url = $this->getExternalTestFileUrl('/test.html');
$nonexistenturl = $this->getExternalTestFileUrl('/test.htmlzz');
$remoteres = new remote_resource(
new \curl(),
new url($url),
(object) [
'name' => 'Test html file',
'description' => 'Some description'
]
);
$nonexistentremoteres = new remote_resource(
new \curl(),
new url($nonexistenturl),
(object) [
'name' => 'Test html file',
'description' => 'Some description'
]
);
// We need to handle size of -1 (missing "Content-Length" header), or where it is set and greater than zero.
$this->assertThat(
$remoteres->get_download_size(),
$this->logicalOr(
$this->equalTo(-1),
$this->greaterThan(0),
),
);
[$path, $name] = $remoteres->download_to_requestdir();
$this->assertIsString($path);
$this->assertEquals('test.html', $name);
$this->assertFileExists($path . '/' . $name);
$this->expectException(\coding_exception::class);
$nonexistentremoteres->get_download_size();
}
}
@@ -0,0 +1,98 @@
<?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/>.
namespace tool_moodlenet\local;
use tool_moodlenet\local\url;
/**
* Class tool_moodlenet_url_testcase, providing test cases for the url class.
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class url_test extends \advanced_testcase {
/**
* Test the parsing to host + path components.
*
* @dataProvider url_provider
* @param string $urlstring The full URL string
* @param string $host the expected host component of the URL.
* @param string $path the expected path component of the URL.
* @param bool $exception whether or not an exception is expected during construction.
*/
public function test_parsing($urlstring, $host, $path, $exception): void {
if ($exception) {
$this->expectException(\coding_exception::class);
$url = new url($urlstring);
return;
}
$url = new url($urlstring);
$this->assertEquals($urlstring, $url->get_value());
$this->assertEquals($host, $url->get_host());
$this->assertEquals($path, $url->get_path());
}
/**
* Data provider.
*
* @return array
*/
public function url_provider() {
return [
'No path' => [
'url' => 'https://example.moodle.net',
'host' => 'example.moodle.net',
'path' => null,
'exception' => false,
],
'Slash path' => [
'url' => 'https://example.moodle.net/',
'host' => 'example.moodle.net',
'path' => '/',
'exception' => false,
],
'Path includes file and extension' => [
'url' => 'https://example.moodle.net/uploads/123456789/pic.png',
'host' => 'example.moodle.net',
'path' => '/uploads/123456789/pic.png',
'exception' => false,
],
'Path includes file, extension and params' => [
'url' => 'https://example.moodle.net/uploads/123456789/pic.png?option=1&option2=test',
'host' => 'example.moodle.net',
'path' => '/uploads/123456789/pic.png',
'exception' => false,
],
'Malformed - invalid' => [
'url' => 'invalid',
'host' => null,
'path' => null,
'exception' => true,
],
'Direct, non-encoded utf8 - invalid' => [
'url' => 'http://москва.рф/services/',
'host' => 'москва.рф',
'path' => '/services/',
'exception' => true,
],
];
}
}