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,78 @@
<?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;
/**
* Unit tests for the import_backup_helper
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_backup_helper_test extends \advanced_testcase {
/**
* Test that the first available context with the capability to upload backup files is returned.
*/
public function test_get_context_for_user(): void {
global $DB;
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$user5 = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$coursecontext = \context_course::instance($course->id);
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
$this->getDataGenerator()->enrol_user($user5->id, $course->id, 'student');
$category = $this->getDataGenerator()->create_category();
$rolerecord = $DB->get_record('role', ['shortname' => 'manager']);
$categorycontext = \context_coursecat::instance($category->id);
$this->getDataGenerator()->role_assign($rolerecord->id, $user3->id, $categorycontext->id);
$this->getDataGenerator()->role_assign($rolerecord->id, $user5->id, $categorycontext->id);
$roleid = $this->getDataGenerator()->create_role();
$sitecontext = \context_system::instance();
assign_capability('moodle/restore:uploadfile', CAP_ALLOW, $roleid, $sitecontext->id, true);
accesslib_clear_all_caches_for_unit_testing();
$this->getDataGenerator()->role_assign($roleid, $user4->id, $sitecontext->id);
$result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user1->id);
$this->assertNull($result);
$result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user2->id);
$this->assertEquals($result, $coursecontext);
$this->assertEquals(CONTEXT_COURSE, $result->contextlevel);
$result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user3->id);
$this->assertEquals($result, $categorycontext);
$this->assertEquals(CONTEXT_COURSECAT, $result->contextlevel);
$result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user4->id);
$this->assertEquals($result, $sitecontext);
$this->assertEquals(CONTEXT_SYSTEM, $result->contextlevel);
$result = \tool_moodlenet\local\import_backup_helper::get_context_for_user($user5->id);
$this->assertEquals($result, $categorycontext);
$this->assertEquals(CONTEXT_COURSECAT, $result->contextlevel);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?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/>.
/**
* Unit tests for tool_moodlenet lib
*
* @package tool_moodlenet
* @copyright 2020 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_moodlenet;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/admin/tool/moodlenet/lib.php');
/**
* Test moodlenet functions
*/
class lib_test extends \advanced_testcase {
/**
* Test the generate_mnet_endpoint function
*
* @dataProvider get_endpoints_provider
* @param string $profileurl
* @param int $course
* @param int $section
* @param string $expected
*/
public function test_generate_mnet_endpoint($profileurl, $course, $section, $expected): void {
$endpoint = generate_mnet_endpoint($profileurl, $course, $section);
$this->assertEquals($expected, $endpoint);
}
/**
* Dataprovider for test_generate_mnet_endpoint
*
* @return array
*/
public function get_endpoints_provider() {
global $CFG;
return [
[
'@name@domain.name',
1,
2,
'https://domain.name/' . MOODLENET_DEFAULT_ENDPOINT . '?site=' . urlencode($CFG->wwwroot)
. '&course=1&section=2'
],
[
'@profile@name@domain.name',
1,
2,
'https://domain.name/' . MOODLENET_DEFAULT_ENDPOINT . '?site=' . urlencode($CFG->wwwroot)
. '&course=1&section=2'
],
[
'https://domain.name',
1,
2,
'https://domain.name/' . MOODLENET_DEFAULT_ENDPOINT . '?site=' . urlencode($CFG->wwwroot)
. '&course=1&section=2'
]
];
}
}
@@ -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,
],
];
}
}
@@ -0,0 +1,136 @@
<?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;
/**
* Unit tests for the profile manager
*
* @package tool_moodlenet
* @category test
* @copyright 2020 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class profile_manager_test extends \advanced_testcase {
/**
* Test that on this site we use the user table to hold moodle net profile information.
*/
public function test_official_profile_exists(): void {
$this->assertTrue(\tool_moodlenet\profile_manager::official_profile_exists());
}
/**
* Test a null is returned when the user's mnet profile field is not set.
*/
public function test_get_moodlenet_user_profile_no_profile_set(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$result = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($user->id);
$this->assertNull($result);
}
/**
* Test a null is returned when the user's mnet profile field is not set.
*/
public function test_moodlenet_user_profile_creation_no_profile_set(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->expectException(\moodle_exception::class);
$this->expectExceptionMessage(get_string('invalidmoodlenetprofile', 'tool_moodlenet'));
$result = new \tool_moodlenet\moodlenet_user_profile("", $user->id);
}
/**
* Test the return of a moodle net profile.
*/
public function test_get_moodlenet_user_profile(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user(['moodlenetprofile' => '@matt@hq.mnet']);
$result = \tool_moodlenet\profile_manager::get_moodlenet_user_profile($user->id);
$this->assertEquals($user->moodlenetprofile, $result->get_profile_name());
}
/**
* Test the creation of a user profile category.
*/
public function test_create_user_profile_category(): void {
global $DB;
$this->resetAfterTest();
$basecategoryname = get_string('pluginname', 'tool_moodlenet');
\tool_moodlenet\profile_manager::create_user_profile_category();
$categoryname = \tool_moodlenet\profile_manager::get_category_name();
$this->assertEquals($basecategoryname, $categoryname);
\tool_moodlenet\profile_manager::create_user_profile_category();
$recordcount = $DB->count_records('user_info_category', ['name' => $basecategoryname]);
$this->assertEquals(1, $recordcount);
// Test the duplication of categories to ensure a unique name is always used.
$categoryname = \tool_moodlenet\profile_manager::get_category_name();
$this->assertEquals($basecategoryname . 1, $categoryname);
\tool_moodlenet\profile_manager::create_user_profile_category();
$categoryname = \tool_moodlenet\profile_manager::get_category_name();
$this->assertEquals($basecategoryname . 2, $categoryname);
}
/**
* Test the creating of the custom user profile field to hold the moodle net profile.
*/
public function test_create_user_profile_text_field(): void {
global $DB;
$this->resetAfterTest();
$shortname = 'mnetprofile';
$categoryid = \tool_moodlenet\profile_manager::create_user_profile_category();
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
$record = $DB->get_record('user_info_field', ['shortname' => $shortname]);
$this->assertEquals($shortname, $record->shortname);
$this->assertEquals($categoryid, $record->categoryid);
// Test for a unique name if 'mnetprofile' is already in use.
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
$profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
$this->assertEquals($shortname . 1, $profilename);
\tool_moodlenet\profile_manager::create_user_profile_text_field($categoryid);
$profilename = \tool_moodlenet\profile_manager::get_profile_field_name();
$this->assertEquals($shortname . 2, $profilename);
}
/**
* Test that the user moodlenet profile is saved.
*/
public function test_save_moodlenet_user_profile(): void {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$profilename = '@matt@hq.mnet';
$moodlenetprofile = new \tool_moodlenet\moodlenet_user_profile($profilename, $user->id);
\tool_moodlenet\profile_manager::save_moodlenet_user_profile($moodlenetprofile);
$userdata = \core_user::get_user($user->id);
$this->assertEquals($profilename, $userdata->moodlenetprofile);
}
}