- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/widgets/media-image-widget.php
r42030 r42343 24 24 parent::clean_up_global_scope(); 25 25 $wp_scripts = null; 26 $wp_styles = null;26 $wp_styles = null; 27 27 } 28 28 … … 36 36 $schema = $widget->get_instance_schema(); 37 37 38 $this->assertEqualSets( array( 39 'alt', 40 'attachment_id', 41 'caption', 42 'height', 43 'image_classes', 44 'image_title', 45 'link_classes', 46 'link_rel', 47 'link_target_blank', 48 'link_type', 49 'link_url', 50 'size', 51 'title', 52 'url', 53 'width', 54 ), array_keys( $schema ) ); 38 $this->assertEqualSets( 39 array( 40 'alt', 41 'attachment_id', 42 'caption', 43 'height', 44 'image_classes', 45 'image_title', 46 'link_classes', 47 'link_rel', 48 'link_target_blank', 49 'link_type', 50 'link_url', 51 'size', 52 'title', 53 'url', 54 'width', 55 ), array_keys( $schema ) 56 ); 55 57 } 56 58 … … 68 70 $this->assertTrue( $widget->widget_options['customize_selective_refresh'] ); 69 71 $this->assertEquals( 'image', $widget->widget_options['mime_type'] ); 70 $this->assertEqualSets( array( 71 'add_to_widget', 72 'replace_media', 73 'edit_media', 74 'media_library_state_multi', 75 'media_library_state_single', 76 'missing_attachment', 77 'no_media_selected', 78 'add_media', 79 'unsupported_file_type', 80 ), array_keys( $widget->l10n ) ); 72 $this->assertEqualSets( 73 array( 74 'add_to_widget', 75 'replace_media', 76 'edit_media', 77 'media_library_state_multi', 78 'media_library_state_single', 79 'missing_attachment', 80 'no_media_selected', 81 'add_media', 82 'unsupported_file_type', 83 ), array_keys( $widget->l10n ) 84 ); 81 85 } 82 86 … … 87 91 */ 88 92 function test_update() { 89 $widget = new WP_Widget_Media_Image();93 $widget = new WP_Widget_Media_Image(); 90 94 $instance = array(); 91 95 … … 94 98 'attachment_id' => 1, 95 99 ); 96 $result = $widget->update( $expected, $instance );100 $result = $widget->update( $expected, $instance ); 97 101 $this->assertSame( $result, $expected ); 98 102 99 103 // Should filter invalid attachment ID. 100 $result = $widget->update( array( 101 'attachment_id' => 'media', 102 ), $instance ); 104 $result = $widget->update( 105 array( 106 'attachment_id' => 'media', 107 ), $instance 108 ); 103 109 $this->assertSame( $result, $instance ); 104 110 … … 107 113 'url' => 'https://example.org', 108 114 ); 109 $result = $widget->update( $expected, $instance );115 $result = $widget->update( $expected, $instance ); 110 116 $this->assertSame( $result, $expected ); 111 117 112 118 // Should filter invalid attachment url. 113 $result = $widget->update( array( 114 'url' => 'not_a_url', 115 ), $instance ); 119 $result = $widget->update( 120 array( 121 'url' => 'not_a_url', 122 ), $instance 123 ); 116 124 $this->assertNotSame( $result, $instance ); 117 125 $this->assertStringStartsWith( 'http://', $result['url'] ); … … 121 129 'title' => 'What a title', 122 130 ); 123 $result = $widget->update( $expected, $instance );131 $result = $widget->update( $expected, $instance ); 124 132 $this->assertSame( $result, $expected ); 125 133 126 134 // Should filter invalid attachment title. 127 $result = $widget->update( array( 128 'title' => '<h1>W00t!</h1>', 129 ), $instance ); 135 $result = $widget->update( 136 array( 137 'title' => '<h1>W00t!</h1>', 138 ), $instance 139 ); 130 140 $this->assertNotSame( $result, $instance ); 131 141 … … 134 144 'size' => 'thumbnail', 135 145 ); 136 $result = $widget->update( $expected, $instance );146 $result = $widget->update( $expected, $instance ); 137 147 $this->assertSame( $result, $expected ); 138 148 139 149 // Should filter invalid image size. 140 $result = $widget->update( array( 141 'size' => 'big league', 142 ), $instance ); 150 $result = $widget->update( 151 array( 152 'size' => 'big league', 153 ), $instance 154 ); 143 155 $this->assertSame( $result, $instance ); 144 156 … … 147 159 'width' => 300, 148 160 ); 149 $result = $widget->update( $expected, $instance );161 $result = $widget->update( $expected, $instance ); 150 162 $this->assertSame( $result, $expected ); 151 163 152 164 // Should filter invalid image width. 153 $result = $widget->update( array( 154 'width' => 'wide', 155 ), $instance ); 165 $result = $widget->update( 166 array( 167 'width' => 'wide', 168 ), $instance 169 ); 156 170 $this->assertSame( $result, $instance ); 157 171 … … 160 174 'height' => 200, 161 175 ); 162 $result = $widget->update( $expected, $instance );176 $result = $widget->update( $expected, $instance ); 163 177 $this->assertSame( $result, $expected ); 164 178 165 179 // Should filter invalid image height. 166 $result = $widget->update( array( 167 'height' => 'high', 168 ), $instance ); 180 $result = $widget->update( 181 array( 182 'height' => 'high', 183 ), $instance 184 ); 169 185 $this->assertSame( $result, $instance ); 170 186 … … 173 189 'caption' => 'A caption with <a href="#">link</a>', 174 190 ); 175 $result = $widget->update( $expected, $instance );191 $result = $widget->update( $expected, $instance ); 176 192 $this->assertSame( $result, $expected ); 177 193 178 194 // Should filter invalid image caption. 179 $result = $widget->update( array( 180 'caption' => '"><i onload="alert(\'hello\')" />', 181 ), $instance ); 182 $this->assertSame( $result, array( 183 'caption' => '"><i />', 184 ) ); 195 $result = $widget->update( 196 array( 197 'caption' => '"><i onload="alert(\'hello\')" />', 198 ), $instance 199 ); 200 $this->assertSame( 201 $result, array( 202 'caption' => '"><i />', 203 ) 204 ); 185 205 186 206 // Should return valid alt text. … … 188 208 'alt' => 'A water tower', 189 209 ); 190 $result = $widget->update( $expected, $instance );210 $result = $widget->update( $expected, $instance ); 191 211 $this->assertSame( $result, $expected ); 192 212 193 213 // Should filter invalid alt text. 194 $result = $widget->update( array( 195 'alt' => '"><i onload="alert(\'hello\')" />', 196 ), $instance ); 197 $this->assertSame( $result, array( 198 'alt' => '">', 199 ) ); 214 $result = $widget->update( 215 array( 216 'alt' => '"><i onload="alert(\'hello\')" />', 217 ), $instance 218 ); 219 $this->assertSame( 220 $result, array( 221 'alt' => '">', 222 ) 223 ); 200 224 201 225 // Should return valid link type. … … 203 227 'link_type' => 'file', 204 228 ); 205 $result = $widget->update( $expected, $instance );229 $result = $widget->update( $expected, $instance ); 206 230 $this->assertSame( $result, $expected ); 207 231 208 232 // Should filter invalid link type. 209 $result = $widget->update( array( 210 'link_type' => 'interesting', 211 ), $instance ); 233 $result = $widget->update( 234 array( 235 'link_type' => 'interesting', 236 ), $instance 237 ); 212 238 $this->assertSame( $result, $instance ); 213 239 … … 216 242 'link_url' => 'https://example.org', 217 243 ); 218 $result = $widget->update( $expected, $instance );244 $result = $widget->update( $expected, $instance ); 219 245 $this->assertSame( $result, $expected ); 220 246 221 247 // Should filter invalid link url. 222 $result = $widget->update( array( 223 'link_url' => 'not_a_url', 224 ), $instance ); 248 $result = $widget->update( 249 array( 250 'link_url' => 'not_a_url', 251 ), $instance 252 ); 225 253 $this->assertNotSame( $result, $instance ); 226 254 $this->assertStringStartsWith( 'http://', $result['link_url'] ); … … 230 258 'image_classes' => 'A water tower', 231 259 ); 232 $result = $widget->update( $expected, $instance );260 $result = $widget->update( $expected, $instance ); 233 261 $this->assertSame( $result, $expected ); 234 262 235 263 // Should filter invalid image classes. 236 $result = $widget->update( array( 237 'image_classes' => '"><i onload="alert(\'hello\')" />', 238 ), $instance ); 239 $this->assertSame( $result, array( 240 'image_classes' => 'i onloadalerthello', 241 ) ); 264 $result = $widget->update( 265 array( 266 'image_classes' => '"><i onload="alert(\'hello\')" />', 267 ), $instance 268 ); 269 $this->assertSame( 270 $result, array( 271 'image_classes' => 'i onloadalerthello', 272 ) 273 ); 242 274 243 275 // Should return valid link classes. … … 245 277 'link_classes' => 'A water tower', 246 278 ); 247 $result = $widget->update( $expected, $instance );279 $result = $widget->update( $expected, $instance ); 248 280 $this->assertSame( $result, $expected ); 249 281 250 282 // Should filter invalid link classes. 251 $result = $widget->update( array( 252 'link_classes' => '"><i onload="alert(\'hello\')" />', 253 ), $instance ); 254 $this->assertSame( $result, array( 255 'link_classes' => 'i onloadalerthello', 256 ) ); 283 $result = $widget->update( 284 array( 285 'link_classes' => '"><i onload="alert(\'hello\')" />', 286 ), $instance 287 ); 288 $this->assertSame( 289 $result, array( 290 'link_classes' => 'i onloadalerthello', 291 ) 292 ); 257 293 258 294 // Should return valid rel text. … … 260 296 'link_rel' => 'previous', 261 297 ); 262 $result = $widget->update( $expected, $instance );298 $result = $widget->update( $expected, $instance ); 263 299 $this->assertSame( $result, $expected ); 264 300 265 301 // Should filter invalid rel text. 266 $result = $widget->update( array( 267 'link_rel' => '"><i onload="alert(\'hello\')" />', 268 ), $instance ); 269 $this->assertSame( $result, array( 270 'link_rel' => 'i onloadalerthello', 271 ) ); 302 $result = $widget->update( 303 array( 304 'link_rel' => '"><i onload="alert(\'hello\')" />', 305 ), $instance 306 ); 307 $this->assertSame( 308 $result, array( 309 'link_rel' => 'i onloadalerthello', 310 ) 311 ); 272 312 273 313 // Should return valid link target. … … 275 315 'link_target_blank' => false, 276 316 ); 277 $result = $widget->update( $expected, $instance );317 $result = $widget->update( $expected, $instance ); 278 318 $this->assertSame( $result, $expected ); 279 319 280 320 // Should filter invalid link target. 281 $result = $widget->update( array( 282 'link_target_blank' => 'top', 283 ), $instance ); 321 $result = $widget->update( 322 array( 323 'link_target_blank' => 'top', 324 ), $instance 325 ); 284 326 $this->assertSame( $result, $instance ); 285 327 … … 288 330 'image_title' => 'What a title', 289 331 ); 290 $result = $widget->update( $expected, $instance );332 $result = $widget->update( $expected, $instance ); 291 333 $this->assertSame( $result, $expected ); 292 334 293 335 // Should filter invalid image title. 294 $result = $widget->update( array( 295 'image_title' => '<h1>W00t!</h1>', 296 ), $instance ); 336 $result = $widget->update( 337 array( 338 'image_title' => '<h1>W00t!</h1>', 339 ), $instance 340 ); 297 341 $this->assertNotSame( $result, $instance ); 298 342 299 343 // Should filter invalid key. 300 $result = $widget->update( array( 301 'imaginary_key' => 'value', 302 ), $instance ); 344 $result = $widget->update( 345 array( 346 'imaginary_key' => 'value', 347 ), $instance 348 ); 303 349 $this->assertSame( $result, $instance ); 304 350 } … … 314 360 $test_image = '/tmp/canola.jpg'; 315 361 copy( DIR_TESTDATA . '/images/canola.jpg', $test_image ); 316 $attachment_id = self::factory()->attachment->create_object( array( 317 'file' => $test_image, 318 'post_parent' => 0, 319 'post_mime_type' => 'image/jpeg', 320 'post_title' => 'Canola', 321 ) ); 362 $attachment_id = self::factory()->attachment->create_object( 363 array( 364 'file' => $test_image, 365 'post_parent' => 0, 366 'post_mime_type' => 'image/jpeg', 367 'post_title' => 'Canola', 368 ) 369 ); 322 370 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $test_image ) ); 323 371 … … 330 378 // Should be empty when there is an invalid attachment_id. 331 379 ob_start(); 332 $widget->render_media( array( 333 'attachment_id' => 666, 334 ) ); 380 $widget->render_media( 381 array( 382 'attachment_id' => 666, 383 ) 384 ); 335 385 $output = ob_get_clean(); 336 386 $this->assertEmpty( $output ); 337 387 338 388 ob_start(); 339 $widget->render_media( array( 340 'attachment_id' => $attachment_id, 341 ) ); 389 $widget->render_media( 390 array( 391 'attachment_id' => $attachment_id, 392 ) 393 ); 342 394 $output = ob_get_clean(); 343 395 … … 350 402 351 403 ob_start(); 352 $widget->render_media( array( 353 'attachment_id' => $attachment_id, 354 'image_title' => 'Custom Title', 355 'image_classes' => 'custom-class', 356 'alt' => 'A flower', 357 'size' => 'custom', 358 'width' => 100, 359 'height' => 100, 360 ) ); 404 $widget->render_media( 405 array( 406 'attachment_id' => $attachment_id, 407 'image_title' => 'Custom Title', 408 'image_classes' => 'custom-class', 409 'alt' => 'A flower', 410 'size' => 'custom', 411 'width' => 100, 412 'height' => 100, 413 ) 414 ); 361 415 $output = ob_get_clean(); 362 416 … … 371 425 // Embeded images. 372 426 ob_start(); 373 $widget->render_media( array( 374 'attachment_id' => null, 375 'caption' => 'With caption', 376 'height' => 100, 377 'link_type' => 'file', 378 'url' => 'http://example.org/url/to/image.jpg', 379 'width' => 100, 380 ) ); 427 $widget->render_media( 428 array( 429 'attachment_id' => null, 430 'caption' => 'With caption', 431 'height' => 100, 432 'link_type' => 'file', 433 'url' => 'http://example.org/url/to/image.jpg', 434 'width' => 100, 435 ) 436 ); 381 437 $output = ob_get_clean(); 382 438 … … 386 442 // Link settings. 387 443 ob_start(); 388 $widget->render_media( array( 389 'attachment_id' => $attachment_id, 390 'link_type' => 'file', 391 ) ); 444 $widget->render_media( 445 array( 446 'attachment_id' => $attachment_id, 447 'link_type' => 'file', 448 ) 449 ); 392 450 $output = ob_get_clean(); 393 451 … … 400 458 401 459 ob_start(); 402 $widget->render_media( array( 403 'attachment_id' => $attachment_id, 404 'link_type' => 'post', 405 'link_classes' => 'custom-link-class', 406 'link_rel' => 'attachment', 407 'link_target_blank' => false, 408 ) ); 460 $widget->render_media( 461 array( 462 'attachment_id' => $attachment_id, 463 'link_type' => 'post', 464 'link_classes' => 'custom-link-class', 465 'link_rel' => 'attachment', 466 'link_target_blank' => false, 467 ) 468 ); 409 469 $output = ob_get_clean(); 410 470 … … 415 475 416 476 ob_start(); 417 $widget->render_media( array( 418 'attachment_id' => $attachment_id, 419 'link_type' => 'custom', 420 'link_url' => 'https://example.org', 421 'link_target_blank' => true, 422 ) ); 477 $widget->render_media( 478 array( 479 'attachment_id' => $attachment_id, 480 'link_type' => 'custom', 481 'link_url' => 'https://example.org', 482 'link_target_blank' => true, 483 ) 484 ); 423 485 $output = ob_get_clean(); 424 486 … … 427 489 428 490 // Populate caption in attachment. 429 wp_update_post( array( 430 'ID' => $attachment_id, 431 'post_excerpt' => 'Default caption', 432 ) ); 491 wp_update_post( 492 array( 493 'ID' => $attachment_id, 494 'post_excerpt' => 'Default caption', 495 ) 496 ); 433 497 434 498 // If no caption is supplied, then the default is '', and so the caption will not be displayed. 435 499 ob_start(); 436 $widget->render_media( array( 437 'attachment_id' => $attachment_id, 438 ) ); 500 $widget->render_media( 501 array( 502 'attachment_id' => $attachment_id, 503 ) 504 ); 439 505 $output = ob_get_clean(); 440 506 $this->assertNotContains( 'wp-caption', $output ); … … 443 509 // If the caption is explicitly null, then the caption of the underlying attachment will be displayed. 444 510 ob_start(); 445 $widget->render_media( array( 446 'attachment_id' => $attachment_id, 447 'caption' => null, 448 ) ); 511 $widget->render_media( 512 array( 513 'attachment_id' => $attachment_id, 514 'caption' => null, 515 ) 516 ); 449 517 $output = ob_get_clean(); 450 518 $this->assertContains( 'class="wp-caption alignnone"', $output ); … … 453 521 // If caption is provided, then it will be displayed. 454 522 ob_start(); 455 $widget->render_media( array( 456 'attachment_id' => $attachment_id, 457 'caption' => 'Custom caption', 458 ) ); 523 $widget->render_media( 524 array( 525 'attachment_id' => $attachment_id, 526 'caption' => 'Custom caption', 527 ) 528 ); 459 529 $output = ob_get_clean(); 460 530 $this->assertContains( 'class="wp-caption alignnone"', $output );
Note: See TracChangeset
for help on using the changeset viewer.