wordpress-develop/tests/phpunit/tests/ajax/MediaEdit.php
Sergey Biryukov e72fff9cef Code Modernization: Replace dirname( __FILE__ ) calls with __DIR__ magic constant.
This avoids the performance overhead of the function call every time `dirname( __FILE__ )` was used instead of `__DIR__`.

This commit also includes:

* Removing unnecessary parentheses from `include`/`require` statements. These are language constructs, not function calls.
* Replacing `include` statements for several files with `require_once`, for consistency:
 * `wp-admin/admin-header.php`
 * `wp-admin/admin-footer.php`
 * `wp-includes/version.php`

Props ayeshrajans, desrosj, valentinbora, jrf, joostdevalk, netweb.
Fixes #48082.

git-svn-id: https://develop.svn.wordpress.org/trunk@47198 602fd350-edb4-49c9-b593-d223f7449a82
2020-02-06 06:31:22 +00:00

101 lines
2.9 KiB
PHP

<?php
/**
* Admin Ajax functions to be tested.
*/
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
/**
* Testing Ajax media editing.
*
* @package WordPress
* @subpackage UnitTests
* @since 3.5.0
* @group ajax
*/
class Tests_Ajax_MediaEdit extends WP_Ajax_UnitTestCase {
/**
* Tear down the test fixture.
*/
public function tearDown() {
// Cleanup.
$this->remove_added_uploads();
parent::tearDown();
}
/**
* @ticket 22985
*/
public function testCropImageThumbnail() {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'thumbnail';
$_REQUEST['do'] = 'save';
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
$media_meta = wp_get_attachment_metadata( $id );
$this->assertArrayHasKey( 'sizes', $media_meta, 'attachment should have size data' );
$this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'attachment should have data for medium size' );
$ret = wp_save_image( $id );
$media_meta = wp_get_attachment_metadata( $id );
$this->assertArrayHasKey( 'sizes', $media_meta, 'cropped attachment should have size data' );
$this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'cropped attachment should have data for medium size' );
}
/**
* @ticket 32171
*/
public function testImageEditOverwriteConstant() {
define( 'IMAGE_EDIT_OVERWRITE', true );
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'all';
$_REQUEST['do'] = 'save';
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
$ret = wp_save_image( $id );
$media_meta = wp_get_attachment_metadata( $id );
$sizes1 = $media_meta['sizes'];
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":189,"h":322}}]';
$ret = wp_save_image( $id );
$media_meta = wp_get_attachment_metadata( $id );
$sizes2 = $media_meta['sizes'];
$file_path = dirname( get_attached_file( $id ) );
foreach ( $sizes1 as $key => $size ) {
if ( $sizes2[ $key ]['file'] !== $size['file'] ) {
$files_that_shouldnt_exist[] = $file_path . '/' . $size['file'];
}
}
foreach ( $files_that_shouldnt_exist as $file ) {
$this->assertFileNotExists( $file, 'IMAGE_EDIT_OVERWRITE is leaving garbage image files behind.' );
}
}
}