2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace mod_resource
;
20 * PHPUnit data generator testcase.
22 * @package mod_resource
24 * @copyright 2013 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 class generator_test
extends \advanced_testcase
{
28 public function test_generator() {
31 $this->resetAfterTest(true);
33 // Must be a non-guest user to create resources.
34 $this->setAdminUser();
36 // There are 0 resources initially.
37 $this->assertEquals(0, $DB->count_records('resource'));
39 // Create the generator object and do standard checks.
40 $generator = $this->getDataGenerator()->get_plugin_generator('mod_resource');
41 $this->assertInstanceOf('mod_resource_generator', $generator);
42 $this->assertEquals('resource', $generator->get_modulename());
44 // Create three instances in the site course.
45 $generator->create_instance(array('course' => $SITE->id
));
46 $generator->create_instance(array('course' => $SITE->id
));
47 $resource = $generator->create_instance(array('course' => $SITE->id
));
48 $this->assertEquals(3, $DB->count_records('resource'));
50 // Check the course-module is correct.
51 $cm = get_coursemodule_from_instance('resource', $resource->id
);
52 $this->assertEquals($resource->id
, $cm->instance
);
53 $this->assertEquals('resource', $cm->modname
);
54 $this->assertEquals($SITE->id
, $cm->course
);
56 // Check the context is correct.
57 $context = \context_module
::instance($cm->id
);
58 $this->assertEquals($resource->cmid
, $context->instanceid
);
60 // Check that generated resource module contains a file.
61 $fs = get_file_storage();
62 $files = $fs->get_area_files($context->id
, 'mod_resource', 'content', false, '', false);
63 $file = array_values($files)[0];
64 $this->assertCount(1, $files);
65 $this->assertEquals('resource3.txt', $file->get_filename());
66 $this->assertEquals('Test resource resource3.txt file', $file->get_content());
68 // Create a new resource specifying the file name.
69 $resource = $generator->create_instance(['course' => $SITE->id
, 'defaultfilename' => 'myfile.pdf']);
71 // Check that generated resource module contains a file with the specified name.
72 $cm = get_coursemodule_from_instance('resource', $resource->id
);
73 $context = \context_module
::instance($cm->id
);
74 $files = $fs->get_area_files($context->id
, 'mod_resource', 'content', false, '', false);
75 $file = array_values($files)[0];
76 $this->assertCount(1, $files);
77 $this->assertEquals('myfile.pdf', $file->get_filename());
78 $this->assertEquals('Test resource myfile.pdf file', $file->get_content());