testes dos filtros
This commit is contained in:
parent
2a25057ecd
commit
b3cb374a27
|
@ -3,9 +3,9 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
exit;
|
||||
}
|
||||
|
||||
class Tainacan_Filter extends Entity {
|
||||
class Tainacan_Filter extends Tainacan_Entity {
|
||||
|
||||
use EntityCollectionRelation;
|
||||
use Tainacan_Entity_Collection_Relation;
|
||||
|
||||
function __construct( $which = 0 ) {
|
||||
|
||||
|
@ -43,6 +43,11 @@ class Tainacan_Filter extends Entity {
|
|||
return $this->get_mapped_property('color');
|
||||
}
|
||||
|
||||
function get_metadata() {
|
||||
$id = $this->get_mapped_property('metadata');
|
||||
return new Tainacan_Metadata( $id );
|
||||
}
|
||||
|
||||
function get_widget( $output = 'object' ){
|
||||
if( $output === 'object'){
|
||||
return unserialize( $this->get_mapped_property('option') );
|
||||
|
@ -64,15 +69,29 @@ class Tainacan_Filter extends Entity {
|
|||
return $this->set_mapped_property('description', $value);
|
||||
}
|
||||
|
||||
function set_color($value) {
|
||||
function set_color( $value ) {
|
||||
return $this->set_mapped_property('parent', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tainacan_Metadata / int $value
|
||||
*/
|
||||
function set_metadata( $value ){
|
||||
$id = ( $value instanceof Tainacan_Metadata ) ? $value->get_id() : $value;
|
||||
|
||||
return $this->set_mapped_property('metadata', $id);
|
||||
}
|
||||
|
||||
function set_widget($value){
|
||||
if( is_object( $value ) && is_subclass_of( $value, 'Tainacan_Filter_Type' ) ){
|
||||
$this->set_option( $value );
|
||||
|
||||
return $this->set_mapped_property('widget', get_class( $value ) ) ;
|
||||
if( is_object( $value ) && is_subclass_of( $value, 'Tainacan_Filter_Type' ) && $this->get_metadata() ){
|
||||
$type = $this->get_metadata()->get_type();
|
||||
|
||||
//if filter matches the metadata type
|
||||
if( in_array( $type->get_primitive_type(), $value->get_supported_types() ) ){
|
||||
$this->set_option( $value );
|
||||
return $this->set_mapped_property('widget', get_class( $value ) ) ;
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -19,4 +19,8 @@ abstract class Tainacan_Field_Type {
|
|||
function get_validation_errors() {
|
||||
return [];
|
||||
}
|
||||
|
||||
function get_primitive_type(){
|
||||
return $this->primitive_type;
|
||||
}
|
||||
}
|
|
@ -3,9 +3,13 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
exit;
|
||||
}
|
||||
|
||||
abstract class Tainacan_Filter_Type extends Entity {
|
||||
abstract class Tainacan_Filter_Type extends Tainacan_Entity {
|
||||
|
||||
var $supported_types = [];
|
||||
|
||||
abstract function render( $metadata );
|
||||
|
||||
function get_supported_types(){
|
||||
return $this->supported_types;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class TainacanFieldType
|
||||
*/
|
||||
class Tainacan_List_Filter_Type extends Tainacan_Filter_Type {
|
||||
|
||||
function __construct(){
|
||||
$this->supported_types[] = 'string';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $metadata
|
||||
* @return string
|
||||
*/
|
||||
|
||||
function render( $filter ){
|
||||
return '<tainacan-filter-list name="'.$filter->get_name().'"></tainacan-filter-list>';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class TainacanFieldType
|
||||
*/
|
||||
class Tainacan_Range_Filter_Type extends Tainacan_Filter_Type {
|
||||
|
||||
function __construct(){
|
||||
$this->supported_types = ['float','date'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $metadata
|
||||
* @return string
|
||||
*/
|
||||
|
||||
function render( $filter ){
|
||||
return '<tainacan-filter-range name="'.$filter->get_name().'"></tainacan-filter-range>';
|
||||
}
|
||||
}
|
|
@ -40,6 +40,10 @@ class Tainacan_Filters {
|
|||
'map' => 'meta',
|
||||
'validation' => ''
|
||||
],
|
||||
'metadata' => [
|
||||
'map' => 'meta',
|
||||
'validation' => ''
|
||||
],
|
||||
];
|
||||
|
||||
function __construct(){
|
||||
|
@ -151,4 +155,36 @@ class Tainacan_Filters {
|
|||
function get_filter_by_id($id) {
|
||||
return new Tainacan_Filter($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
function get_filters_by_metadata_type( $type ){
|
||||
$result = array();
|
||||
$filters_type = $this->get_all_filters_type();
|
||||
|
||||
foreach ( $filters_type as $filter_type ){
|
||||
if( in_array( $type, $filter_type->get_supported_types() ) ){
|
||||
$result[] = $filter_type;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
function get_all_filters_type() {
|
||||
$result = array();
|
||||
|
||||
foreach (get_declared_classes() as $class) {
|
||||
if (is_subclass_of($class, 'Tainacan_Filter_Type')){
|
||||
$result[] = new $class();
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
|
@ -21,37 +21,68 @@ class Test_Filters extends WP_UnitTestCase {
|
|||
$collection = $Tainacan_Collections->insert($collection);
|
||||
|
||||
//setando os valores na classe do metadado
|
||||
$filter->set_name('metadado');
|
||||
$filter->set_description('descricao');
|
||||
$filter->set_name('filtro');
|
||||
$filter->set_collection( $collection );
|
||||
|
||||
//inserindo o metadado
|
||||
$filter = $Tainacan_Filters->insert( $filter );
|
||||
|
||||
$test = $Tainacan_Filters->get_filter_by_id( $filter->get_id() );
|
||||
|
||||
$this->assertEquals($test->get_name(), 'filtro');
|
||||
$this->assertEquals($test->get_collection_id(), $collection->get_id());
|
||||
}
|
||||
|
||||
function test_add_with_metadata_and_type(){
|
||||
global $Tainacan_Collections, $Tainacan_Filters,$Tainacan_Metadatas;
|
||||
|
||||
$collection = new Tainacan_Collection();
|
||||
$metadata = new Tainacan_Metadata();
|
||||
$filter = new Tainacan_Filter();
|
||||
$type = new Tainacan_Text_Field_Type();
|
||||
$filter_list_type = new Tainacan_List_Filter_Type();
|
||||
$filter_range_type = new Tainacan_Range_Filter_Type();
|
||||
|
||||
$collection->set_name('teste');
|
||||
$collection = $Tainacan_Collections->insert($collection);
|
||||
|
||||
//setando os valores na classe do metadado
|
||||
$metadata->set_name('metadado');
|
||||
$metadata->set_collection_id( $collection->get_id() );
|
||||
$metadata->set_type( $type );
|
||||
|
||||
|
||||
//inserindo o metadado
|
||||
$metadata = $Tainacan_Metadatas->insert($metadata);
|
||||
|
||||
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id());
|
||||
//inserindo o filtro
|
||||
$filter->set_name('filtro');
|
||||
$filter->set_collection( $collection );
|
||||
$filter->set_metadata( $metadata );
|
||||
|
||||
$i = new Tainacan_Item();
|
||||
//nao devera permitir um filtro Range para o tipo string
|
||||
$this->assertTrue( $filter->set_widget( $filter_range_type ) === null );
|
||||
|
||||
$i->set_title('item teste');
|
||||
$i->set_description('adasdasdsa');
|
||||
$i->set_collection($collection);
|
||||
$filter->set_widget( $filter_list_type );
|
||||
|
||||
global $Tainacan_Items;
|
||||
$item = $Tainacan_Items->insert($i);
|
||||
$filter = $Tainacan_Filters->insert( $filter );
|
||||
|
||||
$item = $Tainacan_Items->get_item_by_id($item->get_id());
|
||||
$test = $Tainacan_Filters->get_filter_by_id( $filter->get_id() );
|
||||
|
||||
$this->assertEquals( $test->get_name(), 'filtro');
|
||||
$this->assertEquals( $test->get_collection_id(), $collection->get_id() );
|
||||
$this->assertEquals( $test->get_metadata()->get_id(), $metadata->get_id() );
|
||||
$this->assertEquals( get_class( $test->get_widget() ), get_class( $filter_list_type ) );
|
||||
|
||||
}
|
||||
|
||||
$value = 'teste_val';
|
||||
function test_get_filters_type(){
|
||||
global $Tainacan_Filters;
|
||||
|
||||
$item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
|
||||
$item_metadata->set_value($value);
|
||||
$this->assertTrue($item_metadata->validate());
|
||||
$item_metadata = $Tainacan_Item_Metadata->insert($item_metadata);
|
||||
$all_filter_types = $Tainacan_Filters->get_all_filters_type();
|
||||
$this->assertEquals( count( $all_filter_types ), 2 );
|
||||
|
||||
$n_item_metadata = new Tainacan_Item_Metadata_Entity($item, $metadata);
|
||||
$n_item_metadata->set_value($value);
|
||||
$this->assertFalse($n_item_metadata->validate());
|
||||
$float_filters = $Tainacan_Filters->get_filters_by_metadata_type('float');
|
||||
$this->assertTrue( count( $float_filters ) > 0 );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue