Skip to content
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

Fix/missing wp user type #988

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from
Open

Conversation

eddiesshop
Copy link

@eddiesshop eddiesshop commented Aug 26, 2023

Description

I came across this bug when attempting to do the following:

$author_data = [
    'wp_user_nicename', // WP User who also happens to be a Guest Author
    'guest_author_only_nicename'
];
$coauthors_plus->add_coauthors( $post_id, $author_data, $append = false );

I placed my WP_User first in the array because of this block of code. It will update the wp_posts.post_author column for the first WP_User it finds.

However, I noticed that the wp_posts.post_author column was not being updated as I was expecting.

I started digging through the code and found that this block of code was essentially being skipped, due to the fact that this function always returns a Guest Author object.

This is actually causing the add_coauthors function to always respond with false for instances where wp_posts.post_author = 0 or where the user no longer exists on the system. Even if you're passing in a valid WP_User who is also a Guest Author, it would respond with false, as was my case.

With this fix, we retain the fact that the original user_nicename we passed is also a WP_User. In this way, we don't waste compute cycles in that foreach loop where it would never find a valid WP_User it's trying to look for.

Deploy Notes

Are there any new dependencies added that should be taken into account when deploying to WordPress.org?
No

Steps to Test

Outline the steps to test and verify the PR here.

You will need 5 authors:

  1. WP_User who is not linked to Guest Author ( user_nicename: wp_user_1 )
  2. WP_User who is not linked to Guest Author ( user_nicename: wp_user_2 )
  3. Guest Author who is not linked ( user_nicename: ga_1 )
  4. Guest Author who is not linked ( user_nicename: ga_2 )
  5. WP_User who is linked to Guest Author ( user_nicename: wp_user_and_ga_1 )

Example:

  1. Check out PR.
  2. Instantiate CoAuthor_Plus: $cap = new CoAuthor_plus();
  3. Execute $cap->add_coauthors( $post_id, [ 'wp_user_1' ] ); result should be true. wp_posts.post_author should be equal to wp_users.ID
  4. Execute $cap->add_coauthors( $post_id, [ 'ga_1', 'ga_2' ] ); result should be false. This is because $append = false and there was no wp_user passed as a CoAuthor. Notice also that wp_posts.post_author remains untouched.
  5. Execute Step 1 again, then execute Step 4 again, this time passing $append = true, like so: $cap->add_coauthors( $post_id, [ 'ga_1', 'ga_2' ], true ); result should be true. Notice, that all 3 authors are correctly displayed.
  6. Execute $cap->add_coauthors( $post_id, [ 'ga_1', 'wp_user_2' ], true ); result should be true. Notice that wp_posts.post_author is not overwritten. This is because we are appending authors to the post.
  7. Execute $cap->add_coauthors( $post_id, [ 'ga_2', 'wp_user_and_ga_1' ] ); result should be true. Notice here that the WP_User is placed second in the array, but it is correctly picked up now, and the wp_posts.post_author column is correctly updated to match the user's wp_users.ID column.

@eddiesshop
Copy link
Author

The previous 2 commits address another issue I found during some internal testing.

What was happening was that using get_coauthor_by and searching with a Guest Author, if that Guest Author has a valid link to a WP_User, it is summarily ignored. Functions like add_coauthors expect at least one coauthor to be a valid WP_User so that the wp_posts.post_author column can be appropriately updated. The only case where this function is returning an expected value is when you search by the WP_User first. When it arrives at $guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force );, $guest_author === false. It is then forced to move to the switch statement to find a user via their WP_User data.

With this refactor, when you arrive at $guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force ); and a valid $guest_author is obtained, it will now check if the linked_account attribute is set. If so, it will attempt to find the corresponding user for the Guest Account. Crucially, this change still gives priority to returning a Guest Author first and foremost. Only when a Guest Author is not found, will it search for a WP_User. If found, it will also search to see if a linked Guest Author account exists. If it does, it will return that Guest Author object instead, without losing the fact that this account also has a WP_User associated with it. If the plugin is disabled, then it will attempt to find a WP_User.

So using the examples outlined in the description, the below scenario should be tested as well:
8. Execute $cap->add_coauthors( $post_id, [ 'wp_user_2', 'ga_1', 'wp_user_and_ga_1' ] ); result should be true. Notice that wp_posts.post_author is equal to the ID for wp_user_2. Also, all 3 authors can be found when looking at the terms associated to $post_id.
9. Execute $cap->add_coauthors( $post_id, [ 'wp_user_and_ga_1', 'ga_1', 'wp_user_2' ] ); result should be same as above, except now wp_posts.post_author is linked to wp_user_and_ga_1.
10. Execute $cap->add_coauthors( $post_id, [ 'ga_1' ] ); result should be false. Since this is a pure GA account, wp_post.post_author is not updated, however we can see a term-taxonomy associated with this $post_id. Follow this up by executing $cap->add_coauthors( $post_id, [ 'wp_user_2', 'wp_user_and_ga_1' ], true ); the result should be true. Notice that wp_posts.post_author is equal to wp_user_2's user ID.

