-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add task using TaskLists::add_task
API
#2026
Conversation
`woocommerce_admin_onboarding_task_list` is deprecated. Fixes #2024 Remove `remove_woocommerce_extended_task_list_item` hook.
21a172c
to
ecdee9b
Compare
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## develop #2026 +/- ##
===========================================
+ Coverage 58.1% 58.2% +0.2%
+ Complexity 4119 4117 -2
===========================================
Files 455 454 -1
Lines 17695 17681 -14
===========================================
+ Hits 10278 10297 +19
+ Misses 7417 7384 -33
Flags with carried forward coverage won't be shown. Click here to find out more.
|
remove the need for `CompleteSetup` class
Re-generate those docs.
a482406
to
2cc3cd7
Compare
477b938
to
cb99926
Compare
cb99926
to
29f2e36
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes. It's working well and I'm seeing the task being added.
I just left some comments on how to fix the tests as the WP hooks shouldn't be triggered twice.
// Mock tasks list. | ||
TaskLists::clear_lists(); | ||
TaskLists::add_list( [ 'id' => 'extended' ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the WP hooks have already been called before the unit tests starts. So at this point it's already triggered the register
code and init
code which adds the task to the list.
So what we are really doing here is clearing the WC task list with our custom task added. And then we try to add it again in test_register
but we shouldn't trigger the init
hook a second time as that calls all kinds of code we don't want to run a second time.
My suggestion would be to remove these lines of code and leave TaskLists as is (so we are working with a non mocked version of TaskLists).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation :)
Implemented in e9d7c21
But that means we will not be asserting the actual timing of the registration.
Are we fine with that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, CodeCov complains about no coverage for the register
method https://app.codecov.io/gh/woocommerce/google-listings-and-ads/pull/2026/blob/src/TaskList/CompleteSetupTask.php#L27
public function test_register() { | ||
$this->task->register(); | ||
do_action( 'init', 1 ); | ||
|
||
$this->assertGreaterThan( 0, did_action( 'add_woocommerce_extended_task_list_item' ) ); | ||
|
||
$this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); | ||
$this->assertEquals( $this->task, TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the comment above we shouldn't register and call init a second time here. Instead we should just test that it was registered and the hook was called.
The task is never going to be an identical object so at most I'd compare the ID or just leave it at assertNotNull since we are already fetching a specific ID so if that returns something it should always be the right one.
public function test_register() { | |
$this->task->register(); | |
do_action( 'init', 1 ); | |
$this->assertGreaterThan( 0, did_action( 'add_woocommerce_extended_task_list_item' ) ); | |
$this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); | |
$this->assertEquals( $this->task, TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); | |
} | |
public function test_register() { | |
$this->assertEquals( 1, did_action( 'add_woocommerce_extended_task_list_item' ) ); | |
$this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); | |
$this->assertEquals( | |
$this->task->get_id(), | |
TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() )->get_id() | |
); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should not create any instance but test the behavior of the already registered one
1c7b5fb
src/TaskList/CompleteSetupTask.php
Outdated
$this->task_list = TaskLists::get_list( $list_id ); | ||
TaskLists::add_task( $list_id, $this ); | ||
|
||
do_action( 'add_woocommerce_extended_task_list_item', $this->get_id() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems a previous comment I made about this hook got lost. But I was questioning if we still need to call this hook, we don't use it anywhere in our code and neither does WC need it to add the task to the list. It seems convenient for testing to confirm the function was called, but as long as we are testing the task got added to the list then I don't think that's necessary to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just kept it as it was to minimize the set of changes, but if you think we don't need that hook. I'm always for YAGNI and less code :)
As that is what actually matters from the feature's behavior perspective.
Co-authored-by: Mik <[email protected]>
Thank you @mikkamp for the great suggestions. I changed the tests a bit, so everything is passing. However, Codecov still complains about the lack of coverage for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks that looks a lot simpler.
I can understand the desire to get 100% unit test coverage. Although this case might be a bit tricky since the register function would be better covered in an integration test. PHPunit looks for a line of code in the tests which triggers the "covered" code, but that's only happening (triggered by bootstrap.php) before it starts the actual tests. So at most you could add @codeCoverageIgnore
to the register function (but we haven't done that much in other cases).
Also note that if register / init would not be called then this part of the test would fail:
// Fetch the task from the global list.
$this->task = TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' );
$this->task->set_merchant_center_object( $this->merchant_center );
Because $this->task
would be false if the task is not found, so the function set_merchant_center_object
can't be called. So we are testing the result of register, but just not proving it in the code coverage. One improvement we could do is assertInstanceOf
to confirm that $this->task
is the expected object, as that will help with troubleshooting if it ever fails.
Either way I'm going to approve the PR as the actual code changes performs as expected.
…` in case the task is not found. Addresses #2026 (review)
b57c8d3
to
3733294
Compare
Thanks for the clarifications. I agree it's more of an integration test, but I was pretty surprised that Codecov reported as "uncovered" lines that were actually executed. It's a pity it was covered before Codecov checks, but I think as long as it's actually tested, we're safe.
Yep, that's why I put it there. There is a small downside that it's in |
…` in case the task is not found. Addresses woocommerce#2026 (review)
Changes proposed in this Pull Request:
TaskLists::add_task
API,woocommerce_admin_onboarding_task_list
is deprecated.add_woocommerce_extended_task_list_item
&remove_woocommerce_extended_task_list_item
hooks.Re-generate those docs.
Screenshots:
Detailed test instructions:
/wp-admin/admin.php?page=wc-admin
/wp-admin/admin.php?page=wc-admin
Additional details:
register
method is being called a few times for some reason. I don't know how to make sure the callback is called only once in https://github.com/woocommerce/google-listings-and-ads/pull/2026/files#diff-b45e7e0b771cc2f644e3a4805a651b28213c4f6464b615c7c26e947275a3d52fR27-R39Currently, it does not introduce any problem, just does unnecessary runs, but it could lead to unexpected quirks in the future.
Tests are checking it was called at least once.
Changelog entry