Skip to content

Commit

Permalink
Map Select and BooleanSelect inputs to the custom block of the se…
Browse files Browse the repository at this point in the history
…lect field.
  • Loading branch information
eason9487 committed Nov 30, 2023
1 parent 040cb56 commit 9d445df
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 6 deletions.
22 changes: 21 additions & 1 deletion src/Admin/Input/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Select extends Input {
* Select constructor.
*/
public function __construct() {
parent::__construct( 'select' );
parent::__construct( 'select', 'google-listings-and-ads/product-select-field' );
}

/**
Expand Down Expand Up @@ -55,4 +55,24 @@ public function get_view_data(): array {

return $view_data;
}

/**
* Return the attributes of block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_attributes(): array {
$options = [];

foreach ( $this->get_options() as $key => $value ) {
$options[] = [
'label' => $value,
'value' => $key,
];
}

$this->set_block_attribute( 'options', $options );

return parent::get_block_attributes();
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Admin/Input/InputCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
class InputCollectionTest extends UnitTest {
public function test_boolean_select() {
$input = new BooleanSelect();
$input->set_id( 'test-boolean-select' );

$this->assertEquals( 'select', $input->get_type() );
$this->assertEquals( 'google-listings-and-ads/product-select-field', $input->get_block_name() );

// BooleanSelect doesn't reflect the call of set_options method
$input->set_options( [] );
Expand All @@ -36,6 +38,24 @@ public function test_boolean_select() {
$input->get_options()
);

$this->assertEquals(
[
[
'label' => 'Default',
'value' => '',
],
[
'label' => 'Yes',
'value' => 'yes',
],
[
'label' => 'No',
'value' => 'no',
],
],
$input->get_block_attributes()['options']
);

// Null by default
$this->assertNull( $input->get_view_data()['value'] );

Expand Down Expand Up @@ -90,13 +110,16 @@ public function test_integer() {

public function test_select() {
$input = new Select();
$input->set_id( 'test-select' );

$this->assertEquals( 'select', $input->get_type() );
$this->assertEquals( 'google-listings-and-ads/product-select-field', $input->get_block_name() );
$this->assertEquals( 'select short', $input->get_view_data()['class'] );

// Empty options by default
$this->assertEquals( [], $input->get_options() );
$this->assertEquals( [], $input->get_view_data()['options'] );
$this->assertEquals( [], $input->get_block_attributes()['options'] );

// Set and get options
$options = [
Expand All @@ -107,6 +130,19 @@ public function test_select() {

$this->assertEquals( $options, $input->get_options() );
$this->assertEquals( $options, $input->get_view_data()['options'] );
$this->assertEquals(
[
[
'label' => 'bar',
'value' => 'foo',
],
[
'label' => 'hello',
'value' => 'hi',
],
],
$input->get_block_attributes()['options']
);
}

public function test_selectwithtextinput() {
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Admin/Product/Attributes/AttributesBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,19 @@ public function test_register_add_section() {
*/
public function test_register_add_blocks() {
// The total number of blocks to be added to the simple product template is 16,
// and the converted number so far is 8
// and the converted number so far is 15
$this->simple_gla_section
->expects( $this->exactly( 8 ) )
->expects( $this->exactly( 15 ) )
->method( 'add_block' );

$this->simple_gla_section->get_block( 'mocked-singleton' )
->expects( $this->exactly( 8 ) )
->expects( $this->exactly( 15 ) )
->method( 'add_hide_condition' );

// The total number of visible blocks to be added to the variation product template is 15,
// and the converted number so far is 7
// and the converted number so far is 14
$this->variation_gla_section
->expects( $this->exactly( 7 ) )
->expects( $this->exactly( 14 ) )
->method( 'add_block' );

$this->variation_gla_section->get_block( 'mocked-singleton' )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ public function test_adult_input() {
],
$input->get_view_data()
);

$this->assertEquals(
[
'id' => 'google-listings-and-ads-product-attributes-adult',
'blockName' => 'google-listings-and-ads/product-select-field',
'attributes' => [
'property' => 'meta_data._wc_gla_adult',
'label' => 'Adult content',
'tooltip' => 'Whether the product contains nudity or sexually suggestive content',
'options' => $input->get_block_attributes()['options'],
],
],
$input->get_block_config()
);
}

