woocommerce/tests/legacy/unit-tests/widgets/class-wc-tests-widget.php

68 lines
1.7 KiB
PHP
Raw Normal View History

2017-12-14 11:57:53 +00:00
<?php
/**
* Testing WC_Widget functionality.
*
2020-08-05 16:36:24 +00:00
* @package WooCommerce\Tests\Widgets
2017-12-14 11:57:53 +00:00
*/
/**
* Class for testing WC_Widget functionality.
*/
class WC_Tests_Widget extends WC_Unit_Test_Case {
/**
* Test intance creation
*
* @return void
*/
public function test_instance() {
2020-08-26 21:46:44 +00:00
require_once __DIR__ . '/class-dummy-widget.php';
2017-12-14 11:57:53 +00:00
$dummy_widget = new Dummy_Widget();
$this->assertTrue( property_exists( $dummy_widget, 'widget_id' ) );
}
/**
* Test widget caching.
*
* @return void
*/
2017-12-14 11:57:53 +00:00
public function test_caching() {
global $wp_widget_factory;
2020-08-26 21:46:44 +00:00
require_once __DIR__ . '/class-dummy-widget.php';
2017-12-14 11:57:53 +00:00
register_widget( 'Dummy_Widget' );
$dummy_widget = $wp_widget_factory->widgets['Dummy_Widget'];
// Uncached widget.
ob_start();
$cache_hit = $dummy_widget->get_cached_widget( array( 'widget_id' => $dummy_widget->widget_id ) );
$output = ob_get_clean();
$this->assertFalse( $cache_hit );
$this->assertEmpty( $output );
// Render widget to prime the cache.
ob_start();
2017-12-14 11:57:53 +00:00
$dummy_widget->widget( array( 'widget_id' => $dummy_widget->widget_id ), array() );
ob_get_clean();
// Cached widget.
ob_start();
$cache_hit = $dummy_widget->get_cached_widget( array( 'widget_id' => $dummy_widget->widget_id ) );
$output = ob_get_clean();
$this->assertTrue( $cache_hit );
$this->assertEquals( 'Dummy', $output );
2017-12-14 11:57:53 +00:00
}
/**
* Test widget form.
*
* @return void
*/
2017-12-14 11:57:53 +00:00
public function test_form() {
global $wp_widget_factory;
2020-08-26 21:46:44 +00:00
require_once __DIR__ . '/class-dummy-widget.php';
2017-12-14 11:57:53 +00:00
register_widget( 'Dummy_Widget' );
$dummy_widget = $wp_widget_factory->widgets['Dummy_Widget'];
$this->assertEmpty( $dummy_widget->form( array( 'widget_id' => $dummy_widget->widget_id ) ) );
}
}