From 49087a6a35cc5b23ad0e80dcc94e5a9a73d14e7d Mon Sep 17 00:00:00 2001 From: Eduardo humberto Date: Mon, 5 Mar 2018 15:41:32 -0300 Subject: [PATCH] create tests for filter types support --- .../entities/class-tainacan-filter.php | 35 +++++++++++ .../selectbox/class-tainacan-selectbox.php | 2 +- .../class-tainacan-custom-interval.php | 3 +- .../class-tainacan-filter-type.php | 25 ++++++++ tests/test-api-filters.php | 4 +- tests/test-filters.php | 63 ++++++++++++++++++- 6 files changed, 127 insertions(+), 5 deletions(-) diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index 2f91e9831..500cf9961 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -75,6 +75,11 @@ class Filter extends Entity { */ function get_filter_type_object(){ $class_name = $this->get_filter_type(); + + if( !class_exists( $class_name ) ){ + return false; + } + $object_type = new $class_name(); $object_type->set_options( $this->get_filter_options() ); return $object_type; @@ -158,4 +163,34 @@ class Filter extends Entity { public function set_filter_type($value){ $this->set_mapped_property('filter_type', ( is_object( $value ) ) ? get_class( $value ) : $value ); } + + /** + * {@inheritdoc } + * + * Also validates the field, calling the validate_options callback of the Field Type + * + * @return bool valid or not + */ + public function validate() { + $is_valid = parent::validate(); + if (false === $is_valid) + return false; + + $fto = $this->get_filter_type_object(); + if (is_object($fto)) { + $is_valid = $fto->validate_options( $this ); + } + + if (true === $is_valid) + return true; + + if (!is_array($is_valid)) + throw new \Exception("Return of validate_options field type method should be an Array in case of error"); + + foreach ($is_valid as $field => $message) { + $this->add_error($field, $message); + } + + return false; + } } \ No newline at end of file diff --git a/src/classes/field-types/selectbox/class-tainacan-selectbox.php b/src/classes/field-types/selectbox/class-tainacan-selectbox.php index b16f05ed9..297e9da21 100644 --- a/src/classes/field-types/selectbox/class-tainacan-selectbox.php +++ b/src/classes/field-types/selectbox/class-tainacan-selectbox.php @@ -12,7 +12,7 @@ class Selectbox extends Field_Type { function __construct(){ // call field type constructor parent::__construct(); - parent::set_primitive_type(''); + parent::set_primitive_type('string'); $this->component = 'tainacan-selectbox'; } diff --git a/src/classes/filter-types/custom-interval/class-tainacan-custom-interval.php b/src/classes/filter-types/custom-interval/class-tainacan-custom-interval.php index 904c4c8a0..3cba00b78 100644 --- a/src/classes/filter-types/custom-interval/class-tainacan-custom-interval.php +++ b/src/classes/filter-types/custom-interval/class-tainacan-custom-interval.php @@ -15,8 +15,9 @@ class Custom_Interval extends Filter_Type { } /** - * @param $field + * @param $filter * @return string + * @internal param $field */ public function render( $filter ){ $type = ( $filter->get_field()->get_field_type() === 'Tainacan\Field_Types\Date' ) ? 'date' : 'numeric'; diff --git a/src/classes/filter-types/filter-type/class-tainacan-filter-type.php b/src/classes/filter-types/filter-type/class-tainacan-filter-type.php index 3405b7365..2647ab6f5 100644 --- a/src/classes/filter-types/filter-type/class-tainacan-filter-type.php +++ b/src/classes/filter-types/filter-type/class-tainacan-filter-type.php @@ -1,6 +1,7 @@ options = ( is_array( $options ) ) ? $options : unserialize( $options ); } + + /** + * Validates the options Array + * + * This method should be declared by each filter type sub classes + * + * @param \Tainacan\Entities\Filter $filter The field object that is beeing validated + * @return true|Array True if options are valid. If invalid, returns an array where keys are the field keys and values are error messages. + */ + public function validate_options(\Tainacan\Entities\Filter $filter) { + $field_type = $filter->get_field()->get_field_type(); + //if there is no field to validate + if( !$field_type ){ + return true; + } + + $class = ( is_object( $field_type ) ) ? $field_type : new $field_type(); + + if(in_array( $class->get_primitive_type(), $this->supported_types )){ + return true; + } else { + return ['unsupported_type' => __('The field primitive type is not supported by this filter', 'tainacan')]; + } + } } \ No newline at end of file diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index ba8e13d28..1d6a9384e 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -24,7 +24,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { 'name' => 'Metadata filtered', 'description' => 'Is filtered', 'collection_id' => $collection->get_id(), - 'field_type' => 'Tainacan\Field_Types\Text', + 'field_type' => 'Tainacan\Field_Types\Numeric', ), true, true @@ -47,7 +47,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $data = $response->get_data(); - $this->assertTrue(is_array($data) && array_key_exists('filter_type', $data), sprintf('cannot create a range, response: %s', print_r($data, true))); + $this->assertTrue(is_array($data) && array_key_exists('filter_type', $data), sprintf('cannot create a custom interval, response: %s', print_r($data, true))); $this->assertEquals('Tainacan\Filter_Types\Custom_Interval', $data['filter_type']); $this->assertEquals('Filter name', $data['name']); } diff --git a/tests/test-filters.php b/tests/test-filters.php index 6db6e9597..c4b4f42b4 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -99,9 +99,70 @@ class Filters extends TAINACAN_UnitTestCase { global $Tainacan_Filters; $all_filter_types = $Tainacan_Filters->fetch_filter_types(); - $this->assertEquals( 2, count( $all_filter_types ) ); + $this->assertEquals( 3, count( $all_filter_types ) ); $float_filters = $Tainacan_Filters->fetch_supported_filter_types('float'); $this->assertTrue( count( $float_filters ) > 0 ); } + + /** + * @group filter + */ + function test_validate_supported_filters(){ + global $Tainacan_Filters; + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Collection filtered', + 'description' => 'Is filtered', + ), + true + ); + + $field2 = $this->tainacan_entity_factory->create_entity( + 'field', + array( + 'name' => 'Other filtered', + 'description' => 'Is filtered', + 'field_type' => 'Tainacan\Field_Types\Text', + 'collection_id' => $collection->get_id() + ), + true + ); + + $autocomplete = $this->tainacan_filter_factory->create_filter('autocomplete'); + + $filter = $this->tainacan_entity_factory->create_entity( + 'filter', + array( + 'name' => 'filtro', + 'collection' => $collection, + 'description' => 'descricao', + 'field' => $field2, + 'filter_type' => $autocomplete + ), + true + ); + + $test = $Tainacan_Filters->fetch( $filter->get_id() ); + + $this->assertEquals( 'Tainacan\Filter_Types\Autocomplete', $test->get_filter_type()); + + $custom_interval = $this->tainacan_filter_factory->create_filter('custom_interval'); + + $this->expectException('ErrorException'); + $filter2 = $this->tainacan_entity_factory->create_entity( + 'filter', + array( + 'name' => 'filtro 2', + 'collection' => $collection, + 'description' => 'descricao', + 'field' => $field2, + 'filter_type' => $custom_interval + ), + true + ); + + } } \ No newline at end of file