Task List: Avoid fetching tasks on non-visible lists (#45498)

This commit is contained in:
Paul Sealock 2024-03-13 23:18:17 +13:00 committed by GitHub
parent 93c7ebfed4
commit 2f88da580f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 94 additions and 12 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Comment: Fix previously unreleased change

View File

@ -740,14 +740,6 @@ class OnboardingTasks extends \WC_REST_Data_Controller {
$lists = is_array( $task_list_ids ) && count( $task_list_ids ) > 0 ? TaskLists::get_lists_by_ids( $task_list_ids ) : TaskLists::get_lists();
// We have no use for hidden lists, it's expensive to compute individual tasks completion.
$lists = array_filter(
$lists,
function( $list ) {
return ! $list->is_hidden();
}
);
$json = array_map(
function( $list ) {
return $list->sort_tasks()->get_json();

View File

@ -414,10 +414,14 @@ class TaskList {
public function get_json() {
$this->possibly_track_completion();
$tasks_json = array();
foreach ( $this->tasks as $task ) {
$json = $task->get_json();
if ( $json['canView'] ) {
$tasks_json[] = $json;
// We have no use for hidden lists, it's expensive to compute individual tasks completion.
if ( $this->is_visible() ) {
foreach ( $this->tasks as $task ) {
$json = $task->get_json();
if ( $json['canView'] ) {
$tasks_json[] = $json;
}
}
}

View File

@ -25,6 +25,16 @@ class WC_Tests_OnboardingTasks_TaskLists extends WC_Unit_Test_Case {
TaskLists::clear_lists();
}
/**
* Tear down
*/
public function tearDown(): void {
TaskLists::clear_lists();
TaskLists::init_default_lists();
parent::tearDown();
}
/**
* Tests that the "woocommerce_admin_experimental_onboarding_tasklists" filter is able to append tasks to any tasklist.
*/
@ -56,4 +66,76 @@ class WC_Tests_OnboardingTasks_TaskLists extends WC_Unit_Test_Case {
// Assert that the new task list is added.
$this->assertNotEmpty( TaskLists::get_list( 'test' ) );
}
/**
* Tests that hidden task lists don't return their tasks.
*/
public function test_tasklists_get_json_hidden_list() {
// Create a new task list.
$task_list = new TaskList(
array(
'id' => 'test',
'title' => 'Test',
)
);
// Create a new task.
$task = new TestTask(
$task_list,
array(
'id' => 'wc-unit-test_tasklists_get_json_hidden_list',
)
);
// Add task to task list.
$task_list->add_task( $task );
// Hide the task list.
$task_list->hide();
// Get the task list as JSON.
$json = $task_list->get_json();
// Assert that the task list is empty because it is hidden.
$this->assertEmpty( $json['tasks'] );
// Assert list is hidden.
$this->assertTrue( $json['isHidden'] );
}
/**
* Tests that visible tasks do return their tasks.
*/
public function test_tasklists_get_json_visible_list() {
// Create a new task list.
$task_list = new TaskList(
array(
'id' => 'test',
'title' => 'Test',
)
);
// Create a new task.
$task = new TestTask(
$task_list,
array(
'id' => 'wc-unit-test_tasklists_get_json_visible_list',
)
);
// Add task to task list.
$task_list->add_task( $task );
// Make sure the list is visible.
$task_list->unhide();
// Get the task list as JSON.
$json = $task_list->get_json();
// Assert that the task list has one task.
$this->assertCount( 1, $json['tasks'] );
// Assert we have the task we added.
$this->assertEquals( 'wc-unit-test_tasklists_get_json_visible_list', $json['tasks'][0]['id'] );
}
}