Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
mateuswetah 2018-02-20 16:43:20 -03:00
commit edf5777dc4
7 changed files with 216 additions and 10 deletions

View File

@ -7,7 +7,7 @@
</router-link> </router-link>
<router-link class="navbar-item" to="/collections">{{ $i18n.getString('header', 'collections')}}</router-link> <router-link class="navbar-item" to="/collections">{{ $i18n.getString('header', 'collections')}}</router-link>
</div> </div>
<a :href="wordpressAdmin" class="navbar-end"> <a :href="wordpressAdmin" class="is-pulled-right">
<i class="mdi mdi-close mdi-36px mdi-light"></i> <i class="mdi mdi-close mdi-36px mdi-light"></i>
</a> </a>
</nav> </nav>

View File

@ -81,7 +81,6 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
'authorid' => 'author_id', 'authorid' => 'author_id',
'authorname' => 'author_name', 'authorname' => 'author_name',
'search' => 's', 'search' => 's',
'posttype' => 'post_type',
'status' => 'post_status', 'status' => 'post_status',
'offset' => 'offset', 'offset' => 'offset',
'metaquery' => 'meta_query', 'metaquery' => 'meta_query',
@ -116,9 +115,26 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
foreach ($map as $mapped => $mapped_v){ foreach ($map as $mapped => $mapped_v){
if(isset($request[$mapped])){ if(isset($request[$mapped])){
if($mapped === 'metaquery'){ if($mapped === 'metaquery'){
$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 ) { foreach ( $meta_query as $mapped_meta => $meta_v ) {
$args[$mapped_v][$meta_v] = $request[$mapped][$mapped_meta]; 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') { } elseif ($mapped === 'datequery') {
foreach ($date_query as $date_meta => $date_v){ foreach ($date_query as $date_meta => $date_v){
$args[$mapped_v][$date_v] = $request[$mapped][$date_meta]; $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; return $args;
} }
protected function contains_array($array){
foreach ($array as $value){
if(is_array($value)){
return true;
}
}
return false;
}
} }
?> ?>

View File

@ -192,7 +192,7 @@ class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_REST_Controller {
$item_metadata->set_value( $value ); $item_metadata->set_value( $value );
if ( $item_metadata->validate() ) { 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 = $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); $prepared_item['field']['field_type_object'] = $this->prepare_item_for_response($field_updated->get_field()->get_field_type_object(), $request);

View File

@ -267,7 +267,6 @@ class Collections extends Repository {
} elseif(is_array($args)) { } elseif(is_array($args)) {
$args = array_merge([ $args = array_merge([
'posts_per_page' => -1, 'posts_per_page' => -1,
'post_status' => 'publish',
], $args); ], $args);
$args = $this->parse_fetch_args($args); $args = $this->parse_fetch_args($args);

View File

@ -139,5 +139,7 @@ class Item_Metadata extends Repository {
* *
* @return mixed * @return mixed
*/ */
public function update( $object, $new_values = null ) {} public function update( $object, $new_values = null ) {
return $this->insert($object);
}
} }

View File

@ -8,6 +8,7 @@ class TAINACAN_UnitTestCase extends \WP_UnitTestCase {
protected $tainacan_entity_factory; protected $tainacan_entity_factory;
protected $tainacan_field_factory; protected $tainacan_field_factory;
protected $tainacan_filter_factory; protected $tainacan_filter_factory;
protected $tainacan_item_metadata_factory;
protected $user_id; protected $user_id;
public function setUp(){ public function setUp(){

View File

@ -7,12 +7,187 @@ namespace Tainacan\Tests;
* **/ * **/
class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
public function test_collections_queries(){ public function test_queries(){
$this->tainacan_entity_factory->create_entity(
// Populate the database
$collectionA = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
[], [
'name' => 'A',
'description' => 'Collection A',
'status' => 'publish'
],
true 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);
} }
} }