From 9cb4234c3ff36e147d4e29bfa7cda3a20e8660d9 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 12 Sep 2013 04:55:24 +0000 Subject: [PATCH] * Fix the horrendous whitespace in `tests/media.php` * Suppress the deprecated function notice for `wp_convert_bytes_to_hr()` * Add assertions for `size_format()` See #25282. git-svn-id: https://develop.svn.wordpress.org/trunk@25382 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/media.php | 189 +++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 84 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 9066fdc5ed..f1550c4131 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -6,107 +6,123 @@ */ class Tests_Media extends WP_UnitTestCase { - function setUp() { - parent::setUp(); - $this->caption = 'A simple caption.'; - $this->html_content = <<caption = 'A simple caption.'; + $this->html_content = <<bolded caption with a link. CAP; - $this->img_content = <<img_content = << CAP; - $this->img_name = 'image.jpg'; - $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name; - $this->img_html = ''; - $this->img_dimensions = array( 'width' => 100, 'height' => 100 ); - } + $this->img_name = 'image.jpg'; + $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name; + $this->img_html = ''; + $this->img_dimensions = array( 'width' => 100, 'height' => 100 ); + add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); + } - function test_img_caption_shortcode_added() { - global $shortcode_tags; - $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['caption'] ); - $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['wp_caption'] ); - } + function tearDown() { + parent::tearDown(); + remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); + } - function test_img_caption_shortcode_with_empty_params() { - $result = img_caption_shortcode( array() ); - $this->assertNull( $result ); - } + function deprecated_function_run( $function ) { + if ( in_array( $function, array( 'wp_convert_bytes_to_hr' ) ) ) + add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); + } - function test_img_caption_shortcode_with_bad_attr() { - $result = img_caption_shortcode( array(), 'content' ); - $this->assertEquals( 'content', 'content' ); - } + function deprecated_function_trigger_error() { + remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) ); + return false; + } - function test_img_caption_shortcode_with_old_format() { - $result = img_caption_shortcode( - array( 'width' => 20, 'caption' => $this->caption ) - ); - $this->assertEquals( 2, preg_match_all( '/wp-caption/', $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( '/alignnone/', $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( "/width: 30/", $result, $_r ) ); - } + function test_img_caption_shortcode_added() { + global $shortcode_tags; + $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['caption'] ); + $this->assertEquals( 'img_caption_shortcode', $shortcode_tags['wp_caption'] ); + } - function test_img_caption_shortcode_with_old_format_id_and_align() { - $result = img_caption_shortcode( - array( - 'width' => 20, - 'caption' => $this->caption, - 'id' => '"myId', - 'align' => '&myAlignment' - ) - ); - $this->assertEquals( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( '/id=""myId"/', $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) ); - } + function test_img_caption_shortcode_with_empty_params() { + $result = img_caption_shortcode( array() ); + $this->assertNull( $result ); + } - function test_new_img_caption_shortcode_with_html_caption() { - $result = img_caption_shortcode( - array( 'width' => 20, 'caption' => $this->html_content ) - ); - $our_preg = preg_quote( $this->html_content ); + function test_img_caption_shortcode_with_bad_attr() { + $result = img_caption_shortcode( array(), 'content' ); + $this->assertEquals( 'content', 'content' ); + } - $this->assertEquals( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) ); - } + function test_img_caption_shortcode_with_old_format() { + $result = img_caption_shortcode( + array( 'width' => 20, 'caption' => $this->caption ) + ); + $this->assertEquals( 2, preg_match_all( '/wp-caption/', $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( '/alignnone/', $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( "/width: 30/", $result, $_r ) ); + } - function test_new_img_caption_shortcode_new_format() { - $result = img_caption_shortcode( - array( 'width' => 20 ), - $this->img_content . $this->html_content - ); - $img_preg = preg_quote( $this->img_content ); - $content_preg = preg_quote( $this->html_content ); + function test_img_caption_shortcode_with_old_format_id_and_align() { + $result = img_caption_shortcode( + array( + 'width' => 20, + 'caption' => $this->caption, + 'id' => '"myId', + 'align' => '&myAlignment' + ) + ); + $this->assertEquals( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( '/id=""myId"/', $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) ); + } - $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) ); - } + function test_new_img_caption_shortcode_with_html_caption() { + $result = img_caption_shortcode( + array( 'width' => 20, 'caption' => $this->html_content ) + ); + $our_preg = preg_quote( $this->html_content ); - function test_new_img_caption_shortcode_new_format_and_linked_image() { - $linked_image = "{$this->img_content}"; - $result = img_caption_shortcode( - array( 'width' => 20 ), - $linked_image . $this->html_content - ); - $img_preg = preg_quote( $linked_image ); - $content_preg = preg_quote( $this->html_content ); + $this->assertEquals( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) ); + } - $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) ); - } + function test_new_img_caption_shortcode_new_format() { + $result = img_caption_shortcode( + array( 'width' => 20 ), + $this->img_content . $this->html_content + ); + $img_preg = preg_quote( $this->img_content ); + $content_preg = preg_quote( $this->html_content ); - function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() { - $linked_image = "{$this->img_content}"; - $result = img_caption_shortcode( - array( 'width' => 20 ), - $linked_image . "\n\n" . $this->html_content - ); - $img_preg = preg_quote( $linked_image ); - $content_preg = preg_quote( $this->html_content ); + $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) ); + } - $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); - $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) ); - } + function test_new_img_caption_shortcode_new_format_and_linked_image() { + $linked_image = "{$this->img_content}"; + $result = img_caption_shortcode( + array( 'width' => 20 ), + $linked_image . $this->html_content + ); + $img_preg = preg_quote( $linked_image ); + $content_preg = preg_quote( $this->html_content ); + + $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) ); + } + + function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() { + $linked_image = "{$this->img_content}"; + $result = img_caption_shortcode( + array( 'width' => 20 ), + $linked_image . "\n\n" . $this->html_content + ); + $img_preg = preg_quote( $linked_image ); + $content_preg = preg_quote( $this->html_content ); + + $this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); + $this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) ); + } function test_add_remove_oembed_provider() { wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' ); @@ -163,6 +179,11 @@ CAP; $this->assertEquals( '1MB', wp_convert_bytes_to_hr( $mb ) ); $this->assertEquals( '1kB', wp_convert_bytes_to_hr( $kb ) ); + $this->assertEquals( '1 TB', size_format( $tb ) ); + $this->assertEquals( '1 GB', size_format( $gb ) ); + $this->assertEquals( '1 MB', size_format( $mb ) ); + $this->assertEquals( '1 kB', size_format( $kb ) ); + // now some values around $hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb ); $this->assertTrue( abs( 1.50000095367 - (float) str_replace( ',', '.', $hr ) ) < 0.0001 );