1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| <?php
class SampleTest extends WP_UnitTestCase {
public function test_widget_construct() { $widget = new SampleWidget(); $this->assertSame( 'sample-widget', $widget->id_base ); $this->assertSame( 'sample-widget', $widget->widget_options['classname'] ); $this->assertTrue( $widget->widget_options['customize_selective_refresh'] ); $this->assertSame( 400, $widget->control_options['width'] ); $this->assertSame( 350, $widget->control_options['height'] ); }
function test_widget_register() { global $wp_widget_factory;
register_widget('SampleWidget'); $this->assertArrayHasKey( 'SampleWidget', $wp_widget_factory->widgets );
unregister_widget('SampleWidget'); $this->assertArrayNotHasKey( 'SampleWidget', $wp_widget_factory->widgets ); }
function test_widget_widget() { register_widget('SampleWidget');
$this->expectOutputString('<div class="test widget sample-widget"><h2 class="test widget">Sample Widget</h2><span id="message">Hello, world</span></div>');
$instance = [ 'count' => 1, 'dropdown' => 0, 'hierarchical' => 0, 'message' => 'Hello, world', 'title' => 'Sample Widget', ];
$args = [ 'before_widget' => '<div class="test widget %1$s">', 'after_widget' => '</div>', 'before_title' => '<h2 class="test widget">', 'after_title' => '</h2>', ];
the_widget( 'SampleWidget', $instance, $args );
unregister_widget('SampleWidget'); }
public function test_update() { $instance = [ 'message' => 'Hello, world', ]; $expected = [ 'message' => 'Hello, world', ];
$widget = new SampleWidget(); $result = $widget->update($instance, []);
$this->assertEquals($expected, $result); }
public function test_update_validate($message, $expectedMessage) { $instance = [ 'message' => $message, ]; $expected = [ 'message' => $expectedMessage, ];
$widget = new SampleWidget(); $result = $widget->update($instance, []);
$this->assertEquals($expected, $result); }
public function provider_update_validate() { return [ 'tag' => ['<alert>Hello, world</alert>', 'Hello, world'], '>' => ['>Hello, world<', '>Hello, world<'], 'tab' => ["Hello, world", 'Hello, world'], 'newline' => ["Hello,\nworld", 'Hello, world'], ]; } }
|