MediaEdit.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Admin Ajax functions to be tested.
  4. */
  5. require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
  6. /**
  7. * Testing Ajax media editing.
  8. *
  9. * @package WordPress
  10. * @subpackage UnitTests
  11. * @since 3.5.0
  12. * @group ajax
  13. */
  14. class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase {
  15. /**
  16. * Tear down the test fixture.
  17. */
  18. public function tearDown() {
  19. // Cleanup.
  20. $this->remove_added_uploads();
  21. parent::tearDown();
  22. }
  23. /**
  24. * @ticket 22985
  25. * @requires function imagejpeg
  26. */
  27. public function testCropImageThumbnail() {
  28. require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  29. $filename = DIR_TESTDATA . '/images/canola.jpg';
  30. $contents = file_get_contents( $filename );
  31. $upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
  32. $id = $this->_make_attachment( $upload );
  33. $_REQUEST['action'] = 'image-editor';
  34. $_REQUEST['context'] = 'edit-attachment';
  35. $_REQUEST['postid'] = $id;
  36. $_REQUEST['target'] = 'thumbnail';
  37. $_REQUEST['do'] = 'save';
  38. $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
  39. $media_meta = wp_get_attachment_metadata( $id );
  40. $this->assertArrayHasKey( 'sizes', $media_meta, 'attachment should have size data' );
  41. $this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'attachment should have data for medium size' );
  42. $ret = wp_save_image( $id );
  43. $media_meta = wp_get_attachment_metadata( $id );
  44. $this->assertArrayHasKey( 'sizes', $media_meta, 'cropped attachment should have size data' );
  45. $this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'cropped attachment should have data for medium size' );
  46. }
  47. /**
  48. * @ticket 32171
  49. * @requires function imagejpeg
  50. */
  51. public function testImageEditOverwriteConstant() {
  52. define( 'IMAGE_EDIT_OVERWRITE', true );
  53. require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  54. $filename = DIR_TESTDATA . '/images/canola.jpg';
  55. $contents = file_get_contents( $filename );
  56. $upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
  57. $id = $this->_make_attachment( $upload );
  58. $_REQUEST['action'] = 'image-editor';
  59. $_REQUEST['context'] = 'edit-attachment';
  60. $_REQUEST['postid'] = $id;
  61. $_REQUEST['target'] = 'all';
  62. $_REQUEST['do'] = 'save';
  63. $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
  64. $ret = wp_save_image( $id );
  65. $media_meta = wp_get_attachment_metadata( $id );
  66. $sizes1 = $media_meta['sizes'];
  67. $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":189,"h":322}}]';
  68. $ret = wp_save_image( $id );
  69. $media_meta = wp_get_attachment_metadata( $id );
  70. $sizes2 = $media_meta['sizes'];
  71. $file_path = dirname( get_attached_file( $id ) );
  72. foreach ( $sizes1 as $key => $size ) {
  73. if ( $sizes2[ $key ]['file'] !== $size['file'] ) {
  74. $files_that_shouldnt_exist[] = $file_path . '/' . $size['file'];
  75. }
  76. }
  77. foreach ( $files_that_shouldnt_exist as $file ) {
  78. $this->assertFileNotExists( $file, 'IMAGE_EDIT_OVERWRITE is leaving garbage image files behind.' );
  79. }
  80. }
  81. }