mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Widgets: Introduce media widgets for images, audio, and video with extensible base for additional media widgets in the future.
The last time a new widget was introduced, Vuvuzelas were a thing, Angry Birds started taking over phones, and WordPress stopped shipping with Kubrick. Seven years and 17 releases without new widgets have been enough, time to spice up your sidebar! Props westonruter, melchoyce, obenland, timmydcrawford, adamsilverstein, gonom9, wonderboymusic, Fab1en, DrewAPicture, sirbrillig, joen, matias, samikeijonen, afercia, celloexpressions, designsimply, michelleweber, ranh, kjellr, karmatosed. Fixes #32417, #39993, #39994, #39995. git-svn-id: https://develop.svn.wordpress.org/trunk@40640 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,266 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_Widget_Media_Audio functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage widgets
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test wp-includes/widgets/class-wp-widget-audio.php
|
||||
*
|
||||
* @group widgets
|
||||
*/
|
||||
class Test_WP_Widget_Media_Audio extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Clean up global scope.
|
||||
*
|
||||
* @global WP_Scripts $wp_scripts
|
||||
* @global WP_Styles $wp_styles
|
||||
*/
|
||||
function clean_up_global_scope() {
|
||||
global $wp_scripts, $wp_styles;
|
||||
parent::clean_up_global_scope();
|
||||
$wp_scripts = null;
|
||||
$wp_styles = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_instance_schema method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Audio::get_instance_schema
|
||||
*/
|
||||
function test_get_instance_schema() {
|
||||
$wp_widget_audio = new WP_Widget_Media_Audio();
|
||||
$schema = $wp_widget_audio->get_instance_schema();
|
||||
|
||||
$this->assertEqualSets(
|
||||
array_merge(
|
||||
array(
|
||||
'attachment_id',
|
||||
'preload',
|
||||
'loop',
|
||||
'title',
|
||||
'url',
|
||||
),
|
||||
wp_get_audio_extensions()
|
||||
),
|
||||
array_keys( $schema )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test constructor.
|
||||
*
|
||||
* @covers WP_Widget_Media_Audio::__construct()
|
||||
*/
|
||||
function test_constructor() {
|
||||
$widget = new WP_Widget_Media_Audio();
|
||||
|
||||
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'description', $widget->widget_options );
|
||||
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
|
||||
$this->assertEquals( 'audio', $widget->widget_options['mime_type'] );
|
||||
$this->assertEqualSets( array(
|
||||
'add_to_widget',
|
||||
'replace_media',
|
||||
'edit_media',
|
||||
'media_library_state_multi',
|
||||
'media_library_state_single',
|
||||
'missing_attachment',
|
||||
'no_media_selected',
|
||||
'add_media',
|
||||
'unsupported_file_type',
|
||||
), array_keys( $widget->l10n ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_instance_schema method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Audio::update
|
||||
*/
|
||||
function test_update() {
|
||||
$widget = new WP_Widget_Media_Audio();
|
||||
$instance = array();
|
||||
|
||||
// Should return valid attachment ID.
|
||||
$expected = array(
|
||||
'attachment_id' => 1,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment ID.
|
||||
$result = $widget->update( array(
|
||||
'attachment_id' => 'media',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid attachment url.
|
||||
$expected = array(
|
||||
'url' => 'https://chickenandribs.org',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment url.
|
||||
$result = $widget->update( array(
|
||||
'url' => 'not_a_url',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
$this->assertStringStartsWith( 'http://', $result['url'] );
|
||||
|
||||
// Should return loop setting.
|
||||
$expected = array(
|
||||
'loop' => true,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid loop setting.
|
||||
$result = $widget->update( array(
|
||||
'loop' => 'not-boolean',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid attachment title.
|
||||
$expected = array(
|
||||
'title' => 'An audio sample of parrots',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment title.
|
||||
$result = $widget->update( array(
|
||||
'title' => '<h1>Cute Baby Goats</h1>',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
|
||||
// Should return valid preload setting.
|
||||
$expected = array(
|
||||
'preload' => 'none',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid preload setting.
|
||||
$result = $widget->update( array(
|
||||
'preload' => 'nope',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should filter invalid key.
|
||||
$result = $widget->update( array(
|
||||
'h4x' => 'value',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test render_media method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Audio::render_media
|
||||
*/
|
||||
function test_render_media() {
|
||||
$test_audio_file = __FILE__ . '../../data/uploads/small-audio.mp3';
|
||||
$widget = new WP_Widget_Media_Audio();
|
||||
$attachment_id = self::factory()->attachment->create_object( array(
|
||||
'file' => $test_audio_file,
|
||||
'post_parent' => 0,
|
||||
'post_mime_type' => 'audio/mp3',
|
||||
'post_title' => 'Test Audio',
|
||||
) );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $test_audio_file ) );
|
||||
|
||||
// Should be empty when there is no attachment_id.
|
||||
ob_start();
|
||||
$widget->render_media( array() );
|
||||
$output = ob_get_clean();
|
||||
$this->assertEmpty( $output );
|
||||
|
||||
// Should be empty when there is an invalid attachment_id.
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => 777,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
$this->assertEmpty( $output );
|
||||
|
||||
// Tests with audio from library.
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Check default outputs.
|
||||
$this->assertContains( 'preload="none"', $output );
|
||||
$this->assertContains( 'class="wp-audio-shortcode"', $output );
|
||||
$this->assertContains( 'small-audio.mp3', $output );
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'title' => 'Funny',
|
||||
'preload' => 'auto',
|
||||
'loop' => true,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Custom attributes.
|
||||
$this->assertContains( 'preload="auto"', $output );
|
||||
$this->assertContains( 'loop="1"', $output );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enqueue_preview_scripts method.
|
||||
*
|
||||
* @global WP_Scripts $wp_scripts
|
||||
* @global WP_Styles $wp_styles
|
||||
* @covers WP_Widget_Media_Audio::enqueue_preview_scripts
|
||||
*/
|
||||
function test_enqueue_preview_scripts() {
|
||||
global $wp_scripts, $wp_styles;
|
||||
$wp_scripts = null;
|
||||
$wp_styles = null;
|
||||
$widget = new WP_Widget_Media_Audio();
|
||||
|
||||
$this->assertFalse( wp_script_is( 'wp-mediaelement' ) );
|
||||
$this->assertFalse( wp_style_is( 'wp-mediaelement' ) );
|
||||
|
||||
$widget->enqueue_preview_scripts();
|
||||
|
||||
$this->assertTrue( wp_script_is( 'wp-mediaelement' ) );
|
||||
$this->assertTrue( wp_style_is( 'wp-mediaelement' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enqueue_admin_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Audio::enqueue_admin_scripts
|
||||
*/
|
||||
function test_enqueue_admin_scripts() {
|
||||
set_current_screen( 'widgets.php' );
|
||||
$widget = new WP_Widget_Media_Audio();
|
||||
$widget->enqueue_admin_scripts();
|
||||
|
||||
$this->assertTrue( wp_script_is( 'media-audio-widget' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test render_control_template_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Audio::render_control_template_scripts
|
||||
*/
|
||||
function test_render_control_template_scripts() {
|
||||
$widget = new WP_Widget_Media_Audio();
|
||||
|
||||
ob_start();
|
||||
$widget->render_control_template_scripts();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( '<script type="text/html" id="tmpl-wp-media-widget-audio-preview">', $output );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,480 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_Widget_Media_Image functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage widgets
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test wp-includes/widgets/class-wp-widget-image.php
|
||||
*
|
||||
* @group widgets
|
||||
*/
|
||||
class Test_WP_Widget_Media_Image extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Clean up global scope.
|
||||
*
|
||||
* @global WP_Scripts $wp_scripts
|
||||
* @global WP_Styles $wp_styles
|
||||
*/
|
||||
function clean_up_global_scope() {
|
||||
global $wp_scripts, $wp_styles;
|
||||
parent::clean_up_global_scope();
|
||||
$wp_scripts = null;
|
||||
$wp_styles = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_instance_schema method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Image::get_instance_schema
|
||||
*/
|
||||
function test_get_instance_schema() {
|
||||
$widget = new WP_Widget_Media_Image();
|
||||
$schema = $widget->get_instance_schema();
|
||||
|
||||
$this->assertEqualSets( array(
|
||||
'alt',
|
||||
'attachment_id',
|
||||
'caption',
|
||||
'height',
|
||||
'image_classes',
|
||||
'image_title',
|
||||
'link_classes',
|
||||
'link_rel',
|
||||
'link_target_blank',
|
||||
'link_type',
|
||||
'link_url',
|
||||
'size',
|
||||
'title',
|
||||
'url',
|
||||
'width',
|
||||
), array_keys( $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test constructor.
|
||||
*
|
||||
* @covers WP_Widget_Media_Image::__construct()
|
||||
*/
|
||||
function test_constructor() {
|
||||
$widget = new WP_Widget_Media_Image();
|
||||
|
||||
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'description', $widget->widget_options );
|
||||
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
|
||||
$this->assertEquals( 'image', $widget->widget_options['mime_type'] );
|
||||
$this->assertEqualSets( array(
|
||||
'add_to_widget',
|
||||
'replace_media',
|
||||
'edit_media',
|
||||
'media_library_state_multi',
|
||||
'media_library_state_single',
|
||||
'missing_attachment',
|
||||
'no_media_selected',
|
||||
'add_media',
|
||||
'unsupported_file_type',
|
||||
), array_keys( $widget->l10n ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_instance_schema method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Image::update
|
||||
*/
|
||||
function test_update() {
|
||||
$widget = new WP_Widget_Media_Image();
|
||||
$instance = array();
|
||||
|
||||
// Should return valid attachment ID.
|
||||
$expected = array(
|
||||
'attachment_id' => 1,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment ID.
|
||||
$result = $widget->update( array(
|
||||
'attachment_id' => 'media',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid attachment url.
|
||||
$expected = array(
|
||||
'url' => 'https://example.org',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment url.
|
||||
$result = $widget->update( array(
|
||||
'url' => 'not_a_url',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
$this->assertStringStartsWith( 'http://', $result['url'] );
|
||||
|
||||
// Should return valid attachment title.
|
||||
$expected = array(
|
||||
'title' => 'What a title',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment title.
|
||||
$result = $widget->update( array(
|
||||
'title' => '<h1>W00t!</h1>',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
|
||||
// Should return valid image size.
|
||||
$expected = array(
|
||||
'size' => 'thumbnail',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid image size.
|
||||
$result = $widget->update( array(
|
||||
'size' => 'big league',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid image width.
|
||||
$expected = array(
|
||||
'width' => 300,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid image width.
|
||||
$result = $widget->update( array(
|
||||
'width' => 'wide',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid image height.
|
||||
$expected = array(
|
||||
'height' => 200,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid image height.
|
||||
$result = $widget->update( array(
|
||||
'height' => 'high',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid image caption.
|
||||
$expected = array(
|
||||
'caption' => 'A caption with <a href="#">link</a>',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid image caption.
|
||||
$result = $widget->update( array(
|
||||
'caption' => '"><i onload="alert(\'hello\')" />',
|
||||
), $instance );
|
||||
$this->assertSame( $result, array(
|
||||
'caption' => '"><i />',
|
||||
) );
|
||||
|
||||
// Should return valid alt text.
|
||||
$expected = array(
|
||||
'alt' => 'A water tower',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid alt text.
|
||||
$result = $widget->update( array(
|
||||
'alt' => '"><i onload="alert(\'hello\')" />',
|
||||
), $instance );
|
||||
$this->assertSame( $result, array(
|
||||
'alt' => '">',
|
||||
) );
|
||||
|
||||
// Should return valid link type.
|
||||
$expected = array(
|
||||
'link_type' => 'file',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid link type.
|
||||
$result = $widget->update( array(
|
||||
'link_type' => 'interesting',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid link url.
|
||||
$expected = array(
|
||||
'link_url' => 'https://example.org',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid link url.
|
||||
$result = $widget->update( array(
|
||||
'link_url' => 'not_a_url',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
$this->assertStringStartsWith( 'http://', $result['link_url'] );
|
||||
|
||||
// Should return valid image classes.
|
||||
$expected = array(
|
||||
'image_classes' => 'A water tower',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid image classes.
|
||||
$result = $widget->update( array(
|
||||
'image_classes' => '"><i onload="alert(\'hello\')" />',
|
||||
), $instance );
|
||||
$this->assertSame( $result, array(
|
||||
'image_classes' => 'i onloadalerthello',
|
||||
) );
|
||||
|
||||
// Should return valid link classes.
|
||||
$expected = array(
|
||||
'link_classes' => 'A water tower',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid link classes.
|
||||
$result = $widget->update( array(
|
||||
'link_classes' => '"><i onload="alert(\'hello\')" />',
|
||||
), $instance );
|
||||
$this->assertSame( $result, array(
|
||||
'link_classes' => 'i onloadalerthello',
|
||||
) );
|
||||
|
||||
// Should return valid rel text.
|
||||
$expected = array(
|
||||
'link_rel' => 'previous',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid rel text.
|
||||
$result = $widget->update( array(
|
||||
'link_rel' => '"><i onload="alert(\'hello\')" />',
|
||||
), $instance );
|
||||
$this->assertSame( $result, array(
|
||||
'link_rel' => 'i onloadalerthello',
|
||||
) );
|
||||
|
||||
// Should return valid link target.
|
||||
$expected = array(
|
||||
'link_target_blank' => false,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid link target.
|
||||
$result = $widget->update( array(
|
||||
'link_target_blank' => 'top',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid image title.
|
||||
$expected = array(
|
||||
'image_title' => 'What a title',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid image title.
|
||||
$result = $widget->update( array(
|
||||
'image_title' => '<h1>W00t!</h1>',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
|
||||
// Should filter invalid key.
|
||||
$result = $widget->update( array(
|
||||
'imaginary_key' => 'value',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test render_media method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Image::render_media
|
||||
*/
|
||||
function test_render_media() {
|
||||
$widget = new WP_Widget_Media_Image();
|
||||
$attachment_id = self::factory()->attachment->create_object( array(
|
||||
'file' => DIR_TESTDATA . '/images/canola.jpg',
|
||||
'post_parent' => 0,
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_title' => 'Canola',
|
||||
) );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, DIR_TESTDATA . '/images/canola.jpg' ) );
|
||||
|
||||
// Should be empty when there is no attachment_id.
|
||||
ob_start();
|
||||
$widget->render_media( array() );
|
||||
$output = ob_get_clean();
|
||||
$this->assertEmpty( $output );
|
||||
|
||||
// Should be empty when there is an invalid attachment_id.
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => 666,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
$this->assertEmpty( $output );
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// No default title.
|
||||
$this->assertNotContains( 'title="', $output );
|
||||
// Default image classes.
|
||||
$this->assertContains( 'class="image wp-image-' . $attachment_id, $output );
|
||||
$this->assertContains( 'style="max-width: 100%; height: auto;"', $output );
|
||||
$this->assertContains( 'alt=""', $output );
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'image_title' => 'Custom Title',
|
||||
'image_classes' => 'custom-class',
|
||||
'alt' => 'A flower',
|
||||
'size' => 'custom',
|
||||
'width' => 100,
|
||||
'height' => 100,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Custom image title.
|
||||
$this->assertContains( 'title="Custom Title"', $output );
|
||||
// Custom image class.
|
||||
$this->assertContains( 'class="image wp-image-' . $attachment_id . ' custom-class', $output );
|
||||
$this->assertContains( 'alt="A flower"', $output );
|
||||
$this->assertContains( 'width="100"', $output );
|
||||
$this->assertContains( 'height="100"', $output );
|
||||
|
||||
// Embeded images.
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => null,
|
||||
'caption' => 'With caption',
|
||||
'height' => 100,
|
||||
'link_type' => 'file',
|
||||
'url' => 'http://example.org/url/to/image.jpg',
|
||||
'width' => 100,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Custom image class.
|
||||
$this->assertContains( 'src="http://example.org/url/to/image.jpg"', $output );
|
||||
|
||||
// Link settings.
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'link_type' => 'file',
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
$link = '<a href="' . wp_get_attachment_url( $attachment_id ) . '"';
|
||||
$this->assertContains( $link, $output );
|
||||
$link .= ' class=""';
|
||||
$this->assertContains( $link, $output );
|
||||
$link .= ' rel=""';
|
||||
$this->assertContains( $link, $output );
|
||||
$link .= ' target=""';
|
||||
$this->assertContains( $link, $output );
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'link_type' => 'post',
|
||||
'link_classes' => 'custom-link-class',
|
||||
'link_rel' => 'attachment',
|
||||
'link_target_blank' => false,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( '<a href="' . get_attachment_link( $attachment_id ) . '"', $output );
|
||||
$this->assertContains( 'class="custom-link-class"', $output );
|
||||
$this->assertContains( 'rel="attachment"', $output );
|
||||
$this->assertContains( 'target=""', $output );
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'link_type' => 'custom',
|
||||
'link_url' => 'https://example.org',
|
||||
'link_target_blank' => true,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( '<a href="https://example.org"', $output );
|
||||
$this->assertContains( 'target="_blank"', $output );
|
||||
|
||||
// Caption settings.
|
||||
wp_update_post( array(
|
||||
'ID' => $attachment_id,
|
||||
'post_excerpt' => 'Default caption',
|
||||
) );
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( 'class="wp-caption alignnone"', $output );
|
||||
$this->assertContains( '<p class="wp-caption-text">Default caption</p>', $output );
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'caption' => 'Custom caption',
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( 'class="wp-caption alignnone"', $output );
|
||||
$this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enqueue_admin_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Image::enqueue_admin_scripts
|
||||
*/
|
||||
function test_enqueue_admin_scripts() {
|
||||
set_current_screen( 'widgets.php' );
|
||||
$widget = new WP_Widget_Media_Image();
|
||||
$widget->enqueue_admin_scripts();
|
||||
|
||||
$this->assertTrue( wp_script_is( 'media-image-widget' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test render_control_template_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Image::render_control_template_scripts
|
||||
*/
|
||||
function test_render_control_template_scripts() {
|
||||
$widget = new WP_Widget_Media_Image();
|
||||
|
||||
ob_start();
|
||||
$widget->render_control_template_scripts();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( '<script type="text/html" id="tmpl-wp-media-widget-image-preview">', $output );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_Widget_Media_Video functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage widgets
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test wp-includes/widgets/class-wp-widget-video.php
|
||||
*
|
||||
* @group widgets
|
||||
*/
|
||||
class Test_WP_Widget_Media_Video extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Clean up global scope.
|
||||
*
|
||||
* @global WP_Scripts $wp_scripts
|
||||
* @global WP_Styles $wp_styles
|
||||
*/
|
||||
function clean_up_global_scope() {
|
||||
global $wp_scripts, $wp_styles;
|
||||
parent::clean_up_global_scope();
|
||||
$wp_scripts = null;
|
||||
$wp_styles = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_instance_schema method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Video::get_instance_schema()
|
||||
*/
|
||||
function test_get_instance_schema() {
|
||||
$widget = new WP_Widget_Media_Video();
|
||||
$schema = $widget->get_instance_schema();
|
||||
|
||||
$this->assertEqualSets(
|
||||
array_merge(
|
||||
array(
|
||||
'attachment_id',
|
||||
'preload',
|
||||
'loop',
|
||||
'title',
|
||||
'url',
|
||||
'content',
|
||||
),
|
||||
wp_get_video_extensions()
|
||||
),
|
||||
array_keys( $schema )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test constructor.
|
||||
*
|
||||
* @covers WP_Widget_Media_Video::__construct()
|
||||
*/
|
||||
function test_constructor() {
|
||||
$widget = new WP_Widget_Media_Video();
|
||||
|
||||
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'description', $widget->widget_options );
|
||||
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
|
||||
$this->assertEquals( 'video', $widget->widget_options['mime_type'] );
|
||||
$this->assertEqualSets( array(
|
||||
'add_to_widget',
|
||||
'replace_media',
|
||||
'unsupported_file_type',
|
||||
'edit_media',
|
||||
'media_library_state_multi',
|
||||
'media_library_state_single',
|
||||
'missing_attachment',
|
||||
'no_media_selected',
|
||||
'add_media',
|
||||
), array_keys( $widget->l10n ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_instance_schema method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Video::update()
|
||||
*/
|
||||
function test_update() {
|
||||
$widget = new WP_Widget_Media_Video();
|
||||
$instance = array();
|
||||
|
||||
// Should return valid attachment ID.
|
||||
$expected = array(
|
||||
'attachment_id' => 1,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment ID.
|
||||
$result = $widget->update( array(
|
||||
'attachment_id' => 'media',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid attachment url.
|
||||
$expected = array(
|
||||
'url' => 'https://chickenandribs.org',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment url.
|
||||
$result = $widget->update( array(
|
||||
'url' => 'not_a_url',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
$this->assertStringStartsWith( 'http://', $result['url'] );
|
||||
|
||||
// Should return loop setting.
|
||||
$expected = array(
|
||||
'loop' => true,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid loop setting.
|
||||
$result = $widget->update( array(
|
||||
'loop' => 'not-boolean',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid attachment title.
|
||||
$expected = array(
|
||||
'title' => 'A video of goats',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment title.
|
||||
$result = $widget->update( array(
|
||||
'title' => '<h1>Cute Baby Goats</h1>',
|
||||
), $instance );
|
||||
$this->assertNotSame( $result, $instance );
|
||||
|
||||
// Should return valid preload setting.
|
||||
$expected = array(
|
||||
'preload' => 'none',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid preload setting.
|
||||
$result = $widget->update( array(
|
||||
'preload' => 'nope',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should filter invalid key.
|
||||
$result = $widget->update( array(
|
||||
'h4x' => 'value',
|
||||
), $instance );
|
||||
$this->assertSame( $result, $instance );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test render_media method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Video::render_media()
|
||||
* @covers WP_Widget_Media_Video::inject_video_max_width_style()
|
||||
*/
|
||||
function test_render_media() {
|
||||
$test_movie_file = __FILE__ . '../../data/uploads/small-video.m4v';
|
||||
$widget = new WP_Widget_Media_Video();
|
||||
$attachment_id = self::factory()->attachment->create_object( array(
|
||||
'file' => $test_movie_file,
|
||||
'post_parent' => 0,
|
||||
'post_mime_type' => 'video/mp4',
|
||||
'post_title' => 'Test Video',
|
||||
) );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $test_movie_file ) );
|
||||
|
||||
// Should be empty when there is no attachment_id.
|
||||
ob_start();
|
||||
$widget->render_media( array() );
|
||||
$output = ob_get_clean();
|
||||
$this->assertEmpty( $output );
|
||||
|
||||
// Should be empty when there is an invalid attachment_id.
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => 777,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
$this->assertEmpty( $output );
|
||||
|
||||
// Tests with video from library.
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Check default outputs.
|
||||
$this->assertContains( 'preload="metadata"', $output );
|
||||
$this->assertContains( 'class="wp-video"', $output );
|
||||
$this->assertContains( 'width:100%', $output );
|
||||
$this->assertNotContains( 'height=', $output );
|
||||
$this->assertNotContains( 'width="', $output );
|
||||
$this->assertContains( 'small-video.m4v', $output );// Auto parses dimensions.
|
||||
|
||||
ob_start();
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'title' => 'Open Source Cartoon',
|
||||
'preload' => 'metadata',
|
||||
'loop' => true,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Custom attributes.
|
||||
$this->assertContains( 'preload="metadata"', $output );
|
||||
$this->assertContains( 'loop="1"', $output );
|
||||
|
||||
// Externally hosted video.
|
||||
ob_start();
|
||||
$content = '<track srclang="en" label="English" kind="subtitles" src="http://example.com/wp-content/uploads/2017/04/subtitles-en.vtt">';
|
||||
$widget->render_media( array(
|
||||
'attachment_id' => null,
|
||||
'loop' => false,
|
||||
'url' => 'https://www.youtube.com/watch?v=OQSNhk5ICTI',
|
||||
'content' => $content,
|
||||
) );
|
||||
$output = ob_get_clean();
|
||||
|
||||
// Custom attributes.
|
||||
$this->assertContains( 'preload="metadata"', $output );
|
||||
$this->assertContains( 'src="https://www.youtube.com/watch?v=OQSNhk5ICTI', $output );
|
||||
$this->assertContains( $content, $output );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enqueue_preview_scripts method.
|
||||
*
|
||||
* @global WP_Scripts $wp_scripts
|
||||
* @global WP_Styles $wp_styles
|
||||
* @covers WP_Widget_Media_Video::enqueue_preview_scripts()
|
||||
*/
|
||||
function test_enqueue_preview_scripts() {
|
||||
global $wp_scripts, $wp_styles;
|
||||
$widget = new WP_Widget_Media_Video();
|
||||
|
||||
$wp_scripts = null;
|
||||
$wp_styles = null;
|
||||
$widget->enqueue_preview_scripts();
|
||||
$this->assertTrue( wp_script_is( 'wp-mediaelement' ) );
|
||||
$this->assertTrue( wp_style_is( 'wp-mediaelement' ) );
|
||||
$this->assertTrue( wp_script_is( 'froogaloop' ) );
|
||||
|
||||
$wp_scripts = null;
|
||||
$wp_styles = null;
|
||||
add_filter( 'wp_video_shortcode_library', '__return_empty_string' );
|
||||
$widget->enqueue_preview_scripts();
|
||||
$this->assertFalse( wp_script_is( 'wp-mediaelement' ) );
|
||||
$this->assertFalse( wp_style_is( 'wp-mediaelement' ) );
|
||||
$this->assertTrue( wp_script_is( 'froogaloop' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enqueue_admin_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Video::enqueue_admin_scripts()
|
||||
*/
|
||||
function test_enqueue_admin_scripts() {
|
||||
set_current_screen( 'widgets.php' );
|
||||
$widget = new WP_Widget_Media_Video();
|
||||
$widget->enqueue_admin_scripts();
|
||||
|
||||
$this->assertTrue( wp_script_is( 'media-video-widget' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test render_control_template_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media_Video::render_control_template_scripts()
|
||||
*/
|
||||
function test_render_control_template_scripts() {
|
||||
$widget = new WP_Widget_Media_Video();
|
||||
|
||||
ob_start();
|
||||
$widget->render_control_template_scripts();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( '<script type="text/html" id="tmpl-wp-media-widget-video-preview">', $output );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,467 @@
|
||||
<?php
|
||||
/**
|
||||
* Unit tests covering WP_Widget_Media functionality.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage widgets
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class Test_WP_Widget_Media
|
||||
*
|
||||
* @group widgets
|
||||
*/
|
||||
class Test_WP_Widget_Media extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Clean up global scope.
|
||||
*
|
||||
* @global WP_Scripts $wp_scripts
|
||||
* @global WP_Styles $wp_styles
|
||||
*/
|
||||
function clean_up_global_scope() {
|
||||
global $wp_scripts, $wp_styles;
|
||||
parent::clean_up_global_scope();
|
||||
$wp_scripts = null;
|
||||
$wp_styles = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get instance for mocked media widget class.
|
||||
*
|
||||
* @param string $id_base Base ID for the widget, lowercase and unique.
|
||||
* @param string $name Name for the widget displayed on the configuration page.
|
||||
* @param array $widget_options Optional. Widget options.
|
||||
* @param array $control_options Optional. Widget control options.
|
||||
* @return PHPUnit_Framework_MockObject_MockObject|WP_Widget_Media Mocked instance.
|
||||
*/
|
||||
function get_mocked_class_instance( $id_base = 'mocked', $name = 'Mocked', $widget_options = array(), $control_options = array() ) {
|
||||
$original_class_name = 'WP_Widget_Media';
|
||||
$arguments = array( $id_base, $name, $widget_options, $control_options );
|
||||
$mock_class_name = '';
|
||||
$call_original_constructor = true;
|
||||
$call_original_clone = true;
|
||||
$call_autoload = true;
|
||||
$mocked_methods = array( 'render_media' );
|
||||
|
||||
return $this->getMockForAbstractClass( $original_class_name, $arguments, $mock_class_name, $call_original_constructor, $call_original_clone, $call_autoload, $mocked_methods );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test constructor.
|
||||
*
|
||||
* @covers WP_Widget_Media::__construct()
|
||||
* @covers WP_Widget_Media::_register()
|
||||
*/
|
||||
function test_constructor() {
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$widget->_register();
|
||||
|
||||
$this->assertArrayHasKey( 'mime_type', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'customize_selective_refresh', $widget->widget_options );
|
||||
$this->assertArrayHasKey( 'description', $widget->widget_options );
|
||||
$this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
|
||||
$this->assertEmpty( $widget->widget_options['mime_type'] );
|
||||
$this->assertEqualSets( array(
|
||||
'add_to_widget',
|
||||
'replace_media',
|
||||
'edit_media',
|
||||
'media_library_state_multi',
|
||||
'media_library_state_single',
|
||||
'missing_attachment',
|
||||
'no_media_selected',
|
||||
'add_media',
|
||||
'unsupported_file_type',
|
||||
), array_keys( $widget->l10n ) );
|
||||
$this->assertEquals( count( $widget->l10n ), count( array_filter( $widget->l10n ) ), 'Expected all translation strings to be defined.' );
|
||||
$this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
|
||||
$this->assertFalse( has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ), 'Did not expect preview scripts to be enqueued when not in customize preview context.' );
|
||||
$this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( $widget, 'render_control_template_scripts' ) ) );
|
||||
|
||||
// With non-default args.
|
||||
$id_base = 'media_pdf';
|
||||
$name = 'PDF';
|
||||
$widget_options = array(
|
||||
'mime_type' => 'application/pdf',
|
||||
);
|
||||
$control_options = array(
|
||||
'width' => 850,
|
||||
'height' => 1100,
|
||||
);
|
||||
$widget = $this->get_mocked_class_instance( $id_base, $name, $widget_options, $control_options );
|
||||
$this->assertEquals( $id_base, $widget->id_base );
|
||||
$this->assertEquals( $name, $widget->name );
|
||||
|
||||
// Method assertArraySubset doesn't exist in phpunit versions compatible with PHP 5.2.
|
||||
if ( method_exists( $this, 'assertArraySubset' ) ) {
|
||||
$this->assertArraySubset( $widget_options, $widget->widget_options );
|
||||
$this->assertArraySubset( $control_options, $widget->control_options );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test constructor in customize preview.
|
||||
*
|
||||
* @global WP_Customize_Manager $wp_customize
|
||||
* @covers WP_Widget_Media::__construct()
|
||||
* @covers WP_Widget_Media::_register()
|
||||
*/
|
||||
function test_constructor_in_customize_preview() {
|
||||
global $wp_customize;
|
||||
wp_set_current_user( $this->factory()->user->create( array(
|
||||
'role' => 'administrator',
|
||||
) ) );
|
||||
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
|
||||
$wp_customize = new WP_Customize_Manager( array(
|
||||
'changeset_uuid' => wp_generate_uuid4(),
|
||||
) );
|
||||
$wp_customize->start_previewing_theme();
|
||||
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$widget->_register();
|
||||
$this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $widget, 'enqueue_preview_scripts' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is_attachment_with_mime_type method.
|
||||
*
|
||||
* @covers WP_Widget_Media::is_attachment_with_mime_type
|
||||
*/
|
||||
function test_is_attachment_with_mime_type() {
|
||||
$attachment_id = self::factory()->attachment->create_object( array(
|
||||
'file' => DIR_TESTDATA . '/images/canola.jpg',
|
||||
'post_parent' => 0,
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_title' => 'Canola',
|
||||
) );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, DIR_TESTDATA . '/images/canola.jpg' ) );
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
|
||||
$this->assertFalse( $widget->is_attachment_with_mime_type( 0, 'image' ) );
|
||||
$this->assertFalse( $widget->is_attachment_with_mime_type( -123, 'image' ) );
|
||||
|
||||
$post_id = $this->factory()->post->create();
|
||||
$this->assertFalse( $widget->is_attachment_with_mime_type( $post_id, 'image' ) );
|
||||
$this->assertFalse( $widget->is_attachment_with_mime_type( $attachment_id, 'video' ) );
|
||||
$this->assertTrue( $widget->is_attachment_with_mime_type( $attachment_id, 'image' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test sanitize_token_list method.
|
||||
*
|
||||
* @covers WP_Widget_Media::sanitize_token_list
|
||||
*/
|
||||
function test_sanitize_token_list_string() {
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
|
||||
$result = $widget->sanitize_token_list( 'What A false class with-token <a href="#">and link</a>' );
|
||||
$this->assertEquals( 'What A false class with-token a hrefand linka', $result );
|
||||
|
||||
$result = $widget->sanitize_token_list( array( 'foo', '<i>bar', '">NO' ) );
|
||||
$this->assertEquals( $result, 'foo ibar NO' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_instance_schema method.
|
||||
*
|
||||
* @covers WP_Widget_Media::get_instance_schema
|
||||
*/
|
||||
function test_get_instance_schema() {
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$schema = $widget->get_instance_schema();
|
||||
|
||||
$this->assertEqualSets( array(
|
||||
'attachment_id',
|
||||
'title',
|
||||
'url',
|
||||
), array_keys( $schema ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update method.
|
||||
*
|
||||
* @covers WP_Widget_Media::update()
|
||||
*/
|
||||
function test_update() {
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$instance = array();
|
||||
|
||||
// Should return valid attachment ID.
|
||||
$expected = array(
|
||||
'attachment_id' => 1,
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment ID.
|
||||
$result = $widget->update(
|
||||
array(
|
||||
'attachment_id' => 'media',
|
||||
),
|
||||
$instance
|
||||
);
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
// Should return valid attachment url.
|
||||
$expected = array(
|
||||
'url' => 'https://example.org',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment url.
|
||||
$result = $widget->update(
|
||||
array(
|
||||
'url' => 'not_a_url',
|
||||
),
|
||||
$instance
|
||||
);
|
||||
$this->assertNotSame( $result, $instance );
|
||||
|
||||
// Should return valid attachment title.
|
||||
$expected = array(
|
||||
'title' => 'What a title',
|
||||
);
|
||||
$result = $widget->update( $expected, $instance );
|
||||
$this->assertSame( $result, $expected );
|
||||
|
||||
// Should filter invalid attachment title.
|
||||
$result = $widget->update(
|
||||
array(
|
||||
'title' => '<h1>W00t!</h1>',
|
||||
),
|
||||
$instance
|
||||
);
|
||||
$this->assertNotSame( $result, $instance );
|
||||
|
||||
// Should filter invalid key.
|
||||
$result = $widget->update(
|
||||
array(
|
||||
'imaginary_key' => 'value',
|
||||
),
|
||||
$instance
|
||||
);
|
||||
$this->assertSame( $result, $instance );
|
||||
|
||||
add_filter( 'sanitize_text_field', array( $this, '_return_wp_error' ) );
|
||||
$result = $widget->update(
|
||||
array(
|
||||
'title' => 'Title',
|
||||
),
|
||||
$instance
|
||||
);
|
||||
remove_filter( 'sanitize_text_field', array( $this, '_return_wp_error' ) );
|
||||
$this->assertSame( $result, $instance );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for Test_WP_Widget_Media::test_update().
|
||||
*
|
||||
* @return \WP_Error
|
||||
*/
|
||||
function _return_wp_error() {
|
||||
return new WP_Error( 'some-error', 'This is not valid!' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test widget method.
|
||||
*
|
||||
* @covers WP_Widget_Media::widget()
|
||||
* @covers WP_Widget_Media::render_media()
|
||||
*/
|
||||
function test_widget() {
|
||||
$args = array(
|
||||
'before_title' => '<h2>',
|
||||
'after_title' => "</h2>\n",
|
||||
'before_widget' => '<section>',
|
||||
'after_widget' => "</section>\n",
|
||||
);
|
||||
$instance = array(
|
||||
'title' => 'Foo',
|
||||
'url' => 'http://example.com/image.jpg',
|
||||
'attachment_id' => 0,
|
||||
);
|
||||
|
||||
add_filter( 'widget_mocked_instance', array( $this, 'filter_widget_mocked_instance' ), 10, 3 );
|
||||
|
||||
ob_start();
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$widget->expects( $this->atLeastOnce() )->method( 'render_media' )->with( $instance );
|
||||
$this->widget_instance_filter_args = array();
|
||||
$widget->widget( $args, $instance );
|
||||
$this->assertCount( 3, $this->widget_instance_filter_args );
|
||||
$this->assertEquals( $instance, $this->widget_instance_filter_args[0] );
|
||||
$this->assertEquals( $args, $this->widget_instance_filter_args[1] );
|
||||
$this->assertEquals( $widget, $this->widget_instance_filter_args[2] );
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( '<h2>Foo</h2>', $output );
|
||||
$this->assertContains( '<section>', $output );
|
||||
$this->assertContains( '</section>', $output );
|
||||
|
||||
// No title.
|
||||
ob_start();
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$instance['title'] = '';
|
||||
$widget->expects( $this->atLeastOnce() )->method( 'render_media' )->with( $instance );
|
||||
$widget->widget( $args, $instance );
|
||||
$output = ob_get_clean();
|
||||
$this->assertNotContains( '<h2>Foo</h2>', $output );
|
||||
|
||||
// No attachment_id nor url.
|
||||
$instance['url'] = '';
|
||||
$instance['attachment_id'] = 0;
|
||||
ob_start();
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$widget->widget( $args, $instance );
|
||||
$output = ob_get_clean();
|
||||
$this->assertEmpty( $output );
|
||||
}
|
||||
|
||||
/**
|
||||
* Args passed to the widget_{$id_base}_instance filter.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $widget_instance_filter_args = array();
|
||||
|
||||
/**
|
||||
* Filters the media widget instance prior to rendering the media.
|
||||
*
|
||||
* @param array $instance Instance data.
|
||||
* @param array $args Widget args.
|
||||
* @param WP_Widget_Media $object Widget object.
|
||||
* @return array Instance.
|
||||
*/
|
||||
function filter_widget_mocked_instance( $instance, $args, $object ) {
|
||||
$this->widget_instance_filter_args = func_get_args();
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test form method.
|
||||
*
|
||||
* @covers WP_Widget_Media::form()
|
||||
*/
|
||||
function test_form() {
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
|
||||
ob_start();
|
||||
$widget->form( array() );
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( 'name="widget-mocked[][attachment_id]"', $output );
|
||||
$this->assertContains( 'name="widget-mocked[][title]"', $output );
|
||||
$this->assertContains( 'name="widget-mocked[][url]"', $output );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test display_media_state method.
|
||||
*
|
||||
* @covers WP_Widget_Media::display_media_state()
|
||||
*/
|
||||
function test_display_media_state() {
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$attachment_id = self::factory()->attachment->create_object( array(
|
||||
'file' => DIR_TESTDATA . '/images/canola.jpg',
|
||||
'post_parent' => 0,
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
|
||||
$result = $widget->display_media_state( array(), get_post( $attachment_id ) );
|
||||
$this->assertEqualSets( array(), $result );
|
||||
|
||||
$widget->save_settings( array(
|
||||
array(
|
||||
'attachment_id' => $attachment_id,
|
||||
),
|
||||
) );
|
||||
$result = $widget->display_media_state( array(), get_post( $attachment_id ) );
|
||||
$this->assertEqualSets( array( $widget->l10n['media_library_state_single'] ), $result );
|
||||
|
||||
$widget->save_settings( array(
|
||||
array(
|
||||
'attachment_id' => $attachment_id,
|
||||
),
|
||||
array(
|
||||
'attachment_id' => $attachment_id,
|
||||
),
|
||||
) );
|
||||
$result = $widget->display_media_state( array(), get_post( $attachment_id ) );
|
||||
$this->assertEqualSets( array( sprintf( $widget->l10n['media_library_state_multi']['singular'], 2 ) ), $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enqueue_admin_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media::enqueue_admin_scripts()
|
||||
*/
|
||||
function test_enqueue_admin_scripts() {
|
||||
set_current_screen( 'widgets.php' );
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
$widget->enqueue_admin_scripts();
|
||||
|
||||
$this->assertTrue( wp_script_is( 'media-widgets' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test render_control_template_scripts method.
|
||||
*
|
||||
* @covers WP_Widget_Media::render_control_template_scripts
|
||||
*/
|
||||
function test_render_control_template_scripts() {
|
||||
$widget = $this->get_mocked_class_instance();
|
||||
|
||||
ob_start();
|
||||
$widget->render_control_template_scripts();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains( '<script type="text/html" id="tmpl-widget-media-mocked-control">', $output );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test has_content method.
|
||||
*
|
||||
* @covers WP_Widget_Media::has_content()
|
||||
*/
|
||||
function test_has_content() {
|
||||
if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
|
||||
$this->markTestSkipped( 'ReflectionMethod::setAccessible is only available for PHP 5.3+' );
|
||||
return;
|
||||
}
|
||||
|
||||
$attachment_id = self::factory()->attachment->create_object( array(
|
||||
'file' => DIR_TESTDATA . '/images/canola.jpg',
|
||||
'post_parent' => 0,
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
|
||||
$wp_widget_media = new ReflectionClass( 'WP_Widget_Media' );
|
||||
$has_content = $wp_widget_media->getMethod( 'has_content' );
|
||||
$has_content->setAccessible( true );
|
||||
|
||||
$result = $has_content->invokeArgs( $this->get_mocked_class_instance(), array(
|
||||
array(
|
||||
'attachment_id' => 0,
|
||||
'url' => '',
|
||||
),
|
||||
) );
|
||||
$this->assertFalse( $result );
|
||||
|
||||
$result = $has_content->invokeArgs( $this->get_mocked_class_instance(), array(
|
||||
array(
|
||||
'attachment_id' => $attachment_id,
|
||||
'url' => '',
|
||||
),
|
||||
) );
|
||||
$this->assertTrue( $result );
|
||||
|
||||
$result = $has_content->invokeArgs( $this->get_mocked_class_instance(), array(
|
||||
array(
|
||||
'attachment_id' => 0,
|
||||
'url' => 'http://example.com/image.jpg',
|
||||
),
|
||||
) );
|
||||
$this->assertTrue( $result );
|
||||
}
|
||||
}
|
||||
@@ -178,15 +178,15 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
|
||||
$expected['text'] = $instance['text'];
|
||||
$result = $widget->update( $instance, array() );
|
||||
$this->assertEquals( $result, $expected );
|
||||
|
||||
remove_filter( 'map_meta_cap', array( $this, 'grant_unfiltered_html_cap' ) );
|
||||
|
||||
add_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10, 2 );
|
||||
$this->assertFalse( current_user_can( 'unfiltered_html' ) );
|
||||
$instance['text'] = '<script>alert( "Howdy!" );</script>';
|
||||
$expected['text'] = wp_kses_post( $instance['text'] );
|
||||
$result = $widget->update( $instance, array() );
|
||||
$this->assertEquals( $result, $expected );
|
||||
remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10, 2 );
|
||||
remove_filter( 'map_meta_cap', array( $this, 'revoke_unfiltered_html_cap' ), 10 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,113 @@
|
||||
/* globals wp */
|
||||
/* jshint qunit: true */
|
||||
/* eslint-env qunit */
|
||||
/* eslint-disable no-magic-numbers */
|
||||
|
||||
( function() {
|
||||
'use strict';
|
||||
|
||||
module( 'Image Media Widget' );
|
||||
|
||||
test( 'image widget control', function() {
|
||||
var ImageWidgetControl, imageWidgetControlInstance, imageWidgetModelInstance, mappedProps, testImageUrl, templateProps;
|
||||
testImageUrl = 'http://s.w.org/style/images/wp-header-logo.png';
|
||||
equal( typeof wp.mediaWidgets.controlConstructors.media_image, 'function', 'wp.mediaWidgets.controlConstructors.media_image is a function' );
|
||||
ImageWidgetControl = wp.mediaWidgets.controlConstructors.media_image;
|
||||
ok( ImageWidgetControl.prototype instanceof wp.mediaWidgets.MediaWidgetControl, 'wp.mediaWidgets.controlConstructors.media_image subclasses wp.mediaWidgets.MediaWidgetControl' );
|
||||
|
||||
imageWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_image();
|
||||
imageWidgetControlInstance = new ImageWidgetControl({
|
||||
model: imageWidgetModelInstance
|
||||
});
|
||||
|
||||
// Test mapModelToPreviewTemplateProps() when no data is set.
|
||||
templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
|
||||
equal( templateProps.caption, undefined, 'mapModelToPreviewTemplateProps should not return attributes that are should_preview_update false' );
|
||||
equal( templateProps.attachment_id, 0, 'mapModelToPreviewTemplateProps should return default values' );
|
||||
equal( templateProps.currentFilename, '', 'mapModelToPreviewTemplateProps should return a currentFilename' );
|
||||
|
||||
// Test mapModelToPreviewTemplateProps() when data is set on model.
|
||||
imageWidgetControlInstance.model.set( { url: testImageUrl, alt: 'some alt text', link_type: 'none' } );
|
||||
templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
|
||||
equal( templateProps.currentFilename, 'wp-header-logo.png', 'mapModelToPreviewTemplateProps should set currentFilename based off of url' );
|
||||
equal( templateProps.url, testImageUrl, 'mapModelToPreviewTemplateProps should return the proper url' );
|
||||
equal( templateProps.alt, 'some alt text', 'mapModelToPreviewTemplateProps should return the proper alt text' );
|
||||
equal( templateProps.link_type, undefined, 'mapModelToPreviewTemplateProps should ignore attributes that are not needed in the preview' );
|
||||
equal( templateProps.error, false, 'mapModelToPreviewTemplateProps should return error state' );
|
||||
|
||||
// Test mapModelToPreviewTemplateProps() when error is set on model.
|
||||
imageWidgetControlInstance.model.set( 'error', 'missing_attachment' );
|
||||
templateProps = imageWidgetControlInstance.mapModelToPreviewTemplateProps();
|
||||
equal( templateProps.error, 'missing_attachment', 'mapModelToPreviewTemplateProps should return error string' );
|
||||
|
||||
// Reset model.
|
||||
imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: null });
|
||||
|
||||
// Test isSelected().
|
||||
equal( imageWidgetControlInstance.isSelected(), false, 'media_image.isSelected() should return false when no media is selected' );
|
||||
imageWidgetControlInstance.model.set({ error: 'missing_attachment', attachment_id: 777 });
|
||||
equal( imageWidgetControlInstance.isSelected(), false, 'media_image.isSelected() should return false when media is selected and error is set' );
|
||||
imageWidgetControlInstance.model.set({ error: false, attachment_id: 777 });
|
||||
equal( imageWidgetControlInstance.isSelected(), true, 'media_image.isSelected() should return true when media is selected and no error exists' );
|
||||
imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: testImageUrl });
|
||||
equal( imageWidgetControlInstance.isSelected(), true, 'media_image.isSelected() should return true when url is set and no error exists' );
|
||||
|
||||
// Reset model.
|
||||
imageWidgetControlInstance.model.set({ error: false, attachment_id: 0, url: null });
|
||||
|
||||
// Test editing of widget title.
|
||||
imageWidgetControlInstance.render();
|
||||
imageWidgetControlInstance.$el.find( '.title' ).val( 'Chicken and Ribs' ).trigger( 'input' );
|
||||
equal( imageWidgetModelInstance.get( 'title' ), 'Chicken and Ribs', 'Changing title should update model title attribute' );
|
||||
|
||||
// Test mapMediaToModelProps
|
||||
mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'file', url: testImageUrl } );
|
||||
equal( mappedProps.link_url, testImageUrl, 'mapMediaToModelProps should set file link_url according to mediaFrameProps.link' );
|
||||
mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'post', postUrl: 'https://wordpress.org/image-2/' } );
|
||||
equal( mappedProps.link_url, 'https://wordpress.org/image-2/', 'mapMediaToModelProps should set file link_url according to mediaFrameProps.link' );
|
||||
mappedProps = imageWidgetControlInstance.mapMediaToModelProps( { link: 'custom', linkUrl: 'https://wordpress.org' } );
|
||||
equal( mappedProps.link_url, 'https://wordpress.org', 'mapMediaToModelProps should set custom link_url according to mediaFrameProps.linkUrl' );
|
||||
|
||||
// Test mapModelToMediaFrameProps().
|
||||
imageWidgetControlInstance.model.set({ error: false, url: testImageUrl, 'link_type': 'custom', 'link_url': 'https://wordpress.org', 'size': 'custom', 'width': 100, 'height': 150, 'title': 'widget title', 'image_title': 'title of image' });
|
||||
mappedProps = imageWidgetControlInstance.mapModelToMediaFrameProps( imageWidgetControlInstance.model.toJSON() );
|
||||
equal( mappedProps.linkUrl, 'https://wordpress.org', 'mapModelToMediaFrameProps should set linkUrl from model.link_url' );
|
||||
equal( mappedProps.link, 'custom', 'mapModelToMediaFrameProps should set link from model.link_type' );
|
||||
equal( mappedProps.width, 100, 'mapModelToMediaFrameProps should set width when model.size is custom' );
|
||||
equal( mappedProps.height, 150, 'mapModelToMediaFrameProps should set height when model.size is custom' );
|
||||
equal( mappedProps.title, 'title of image', 'mapModelToMediaFrameProps should set title from model.image_title' );
|
||||
});
|
||||
|
||||
test( 'image widget control renderPreview', function( assert ) {
|
||||
var imageWidgetControlInstance, imageWidgetModelInstance, done;
|
||||
done = assert.async();
|
||||
|
||||
imageWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_image();
|
||||
imageWidgetControlInstance = new wp.mediaWidgets.controlConstructors.media_image({
|
||||
model: imageWidgetModelInstance
|
||||
});
|
||||
equal( imageWidgetControlInstance.$el.find( 'img' ).length, 0, 'No images should be rendered' );
|
||||
imageWidgetControlInstance.model.set({ error: false, url: 'http://s.w.org/style/images/wp-header-logo.png' });
|
||||
|
||||
// Due to renderPreview being deferred.
|
||||
setTimeout( function() {
|
||||
equal( imageWidgetControlInstance.$el.find( 'img[src="http://s.w.org/style/images/wp-header-logo.png"]' ).length, 1, 'One image should be rendered' );
|
||||
done();
|
||||
}, 50 );
|
||||
|
||||
start();
|
||||
});
|
||||
|
||||
test( 'image media model', function() {
|
||||
var ImageWidgetModel, imageWidgetModelInstance;
|
||||
equal( typeof wp.mediaWidgets.modelConstructors.media_image, 'function', 'wp.mediaWidgets.modelConstructors.media_image is a function' );
|
||||
ImageWidgetModel = wp.mediaWidgets.modelConstructors.media_image;
|
||||
ok( ImageWidgetModel.prototype instanceof wp.mediaWidgets.MediaWidgetModel, 'wp.mediaWidgets.modelConstructors.media_image subclasses wp.mediaWidgets.MediaWidgetModel' );
|
||||
|
||||
imageWidgetModelInstance = new ImageWidgetModel();
|
||||
_.each( imageWidgetModelInstance.attributes, function( value, key ) {
|
||||
equal( value, ImageWidgetModel.prototype.schema[ key ][ 'default' ], 'Should properly set default for ' + key );
|
||||
});
|
||||
});
|
||||
|
||||
})();
|
||||
@@ -0,0 +1,68 @@
|
||||
/* globals wp */
|
||||
/* jshint qunit: true */
|
||||
/* eslint-env qunit */
|
||||
/* eslint-disable no-magic-numbers */
|
||||
|
||||
( function() {
|
||||
'use strict';
|
||||
|
||||
module( 'Video Media Widget' );
|
||||
|
||||
test( 'video widget control', function() {
|
||||
var VideoWidgetControl, videoWidgetControlInstance, videoWidgetModelInstance, mappedProps, testVideoUrl;
|
||||
testVideoUrl = 'https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4';
|
||||
equal( typeof wp.mediaWidgets.controlConstructors.media_video, 'function', 'wp.mediaWidgets.controlConstructors.media_video is a function' );
|
||||
VideoWidgetControl = wp.mediaWidgets.controlConstructors.media_video;
|
||||
ok( VideoWidgetControl.prototype instanceof wp.mediaWidgets.MediaWidgetControl, 'wp.mediaWidgets.controlConstructors.media_video subclasses wp.mediaWidgets.MediaWidgetControl' );
|
||||
|
||||
videoWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_video();
|
||||
videoWidgetControlInstance = new VideoWidgetControl({
|
||||
model: videoWidgetModelInstance
|
||||
});
|
||||
|
||||
// Test mapModelToMediaFrameProps().
|
||||
videoWidgetControlInstance.model.set({ error: false, url: testVideoUrl, loop: false, preload: 'meta' });
|
||||
mappedProps = videoWidgetControlInstance.mapModelToMediaFrameProps( videoWidgetControlInstance.model.toJSON() );
|
||||
equal( mappedProps.url, testVideoUrl, 'mapModelToMediaFrameProps should set url' );
|
||||
equal( mappedProps.loop, false, 'mapModelToMediaFrameProps should set loop' );
|
||||
equal( mappedProps.preload, 'meta', 'mapModelToMediaFrameProps should set preload' );
|
||||
|
||||
// Test mapMediaToModelProps().
|
||||
mappedProps = videoWidgetControlInstance.mapMediaToModelProps( { loop: false, preload: 'meta', url: testVideoUrl, title: 'random movie file title' } );
|
||||
equal( mappedProps.title, undefined, 'mapMediaToModelProps should ignore title inputs' );
|
||||
equal( mappedProps.loop, false, 'mapMediaToModelProps should set loop' );
|
||||
equal( mappedProps.preload, 'meta', 'mapMediaToModelProps should set preload' );
|
||||
});
|
||||
|
||||
test( 'video widget control renderPreview', function( assert ) {
|
||||
var videoWidgetControlInstance, videoWidgetModelInstance, done;
|
||||
done = assert.async();
|
||||
|
||||
videoWidgetModelInstance = new wp.mediaWidgets.modelConstructors.media_video();
|
||||
videoWidgetControlInstance = new wp.mediaWidgets.controlConstructors.media_video({
|
||||
model: videoWidgetModelInstance
|
||||
});
|
||||
equal( videoWidgetControlInstance.$el.find( 'a' ).length, 0, 'No video links should be rendered' );
|
||||
videoWidgetControlInstance.model.set({ error: false, url: 'https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4' });
|
||||
|
||||
// Due to renderPreview being deferred.
|
||||
setTimeout( function() {
|
||||
equal( videoWidgetControlInstance.$el.find( 'a[href="https://videos.files.wordpress.com/AHz0Ca46/wp4-7-vaughan-r8-mastered_hd.mp4"]' ).length, 1, 'One video link should be rendered' );
|
||||
done();
|
||||
}, 50 );
|
||||
start();
|
||||
});
|
||||
|
||||
test( 'video media model', function() {
|
||||
var VideoWidgetModel, videoWidgetModelInstance;
|
||||
equal( typeof wp.mediaWidgets.modelConstructors.media_video, 'function', 'wp.mediaWidgets.modelConstructors.media_video is a function' );
|
||||
VideoWidgetModel = wp.mediaWidgets.modelConstructors.media_video;
|
||||
ok( VideoWidgetModel.prototype instanceof wp.mediaWidgets.MediaWidgetModel, 'wp.mediaWidgets.modelConstructors.media_video subclasses wp.mediaWidgets.MediaWidgetModel' );
|
||||
|
||||
videoWidgetModelInstance = new VideoWidgetModel();
|
||||
_.each( videoWidgetModelInstance.attributes, function( value, key ) {
|
||||
equal( value, VideoWidgetModel.prototype.schema[ key ][ 'default' ], 'Should properly set default for ' + key );
|
||||
});
|
||||
});
|
||||
|
||||
})();
|
||||
@@ -0,0 +1,45 @@
|
||||
/* globals wp, Backbone */
|
||||
/* jshint qunit: true */
|
||||
/* eslint-env qunit */
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
module( 'Media Widgets' );
|
||||
|
||||
test( 'namespace', function() {
|
||||
equal( typeof wp.mediaWidgets, 'object', 'wp.mediaWidgets is an object' );
|
||||
equal( typeof wp.mediaWidgets.controlConstructors, 'object', 'wp.mediaWidgets.controlConstructors is an object' );
|
||||
equal( typeof wp.mediaWidgets.modelConstructors, 'object', 'wp.mediaWidgets.modelConstructors is an object' );
|
||||
equal( typeof wp.mediaWidgets.widgetControls, 'object', 'wp.mediaWidgets.widgetControls is an object' );
|
||||
equal( typeof wp.mediaWidgets.handleWidgetAdded, 'function', 'wp.mediaWidgets.handleWidgetAdded is an function' );
|
||||
equal( typeof wp.mediaWidgets.handleWidgetUpdated, 'function', 'wp.mediaWidgets.handleWidgetUpdated is an function' );
|
||||
equal( typeof wp.mediaWidgets.init, 'function', 'wp.mediaWidgets.init is an function' );
|
||||
});
|
||||
|
||||
test( 'media widget control', function() {
|
||||
equal( typeof wp.mediaWidgets.MediaWidgetControl, 'function', 'wp.mediaWidgets.MediaWidgetControl' );
|
||||
ok( wp.mediaWidgets.MediaWidgetControl.prototype instanceof Backbone.View, 'wp.mediaWidgets.MediaWidgetControl subclasses Backbone.View' );
|
||||
});
|
||||
|
||||
test( 'media widget model', function() {
|
||||
var widgetModelInstance;
|
||||
equal( typeof wp.mediaWidgets.MediaWidgetModel, 'function', 'wp.mediaWidgets.MediaWidgetModel is a function' );
|
||||
ok( wp.mediaWidgets.MediaWidgetModel.prototype instanceof Backbone.Model, 'wp.mediaWidgets.MediaWidgetModel subclasses Backbone.Model' );
|
||||
|
||||
widgetModelInstance = new wp.mediaWidgets.MediaWidgetModel();
|
||||
equal( widgetModelInstance.get( 'title' ), '', 'wp.mediaWidgets.MediaWidgetModel defaults title to empty string' );
|
||||
equal( widgetModelInstance.get( 'attachment_id' ), 0, 'wp.mediaWidgets.MediaWidgetModel defaults attachment_id to 0' );
|
||||
equal( widgetModelInstance.get( 'url' ), 0, 'wp.mediaWidgets.MediaWidgetModel defaults url to empty string' );
|
||||
|
||||
widgetModelInstance.set({
|
||||
title: 'chicken and ribs',
|
||||
attachment_id: '1',
|
||||
url: 'https://wordpress.org'
|
||||
});
|
||||
equal( widgetModelInstance.get( 'title' ), 'chicken and ribs', 'wp.mediaWidgets.MediaWidgetModel properly sets the title attribute' );
|
||||
equal( widgetModelInstance.get( 'url' ), 'https://wordpress.org', 'wp.mediaWidgets.MediaWidgetModel properly sets the url attribute' );
|
||||
equal( widgetModelInstance.get( 'attachment_id' ), 1, 'wp.mediaWidgets.MediaWidgetModel properly sets and casts the attachment_id attribute' );
|
||||
});
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user