| | 1 | <?php |
| | 2 | /** |
| | 3 | * Admin ajax functions to be tested |
| | 4 | */ |
| | 5 | require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); |
| | 6 | |
| | 7 | /** |
| | 8 | * Testing ajax media editing |
| | 9 | * |
| | 10 | * @package WordPress |
| | 11 | * @subpackage UnitTests |
| | 12 | * @since 3.5.0 |
| | 13 | * @group ajax |
| | 14 | */ |
| | 15 | class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase { |
| | 16 | |
| | 17 | /** |
| | 18 | * List of media thumbnail ids |
| | 19 | * @var array |
| | 20 | */ |
| | 21 | protected $_ids = array(); |
| | 22 | |
| | 23 | /** |
| | 24 | * Set up the test fixture. |
| | 25 | */ |
| | 26 | public function setUp() { |
| | 27 | parent::setUp(); |
| | 28 | } |
| | 29 | |
| | 30 | /** |
| | 31 | * Tear down the test fixture. |
| | 32 | */ |
| | 33 | public function tearDown() { |
| | 34 | // Cleanup |
| | 35 | foreach ($this->_ids as $id){ |
| | 36 | wp_delete_attachment($id, true); |
| | 37 | } |
| | 38 | |
| | 39 | $uploads = wp_upload_dir(); |
| | 40 | foreach ( scandir( $uploads['basedir'] ) as $file ) |
| | 41 | _rmdir( $uploads['basedir'] . '/' . $file ); |
| | 42 | |
| | 43 | parent::tearDown(); |
| | 44 | } |
| | 45 | |
| | 46 | /** |
| | 47 | * Function snagged from ./tests/post/attachments.php |
| | 48 | */ |
| | 49 | function _make_attachment($upload, $parent_post_id = -1) { |
| | 50 | $type = ''; |
| | 51 | if ( !empty($upload['type']) ) { |
| | 52 | $type = $upload['type']; |
| | 53 | } else { |
| | 54 | $mime = wp_check_filetype( $upload['file'] ); |
| | 55 | if ($mime) |
| | 56 | $type = $mime['type']; |
| | 57 | } |
| | 58 | |
| | 59 | $attachment = array( |
| | 60 | 'post_title' => basename( $upload['file'] ), |
| | 61 | 'post_content' => '', |
| | 62 | 'post_type' => 'attachment', |
| | 63 | 'post_parent' => $parent_post_id, |
| | 64 | 'post_mime_type' => $type, |
| | 65 | 'guid' => $upload[ 'url' ], |
| | 66 | ); |
| | 67 | |
| | 68 | // Save the data |
| | 69 | $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); |
| | 70 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
| | 71 | return $this->_ids[] = $id; |
| | 72 | } |
| | 73 | |
| | 74 | /** |
| | 75 | * @ticket 22985 |
| | 76 | */ |
| | 77 | public function testCropImageThumbnail() { |
| | 78 | include_once( ABSPATH . 'wp-admin/includes/image-edit.php' ); |
| | 79 | |
| | 80 | $filename = DIR_TESTDATA . '/images/canola.jpg'; |
| | 81 | $contents = file_get_contents($filename); |
| | 82 | |
| | 83 | $upload = wp_upload_bits(basename($filename), null, $contents); |
| | 84 | $id = $this->_make_attachment($upload); |
| | 85 | |
| | 86 | $_REQUEST['action'] = 'image-editor'; |
| | 87 | $_REQUEST['context'] = 'edit-attachment'; |
| | 88 | $_REQUEST['postid'] = $id; |
| | 89 | $_REQUEST['target'] = 'thumbnail'; |
| | 90 | $_REQUEST['do'] = 'save'; |
| | 91 | $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]'; |
| | 92 | |
| | 93 | $media_meta = wp_get_attachment_metadata($id); |
| | 94 | $this->assertArrayHasKey('sizes', $media_meta, 'attachment should have size data'); |
| | 95 | $this->assertArrayHasKey('medium', $media_meta['sizes'], 'attachment should have data for medium size'); |
| | 96 | $ret = wp_save_image($id); |
| | 97 | |
| | 98 | $media_meta = wp_get_attachment_metadata($id); |
| | 99 | $this->assertArrayHasKey('sizes', $media_meta, 'cropped attachment should have size data'); |
| | 100 | $this->assertArrayHasKey('medium', $media_meta['sizes'], 'cropped attachment should have data for medium size'); |
| | 101 | } |
| | 102 | } |