| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: filefield.test,v 1.9 2010/12/10 19:39:27 quicksketch Exp $ 3 4 class FileFieldTestCase extends DrupalWebTestCase { 5 protected $admin_user; 6 7 /** 8 * Implementation of setUp(). 9 */ 10 function setUp() { 11 // Views is included here just so that it doesn't whine when CCK tries to 12 // clear the caches. 13 $modules = array_merge(func_get_args(), array('content', 'filefield', 'filefield_meta', 'getid3', 'mimedetect', 'token', 'views')); 14 call_user_func_array(array($this, 'parent::setUp'), $modules); 15 16 // Create and login user 17 $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer site configuration', 'administer content types', 'administer nodes', 'administer files')); 18 $this->drupalLogin($this->admin_user); 19 } 20 21 /** 22 * Get a sample file of the specified type. 23 */ 24 function getTestFile($type, $size = NULL) { 25 // Get a file to upload. 26 $file = current($this->drupalGetTestFiles($type, $size)); 27 28 // SimpleTest files incorrectly use "filename" instead of "filepath". 29 $file->filepath = $file->filename; 30 $file->filename = basename($file->filepath); 31 $file->filesize = filesize($file->filepath); 32 33 return $file; 34 } 35 36 /** 37 * Create a new file field. 38 * 39 * @param $name 40 * The name of the new field (all lowercase), exclude the "field_" prefix. 41 * @param $type 42 * The node type that this field will be added to. 43 * @param $field_options 44 * A list of field options that will be added to the defaults. 45 * @param $widget_options 46 * A list of widget options that will be added to the widget defaults. 47 */ 48 function createFileField($name, $type, $field_options = array(), $widget_options = array()) { 49 module_load_include('inc', 'content', 'includes/content.crud'); 50 $field = array( 51 'label' => $name, 52 'field_name' => $name, 53 'type' => 'filefield', 54 'widget_type' => 'filefield_widget', 55 'weight' => 0, 56 'parent' => 0, 57 'type_name' => $type, 58 'list_field' => 0, 59 'list_default' => 1, 60 'description_field' => 0, 61 ); 62 63 $field = array_merge($field, $field_options); 64 $field = content_field_instance_create($field); 65 66 $widget = array( 67 'type' => 'filefield_widget', 68 'file_extensions' => '', 69 ); 70 71 $field['widget'] = array_merge($field['widget'], $widget, $widget_options); 72 $field = content_field_instance_update($field); 73 74 return $field; 75 } 76 77 /** 78 * Update an existing FileField with new settings. 79 */ 80 function updateFileField($name, $type, $field_options = array(), $widget_options = array()) { 81 $field = content_fields($name, $type); 82 $field = array_merge($field, $field_options); 83 $field['widget'] = array_merge($field['widget'], $widget_options); 84 85 return content_field_instance_update($field); 86 } 87 88 /** 89 * Upload a file to a node. 90 */ 91 function uploadNodeFile($file, $field, $nid_or_type, $new_revision = TRUE) { 92 $field_name = $field['field_name']; 93 $edit = array( 94 'title' => $this->randomName(), 95 'revision' => (string) (int) $new_revision, 96 ); 97 98 if (is_numeric($nid_or_type)) { 99 $node = node_load($nid_or_type); 100 $delta = isset($node->$field_name) ? count($node->$field_name) : 0; 101 $edit['files[' . $field_name . '_' . $delta . ']'] = realpath($file->filepath); 102 $this->drupalPost('node/' . $nid_or_type . '/edit', $edit, t('Save')); 103 } 104 else { 105 $delta = '0'; 106 $edit['files[' . $field_name . '_' . $delta . ']'] = realpath($file->filepath); 107 $type = str_replace('_', '-', $nid_or_type); 108 $this->drupalPost('node/add/' . $type, $edit, t('Save')); 109 } 110 111 $matches = array(); 112 preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches); 113 $nid = isset($matches[1]) ? $matches[1] : FALSE; 114 115 // Edit the node and add a description if possible. 116 if ($nid && $field['description_field']) { 117 $edit = array( 118 'revision' => 0, 119 $field_name . '[' . $delta . '][data][description]' => $this->randomString(), 120 ); 121 $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save')); 122 } 123 124 return $nid; 125 } 126 127 /** 128 * Remove a file from a node. 129 * 130 * Note that if replacing a file, it must first be removed then added again. 131 */ 132 function removeNodeFile($nid, $new_revision = TRUE) { 133 $edit = array( 134 'revision' => (string) (int) $new_revision, 135 ); 136 137 $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove')); 138 $this->drupalPost(NULL, $edit, t('Save')); 139 } 140 141 /** 142 * Replace a file within a node. 143 */ 144 function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) { 145 $edit = array( 146 'files[' . $field_name . '_0]' => realpath($file->filepath), 147 'revision' => (string) (int) $new_revision, 148 ); 149 150 $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove')); 151 $this->drupalPost(NULL, $edit, t('Save')); 152 } 153 154 /** 155 * Assert that a file exists physically on disk. 156 */ 157 function assertFileExists($file, $message = NULL) { 158 $message = isset($message) ? $message : t('File %file exists on the disk.', array('%file' => $file['filepath'])); 159 $this->assertTrue(is_file($file['filepath']), $message); 160 } 161 162 /** 163 * Assert that a file exists in the database. 164 */ 165 function assertFileEntryExists($file, $message = NULL) { 166 module_load_include('inc', 'filefield', 'field_file'); 167 $db_file = field_file_load($file['fid'], TRUE); 168 $message = isset($message) ? $message : t('File %file exists in database at the correct path.', array('%file' => $file['filepath'])); 169 $this->assertEqual($db_file['filepath'], $file['filepath'], $message); 170 } 171 172 /** 173 * Assert that a file does not exist on disk. 174 */ 175 function assertFileNotExists($file, $message = NULL) { 176 $message = isset($message) ? $message : t('File %file exists on the disk.', array('%file' => $file['filepath'])); 177 $this->assertFalse(is_file($file['filepath']), $message); 178 } 179 180 /** 181 * Assert that a file does not exist in the database. 182 */ 183 function assertFileEntryNotExists($file, $message) { 184 module_load_include('inc', 'filefield', 'field_file'); 185 $message = isset($message) ? $message : t('File %file exists in database at the correct path.', array('%file' => $file['filepath'])); 186 $this->assertFalse(field_file_load($file['fid'], TRUE), $message); 187 } 188 } 189 190 /** 191 * Test class to test file handling with node revisions. 192 */ 193 class FileFieldRevisionTestCase extends FileFieldTestCase { 194 function getInfo() { 195 return array( 196 'name' => t('FileField revision test'), 197 'description' => t('Test creating and deleting revisions with files attached.'), 198 'group' => t('FileField'), 199 ); 200 } 201 202 /** 203 * Test creating multiple revisions of a node and managing the attached files. 204 * 205 * Expected behaviors: 206 * - Adding a new revision will make another entry in the field table, but 207 * the original file will not be duplicated. 208 * - Deleting a revision should not delete the original file if the file 209 * is in use by another revision. 210 * - When the last revision that uses a file is deleted, the original file 211 * should be deleted also. 212 */ 213 function testRevisions() { 214 $field_name = 'field_' . strtolower($this->randomName()); 215 $type = $this->drupalCreateContentType(); 216 $field_options = array( 217 'description_field' => '1', 218 ); 219 $field = $this->createFileField($field_name, $type->name, $field_options); 220 221 $test_file = $this->getTestFile('text'); 222 223 // Create a new node with the uploaded file. 224 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 225 226 // Check that the file exists on disk and in the database. 227 $node = node_load($nid, NULL, TRUE); 228 $node_file_r1 = $node->{$field['field_name']}[0]; 229 $node_vid_r1 = $node->vid; 230 $this->assertFileExists($node_file_r1, t('New file saved to disk on node creation.')); 231 $this->assertFileEntryExists($node_file_r1, t('File entry exists in database on node creation.')); 232 233 // Upload another file to the same node in a new revision. 234 $this->replaceNodeFile($test_file, $field_name, $nid); 235 $node = node_load($nid, NULL, TRUE); 236 $node_file_r2 = $node->{$field['field_name']}[0]; 237 $node_vid_r2 = $node->vid; 238 $this->assertFileExists($node_file_r2, t('Replacement file exists on disk after creating new revision.')); 239 $this->assertFileEntryExists($node_file_r2, t('Replacement file entry exists in database after creating new revision.')); 240 241 // Check that the original file is still in place on the first revision. 242 $node = node_load($nid, $node_vid_r1, TRUE); 243 $this->assertEqual($node_file_r1, $node->{$field['field_name']}[0], t('Original file still in place after replacing file in new revision.')); 244 $this->assertFileExists($node_file_r1, t('Original file still in place after replacing file in new revision.')); 245 $this->assertFileEntryExists($node_file_r1, t('Original file entry still in place after replacing file in new revision')); 246 247 // Save a new version of the node without any changes. 248 // Check that the file is still the same as the previous revision. 249 $this->drupalPost('node/' . $nid . '/edit', array('revision' => '1'), t('Save')); 250 $node = node_load($nid, NULL, TRUE); 251 $node_file_r3 = $node->{$field['field_name']}[0]; 252 $node_vid_r3 = $node->vid; 253 254 // FileField Meta's extensive meta data can be difficult to match up exactly 255 // (mostly differences between strings and integers). Just compare the 256 // descriptions. 257 $node_file_r2['data'] = array('description' => $node_file_r2['data']['description']); 258 $node_file_r3['data'] = array('description' => $node_file_r3['data']['description']); 259 $this->assertEqual($node_file_r2, $node_file_r3, t('Previous revision file still in place after creating a new revision without a new file.')); 260 261 // Revert to the first revision and check that the original file is active. 262 $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert')); 263 $node = node_load($nid, NULL, TRUE); 264 $node_file_r4 = $node->{$field['field_name']}[0]; 265 $node_vid_r4 = $node->vid; 266 $this->assertEqual($node_file_r1, $node_file_r4, t('Original revision file still in place after reverting to the original revision.')); 267 268 // Delete the second revision and check that the file is kept (since it is 269 // still being used by the third revision). 270 $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', array(), t('Delete')); 271 $this->assertFileExists($node_file_r3, t('Second file is still available after deleting second revision, since it is being used by the third revision.')); 272 $this->assertFileEntryExists($node_file_r3, t('Second file entry is still available after deleting second revision, since it is being used by the third revision.')); 273 274 // Delete the third revision and check that the file is deleted also. 275 $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', array(), t('Delete')); 276 $this->assertFileNotExists($node_file_r3, t('Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.')); 277 $this->assertFileEntryNotExists($node_file_r3, t('Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.')); 278 279 // Delete the entire node and check that the original file is deleted. 280 $this->drupalPost('node/' . $nid . '/delete', array(), t('Delete')); 281 $this->assertFileNotExists($node_file_r1, t('Original file is deleted after deleting the entire node with two revisions remaining.')); 282 $this->assertFileEntryNotExists($node_file_r1, t('Original file entry is deleted after deleting the entire node with two revisions remaining.')); 283 } 284 } 285 286 /** 287 * Test class to check that formatters are working properly. 288 */ 289 class FileFieldDisplayTestCase extends FileFieldTestCase { 290 function getInfo() { 291 return array( 292 'name' => t('FileField display tests'), 293 'description' => t('Test the display of file fields in node and views.'), 294 'group' => t('FileField'), 295 ); 296 } 297 298 /** 299 * Test normal formatter display on node display. 300 */ 301 function testNodeDisplay() { 302 $field_name = 'field_' . strtolower($this->randomName()); 303 $type = $this->drupalCreateContentType(); 304 $field_options = array( 305 'description_field' => '1', 306 'list_field' => '1', 307 'list_default' => '1', 308 ); 309 $field = $this->createFileField($field_name, $type->name, $field_options); 310 $test_file = $this->getTestFile('text'); 311 312 // Create a new node with the uploaded file. 313 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 314 $this->drupalGet('node/' . $nid); 315 316 // Check that the default formatter is displaying with the file name. 317 $node = node_load($nid, NULL, TRUE); 318 $node_file = $node->{$field['field_name']}[0]; 319 $default_output = theme('filefield_file', $node_file); 320 $this->assertRaw($default_output, t('Default formatter displaying correctly on full node view.')); 321 322 // Turn the "list" option off and check that the file is no longer listed. 323 $edit = array($field['field_name'] . '[0][list]' => FALSE); 324 $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save')); 325 326 $this->assertNoRaw($default_output, t('Field is hidden when "list" option is unchecked.')); 327 328 } 329 } 330 331 /** 332 * Test class to check for various validations. 333 */ 334 class FileFieldValidateTestCase extends FileFieldTestCase { 335 protected $field; 336 protected $node_type; 337 338 function getInfo() { 339 return array( 340 'name' => t('FileField validation tests'), 341 'description' => t('Tests validation functions such as file type, max file size, max size per node, and required.'), 342 'group' => t('FileField'), 343 ); 344 } 345 346 /** 347 * Implementation of setUp(). 348 */ 349 function setUp() { 350 $modules = array_merge(func_get_args(), array('content', 'filefield', 'filefield_meta', 'getid3', 'mimedetect', 'token', 'views')); 351 call_user_func_array(array($this, 'parent::setUp'), $modules); 352 353 $this->node_type = $this->drupalCreateContentType(); 354 $this->node_type->url_name = str_replace('_', '-', $this->node_type->name); 355 $field_name = 'field_' . strtolower($this->randomName()); 356 $this->field = $this->createFileField($field_name, $this->node_type->name); 357 } 358 359 /** 360 * Test required property on file fields. 361 */ 362 function testRequired() { 363 $type = $this->node_type; 364 $field = $this->field; 365 366 // Make our field required. 367 $this->updateFileField($field['field_name'], $type->name, array('required' => '1')); 368 369 $test_file = $this->getTestFile('image'); 370 371 // Try to post a new node without uploading a file. 372 $edit = array('title' => $this->randomName()); 373 $this->drupalPost('node/add/' . $type->url_name, $edit, t('Save')); 374 375 $this->assertRaw(t('%title field is required.', array('%title' => $field['widget']['label'])), t('Node save failed when required file field was empty.')); 376 377 // Create a new node with the uploaded file. 378 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 379 $node = node_load($nid, NULL, TRUE); 380 $node_file = $node->{$field['field_name']}[0]; 381 $this->assertFileExists($node_file, t('File exists after uploading to the required field.')); 382 $this->assertFileEntryExists($node_file, t('File entry exists after uploading to the required field.')); 383 384 // Try again with a multiple value field. 385 $this->updateFileField($field['field_name'], $type->name, array('multiple' => '0', 'required' => '1')); 386 387 // Try to post a new node without uploading a file in the multivalue field. 388 $edit = array('title' => $this->randomName()); 389 $this->drupalPost('node/add/' . $type->url_name, $edit, t('Save')); 390 391 $this->assertRaw(t('%title field is required.', array('%title' => $field['widget']['label'])), t('Node save failed when required multiple value file field was empty.')); 392 393 // Create a new node with the uploaded file into the multivalue field. 394 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 395 $node = node_load($nid, NULL, TRUE); 396 $node_file = $node->{$field['field_name']}[0]; 397 $this->assertFileExists($node_file, t('File exists after uploading to the required multiple value field.')); 398 $this->assertFileEntryExists($node_file, t('File entry exists after uploading to the required multipel value field.')); 399 400 // Set the field back to not required. 401 $this->updateFileField($field['field_name'], $type->name, array('multiple' => '0', 'required' => '1')); 402 } 403 404 /** 405 * Test the max file size validator. 406 */ 407 function testFileMaxSize() { 408 $type = $this->node_type; 409 $field = $this->field; 410 411 $small_file = $this->getTestFile('text', 131072); // 128KB. 412 $large_file = $this->getTestFile('text', 1310720); // 1.2MB 413 414 // Test uploading both a large and small file with different increments. 415 $sizes = array( 416 '1M' => 1048576, 417 '1024K' => 1048576, 418 '1048576' => 1048576, 419 ); 420 421 foreach ($sizes as $max_filesize_per_file => $file_limit) { 422 // Set the max file upload size. 423 $this->updateFileField($field['field_name'], $type->name, array(), array('max_filesize_per_file' => $max_filesize_per_file)); 424 425 // Create a new node with the small file, which should pass. 426 $nid = $this->uploadNodeFile($small_file, $field, $type->name); 427 $node = node_load($nid, NULL, TRUE); 428 $node_file = $node->{$field['field_name']}[0]; 429 $this->assertFileExists($node_file, t('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize_per_file))); 430 $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize_per_file))); 431 432 // Check that uploading the large file fails (1M limit). 433 $nid = $this->uploadNodeFile($large_file, $field, $type->name); 434 $error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($large_file->filesize), '%maxsize' => format_size($file_limit))); 435 $this->assertRaw($error_message, t('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', array('%filesize' => format_size($large_file->filesize), '%maxsize' => $max_filesize_per_file))); 436 } 437 438 // Turn off the max filesize. 439 $this->updateFileField($field['field_name'], $type->name, array(), array('max_filesize_per_file' => '')); 440 441 // Upload the big file successfully. 442 $nid = $this->uploadNodeFile($large_file, $field, $type->name); 443 $node = node_load($nid, NULL, TRUE); 444 $node_file = $node->{$field['field_name']}[0]; 445 $this->assertFileExists($node_file, t('File exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize)))); 446 $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize)))); 447 } 448 449 /** 450 * Test the max file size per node validator. 451 */ 452 function testNodeMaxSize() { 453 $type = $this->node_type; 454 $field = $this->field; 455 456 $small_file = $this->getTestFile('text', 131072); // 128KB. 457 $large_file = $this->getTestFile('text', 1310720); // 1.2MB 458 459 // Set the max file upload size. 460 $max_node_limit = '256K'; 461 $file_limit = 262144; 462 $this->updateFileField($field['field_name'], $type->name, array('multiple' => '1'), array('max_filesize_per_node' => $max_node_limit)); 463 464 // Create a new node with the small file, which should pass. 465 $nid = $this->uploadNodeFile($small_file, $field, $type->name); 466 $node = node_load($nid, NULL, TRUE); 467 $node_file = $node->{$field['field_name']}[0]; 468 $this->assertFileExists($node_file, t('File exists after uploading a file (%filesize) under the max node limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_node_limit))); 469 $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) under the max node limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_node_limit))); 470 471 // Add a second file to the same node which should pass. 472 $nid = $this->uploadNodeFile($small_file, $field, $nid); 473 $node = node_load($nid, NULL, TRUE); 474 $node_file = $node->{$field['field_name']}[0]; 475 $this->assertFileExists($node_file, t('File exists after uploading a file (%filesize) under the max node limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_node_limit))); 476 $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) under the max node limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_node_limit))); 477 478 // Add a third file to the same node which should fail. 479 $nid = $this->uploadNodeFile($small_file, $field, $nid); 480 $error_message = t('exceeds field settings of %msize.', array('%msize' => format_size($file_limit))); 481 $this->assertRaw($error_message, t('File not uploaded as the file (%filesize) exceeds the max node limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_node_limit))); 482 483 // Check that uploading the large file fails (1M limit). 484 $nid = $this->uploadNodeFile($large_file, $field, $type->name); 485 $error_message = t('exceeds field settings of %msize.', array('%msize' => format_size($file_limit))); 486 $this->assertRaw($error_message, t('File not uploaded as the file (%filesize) exceeds the max node limit (%maxsize).', array('%filesize' => format_size($large_file->filesize), '%maxsize' => $max_node_limit))); 487 488 // Turn off the max filesize per node. 489 $this->updateFileField($field['field_name'], $type->name, array('multiple' => '0'), array('max_filesize_per_node' => '')); 490 } 491 492 /** 493 * Test the file extension, do additional checks if mimedetect is installed. 494 */ 495 function testFileExtension() { 496 $type = $this->node_type; 497 $field = $this->field; 498 499 // Setup files for extension checking. 500 $test_file = $this->getTestFile('image'); 501 preg_match('/(?<=\.)[^\.]*$/', $test_file->filename, $matches); 502 $extention = current($matches); 503 $wrong_extension_file = drupal_clone($test_file); 504 $wrong_extension_file->filename = str_replace(".$extention", '.jpg', $test_file->filename); 505 $wrong_extension_file->filepath = file_directory_path() .'/'. $wrong_extension_file->filename; 506 $original_path = $test_file->filepath; 507 file_copy($original_path, $wrong_extension_file->filepath); 508 509 $this->updateFileField($field['field_name'], $type->name, array(), array('file_extensions' => '')); 510 511 // Check that the file can be uploaded with no extension checking. 512 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 513 $node = node_load($nid, NULL, TRUE); 514 $node_file = $node->{$field['field_name']}[0]; 515 $this->assertFileExists($node_file, t('File exists after uploading a file with no extension checking.')); 516 $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file with no extension checking.')); 517 518 // Enable extension checking. 519 $this->updateFileField($field['field_name'], $type->name, array(), array('file_extensions' => "txt png jpg $extention")); 520 521 // Check that the file can be uploaded with extension checking. 522 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 523 $node = node_load($nid, NULL, TRUE); 524 $node_file = $node->{$field['field_name']}[0]; 525 $this->assertFileExists($node_file, t('File exists after uploading a file with extension checking.')); 526 $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file with extension checking.')); 527 528 // Check that a mismatched extension cannot be uploaded. 529 $mimedetect = FALSE; 530 if (module_exists('mimedetect')) { 531 $mimedetect = TRUE; 532 module_load_include('install', 'mimedetect'); 533 if (!extension_loaded('fileinfo')) { 534 variable_set('mimedetect_enable_file_binary', 1); 535 } 536 $requirements = mimedetect_requirements('runtime'); 537 foreach ($requirements as $requirement) { 538 if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) { 539 $mimedetect = FALSE; 540 } 541 } 542 } 543 if ($mimedetect) { 544 $this->uploadNodeFile($wrong_extension_file, $field, $type->name); 545 $error_pattern = "/The file contents \([a-z\-\/]+\) do not match its extension \([a-z]+\)\./"; 546 $this->assertPattern($error_pattern, t('File prevented from uploading because its extension does not match its content.')); 547 } 548 else { 549 $this->assertTrue(TRUE, t('Mime type checking and extension spoofing skipped because the mimedetect module is not available.')); 550 } 551 552 // Disable the extension checking. 553 $this->updateFileField($field['field_name'], $type->name, array(), array('file_extensions' => '')); 554 } 555 } 556 557 /** 558 * Test class to check that files are uploaded to proper locations. 559 */ 560 class FileFieldPathTestCase extends FileFieldTestCase { 561 function getInfo() { 562 return array( 563 'name' => t('FileField file path tests'), 564 'description' => t('Test that files are uploaded to the proper location, extra testing if Token module is available.'), 565 'group' => t('FileField'), 566 ); 567 } 568 569 /** 570 * Test normal formatter display on node display. 571 */ 572 function testUploadPath() { 573 $field_name = 'field_' . strtolower($this->randomName()); 574 $type = $this->drupalCreateContentType(); 575 $field = $this->createFileField($field_name, $type->name); 576 $test_file = $this->getTestFile('text'); 577 578 // Create a new node. 579 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 580 581 // Check that the file was uploaded to the file root. 582 $node = node_load($nid, NULL, TRUE); 583 $node_file = $node->{$field['field_name']}[0]; 584 $this->assertPathMatch(file_directory_path() . '/' . $test_file->filename, $node_file['filepath'], t('The file %file was uploaded to the correct path.', array('%file' => $node_file['filepath']))); 585 586 // Change the path to contain multiple subdirectories. 587 $field = $this->updateFileField($field['field_name'], $type->name, array(), array('file_path' => 'foo/bar/baz')); 588 589 // Upload a new file into the subdirectories. 590 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 591 592 // Check that the file was uploaded into the subdirectory. 593 $node = node_load($nid, NULL, TRUE); 594 $node_file = $node->{$field['field_name']}[0]; 595 $this->assertPathMatch(file_directory_path() . '/foo/bar/baz/' . $test_file->filename, $node_file['filepath'], t('The file %file was uploaded to the correct path.', array('%file' => $node_file['filepath']))); 596 597 // Check the path when used with tokens. 598 if (module_exists('token')) { 599 // Change the path to contain multiple token directories. 600 $field = $this->updateFileField($field['field_name'], $type->name, array(), array('file_path' => '[uid]/[user-raw]')); 601 602 // Upload a new file into the token subdirectories. 603 $nid = $this->uploadNodeFile($test_file, $field, $type->name); 604 605 // Check that the file was uploaded into the subdirectory. 606 $node = node_load($nid, NULL, TRUE); 607 $node_file = $node->{$field['field_name']}[0]; 608 $subdirectory = token_replace('[uid]/[user-raw]', 'user', $this->admin_user); 609 $this->assertPathMatch(file_directory_path() . '/' . $subdirectory . '/' . $test_file->filename, $node_file['filepath'], t('The file %file was uploaded to the correct path with token replacements.', array('%file' => $node_file['filepath']))); 610 } 611 else { 612 $this->assertTrue(TRUE, t('File path token test skipped because the Token module is not available.')); 613 } 614 615 } 616 617 /** 618 * A loose assertion to check that a file is uploaded to the right location. 619 * 620 * @param $expected_path 621 * The location where the file is expected to be uploaded. Duplicate file 622 * names to not need to be taken into account. 623 * @param $actual_path 624 * Where the file was actually uploaded. 625 * @param $message 626 * The message to display with this assertion. 627 */ 628 function assertPathMatch($expected_path, $actual_path, $message) { 629 // Strip off the extension of the expected path to allow for _0, _1, etc. 630 // suffixes when the file hits a duplicate name. 631 $pos = strrpos($expected_path, '.'); 632 $base_path = substr($expected_path, 0, $pos); 633 $extension = substr($expected_path, $pos + 1); 634 635 $result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path); 636 $this->assertTrue($result, $message); 637 } 638 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |