2021-02-18 00:01:25 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2021-02-18 00:10:55 +00:00
|
|
|
* Tests for the WC_Admin_Dashboard_Setup class.
|
2021-02-18 00:01:25 +00:00
|
|
|
*
|
|
|
|
* @package WooCommerce\Tests\Admin
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-02-18 00:10:55 +00:00
|
|
|
* Class WC_Admin_Dashboard_Setup_Test
|
2021-02-18 00:01:25 +00:00
|
|
|
*/
|
2021-02-18 00:10:55 +00:00
|
|
|
class WC_Admin_Dashboard_Setup_Test extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
2021-03-29 15:04:54 +00:00
|
|
|
// Set default country to non-US so that 'payments' task gets added but 'woocommerce-payments' doesn't,
|
|
|
|
// by default it won't be considered completed but we can manually change that as needed.
|
|
|
|
update_option( 'woocommerce_default_country', 'JP' );
|
|
|
|
|
2021-02-18 00:10:55 +00:00
|
|
|
parent::setUp();
|
|
|
|
}
|
2021-02-18 00:01:25 +00:00
|
|
|
|
2021-03-29 15:04:54 +00:00
|
|
|
/**
|
|
|
|
* Tear down
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
remove_all_filters( 'woocommerce_available_payment_gateways' );
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2021-02-18 00:01:25 +00:00
|
|
|
/**
|
|
|
|
* Includes widget class and return the class.
|
|
|
|
*
|
2021-02-18 00:10:55 +00:00
|
|
|
* @return WC_Admin_Dashboard_Setup
|
2021-02-18 00:01:25 +00:00
|
|
|
*/
|
|
|
|
public function get_widget() {
|
2021-02-18 00:10:55 +00:00
|
|
|
return include __DIR__ . '/../../../../includes/admin/class-wc-admin-dashboard-setup.php';
|
2021-02-18 00:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return widget output (HTML).
|
|
|
|
*
|
|
|
|
* @return string Render widget HTML
|
|
|
|
*/
|
|
|
|
public function get_widget_output() {
|
2021-02-18 00:10:55 +00:00
|
|
|
update_option( 'woocommerce_task_list_hidden', 'no' );
|
2021-02-18 00:01:25 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->get_widget()->render();
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests widget does not get rendered when woocommerce_task_list_hidden or woocommerce_task_list_hidden
|
|
|
|
* is true.
|
|
|
|
*
|
|
|
|
* @dataProvider should_display_widget_data_provider
|
|
|
|
*
|
|
|
|
* @param array $options a set of options.
|
|
|
|
*/
|
|
|
|
public function test_widget_does_not_get_rendered( array $options ) {
|
|
|
|
global $wp_meta_boxes;
|
|
|
|
|
|
|
|
foreach ( $options as $name => $value ) {
|
|
|
|
update_option( $name, $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->get_widget();
|
|
|
|
$this->assertNull( $wp_meta_boxes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given both woocommerce_task_list_hidden and woocommerce_task_list_complete are false
|
|
|
|
* Then the widget should be added to the $wp_meta_boxes
|
|
|
|
*/
|
|
|
|
public function test_widget_gets_rendered_when_both_options_are_false() {
|
|
|
|
global $wp_meta_boxes;
|
|
|
|
update_option( 'woocommerce_task_list_complete', false );
|
|
|
|
update_option( 'woocommerce_task_list_hidden', false );
|
|
|
|
|
|
|
|
$this->get_widget();
|
2021-02-23 03:46:31 +00:00
|
|
|
$this->assertArrayHasKey( 'wc_admin_dashboard_setup', $wp_meta_boxes['dashboard']['normal']['high'] );
|
2021-02-18 00:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-29 15:04:54 +00:00
|
|
|
* Tests the widget output when 1 task has been completed.
|
2021-02-18 00:01:25 +00:00
|
|
|
*/
|
|
|
|
public function test_initial_widget_output() {
|
2021-03-29 15:04:54 +00:00
|
|
|
// Force the "payments" task to be considered incomplete.
|
|
|
|
add_filter(
|
|
|
|
'woocommerce_available_payment_gateways',
|
|
|
|
function() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-02-18 00:01:25 +00:00
|
|
|
$html = $this->get_widget_output();
|
|
|
|
|
|
|
|
$required_strings = array(
|
2021-03-29 15:04:54 +00:00
|
|
|
'Step 0 of 6',
|
2021-02-18 00:01:25 +00:00
|
|
|
'You're almost there! Once you complete store setup you can start receiving orders.',
|
|
|
|
'Start selling',
|
|
|
|
'admin.php\?page=wc-admin&path=%2Fsetup-wizard',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $required_strings as $required_string ) {
|
|
|
|
$this->assertRegexp( "/${required_string}/", $html );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests completed task count as it completes one by one
|
|
|
|
*/
|
|
|
|
public function test_widget_renders_completed_task_count() {
|
2021-03-29 15:04:54 +00:00
|
|
|
// Force the "payments" task to be considered completed
|
|
|
|
// by faking a valid payment gateway.
|
|
|
|
add_filter(
|
|
|
|
'woocommerce_available_payment_gateways',
|
|
|
|
function() {
|
|
|
|
return array(
|
|
|
|
new class() extends WC_Payment_Gateway {
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$completed_tasks = array( 'payments' );
|
2021-02-18 00:01:25 +00:00
|
|
|
$tasks = $this->get_widget()->get_tasks();
|
|
|
|
$tasks_count = count( $tasks );
|
2021-03-29 15:04:54 +00:00
|
|
|
unset( $tasks['payments'] ); // That one is completed already.
|
2021-02-18 00:01:25 +00:00
|
|
|
foreach ( $tasks as $key => $task ) {
|
|
|
|
array_push( $completed_tasks, $key );
|
|
|
|
update_option( 'woocommerce_task_list_tracked_completed_tasks', $completed_tasks );
|
|
|
|
$completed_tasks_count = count( $completed_tasks );
|
|
|
|
// When all tasks are completed, assert that the widget output is empty.
|
|
|
|
// As widget won't be rendered when tasks are completed.
|
|
|
|
if ( $completed_tasks_count === $tasks_count ) {
|
|
|
|
$this->assertEmpty( $this->get_widget_output() );
|
|
|
|
} else {
|
2021-03-29 15:04:54 +00:00
|
|
|
$this->assertRegexp( "/Step ${completed_tasks_count} of 6/", $this->get_widget_output() );
|
2021-02-18 00:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides dataset that controls output of `should_display_widget`
|
|
|
|
*/
|
|
|
|
public function should_display_widget_data_provider() {
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
array(
|
2021-02-18 00:10:55 +00:00
|
|
|
'woocommerce_task_list_complete' => 'yes',
|
2021-03-29 15:04:54 +00:00
|
|
|
'woocommerce_task_list_hidden' => 'no',
|
2021-02-18 00:01:25 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array(
|
2021-02-18 00:10:55 +00:00
|
|
|
'woocommerce_task_list_complete' => 'no',
|
2021-03-29 15:04:54 +00:00
|
|
|
'woocommerce_task_list_hidden' => 'yes',
|
2021-02-18 00:01:25 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|