| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: image.test,v 1.14 2009/12/30 12:51:47 joachim Exp $ 3 4 /** 5 * Test image functionality. 6 */ 7 class ImageTestCase extends DrupalWebTestCase { 8 protected $web_user; 9 protected $image; 10 protected $another_image; 11 12 function getInfo() { 13 return array( 14 'name' => t('Image functionality'), 15 'description' => t('Test Image module functionality.'), 16 'group' => t('Image'), 17 ); 18 } 19 20 function setUp() { 21 parent::setUp('image'); 22 23 // User to create images. 24 $this->web_user = $this->drupalCreateUser(array('create images', 'view original images', 'edit own images', 'edit any images', 25 'delete own images', 'delete any images', 'administer site configuration')); 26 $this->drupalLogin($this->web_user); 27 28 // Uploadable image. 29 $this->image = 'misc/druplicon.png'; 30 $this->another_image = 'misc/throbber.gif'; 31 32 // Set small dimensions for testing scale so $this->image is small enough. 33 $image_sizes = image_get_sizes(); 34 $image_sizes['_original']['operation'] = 'scale'; 35 $image_sizes['thumbnail']['operation'] = 'scale'; 36 $image_sizes['thumbnail']['width'] = 5; 37 $image_sizes['thumbnail']['height'] = 5; 38 $image_sizes['preview']['operation'] = 'scale'; 39 $image_sizes['preview']['width'] = 10; 40 $image_sizes['preview']['height'] = 10; 41 variable_set('image_sizes', $image_sizes); 42 } 43 44 /** 45 * Clean-up temporary files. 46 * 47 * @see system_cron() 48 * 49 * @todo This lets SimpleTest die for any reason. 50 */ 51 function __tearDown() { 52 $result = db_query('SELECT * FROM {files} WHERE status = %d', FILE_STATUS_TEMPORARY); 53 while ($file = db_fetch_object($result)) { 54 if (file_exists($file->filepath)) { 55 // If files that exist cannot be deleted, continue so the database remains 56 // consistent. 57 if (!file_delete($file->filepath)) { 58 $this->error(t('Could not delete temporary file "%path" during garbage collection', array('%path' => $file->filepath))); 59 continue; 60 } 61 } 62 db_query('DELETE FROM {files} WHERE fid = %d', $file->fid); 63 } 64 } 65 66 /** 67 * Verify creating/displaying/editing/deleting image nodes. 68 */ 69 function testImageNode() { 70 // Create an image. 71 $edit = array( 72 'title' => $this->randomName(), 73 'body' => $this->randomName(), 74 'files[image]' => realpath($this->image), 75 ); 76 $this->drupalPost('node/add/image', $edit, t('Save')); 77 78 $this->assertRaw(t('@type %title has been created.', array('@type' => 'Image', '%title' => $edit['title'])), t('Image node was created.')); 79 80 $node = node_load(array('title' => $edit['title'])); 81 $this->assertTrue($node, t('Image node is found in database.')); 82 83 // Display an image. 84 $this->drupalGet('node/' . $node->nid, array('query' => 'size=_original')); 85 $this->assertPattern('@<img[^>]+?' . $node->images['_original'] . '[^>]+?>@', t('Original image displayed on the page.')); 86 $this->assertTrue(file_exists($node->images['_original']), t('Original image exists.')); 87 88 $this->drupalGet('node/' . $node->nid); 89 $this->assertPattern('@<img[^>]+?' . $node->images['preview'] . '[^>]+?>@', t('Image preview displayed on the page.')); 90 $this->assertTrue(file_exists($node->images['preview']), t('Image preview exists.')); 91 92 $this->drupalGet('node/' . $node->nid, array('query' => 'size=thumbnail')); 93 $this->assertPattern('@<img[^>]+?' . $node->images['thumbnail'] . '[^>]+?>@', t('Image thumbnail displayed on the page.')); 94 $this->assertTrue(file_exists($node->images['thumbnail']), t('Image thumbnail exists.')); 95 96 // Promote the node so we can test the thumbnail appears in the teaser. 97 $node->promote = TRUE; 98 node_save($node); 99 $this->drupalGet('node'); 100 $this->assertPattern('@<img[^>]+?' . $node->images['thumbnail'] . '[^>]+?>@', t('Image thumbnail displayed on the front page teaser.')); 101 102 // Edit an image. 103 $another_edit = array( 104 'title' => $edit['title'], 105 'files[image]' => realpath($this->another_image), 106 ); 107 $this->drupalPost('node/' . $node->nid . '/edit', $another_edit, t('Save')); 108 $another_node = node_load(array('title' => $edit['title'])); 109 $this->assertFalse(file_exists($node->images['preview']) || file_exists($node->images['_original']) || file_exists($node->images['thumbnail']), t('Previous image derivative files were deleted.')); 110 111 // Delete an image. 112 $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete')); 113 $this->assertRaw(t('@type %title has been deleted.', array('@type' => 'Image', '%title' => $edit['title'])), t('Image node was deleted.')); 114 $node = node_load(array('title' => $edit['title'])); 115 $this->assertFalse($node, t('Image node is not found in database.')); 116 $this->assertFalse(file_exists($another_node->images['preview']) || file_exists($another_node->images['_original']) || file_exists($another_node->images['thumbnail']), t('Image file was deleted.')); 117 } 118 119 /** 120 * Verify that images cannot be created without a file. 121 */ 122 function testImageNodeValidation() { 123 // Create an image node without image file. 124 $edit = array( 125 'title' => $this->randomName(), 126 'body' => $this->randomName(), 127 ); 128 $this->drupalPost('node/add/image', $edit, t('Save')); 129 $this->assertRaw(t('You must upload an image.'), t('Refused node creation without image.')); 130 131 $node = node_load(array('title' => $edit['title'])); 132 $this->assertFalse($node, t('Image node is not found in database.')); 133 } 134 135 /** 136 * Verify image previews and proper submission. 137 */ 138 function testImageNodePreview() { 139 // Setup POST data for image. 140 $edit = array( 141 'title' => $this->randomName(), 142 'body' => $this->randomName(), 143 'files[image]' => realpath($this->image), 144 ); 145 146 // Preview image node. 147 $this->drupalGet('node/add/image'); 148 $this->drupalPost(NULL, $edit, t('Preview')); 149 150 // Save image node preview as is. 151 $this->drupalPost(NULL, NULL, t('Save')); 152 153 // Verify image node was properly created. 154 $node = node_load(array('title' => $edit['title'])); 155 $this->assertTrue($node, t('Image node is found in database.')); 156 $this->assertTrue(file_exists($node->images['_original']), t('Original image exists.')); 157 $filename = preg_replace('/_[^\.]+/', '', basename($node->images['_original'])); 158 $this->assertTrue($filename == basename($this->image), t('Image file was properly stored.')); 159 } 160 161 /** 162 * Verify that images can be created in parallel (session handling). 163 * 164 * @todo Doesn't look valid. Can SimpleTest support this at all? 165 * @todo testImageCreateNodeFrom() fails is this test is run. 166 */ 167 function __testImageNodeValidationAsync() { 168 // Setup POST data for first image. 169 $edit1 = array( 170 'title' => $this->randomName(), 171 'body' => $this->randomName(), 172 'files[image]' => realpath($this->image), 173 ); 174 // Setup POST data for second image. 175 $edit2 = array( 176 'title' => $this->randomName(), 177 'body' => $this->randomName(), 178 'files[image]' => realpath($this->another_image), 179 ); 180 181 // Preview the first image node. 182 $this->drupalGet('node/add/image'); 183 $this->drupalPost(NULL, $edit1, t('Preview')); 184 185 // Preview the second image node. 186 $this->drupalGet('node/add/image'); 187 $this->drupalPost(NULL, $edit2, t('Preview')); 188 189 // Save the first image node. 190 $this->drupalPost(NULL, $edit1, t('Save')); 191 // Save the second image node. 192 $this->drupalPost('node/add/image', $edit2, t('Save')); 193 194 // Verify first image node contains the first image. 195 $node = node_load(array('title' => $edit1['title'])); 196 $filename = preg_replace('/_[^\.]+/', '', basename($node->images['_original'])); 197 $this->assertTrue($filename == basename($this->image), t('First image was properly submitted.')); 198 199 // Verify second image node contains the second image. 200 $node = node_load(array('title' => $edit2['title'])); 201 $filename = preg_replace('/_[^\.]+/', '', basename($node->images['_original'])); 202 $this->assertTrue($filename == basename($this->another_image), t('Second image was properly submitted.')); 203 } 204 205 /** 206 * Verify derivatives take their path from the original image. 207 */ 208 function testImageDerivativePath() { 209 // Create an image. 210 $edit = array( 211 'title' => $this->randomName(), 212 'body' => $this->randomName(), 213 'files[image]' => realpath($this->image), 214 ); 215 $this->drupalPost('node/add/image', $edit, t('Save')); 216 217 $this->assertRaw(t('@type %title has been created.', array('@type' => 'Image', '%title' => $edit['title'])), 218 t('Image node was created.')); 219 220 $node = node_load(array('title' => $edit['title'])); 221 $this->assertTrue($node, t('Image node is found in database.')); 222 223 // Change the file path using admin form. 224 $config_change = array( 225 'image_default_path' => 'images_alternate', 226 ); 227 $this->drupalPost('admin/settings/image', $config_change, t('Save configuration')); 228 $this->assertRaw(t('The configuration options have been saved.'), t('Image path was changed.')); 229 230 // Create another image. 231 $second_edit = array( 232 'title' => $this->randomName(), 233 'body' => $this->randomName(), 234 'files[image]' => realpath($this->another_image), 235 ); 236 $this->drupalPost('node/add/image', $second_edit, t('Save')); 237 238 // Test that image node was created and that it has the correct path. 239 $this->assertRaw(t('@type %title has been created.', array('@type' => 'Image', '%title' => $second_edit['title'])), 240 t('Second image node was created.')); 241 $second_node = node_load(array('title' => $second_edit['title'])); 242 $this->assertTrue($second_node, t('Second image node is found in database.')); 243 244 $this->assertTrue(preg_match('/images_alternate/', $second_node->images['_original']), 245 t("New path {$second_node->images['_original']} used for second image.")); 246 247 // Delete physical files for first image's derivatives. 248 file_delete($node->images['preview']); 249 $this->assertFalse(file_exists($node->images['preview']), t('First image preview image file deleted.')); 250 file_delete($node->images['thumbnail']); 251 $this->assertFalse(file_exists($node->images['thumbnail']), t('First image thumbnail image file deleted.')); 252 253 // Edit the first image to rebuild derivatives. 254 $another_edit = array( 255 'rebuild_images' => TRUE, 256 ); 257 $this->drupalPost('node/' . $node->nid . '/edit', $another_edit, t('Save')); 258 $this->assertRaw(t('@type %title has been updated.', array('@type' => 'Image', '%title' => $edit['title'])), 259 t('First image node was updated to rebuild derivatives.')); 260 261 // Reload first image node. 262 $first_node = node_load(array('title' => $edit['title'])); 263 264 // Compare regenerated thumbnail paths with original node. 265 $this->assertTrue($node->images['thumbnail'] == $first_node->images['thumbnail'], t("Rebuilt derivatives for first image have same thumbnail path as original node: {$node->images['thumbnail']} == regenerated thumbnail path {$first_node->images['thumbnail']}")); 266 267 // Check regenerated files physically exist. 268 $this->assertTrue(file_exists($first_node->images['preview']), t('First image preview file exists.')); 269 $this->assertTrue(file_exists($first_node->images['thumbnail']), t('First image thumbnail file exists.')); 270 271 // Change the file path back using admin form. 272 $config_change = array( 273 'image_default_path' => 'images', 274 ); 275 $this->drupalPost('admin/settings/image', $config_change, t('Save configuration')); 276 $this->assertRaw(t('The configuration options have been saved.'), t('Image path was changed back.')); 277 } 278 279 /** 280 * Verify that images with missing file information can be deleted from edit form. 281 */ 282 function testImageNodeDeletion() { 283 // Create an image. 284 $edit = array( 285 'title' => $this->randomName(), 286 'body' => $this->randomName(), 287 'files[image]' => realpath($this->image), 288 ); 289 290 // Create image. 291 $this->drupalPost('node/add/image', $edit, t('Save')); 292 $this->assertRaw(t('@type %title has been created.', array('@type' => 'Image', '%title' => $edit['title'])), t('Image node was created.')); 293 $node = node_load(array('title' => $edit['title'])); 294 $this->assertTrue($node, t('Image node is found in database.')); 295 296 // Remove {files} row to create corruption condition. 297 $this->assertTrue(db_query("DELETE f FROM {files} f INNER JOIN {image} i WHERE f.fid = i.fid AND i.nid = %d", $node->nid), t('Image file information was deleted.')); 298 $node = node_load(array('title' => $edit['title'])); 299 $this->assertFalse(isset($node->images['_original']), t('Image file information is missing on re-loaded node.')); 300 301 $this->drupalPost('node/' . $node->nid . '/edit', array(), t('Delete')); 302 $this->assertRaw(t('Are you sure you want to delete %title?', array('%title' => $edit['title'])), t('Delete confirmation form is displayed.')); 303 $this->drupalPost(NULL, array(), t('Delete')); 304 $this->assertRaw(t('@type %title has been deleted.', array('@type' => 'Image', '%title' => $edit['title'])), t('Image node was deleted.')); 305 } 306 307 /** 308 * Verify image_create_node_from() works like regular image node creation. 309 */ 310 function testImageCreateNodeFrom() { 311 $edit = array( 312 'title' => $this->randomName(), 313 'body' => $this->randomName(), 314 'files[image]' => realpath($this->image), 315 ); 316 $this->drupalPost('node/add/image', $edit, t('Save')); 317 $this->assertRaw(t('@type %title has been created.', array('@type' => 'Image', '%title' => $edit['title'])), t('Image created.')); 318 319 $node_post = node_load(array('title' => $edit['title'])); 320 $this->assertTrue($node_post, t('Form created image node found in database.')); 321 322 // Copy the image, since image_create_node_from() deletes the original image. 323 file_copy($edit['files[image]'], file_directory_temp()); 324 $node_api = image_create_node_from(file_directory_temp() . '/' . basename($edit['files[image]']), $edit['title'], $edit['body']); 325 $this->assertTrue(is_object($node_api) && $node_api->nid, t('API created image node found in database.')); 326 // Rebuild derivative images. 327 $node_api = node_load($node_api->nid, NULL, TRUE); 328 329 // Verify that both nodes are equal. 330 $equality = ($node_post->title == $node_api->title); 331 $this->assertTrue($equality, t('Image node titles are equal.')); 332 $equality = $equality && (strip_tags($node_post->body) == strip_tags($node_api->body)); 333 $this->assertTrue($equality, t('Image node body texts are equal.')); 334 $equality = $equality && (filesize($node_post->images['_original']) == filesize($node_api->images['_original'])); 335 $this->assertTrue($equality, t('Original images are equal.')); 336 /* 337 // These tests are temporarily commented out as they fail due to 338 // image_create_node_from() not creative derivatives. 339 // @see <http://drupal.org/node/670686> for details. 340 $equality = $equality && (filesize($node_post->images['preview']) == filesize($node_api->images['preview'])); 341 $this->assertTrue($equality, t('Preview images are equal.')); 342 $equality = $equality && (filesize($node_post->images['thumbnail']) == filesize($node_api->images['thumbnail'])); 343 $this->assertTrue($equality, t('Preview images are equal.')); 344 */ 345 } 346 } 347
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |