-
Notifications
You must be signed in to change notification settings - Fork 0
06: Conflict policy constants #6
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
Merged
+191
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d525ed3
Add Conflict_Policy constants
nikolaystrikhar 8b8cd94
Address review: let callers reject an unknown conflict policy
nikolaystrikhar 686266b
Plan: make the resolver reject unknown conflict policies
nikolaystrikhar 5b32788
Refactor Conflict_Policy: make all() private and update tests to use …
nikolaystrikhar 49393e1
Merge main into 06-conflict-policy
nikolaystrikhar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| <?php | ||
| /** | ||
| * @package Nexcess\PluginAbsorber | ||
| */ | ||
|
|
||
| namespace Nexcess\PluginAbsorber; | ||
|
|
||
| /** | ||
| * What to do when a sub-plugin's standalone counterpart is still active. | ||
| * | ||
| * @since 1.0.0 | ||
| */ | ||
| final class Conflict_Policy { | ||
| /** | ||
| * Deactivate the standalone, notify, and redirect. The bundled copy loads on the next | ||
| * request, since the standalone has already defined the guard constant on this one. | ||
| * | ||
| * The default. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @var string | ||
| */ | ||
| public const DEACTIVATE = 'deactivate'; | ||
|
|
||
| /** | ||
| * Leave the standalone alone and let it win. The load guard stands the bundled copy down. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @var string | ||
| */ | ||
| public const DEFER = 'defer'; | ||
|
|
||
| /** | ||
| * Leave the standalone active but ask the user to deactivate it. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @var string | ||
| */ | ||
| public const NOTICE_ONLY = 'notice_only'; | ||
|
|
||
| /** | ||
| * Whether a policy string is one this library understands. | ||
| * | ||
| * Hosts may persist a policy in an option and filters may return anything, so callers that | ||
| * dispatch on a policy should reject unknown values here rather than letting them fall | ||
| * through to a default branch — deactivating a plugin the site owner deliberately turned on | ||
| * is the most surprising of the three outcomes to arrive at by accident. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @param string $policy Policy to check. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function is_valid( string $policy ): bool { | ||
| return in_array( $policy, self::all(), true ); | ||
| } | ||
|
|
||
| /** | ||
| * Every policy this library understands. | ||
| * | ||
| * @since 1.0.0 | ||
| * | ||
| * @return string[] | ||
| */ | ||
| private static function all(): array { | ||
| return [ | ||
| self::DEACTIVATE, | ||
| self::DEFER, | ||
| self::NOTICE_ONLY, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
| /** | ||
| * @package Nexcess\PluginAbsorber | ||
| */ | ||
|
|
||
| namespace Nexcess\PluginAbsorber\Tests\Unit; | ||
|
|
||
| use Codeception\TestCase\WPTestCase; | ||
| use Generator; | ||
| use Nexcess\PluginAbsorber\Conflict_Policy; | ||
| use ReflectionClass; | ||
|
|
||
| /** | ||
| * @since 1.0.0 | ||
| */ | ||
| class ConflictPolicyTest extends WPTestCase { | ||
| public function test_the_policy_values_are_stable(): void { | ||
| $this->assertSame( 'deactivate', Conflict_Policy::DEACTIVATE ); | ||
| $this->assertSame( 'defer', Conflict_Policy::DEFER ); | ||
| $this->assertSame( 'notice_only', Conflict_Policy::NOTICE_ONLY ); | ||
| } | ||
|
|
||
| /** | ||
| * Pins the whole set, not just the three names. A fourth policy added without teaching | ||
| * the resolver about it would otherwise be swallowed by that switch's default branch. | ||
| */ | ||
| public function test_no_policy_is_added_or_removed_unnoticed(): void { | ||
| $constants = ( new ReflectionClass( Conflict_Policy::class ) )->getConstants(); | ||
|
|
||
| $this->assertSame( | ||
| [ | ||
| 'DEACTIVATE' => 'deactivate', | ||
| 'DEFER' => 'defer', | ||
| 'NOTICE_ONLY' => 'notice_only', | ||
| ], | ||
| $constants | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider valid_policies | ||
| * | ||
| * @param string $policy Policy under test. | ||
| */ | ||
| public function test_it_accepts_a_known_policy( string $policy ): void { | ||
| $this->assertTrue( Conflict_Policy::is_valid( $policy ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Drawn from the constants rather than a hand-written list, so a fourth policy declared | ||
| * without being added to the set behind is_valid() fails here instead of passing unnoticed. | ||
| * | ||
| * @return Generator<string,array{0:string}> | ||
| */ | ||
| public static function valid_policies(): Generator { | ||
| $constants = ( new ReflectionClass( Conflict_Policy::class ) )->getConstants(); | ||
|
|
||
| foreach ( $constants as $name => $value ) { | ||
| yield $name => [ (string) $value ]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider invalid_policies | ||
| * | ||
| * @param string $policy Policy under test. | ||
| */ | ||
| public function test_it_rejects_an_unknown_policy( string $policy ): void { | ||
| $this->assertFalse( Conflict_Policy::is_valid( $policy ) ); | ||
| } | ||
|
|
||
| /** | ||
| * @return Generator<string,array{0:string}> | ||
| */ | ||
| public static function invalid_policies(): Generator { | ||
| yield 'typo' => [ 'defered' ]; | ||
| yield 'empty' => [ '' ]; | ||
| yield 'wrong case' => [ 'DEACTIVATE' ]; | ||
| yield 'constant' => [ 'Conflict_Policy::DEFER' ]; | ||
| } | ||
|
Comment on lines
+72
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
Oops, something went wrong.
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.
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.
Generatorwould be better :)