See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [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 /** 18 * Test for content bank contenttype class. 19 * 20 * @package core_contentbank 21 * @category test 22 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core_contentbank; 27 28 use stdClass; 29 use context_system; 30 use contenttype_testable\contenttype as contenttype; 31 32 /** 33 * Test for content bank contenttype class. 34 * 35 * @package core_contentbank 36 * @category test 37 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 * @coversDefaultClass \core_contentbank\content 40 * 41 */ 42 class core_contenttype_content_testcase extends \advanced_testcase { 43 44 /** 45 * Setup to ensure that fixtures are loaded. 46 */ 47 public static function setupBeforeClass(): void { 48 global $CFG; 49 50 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php'); 51 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php'); 52 } 53 54 /** 55 * Tests for behaviour of get_name(). 56 * 57 * @covers ::get_name 58 */ 59 public function test_get_name() { 60 $this->resetAfterTest(); 61 62 // Create content. 63 $record = new stdClass(); 64 $record->name = 'Test content'; 65 $record->configdata = ''; 66 67 $contenttype = new contenttype(context_system::instance()); 68 $content = $contenttype->create_content($record); 69 $this->assertEquals($record->name, $content->get_name()); 70 } 71 72 /** 73 * Data provider for test_set_name. 74 * 75 * @return array 76 */ 77 public function set_name_provider() { 78 return [ 79 'Standard name' => ['New name', 'New name'], 80 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'], 81 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'], 82 'Name with tags' => ['This is <b>bold</b>', 'This is bold'], 83 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)], 84 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)], 85 'Empty name' => ['', 'Old name'], 86 'Blanks only' => [' ', 'Old name'], 87 ]; 88 } 89 90 /** 91 * Tests for 'set_name' behaviour. 92 * 93 * @dataProvider set_name_provider 94 * @param string $newname The name to set 95 * @param string $expected The name result 96 * 97 * @covers ::set_name 98 */ 99 public function test_set_name(string $newname, string $expected) { 100 global $DB; 101 102 $this->resetAfterTest(); 103 $this->setAdminUser(); 104 105 $oldname = "Old name"; 106 $context = context_system::instance(); 107 108 // Create content. 109 $record = new stdClass(); 110 $record->name = $oldname; 111 112 $contenttype = new contenttype($context); 113 $content = $contenttype->create_content($record); 114 $this->assertEquals($oldname, $content->get_name()); 115 116 $content->set_name($newname); 117 $this->assertEquals($expected, $content->get_name()); 118 119 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]); 120 $this->assertEquals($expected, $record->name); 121 } 122 123 /** 124 * Tests for behaviour of get_content_type(). 125 * 126 * @covers ::get_content_type 127 */ 128 public function test_get_content_type() { 129 $this->resetAfterTest(); 130 131 // Create content. 132 $record = new stdClass(); 133 $record->name = 'Test content'; 134 $record->configdata = ''; 135 136 $contenttype = new contenttype(context_system::instance()); 137 $content = $contenttype->create_content($record); 138 $this->assertEquals('contenttype_testable', $content->get_content_type()); 139 } 140 141 /** 142 * Tests for 'configdata' behaviour. 143 * 144 * @covers ::set_configdata 145 */ 146 public function test_configdata_changes() { 147 $this->resetAfterTest(); 148 149 $configdata = "{img: 'icon.svg'}"; 150 151 // Create content. 152 $record = new stdClass(); 153 $record->configdata = $configdata; 154 155 $contenttype = new contenttype(context_system::instance()); 156 $content = $contenttype->create_content($record); 157 $this->assertEquals($configdata, $content->get_configdata()); 158 159 $configdata = "{alt: 'Name'}"; 160 $content->set_configdata($configdata); 161 $this->assertEquals($configdata, $content->get_configdata()); 162 } 163 164 /** 165 * Tests for 'set_contextid' behaviour. 166 * 167 * @covers ::set_contextid 168 */ 169 public function test_set_contextid() { 170 $this->resetAfterTest(); 171 $this->setAdminUser(); 172 $context = context_system::instance(); 173 $course = $this->getDataGenerator()->create_course(); 174 $newcontext = \context_course::instance($course->id); 175 176 // Add some content to the content bank. 177 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank'); 178 $contents = $generator->generate_contentbank_data('contenttype_testable', 3, 0, $context); 179 $content = reset($contents); 180 181 $oldcontextid = $content->get_contextid(); 182 183 $file = $content->get_file(); 184 $this->assertEquals($oldcontextid, $file->get_contextid()); 185 $this->assertEquals($context->id, $oldcontextid); 186 $this->assertNotEquals($newcontext->id, $oldcontextid); 187 188 $content->set_contextid($newcontext->id); 189 $file = $content->get_file(); 190 191 $this->assertEquals($newcontext->id, $content->get_contextid()); 192 $this->assertEquals($newcontext->id, $file->get_contextid()); 193 } 194 195 /** 196 * Tests for 'import_file' behaviour when replacing a file. 197 * 198 * @covers ::import_file 199 */ 200 public function test_import_file_replace(): void { 201 global $USER; 202 $this->resetAfterTest(); 203 $this->setAdminUser(); 204 $context = context_system::instance(); 205 206 // Add some content to the content bank. 207 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank'); 208 $contents = $generator->generate_contentbank_data('contenttype_testable', 3, 0, $context); 209 $content = reset($contents); 210 211 $originalfile = $content->get_file(); 212 213 // Create a dummy file. 214 $filerecord = array( 215 'contextid' => $context->id, 216 'component' => 'contentbank', 217 'filearea' => 'draft', 218 'itemid' => $content->get_id(), 219 'filepath' => '/', 220 'filename' => 'example.txt' 221 ); 222 $fs = get_file_storage(); 223 $file = $fs->create_file_from_string($filerecord, 'Dummy content '); 224 225 $importedfile = $content->import_file($file); 226 227 $this->assertEquals($originalfile->get_filename(), $importedfile->get_filename()); 228 $this->assertEquals($originalfile->get_filearea(), $importedfile->get_filearea()); 229 $this->assertEquals($originalfile->get_filepath(), $importedfile->get_filepath()); 230 $this->assertEquals($originalfile->get_mimetype(), $importedfile->get_mimetype()); 231 232 $this->assertEquals($file->get_userid(), $importedfile->get_userid()); 233 $this->assertEquals($file->get_contenthash(), $importedfile->get_contenthash()); 234 } 235 236 /** 237 * Tests for 'import_file' behaviour when uploading a new file. 238 * 239 * @covers ::import_file 240 */ 241 public function test_import_file_upload(): void { 242 global $USER; 243 $this->resetAfterTest(); 244 $this->setAdminUser(); 245 $context = context_system::instance(); 246 247 $type = new contenttype($context); 248 $record = (object)[ 249 'name' => 'content name', 250 'usercreated' => $USER->id, 251 ]; 252 $content = $type->create_content($record); 253 254 // Create a dummy file. 255 $filerecord = array( 256 'contextid' => $context->id, 257 'component' => 'contentbank', 258 'filearea' => 'draft', 259 'itemid' => $content->get_id(), 260 'filepath' => '/', 261 'filename' => 'example.txt' 262 ); 263 $fs = get_file_storage(); 264 $file = $fs->create_file_from_string($filerecord, 'Dummy content '); 265 266 $importedfile = $content->import_file($file); 267 268 $this->assertEquals($file->get_filename(), $importedfile->get_filename()); 269 $this->assertEquals($file->get_userid(), $importedfile->get_userid()); 270 $this->assertEquals($file->get_mimetype(), $importedfile->get_mimetype()); 271 $this->assertEquals($file->get_contenthash(), $importedfile->get_contenthash()); 272 $this->assertEquals('public', $importedfile->get_filearea()); 273 $this->assertEquals('/', $importedfile->get_filepath()); 274 275 $contentfile = $content->get_file($file); 276 $this->assertEquals($importedfile->get_id(), $contentfile->get_id()); 277 } 278 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body