Ticket #41658: 41658.2.diff
| File 41658.2.diff, 77.5 KB (added by , 9 years ago) |
|---|
-
tests/phpunit/tests/filesystem/find_folder.php
1 <?php2 3 require_once dirname( __FILE__ ) . '/base.php';4 5 /**6 * @group filesystem7 * @group wp-filesystem8 */9 class WP_Filesystem_find_folder_UnitTestCases extends WP_Filesystem_UnitTestCase {10 11 function test_ftp_has_root_access() {12 global $wp_filesystem;13 $fs = $wp_filesystem;14 $fs->init('15 /var/www/wordpress/16 /var/www/wordpress/wp-includes/17 /var/www/wordpress/index.php18 ');19 20 $path = $fs->find_folder( '/var/www/wordpress/' );21 $this->assertEquals( '/var/www/wordpress/', $path );22 23 $path = $fs->find_folder( '/this/directory/doesnt/exist/' );24 $this->assertFalse( $path );25 26 }27 28 function test_sibling_wordpress_in_subdir() {29 global $wp_filesystem;30 $fs = $wp_filesystem;31 $fs->init('32 /www/example.com/wordpress/33 /www/example.com/wordpress/wp-includes/34 /www/example.com/wordpress/index.php35 /www/wp.example.com/wordpress/36 /www/wp.example.com/wordpress/wp-includes/37 /www/wp.example.com/wordpress/wp-content/38 /www/wp.example.com/wordpress/index.php39 /www/index.php40 ');41 42 $path = $fs->find_folder( '/var/www/example.com/wordpress/' );43 $this->assertEquals( '/www/example.com/wordpress/', $path );44 45 $path = $fs->find_folder( '/var/www/wp.example.com/wordpress/wp-content/' );46 $this->assertEquals( '/www/wp.example.com/wordpress/wp-content/', $path );47 48 }49 50 /**51 * Two WordPress installs, with one contained within the other52 * FTP / = /var/www/example.com/ on Disk53 * example.com at /54 * wp.example.com at /wp.example.com/wordpress/55 */56 function test_subdir_of_another() {57 global $wp_filesystem;58 $fs = $wp_filesystem;59 $fs->init('60 /wp.example.com/index.php61 /wp.example.com/wordpress/62 /wp.example.com/wordpress/wp-includes/63 /wp.example.com/wordpress/index.php64 /wp-includes/65 /index.php66 ');67 68 $path = $fs->abspath( '/var/www/example.com/wp.example.com/wordpress/' );69 $this->assertEquals( '/wp.example.com/wordpress/', $path );70 71 $path = $fs->abspath( '/var/www/example.com/' );72 $this->assertEquals( '/', $path );73 74 }75 76 /**77 * Test the WordPress ABSPATH containing TWO tokens (www) of which exists in the current FTP home.78 *79 * @ticket 2093480 */81 function test_multiple_tokens_in_path1() {82 global $wp_filesystem;83 $fs = $wp_filesystem;84 $fs->init('85 # www.example.com86 /example.com/www/index.php87 /example.com/www/wp-includes/88 /example.com/www/wp-content/plugins/89 90 # sub.example.com91 /example.com/sub/index.php92 /example.com/sub/wp-includes/93 /example.com/sub/wp-content/plugins/94 ');95 96 // www.example.com97 $path = $fs->abspath( '/var/www/example.com/www/' );98 $this->assertEquals( '/example.com/www/', $path );99 100 // sub.example.com101 $path = $fs->abspath( '/var/www/example.com/sub/' );102 $this->assertEquals( '/example.com/sub/', $path );103 104 // sub.example.com - Plugins105 $path = $fs->find_folder( '/var/www/example.com/sub/wp-content/plugins/' );106 $this->assertEquals( '/example.com/sub/wp-content/plugins/', $path );107 }108 109 }110 No newline at end of file -
tests/phpunit/tests/hooks/add_filter.php
1 <?php2 3 4 /**5 * Test the add_filter method of WP_Hook6 *7 * @group hooks8 */9 class Tests_WP_Hook_Add_Filter extends WP_UnitTestCase {10 11 public $hook;12 13 public function test_add_filter_with_function() {14 $callback = '__return_null';15 $hook = new WP_Hook();16 $tag = __FUNCTION__;17 $priority = rand( 1, 100 );18 $accepted_args = rand( 1, 100 );19 20 $hook->add_filter( $tag, $callback, $priority, $accepted_args );21 22 $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );23 $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );24 $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );25 }26 27 public function test_add_filter_with_object() {28 $a = new MockAction();29 $callback = array( $a, 'action' );30 $hook = new WP_Hook();31 $tag = __FUNCTION__;32 $priority = rand( 1, 100 );33 $accepted_args = rand( 1, 100 );34 35 $hook->add_filter( $tag, $callback, $priority, $accepted_args );36 37 $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );38 $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );39 $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );40 }41 42 public function test_add_filter_with_static_method() {43 $callback = array( 'MockAction', 'action' );44 $hook = new WP_Hook();45 $tag = __FUNCTION__;46 $priority = rand( 1, 100 );47 $accepted_args = rand( 1, 100 );48 49 $hook->add_filter( $tag, $callback, $priority, $accepted_args );50 51 $function_index = _wp_filter_build_unique_id( $tag, $callback, $priority );52 $this->assertEquals( $callback, $hook->callbacks[ $priority ][ $function_index ]['function'] );53 $this->assertEquals( $accepted_args, $hook->callbacks[ $priority ][ $function_index ]['accepted_args'] );54 }55 56 public function test_add_two_filters_with_same_priority() {57 $callback_one = '__return_null';58 $callback_two = '__return_false';59 $hook = new WP_Hook();60 $tag = __FUNCTION__;61 $priority = rand( 1, 100 );62 $accepted_args = rand( 1, 100 );63 64 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );65 $this->assertCount( 1, $hook->callbacks[ $priority ] );66 67 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );68 $this->assertCount( 2, $hook->callbacks[ $priority ] );69 }70 71 public function test_add_two_filters_with_different_priority() {72 $callback_one = '__return_null';73 $callback_two = '__return_false';74 $hook = new WP_Hook();75 $tag = __FUNCTION__;76 $priority = rand( 1, 100 );77 $accepted_args = rand( 1, 100 );78 79 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );80 $this->assertCount( 1, $hook->callbacks[ $priority ] );81 82 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );83 $this->assertCount( 1, $hook->callbacks[ $priority ] );84 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );85 }86 87 public function test_readd_filter() {88 $callback = '__return_null';89 $hook = new WP_Hook();90 $tag = __FUNCTION__;91 $priority = rand( 1, 100 );92 $accepted_args = rand( 1, 100 );93 94 $hook->add_filter( $tag, $callback, $priority, $accepted_args );95 $this->assertCount( 1, $hook->callbacks[ $priority ] );96 97 $hook->add_filter( $tag, $callback, $priority, $accepted_args );98 $this->assertCount( 1, $hook->callbacks[ $priority ] );99 }100 101 public function test_readd_filter_with_different_priority() {102 $callback = '__return_null';103 $hook = new WP_Hook();104 $tag = __FUNCTION__;105 $priority = rand( 1, 100 );106 $accepted_args = rand( 1, 100 );107 108 $hook->add_filter( $tag, $callback, $priority, $accepted_args );109 $this->assertCount( 1, $hook->callbacks[ $priority ] );110 111 $hook->add_filter( $tag, $callback, $priority + 1, $accepted_args );112 $this->assertCount( 1, $hook->callbacks[ $priority ] );113 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );114 }115 116 public function test_sort_after_add_filter() {117 $a = new MockAction();118 $b = new MockAction();119 $c = new MockAction();120 $hook = new WP_Hook();121 $tag = __FUNCTION__;122 123 $hook->add_filter( $tag, array( $a, 'action' ), 10, 1 );124 $hook->add_filter( $tag, array( $b, 'action' ), 5, 1 );125 $hook->add_filter( $tag, array( $c, 'action' ), 8, 1 );126 127 $this->assertEquals( array( 5, 8, 10 ), array_keys( $hook->callbacks ) );128 }129 130 public function test_remove_and_add() {131 $this->hook = new Wp_Hook();132 133 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );134 135 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );136 137 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );138 139 $value = $this->hook->apply_filters( '', array() );140 141 $this->assertSame( '24', $value );142 }143 144 public function test_remove_and_add_last_filter() {145 $this->hook = new Wp_Hook();146 147 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );148 149 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );150 151 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 12, 1 );152 153 $value = $this->hook->apply_filters( '', array() );154 155 $this->assertSame( '12', $value );156 }157 158 public function test_remove_and_recurse_and_add() {159 $this->hook = new Wp_Hook();160 161 $this->hook->add_filter( 'remove_and_add', '__return_empty_string', 10, 0 );162 163 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add1' ), 11, 1 );164 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );165 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add3' ), 11, 1 );166 167 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add4' ), 12, 1 );168 169 $value = $this->hook->apply_filters( '', array() );170 171 $this->assertSame( '1-134-234', $value );172 }173 174 public function _filter_remove_and_add1( $string ) {175 return $string . '1';176 }177 178 public function _filter_remove_and_add2( $string ) {179 $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11 );180 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_add2' ), 11, 1 );181 182 return $string . '2';183 }184 185 public function _filter_remove_and_recurse_and_add2( $string ) {186 $this->hook->remove_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11 );187 188 $string .= '-' . $this->hook->apply_filters( '', array() ) . '-';189 190 $this->hook->add_filter( 'remove_and_add', array( $this, '_filter_remove_and_recurse_and_add2' ), 11, 1 );191 192 return $string . '2';193 }194 195 public function _filter_remove_and_add3( $string ) {196 return $string . '3';197 }198 199 public function _filter_remove_and_add4( $string ) {200 return $string . '4';201 }202 203 public function test_remove_and_add_action() {204 $this->hook = new Wp_Hook();205 $this->action_output = '';206 207 $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );208 209 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11, 0 );210 211 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add4' ), 12, 0 );212 213 $this->hook->do_action( array() );214 215 $this->assertSame( '24', $this->action_output );216 }217 218 public function test_remove_and_add_last_action() {219 $this->hook = new Wp_Hook();220 $this->action_output = '';221 222 $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );223 224 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add1' ), 11, 0 );225 226 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 12, 0 );227 228 $this->hook->do_action( array() );229 230 $this->assertSame( '12', $this->action_output );231 }232 233 public function test_remove_and_recurse_and_add_action() {234 $this->hook = new Wp_Hook();235 $this->action_output = '';236 237 $this->hook->add_filter( 'remove_and_add_action', '__return_empty_string', 10, 0 );238 239 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add1' ), 11, 0 );240 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11, 0 );241 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add3' ), 11, 0 );242 243 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add4' ), 12, 0 );244 245 $this->hook->do_action( array() );246 247 $this->assertSame( '1-134-234', $this->action_output );248 }249 250 public function _action_remove_and_add1() {251 $this->action_output .= 1;252 }253 254 public function _action_remove_and_add2() {255 $this->hook->remove_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11 );256 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_add2' ), 11, 0 );257 258 $this->action_output .= '2';259 }260 261 public function _action_remove_and_recurse_and_add2() {262 $this->hook->remove_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11 );263 264 $this->action_output .= '-';265 $this->hook->do_action( array() );266 $this->action_output .= '-';267 268 $this->hook->add_filter( 'remove_and_add_action', array( $this, '_action_remove_and_recurse_and_add2' ), 11, 0 );269 270 $this->action_output .= '2';271 }272 273 public function _action_remove_and_add3() {274 $this->action_output .= '3';275 }276 277 public function _action_remove_and_add4() {278 $this->action_output .= '4';279 }280 } -
tests/phpunit/tests/hooks/apply_filters.php
Property changes on: tests/phpunit/tests/hooks/add_filter.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the apply_filters method of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Apply_Filters extends WP_UnitTestCase {9 10 public function test_apply_filters_with_callback() {11 $a = new MockAction();12 $callback = array( $a, 'filter' );13 $hook = new WP_Hook();14 $tag = __FUNCTION__;15 $priority = rand( 1, 100 );16 $accepted_args = rand( 1, 100 );17 $arg = __FUNCTION__ . '_arg';18 19 $hook->add_filter( $tag, $callback, $priority, $accepted_args );20 21 $returned = $hook->apply_filters( $arg, array( $arg ) );22 23 $this->assertEquals( $returned, $arg );24 $this->assertEquals( 1, $a->get_call_count() );25 }26 27 public function test_apply_filters_with_multiple_calls() {28 $a = new MockAction();29 $callback = array( $a, 'filter' );30 $hook = new WP_Hook();31 $tag = __FUNCTION__;32 $priority = rand( 1, 100 );33 $accepted_args = rand( 1, 100 );34 $arg = __FUNCTION__ . '_arg';35 36 $hook->add_filter( $tag, $callback, $priority, $accepted_args );37 38 $returned_one = $hook->apply_filters( $arg, array( $arg ) );39 $returned_two = $hook->apply_filters( $returned_one, array( $returned_one ) );40 41 $this->assertEquals( $returned_two, $arg );42 $this->assertEquals( 2, $a->get_call_count() );43 }44 45 } -
tests/phpunit/tests/hooks/do_action.php
Property changes on: tests/phpunit/tests/hooks/apply_filters.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the do_action method of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Do_Action extends WP_UnitTestCase {9 private $events = array();10 private $action_output = '';11 private $hook;12 13 public function setUp() {14 parent::setUp();15 $this->events = array();16 }17 18 public function test_do_action_with_callback() {19 $a = new MockAction();20 $callback = array( $a, 'action' );21 $hook = new WP_Hook();22 $tag = __FUNCTION__;23 $priority = rand( 1, 100 );24 $accepted_args = rand( 1, 100 );25 $arg = __FUNCTION__ . '_arg';26 27 $hook->add_filter( $tag, $callback, $priority, $accepted_args );28 $hook->do_action( array( $arg ) );29 30 $this->assertEquals( 1, $a->get_call_count() );31 }32 33 public function test_do_action_with_multiple_calls() {34 $a = new MockAction();35 $callback = array( $a, 'filter' );36 $hook = new WP_Hook();37 $tag = __FUNCTION__;38 $priority = rand( 1, 100 );39 $accepted_args = rand( 1, 100 );40 $arg = __FUNCTION__ . '_arg';41 42 $hook->add_filter( $tag, $callback, $priority, $accepted_args );43 $hook->do_action( array( $arg ) );44 $hook->do_action( array( $arg ) );45 46 $this->assertEquals( 2, $a->get_call_count() );47 }48 49 public function test_do_action_with_multiple_callbacks_on_same_priority() {50 $a = new MockAction();51 $b = new MockAction();52 $callback_one = array( $a, 'filter' );53 $callback_two = array( $b, 'filter' );54 $hook = new WP_Hook();55 $tag = __FUNCTION__;56 $priority = rand( 1, 100 );57 $accepted_args = rand( 1, 100 );58 $arg = __FUNCTION__ . '_arg';59 60 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );61 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );62 $hook->do_action( array( $arg ) );63 64 $this->assertEquals( 1, $a->get_call_count() );65 $this->assertEquals( 1, $a->get_call_count() );66 }67 68 public function test_do_action_with_multiple_callbacks_on_different_priorities() {69 $a = new MockAction();70 $b = new MockAction();71 $callback_one = array( $a, 'filter' );72 $callback_two = array( $b, 'filter' );73 $hook = new WP_Hook();74 $tag = __FUNCTION__;75 $priority = rand( 1, 100 );76 $accepted_args = rand( 1, 100 );77 $arg = __FUNCTION__ . '_arg';78 79 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );80 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );81 $hook->do_action( array( $arg ) );82 83 $this->assertEquals( 1, $a->get_call_count() );84 $this->assertEquals( 1, $a->get_call_count() );85 }86 87 public function test_do_action_with_no_accepted_args() {88 $callback = array( $this, '_action_callback' );89 $hook = new WP_Hook();90 $tag = __FUNCTION__;91 $priority = rand( 1, 100 );92 $accepted_args = 0;93 $arg = __FUNCTION__ . '_arg';94 95 $hook->add_filter( $tag, $callback, $priority, $accepted_args );96 $hook->do_action( array( $arg ) );97 98 $this->assertEmpty( $this->events[0]['args'] );99 }100 101 public function test_do_action_with_one_accepted_arg() {102 $callback = array( $this, '_action_callback' );103 $hook = new WP_Hook();104 $tag = __FUNCTION__;105 $priority = rand( 1, 100 );106 $accepted_args = 1;107 $arg = __FUNCTION__ . '_arg';108 109 $hook->add_filter( $tag, $callback, $priority, $accepted_args );110 $hook->do_action( array( $arg ) );111 112 $this->assertCount( 1, $this->events[0]['args'] );113 }114 115 public function test_do_action_with_more_accepted_args() {116 $callback = array( $this, '_action_callback' );117 $hook = new WP_Hook();118 $tag = __FUNCTION__;119 $priority = rand( 1, 100 );120 $accepted_args = 1000;121 $arg = __FUNCTION__ . '_arg';122 123 $hook->add_filter( $tag, $callback, $priority, $accepted_args );124 $hook->do_action( array( $arg ) );125 126 $this->assertCount( 1, $this->events[0]['args'] );127 }128 129 public function test_do_action_doesnt_change_value() {130 $this->hook = new WP_Hook();131 $this->action_output = '';132 133 $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value1' ), 10, 1 );134 $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );135 $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value3' ), 11, 1 );136 137 $this->hook->do_action( array( 'a' ) );138 139 $this->assertSame( 'a1-b1b3-a2a3', $this->action_output );140 }141 142 public function _filter_do_action_doesnt_change_value1( $value ) {143 $this->action_output .= $value . 1;144 return 'x1';145 }146 public function _filter_do_action_doesnt_change_value2( $value ) {147 $this->hook->remove_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10 );148 149 $this->action_output .= '-';150 $this->hook->do_action( array( 'b' ) );151 $this->action_output .= '-';152 153 $this->hook->add_filter( 'do_action_doesnt_change_value', array( $this, '_filter_do_action_doesnt_change_value2' ), 10, 1 );154 155 $this->action_output .= $value . 2;156 157 return 'x2';158 }159 160 public function _filter_do_action_doesnt_change_value3( $value ) {161 $this->action_output .= $value . 3;162 return 'x3';163 }164 165 /**166 * Use this rather than MockAction so we can test callbacks with no args167 */168 public function _action_callback() {169 $args = func_get_args();170 $this->events[] = array('action' => __FUNCTION__, 'args'=>$args);171 }172 } -
tests/phpunit/tests/hooks/do_all_hook.php
Property changes on: tests/phpunit/tests/hooks/do_action.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the do_all_hook method of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Do_All_Hook extends WP_UnitTestCase {9 10 public function test_do_all_hook_with_multiple_calls() {11 $a = new MockAction();12 $callback = array( $a, 'action' );13 $hook = new WP_Hook();14 $tag = 'all';15 $priority = rand( 1, 100 );16 $accepted_args = rand( 1, 100 );17 $arg = 'all_arg';18 19 $hook->add_filter( $tag, $callback, $priority, $accepted_args );20 $args = array( $arg );21 $hook->do_all_hook( $args );22 $hook->do_all_hook( $args );23 24 $this->assertEquals( 2, $a->get_call_count() );25 }26 } -
tests/phpunit/tests/hooks/has_filter.php
Property changes on: tests/phpunit/tests/hooks/do_all_hook.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the has_filter method of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Has_Filter extends WP_UnitTestCase {9 10 public function test_has_filter_with_function() {11 $callback = '__return_null';12 $hook = new WP_Hook();13 $tag = __FUNCTION__;14 $priority = rand( 1, 100 );15 $accepted_args = rand( 1, 100 );16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args );18 19 $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) );20 }21 22 public function test_has_filter_with_object() {23 $a = new MockAction();24 $callback = array( $a, 'action' );25 $hook = new WP_Hook();26 $tag = __FUNCTION__;27 $priority = rand( 1, 100 );28 $accepted_args = rand( 1, 100 );29 30 $hook->add_filter( $tag, $callback, $priority, $accepted_args );31 32 $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) );33 }34 35 public function test_has_filter_with_static_method() {36 $callback = array( 'MockAction', 'action' );37 $hook = new WP_Hook();38 $tag = __FUNCTION__;39 $priority = rand( 1, 100 );40 $accepted_args = rand( 1, 100 );41 42 $hook->add_filter( $tag, $callback, $priority, $accepted_args );43 44 $this->assertEquals( $priority, $hook->has_filter( $tag, $callback ) );45 }46 47 public function test_has_filter_without_callback() {48 $callback = '__return_null';49 $hook = new WP_Hook();50 $tag = __FUNCTION__;51 $priority = rand( 1, 100 );52 $accepted_args = rand( 1, 100 );53 54 $hook->add_filter( $tag, $callback, $priority, $accepted_args );55 56 $this->assertTrue( $hook->has_filter() );57 }58 59 public function test_not_has_filter_without_callback() {60 $hook = new WP_Hook();61 $this->assertFalse( $hook->has_filter() );62 }63 64 public function test_not_has_filter_with_callback() {65 $callback = '__return_null';66 $hook = new WP_Hook();67 $tag = __FUNCTION__;68 69 $this->assertFalse( $hook->has_filter( $tag, $callback ) );70 }71 72 public function test_has_filter_with_wrong_callback() {73 $callback = '__return_null';74 $hook = new WP_Hook();75 $tag = __FUNCTION__;76 $priority = rand( 1, 100 );77 $accepted_args = rand( 1, 100 );78 79 $hook->add_filter( $tag, $callback, $priority, $accepted_args );80 81 $this->assertFalse( $hook->has_filter( $tag, '__return_false' ) );82 }83 } -
tests/phpunit/tests/hooks/has_filters.php
Property changes on: tests/phpunit/tests/hooks/has_filter.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the has_filters method of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Has_Filters extends WP_UnitTestCase {9 10 public function test_has_filters_with_callback() {11 $callback = '__return_null';12 $hook = new WP_Hook();13 $tag = __FUNCTION__;14 $priority = rand( 1, 100 );15 $accepted_args = rand( 1, 100 );16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args );18 19 $this->assertTrue( $hook->has_filters() );20 }21 22 public function test_has_filters_without_callback() {23 $hook = new WP_Hook();24 $this->assertFalse( $hook->has_filters() );25 }26 27 public function test_not_has_filters_with_removed_callback() {28 $callback = '__return_null';29 $hook = new WP_Hook();30 $tag = __FUNCTION__;31 $priority = rand( 1, 100 );32 $accepted_args = rand( 1, 100 );33 34 $hook->add_filter( $tag, $callback, $priority, $accepted_args );35 $hook->remove_filter( $tag, $callback, $priority );36 $this->assertFalse( $hook->has_filters() );37 }38 39 public function test_not_has_filter_with_directly_removed_callback() {40 $callback = '__return_null';41 $hook = new WP_Hook();42 $tag = __FUNCTION__;43 $priority = rand( 1, 100 );44 $accepted_args = rand( 1, 100 );45 46 $hook->add_filter( $tag, $callback, $priority, $accepted_args );47 $function_key = _wp_filter_build_unique_id( $tag, $callback, $priority );48 unset( $hook->callbacks[ $priority ][ $function_key ] );49 50 $this->assertFalse( $hook->has_filters() );51 }52 } -
tests/phpunit/tests/hooks/preinit_hooks.php
Property changes on: tests/phpunit/tests/hooks/has_filters.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the IteratorAggregate implementation of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Preinit_Hooks extends WP_UnitTestCase {9 10 public function test_array_to_hooks() {11 $tag1 = __FUNCTION__ . '_1';12 $priority1 = rand( 1, 100 );13 $tag2 = __FUNCTION__ . '_2';14 $priority2 = rand( 1, 100 );15 $filters = array(16 $tag1 => array(17 $priority1 => array(18 'test1' => array(19 'function' => '__return_false',20 'accepted_args' => 2,21 ),22 ),23 ),24 $tag2 => array(25 $priority2 => array(26 'test1' => array(27 'function' => '__return_null',28 'accepted_args' => 1,29 ),30 ),31 ),32 );33 34 $hooks = WP_Hook::build_preinitialized_hooks( $filters );35 36 $this->assertEquals( $priority1, $hooks[ $tag1 ]->has_filter( $tag1, '__return_false' ) );37 $this->assertEquals( $priority2, $hooks[ $tag2 ]->has_filter( $tag2, '__return_null' ) );38 }39 } -
tests/phpunit/tests/hooks/remove_all_filters.php
Property changes on: tests/phpunit/tests/hooks/preinit_hooks.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the remove_all_filters method of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Remove_All_Filters extends WP_UnitTestCase {9 10 public function test_remove_all_filters() {11 $callback = '__return_null';12 $hook = new WP_Hook();13 $tag = __FUNCTION__;14 $priority = rand( 1, 100 );15 $accepted_args = rand( 1, 100 );16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args );18 19 $hook->remove_all_filters();20 21 $this->assertFalse( $hook->has_filters() );22 }23 24 public function test_remove_all_filters_with_priority() {25 $callback_one = '__return_null';26 $callback_two = '__return_false';27 $hook = new WP_Hook();28 $tag = __FUNCTION__;29 $priority = rand( 1, 100 );30 $accepted_args = rand( 1, 100 );31 32 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );33 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );34 35 $hook->remove_all_filters( $priority );36 37 $this->assertFalse( $hook->has_filter( $tag, $callback_one ) );38 $this->assertTrue( $hook->has_filters() );39 $this->assertEquals( $priority + 1, $hook->has_filter( $tag, $callback_two ) );40 }41 } -
tests/phpunit/tests/hooks/remove_filter.php
Property changes on: tests/phpunit/tests/hooks/remove_all_filters.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the remove_filter method of WP_Hook5 *6 * @group hooks7 */8 class Tests_WP_Hook_Remove_Filter extends WP_UnitTestCase {9 10 public function test_remove_filter_with_function() {11 $callback = '__return_null';12 $hook = new WP_Hook();13 $tag = __FUNCTION__;14 $priority = rand( 1, 100 );15 $accepted_args = rand( 1, 100 );16 17 $hook->add_filter( $tag, $callback, $priority, $accepted_args );18 $hook->remove_filter( $tag, $callback, $priority );19 20 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );21 }22 23 public function test_remove_filter_with_object() {24 $a = new MockAction();25 $callback = array( $a, 'action' );26 $hook = new WP_Hook();27 $tag = __FUNCTION__;28 $priority = rand( 1, 100 );29 $accepted_args = rand( 1, 100 );30 31 $hook->add_filter( $tag, $callback, $priority, $accepted_args );32 $hook->remove_filter( $tag, $callback, $priority );33 34 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );35 }36 37 public function test_remove_filter_with_static_method() {38 $callback = array( 'MockAction', 'action' );39 $hook = new WP_Hook();40 $tag = __FUNCTION__;41 $priority = rand( 1, 100 );42 $accepted_args = rand( 1, 100 );43 44 $hook->add_filter( $tag, $callback, $priority, $accepted_args );45 $hook->remove_filter( $tag, $callback, $priority );46 47 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );48 }49 50 public function test_remove_filters_with_another_at_same_priority() {51 $callback_one = '__return_null';52 $callback_two = '__return_false';53 $hook = new WP_Hook();54 $tag = __FUNCTION__;55 $priority = rand( 1, 100 );56 $accepted_args = rand( 1, 100 );57 58 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );59 $hook->add_filter( $tag, $callback_two, $priority, $accepted_args );60 61 $hook->remove_filter( $tag, $callback_one, $priority );62 63 $this->assertCount( 1, $hook->callbacks[ $priority ] );64 }65 66 public function test_remove_filter_with_another_at_different_priority() {67 $callback_one = '__return_null';68 $callback_two = '__return_false';69 $hook = new WP_Hook();70 $tag = __FUNCTION__;71 $priority = rand( 1, 100 );72 $accepted_args = rand( 1, 100 );73 74 $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );75 $hook->add_filter( $tag, $callback_two, $priority + 1, $accepted_args );76 77 $hook->remove_filter( $tag, $callback_one, $priority );78 $this->assertFalse( isset( $hook->callbacks[ $priority ] ) );79 $this->assertCount( 1, $hook->callbacks[ $priority + 1 ] );80 }81 } -
tests/phpunit/tests/image/editor_gd.php
Property changes on: tests/phpunit/tests/hooks/remove_filter.php ___________________________________________________________________ Deleted: svn:executable ## -1 +0,0 ## -* \ No newline at end of property
1 <?php2 3 /**4 * Test the WP_Image_Editor_GD class5 * @group image6 * @group media7 * @group wp-image-editor-gd8 */9 require_once( dirname( __FILE__ ) . '/base.php' );10 11 class Tests_Image_Editor_GD extends WP_Image_UnitTestCase {12 13 public $editor_engine = 'WP_Image_Editor_GD';14 15 public function setUp() {16 require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );17 require_once( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' );18 19 parent::setUp();20 }21 22 public function tearDown() {23 $folder = DIR_TESTDATA . '/images/waffles-*.jpg';24 25 foreach ( glob( $folder ) as $file ) {26 unlink( $file );27 }28 29 $this->remove_added_uploads();30 31 parent::tearDown();32 }33 34 public function test_supports_mime_type_jpeg() {35 $gd_image_editor = new WP_Image_Editor_GD( null );36 $expected = imagetypes() & IMG_JPG;37 $this->assertEquals( $expected, $gd_image_editor->supports_mime_type( 'image/jpeg' ) );38 }39 40 public function test_supports_mime_type_png() {41 $gd_image_editor = new WP_Image_Editor_GD( null );42 $expected = imagetypes() & IMG_PNG;43 $this->assertEquals( $expected, $gd_image_editor->supports_mime_type( 'image/png' ) );44 }45 46 public function test_supports_mime_type_gif() {47 $gd_image_editor = new WP_Image_Editor_GD( null );48 $expected = imagetypes() & IMG_GIF;49 $this->assertEquals( $expected, $gd_image_editor->supports_mime_type( 'image/gif' ) );50 }51 52 /**53 * Test resizing an image, not using crop54 */55 public function test_resize() {56 $file = DIR_TESTDATA . '/images/waffles.jpg';57 58 $gd_image_editor = new WP_Image_Editor_GD( $file );59 $gd_image_editor->load();60 61 $gd_image_editor->resize( 100, 50 );62 63 $this->assertEquals(64 array(65 'width' => 75,66 'height' => 50,67 ),68 $gd_image_editor->get_size()69 );70 }71 72 /**73 * Test multi_resize with single image resize and no crop74 */75 public function test_single_multi_resize() {76 $file = DIR_TESTDATA . '/images/waffles.jpg';77 78 $gd_image_editor = new WP_Image_Editor_GD( $file );79 $gd_image_editor->load();80 81 $sizes_array = array(82 array(83 'width' => 50,84 'height' => 50,85 ),86 );87 88 $resized = $gd_image_editor->multi_resize( $sizes_array );89 90 # First, check to see if returned array is as expected91 $expected_array = array(92 array(93 'file' => 'waffles-50x33.jpg',94 'width' => 50,95 'height' => 33,96 'mime-type' => 'image/jpeg',97 ),98 );99 100 $this->assertEquals( $expected_array, $resized );101 102 // Now, verify real dimensions are as expected103 $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file'];104 $this->assertImageDimensions(105 $image_path,106 $expected_array[0]['width'],107 $expected_array[0]['height']108 );109 }110 111 /**112 * Ensure multi_resize doesn't create an image when113 * both height and weight are missing, null, or 0.114 *115 * ticket 26823116 */117 public function test_multi_resize_does_not_create() {118 $file = DIR_TESTDATA . '/images/waffles.jpg';119 120 $gd_image_editor = new WP_Image_Editor_GD( $file );121 $gd_image_editor->load();122 123 $sizes_array = array(124 array(125 'width' => 0,126 'height' => 0,127 ),128 array(129 'width' => 0,130 'height' => 0,131 'crop' => true,132 ),133 array(134 'width' => null,135 'height' => null,136 ),137 array(138 'width' => null,139 'height' => null,140 'crop' => true,141 ),142 array(143 'width' => '',144 'height' => '',145 ),146 array(147 'width' => '',148 'height' => '',149 'crop' => true,150 ),151 array(152 'width' => 0,153 ),154 array(155 'width' => 0,156 'crop' => true,157 ),158 array(159 'width' => null,160 ),161 array(162 'width' => null,163 'crop' => true,164 ),165 array(166 'width' => '',167 ),168 array(169 'width' => '',170 'crop' => true,171 ),172 );173 174 $resized = $gd_image_editor->multi_resize( $sizes_array );175 176 // If no images are generated, the returned array is empty.177 $this->assertEmpty( $resized );178 }179 180 /**181 * Test multi_resize with multiple sizes182 *183 * ticket 26823184 */185 public function test_multi_resize() {186 $file = DIR_TESTDATA . '/images/waffles.jpg';187 188 $gd_image_editor = new WP_Image_Editor_GD( $file );189 $gd_image_editor->load();190 191 $sizes_array = array(192 193 /**194 * #0 - 10x10 resize, no cropping.195 * By aspect, should be 10x6 output.196 */197 array(198 'width' => 10,199 'height' => 10,200 'crop' => false,201 ),202 203 /**204 * #1 - 75x50 resize, with cropping.205 * Output dimensions should be 75x50206 */207 array(208 'width' => 75,209 'height' => 50,210 'crop' => true,211 ),212 213 /**214 * #2 - 20 pixel max height, no cropping.215 * By aspect, should be 30x20 output.216 */217 array(218 'width' => 9999, # Arbitrary High Value219 'height' => 20,220 'crop' => false,221 ),222 223 /**224 * #3 - 45 pixel max height, with cropping.225 * By aspect, should be 45x400 output.226 */227 array(228 'width' => 45,229 'height' => 9999, # Arbitrary High Value230 'crop' => true,231 ),232 233 /**234 * #4 - 50 pixel max width, no cropping.235 * By aspect, should be 50x33 output.236 */237 array(238 'width' => 50,239 ),240 241 /**242 * #5 - 55 pixel max width, no cropping, null height243 * By aspect, should be 55x36 output.244 */245 array(246 'width' => 55,247 'height' => null,248 ),249 250 /**251 * #6 - 55 pixel max height, no cropping, no width specified.252 * By aspect, should be 82x55 output.253 */254 array(255 'height' => 55,256 ),257 258 /**259 * #7 - 60 pixel max height, no cropping, null width.260 * By aspect, should be 90x60 output.261 */262 array(263 'width' => null,264 'height' => 60,265 ),266 267 /**268 * #8 - 70 pixel max height, no cropping, negative width.269 * By aspect, should be 105x70 output.270 */271 array(272 'width' => -9999, # Arbitrary Negative Value273 'height' => 70,274 ),275 276 /**277 * #9 - 200 pixel max width, no cropping, negative height.278 * By aspect, should be 200x133 output.279 */280 array(281 'width' => 200,282 'height' => -9999, # Arbitrary Negative Value283 ),284 );285 286 $resized = $gd_image_editor->multi_resize( $sizes_array );287 288 $expected_array = array(289 290 // #0291 array(292 'file' => 'waffles-10x7.jpg',293 'width' => 10,294 'height' => 7,295 'mime-type' => 'image/jpeg',296 ),297 298 // #1299 array(300 'file' => 'waffles-75x50.jpg',301 'width' => 75,302 'height' => 50,303 'mime-type' => 'image/jpeg',304 ),305 306 // #2307 array(308 'file' => 'waffles-30x20.jpg',309 'width' => 30,310 'height' => 20,311 'mime-type' => 'image/jpeg',312 ),313 314 // #3315 array(316 'file' => 'waffles-45x400.jpg',317 'width' => 45,318 'height' => 400,319 'mime-type' => 'image/jpeg',320 ),321 322 // #4323 array(324 'file' => 'waffles-50x33.jpg',325 'width' => 50,326 'height' => 33,327 'mime-type' => 'image/jpeg',328 ),329 330 // #5331 array(332 'file' => 'waffles-55x37.jpg',333 'width' => 55,334 'height' => 37,335 'mime-type' => 'image/jpeg',336 ),337 338 // #6339 array(340 'file' => 'waffles-83x55.jpg',341 'width' => 83,342 'height' => 55,343 'mime-type' => 'image/jpeg',344 ),345 346 // #7347 array(348 'file' => 'waffles-90x60.jpg',349 'width' => 90,350 'height' => 60,351 'mime-type' => 'image/jpeg',352 ),353 354 // #8355 array(356 'file' => 'waffles-105x70.jpg',357 'width' => 105,358 'height' => 70,359 'mime-type' => 'image/jpeg',360 ),361 362 // #9363 array(364 'file' => 'waffles-200x133.jpg',365 'width' => 200,366 'height' => 133,367 'mime-type' => 'image/jpeg',368 ),369 );370 371 $this->assertNotNull( $resized );372 $this->assertEquals( $expected_array, $resized );373 374 foreach( $resized as $key => $image_data ){375 $image_path = DIR_TESTDATA . '/images/' . $image_data['file'];376 377 // Now, verify real dimensions are as expected378 $this->assertImageDimensions(379 $image_path,380 $expected_array[$key]['width'],381 $expected_array[$key]['height']382 );383 }384 }385 386 /**387 * Test resizing an image with cropping388 */389 public function test_resize_and_crop() {390 $file = DIR_TESTDATA . '/images/waffles.jpg';391 392 $gd_image_editor = new WP_Image_Editor_GD( $file );393 $gd_image_editor->load();394 395 $gd_image_editor->resize( 100, 50, true );396 397 $this->assertEquals(398 array(399 'width' => 100,400 'height' => 50,401 ),402 $gd_image_editor->get_size()403 );404 }405 406 /**407 * Test cropping an image408 */409 public function test_crop() {410 $file = DIR_TESTDATA . '/images/gradient-square.jpg';411 412 $gd_image_editor = new WP_Image_Editor_GD( $file );413 $gd_image_editor->load();414 415 $gd_image_editor->crop( 0, 0, 50, 50 );416 417 $this->assertEquals(418 array(419 'width' => 50,420 'height' => 50,421 ),422 $gd_image_editor->get_size()423 );424 }425 426 /**427 * Test rotating an image 180 deg428 */429 public function test_rotate() {430 $file = DIR_TESTDATA . '/images/gradient-square.jpg';431 432 $gd_image_editor = new WP_Image_Editor_GD( $file );433 $gd_image_editor->load();434 435 $property = new ReflectionProperty( $gd_image_editor, 'image' );436 $property->setAccessible( true );437 438 $color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 );439 440 $gd_image_editor->rotate( 180 );441 442 $this->assertEquals( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 99, 99 ) );443 }444 445 /**446 * Test flipping an image447 */448 public function test_flip() {449 $file = DIR_TESTDATA . '/images/gradient-square.jpg';450 451 $gd_image_editor = new WP_Image_Editor_GD( $file );452 $gd_image_editor->load();453 454 $property = new ReflectionProperty( $gd_image_editor, 'image' );455 $property->setAccessible( true );456 457 $color_top_left = imagecolorat( $property->getValue( $gd_image_editor ), 0, 0 );458 459 $gd_image_editor->flip( true, false );460 461 $this->assertEquals( $color_top_left, imagecolorat( $property->getValue( $gd_image_editor ), 0, 99 ) );462 }463 464 /**465 * Test the image created with WP_Image_Editor_GD preserves alpha when resizing466 *467 * @ticket 23039468 */469 public function test_image_preserves_alpha_on_resize() {470 if ( ! ( imagetypes() & IMG_PNG ) ) {471 $this->fail( 'This test requires PHP to be compiled with PNG support.' );472 }473 474 $file = DIR_TESTDATA . '/images/transparent.png';475 476 $editor = wp_get_image_editor( $file );477 478 $this->assertNotInstanceOf( 'WP_Error', $editor );479 480 $editor->load();481 $editor->resize( 5, 5 );482 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';483 484 $editor->save( $save_to_file );485 486 $this->assertImageAlphaAtPointGD( $save_to_file, array( 0,0 ), 127 );487 488 unlink( $save_to_file );489 }490 491 /**492 * Test the image created with WP_Image_Editor_GD preserves alpha with no resizing etc493 *494 * @ticket 23039495 */496 public function test_image_preserves_alpha() {497 if ( ! ( imagetypes() & IMG_PNG ) ) {498 $this->fail( 'This test requires PHP to be compiled with PNG support.' );499 }500 501 $file = DIR_TESTDATA . '/images/transparent.png';502 503 $editor = wp_get_image_editor( $file );504 505 $this->assertNotInstanceOf( 'WP_Error', $editor );506 507 $editor->load();508 509 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';510 511 $editor->save( $save_to_file );512 513 $this->assertImageAlphaAtPointGD( $save_to_file, array( 0,0 ), 127 );514 515 unlink( $save_to_file );516 }517 518 /**519 *520 * @ticket 30596521 */522 public function test_image_preserves_alpha_on_rotate() {523 if ( ! ( imagetypes() & IMG_PNG ) ) {524 $this->fail( 'This test requires PHP to be compiled with PNG support.' );525 }526 527 $file = DIR_TESTDATA . '/images/transparent.png';528 529 $image = imagecreatefrompng( $file );530 $rgb = imagecolorat( $image, 0, 0 );531 $expected = imagecolorsforindex( $image, $rgb );532 533 $editor = new WP_Image_Editor_GD( $file );534 $this->assertNotInstanceOf( 'WP_Error', $editor );535 $editor->load();536 $editor->rotate( 180 );537 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';538 539 $editor->save( $save_to_file );540 541 $this->assertImageAlphaAtPointGD( $save_to_file, array( 0,0 ), $expected['alpha'] );542 unlink( $save_to_file );543 544 }545 546 /**547 * Test WP_Image_Editor_GD handles extension-less images548 * @ticket 39195549 */550 public function test_image_non_existent_extension() {551 $image_editor = new WP_Image_Editor_GD( DIR_TESTDATA.'/images/test-image-no-extension' );552 $result = $image_editor->load();553 554 $this->assertTrue( $result );555 }556 } -
tests/phpunit/tests/image/editor_imagick.php
1 <?php2 3 /**4 * Test the WP_Image_Editor_Imagick class5 * @group image6 * @group media7 * @group wp-image-editor-imagick8 */9 require_once( dirname( __FILE__ ) . '/base.php' );10 11 class Tests_Image_Editor_Imagick extends WP_Image_UnitTestCase {12 13 public $editor_engine = 'WP_Image_Editor_Imagick';14 15 public function setUp() {16 require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );17 require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );18 19 parent::setUp();20 }21 22 public function tearDown() {23 $folder = DIR_TESTDATA . '/images/waffles-*.jpg';24 25 foreach ( glob( $folder ) as $file ) {26 unlink( $file );27 }28 29 $this->remove_added_uploads();30 31 parent::tearDown();32 }33 34 /**35 * Check support for ImageMagick compatible mime types.36 */37 public function test_supports_mime_type() {38 $imagick_image_editor = new WP_Image_Editor_Imagick( null );39 40 $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/jpeg' ), 'Does not support image/jpeg' );41 $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/png' ), 'Does not support image/png' );42 $this->assertTrue( $imagick_image_editor->supports_mime_type( 'image/gif' ), 'Does not support image/gif' );43 }44 45 /**46 * Test resizing an image, not using crop47 */48 public function test_resize() {49 $file = DIR_TESTDATA . '/images/waffles.jpg';50 51 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );52 $imagick_image_editor->load();53 54 $imagick_image_editor->resize( 100, 50 );55 56 $this->assertEquals(57 array(58 'width' => 75,59 'height' => 50,60 ),61 $imagick_image_editor->get_size()62 );63 }64 65 /**66 * Test multi_resize with single image resize and no crop67 */68 public function test_single_multi_resize() {69 $file = DIR_TESTDATA . '/images/waffles.jpg';70 71 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );72 $imagick_image_editor->load();73 74 $sizes_array = array(75 array(76 'width' => 50,77 'height' => 50,78 ),79 );80 81 $resized = $imagick_image_editor->multi_resize( $sizes_array );82 83 # First, check to see if returned array is as expected84 $expected_array = array(85 array(86 'file' => 'waffles-50x33.jpg',87 'width' => 50,88 'height' => 33,89 'mime-type' => 'image/jpeg',90 ),91 );92 93 $this->assertEquals( $expected_array, $resized );94 95 // Now, verify real dimensions are as expected96 $image_path = DIR_TESTDATA . '/images/'. $resized[0]['file'];97 $this->assertImageDimensions(98 $image_path,99 $expected_array[0]['width'],100 $expected_array[0]['height']101 );102 }103 104 /**105 * Ensure multi_resize doesn't create an image when106 * both height and weight are missing, null, or 0.107 *108 * ticket 26823109 */110 public function test_multi_resize_does_not_create() {111 $file = DIR_TESTDATA . '/images/waffles.jpg';112 113 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );114 $imagick_image_editor->load();115 116 $sizes_array = array(117 array(118 'width' => 0,119 'height' => 0,120 ),121 array(122 'width' => 0,123 'height' => 0,124 'crop' => true,125 ),126 array(127 'width' => null,128 'height' => null,129 ),130 array(131 'width' => null,132 'height' => null,133 'crop' => true,134 ),135 array(136 'width' => '',137 'height' => '',138 ),139 array(140 'width' => '',141 'height' => '',142 'crop' => true,143 ),144 array(145 'width' => 0,146 ),147 array(148 'width' => 0,149 'crop' => true,150 ),151 array(152 'width' => null,153 ),154 array(155 'width' => null,156 'crop' => true,157 ),158 array(159 'width' => '',160 ),161 array(162 'width' => '',163 'crop' => true,164 ),165 );166 167 $resized = $imagick_image_editor->multi_resize( $sizes_array );168 169 // If no images are generated, the returned array is empty.170 $this->assertEmpty( $resized );171 }172 173 /**174 * Test multi_resize with multiple sizes175 *176 * ticket 26823177 */178 public function test_multi_resize() {179 $file = DIR_TESTDATA . '/images/waffles.jpg';180 181 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );182 $imagick_image_editor->load();183 184 $sizes_array = array(185 186 /**187 * #0 - 10x10 resize, no cropping.188 * By aspect, should be 10x6 output.189 */190 array(191 'width' => 10,192 'height' => 10,193 'crop' => false,194 ),195 196 /**197 * #1 - 75x50 resize, with cropping.198 * Output dimensions should be 75x50199 */200 array(201 'width' => 75,202 'height' => 50,203 'crop' => true,204 ),205 206 /**207 * #2 - 20 pixel max height, no cropping.208 * By aspect, should be 30x20 output.209 */210 array(211 'width' => 9999, # Arbitrary High Value212 'height' => 20,213 'crop' => false,214 ),215 216 /**217 * #3 - 45 pixel max height, with cropping.218 * By aspect, should be 45x400 output.219 */220 array(221 'width' => 45,222 'height' => 9999, # Arbitrary High Value223 'crop' => true,224 ),225 226 /**227 * #4 - 50 pixel max width, no cropping.228 * By aspect, should be 50x33 output.229 */230 array(231 'width' => 50,232 ),233 234 /**235 * #5 - 55 pixel max width, no cropping, null height236 * By aspect, should be 55x36 output.237 */238 array(239 'width' => 55,240 'height' => null,241 ),242 243 /**244 * #6 - 55 pixel max height, no cropping, no width specified.245 * By aspect, should be 82x55 output.246 */247 array(248 'height' => 55,249 ),250 251 /**252 * #7 - 60 pixel max height, no cropping, null width.253 * By aspect, should be 90x60 output.254 */255 array(256 'width' => null,257 'height' => 60,258 ),259 260 /**261 * #8 - 70 pixel max height, no cropping, negative width.262 * By aspect, should be 105x70 output.263 */264 array(265 'width' => -9999, # Arbitrary Negative Value266 'height' => 70,267 ),268 269 /**270 * #9 - 200 pixel max width, no cropping, negative height.271 * By aspect, should be 200x133 output.272 */273 array(274 'width' => 200,275 'height' => -9999, # Arbitrary Negative Value276 ),277 );278 279 $resized = $imagick_image_editor->multi_resize( $sizes_array );280 281 $expected_array = array(282 283 // #0284 array(285 'file' => 'waffles-10x7.jpg',286 'width' => 10,287 'height' => 7,288 'mime-type' => 'image/jpeg',289 ),290 291 // #1292 array(293 'file' => 'waffles-75x50.jpg',294 'width' => 75,295 'height' => 50,296 'mime-type' => 'image/jpeg',297 ),298 299 // #2300 array(301 'file' => 'waffles-30x20.jpg',302 'width' => 30,303 'height' => 20,304 'mime-type' => 'image/jpeg',305 ),306 307 // #3308 array(309 'file' => 'waffles-45x400.jpg',310 'width' => 45,311 'height' => 400,312 'mime-type' => 'image/jpeg',313 ),314 315 // #4316 array(317 'file' => 'waffles-50x33.jpg',318 'width' => 50,319 'height' => 33,320 'mime-type' => 'image/jpeg',321 ),322 323 // #5324 array(325 'file' => 'waffles-55x37.jpg',326 'width' => 55,327 'height' => 37,328 'mime-type' => 'image/jpeg',329 ),330 331 // #6332 array(333 'file' => 'waffles-83x55.jpg',334 'width' => 83,335 'height' => 55,336 'mime-type' => 'image/jpeg',337 ),338 339 // #7340 array(341 'file' => 'waffles-90x60.jpg',342 'width' => 90,343 'height' => 60,344 'mime-type' => 'image/jpeg',345 ),346 347 // #8348 array(349 'file' => 'waffles-105x70.jpg',350 'width' => 105,351 'height' => 70,352 'mime-type' => 'image/jpeg',353 ),354 355 // #9356 array(357 'file' => 'waffles-200x133.jpg',358 'width' => 200,359 'height' => 133,360 'mime-type' => 'image/jpeg',361 ),362 );363 364 $this->assertNotNull( $resized );365 $this->assertEquals( $expected_array, $resized );366 367 foreach( $resized as $key => $image_data ){368 $image_path = DIR_TESTDATA . '/images/' . $image_data['file'];369 370 // Now, verify real dimensions are as expected371 $this->assertImageDimensions(372 $image_path,373 $expected_array[$key]['width'],374 $expected_array[$key]['height']375 );376 }377 }378 379 /**380 * Test resizing an image with cropping381 */382 public function test_resize_and_crop() {383 $file = DIR_TESTDATA . '/images/waffles.jpg';384 385 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );386 $imagick_image_editor->load();387 388 $imagick_image_editor->resize( 100, 50, true );389 390 $this->assertEquals(391 array(392 'width' => 100,393 'height' => 50,394 ),395 $imagick_image_editor->get_size()396 );397 }398 399 /**400 * Test cropping an image401 */402 public function test_crop() {403 $file = DIR_TESTDATA . '/images/gradient-square.jpg';404 405 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );406 $imagick_image_editor->load();407 408 $imagick_image_editor->crop( 0, 0, 50, 50 );409 410 $this->assertEquals(411 array(412 'width' => 50,413 'height' => 50,414 ),415 $imagick_image_editor->get_size()416 );417 }418 419 /**420 * Test rotating an image 180 deg421 */422 public function test_rotate() {423 $file = DIR_TESTDATA . '/images/gradient-square.jpg';424 425 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );426 $imagick_image_editor->load();427 428 $property = new ReflectionProperty( $imagick_image_editor, 'image' );429 $property->setAccessible( true );430 431 $color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 1, 1 )->getColor();432 433 $imagick_image_editor->rotate( 180 );434 435 $this->assertEquals( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 99, 99 )->getColor() );436 }437 438 /**439 * Test flipping an image440 */441 public function test_flip() {442 $file = DIR_TESTDATA . '/images/gradient-square.jpg';443 444 $imagick_image_editor = new WP_Image_Editor_Imagick( $file );445 $imagick_image_editor->load();446 447 $property = new ReflectionProperty( $imagick_image_editor, 'image' );448 $property->setAccessible( true );449 450 $color_top_left = $property->getValue( $imagick_image_editor )->getImagePixelColor( 1, 1 )->getColor();451 452 $imagick_image_editor->flip( true, false );453 454 $this->assertEquals( $color_top_left, $property->getValue( $imagick_image_editor )->getImagePixelColor( 0, 99 )->getColor() );455 }456 457 /**458 * Test the image created with WP_Image_Editor_Imagick preserves alpha when resizing459 *460 * @ticket 24871461 */462 public function test_image_preserves_alpha_on_resize() {463 $file = DIR_TESTDATA . '/images/transparent.png';464 465 $editor = new WP_Image_Editor_Imagick( $file );466 467 $this->assertNotInstanceOf( 'WP_Error', $editor );468 469 $editor->load();470 $editor->resize( 5, 5 );471 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';472 473 $editor->save( $save_to_file );474 475 $im = new Imagick( $save_to_file );476 $pixel = $im->getImagePixelColor( 0, 0 );477 $expected = $pixel->getColorValue( imagick::COLOR_ALPHA );478 479 $this->assertImageAlphaAtPointImagick( $save_to_file, array( 0,0 ), $expected );480 481 unlink( $save_to_file );482 }483 484 /**485 * Test the image created with WP_Image_Editor_Imagick preserves alpha with no resizing etc486 *487 * @ticket 24871488 */489 public function test_image_preserves_alpha() {490 $file = DIR_TESTDATA . '/images/transparent.png';491 492 $editor = new WP_Image_Editor_Imagick( $file );493 494 $this->assertNotInstanceOf( 'WP_Error', $editor );495 496 $editor->load();497 498 $save_to_file = tempnam( get_temp_dir(), '' ) . '.png';499 500 $editor->save( $save_to_file );501 502 $im = new Imagick( $save_to_file );503 $pixel = $im->getImagePixelColor( 0, 0 );504 $expected = $pixel->getColorValue( imagick::COLOR_ALPHA );505 506 $this->assertImageAlphaAtPointImagick( $save_to_file, array( 0,0 ), $expected );507 508 unlink( $save_to_file );509 }510 511 /**512 *513 * @ticket 30596514 */515 public function test_image_preserves_alpha_on_rotate() {516 $file = DIR_TESTDATA . '/images/transparent.png';517 518 $pre_rotate_editor = new Imagick( $file );519 $pre_rotate_pixel = $pre_rotate_editor->getImagePixelColor( 0, 0 );520 $pre_rotate_alpha = $pre_rotate_pixel->getColorValue( imagick::COLOR_ALPHA );521 $save_to_file = tempnam( get_temp_dir(),'' ) . '.png';522 $pre_rotate_editor->writeImage( $save_to_file );523 $pre_rotate_editor->destroy();524 525 $image_editor = new WP_Image_Editor_Imagick( $save_to_file );526 $image_editor->load();527 $this->assertNotInstanceOf( 'WP_Error', $image_editor );528 $image_editor->rotate( 180 );529 $image_editor->save( $save_to_file );530 531 $this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $pre_rotate_alpha );532 unlink( $save_to_file );533 }534 535 /**536 * Test WP_Image_Editor_Imagick handles extension-less images537 * @ticket 39195538 */539 public function test_image_non_existent_extension() {540 $image_editor = new WP_Image_Editor_Imagick( DIR_TESTDATA.'/images/test-image-no-extension' );541 $result = $image_editor->load();542 543 $this->assertTrue( $result );544 }545 546 /**547 * Test resetting Exif orientation data on rotate548 *549 * @ticket 37140550 */551 public function test_remove_orientation_data_on_rotate() {552 $file = DIR_TESTDATA . "/images/test-image-upside-down.jpg";553 $data = wp_read_image_metadata( $file );554 555 // The orientation value 3 is equivalent to rotated upside down (180 degrees).556 $this->assertEquals( 3, intval( $data['orientation'] ), 'Orientation value read from does not match image file Exif data: ' . $file );557 558 $temp_file = wp_tempnam( $file );559 $image = wp_get_image_editor( $file );560 561 // Test a value that would not lead back to 1, as WP is resetting the value to 1 manually.562 $image->rotate( 90 );563 $ret = $image->save( $temp_file, 'image/jpeg' );564 565 $data = wp_read_image_metadata( $ret['path'] );566 567 // Make sure the image is no longer in The Upside Down Exif orientation.568 $this->assertEquals( 1, intval( $data['orientation'] ), 'Orientation Exif data was not updated after rotating image: ' . $file );569 570 // Remove both the generated file ending in .tmp and tmp.jpg due to wp_tempnam().571 unlink( $temp_file );572 unlink( $ret['path'] );573 }574 575 } -
tests/phpunit/tests/image/intermediate_size.php
1 <?php2 /**3 * @group image4 * @group media5 * @group upload6 */7 class Tests_Image_Intermediate_Size extends WP_UnitTestCase {8 function tearDown() {9 $this->remove_added_uploads();10 11 remove_image_size( 'test-size' );12 remove_image_size( 'false-height' );13 remove_image_size( 'false-width' );14 remove_image_size( 'off-by-one' );15 parent::tearDown();16 }17 18 public function _make_attachment( $file, $parent_post_id = 0 ) {19 $contents = file_get_contents( $file );20 $upload = wp_upload_bits( basename( $file ), null, $contents );21 22 return parent::_make_attachment( $upload, $parent_post_id );23 }24 25 function test_make_intermediate_size_no_size() {26 $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );27 28 $this->assertFalse( $image );29 }30 31 function test_make_intermediate_size_width() {32 if ( !function_exists( 'imagejpeg' ) )33 $this->fail( 'jpeg support unavailable' );34 35 $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 100, 0, false );36 37 $this->assertInternalType( 'array', $image );38 }39 40 function test_make_intermediate_size_height() {41 if ( !function_exists( 'imagejpeg' ) )42 $this->fail( 'jpeg support unavailable' );43 44 $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 75, false );45 46 $this->assertInternalType( 'array', $image );47 }48 49 function test_make_intermediate_size_successful() {50 if ( !function_exists( 'imagejpeg' ) )51 $this->fail( 'jpeg support unavailable' );52 53 $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 100, 75, true );54 55 $this->assertInternalType( 'array', $image );56 $this->assertEquals( 100, $image['width'] );57 $this->assertEquals( 75, $image['height'] );58 $this->assertEquals( 'image/jpeg', $image['mime-type'] );59 60 $this->assertFalse( isset( $image['path'] ) );61 62 unlink( DIR_TESTDATA . '/images/a2-small-100x75.jpg' );63 }64 65 /**66 * @ticket 1762667 */68 function test_get_intermediate_sizes_by_name() {69 add_image_size( 'test-size', 330, 220, true );70 71 $file = DIR_TESTDATA . '/images/waffles.jpg';72 $id = $this->_make_attachment( $file, 0 );73 74 // look for a size by name75 $image = image_get_intermediate_size( $id, 'test-size' );76 77 // cleanup78 remove_image_size( 'test-size' );79 80 // test for the expected string because the array will by definition81 // return with the correct height and width attributes82 $this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );83 }84 85 /**86 * @ticket 1762687 */88 function test_get_intermediate_sizes_by_array_exact() {89 // Only one dimention match shouldn't return false positive (see: 17626)90 add_image_size( 'test-size', 330, 220, true );91 add_image_size( 'false-height', 330, 400, true );92 add_image_size( 'false-width', 600, 220, true );93 94 $file = DIR_TESTDATA . '/images/waffles.jpg';95 $id = $this->_make_attachment( $file, 0 );96 97 // look for a size by array that exists98 // note: staying larger than 300px to miss default medium crop99 $image = image_get_intermediate_size( $id, array( 330, 220 ) );100 101 // test for the expected string because the array will by definition102 // return with the correct height and width attributes103 $this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );104 }105 106 /**107 * @ticket 17626108 */109 function test_get_intermediate_sizes_by_array_nearest() {110 // If an exact size is not found, it should be returned111 // If not, find nearest size that is larger (see: 17626)112 add_image_size( 'test-size', 450, 300, true );113 add_image_size( 'false-height', 330, 100, true );114 add_image_size( 'false-width', 150, 220, true );115 116 $file = DIR_TESTDATA . '/images/waffles.jpg';117 $id = $this->_make_attachment( $file, 0 );118 119 // look for a size by array that doesn't exist120 // note: staying larger than 300px to miss default medium crop121 $image = image_get_intermediate_size( $id, array( 330, 220 ) );122 123 // you have to test for the string because the image will by definition124 // return with the correct height and width attributes125 $this->assertTrue( strpos( $image['file'], '450x300' ) > 0 );126 }127 128 /**129 * @ticket 17626130 */131 function test_get_intermediate_sizes_by_array_nearest_false() {132 // If an exact size is not found, it should be returned133 // If not, find nearest size that is larger, otherwise return false (see: 17626)134 add_image_size( 'false-height', 330, 100, true );135 add_image_size( 'false-width', 150, 220, true );136 137 $file = DIR_TESTDATA . '/images/waffles.jpg';138 $id = $this->_make_attachment( $file, 0 );139 140 // look for a size by array that doesn't exist141 // note: staying larger than 300px to miss default medium crop142 $image = image_get_intermediate_size( $id, array( 330, 220 ) );143 144 // you have to test for the string because the image will by definition145 // return with the correct height and width attributes146 $this->assertFalse( $image );147 }148 149 /**150 * @ticket 17626151 */152 function test_get_intermediate_sizes_by_array_zero_height() {153 // Generate random width154 $random_w = rand( 300, 400 );155 156 // Only one dimention match shouldn't return false positive (see: 17626)157 add_image_size( 'test-size', $random_w, 0, false );158 add_image_size( 'false-height', $random_w, 100, true );159 160 $file = DIR_TESTDATA . '/images/waffles.jpg';161 $id = $this->_make_attachment( $file, 0 );162 163 $original = wp_get_attachment_metadata( $id );164 $image_w = $random_w;165 $image_h = round( ( $image_w / $original['width'] ) * $original['height'] );166 167 // look for a size by array that exists168 // note: staying larger than 300px to miss default medium crop169 $image = image_get_intermediate_size( $id, array( $random_w, 0 ) );170 171 // test for the expected string because the array will by definition172 // return with the correct height and width attributes173 $this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );174 }175 176 /**177 * @ticket 17626178 * @ticket 34087179 */180 function test_get_intermediate_sizes_by_array_zero_width() {181 // 202 is the smallest height that will trigger a miss for 'false-height'.182 $height = 202;183 184 // Only one dimention match shouldn't return false positive (see: 17626)185 add_image_size( 'test-size', 0, $height, false );186 add_image_size( 'false-height', 300, $height, true );187 188 $file = DIR_TESTDATA . '/images/waffles.jpg';189 $id = $this->_make_attachment( $file, 0 );190 191 $original = wp_get_attachment_metadata( $id );192 $image_h = $height;193 $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );194 195 // look for a size by array that exists196 // note: staying larger than 300px to miss default medium crop197 $image = image_get_intermediate_size( $id, array( 0, $height ) );198 199 // test for the expected string because the array will by definition200 // return with the correct height and width attributes201 $this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );202 }203 204 /**205 * @ticket 17626206 * @ticket 34087207 */208 public function test_get_intermediate_sizes_should_match_size_with_off_by_one_aspect_ratio() {209 // Original is 600x400. 300x201 is close enough to match.210 $width = 300;211 $height = 201;212 add_image_size( 'off-by-one', $width, $height, true );213 214 $file = DIR_TESTDATA . '/images/waffles.jpg';215 $id = $this->_make_attachment( $file, 0 );216 217 $original = wp_get_attachment_metadata( $id );218 $image_h = $height;219 $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );220 221 // look for a size by array that exists222 // note: staying larger than 300px to miss default medium crop223 $image = image_get_intermediate_size( $id, array( 0, $height ) );224 225 $this->assertTrue( strpos( $image['file'], $width . 'x' . $height ) > 0 );226 }227 228 /**229 * @ticket 34384230 */231 public function test_get_intermediate_size_with_small_size_array() {232 // Add a hard cropped size that matches the aspect ratio we're going to test.233 add_image_size( 'test-size', 200, 100, true );234 235 $file = DIR_TESTDATA . '/images/waffles.jpg';236 $id = $this->_make_attachment( $file, 0 );237 238 // Request a size by array that doesn't exist and is smaller than the 'thumbnail'239 $image = image_get_intermediate_size( $id, array( 50, 25 ) );240 241 // We should get the 'test-size' file and not the thumbnail.242 $this->assertTrue( strpos( $image['file'], '200x100' ) > 0 );243 }244 245 /**246 * @ticket 34384247 */248 public function test_get_intermediate_size_with_small_size_array_fallback() {249 $file = DIR_TESTDATA . '/images/waffles.jpg';250 $id = $this->_make_attachment( $file, 0 );251 252 $original = wp_get_attachment_metadata( $id );253 $thumbnail_file = $original['sizes']['thumbnail']['file'];254 255 // Request a size by array that doesn't exist and is smaller than the 'thumbnail'256 $image = image_get_intermediate_size( $id, array( 50, 25 ) );257 258 // We should get the 'thumbnail' file as a fallback.259 $this->assertSame( $image['file'], $thumbnail_file );260 }261 } -
tests/phpunit/tests/image/resize_gd.php
1 <?php2 3 /**4 * @group image5 * @group media6 * @group upload7 * @group resize8 */9 require_once( dirname( __FILE__ ) . '/resize.php' );10 11 class Test_Image_Resize_GD extends WP_Tests_Image_Resize_UnitTestCase {12 13 /**14 * Use the GD image editor engine15 * @var string16 */17 public $editor_engine = 'WP_Image_Editor_GD';18 19 public function setUp() {20 require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );21 require_once( ABSPATH . WPINC . '/class-wp-image-editor-gd.php' );22 23 parent::setUp();24 }25 26 /**27 * Try resizing a php file (bad image)28 * @ticket 682129 */30 public function test_resize_bad_image() {31 32 $image = $this->resize_helper( DIR_TESTDATA.'/export/crazy-cdata.xml', 25, 25 );33 $this->assertInstanceOf( 'WP_Error', $image );34 $this->assertEquals( 'invalid_image', $image->get_error_code() );35 }36 37 } -
tests/phpunit/tests/image/resize_imagick.php
1 <?php2 3 /**4 * @group image5 * @group media6 * @group upload7 * @group resize8 */9 require_once( dirname( __FILE__ ) . '/resize.php' );10 11 class Test_Image_Resize_Imagick extends WP_Tests_Image_Resize_UnitTestCase {12 13 /**14 * Use the Imagick image editor engine15 * @var string16 */17 public $editor_engine = 'WP_Image_Editor_Imagick';18 19 public function setUp() {20 require_once( ABSPATH . WPINC . '/class-wp-image-editor.php' );21 require_once( ABSPATH . WPINC . '/class-wp-image-editor-imagick.php' );22 23 parent::setUp();24 }25 }26 No newline at end of file -
tests/phpunit/tests/image/site_icon.php
1 <?php2 /**3 * Tests for the WP_Site_Icon class.4 *5 * @group site_icon6 */7 8 require_once( ABSPATH . 'wp-admin/includes/class-wp-site-icon.php' );9 10 class Tests_WP_Site_Icon extends WP_UnitTestCase {11 protected $wp_site_icon;12 13 public $attachment_id = 0;14 15 function setUp() {16 parent::setUp();17 18 $this->wp_site_icon = new WP_Site_Icon();19 }20 21 function tearDown() {22 $this->_remove_custom_logo();23 $this->remove_added_uploads();24 parent::tearDown();25 }26 27 function _remove_custom_logo() {28 remove_theme_mod( 'custom_logo' );29 }30 31 function test_intermediate_image_sizes() {32 $image_sizes = $this->wp_site_icon->intermediate_image_sizes( array() );33 34 $sizes = array();35 foreach ( $this->wp_site_icon->site_icon_sizes as $size ) {36 $sizes[] = 'site_icon-' . $size;37 }38 39 $this->assertEquals( $sizes, $image_sizes );40 }41 42 function test_intermediate_image_sizes_with_filter() {43 add_filter( 'site_icon_image_sizes', array( $this, '_custom_test_sizes' ) );44 $image_sizes = $this->wp_site_icon->intermediate_image_sizes( array() );45 46 $sizes = array();47 foreach ( $this->wp_site_icon->site_icon_sizes as $size ) {48 $sizes[] = 'site_icon-' . $size;49 }50 51 // Is our custom icon size there?52 $this->assertContains( 'site_icon-321', $image_sizes );53 54 // All icon sizes should be part of the array, including sizes added through the filter.55 $this->assertEquals( $sizes, $image_sizes );56 57 // Remove custom size.58 unset( $this->wp_site_icon->site_icon_sizes[ array_search( 321, $this->wp_site_icon->site_icon_sizes ) ] );59 // Remove the filter we added60 remove_filter( 'site_icon_image_sizes', array( $this, '_custom_test_sizes' ) );61 }62 63 function test_additional_sizes() {64 $image_sizes = $this->wp_site_icon->additional_sizes( array() );65 66 $sizes = array();67 foreach ( $this->wp_site_icon->site_icon_sizes as $size ) {68 $sizes[ 'site_icon-' . $size ] = array(69 'width ' => $size,70 'height' => $size,71 'crop' => true,72 );73 }74 75 $this->assertEquals( $sizes, $image_sizes );76 }77 78 function test_additional_sizes_with_filter() {79 add_filter( 'site_icon_image_sizes', array( $this, '_custom_test_sizes' ) );80 $image_sizes = $this->wp_site_icon->additional_sizes( array() );81 82 $sizes = array();83 foreach ( $this->wp_site_icon->site_icon_sizes as $size ) {84 $sizes[ 'site_icon-' . $size ] = array(85 'width ' => $size,86 'height' => $size,87 'crop' => true,88 );89 }90 91 // Is our custom icon size there?92 $this->assertArrayHasKey( 'site_icon-321', $image_sizes );93 94 // All icon sizes should be part of the array, including sizes added through the filter.95 $this->assertEquals( $sizes, $image_sizes );96 97 // Remove custom size.98 unset( $this->wp_site_icon->site_icon_sizes[ array_search( 321, $this->wp_site_icon->site_icon_sizes ) ] );99 }100 101 function test_create_attachment_object() {102 $attachment_id = $this->_insert_attachment();103 $parent_url = get_post( $attachment_id )->guid;104 $cropped = str_replace( basename( $parent_url ), 'cropped-test-image.jpg', $parent_url );105 106 $object = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id );107 108 $this->assertEquals( $object['post_title'], 'cropped-test-image.jpg' );109 $this->assertEquals( $object['context'], 'site-icon' );110 $this->assertEquals( $object['post_mime_type'], 'image/jpeg' );111 $this->assertEquals( $object['post_content'], $cropped );112 $this->assertEquals( $object['guid'], $cropped );113 }114 115 function test_insert_cropped_attachment() {116 $attachment_id = $this->_insert_attachment();117 $parent_url = get_post( $attachment_id )->guid;118 $cropped = str_replace( basename( $parent_url ), 'cropped-test-image.jpg', $parent_url );119 120 $object = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id );121 $cropped_id = $this->wp_site_icon->insert_attachment( $object, $cropped );122 123 $this->assertInternalType( 'int', $cropped_id );124 $this->assertGreaterThan( 0, $cropped_id );125 }126 127 function test_delete_attachment_data() {128 $attachment_id = $this->_insert_attachment();129 update_option( 'site_icon', $attachment_id );130 131 wp_delete_attachment( $attachment_id, true );132 133 $this->assertFalse( get_option( 'site_icon', false ) );134 }135 136 /**137 * @ticket 34368138 */139 function test_get_post_metadata() {140 $attachment_id = $this->_insert_attachment();141 update_option( 'site_icon', $attachment_id );142 143 $this->wp_site_icon->get_post_metadata( '', $attachment_id, '_some_post_meta', true );144 $this->assertFalse( has_filter( 'intermediate_image_sizes', array( $this->wp_site_icon, 'intermediate_image_sizes' ) ) );145 146 $this->wp_site_icon->get_post_metadata( '', $attachment_id, '_wp_attachment_backup_sizes', true );147 $this->assertSame( 10, has_filter( 'intermediate_image_sizes', array( $this->wp_site_icon, 'intermediate_image_sizes' ) ) );148 149 wp_delete_attachment( $attachment_id, true );150 }151 152 function _custom_test_sizes( $sizes ) {153 $sizes[] = 321;154 155 return $sizes;156 }157 158 function _insert_attachment() {159 if ( $this->attachment_id ) {160 return $this->attachment_id;161 }162 163 $filename = DIR_TESTDATA . '/images/test-image.jpg';164 $contents = file_get_contents( $filename );165 166 $upload = wp_upload_bits( basename( $filename ), null, $contents );167 168 $this->attachment_id = $this->_make_attachment( $upload );169 return $this->attachment_id;170 }171 }