attachments.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @group post
  4. * @group media
  5. * @group upload
  6. */
  7. class Tests_Post_Attachments extends WP_UnitTestCase {
  8. function tearDown() {
  9. // Remove all uploads.
  10. $uploads = wp_upload_dir();
  11. foreach ( scandir( $uploads['basedir'] ) as $file )
  12. _rmdir( $uploads['basedir'] . '/' . $file );
  13. parent::tearDown();
  14. }
  15. function _make_attachment( $upload, $parent_post_id = 0 ) {
  16. $type = '';
  17. if ( !empty($upload['type']) ) {
  18. $type = $upload['type'];
  19. } else {
  20. $mime = wp_check_filetype( $upload['file'] );
  21. if ($mime)
  22. $type = $mime['type'];
  23. }
  24. $attachment = array(
  25. 'post_title' => basename( $upload['file'] ),
  26. 'post_content' => '',
  27. 'post_type' => 'attachment',
  28. 'post_parent' => $parent_post_id,
  29. 'post_mime_type' => $type,
  30. 'guid' => $upload[ 'url' ],
  31. );
  32. // Save the data
  33. $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
  34. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
  35. return $this->ids[] = $id;
  36. }
  37. function test_insert_bogus_image() {
  38. $filename = rand_str().'.jpg';
  39. $contents = rand_str();
  40. $upload = wp_upload_bits($filename, null, $contents);
  41. $this->assertTrue( empty($upload['error']) );
  42. $id = $this->_make_attachment($upload);
  43. }
  44. function test_insert_image_no_thumb() {
  45. // this image is smaller than the thumbnail size so it won't have one
  46. $filename = ( DIR_TESTDATA.'/images/test-image.jpg' );
  47. $contents = file_get_contents($filename);
  48. $upload = wp_upload_bits(basename($filename), null, $contents);
  49. $this->assertTrue( empty($upload['error']) );
  50. $id = $this->_make_attachment($upload);
  51. // intermediate copies should not exist
  52. $this->assertFalse( image_get_intermediate_size($id, 'thumbnail') );
  53. $this->assertFalse( image_get_intermediate_size($id, 'medium') );
  54. // medium and full size will both point to the original
  55. $downsize = image_downsize($id, 'medium');
  56. $this->assertEquals( 'test-image.jpg', basename($downsize[0]) );
  57. $this->assertEquals( 50, $downsize[1] );
  58. $this->assertEquals( 50, $downsize[2] );
  59. $downsize = image_downsize($id, 'full');
  60. $this->assertEquals( 'test-image.jpg', basename($downsize[0]) );
  61. $this->assertEquals( 50, $downsize[1] );
  62. $this->assertEquals( 50, $downsize[2] );
  63. }
  64. function test_insert_image_thumb_only() {
  65. if ( !function_exists( 'imagejpeg' ) )
  66. $this->markTestSkipped( 'jpeg support unavailable' );
  67. update_option( 'medium_size_w', 0 );
  68. update_option( 'medium_size_h', 0 );
  69. $filename = ( DIR_TESTDATA.'/images/a2-small.jpg' );
  70. $contents = file_get_contents($filename);
  71. $upload = wp_upload_bits(basename($filename), null, $contents);
  72. $this->assertTrue( empty($upload['error']) );
  73. $id = $this->_make_attachment($upload);
  74. // intermediate copies should exist: thumbnail only
  75. $thumb = image_get_intermediate_size($id, 'thumbnail');
  76. $this->assertEquals( 'a2-small-150x150.jpg', $thumb['file'] );
  77. $uploads = wp_upload_dir();
  78. $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']) );
  79. $this->assertFalse( image_get_intermediate_size($id, 'medium') );
  80. // the thumb url should point to the thumbnail intermediate
  81. $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url($id) );
  82. // image_downsize() should return the correct images and sizes
  83. $downsize = image_downsize($id, 'thumbnail');
  84. $this->assertEquals( 'a2-small-150x150.jpg', basename($downsize[0]) );
  85. $this->assertEquals( 150, $downsize[1] );
  86. $this->assertEquals( 150, $downsize[2] );
  87. // medium and full will both point to the original
  88. $downsize = image_downsize($id, 'medium');
  89. $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
  90. $this->assertEquals( 400, $downsize[1] );
  91. $this->assertEquals( 300, $downsize[2] );
  92. $downsize = image_downsize($id, 'full');
  93. $this->assertEquals( 'a2-small.jpg', basename($downsize[0]) );
  94. $this->assertEquals( 400, $downsize[1] );
  95. $this->assertEquals( 300, $downsize[2] );
  96. }
  97. function test_insert_image_medium() {
  98. if ( !function_exists( 'imagejpeg' ) )
  99. $this->markTestSkipped( 'jpeg support unavailable' );
  100. update_option('medium_size_w', 400);
  101. update_option('medium_size_h', 0);
  102. $filename = ( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG' );
  103. $contents = file_get_contents($filename);
  104. $upload = wp_upload_bits(basename($filename), null, $contents);
  105. $this->assertTrue( empty($upload['error']) );
  106. $id = $this->_make_attachment($upload);
  107. $uploads = wp_upload_dir();
  108. // intermediate copies should exist: thumbnail and medium
  109. $thumb = image_get_intermediate_size($id, 'thumbnail');
  110. $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', $thumb['file'] );
  111. $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']) );
  112. $medium = image_get_intermediate_size($id, 'medium');
  113. $this->assertEquals( '2007-06-17DSC_4173-400x602.jpg', $medium['file'] );
  114. $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path']) );
  115. // the thumb url should point to the thumbnail intermediate
  116. $this->assertEquals( $thumb['url'], wp_get_attachment_thumb_url($id) );
  117. // image_downsize() should return the correct images and sizes
  118. $downsize = image_downsize($id, 'thumbnail');
  119. $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', basename($downsize[0]) );
  120. $this->assertEquals( 150, $downsize[1] );
  121. $this->assertEquals( 150, $downsize[2] );
  122. $downsize = image_downsize($id, 'medium');
  123. $this->assertEquals( '2007-06-17DSC_4173-400x602.jpg', basename($downsize[0]) );
  124. $this->assertEquals( 400, $downsize[1] );
  125. $this->assertEquals( 602, $downsize[2] );
  126. $downsize = image_downsize($id, 'full');
  127. $this->assertEquals( '2007-06-17DSC_4173.jpg', basename($downsize[0]) );
  128. $this->assertEquals( 680, $downsize[1] );
  129. $this->assertEquals( 1024, $downsize[2] );
  130. }
  131. function test_insert_image_delete() {
  132. if ( !function_exists( 'imagejpeg' ) )
  133. $this->markTestSkipped( 'jpeg support unavailable' );
  134. update_option('medium_size_w', 400);
  135. update_option('medium_size_h', 0);
  136. $filename = ( DIR_TESTDATA.'/images/2007-06-17DSC_4173.JPG' );
  137. $contents = file_get_contents($filename);
  138. $upload = wp_upload_bits(basename($filename), null, $contents);
  139. $this->assertTrue( empty($upload['error']) );
  140. $id = $this->_make_attachment($upload);
  141. $uploads = wp_upload_dir();
  142. // check that the file and intermediates exist
  143. $thumb = image_get_intermediate_size($id, 'thumbnail');
  144. $this->assertEquals( '2007-06-17DSC_4173-150x150.jpg', $thumb['file'] );
  145. $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path']) );
  146. $medium = image_get_intermediate_size($id, 'medium');
  147. $this->assertEquals( '2007-06-17DSC_4173-400x602.jpg', $medium['file'] );
  148. $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path']) );
  149. $meta = wp_get_attachment_metadata($id);
  150. $original = $meta['file'];
  151. $this->assertTrue( is_file($uploads['basedir'] . DIRECTORY_SEPARATOR . $original) );
  152. // now delete the attachment and make sure all files are gone
  153. wp_delete_attachment($id);
  154. $this->assertFalse( is_file($thumb['path']) );
  155. $this->assertFalse( is_file($medium['path']) );
  156. $this->assertFalse( is_file($original) );
  157. }
  158. /**
  159. * GUID should never be empty
  160. * @ticket 18310
  161. * @ticket 21963
  162. */
  163. function test_insert_image_without_guid() {
  164. // this image is smaller than the thumbnail size so it won't have one
  165. $filename = ( DIR_TESTDATA.'/images/test-image.jpg' );
  166. $contents = file_get_contents($filename);
  167. $upload = wp_upload_bits(basename($filename), null, $contents);
  168. $this->assertTrue( empty($upload['error']) );
  169. $upload['url'] = '';
  170. $id = $this->_make_attachment( $upload );
  171. $guid = get_the_guid( $id );
  172. $this->assertFalse( empty( $guid ) );
  173. }
  174. }