Repository navigation
Fixed handling of array values in logo palette frontend #4705
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
Open
girishpanchal30
wants to merge
1
commit into
development
Choose a base branch
from
bugfix/4697
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−3
Open
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
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,155 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the logo palette setting read paths. | ||
| * | ||
| * @package neve | ||
| */ | ||
|
|
||
| use HFG\Core\Builder\Abstract_Builder; | ||
| use HFG\Core\Components\Abstract_Component; | ||
| use HFG\Core\Components\Logo; | ||
| use HFG\Main; | ||
| use Neve\Core\Settings\Mods; | ||
|
|
||
| /** | ||
| * Class TestNeveLogoPalette | ||
| */ | ||
| class TestNeveLogoPalette extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Previous render state and Mods cache mode. | ||
| * | ||
| * @var array{builder: mixed, builder_component: mixed, current_component: mixed, no_cache: bool} | ||
| */ | ||
| private $previous_state; | ||
|
|
||
| /** | ||
| * Light logo attachment id. | ||
| * | ||
| * @var int | ||
| */ | ||
| private $light_id; | ||
|
|
||
| /** | ||
| * Dark logo attachment id. | ||
| * | ||
| * @var int | ||
| */ | ||
| private $dark_id; | ||
|
|
||
| /** | ||
| * Store the global state the tests change and store the palette as an array. | ||
| */ | ||
| public function set_up(): void { | ||
| parent::set_up(); | ||
|
|
||
| $this->previous_state = array( | ||
| 'builder' => Abstract_Builder::$current_builder, | ||
| 'builder_component' => Abstract_Builder::$current_component, | ||
| 'current_component' => Abstract_Component::$current_component, | ||
| 'no_cache' => Mods::$no_cache, | ||
| ); | ||
| Mods::$no_cache = true; | ||
|
|
||
| $this->light_id = self::factory()->attachment->create_object( 'logo-light.png', 0, array( 'post_mime_type' => 'image/png' ) ); | ||
| $this->dark_id = self::factory()->attachment->create_object( 'logo-dark.png', 0, array( 'post_mime_type' => 'image/png' ) ); | ||
|
|
||
| set_theme_mod( | ||
| Logo::COMPONENT_ID . '_' . Logo::LOGO, | ||
| array( | ||
| 'light' => $this->light_id, | ||
| 'dark' => $this->dark_id, | ||
| 'same' => false, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Restore the global state. | ||
| */ | ||
| public function tear_down(): void { | ||
| remove_theme_mod( Logo::COMPONENT_ID . '_' . Logo::LOGO ); | ||
| remove_theme_mod( 'custom_logo' ); | ||
|
|
||
| Abstract_Builder::$current_builder = $this->previous_state['builder']; | ||
| Abstract_Builder::$current_component = $this->previous_state['builder_component']; | ||
| Abstract_Component::$current_component = $this->previous_state['current_component']; | ||
| Mods::$no_cache = $this->previous_state['no_cache']; | ||
|
|
||
| parent::tear_down(); | ||
| } | ||
|
|
||
| /** | ||
| * Get the registered header logo component. | ||
| * | ||
| * @return Logo | ||
| */ | ||
| private function logo_component(): Logo { | ||
| $component = Main::get_instance()->get_builder( 'header' )->get_component( Logo::COMPONENT_ID ); | ||
|
|
||
| $this->assertInstanceOf( Logo::class, $component ); | ||
|
|
||
| return $component; | ||
| } | ||
|
|
||
| /** | ||
| * Logo variants are built from a palette stored as an array. | ||
| */ | ||
| public function test_logo_variants_read_array_palette(): void { | ||
| $variants = $this->logo_component()->filter_logo_variants( array() ); | ||
|
|
||
| $this->assertFalse( $variants[ Logo::COMPONENT_ID ]['same'] ); | ||
| $this->assertStringEndsWith( 'logo-light.png', $variants[ Logo::COMPONENT_ID ]['light']['src'] ); | ||
| $this->assertStringEndsWith( 'logo-dark.png', $variants[ Logo::COMPONENT_ID ]['dark']['src'] ); | ||
| } | ||
|
|
||
| /** | ||
| * Changing the site logo updates a palette stored as an array. | ||
| */ | ||
| public function test_custom_logo_update_reads_array_palette(): void { | ||
| $new_logo_id = self::factory()->attachment->create_object( 'logo-new.png', 0, array( 'post_mime_type' => 'image/png' ) ); | ||
|
|
||
| $this->assertSame( $new_logo_id, $this->logo_component()->update_logo_theme_mod( $new_logo_id, $this->light_id ) ); | ||
| $this->assertSame( | ||
| array( | ||
| 'light' => $new_logo_id, | ||
| 'dark' => $this->dark_id, | ||
| 'same' => false, | ||
| ), | ||
| json_decode( get_theme_mod( Logo::COMPONENT_ID . '_' . Logo::LOGO ), true ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * The logo template renders the light logo from a palette stored as an array. | ||
| */ | ||
| public function test_logo_template_reads_array_palette(): void { | ||
| $this->logo_component(); | ||
| Abstract_Builder::$current_builder = 'header'; | ||
| Abstract_Builder::$current_component = null; | ||
| Abstract_Component::$current_component = Logo::COMPONENT_ID; | ||
|
|
||
| ob_start(); | ||
| Main::get_instance()->load( 'components/component-logo' ); | ||
| $output = ob_get_clean(); | ||
|
|
||
| $this->assertStringContainsString( 'logo-light.png', $output ); | ||
| } | ||
|
|
||
| /** | ||
| * Stored palettes decode to an array whatever their shape. | ||
| */ | ||
| public function test_decode_logo_json_normalizes_stored_values(): void { | ||
| $palette = array( | ||
| 'light' => 12, | ||
| 'dark' => 34, | ||
| 'same' => false, | ||
| ); | ||
|
|
||
| $this->assertSame( $palette, Logo::decode_logo_json( $palette ) ); | ||
| $this->assertSame( $palette, Logo::decode_logo_json( wp_json_encode( $palette ) ) ); | ||
| $this->assertSame( array(), Logo::decode_logo_json( 'not json' ) ); | ||
| $this->assertSame( array(), Logo::decode_logo_json( '5' ) ); | ||
| $this->assertSame( array(), Logo::decode_logo_json( false ) ); | ||
| } | ||
| } |
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.
notedecode_logo_json()now passes any stored array through without checking its members. If a stored palette array holds a non-scalarlightordark, that value reacheswp_get_attachment_image_url()infilter_logo_variants()and$custom_logo_idin the logo template. Can Neve Pro conditional headers or a starter-site import store such an array? If they only store attachment IDs, this is safe. If they can store nested values, the logo breaks or warns.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.
Not changed: a palette stored as a JSON string already decodes to the same nested shape through
json_decode( ..., true )on the base code, so member handling is untouched. This PR only stops array-stored palettes from crashing. The only writers (the Customizer palette control viasanitize_logo_json(), and the starter-site importer copying exported Customizer values) store attachment IDs forlight/dark.