Repository navigation
Make banner layouts and their positions filterable #31
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
Merged
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,111 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the filterable banner layouts and per-layout positions | ||
| * (frcn_cookie_notice_layouts / frcn_cookie_notice_layout_positions), and | ||
| * that Settings::sanitize_settings() validates against them. | ||
| * | ||
| * @package FrontConsent | ||
| */ | ||
|
|
||
| use FrontConsent\Admin\Settings; | ||
| use FrontConsent\Frontend\CookieNotice; | ||
| use Yoast\WPTestUtils\WPIntegration\TestCase; | ||
|
|
||
| class CookieNoticeLayoutsTest extends TestCase { | ||
|
|
||
| /** | ||
| * @var Settings | ||
| */ | ||
| private $settings; | ||
|
|
||
| public function set_up() { | ||
| parent::set_up(); | ||
| $this->settings = new Settings(); | ||
| } | ||
|
|
||
| public function tear_down() { | ||
| remove_all_filters( 'frcn_cookie_notice_layouts' ); | ||
| remove_all_filters( 'frcn_cookie_notice_layout_positions' ); | ||
| delete_option( Settings::OPTION_NAME ); | ||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Register a 'compact' layout with three positions, like an add-on would. | ||
| */ | ||
| private function add_compact_layout() { | ||
| add_filter( | ||
| 'frcn_cookie_notice_layouts', | ||
| static function ( $layouts ) { | ||
| $layouts['compact'] = 'Compact'; | ||
| return $layouts; | ||
| } | ||
| ); | ||
| add_filter( | ||
| 'frcn_cookie_notice_layout_positions', | ||
| static function ( $positions ) { | ||
| $positions['compact'] = array( | ||
| 'bottom-center' => 'Bottom center', | ||
| 'bottom-left' => 'Bottom left', | ||
| 'bottom-right' => 'Bottom right', | ||
| ); | ||
| return $positions; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public function test_built_in_layouts_and_positions() { | ||
| $this->assertSame( array( 'bar', 'box', 'popup' ), array_keys( CookieNotice::get_layouts() ) ); | ||
| $this->assertSame( array( 'box' ), array_keys( CookieNotice::get_layout_positions() ) ); | ||
| } | ||
|
|
||
| public function test_a_filtered_layout_is_offered_and_saved() { | ||
| $this->add_compact_layout(); | ||
|
|
||
| $this->assertArrayHasKey( 'compact', CookieNotice::get_layouts() ); | ||
|
|
||
| $result = $this->settings->sanitize_settings( | ||
| array( | ||
| 'cookie_notice_layout' => 'compact', | ||
| 'cookie_notice_position' => 'bottom-center', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 'compact', $result['cookie_notice_layout'] ); | ||
| $this->assertSame( 'bottom-center', $result['cookie_notice_position'] ); | ||
| } | ||
|
|
||
| public function test_an_unregistered_layout_falls_back_to_bar() { | ||
| $result = $this->settings->sanitize_settings( array( 'cookie_notice_layout' => 'compact' ) ); | ||
|
|
||
| $this->assertSame( 'bar', $result['cookie_notice_layout'] ); | ||
| } | ||
|
|
||
| public function test_a_position_not_supported_by_the_layout_falls_back_to_its_first_position() { | ||
| $result = $this->settings->sanitize_settings( | ||
| array( | ||
| 'cookie_notice_layout' => 'box', | ||
| 'cookie_notice_position' => 'bottom-center', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 'bottom-right', $result['cookie_notice_position'] ); | ||
| } | ||
|
|
||
| public function test_a_layout_without_positions_keeps_a_valid_box_position() { | ||
| $result = $this->settings->sanitize_settings( | ||
| array( | ||
| 'cookie_notice_layout' => 'bar', | ||
| 'cookie_notice_position' => 'bottom-left', | ||
| ) | ||
| ); | ||
|
|
||
| $this->assertSame( 'bottom-left', $result['cookie_notice_position'] ); | ||
| } | ||
|
|
||
| public function test_a_non_array_filter_result_is_ignored() { | ||
| add_filter( 'frcn_cookie_notice_layouts', '__return_false' ); | ||
|
|
||
| $this->assertSame( array( 'bar', 'box', 'popup' ), array_keys( CookieNotice::get_layouts() ) ); | ||
| } | ||
| } |
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.
On supported PHP 7.0–7.2 installations, saving settings for a layout with a submitted Position field (such as
box) reaches this branch and fatals becausearray_key_first()was introduced in PHP 7.3. Use a PHP 7.0-compatible way to obtain the first allowed position so these users can save the settings page.AGENTS.md reference: AGENTS.md:L11-L12
Useful? React with 👍 / 👎.