diff --git a/src/classes/entities/class-tainacan-entity.php b/src/classes/entities/class-tainacan-entity.php index 9940d8be7..d21c5dce0 100644 --- a/src/classes/entities/class-tainacan-entity.php +++ b/src/classes/entities/class-tainacan-entity.php @@ -366,7 +366,7 @@ class Entity { $attributes = []; foreach($map as $prop => $content) { - $attributes[$prop] = $this->get_mapped_property($prop); + $attributes[$prop] = $this->get($prop); } $hook_prefix = self::get_post_type(); diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index ada699f51..f1e84e2d1 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -152,7 +152,7 @@ class Filter extends Entity { } $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; } @@ -171,6 +171,10 @@ class Filter extends Entity { * @return array Configurations for the filter type object */ 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'); } diff --git a/src/classes/entities/class-tainacan-metadatum.php b/src/classes/entities/class-tainacan-metadatum.php index 70e965372..5a57dc165 100644 --- a/src/classes/entities/class-tainacan-metadatum.php +++ b/src/classes/entities/class-tainacan-metadatum.php @@ -178,7 +178,7 @@ class Metadatum extends Entity { } $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; } @@ -197,7 +197,11 @@ class Metadatum extends Entity { * @return array Configurations for the metadatum type object */ function get_metadata_type_options(){ - return $this->get_mapped_property('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'); } /** diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index e68ee2d65..6588078bd 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -366,6 +366,238 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { $this->assertCount(2, $data4); //$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']); + + } + } ?> \ No newline at end of file diff --git a/tests/test-api-items.php b/tests/test-api-items.php index a4ec553ea..cec2ec805 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -13,6 +13,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { array( 'name' => 'Javascript Frameworks', 'description' => 'The best framework to javascript', + 'status' => 'publish' ), true ); diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 8f43b1001..d098984e1 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -149,7 +149,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { 'collection', array( 'name' => 'Statement', - 'description' => 'No Statement' + 'description' => 'No Statement', + 'status' => 'publish' ), true ); @@ -222,7 +223,8 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { 'collection', array( 'name' => 'Statement', - 'description' => 'No Statement' + 'description' => 'No Statement', + 'status' => 'publish' ), true ); @@ -403,129 +405,176 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals($metadatum->get_id(), $data['id']); $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']); + + } - // public function test_fetch_all_metadatum_values(){ - // $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); - // - // $collection = $this->tainacan_entity_factory->create_entity( - // 'collection', - // array( - // 'name' => 'Statement', - // 'description' => 'No Statement', - // 'status' => 'publish' - // ), - // true - // ); - // - // $item1 = $this->tainacan_entity_factory->create_entity( - // 'item', - // array( - // 'title' => 'No name1', - // 'description' => 'No description1', - // 'status' => 'publish', - // 'collection' => $collection - // ), - // true - // ); - // - // $item2 = $this->tainacan_entity_factory->create_entity( - // 'item', - // array( - // 'title' => 'No name2', - // 'description' => 'No description2', - // 'status' => 'private', - // 'collection' => $collection - // ), - // true - // ); - // - // $item3 = $this->tainacan_entity_factory->create_entity( - // 'item', - // array( - // 'title' => 'No name3', - // 'description' => 'No description3', - // 'status' => 'private', - // 'collection' => $collection - // ), - // true - // ); - // - // $metadatum = $this->tainacan_entity_factory->create_entity( - // 'metadatum', - // array( - // 'name' => 'Data', - // 'description' => 'Descreve valor do campo data.', - // 'collection' => $collection, - // 'status' => 'publish', - // 'metadata_type' => 'Tainacan\Metadata_Types\Text', - // ), - // true - // ); - // - // $item_metadata1 = new \Tainacan\Entities\Item_Metadata_Entity($item1, $metadatum); - // $item_metadata1->set_value('12/12/2017'); - // - // $item_metadata1->validate(); - // $Tainacan_Item_Metadata->insert($item_metadata1); - // - // $item_metadata2 = new \Tainacan\Entities\Item_Metadata_Entity($item2, $metadatum); - // $item_metadata2->set_value('02/03/2018'); - // - // $item_metadata2->validate(); - // $Tainacan_Item_Metadata->insert($item_metadata2); - // - // // Is repeated for test return of duplicates - // $item_metadata3 = new \Tainacan\Entities\Item_Metadata_Entity($item3, $metadatum); - // $item_metadata3->set_value('12/12/2017'); - // - // $item_metadata3->validate(); - // $Tainacan_Item_Metadata->insert($item_metadata3); - // - // //======================= - // - // $request = new \WP_REST_Request( - // 'GET', - // $this->namespace . '/collection/' . $collection->get_id() . '/facets/' . $metadatum->get_id() - // ); - // - // //======================= - // - // // Set no one user - // wp_set_current_user(0); - // - // $response1 = $this->server->dispatch($request); - // - // $data1 = $response1->get_data(); - // - // $this->assertCount(1, $data1); - // $this->assertEquals('12/12/2017', $data1[0]['value']); - // - // //======================= - // - // $new_user1 = $this->factory()->user->create(array( 'role' => 'subscriber' )); - // wp_set_current_user($new_user1); - // - // $response1 = $this->server->dispatch($request); - // - // $data1 = $response1->get_data(); - // - // $this->assertCount(1, $data1); - // $this->assertEquals('12/12/2017', $data1[0]['value']); - // - // //======================= - // - // $new_user2 = $this->factory()->user->create(array( 'role' => 'administrator' )); - // wp_set_current_user($new_user2); - // - // $response2 = $this->server->dispatch($request); - // - // $data2 = $response2->get_data(); - // - // // Only two without duplicates - // //$this->assertCount(2, $data2); - // //$this->assertEquals('12/12/2017', $data2[0]['value']); - // //$this->assertEquals('02/03/2018', $data2[1]['value']); - // } } ?> \ No newline at end of file