create tests for filter types support
This commit is contained in:
parent
f5d9806f0e
commit
49087a6a35
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Filter_Types;
|
||||
use Tainacan\Field_Types;
|
||||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
|
@ -64,4 +65,28 @@ abstract class Filter_Type {
|
|||
public function set_options( $options ){
|
||||
$this->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')];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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']);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue