MediaEdit.php 2.6 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. * List of media thumbnail ids
  17. * @var array
  18. */
  19. protected $_ids = array();
  20. /**
  21. * Set up the test fixture.
  22. */
  23. public function setUp() {
  24. parent::setUp();
  25. }
  26. /**
  27. * Tear down the test fixture.
  28. */
  29. public function tearDown() {
  30. // Cleanup
  31. foreach ($this->_ids as $id){
  32. wp_delete_attachment($id, true);
  33. }
  34. $uploads = wp_upload_dir();
  35. foreach ( scandir( $uploads['basedir'] ) as $file )
  36. _rmdir( $uploads['basedir'] . '/' . $file );
  37. parent::tearDown();
  38. }
  39. /**
  40. * Function snagged from ./tests/post/attachments.php
  41. */
  42. function _make_attachment($upload, $parent_post_id = 0) {
  43. $type = '';
  44. if ( !empty($upload['type']) ) {
  45. $type = $upload['type'];
  46. } else {
  47. $mime = wp_check_filetype( $upload['file'] );
  48. if ($mime)
  49. $type = $mime['type'];
  50. }
  51. $attachment = array(
  52. 'post_title' => basename( $upload['file'] ),
  53. 'post_content' => '',
  54. 'post_type' => 'attachment',
  55. 'post_parent' => $parent_post_id,
  56. 'post_mime_type' => $type,
  57. 'guid' => $upload[ 'url' ],
  58. );
  59. // Save the data
  60. $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
  61. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
  62. return $this->_ids[] = $id;
  63. }
  64. /**
  65. * @ticket 22985
  66. */
  67. public function testCropImageThumbnail() {
  68. include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
  69. $filename = DIR_TESTDATA . '/images/canola.jpg';
  70. $contents = file_get_contents($filename);
  71. $upload = wp_upload_bits(basename($filename), null, $contents);
  72. $id = $this->_make_attachment($upload);
  73. $_REQUEST['action'] = 'image-editor';
  74. $_REQUEST['context'] = 'edit-attachment';
  75. $_REQUEST['postid'] = $id;
  76. $_REQUEST['target'] = 'thumbnail';
  77. $_REQUEST['do'] = 'save';
  78. $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
  79. $media_meta = wp_get_attachment_metadata($id);
  80. $this->assertArrayHasKey('sizes', $media_meta, 'attachment should have size data');
  81. $this->assertArrayHasKey('medium', $media_meta['sizes'], 'attachment should have data for medium size');
  82. $ret = wp_save_image($id);
  83. $media_meta = wp_get_attachment_metadata($id);
  84. $this->assertArrayHasKey('sizes', $media_meta, 'cropped attachment should have size data');
  85. $this->assertArrayHasKey('medium', $media_meta['sizes'], 'cropped attachment should have data for medium size');
  86. }
  87. }