test and fix metadata and filter type options in API response

This commit is contained in:
Leo Germani 2019-10-16 11:05:29 -03:00
parent 626e1cce60
commit 9440f70551
6 changed files with 418 additions and 6 deletions

View File

@ -366,7 +366,7 @@ class Entity {
$attributes = []; $attributes = [];
foreach($map as $prop => $content) { foreach($map as $prop => $content) {
$attributes[$prop] = $this->get_mapped_property($prop); $attributes[$prop] = $this->get($prop);
} }
$hook_prefix = self::get_post_type(); $hook_prefix = self::get_post_type();

View File

@ -152,7 +152,7 @@ class Filter extends Entity {
} }
$object_type = new $class_name(); $object_type = new $class_name();
$object_type->set_options( $this->get_filter_type_options() ); $object_type->set_options( $this->get_mapped_property('filter_type_options') );
return $object_type; return $object_type;
} }
@ -171,6 +171,10 @@ class Filter extends Entity {
* @return array Configurations for the filter type object * @return array Configurations for the filter type object
*/ */
function get_filter_type_options(){ function get_filter_type_options(){
$object = $this->get_filter_type_object();
if ($object) {
return $object->get_options(); // merge with dedault filter type options
}
return $this->get_mapped_property('filter_type_options'); return $this->get_mapped_property('filter_type_options');
} }

View File

@ -178,7 +178,7 @@ class Metadatum extends Entity {
} }
$object_type = new $class_name(); $object_type = new $class_name();
$object_type->set_options( $this->get_metadata_type_options() ); $object_type->set_options( $this->get_mapped_property('metadata_type_options') );
return $object_type; return $object_type;
} }
@ -197,6 +197,10 @@ class Metadatum extends Entity {
* @return array Configurations for the metadatum type object * @return array Configurations for the metadatum type object
*/ */
function get_metadata_type_options(){ function get_metadata_type_options(){
$object = $this->get_metadata_type_object();
if ($object) {
return $object->get_options(); // merge with dedault metadata type options
}
return $this->get_mapped_property('metadata_type_options'); return $this->get_mapped_property('metadata_type_options');
} }

View File

@ -366,6 +366,238 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
$this->assertCount(2, $data4); $this->assertCount(2, $data4);
//$this->assertEquals('4x Filter', $data4[0]['name']); //$this->assertEquals('4x Filter', $data4[0]['name']);
} }
public function test_return_filter_type_options_in_get_item() {
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$meta = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'number',
'status' => 'publish',
'collection' => $collection1,
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
),
true
);
$filter_numeric = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'numeric',
'status' => 'publish',
'collection' => $collection1,
'metadatum' => $meta,
'filter_type' => 'Tainacan\Filter_Types\Numeric_Interval',
'filter_type_options' => [
'step' => 3,
]
),
true
);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/filters/' . $filter_numeric->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($filter_numeric->get_id(), $data['id']);
$this->assertEquals('numeric', $data['name']);
$this->assertEquals(3, $data['filter_type_options']['step']);
}
public function test_return_filter_type_options_in_get_items() {
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$meta = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'number',
'status' => 'publish',
'collection' => $collection1,
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
),
true
);
$filter_numeric = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'numeric',
'status' => 'publish',
'collection' => $collection1,
'metadatum' => $meta,
'filter_type' => 'Tainacan\Filter_Types\Numeric_Interval',
'filter_type_options' => [
'step' => 3,
]
),
true
);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/collection/' . $collection1->get_id() . '/filters'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
//var_dump($data, $this->namespace . '/collection/' . $collection2->get_id() . '/metadata/');
foreach ($data as $d) {
if ($d['id'] == $filter_numeric->get_id()) {
$meta = $d;
break;
}
}
$this->assertEquals($filter_numeric->get_id(), $meta['id']);
$this->assertEquals('numeric', $meta['name']);
$this->assertEquals(3, $meta['filter_type_options']['step']);
}
public function test_return_filter_type_options_in_get_item_default_value() {
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$meta = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'number',
'status' => 'publish',
'collection' => $collection1,
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
),
true
);
$filter_numeric = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'numeric',
'status' => 'publish',
'collection' => $collection1,
'metadatum' => $meta,
'filter_type' => 'Tainacan\Filter_Types\Numeric_Interval',
// 'filter_type_options' => [
// 'step' => 3,
// ]
),
true
);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/filters/' . $filter_numeric->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($filter_numeric->get_id(), $data['id']);
$this->assertEquals('numeric', $data['name']);
$this->assertEquals(1, $data['filter_type_object']['options']['step']);
$this->assertEquals(1, $data['filter_type_options']['step']);
}
public function test_return_metadata_type_options_inside_metadatum_property() {
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$core1 = $collection1->get_core_title_metadatum();
$meta_relationship = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'relationship',
'status' => 'publish',
'collection' => $collection2,
'metadata_type' => 'Tainacan\Metadata_Types\Relationship',
'metadata_type_options' => [
'repeated' => 'yes',
'collection_id' => $collection1->get_id(),
'search' => $core1->get_id()
]
),
true
);
$filter = $this->tainacan_entity_factory->create_entity(
'filter',
array(
'name' => 'test',
'status' => 'publish',
'collection' => $collection2,
'metadatum' => $meta_relationship,
'filter_type' => 'Tainacan\Filter_Types\Autocomplete',
),
true
);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/filters/' . $filter->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($filter->get_id(), $data['id']);
$this->assertEquals('test', $data['name']);
$this->assertEquals('yes', $data['metadatum']['metadata_type_object']['options']['repeated']);
$this->assertEquals($collection1->get_id(), $data['metadatum']['metadata_type_object']['options']['collection_id']);
$this->assertEquals($core1->get_id(), $data['metadatum']['metadata_type_object']['options']['search']);
}
} }
?> ?>

