Make WordPress Core

Opened 9 years ago

Closed 3 months ago

Last modified 3 months ago

#38433 closed enhancement (fixed)

Complete test coverage for current_user_can_for_site()

Reported by: johnbillion's profile johnbillion Owned by:
Milestone: 6.7 Priority: normal
Severity: normal Version: 4.3
Component: Role/Capability Keywords: good-first-bug has-patch has-unit-tests
Focuses: Cc:

Description (last modified by johnbillion)

In Tests_User_Capabilities, all roles and capabilities are tested using current_user_can(). They should all be tested using current_user_can_for_site(), too.

Attachments (4)

38433.diff (1.8 KB) - added by santilinwp 7 years ago.
38433.output (1.0 KB) - added by santilinwp 7 years ago.
38433.2.diff (3.1 KB) - added by santilinwp 7 years ago.
An extended patch which demostrates the duplicating of tests (multisite and not multisite)
38433 (1).diff (3.1 KB) - added by sachinrajcp123 3 months ago.

Download all attachments as: .zip

Change History (15)

#1 @desrosj
7 years ago

  • Keywords needs-patch good-first-bug added

Marking good-first-bug for someone that would like to dive into writing unit tests.

#2 in reply to: ↑ description ; follow-up: @santilinwp
7 years ago

I'v duplicated the calls in one function: test_subscriber_cant_edit_posts with the desired result, but then I have added a new function to test that an editor CAN edit their posts but no others in another site, but it looks like current_user_can_for_blog takes the current site when the other site doesn't exist.

I'll attach the diff and the output.

Last edited 7 years ago by santilinwp (previous) (diff)

@santilinwp
7 years ago

@santilinwp
7 years ago

#3 in reply to: ↑ 2 @santilinwp
7 years ago

Replying to santilinwp:

I'v duplicated the calls in one function: test_subscriber_cant_edit_posts with the desired result, but then I have added a new function to test that an editor CAN edit their posts but no others in another site, but it looks like current_user_can_for_blog takes the current site when the other site doesn't exist.

I'll attach the diff and the output.

Digging a little bit more in the code I've learnt that current_user_can_for_blog just disregards the $blog_id parameter if not is_multisite, so the test is allright.

#4 @santilinwp
7 years ago

  • Keywords has-patch needs-dev-note added; needs-patch removed

I understand that the way to go is duplicate most of the functions to make multisite and not multisite versions, like in the attached example.

@santilinwp
7 years ago

An extended patch which demostrates the duplicating of tests (multisite and not multisite)

This ticket was mentioned in Slack in #core by backermann1978. View the logs.


5 years ago

#6 @michaelwp85
10 months ago

While looking for a good first ticket to contribute I looked at this one.

I think this ticket can be closed, and marked as done. Seeing current_user__can_for_blog has been deprecated: * @deprecated 6.7.0 Use current_user_can_for_site() instead. I don't know the test coverage, but there are multiple tests for current_user_can_for_site.

#7 @sachinrajcp123
3 months ago

Added current_user_can_for_blog() assertions to Tests_User_Capabilities for full coverage.

#8 @johnbillion
3 months ago

  • Description modified (diff)
  • Keywords has-unit-tests added; needs-unit-tests needs-dev-note removed
  • Summary changed from Complete test coverage for current_user_can_for_blog() to Complete test coverage for current_user_can_for_site()

Thanks so much for the patch @sachinrajcp123 . Would you like to open a GitHub pull request with those changes please so the test suite can run?

Also the function has been deprecated since this ticket was opened so I've changed the title. The preferred function name is now current_user_can_for_site().

#9 @johnbillion
3 months ago

  • Milestone changed from Future Release to 6.7
  • Resolution set to fixed
  • Status changed from new to closed

Oh you know what, @michaelwp85 is correct. This function actually has test coverage now since #45197. Thanks everyone!

This ticket was mentioned in Slack in #core-committers by johnbillion. View the logs.


3 months ago

This ticket was mentioned in PR #9454 on WordPress/wordpress-develop by @sachinrajcp123.


3 months ago
#11

Adds test coverage for current_user_can_for_site() and user_can_for_site().
Replaces remaining uses of deprecated current_user_can_for_blog() in tests.

See: https://core.trac.wordpress.org/ticket/38433

Note: See TracTickets for help on using tickets.