Differences Between: [Versions 310 and 403] [Versions 39 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace mod_resource; 18 19 /** 20 * PHPUnit data generator testcase. 21 * 22 * @package mod_resource 23 * @category phpunit 24 * @covers \mod_resource_generator 25 * @copyright 2013 The Open University 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 class generator_test extends \advanced_testcase { 29 30 public function test_generator() { 31 global $DB, $SITE; 32 33 $this->resetAfterTest(true); 34 35 // Must be a non-guest user to create resources. 36 $this->setAdminUser(); 37 38 // There are 0 resources initially. 39 $this->assertEquals(0, $DB->count_records('resource')); 40 41 // Create the generator object and do standard checks. 42 $generator = $this->getDataGenerator()->get_plugin_generator('mod_resource'); 43 $this->assertInstanceOf('mod_resource_generator', $generator); 44 $this->assertEquals('resource', $generator->get_modulename()); 45 46 // Create three instances in the site course. 47 $generator->create_instance(array('course' => $SITE->id)); 48 $generator->create_instance(array('course' => $SITE->id)); 49 $resource = $generator->create_instance(array('course' => $SITE->id)); 50 $this->assertEquals(3, $DB->count_records('resource')); 51 52 // Check the course-module is correct. 53 $cm = get_coursemodule_from_instance('resource', $resource->id); 54 $this->assertEquals($resource->id, $cm->instance); 55 $this->assertEquals('resource', $cm->modname); 56 $this->assertEquals($SITE->id, $cm->course); 57 58 // Check the context is correct. 59 $context = \context_module::instance($cm->id); 60 $this->assertEquals($resource->cmid, $context->instanceid); 61 62 // Check that generated resource module contains a file. 63 $fs = get_file_storage(); 64 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false); 65 $file = array_values($files)[0]; 66 $this->assertCount(1, $files); 67 $this->assertEquals('resource3.txt', $file->get_filename()); 68 $this->assertEquals('Test resource resource3.txt file', $file->get_content()); 69 70 // Create a new resource specifying the file name. 71 $resource = $generator->create_instance(['course' => $SITE->id, 'defaultfilename' => 'myfile.pdf']); 72 73 // Check that generated resource module contains a file with the specified name. 74 $cm = get_coursemodule_from_instance('resource', $resource->id); 75 $context = \context_module::instance($cm->id); 76 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false); 77 $file = array_values($files)[0]; 78 $this->assertCount(1, $files); 79 $this->assertEquals('myfile.pdf', $file->get_filename()); 80 $this->assertEquals('Test resource myfile.pdf file', $file->get_content()); 81 82 // Create a new resource uploading a file. 83 $resource = $generator->create_instance([ 84 'course' => $SITE->id, 85 'uploaded' => true, 86 'defaultfilename' => 'mod/resource/tests/fixtures/samplefile.txt', 87 ]); 88 89 // Check that generated resource module contains the uploaded samplefile.txt. 90 $cm = get_coursemodule_from_instance('resource', $resource->id); 91 $context = \context_module::instance($cm->id); 92 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false); 93 $file = array_values($files)[0]; 94 $this->assertCount(1, $files); 95 $this->assertEquals('samplefile.txt', $file->get_filename()); 96 $this->assertEquals('Hello!', $file->get_content()); 97 98 // Try to generate a resource with uploaded file without specifying the file. 99 try { 100 $resource = $generator->create_instance([ 101 'course' => $SITE->id, 102 'uploaded' => true, 103 ]); 104 $this->assertTrue(false, 'coding_exception expected, defaultfilename is required'); 105 } catch (\Exception $e) { 106 $this->assertInstanceOf(\coding_exception::class, $e); 107 $this->assertStringContainsString('defaultfilename option is required', $e->getMessage()); 108 } 109 110 // Try to generate a resource with uploaded file pointing to non-existing file. 111 try { 112 $resource = $generator->create_instance([ 113 'course' => $SITE->id, 114 'uploaded' => true, 115 'defaultfilename' => 'mod/resource/tests/fixtures/doesnotexist.txt', 116 ]); 117 $this->assertTrue(false, 'coding_exception expected, defaultfilename must point to an existing file'); 118 } catch (\Exception $e) { 119 $this->assertInstanceOf(\coding_exception::class, $e); 120 $this->assertStringContainsString('defaultfilename option must point to an existing file', $e->getMessage()); 121 } 122 } 123 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body