Skip to content

Commit

Permalink
Avoid calling the ProductTemplates API add_hide_condition with an e…
Browse files Browse the repository at this point in the history
…mpty string that causes errors.

Address: #2151 (comment)
  • Loading branch information
eason9487 committed Dec 15, 2023
1 parent 0408038 commit 46830dc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Admin/Product/Attributes/AttributesBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ private function add_blocks( BlockInterface $section ): void {
// When editing a simple or variable product, its product type on the frontend side can be
// changed dynamically. So, it needs to use the ProductTemplates API `add_hide_condition`
// to conditionally hide attributes.
$block = $section->add_block( $input->get_block_config() );
$block->add_hide_condition( $this->get_hide_condition( $attribute_type ) );
$block = $section->add_block( $input->get_block_config() );
$hide_condition = $this->get_hide_condition( $attribute_type );

if ( '' !== $hide_condition ) {
$block->add_hide_condition( $hide_condition );
}
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Admin/Product/Attributes/AttributesBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\Adult;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeManager;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\Brand;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\Color;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\Gender;
use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\ContainerAwareUnitTest;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -257,4 +258,44 @@ public function test_register_add_blocks() {
do_action( self::SIMPLE_ATTRIBUTES_SECTION_HOOK, $this->simple_anchor_block );
do_action( self::VARIATION_IMAGES_SECTION_HOOK, $this->variation_anchor_block );
}

/**
* Assert it only calls `$block->add_hide_condition` when `$attributes_block->get_hide_condition()`
* is not an empty string.
*/
public function test_register_add_product_attribute_blocks_conditionally_add_hide_condition() {
$attribute_manager = $this->createStub( AttributeManager::class );
$attribute_manager
->method( 'get_attribute_types_for_product_types' )
->willReturn(
[
'color' => Color::class,
'brand' => Brand::class,
]
);

$attributes_block = new AttributesBlock( $attribute_manager, $this->merchant_center );

// Hide all product types for Brand
add_filter(
'woocommerce_gla_attribute_hidden_product_types_brand',
[ Brand::class, 'get_applicable_product_types' ]
);

$this->assertNotEquals( '', $attributes_block->get_hide_condition( Color::class ) );
$this->assertEquals( '', $attributes_block->get_hide_condition( Brand::class ) );

$this->simple_gla_section
->expects( $this->exactly( 2 ) )
->method( 'add_block' );

$this->simple_gla_section->get_block( 'mocked-singleton' )
->expects( $this->exactly( 1 ) )
->method( 'add_hide_condition' )
->with( $attributes_block->get_hide_condition( Color::class ) );

$attributes_block->register();

do_action( self::SIMPLE_ATTRIBUTES_SECTION_HOOK, $this->simple_anchor_block );
}
}

0 comments on commit 46830dc

Please sign in to comment.