attachment->create_upload_object( $filename ); $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' ); foreach ( $post_statuses as $post_status ) { $date = ''; if ( 'future' === $post_status ) { strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) ); } self::$post_ids[ $post_status ] = $factory->post->create( array( 'post_status' => 'trash' === $post_status ? 'publish' : $post_status, 'post_date' => $date, 'post_name' => "$post_status-post", ) ); // Attachments without media. self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object( array( 'post_parent' => self::$post_ids[ $post_status ], 'post_status' => 'inherit', 'post_name' => "$post_status-attachment", 'post_date' => $date, ) ); } // Trash the trash post. wp_trash_post( self::$post_ids['trash'] ); } public static function wpTearDownAfterClass() { $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes; } public static function tearDownAfterClass() { wp_delete_post( self::$large_id, true ); parent::tearDownAfterClass(); } function setUp() { parent::setUp(); $this->caption = 'A simple caption.'; $this->alternate_caption = 'Alternate caption.'; $this->html_content = <<bolded caption with a link. CAP; $this->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_meta = array( 'width' => 100, 'height' => 100, 'sizes' => '', ); } function test_img_caption_shortcode_added() { global $shortcode_tags; $this->assertSame( 'img_caption_shortcode', $shortcode_tags['caption'] ); $this->assertSame( 'img_caption_shortcode', $shortcode_tags['wp_caption'] ); } function test_img_caption_shortcode_with_empty_params() { $result = img_caption_shortcode( array() ); $this->assertNull( $result ); } /** * @ticket 33981 */ function test_img_caption_shortcode_with_empty_params_but_content() { $result = img_caption_shortcode( array(), $this->caption ); $this->assertSame( $this->caption, $result ); } /** * @ticket 33981 */ function test_img_caption_shortcode_short_circuit_filter() { add_filter( 'img_caption_shortcode', array( $this, '_return_alt_caption' ) ); $result = img_caption_shortcode( array(), $this->caption ); $this->assertSame( $this->alternate_caption, $result ); } /** * Filter used in test_img_caption_shortcode_short_circuit_filter() */ function _return_alt_caption() { return $this->alternate_caption; } /** * @ticket 33981 */ function test_img_caption_shortcode_empty_width() { $result = img_caption_shortcode( array( 'width' => 0, ), $this->caption ); $this->assertSame( $this->caption, $result ); } /** * @ticket 33981 */ function test_img_caption_shortcode_empty_caption() { $result = img_caption_shortcode( array( 'caption' => '', ) ); $this->assertNull( $result ); } /** * @ticket 33981 */ function test_img_caption_shortcode_empty_caption_and_content() { $result = img_caption_shortcode( array( 'caption' => '', ), $this->caption ); $this->assertSame( $this->caption, $result ); } function test_img_caption_shortcode_with_old_format() { $result = img_caption_shortcode( array( 'width' => 20, 'caption' => $this->caption, ) ); $this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) ); $this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) ); $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) ); if ( current_theme_supports( 'html5', 'caption' ) ) { $this->assertSame( 1, preg_match_all( '/width: 20/', $result, $_r ) ); } else { $this->assertSame( 1, preg_match_all( '/width: 30/', $result, $_r ) ); } } 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->assertSame( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) ); $this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) ); $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) ); } function test_img_caption_shortcode_with_old_format_and_class() { $result = img_caption_shortcode( array( 'width' => 20, 'class' => 'some-class another-class', 'caption' => $this->caption, ) ); $this->assertSame( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $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 ); $this->assertSame( 1, preg_match_all( "~{$our_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 ); $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); $this->assertSame( 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->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); $this->assertSame( 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->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) ); $this->assertSame( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) ); } /** * @ticket 34595 */ function test_img_caption_shortcode_has_aria_describedby() { $result = img_caption_shortcode( array( 'width' => 20, 'id' => 'myId', ), $this->img_content . $this->html_content ); $this->assertSame( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) ); } function test_add_remove_oembed_provider() { wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' ); $this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) ); $this->assertFalse( wp_oembed_remove_provider( 'http://foo.bar/*' ) ); } /** * @ticket 23776 */ function test_autoembed_empty() { global $wp_embed; $content = ''; $result = $wp_embed->autoembed( $content ); $this->assertSame( $content, $result ); } /** * @ticket 23776 */ function test_autoembed_no_paragraphs_around_urls() { global $wp_embed; $content = <<http://some.link/ http://some.other.link/ EOF; $result = $wp_embed->autoembed( $content ); $this->assertSame( $content, $result ); } function data_autoembed() { return array( // Should embed. array( 'https://w.org', '[embed]', ), array( 'test https://w.org test', 'test [embed] test', ), array( '

https://w.org

', '

[embed]

', ), array( '

https://w.org

', '

[embed]

', ), array( '

test https://w.org test

', '

test [embed] test

', ), array( '

https://w.org

', '

[embed]

', ), // Should NOT embed. array( 'test https://w.org

', ), array( 'https://w.org', ), array( '
https://w.org

', ), array( ' https://w.org', ), ); } /** * @dataProvider data_autoembed */ function test_autoembed( $content, $result = null ) { $wp_embed = new Test_Autoembed; $this->assertSame( $wp_embed->autoembed( $content ), $result ? $result : $content ); } function test_wp_prepare_attachment_for_js() { // Attachment without media. $id = wp_insert_attachment( array( 'post_status' => 'publish', 'post_title' => 'Prepare', 'post_content_filtered' => 'Prepare', 'post_type' => 'post', ) ); $post = get_post( $id ); $prepped = wp_prepare_attachment_for_js( $post ); $this->assertInternalType( 'array', $prepped ); $this->assertSame( 0, $prepped['uploadedTo'] ); $this->assertSame( '', $prepped['mime'] ); $this->assertSame( '', $prepped['type'] ); $this->assertSame( '', $prepped['subtype'] ); // #21963, there will be a GUID always, so there will be a URL. $this->assertNotEquals( '', $prepped['url'] ); $this->assertSame( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] ); // Fake a mime. $post->post_mime_type = 'image/jpeg'; $prepped = wp_prepare_attachment_for_js( $post ); $this->assertSame( 'image/jpeg', $prepped['mime'] ); $this->assertSame( 'image', $prepped['type'] ); $this->assertSame( 'jpeg', $prepped['subtype'] ); // Fake a mime without a slash. See #WP22532. $post->post_mime_type = 'image'; $prepped = wp_prepare_attachment_for_js( $post ); $this->assertSame( 'image', $prepped['mime'] ); $this->assertSame( 'image', $prepped['type'] ); $this->assertSame( '', $prepped['subtype'] ); // Test that if author is not found, we return "(no author)" as `display_name`. // The previously used test post contains no author, so we can reuse it. $this->assertSame( '(no author)', $prepped['authorName'] ); // Test that if author has HTML entities in display_name, they're decoded correctly. $html_entity_author = self::factory()->user->create( array( 'display_name' => 'You & Me', ) ); $post->post_author = $html_entity_author; $prepped = wp_prepare_attachment_for_js( $post ); $this->assertSame( 'You & Me', $prepped['authorName'] ); } /** * @ticket 38965 */ function test_wp_prepare_attachment_for_js_without_image_sizes() { // Create the attachement post. $id = wp_insert_attachment( array( 'post_title' => 'Attachment Title', 'post_type' => 'attachment', 'post_parent' => 0, 'post_mime_type' => 'image/jpeg', 'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg', ) ); // Add attachment metadata without sizes. wp_update_attachment_metadata( $id, array( 'width' => 50, 'height' => 50, 'file' => 'test-image.jpg', ) ); $prepped = wp_prepare_attachment_for_js( get_post( $id ) ); $this->assertTrue( isset( $prepped['sizes'] ) ); } /** * @ticket 19067 * @expectedDeprecated wp_convert_bytes_to_hr */ function test_wp_convert_bytes_to_hr() { $kb = 1024; $mb = $kb * 1024; $gb = $mb * 1024; $tb = $gb * 1024; // Test if boundaries are correct. $this->assertSame( '1TB', wp_convert_bytes_to_hr( $tb ) ); $this->assertSame( '1GB', wp_convert_bytes_to_hr( $gb ) ); $this->assertSame( '1MB', wp_convert_bytes_to_hr( $mb ) ); $this->assertSame( '1KB', wp_convert_bytes_to_hr( $kb ) ); $this->assertSame( '1 TB', size_format( $tb ) ); $this->assertSame( '1 GB', size_format( $gb ) ); $this->assertSame( '1 MB', size_format( $mb ) ); $this->assertSame( '1 KB', size_format( $kb ) ); // Now some values around. $hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb ); $this->assertEqualsWithDelta( 1.50000095367, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); $hr = wp_convert_bytes_to_hr( $tb - $mb - $kb ); $this->assertEqualsWithDelta( 1023.99902248, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); $hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb ); $this->assertEqualsWithDelta( 1.5009765625, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); $hr = wp_convert_bytes_to_hr( $gb - $mb - $kb ); $this->assertEqualsWithDelta( 1022.99902344, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' ); // Edge. $this->assertSame( '-1B', wp_convert_bytes_to_hr( -1 ) ); $this->assertSame( '0B', wp_convert_bytes_to_hr( 0 ) ); } /** * @ticket 22960 */ function test_get_attached_images() { $post_id = self::factory()->post->create(); $attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array( 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $images = get_attached_media( 'image', $post_id ); $this->assertEquals( $images, array( $attachment_id => get_post( $attachment_id ) ) ); } /** * @ticket 22960 */ function test_post_galleries_images() { $ids1 = array(); $ids1_srcs = array(); foreach ( range( 1, 3 ) as $i ) { $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids1[] = $attachment_id; $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; } $ids2 = array(); $ids2_srcs = array(); foreach ( range( 4, 6 ) as $i ) { $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids2[] = $attachment_id; $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; } $ids1_joined = implode( ',', $ids1 ); $ids2_joined = implode( ',', $ids2 ); $blob = <<post->create( array( 'post_content' => $blob ) ); $srcs = get_post_galleries_images( $post_id ); $this->assertSame( $srcs, array( $ids1_srcs, $ids2_srcs ) ); } /** * @ticket 39304 */ function test_post_galleries_images_without_global_post() { // Set up an unattached image. $this->factory->attachment->create_object( array( 'file' => 'test.jpg', 'post_parent' => 0, 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $post_id = $this->factory->post->create( array( 'post_content' => '[gallery]', ) ); $galleries = get_post_galleries( $post_id, false ); $this->assertEmpty( $galleries[0]['src'] ); } /** * @ticket 39304 */ function test_post_galleries_ignores_global_post() { $global_post_id = $this->factory->post->create( array( 'post_content' => 'Global Post', ) ); $post_id = $this->factory->post->create( array( 'post_content' => '[gallery]', ) ); $this->factory->attachment->create_object( array( 'file' => 'test.jpg', 'post_parent' => $post_id, 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $expected_srcs = array( 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg', ); // Set the global $post context to the other post. $GLOBALS['post'] = get_post( $global_post_id ); $galleries = get_post_galleries( $post_id, false ); $this->assertNotEmpty( $galleries[0]['src'] ); $this->assertSame( $galleries[0]['src'], $expected_srcs ); } /** * @ticket 39304 */ function test_post_galleries_respects_id_attrs() { $post_id = $this->factory->post->create( array( 'post_content' => 'No gallery defined', ) ); $post_id_two = $this->factory->post->create( array( 'post_content' => "[gallery id='$post_id']", ) ); $this->factory->attachment->create_object( array( 'file' => 'test.jpg', 'post_parent' => $post_id, 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $expected_srcs = array( 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg', ); $galleries = get_post_galleries( $post_id_two, false ); // Set the global $post context. $GLOBALS['post'] = get_post( $post_id_two ); $galleries_with_global_context = get_post_galleries( $post_id_two, false ); // Check that the global post state doesn't affect the results. $this->assertSame( $galleries, $galleries_with_global_context ); $this->assertNotEmpty( $galleries[0]['src'] ); $this->assertSame( $galleries[0]['src'], $expected_srcs ); } /** * @ticket 22960 */ function test_post_gallery_images() { $ids1 = array(); $ids1_srcs = array(); foreach ( range( 1, 3 ) as $i ) { $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids1[] = $attachment_id; $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; } $ids2 = array(); $ids2_srcs = array(); foreach ( range( 4, 6 ) as $i ) { $attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array( 'post_mime_type' => 'image/jpeg', 'post_type' => 'attachment', ) ); $metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids2[] = $attachment_id; $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; } $ids1_joined = implode( ',', $ids1 ); $ids2_joined = implode( ',', $ids2 ); $blob = <<post->create( array( 'post_content' => $blob ) ); $srcs = get_post_gallery_images( $post_id ); $this->assertSame( $srcs, $ids1_srcs ); } function test_get_media_embedded_in_content() { $object = << OBJ; $embed = << EMBED; $iframe = <<'; $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); $lazy_img_xhtml = wp_img_tag_add_loading_attr( $img_xhtml, 'test' ); $lazy_img_html5 = wp_img_tag_add_loading_attr( $img_html5, 'test' ); // The following should not be modified because there already is a 'loading' attribute. $img_eager = str_replace( ' />', ' loading="eager" />', $img ); $content = '

Image, standard.

%1$s

Image, XHTML 1.0 style (no space before the closing slash).

%2$s

Image, HTML 5.0 style.

%3$s

Image, with pre-existing "loading" attribute. Should not be modified.

%4$s

Image, without dimension attributes. Should not be modified.

%5$s

Iframe, standard. Should not be modified.

%6$s'; $content_unfiltered = sprintf( $content, $img, $img_xhtml, $img_html5, $img_eager, $img_no_width_height, $iframe ); $content_filtered = sprintf( $content, $lazy_img, $lazy_img_xhtml, $lazy_img_html5, $img_eager, $img_no_width_height, $iframe ); // Do not add width, height, srcset, and sizes. add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); } /** * @ticket 44427 */ function test_wp_filter_content_tags_loading_lazy_opted_in() { $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); $lazy_img = wp_img_tag_add_loading_attr( $img, 'test' ); $content = '

Image, standard.

%1$s'; $content_unfiltered = sprintf( $content, $img ); $content_filtered = sprintf( $content, $lazy_img ); // Do not add srcset and sizes while testing. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); // Enable globally for all tags. add_filter( 'wp_lazy_loading_enabled', '__return_true' ); $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); remove_filter( 'wp_lazy_loading_enabled', '__return_true' ); remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); } /** * @ticket 44427 */ function test_wp_filter_content_tags_loading_lazy_opted_out() { $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); $content = '

Image, standard.

%1$s'; $content = sprintf( $content, $img ); // Do not add srcset and sizes while testing. add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); // Disable globally for all tags. add_filter( 'wp_lazy_loading_enabled', '__return_false' ); $this->assertSame( $content, wp_filter_content_tags( $content ) ); remove_filter( 'wp_lazy_loading_enabled', '__return_false' ); remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); } /** * @ticket 44427 * @ticket 50367 */ function test_wp_img_tag_add_loading_attr() { $img = ' width='; $img = wp_img_tag_add_loading_attr( $img, 'test' ); $this->assertContains( ' loading="lazy"', $img ); } /** * @ticket 44427 * @ticket 50367 */ function test_wp_img_tag_add_loading_attr_without_src() { $img = ' width='; $img = wp_img_tag_add_loading_attr( $img, 'test' ); $this->assertNotContains( ' loading=', $img ); } /** * @ticket 44427 * @ticket 50367 */ function test_wp_img_tag_add_loading_attr_with_single_quotes() { $img = " width="; $img = wp_img_tag_add_loading_attr( $img, 'test' ); $this->assertNotContains( ' loading=', $img ); // Test specifically that the attribute is not there with double-quotes, // to avoid regressions. $this->assertNotContains( ' loading="lazy"', $img ); } /** * @ticket 44427 * @ticket 50425 */ function test_wp_img_tag_add_loading_attr_opt_out() { $img = ' width='; add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); $this->assertNotContains( ' loading=', $img ); } /** * @ticket 44427 * @ticket 50425 */ function test_wp_get_attachment_image_loading() { $img = wp_get_attachment_image( self::$large_id ); $this->assertContains( ' loading="lazy"', $img ); } /** * @ticket 44427 * @ticket 50425 */ function test_wp_get_attachment_image_loading_opt_out() { add_filter( 'wp_lazy_loading_enabled', '__return_false' ); $img = wp_get_attachment_image( self::$large_id ); // There should not be any loading attribute in this case. $this->assertNotContains( ' loading=', $img ); } /** * @ticket 44427 * @ticket 50425 */ function test_wp_get_attachment_image_loading_opt_out_individual() { // The default is already tested above, the filter below ensures that // lazy-loading is definitely enabled globally for images. add_filter( 'wp_lazy_loading_enabled', '__return_true' ); $img = wp_get_attachment_image( self::$large_id, 'thumbnail', false, array( 'loading' => false ) ); // There should not be any loading attribute in this case. $this->assertNotContains( ' loading=', $img ); } /** * @ticket 44427 * @ticket 50425 * @dataProvider data_wp_lazy_loading_enabled_tag_name_defaults * * @param string $tag_name Tag name. * @param bool $expected Expected return value. */ function test_wp_lazy_loading_enabled_tag_name_defaults( $tag_name, $expected ) { if ( $expected ) { $this->assertTrue( wp_lazy_loading_enabled( $tag_name, 'the_content' ) ); } else { $this->assertFalse( wp_lazy_loading_enabled( $tag_name, 'the_content' ) ); } } function data_wp_lazy_loading_enabled_tag_name_defaults() { return array( 'img => true' => array( 'img', true ), 'iframe => false' => array( 'iframe', false ), 'arbitrary tag => false' => array( 'blink', false ), ); } /** * @ticket 50425 * @dataProvider data_wp_lazy_loading_enabled_context_defaults * * @param string $context Function context. * @param bool $expected Expected return value. */ function test_wp_lazy_loading_enabled_context_defaults( $context, $expected ) { if ( $expected ) { $this->assertTrue( wp_lazy_loading_enabled( 'img', $context ) ); } else { $this->assertFalse( wp_lazy_loading_enabled( 'img', $context ) ); } } function data_wp_lazy_loading_enabled_context_defaults() { return array( 'wp_get_attachment_image => true' => array( 'wp_get_attachment_image', true ), 'the_content => true' => array( 'the_content', true ), 'the_excerpt => true' => array( 'the_excerpt', true ), 'widget_text_content => true' => array( 'widget_text_content', true ), 'get_avatar => true' => array( 'get_avatar', true ), 'arbitrary context => true' => array( 'something_completely_arbitrary', true ), ); } /** * @ticket 50543 */ function test_wp_image_file_matches_image_meta() { $image_meta = wp_get_attachment_metadata( self::$large_id ); $image_src_full = wp_get_attachment_image_url( self::$large_id, 'full' ); $image_src_medium = wp_get_attachment_image_url( self::$large_id, 'medium' ); $this->assertTrue( wp_image_file_matches_image_meta( $image_src_full, $image_meta ) ); $this->assertTrue( wp_image_file_matches_image_meta( $image_src_medium, $image_meta ) ); } /** * @ticket 50543 */ function test_wp_image_file_matches_image_meta_no_subsizes() { $image_meta = wp_get_attachment_metadata( self::$large_id ); $image_src = wp_get_attachment_image_url( self::$large_id, 'full' ); $image_meta['sizes'] = array(); $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); } /** * @ticket 50543 */ function test_wp_image_file_matches_image_meta_invalid_meta() { $image_meta = ''; // Attachment is not an image. $image_src = $this->img_url; $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); } /** * @ticket 50543 */ function test_wp_image_file_matches_image_meta_different_meta() { $image_meta = wp_get_attachment_metadata( self::$large_id ); $image_src = $this->img_url; // Different image. $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); } /** * @ticket 50543 */ function test_wp_image_file_matches_image_meta_original_image() { $image_meta = wp_get_attachment_metadata( self::$large_id ); $image_src = wp_get_original_image_url( self::$large_id ); $this->assertTrue( wp_image_file_matches_image_meta( $image_src, $image_meta ) ); } /** * @ticket 22101 */ function test_gallery_shortcode_when_is_feed_true() { $this->go_to( '/?feed=rss2' ); // Default: Links to image attachment page URL. $actual = gallery_shortcode( array( 'ids' => self::$large_id, ) ); $this->assertContains( '?attachment_id=', $actual ); // File: Links to image file URL. $actual = gallery_shortcode( array( 'ids' => self::$large_id, 'link' => 'file', ) ); $this->assertSame( 2, substr_count( $actual, '.jpg' ) ); // None: Does not link. $actual = gallery_shortcode( array( 'ids' => self::$large_id, 'link' => 'none', ) ); $this->assertNotContains( 'set_permalink_structure( '/%postname%' ); $post = get_post( self::$post_ids[ $post_key ] ); /* * The dataProvider runs before the fixures are set up, therefore the * post object IDs are placeholders that needs to be replaced. */ $expected = home_url( str_replace( '%ID%', $post->ID, $expected ) ); $this->assertSame( $expected, get_permalink( $post ) ); $this->go_to( get_permalink( $post ) ); $this->assertSame( $expected_404, is_404() ); } /** * Data provider for test_attachment_permalinks_based_on_parent_status(). * * @return array[] { * @type string $post_key Post as keyed in the shared fixture array. * @type string $expected Expected result. * $type bool $expected_404 Whether the page is expected to return a 404 result. * } */ function data_attachment_permalinks_based_on_parent_status() { return array( array( 'draft-attachment', '/?attachment_id=%ID%', true ), array( 'publish-attachment', '/publish-post/publish-attachment', false ), array( 'future-attachment', '/future-post/future-attachment', false ), array( 'auto-draft-attachment', '/?attachment_id=%ID%', true ), array( 'trash-attachment', '/?attachment_id=%ID%', false ), ); } } /** * Helper class for `test_autoembed`. */ class Test_Autoembed extends WP_Embed { public function shortcode( $attr, $url = '' ) { return '[embed]'; } }