-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Media & text: Update the image replacement logic #62030
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f84d248
Media & text: Update the image replacement logic when the media is on…
carolinan 0f4a432
Try to fix PHP coding standard issues.
carolinan 0865ac3
Look for the figure with the correct class name instead of any figure…
carolinan 0e67eaa
Merge branch 'trunk' into try/media-text--media-on-right
carolinan 884f732
Update the logic for inserting the featured image
carolinan c326e19
Try to fix PHP CS issues with spacing and the arrays.
carolinan 45b2fb3
Try to fix PHP CS issues with spacing inside the arrays.
carolinan ee92f20
Add a test class for the Media & Text block
carolinan e60ac3d
Merge branch 'trunk' into try/media-text--media-on-right
carolinan c62c46c
Replace the $image_tag variable and remove the CSS class on the img tag
carolinan 912ae47
Revert "Replace the $image_tag variable and remove the CSS class on t…
carolinan 287b230
Add an if statement around the figure tag and the image tag
carolinan ff42bab
Try to fix PHP CS issues
carolinan 117267c
Split test_render_block_core_media_text_featured_image into four tests
carolinan 5efa53b
Update phpunit/blocks/render-block-media-text-test.php
carolinan e0402d5
Merge branch 'trunk' into try/media-text--media-on-right
carolinan 2bb1e79
Move the PHP variables for media size slug and image tag
carolinan 2d44777
Try to fix PHPCS spacing issues
carolinan a0b030d
Merge branch 'trunk' into try/media-text--media-on-right
carolinan dbe8aeb
Add a helper method for wp_query (post data) to reduce code duplication
carolinan 0b03266
PHP coding standards: adjust spacing around equal signs.
carolinan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
/** | ||
* Media & Text block rendering tests. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
*/ | ||
|
||
/** | ||
* Tests for the Media & Text block. | ||
* | ||
* @group blocks | ||
* | ||
* @covers ::render_block_core_media_text | ||
*/ | ||
class Render_Block_MediaText_Test extends WP_UnitTestCase { | ||
|
||
/** | ||
* Post object. | ||
* | ||
* @var WP_Post | ||
*/ | ||
protected static $post; | ||
|
||
/** | ||
* Attachment id. | ||
* | ||
* @var int | ||
*/ | ||
protected static $attachment_id; | ||
|
||
/** | ||
* Setup method. | ||
*/ | ||
public static function wpSetUpBeforeClass() { | ||
self::$post = self::factory()->post->create_and_get(); | ||
$file = DIR_TESTDATA . '/images/canola.jpg'; | ||
|
||
self::$attachment_id = self::factory()->attachment->create_upload_object( | ||
$file, | ||
self::$post->ID, | ||
array( | ||
'post_mime_type' => 'image/jpeg', | ||
) | ||
); | ||
|
||
set_post_thumbnail( self::$post, self::$attachment_id ); | ||
} | ||
|
||
/** | ||
* Tear down method. | ||
*/ | ||
public static function wpTearDownAfterClass() { | ||
wp_delete_post( self::$post->ID, true ); | ||
wp_delete_post( self::$attachment_id, true ); | ||
} | ||
|
||
/** | ||
* Helper method for $wp_query. | ||
*/ | ||
public static function setup_query() { | ||
global $wp_query; | ||
$wp_query->in_the_loop = true; | ||
$wp_query->post = self::$post; | ||
$wp_query->posts = array( self::$post ); | ||
$GLOBALS['post'] = self::$post; | ||
} | ||
|
||
/** | ||
* Test gutenberg_render_block_core_media_text with the featured image on the left. | ||
*/ | ||
public function test_render_block_core_media_text_featured_image() { | ||
$this->setup_query(); | ||
|
||
$content = '<div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"></figure><div class="wp-block-media-text__content"><p></p></div></div>'; | ||
|
||
// Assert that the rendered block contains the featured image. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
); | ||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( '<img alt="" src="' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . '"', $rendered ); | ||
|
||
// Assert that the rendered block contains the featured image as the background-image url, | ||
// and not the image element, when image fill is true. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
'imageFill' => true, | ||
); | ||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( 'background-image:url(' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . ')', $rendered ); | ||
$this->assertStringNotContainsString( '<img', $rendered ); | ||
} | ||
|
||
/** | ||
* Test gutenberg_render_block_core_media_text with the featured image on the left, | ||
* and a second media & text block nested inside the content area. | ||
*/ | ||
public function test_render_block_core_media_text_featured_image_nested() { | ||
$this->setup_query(); | ||
$content = '<div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"></figure><div class="wp-block-media-text__content"><div class="wp-block-media-text is-stacked-on-mobile"><figure class="wp-block-media-text__media"></figure><div class="wp-block-media-text__content"><p></p></div></div></div></div>'; | ||
|
||
// Assert that the rendered block contains the featured image. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
); | ||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( '<img alt="" src="' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . '"', $rendered ); | ||
|
||
// Assert that the rendered block contains the featured image as the background-image url, | ||
// and not the image element, when image fill is true. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
'imageFill' => true, | ||
); | ||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( 'background-image:url(' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . ')', $rendered ); | ||
$this->assertStringNotContainsString( '<img', $rendered ); | ||
} | ||
|
||
/** | ||
* Test gutenberg_render_block_core_media_text with the featured image on the right. | ||
*/ | ||
public function test_render_block_core_media_text_featured_image_media_on_right() { | ||
$this->setup_query(); | ||
|
||
$content = '<div class="wp-block-media-text has-media-on-the-right is-stacked-on-mobile"><div class="wp-block-media-text__content"><p></p></div><figure class="wp-block-media-text__media"></figure></div>'; | ||
|
||
// Assert that the rendered block contains the featured image when media is on the right. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
'mediaPosition' => 'right', | ||
); | ||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( '<img alt="" src="' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . '"', $rendered ); | ||
|
||
// Assert that the rendered block contains the featured image as the background-image url, | ||
// and not the image element, when image fill is true and the media is on the right. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
'mediaPosition' => 'right', | ||
'imageFill' => true, | ||
); | ||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( 'background-image:url(' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . ')', $rendered ); | ||
$this->assertStringNotContainsString( '<img', $rendered ); | ||
} | ||
|
||
/** | ||
* Test gutenberg_render_block_core_media_text with the featured image on the right, | ||
* and a second media & text block nested inside the content area. | ||
*/ | ||
public function test_render_block_core_media_text_featured_image_media_on_right_nested() { | ||
$this->setup_query(); | ||
|
||
$content = '<div class="wp-block-media-text has-media-on-the-right is-stacked-on-mobile"><div class="wp-block-media-text__content"><div class="wp-block-media-text is-stacked-on-mobile"><div class="wp-block-media-text__content"><p></p></div><figure class="wp-block-media-text__media"></figure></div></div><figure class="wp-block-media-text__media"></figure></div>'; | ||
|
||
// Assert that the rendered block contains the featured image when media is on the right. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
'mediaPosition' => 'right', | ||
); | ||
|
||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( '<img alt="" src="' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . '"', $rendered ); | ||
|
||
// Assert that the rendered block contains the featured image as the background-image url, | ||
// and not the image element, when image fill is true and the media is on the right. | ||
$attributes = array( | ||
'useFeaturedImage' => true, | ||
'mediaPosition' => 'right', | ||
'imageFill' => true, | ||
); | ||
|
||
$rendered = gutenberg_render_block_core_media_text( $attributes, $content ); | ||
$this->assertStringContainsString( 'background-image:url(' . wp_get_attachment_image_url( self::$attachment_id, 'full' ) . ')', $rendered ); | ||
$this->assertStringNotContainsString( '<img', $rendered ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea for possible follow up: When the image is a background image add accessible alt text via
aria-label
(or whatever's appropriate) on the figure element