From eba58c556fea5558eec3a54151ab24457214faa4 Mon Sep 17 00:00:00 2001 From: Amy Evans Date: Wed, 9 Dec 2020 10:09:22 -0600 Subject: [PATCH 1/2] Add test for overriding a theme in the config callback --- tests/components/test-class-component.php | 33 ++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/components/test-class-component.php b/tests/components/test-class-component.php index f5311dd0..aa68b67e 100644 --- a/tests/components/test-class-component.php +++ b/tests/components/test-class-component.php @@ -183,10 +183,11 @@ public function get_config_for_test_schema(): array { [ [ 'test-default' => 'overridden', + 'theme' => 'overridden', ], [ 'test-default' => 'overridden', - 'theme' => 'default', + 'theme' => 'overridden', ], 'Default config values not overridden.', ], @@ -475,6 +476,36 @@ public function test_config_callback() { ); } + /** + * Test overriding `theme` in a config callback. + */ + public function test_config_callback_for_theme() { + $name = 'example/component'; + + /* + * Register component with callback. + * + * We're using a callback that uses the config in order + * to ensure that the callback is passed an array. + */ + get_registry()->register_component( + $name, + [ + 'config_callback' => function ( array $config ) { + $config['theme'] = 'fred'; + return $config; + }, + ] + ); + + $component = new Component( $name ); + + // Clean up. + get_registry()->unregister_component( $name ); + + $this->assertEquals( 'fred', $component->get_theme() ); + } + /** * Test children set from config. */ From a418111dae364b57d7cbe9292941379db5a23ff2 Mon Sep 17 00:00:00 2001 From: Amy Evans Date: Wed, 9 Dec 2020 10:16:30 -0600 Subject: [PATCH 2/2] Ensure theme gets set appropriately --- inc/components/class-component.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/components/class-component.php b/inc/components/class-component.php index 44ecd5f1..6c0941ef 100644 --- a/inc/components/class-component.php +++ b/inc/components/class-component.php @@ -199,7 +199,7 @@ private function process_args( array $args ): self { ->apply_context() ->set_config( $args['config'] ) ->hydrate_config() - ->set_theme( 'irving/site-theme' !== $this->get_name() ? $args['config']['theme'] : $args['theme'] ) + ->set_theme( 'irving/site-theme' !== $this->get_name() ? $this->get_config( 'theme' ) : $args['theme'] ) ->set_children( $args['children'] ); return $this;