View File

@ -13,6 +13,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
array( array(
'name' => 'Javascript Frameworks', 'name' => 'Javascript Frameworks',
'description' => 'The best framework to javascript', 'description' => 'The best framework to javascript',
'status' => 'publish'
), ),
true true
); );

View File

@ -149,7 +149,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
'collection', 'collection',
array( array(
'name' => 'Statement', 'name' => 'Statement',
'description' => 'No Statement' 'description' => 'No Statement',
'status' => 'publish'
), ),
true true
); );
@ -222,7 +223,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
'collection', 'collection',
array( array(
'name' => 'Statement', 'name' => 'Statement',
'description' => 'No Statement' 'description' => 'No Statement',
'status' => 'publish'
), ),
true true
); );
@ -404,6 +406,175 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
$this->assertEquals('No name', $data['name']); $this->assertEquals('No name', $data['name']);
} }
public function test_return_metadata_type_options_in_get_item() {
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$core1 = $collection1->get_core_title_metadatum();
$meta_relationship = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'relationship',
'status' => 'publish',
'collection' => $collection2,
'metadata_type' => 'Tainacan\Metadata_Types\Relationship',
'metadata_type_options' => [
'repeated' => 'yes',
'collection_id' => $collection1->get_id(),
'search' => $core1->get_id()
]
),
true
);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/metadata/' . $meta_relationship->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($meta_relationship->get_id(), $data['id']);
$this->assertEquals('relationship', $data['name']);
$this->assertEquals('yes', $data['metadata_type_options']['repeated']);
$this->assertEquals($collection1->get_id(), $data['metadata_type_options']['collection_id']);
$this->assertEquals($core1->get_id(), $data['metadata_type_options']['search']);
}
public function test_return_metadata_type_options_in_get_items() {
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$core1 = $collection1->get_core_title_metadatum();
$meta_relationship = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'relationship',
'status' => 'publish',
'collection' => $collection2,
'metadata_type' => 'Tainacan\Metadata_Types\Relationship',
'metadata_type_options' => [
'repeated' => 'yes',
'collection_id' => $collection1->get_id(),
'search' => $core1->get_id()
]
),
true
);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/collection/' . $collection2->get_id() . '/metadata'
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
//var_dump($data, $this->namespace . '/collection/' . $collection2->get_id() . '/metadata/');
foreach ($data as $d) {
if ($d['id'] == $meta_relationship->get_id()) {
$meta = $d;
break;
}
}
$this->assertEquals($meta_relationship->get_id(), $meta['id']);
$this->assertEquals('relationship', $meta['name']);
$this->assertEquals('yes', $meta['metadata_type_options']['repeated']);
$this->assertEquals($collection1->get_id(), $meta['metadata_type_options']['collection_id']);
$this->assertEquals($core1->get_id(), $meta['metadata_type_options']['search']);
}
public function test_return_metadata_type_options_in_get_item_default_option() {
$collection1 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test_col',
'status' => 'publish'
),
true
);
$tax = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'tax_test',
'collections' => [$collection1],
'status' => 'publish'
),
true
);
$meta = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'tax',
'status' => 'publish',
'collection' => $collection1,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'metadata_type_options' => [
'taxonomy_id' => $tax->get_id(),
]
),
true
);
$request = new \WP_REST_Request(
'GET',
$this->namespace . '/metadata/' . $meta->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals($meta->get_id(), $data['id']);
$this->assertEquals('tax', $data['name']);
$this->assertEquals($tax->get_id(), $data['metadata_type_options']['taxonomy_id']);
$this->assertEquals('no', $data['metadata_type_options']['allow_new_terms']);
}
} }
?> ?>