public function test_age_group_input() {
Expand All @@ -96,6 +110,20 @@ public function test_age_group_input() {
],
$input->get_view_data()
);

$this->assertEquals(
[
'id' => 'google-listings-and-ads-product-attributes-ageGroup',
'blockName' => 'google-listings-and-ads/product-select-field',
'attributes' => [
'property' => 'meta_data._wc_gla_ageGroup',
'label' => 'Age Group',
'tooltip' => 'Target age group of the item.',
'options' => $input->get_block_attributes()['options'],
],
],
$input->get_block_config()
);
}

public function test_availability_date_input() {
Expand Down Expand Up @@ -213,6 +241,20 @@ public function test_condition_input() {
],
$input->get_view_data()
);

$this->assertEquals(
[
'id' => 'google-listings-and-ads-product-attributes-condition',
'blockName' => 'google-listings-and-ads/product-select-field',
'attributes' => [
'property' => 'meta_data._wc_gla_condition',
'label' => 'Condition',
'tooltip' => 'Condition or state of the item.',
'options' => $input->get_block_attributes()['options'],
],
],
$input->get_block_config()
);
}

public function test_gender_input() {
Expand All @@ -238,6 +280,20 @@ public function test_gender_input() {
],
$input->get_view_data()
);

$this->assertEquals(
[
'id' => 'google-listings-and-ads-product-attributes-gender',
'blockName' => 'google-listings-and-ads/product-select-field',
'attributes' => [
'property' => 'meta_data._wc_gla_gender',
'label' => 'Gender',
'tooltip' => 'The gender for which your product is intended.',
'options' => $input->get_block_attributes()['options'],
],
],
$input->get_block_config()
);
}

public function test_gtin_input() {
Expand Down Expand Up @@ -301,6 +357,20 @@ public function test_is_bundle_input() {
],
$input->get_view_data()
);

$this->assertEquals(
[
'id' => 'google-listings-and-ads-product-attributes-isBundle',
'blockName' => 'google-listings-and-ads/product-select-field',
'attributes' => [
'property' => 'meta_data._wc_gla_isBundle',
'label' => 'Is Bundle?',
'tooltip' => 'Whether the item is a bundle of products. A bundle is a custom grouping of different products sold by a merchant for a single price.',
'options' => $input->get_block_attributes()['options'],
],
],
$input->get_block_config()
);
}

public function test_material_input() {
Expand Down Expand Up @@ -502,6 +572,20 @@ public function test_size_system_input() {
],
$input->get_view_data()
);

$this->assertEquals(
[
'id' => 'google-listings-and-ads-product-attributes-sizeSystem',
'blockName' => 'google-listings-and-ads/product-select-field',
'attributes' => [
'property' => 'meta_data._wc_gla_sizeSystem',
'label' => 'Size system',
'tooltip' => 'System in which the size is specified. Recommended for apparel items.',
'options' => $input->get_block_attributes()['options'],
],
],
$input->get_block_config()
);
}

public function test_size_type_input() {
Expand All @@ -527,5 +611,19 @@ public function test_size_type_input() {
],
$input->get_view_data()
);

$this->assertEquals(
[
'id' => 'google-listings-and-ads-product-attributes-sizeType',
'blockName' => 'google-listings-and-ads/product-select-field',
'attributes' => [
'property' => 'meta_data._wc_gla_sizeType',
'label' => 'Size type',
'tooltip' => 'The cut of the item. Recommended for apparel items.',
'options' => $input->get_block_attributes()['options'],
],
],
$input->get_block_config()
);
}
}

0 comments on commit 9d445df

Please sign in to comment.