@GaryJones
Copy link
Contributor

Thanks @eddiesshop. A couple of quick meta questions:

  1. Should/Can the fix for the extra issue go into a new PR?
  2. Since most of the steps are to execute code, could you please add suitable tests (happy and sad paths) for each fix?

This might be a problematic decision. But the way I justify this change is that if you are appending co-authors, there may already be a WP_User set as the author. So we don't really have to care whether one is passed or not. Because of this, we do not need to forcibly return a `false` flag since that is confusing to the caller, especially because we actually do save the guest authors which are given in the call! Instead, if the $append flag is false, we should expect that at least one user will be a WP_User. In that case, if none is passed in, then there is a mismatch of the intended authors. Because now, the `wp_posts.post_author` column will have an old `wp_users.ID` which remains set and most likely isn't the intent of the caller.
Also, returning the actual response from the DB, to make this call even more accurate in terms of what is actually happen at the DB layer.
A pure WP_User (i.e. a WP_User that IS NOT linked to a Guest Author) needs to be handled specially.
This refactor is absolutely necessary in order for all the previous fixes to work as expected. Without this fix, what happens is that when you use `get_coauthor_by` by searching with a Guest Author, if that Guest Author has a valid link to a WP_User, it is summarily ignored. Functions like `add_coauthors` expect at least one coauthor to be a valid WP_User so that the `wp_posts.post_author` column can be appropriately updated. The only case where this function is returning an expected value is when you search by the WP_User first. When it arrives at `$guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force );`, `$guest_author === false`. It is then forced to move to the switch statement to find a user via their WP_User data.

With this refactor, `get_coauthor_by` will now check if the `linked_account` attribute is set. If so, it will attempt to find the corresponding user for the Guest Account. It still gives priority to returning a Guest Author. When a Guest Author is not found, it will search for a WP_User. If found, it will also search to see if a linked Guest Author account exists. If it does, it will return that Guest Author object instead, without losing the fact that this account also has a WP_User associated with it.
I forgot to run tests on my previous commit. This satisfies the test Test_CoAuthors_Plus::test_get_coauthor_by_when_guest_authors_not_enabled which is expecting a WP_User when the plugin is not enabled.
This might be a problematic decision. But the way I justify this change is that if you are appending co-authors, there may already be a WP_User set as the author. So we don't really have to care whether one is passed or not. Because of this, we do not need to forcibly return a `false` flag since that is confusing to the caller, especially because we actually do save the guest authors which are given in the call! Instead, if the $append flag is false, we should expect that at least one user will be a WP_User. In that case, if none is passed in, then there is a mismatch of the intended authors. Because now, the `wp_posts.post_author` column will have an old `wp_users.ID` which remains set and most likely isn't the intent of the caller.
Also, returning the actual response from the DB, to make this call even more accurate in terms of what is actually happen at the DB layer.
A pure WP_User (i.e. a WP_User that IS NOT linked to a Guest Author) needs to be handled specially.
This refactor is absolutely necessary in order for all the previous fixes to work as expected. Without this fix, what happens is that when you use `get_coauthor_by` by searching with a Guest Author, any link to a WP_User the Guest Author may have is summarily ignored. Functions like `add_coauthors` expect at least one coauthor to be a valid WP_User so that the `wp_posts.post_author` column can be appropriately updated. The only case where this function is currently returning an expected value is when you search by a WP_User account/field first. When it arrives at `$guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force );`, `$guest_author === false`. It is then forced to move to the switch statement to find a user via their WP_User data.

With this refactor, `get_coauthor_by` will now check if the `linked_account` attribute is set. If so, it will then attempt to find the corresponding WP_User for the Guest Author. Crucially, it still gives priority to returning a Guest Author. When a Guest Author is not found, it will then attempt to search for a WP_User. If found, it will also search to see if a linked Guest Author account exists. If it does, it will return that Guest Author object instead, without losing the fact that this account also has a WP_User associated with it.
These user_login's were causing other tests to fail because you cannot create another user with the same user_login.
Older version of PHPUnit do not have this function available. Updating to workaround: `assertTrue( property_exists( $obj, 'prop' ) )`
Copy link
Contributor

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eddiesshop - thank you for your patience as I reviewed it.

