2021-09-20 21:17:22 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Test the Task class.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Admin\Tests\OnboardingTasks
|
|
|
|
*/
|
|
|
|
|
2021-11-16 13:59:55 +00:00
|
|
|
require_once __DIR__ . '/test-task.php';
|
|
|
|
|
2022-03-01 12:34:18 +00:00
|
|
|
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
|
|
|
|
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskList;
|
|
|
|
|
2021-09-20 21:17:22 +00:00
|
|
|
/**
|
|
|
|
* class WC_Tests_OnboardingTasks_Task
|
|
|
|
*/
|
|
|
|
class WC_Tests_OnboardingTasks_Task extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task is visible by default.
|
|
|
|
*/
|
|
|
|
public function test_capability_visible() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertEquals( true, $task->can_view() );
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task is not visible when not capable of being viewed.
|
|
|
|
*/
|
|
|
|
public function test_capability_not_visible() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
'can_view' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertEquals( false, $task->can_view() );
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task can be dismissed.
|
|
|
|
*/
|
|
|
|
public function test_dismiss() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
'is_dismissable' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$update = $task->dismiss();
|
|
|
|
$dismissed = get_option( Task::DISMISSED_OPTION, array() );
|
|
|
|
$this->assertEquals( true, $update );
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertContains( $task->get_id(), $dismissed );
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a dismissal can be undone.
|
|
|
|
*/
|
|
|
|
public function test_undo_dismiss() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
'is_dismissable' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$task->dismiss();
|
|
|
|
$task->undo_dismiss();
|
|
|
|
$dismissed = get_option( Task::DISMISSED_OPTION, array() );
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertNotContains( $task->get_id(), $dismissed );
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a non-dismissable task cannot be dismissed.
|
|
|
|
*/
|
|
|
|
public function test_not_dismissable() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-non-dismissable-task',
|
|
|
|
'is_dismissable' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$update = $task->dismiss();
|
|
|
|
$dismissed = get_option( Task::DISMISSED_OPTION, array() );
|
|
|
|
$this->assertEquals( false, $update );
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertNotContains( $task->get_id(), $dismissed );
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task can be snoozed.
|
|
|
|
*/
|
|
|
|
public function test_snooze() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-snoozeable-task',
|
|
|
|
'is_snoozeable' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$update = $task->snooze();
|
|
|
|
$snoozed = get_option( Task::SNOOZED_OPTION, array() );
|
|
|
|
$this->assertEquals( true, $update );
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertArrayHasKey( $task->get_id(), $snoozed );
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task can be unsnoozed.
|
|
|
|
*/
|
|
|
|
public function test_undo_snooze() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-snoozeable-task',
|
|
|
|
'is_snoozeable' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$task->snooze();
|
|
|
|
$task->undo_snooze();
|
|
|
|
$snoozed = get_option( Task::SNOOZED_OPTION, array() );
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertArrayNotHasKey( $task->get_id(), $snoozed );
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task's snooze time is automatically added.
|
|
|
|
*/
|
|
|
|
public function test_snoozed_until() {
|
|
|
|
$time = time() * 1000;
|
|
|
|
$snoozed = get_option( Task::SNOOZED_OPTION, array() );
|
|
|
|
$snoozed['wc-unit-test-task'] = $time;
|
|
|
|
update_option( Task::SNOOZED_OPTION, $snoozed );
|
|
|
|
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
'is_snoozeable' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertEquals( $time, $task->get_snoozed_until() );
|
2021-09-20 21:17:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a non snoozeable task cannot be snoozed.
|
|
|
|
*/
|
|
|
|
public function test_not_snoozeable() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-snoozeable-task',
|
|
|
|
'is_snoozeable' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$task->snooze();
|
|
|
|
$this->assertEquals( false, $task->is_snoozed() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task is no longer consider snoozed after the time has passed.
|
|
|
|
*/
|
|
|
|
public function test_snooze_time() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-snoozeable-task',
|
|
|
|
'is_snoozeable' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$time = time() * 1000 - 1;
|
|
|
|
$snoozed = get_option( Task::SNOOZED_OPTION, array() );
|
|
|
|
$snoozed['wc-unit-test-snoozeable-task'] = $time;
|
|
|
|
update_option( Task::SNOOZED_OPTION, $snoozed );
|
|
|
|
|
|
|
|
$this->assertEquals( false, $task->is_snoozed() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that a task's properties are returned as JSON.
|
|
|
|
*/
|
|
|
|
public function test_json() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-09-20 21:17:22 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$json = $task->get_json();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'id', $json );
|
|
|
|
$this->assertArrayHasKey( 'title', $json );
|
|
|
|
$this->assertArrayHasKey( 'content', $json );
|
|
|
|
$this->assertArrayHasKey( 'actionLabel', $json );
|
|
|
|
$this->assertArrayHasKey( 'actionUrl', $json );
|
|
|
|
$this->assertArrayHasKey( 'isComplete', $json );
|
2021-09-29 23:47:01 +00:00
|
|
|
$this->assertArrayHasKey( 'canView', $json );
|
2021-09-20 21:17:22 +00:00
|
|
|
$this->assertArrayHasKey( 'time', $json );
|
|
|
|
$this->assertArrayHasKey( 'isDismissed', $json );
|
|
|
|
$this->assertArrayHasKey( 'isDismissable', $json );
|
|
|
|
$this->assertArrayHasKey( 'isSnoozed', $json );
|
|
|
|
$this->assertArrayHasKey( 'isSnoozeable', $json );
|
|
|
|
$this->assertArrayHasKey( 'snoozedUntil', $json );
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:07:50 +00:00
|
|
|
/**
|
|
|
|
* Tests that a task can be actioned.
|
|
|
|
*/
|
|
|
|
public function test_action_task() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-05 17:07:50 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$update = $task->mark_actioned();
|
|
|
|
$actioned = get_option( Task::ACTIONED_OPTION, array() );
|
|
|
|
$this->assertEquals( true, $update );
|
2021-11-16 13:59:55 +00:00
|
|
|
$this->assertContains( $task->get_id(), $actioned );
|
2021-10-05 17:07:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 18:28:52 +00:00
|
|
|
/**
|
|
|
|
* Test task sort with empty config.
|
|
|
|
*/
|
|
|
|
public function test_sort_with_empty_sort_by_config() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_a = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'a',
|
|
|
|
)
|
|
|
|
);
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_b = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'b',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = Task::sort( $task_a, $task_b );
|
|
|
|
$this->assertEquals( 0, $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test task sort with config with invalid key.
|
|
|
|
*/
|
|
|
|
public function test_sort_with_sort_by_config_with_invalid_key() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_a = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'a',
|
|
|
|
)
|
|
|
|
);
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_b = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'b',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = Task::sort(
|
|
|
|
$task_a,
|
|
|
|
$task_b,
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'key' => 'invalid',
|
|
|
|
'order' => 'asc',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals( 0, $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test task sort with config with valid key asc.
|
|
|
|
*/
|
|
|
|
public function test_sort_with_sort_by_config_with_valid_key_asc() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_a = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'a',
|
|
|
|
'level' => 1,
|
|
|
|
)
|
|
|
|
);
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_b = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'b',
|
|
|
|
'level' => 2,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = Task::sort(
|
|
|
|
$task_a,
|
|
|
|
$task_b,
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'key' => 'level',
|
|
|
|
'order' => 'asc',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals( -1, $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test task sort with config with valid key desc.
|
|
|
|
*/
|
|
|
|
public function test_sort_with_sort_by_config_with_valid_key_desc() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_a = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'a',
|
|
|
|
'level' => 1,
|
|
|
|
)
|
|
|
|
);
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_b = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'b',
|
|
|
|
'level' => 2,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$result = Task::sort(
|
|
|
|
$task_a,
|
|
|
|
$task_b,
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'key' => 'level',
|
|
|
|
'order' => 'desc',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEquals( 1, $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test task sort with config with multiple keys.
|
|
|
|
*/
|
|
|
|
public function test_sort_with_sort_by_config_with_multiple_keys() {
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_a = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'a',
|
|
|
|
'level' => 2,
|
|
|
|
'is_complete' => false,
|
|
|
|
)
|
|
|
|
);
|
2021-11-16 13:59:55 +00:00
|
|
|
$task_b = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2021-10-12 18:28:52 +00:00
|
|
|
array(
|
|
|
|
'id' => 'b',
|
|
|
|
'level' => 2,
|
|
|
|
'is_complete' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$sort_by = array(
|
|
|
|
array(
|
|
|
|
'key' => 'level',
|
|
|
|
'order' => 'asc',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'key' => 'is_complete',
|
|
|
|
'order' => 'asc',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$result = Task::sort( $task_a, $task_b, $sort_by );
|
|
|
|
// Given levels are the same it should return the comparison of is_complete.
|
|
|
|
$this->assertEquals( -1, $result );
|
|
|
|
|
|
|
|
$task_a->is_complete = true;
|
|
|
|
$result = Task::sort( $task_a, $task_b, $sort_by );
|
|
|
|
// Given levels are the same it should return the comparison of is_complete.
|
|
|
|
$this->assertEquals( 0, $result );
|
|
|
|
}
|
2022-01-18 20:29:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the parent list ID is retreived.
|
|
|
|
*/
|
|
|
|
public function test_get_list_id() {
|
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'setup' ) ),
|
2022-01-18 20:29:14 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( 'extended', $task->get_list_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that tracks events are recorded with the correct IDs.
|
|
|
|
*/
|
|
|
|
public function test_record_tracks_event() {
|
|
|
|
$task = new TestTask(
|
2022-03-01 12:34:18 +00:00
|
|
|
new TaskList( array( 'id' => 'extended' ) ),
|
2022-01-18 20:29:14 +00:00
|
|
|
array(
|
|
|
|
'id' => 'wc-unit-test-task',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( 'extended_tasklist_test_event', $task->record_tracks_event( 'test_event' ) );
|
|
|
|
}
|
2021-09-20 21:17:22 +00:00
|
|
|
}
|
|
|
|
|