From fd6af15303d8a090df876cd896228724d9db5071 Mon Sep 17 00:00:00 2001 From: weryques Date: Tue, 20 Feb 2018 16:06:18 -0300 Subject: [PATCH] Initiated Meta Query Tests --- src/admin/admin.vue | 2 +- src/api/class-tainacan-rest-controller.php | 35 +++- ...tainacan-rest-item-metadata-controller.php | 2 +- .../class-tainacan-collections.php | 1 - .../class-tainacan-item-metadata.php | 4 +- tests/tainacan-unit-test-case.php | 1 + tests/test-api-queries.php | 181 +++++++++++++++++- 7 files changed, 216 insertions(+), 10 deletions(-) diff --git a/src/admin/admin.vue b/src/admin/admin.vue index 200e37ad3..8ad596c3b 100644 --- a/src/admin/admin.vue +++ b/src/admin/admin.vue @@ -7,7 +7,7 @@ {{ $i18n.getString('header', 'collections')}} - + diff --git a/src/api/class-tainacan-rest-controller.php b/src/api/class-tainacan-rest-controller.php index 12d4aeeb6..20cce44ca 100644 --- a/src/api/class-tainacan-rest-controller.php +++ b/src/api/class-tainacan-rest-controller.php @@ -81,7 +81,6 @@ class TAINACAN_REST_Controller extends WP_REST_Controller { 'authorid' => 'author_id', 'authorname' => 'author_name', 'search' => 's', - 'posttype' => 'post_type', 'status' => 'post_status', 'offset' => 'offset', 'metaquery' => 'meta_query', @@ -116,9 +115,26 @@ class TAINACAN_REST_Controller extends WP_REST_Controller { foreach ($map as $mapped => $mapped_v){ if(isset($request[$mapped])){ if($mapped === 'metaquery'){ - foreach ($meta_query as $mapped_meta => $meta_v){ - $args[$mapped_v][$meta_v] = $request[$mapped][$mapped_meta]; + $request_meta_query = $request[$mapped]; + + // If is a multidimensional array (array of array) + if($this->contains_array($request_meta_query)) { + + foreach ( $request_meta_query as $index1 => $a ) { + foreach ( $meta_query as $mapped_meta => $meta_v ) { + if ( isset( $a[ $meta_v ] ) ) { + $args[ $mapped_v ][ $index1 ][ $meta_v ] = $request[ $mapped ][ $index1 ][ $meta_v ]; + } + } + } + } else { + foreach ( $meta_query as $mapped_meta => $meta_v ) { + if(isset($request[$mapped][$meta_v])) { + $args[ $mapped_v ][ $meta_v ] = $request[ $mapped ][ $meta_v ]; + } + } } + } elseif ($mapped === 'datequery') { foreach ($date_query as $date_meta => $date_v){ $args[$mapped_v][$date_v] = $request[$mapped][$date_meta]; @@ -129,9 +145,22 @@ class TAINACAN_REST_Controller extends WP_REST_Controller { } } + $args['perm'] = 'readable'; + + var_dump($args); return $args; } + protected function contains_array($array){ + foreach ($array as $value){ + if(is_array($value)){ + return true; + } + } + + return false; + } + } ?> \ No newline at end of file diff --git a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php index 69aa75dec..29c48191d 100644 --- a/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-item-metadata-controller.php @@ -192,7 +192,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller { $item_metadata->set_value( $value ); if ( $item_metadata->validate() ) { - $field_updated = $this->item_metadata_repository->insert( $item_metadata ); + $field_updated = $this->item_metadata_repository->update( $item_metadata ); $prepared_item = $this->prepare_item_for_response($field_updated, $request); $prepared_item['field']['field_type_object'] = $this->prepare_item_for_response($field_updated->get_field()->get_field_type_object(), $request); diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 990049d69..7076daa1b 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -267,7 +267,6 @@ class Collections extends Repository { } elseif(is_array($args)) { $args = array_merge([ 'posts_per_page' => -1, - 'post_status' => 'publish', ], $args); $args = $this->parse_fetch_args($args); diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 33426ccaf..072d14e36 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -139,5 +139,7 @@ class Item_Metadata extends Repository { * * @return mixed */ - public function update( $object, $new_values = null ) {} + public function update( $object, $new_values = null ) { + return $this->insert($object); + } } \ No newline at end of file diff --git a/tests/tainacan-unit-test-case.php b/tests/tainacan-unit-test-case.php index 68095debe..ce2373784 100644 --- a/tests/tainacan-unit-test-case.php +++ b/tests/tainacan-unit-test-case.php @@ -8,6 +8,7 @@ class TAINACAN_UnitTestCase extends \WP_UnitTestCase { protected $tainacan_entity_factory; protected $tainacan_field_factory; protected $tainacan_filter_factory; + protected $tainacan_item_metadata_factory; protected $user_id; public function setUp(){ diff --git a/tests/test-api-queries.php b/tests/test-api-queries.php index 4c0446c8b..b1f288e28 100644 --- a/tests/test-api-queries.php +++ b/tests/test-api-queries.php @@ -7,12 +7,187 @@ namespace Tainacan\Tests; * **/ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { - public function test_collections_queries(){ - $this->tainacan_entity_factory->create_entity( + public function test_queries(){ + + // Populate the database + $collectionA = $this->tainacan_entity_factory->create_entity( 'collection', - [], + [ + 'name' => 'A', + 'description' => 'Collection A', + 'status' => 'publish' + ], true ); + + // Create Items + $itemA1 = $this->tainacan_entity_factory->create_entity( + 'item', + [ + 'title' => 'Item A-1', + 'description' => 'Item in collection A', + 'status' => 'publish', + 'collection' => $collectionA + ], + true + ); + + $itemA2 = $this->tainacan_entity_factory->create_entity( + 'item', + [ + 'title' => 'Item A-2', + 'description' => 'Item in collection A', + 'status' => 'private', + 'collection' => $collectionA + ], + true + ); + + $itemA3 = $this->tainacan_entity_factory->create_entity( + 'item', + [ + 'title' => 'Item A-3', + 'description' => 'Item in collection A', + 'status' => 'publish', + 'collection' => $collectionA + ], + true + ); + + // Create Metadata and Field Type + + $field_type = $this->tainacan_field_factory->create_field('text'); + + $fieldA1 = $this->tainacan_entity_factory->create_entity( + 'field', + [ + 'name' => 'Field A-1', + 'description' => 'Simple field in collection A', + 'status' => 'publish', + 'collection' => $collectionA, + 'field_type' => $field_type + ], + true + ); + + $fieldA2 = $this->tainacan_entity_factory->create_entity( + 'field', + [ + 'name' => 'Field A-2', + 'description' => 'Simple field in collection A', + 'status' => 'publish', + 'collection' => $collectionA, + 'field_type' => $field_type + ], + true + ); + + $fieldA3 = $this->tainacan_entity_factory->create_entity( + 'field', + [ + 'name' => 'Field A-3', + 'description' => 'Multiple field in a collection A', + 'status' => 'publish', + 'collection' => $collectionA, + 'field_type' => $field_type, + 'multiple' => 'yes' + ], + true + ); + + // Create a Item Metadata + $itemA1_metadata1 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA1, $fieldA1, 'E'); + $itemA1_metadata2 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA1, $fieldA2, 'X'); + $itemA1_metadata3 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA1, $fieldA3, ['Y', 'Z']); + + $itemA2_metadata1 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA2, $fieldA1, 'D'); + $itemA2_metadata2 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA2, $fieldA2, 'Q'); + $itemA2_metadata3 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA2, $fieldA3, ['R', 'S']); + + $itemA3_metadata1 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA3, $fieldA1, 'G'); + $itemA3_metadata2 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA3, $fieldA2, 'T'); + $itemA3_metadata3 = $this->tainacan_item_metadata_factory->create_item_metadata($itemA3, $fieldA3, ['Q', 'V']); + + // + + $collectionB = $this->tainacan_entity_factory->create_entity( + 'collection', + [ + 'name' => 'B', + 'description' => 'Collection B', + 'status' => 'publish' + ], + true + ); + + $collectionC = $this->tainacan_entity_factory->create_entity( + 'collection', + [ + 'name' => 'C', + 'description' => 'Collection C', + 'status' => 'private' + ], + true + ); + + + // Fetch a collection with a specific name + $name_query = ['name' => 'B']; + + $name_request = new \WP_REST_Request('GET', $this->namespace . '/collections'); + + $name_request->set_query_params($name_query); + + $name_response = $this->server->dispatch($name_request); + $data1 = $name_response->get_data(); + + $this->assertCount(1, $data1); + $this->assertEquals($collectionB->get_name(), $data1[0]['name']); + + + + // Search collection with a specific keyword and not other keyword + $search_query = ['search' => 'Collection -A']; + + $search_request = new \WP_REST_Request('GET', $this->namespace . '/collections'); + + $search_request->set_query_params($search_query); + + $search_response = $this->server->dispatch($search_request); + $data2 = $search_response->get_data(); + + $this->assertCount(2, $data2); + + $names = [$data2[0]['name'], $data2[1]['name']]; + $this->assertNotContains('A', $names); + + + + /* Meta Query: + Fetch items from a collection desc ordered by fieldA1 and its only in range A to Y. + */ + + $meta_query = [ + 'metakey' => $fieldA1->get_id(), + 'orderby' => 'meta_value', + 'order' => 'DESC', + 'metaquery' => array( + array( + 'key' => $fieldA1->get_id(), + 'value' => array( 'A', 'F' ), + 'compare' => 'BETWEEN' + ), + ), + ]; + + $meta_query_request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collectionA->get_id() . '/items'); + + $meta_query_request->set_query_params($meta_query); + + $meta_query_response = $this->server->dispatch($meta_query_request); + $data3 = $meta_query_response->get_data(); + + $this->assertCount(2, $data3); } }