I appreciate the effort gone into creating all the tests, and seeing them pass really helps me to be confident that the expected behaviour for add_coauthors() would work as intended.

The beauty of tests lies in keeping things granular, and not making repeated assertions as they add to the noise and make it much harder to see what the real focus of a test is. Keeping the "Arrange, Act, Assert" structure in mind helps to ensure that the assertions are only happening after the key act (in this case, usually the call to add_coauthors()) has happened. Earlier tests may also assert before the act as well if we need to confirm the WP core default behaviour around the factory generations.

php/class-coauthors-plus.php Show resolved Hide resolved
php/class-coauthors-plus.php Outdated Show resolved Hide resolved
php/class-coauthors-plus.php Outdated Show resolved Hide resolved
tests/test-coauthors-plus.php Outdated Show resolved Hide resolved
tests/test-coauthors-plus.php Outdated Show resolved Hide resolved
Comment on lines 1097 to 1099

$this->assertEquals( 1, $query->found_posts );
$this->assertEquals( $this->author1->ID, $query->posts[0]->post_author );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->assertEquals( 1, $query->found_posts );
$this->assertEquals( $this->author1->ID, $query->posts[0]->post_author );

Comment on lines 1228 to 1242
$linked_author_1 = $this->_cap->get_coauthor_by( 'id', $this->author2->ID );
$this->assertIsObject( $linked_author_1 );
$this->assertThat(
$linked_author_1,
$this->logicalNot(
$this->isInstanceOf( WP_User::class )
)
);
$this->assertTrue( property_exists( $linked_author_1, 'type' ) );
$this->assertEquals( 'guest-author', $linked_author_1->type );
$this->assertTrue( property_exists( $linked_author_1, 'is_wp_user' ) );
$this->assertTrue( $linked_author_1->is_wp_user );
$this->assertTrue( property_exists( $linked_author_1, 'wp_user' ) );
$this->assertInstanceOf( WP_User::class, $linked_author_1->wp_user );
$this->assertEquals( $this->author2->ID, $linked_author_1->wp_user->ID );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It strongly feels like these assertions should go into a test specifically for get_coauthor_by() - so that this test method can just call the happy path of get_coauthor_by() and focus on what happens when the guest author is added with add_coauthors(). i.e. the whole test should be something like:

// Arrange:
// Create linked guest author from User 1.
// Create linked guest author from User 2.
// Create post with Editor 1 as the author.

// Act:
// Add both guest authors to the post.

// Assert:
// Assert the post author is User 1.

That should be it. All the other assertions should be handled (happy and sad paths) in other tests. The only thing this test should be doing is adding multiple ordered linked tests and see if the post_author field was updated.

}

/**
* Very similar test as before. The only difference is that we are appending,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data provider please.

}

/**
* Here we are testing a similar scenario as above, except we are passing the linked user's WP_User login field
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the test is to check that a user_login field is correctly converted, and applied, then you only need two users at most, and it can be simplified down.

Comment on lines 1920 to 1943
* This test is a combination of a few different aspects that establish some expected plugin behavior.
* Two posts are created to test with. Both have no WP_User as the author (i.e. wp_posts.post_author = 0).
* For the first post:
* 1. A guest author is created, and assigned to the post.
* 2. Since there is no WP_User which is passed, and the GA is NOT being appended, the result should be false.
* 3. The wp_posts.post_author column should still be 0 since this is a GA and not a WP_User or a linked GA.
*
* For the second post:
* 1. A guest author is created, and appended to the post.
* 2. Since we are appending a coauthor, it does not matter if there was already a WP_User author, so result should be true.
* 3. The wp_posts.post_author column should still be 0 since this is a GA and not a WP_User or a linked GA.
*
* Going back to the first post:
* 1. A linked user and a WP_User are created and assigned to this post.
* 2. Result should be true since here we essentially have 2 WP_Users.
* 3. Since there is a WP_User, and it is passed first, the wp_posts.post_author column should be set to the ID for that WP_User.
* 4. There should only be 2 author terms for this post, one for the WP_User, and one for the linked account.
* The term for the GA which was previously assigned is deleted.
*
* Finally, going back to the second post:
* 1. A linked user and a WP_User are created and appended to this post.
* 2. Result should be true since we have 2 WP_Users.
* 3. Here we passed the linked author first, so the wp_posts.post_author column should match the ID for the linked WP_user account.
* 4. There should be 3 author terms for this post, one for the GA, the WP_user, and linked account.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I greatly appreciate the extra documentation on this test method. But, other than behaviour around when a post starts off with post_author = 0, what else is being tested here that hasn't already been tested above?

@GaryJones GaryJones modified the milestones: 3.6.1, 3.6.2 Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

post_author not updated properly if assigning a new WP user
2 participants