diff --git a/tests/Unit/Admin/Input/InputCollectionTest.php b/tests/Unit/Admin/Input/InputCollectionTest.php new file mode 100644 index 0000000000..444f8b0825 --- /dev/null +++ b/tests/Unit/Admin/Input/InputCollectionTest.php @@ -0,0 +1,252 @@ +assertEquals( 'select', $input->get_type() ); + + // BooleanSelect doesn't reflect the call of set_options method + $input->set_options( [] ); + + $this->assertEquals( + [ + '' => 'Default', + 'yes' => 'Yes', + 'no' => 'No', + ], + $input->get_options() + ); + + // Null by default + $this->assertNull( $input->get_view_data()['value'] ); + + // Convert to a 'yes' or 'no' if the data is the bool type + $input->set_data( true ); + $this->assertEquals( 'yes', $input->get_view_data()['value'] ); + + $input->set_data( false ); + $this->assertEquals( 'no', $input->get_view_data()['value'] ); + } + + public function test_date_time() { + $input = new DateTime(); + + $this->assertEquals( 'datetime', $input->get_type() ); + + // Null by default + $view_data = $input->get_view_data(); + + $this->assertNull( $view_data['value'] ); + $this->assertArrayNotHasKey( 'date', $view_data ); + $this->assertArrayNotHasKey( 'time', $view_data ); + + // Set date and time data with a string + $input->set_data( '2023-09-04T08:42:00+00:00' ); + $view_data = $input->get_view_data(); + + $this->assertEquals( '2023-09-04 08:42:00', $view_data['value'] ); + $this->assertEquals( '2023-09-04', $view_data['date'] ); + $this->assertEquals( '08:42', $view_data['time'] ); + + // Set date and time data with a key-value array + $input->set_data( + [ + 'date' => '2024-01-02', + 'time' => '19:27:56', + ] + ); + $view_data = $input->get_view_data(); + + $this->assertEquals( '2024-01-02 19:27:56', $view_data['value'] ); + $this->assertEquals( '2024-01-02', $view_data['date'] ); + $this->assertEquals( '19:27', $view_data['time'] ); + } + + public function test_integer() { + $input = new Integer(); + + $this->assertEquals( 'integer', $input->get_type() ); + $this->assertEquals( 'woocommerce/product-number-field', $input->get_block_name() ); + } + + public function test_select() { + $input = new Select(); + + $this->assertEquals( 'select', $input->get_type() ); + $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'] ); + + // Set and get options + $options = [ + 'foo' => 'bar', + 'hi' => 'hello', + ]; + $input->set_options( $options ); + + $this->assertEquals( $options, $input->get_options() ); + $this->assertEquals( $options, $input->get_view_data()['options'] ); + } + + public function test_selectwithtextinput() { + $input = new SelectWithTextInput(); + $input->set_name( 'name' ); + + $this->assertEquals( 'select-with-text-input', $input->get_type() ); + + // Default view data + $this->assertEquals( + [ + 'id' => 'gla_name', + 'type' => 'select-with-text-input', + 'label' => null, + 'description' => null, + 'desc_tip' => true, + 'value' => null, + 'name' => 'gla_name', + 'is_root' => true, + 'children' => [ + '_gla_select' => [ + 'id' => 'gla_name__gla_select', + 'type' => 'select', + 'label' => null, + 'description' => null, + 'desc_tip' => true, + 'value' => null, + 'name' => 'gla_name[_gla_select]', + 'is_root' => false, + 'children' => [], + 'class' => 'select short', + 'options' => [ + '_gla_custom_value' => 'Enter a custom value', + ], + ], + '_gla_custom_value' => [ + 'id' => 'gla_name__gla_custom_value', + 'type' => 'text', + 'label' => 'Enter your value', + 'description' => null, + 'desc_tip' => true, + 'value' => null, + 'name' => 'gla_name[_gla_custom_value]', + 'is_root' => false, + 'children' => [], + 'wrapper_class' => 'custom-input', + ], + ], + 'gla_wrapper_class' => ' select-with-text-input', + ], + $input->get_view_data() + ); + + // After calling setters + $input + ->set_label( 'Hi label' ) + ->set_description( 'Hello description' ) + ->set_options( + [ + '' => 'Default name', + 'use_admin_name' => 'Use admin name', + ] + ); + + $this->assertEquals( + [ + 'id' => 'gla_name', + 'type' => 'select-with-text-input', + 'label' => 'Hi label', + 'description' => 'Hello description', + 'desc_tip' => true, + 'value' => null, + 'name' => 'gla_name', + 'is_root' => true, + 'children' => [ + '_gla_select' => [ + 'id' => 'gla_name__gla_select', + 'type' => 'select', + 'label' => 'Hi label', + 'description' => 'Hello description', + 'desc_tip' => true, + 'value' => null, + 'name' => 'gla_name[_gla_select]', + 'is_root' => false, + 'children' => [], + 'class' => 'select short', + 'options' => [ + '_gla_custom_value' => 'Enter a custom value', + '' => 'Default name', + 'use_admin_name' => 'Use admin name', + ], + ], + '_gla_custom_value' => [ + 'id' => 'gla_name__gla_custom_value', + 'type' => 'text', + 'label' => 'Enter your value', + 'description' => null, + 'desc_tip' => true, + 'value' => null, + 'name' => 'gla_name[_gla_custom_value]', + 'is_root' => false, + 'children' => [], + 'wrapper_class' => 'custom-input', + ], + ], + 'gla_wrapper_class' => ' select-with-text-input', + ], + $input->get_view_data() + ); + + // Selected an option other than value from the custom text input + $input->set_data( + [ + '_gla_select' => 'use_admin_name', + '_gla_custom_value' => null, + ] + ); + + $this->assertEquals( 'use_admin_name', $input->get_view_data()['value'] ); + $this->assertEquals( 'use_admin_name', $input->get_view_data()['children']['_gla_select']['value'] ); + $this->assertNull( $input->get_view_data()['children']['_gla_custom_value']['value'] ); + + // Selected the _gla_custom_value option and entered a value via the custom text input + $input->set_data( + [ + '_gla_select' => 'mismatching any option', + '_gla_custom_value' => 'Say my name!', + ] + ); + + $this->assertEquals( 'Say my name!', $input->get_view_data()['value'] ); + $this->assertEquals( '_gla_custom_value', $input->get_view_data()['children']['_gla_select']['value'] ); + $this->assertEquals( 'Say my name!', $input->get_view_data()['children']['_gla_custom_value']['value'] ); + } + + + public function test_text() { + $input = new Text(); + + $this->assertEquals( 'text', $input->get_type() ); + $this->assertEquals( 'woocommerce/product-text-field', $input->get_block_name() ); + } +} diff --git a/tests/Unit/Admin/Input/InputTest.php b/tests/Unit/Admin/Input/InputTest.php new file mode 100644 index 0000000000..1277477410 --- /dev/null +++ b/tests/Unit/Admin/Input/InputTest.php @@ -0,0 +1,187 @@ +input = new Input( 'text', 'woocommerce/product-text-field' ); + } + + public function test_constructor_and_init_values() { + $input = new Input( 'integer', 'woocommerce/product-number-field' ); + + $this->assertEquals( 'integer', $input->get_type() ); + $this->assertEquals( 'woocommerce/product-number-field', $input->get_block_name() ); + } + + public function test_set_id_get_id() { + $this->assertNull( $this->input->get_id() ); + $this->input->set_id( 'color' ); + $this->assertEquals( 'color', $this->input->get_id() ); + } + + public function test_set_label_get_label() { + $this->assertNull( $this->input->get_label() ); + $this->input->set_label( 'Color' ); + $this->assertEquals( 'Color', $this->input->get_label() ); + } + + public function test_set_description_get_description() { + $this->assertNull( $this->input->get_description() ); + $this->input->set_description( 'Color of the product' ); + $this->assertEquals( 'Color of the product', $this->input->get_description() ); + } + + public function test_set_value_get_value() { + $this->assertNull( $this->input->get_value() ); + $this->input->set_value( 'black' ); + $this->assertEquals( 'black', $this->input->get_value() ); + } + + public function test_get_view_id() { + $this->input->set_name( 'leaf' ); + $this->assertEquals( 'gla_leaf', $this->input->get_view_id() ); + + // An input can have a Form parent + $this->input->set_id( 'leaf' ); + $form = new Form(); + $form->add( $this->input ); + $this->assertEquals( 'gla__leaf', $this->input->get_view_id() ); + + // The depth of the tree structure of Form and Input can be > 1. Depth starts from 0 + $nested_input = new Input( 'text', '' ); + $nested_input->set_id( 'mid-node' ); + $form->remove( 'leaf' ); + $form->add( $nested_input ); + $nested_input->add( $this->input ); + $this->assertEquals( 'gla__mid-node_leaf', $this->input->get_view_id() ); + } + + public function test_get_view_data() { + $form = new Form(); + $leaf = new Input( 'select', '' ); + + $this->input + ->set_id( 'color' ) + ->set_label( 'Color' ) + ->set_value( 'black' ) + ->set_description( 'Color of the product' ) + ->set_name( 'attr_color' ); + + $leaf + ->set_id( 'leaf' ) + ->set_name( 'attr_leaf' ); + + $form->add( $this->input ); + $this->input->add( $leaf ); + + $this->assertEquals( + [ + // Input + 'id' => 'gla__color', + 'type' => 'text', + 'label' => 'Color', + 'value' => 'black', + 'description' => 'Color of the product', + 'desc_tip' => true, + // Form + 'name' => 'gla_[attr_color]', + 'is_root' => false, + 'children' => [ + 'attr_leaf' => [ + // Input + 'id' => 'gla__color_leaf', + 'type' => 'select', + 'label' => null, + 'value' => null, + 'description' => null, + 'desc_tip' => true, + // Form + 'name' => 'gla_[attr_color][attr_leaf]', + 'is_root' => false, + 'children' => [], + ], + ], + ], + $this->input->get_view_data() + ); + } + + public function test_set_block_attribute_get_block_attributes() { + $this->input->set_id( 'color' ); + + $this->assertEquals( + [ + 'property' => 'meta_data._wc_gla_color', + 'label' => null, + 'tooltip' => null, + ], + $this->input->get_block_attributes() + ); + + $this->input->set_label( 'Color' ); + $this->input->set_description( 'Color of the product' ); + + $this->assertEquals( + [ + 'property' => 'meta_data._wc_gla_color', + 'label' => 'Color', + 'tooltip' => 'Color of the product', + ], + $this->input->get_block_attributes() + ); + + $this->input->set_block_attribute( 'required', true ); + $this->input->set_block_attribute( 'min', 10 ); + $this->input->set_block_attribute( 'placeholder', 'Enter a color' ); + + $this->assertEquals( + [ + 'property' => 'meta_data._wc_gla_color', + 'label' => 'Color', + 'tooltip' => 'Color of the product', + 'required' => true, + 'min' => 10, + 'placeholder' => 'Enter a color', + ], + $this->input->get_block_attributes() + ); + } + + public function test_get_block_config() { + $this->input->set_id( 'size' ); + $this->input->set_label( 'Size' ); + $this->input->set_description( 'Size of the product' ); + $this->input->set_block_attribute( 'maxLength', 120 ); + $this->input->set_block_attribute( 'help', 'Hello!' ); + + $this->assertEquals( + [ + 'id' => 'google-listings-and-ads-product-attributes-size', + 'blockName' => 'woocommerce/product-text-field', + 'attributes' => [ + 'property' => 'meta_data._wc_gla_size', + 'label' => 'Size', + 'tooltip' => 'Size of the product', + 'maxLength' => 120, + 'help' => 'Hello!', + ], + ], + $this->input->get_block_config() + ); + } +}