Skip to content

Commit

Permalink
Issue #864: Subclass 'Gallery' widget, to output valid AMP markup.
Browse files Browse the repository at this point in the history
Only implement the render_media() function.
@todo: filter the markup in it.
This includes a PHPUnit test class for it, which will now fail.
  • Loading branch information
Ryan Kienstra committed Jan 18, 2018
1 parent be58d02 commit 436722d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
1 change: 1 addition & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class AMP_Autoloader {
'AMP_WP_Utils' => 'includes/utils/class-amp-wp-utils',
'AMP_Widget_Archives' => 'includes/widgets/class-amp-widget-archives',
'AMP_Widget_Categories' => 'includes/widgets/class-amp-widget-categories',
'AMP_Widget_Media_Gallery' => 'includes/widgets/class-amp-widget-media-gallery',
'AMP_Widget_Recent_Comments' => 'includes/widgets/class-amp-widget-recent-comments',
'AMP_Widgets' => 'includes/widgets/class-amp-widgets',
'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed',
Expand Down
30 changes: 30 additions & 0 deletions includes/widgets/class-amp-widget-media-gallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Class AMP_Widget_Media_Gallery
*
* @package AMP
*/

/**
* Class AMP_Widget_Media_Gallery
*
* @package AMP
*/
class AMP_Widget_Media_Gallery extends WP_Widget_Media_Gallery {

/**
* Echoes the markup of the widget.
*
* @todo filter $output, to convert <imp> to <amp-img> and remove the <style>.
* @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.
}

}
1 change: 1 addition & 0 deletions includes/widgets/class-amp-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function get_widgets() {
return array(
'WP_Widget_Archives' => 'AMP_Widget_Archives',
'WP_Widget_Categories' => 'AMP_Widget_Categories',
'WP_Widget_Media_Gallery' => 'AMP_Widget_Media_Gallery',
'WP_Widget_Recent_Comments' => 'AMP_Widget_Recent_Comments',
);
}
Expand Down
92 changes: 92 additions & 0 deletions tests/test-class-amp-widget-media-gallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Tests for class AMP_Widget_Media_Gallery.
*
* @package AMP
*/

/**
* Tests for class AMP_Widget_Media_Gallery.
*
* @package AMP
*/
class Test_AMP_Widget_Media_Gallery 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_Gallery();
}

/**
* Test construct().
*
* @see AMP_Widget_Media_Gallery::__construct().
*/
public function test_construct() {
global $wp_widget_factory;
$amp_widget = $wp_widget_factory->widgets['AMP_Widget_Media_Gallery'];

$this->assertEquals( 'media_gallery', $amp_widget->id_base );
$this->assertEquals( 'Gallery', $amp_widget->name );
$this->assertEquals( 'widget_media_gallery', $amp_widget->widget_options['classname'] );
$this->assertEquals( true, $amp_widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'Displays an image gallery.', $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_Gallery::widget().
*/
public function test_render_media() {
$first_test_image = '/tmp/test-image.jpg';
copy( DIR_TESTDATA . '/images/test-image.jpg', $first_test_image );
$first_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( $first_attachment_id, wp_generate_attachment_metadata( $first_attachment_id, $first_test_image ) );
$ids[] = $first_attachment_id;

$second_test_image = '/tmp/test-image.jpg';
copy( DIR_TESTDATA . '/images/test-image.jpg', $second_test_image );
$second_attachment_id = self::factory()->attachment->create_object( array(
'file' => $second_test_image,
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'post_title' => 'Test Image',
) );
wp_update_attachment_metadata( $second_attachment_id, wp_generate_attachment_metadata( $second_attachment_id, $second_test_image ) );
$ids[] = $second_attachment_id;
$instance = array(
'title' => 'Test Gallery Widget',
'ids' => $ids,
);

ob_start();
$this->instance->render_media( $instance );
$output = ob_get_clean();

$this->assertFalse( strpos( $output, '<img' ) );
$this->assertFalse( strpos( $output, '<style' ) );
}

}
12 changes: 6 additions & 6 deletions tests/test-class-amp-widget-recent-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function setUp() {
*/
public function test_construct() {
global $wp_widget_factory;
$amp_categories = $wp_widget_factory->widgets['AMP_Widget_Recent_Comments'];
$amp_widget = $wp_widget_factory->widgets['AMP_Widget_Recent_Comments'];

$this->assertEquals( 'recent-comments', $amp_categories->id_base );
$this->assertEquals( 'Recent Comments', $amp_categories->name );
$this->assertEquals( 'widget_recent_comments', $amp_categories->widget_options['classname'] );
$this->assertEquals( true, $amp_categories->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'Your site&#8217;s most recent comments.', $amp_categories->widget_options['description'] );
$this->assertEquals( 'recent-comments', $amp_widget->id_base );
$this->assertEquals( 'Recent Comments', $amp_widget->name );
$this->assertEquals( 'widget_recent_comments', $amp_widget->widget_options['classname'] );
$this->assertEquals( true, $amp_widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'Your site&#8217;s most recent comments.', $amp_widget->widget_options['description'] );
$this->assertFalse( apply_filters( 'show_recent_comments_widget_style', true ) );
}

Expand Down

0 comments on commit 436722d

Please sign in to comment.