Tests: fix broken setup of mocked WP_Role objects + fix an incorrect test #165
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
Summary
This PR can be summarized in the following changelog entry:
Relevant technical choices:
Tests: fix broken setup of mocked WP_Role objects
This fixes three different issues with the mocking of the WP_Role objects.
1. Incorrect implementation of
allows()
.As per the Mockery documentation about the
allows()
syntax, the use ofallows()
can take two forms:method_name
=>return_value
entries.method_name
andandReturn( 'return value' )
to it.In the Duplicate Post
TestCase
class method [1] was being used, but implemented incorrectly.The
return_value
should be an actual value, not a callback which will be called to get at the value.In practice, that meant that the mocked
WP_Role
hap_cap()
,add_cap()
andremove_cap()
methods were returning a closure (object instance of theClosure
class and not the expected return value.The only reason this hadn't led to failing tests before is the prevalence of implicit loose type comparisons in the code base, i.e.
if ( $something ) {}
.Ref: http://docs.mockery.io/en/latest/reference/alternative_should_receive_syntax.html#allows
2. Void method(s) returning a value.
The WP
WP_Role::add_cap()
method is, just like theWP_Role::remove_cap()
method, avoid
method, i.e. it does not return a value.As it was, the
add_cap()
was returning a closure, but based on the return value of the closure, it was intended for the method to returntrue
.Changing this to
null
now in an attempt to more closely match the original method.Ref: https://developer.wordpress.org/reference/classes/wp_role/add_cap/
3. Incorrect use of
makePartial()
.As per the documentation:
As the actual
WP_Role
class is not available to the test suite as WP will not be loaded, making these mocks "partials" is useless.Ref: http://docs.mockery.io/en/latest/reference/creating_test_doubles.html#partial-test-doubles
Tests: fix bug in Options_Form_Generator_Test::test_generate_roles_permission_list()
As per the mock of the WP_Role objects in the
Yoast\WP\Duplicate_Post\Tests\TestCase::setUp()
method, thehas_cap()
method will respectively returntrue
for theEditor
role andfalse
for theAdministrator
and theSubscriber
role.Based on that, the expected output for the
Options_Form_Generator_Test::test_generate_roles_permission_list()
test should have only theEditor
checkbox checked.This fixes the incorrectly checked checkbox for the
Administrator
role.Test instructions
This PR can be tested by following these steps: