-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #864: Subclass 'Image' widget to output valid AMP markup.
Like the 'Gallery' widget, only implement the render_media(). @todo: filter markup. The test currently fails because of this.
- Loading branch information
Ryan Kienstra
committed
Jan 18, 2018
1 parent
436722d
commit 44287d8
Showing
4 changed files
with
115 additions
and
0 deletions.
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,30 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Media_Image | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Media_Image | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Media_Image extends WP_Widget_Media_Image { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to convert <imp> to <amp-img> and remove the 'style' attribute. | ||
* @see https://github.com/Automattic/amp-wp/issues/864 | ||
* @param array $instance Data for widget. | ||
* @return void. | ||
*/ | ||
public function render_media( $instance ) { | ||
ob_start(); | ||
parent::render_media( $instance ); | ||
$output = ob_get_clean(); | ||
echo $output; // WPCS: XSS ok. | ||
} | ||
|
||
} |
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,83 @@ | ||
<?php | ||
/** | ||
* Tests for class AMP_Widget_Media_Image. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Tests for class AMP_Widget_Media_Image. | ||
* | ||
* @package AMP | ||
*/ | ||
class Test_AMP_Widget_Media_Image extends WP_UnitTestCase { | ||
|
||
/** | ||
* Instance of the widget. | ||
* | ||
* @var object | ||
*/ | ||
public $instance; | ||
|
||
/** | ||
* Setup. | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$amp_widgets = new AMP_Widgets(); | ||
$amp_widgets->register_widgets(); | ||
$this->instance = new AMP_Widget_Media_Image(); | ||
} | ||
|
||
/** | ||
* Test construct(). | ||
* | ||
* @see AMP_Widget_Media_Image::__construct(). | ||
*/ | ||
public function test_construct() { | ||
global $wp_widget_factory; | ||
$amp_widget = $wp_widget_factory->widgets['AMP_Widget_Media_Image']; | ||
|
||
$this->assertEquals( 'media_image', $amp_widget->id_base ); | ||
$this->assertEquals( 'Image', $amp_widget->name ); | ||
$this->assertEquals( 'widget_media_image', $amp_widget->widget_options['classname'] ); | ||
$this->assertEquals( true, $amp_widget->widget_options['customize_selective_refresh'] ); | ||
$this->assertEquals( 'Displays an image.', $amp_widget->widget_options['description'] ); | ||
} | ||
|
||
/** | ||
* Test widget(). | ||
* | ||
* Mock image logic mainly copied from Test_WP_Widget_Media_image::test_render_media(). | ||
* | ||
* @see AMP_Widget_Media_Image::widget(). | ||
*/ | ||
public function test_render_media() { | ||
$first_test_image = '/tmp/test-image.jpg'; | ||
copy( DIR_TESTDATA . '/images/test-image.jpg', $first_test_image ); | ||
$attachment_id = self::factory()->attachment->create_object( array( | ||
'file' => $first_test_image, | ||
'post_parent' => 0, | ||
'post_mime_type' => 'image/jpeg', | ||
'post_title' => 'Test Image', | ||
) ); | ||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $first_test_image ) ); | ||
$instance = array( | ||
'title' => 'Test Image Widget', | ||
'attachment_id' => $attachment_id, | ||
'height' => 100, | ||
'width' => 100, | ||
'url' => 'https://example.com/amp', | ||
); | ||
|
||
ob_start(); | ||
$this->instance->render_media( $instance ); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertFalse( strpos( $output, '<img' ) ); | ||
$this->assertFalse( strpos( $output, 'style=' ) ); | ||
} | ||
|
||
} |