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 the import_processor class. 19 * 20 * @package tool_moodlenet 21 * @category test 22 * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 namespace tool_moodlenet\local\tests; 26 27 use tool_moodlenet\local\import_handler_registry; 28 use tool_moodlenet\local\import_processor; 29 use tool_moodlenet\local\import_strategy_file; 30 use tool_moodlenet\local\import_strategy_link; 31 use tool_moodlenet\local\remote_resource; 32 use tool_moodlenet\local\url; 33 34 defined('MOODLE_INTERNAL') || die(); 35 36 /** 37 * Class tool_moodlenet_import_processor_testcase, providing test cases for the import_processor class. 38 */ 39 class tool_moodlenet_import_processor_testcase extends \advanced_testcase { 40 41 /** 42 * An integration test, this confirms the ability to construct an import processor and run the import for the current user. 43 */ 44 public function test_process_valid_resource() { 45 $this->resetAfterTest(); 46 47 // Set up a user as a teacher in a course. 48 $course = $this->getDataGenerator()->create_course(); 49 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); 50 $section = 0; 51 $this->setUser($teacher); 52 53 // Set up the import, using a mod_resource handler for the html extension. 54 $resourceurl = $this->getExternalTestFileUrl('/test.html'); 55 $remoteresource = new remote_resource( 56 new \curl(), 57 new url($resourceurl), 58 (object) [ 59 'name' => 'Resource name', 60 'description' => 'Resource description' 61 ] 62 ); 63 $handlerregistry = new import_handler_registry($course, $teacher); 64 $handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'resource', 65 new import_strategy_file()); 66 $importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry); 67 68 // Import the file. 69 $importproc->process(); 70 71 // Verify there is a new mod_resource created with correct name, description and containing the test.html file. 72 $modinfo = get_fast_modinfo($course, $teacher->id); 73 $cms = $modinfo->get_instances(); 74 $this->assertArrayHasKey('resource', $cms); 75 $cminfo = array_shift($cms['resource']); 76 $this->assertEquals('Resource name', $cminfo->get_formatted_name()); 77 $cm = get_coursemodule_from_id('', $cminfo->id, 0, false, MUST_EXIST); 78 list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cminfo, $course); 79 $this->assertEquals($remoteresource->get_description(), $data->intro); 80 $fs = get_file_storage(); 81 $files = $fs->get_area_files(\context_module::instance($cminfo->id)->id, 'mod_resource', 'content', false, 82 'sortorder DESC, id ASC', false); 83 $file = reset($files); 84 $this->assertEquals('test.html', $file->get_filename()); 85 $this->assertEquals('text/html', $file->get_mimetype()); 86 } 87 88 /** 89 * Test confirming that an exception is thrown when trying to process a resource which does not exist. 90 */ 91 public function test_process_invalid_resource() { 92 $this->resetAfterTest(); 93 94 // Set up a user as a teacher in a course. 95 $course = $this->getDataGenerator()->create_course(); 96 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); 97 $section = 0; 98 $this->setUser($teacher); 99 100 // Set up the import, using a mod_resource handler for the html extension. 101 $resourceurl = $this->getExternalTestFileUrl('/test.htmlzz'); 102 $remoteresource = new remote_resource( 103 new \curl(), 104 new url($resourceurl), 105 (object) [ 106 'name' => 'Resource name', 107 'description' => 'Resource description' 108 ] 109 ); 110 $handlerregistry = new import_handler_registry($course, $teacher); 111 $handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'resource', 112 new import_strategy_file()); 113 $importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry); 114 115 // Import the file. 116 $this->expectException(\coding_exception::class); 117 $importproc->process(); 118 } 119 120 /** 121 * Test confirming that imports can be completed using alternative import strategies. 122 */ 123 public function test_process_alternative_import_strategies() { 124 $this->resetAfterTest(); 125 126 // Set up a user as a teacher in a course. 127 $course = $this->getDataGenerator()->create_course(); 128 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'); 129 $section = 0; 130 $this->setUser($teacher); 131 132 // Set up the import, using a mod_url handler and the link import strategy. 133 $remoteresource = new remote_resource( 134 new \curl(), 135 new url('http://example.com/cats.pdf'), 136 (object) [ 137 'name' => 'Resource name', 138 'description' => 'Resource description' 139 ] 140 ); 141 $handlerregistry = new import_handler_registry($course, $teacher); 142 $handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'url', 143 new import_strategy_link()); 144 $importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry); 145 146 // Import the resource as a link. 147 $importproc->process(); 148 149 // Verify there is a new mod_url created with name 'cats' and containing the URL of the resource. 150 $modinfo = get_fast_modinfo($course, $teacher->id); 151 $cms = $modinfo->get_instances(); 152 $this->assertArrayHasKey('url', $cms); 153 $cminfo = array_shift($cms['url']); 154 $this->assertEquals('Resource name', $cminfo->get_formatted_name()); 155 } 156 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body