2017-12-11 16:46:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
|
|
|
use Tainacan\Repositories;
|
|
|
|
|
2018-01-19 22:39:32 +00:00
|
|
|
/**
|
|
|
|
* @group api
|
|
|
|
*/
|
2017-12-11 16:46:26 +00:00
|
|
|
class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
|
|
|
|
|
|
|
public function test_insert_metadata() {
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
|
|
|
|
|
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
2017-12-12 11:59:50 +00:00
|
|
|
'title' => 'No name',
|
2017-12-11 16:46:26 +00:00
|
|
|
'description' => 'No description',
|
2017-12-12 11:59:50 +00:00
|
|
|
'collection' => $collection
|
2017-12-11 16:46:26 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = json_encode(
|
2017-12-11 16:46:26 +00:00
|
|
|
array(
|
2017-12-12 11:59:50 +00:00
|
|
|
'name' => 'Moeda',
|
2017-12-11 16:46:26 +00:00
|
|
|
'description' => 'Descreve campo moeda.',
|
2018-02-22 20:19:18 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2017-12-11 16:46:26 +00:00
|
|
|
)
|
|
|
|
);
|
2018-01-30 14:01:24 +00:00
|
|
|
|
2017-12-11 16:46:26 +00:00
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'POST',
|
2018-01-31 15:50:10 +00:00
|
|
|
$this->namespace . '/collection/' . $collection->get_id() . '/fields'
|
2017-12-11 16:46:26 +00:00
|
|
|
);
|
2018-01-31 14:47:59 +00:00
|
|
|
$request->set_body($field);
|
2017-12-11 16:46:26 +00:00
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field_added = $response->get_data();
|
|
|
|
$this->assertTrue(is_array($field_added) && array_key_exists('name', $field_added), sprintf('cannot create field, response: %s', print_r($field_added, true)));
|
|
|
|
$this->assertEquals('Moeda', $field_added['name']);
|
2018-01-30 14:01:24 +00:00
|
|
|
|
2017-12-11 16:46:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:17:55 +00:00
|
|
|
|
|
|
|
public function test_get_item_and_collection_metadata(){
|
|
|
|
global $Tainacan_Item_Metadata;
|
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
2017-12-18 12:52:45 +00:00
|
|
|
'name' => 'Statement',
|
|
|
|
'description' => 'No Statement'
|
2017-12-12 17:17:55 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'No name',
|
|
|
|
'description' => 'No description',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2017-12-12 17:17:55 +00:00
|
|
|
array(
|
|
|
|
'name' => 'Data',
|
|
|
|
'description' => 'Descreve valor do campo data.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish',
|
2018-02-10 02:04:51 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2017-12-12 17:17:55 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $field);
|
2017-12-12 17:17:55 +00:00
|
|
|
$item_metadata->set_value('12/12/2017');
|
|
|
|
|
|
|
|
$item_metadata->validate();
|
|
|
|
$Tainacan_Item_Metadata->insert($item_metadata);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
#################### Get field of collection ######################
|
2017-12-12 17:17:55 +00:00
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET',
|
2018-01-31 15:50:10 +00:00
|
|
|
$this->namespace . '/collection/' . $collection->get_id() . '/fields'
|
2017-12-12 17:17:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2018-02-16 19:16:45 +00:00
|
|
|
$fields_names = array_map(function($field) {return $field['name'];}, $data);
|
2017-12-12 17:17:55 +00:00
|
|
|
|
2018-02-16 19:16:45 +00:00
|
|
|
$this->assertContains('Data', $fields_names);
|
2017-12-12 17:17:55 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
################### Get field of item with value #######################
|
2017-12-12 17:17:55 +00:00
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET',
|
2018-01-31 15:50:10 +00:00
|
|
|
$this->namespace . '/item/' . $item->get_id() . '/metadata'
|
2017-12-12 17:17:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
2018-01-31 14:47:59 +00:00
|
|
|
$this->assertTrue(is_array($data) && array_key_exists(0, $data), sprintf('cannot read field, response: %s', print_r($data, true)));
|
2017-12-12 17:17:55 +00:00
|
|
|
|
2018-02-19 16:29:43 +00:00
|
|
|
|
|
|
|
$fields_names = array_map(function($item_metadata) {return $item_metadata['field']['name'];}, $data);
|
|
|
|
$values = array_map(function($item_metadata) {return $item_metadata['value'];}, $data);
|
|
|
|
|
|
|
|
$this->assertContains('Data', $fields_names);
|
|
|
|
$this->assertContains('12/12/2017', $values);
|
2017-12-12 17:17:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 14:55:39 +00:00
|
|
|
public function test_update_metadata(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Statement',
|
|
|
|
'description' => 'No Statement'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'No name',
|
|
|
|
'description' => 'No description',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2018-01-19 14:55:39 +00:00
|
|
|
array(
|
|
|
|
'name' => 'Data',
|
|
|
|
'description' => 'Descreve o dado do campo data.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish',
|
2018-02-10 02:04:51 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2018-02-14 18:52:50 +00:00
|
|
|
'multiple' => 'yes'
|
2018-01-19 14:55:39 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$meta_values = json_encode(
|
|
|
|
array(
|
2018-02-14 18:52:50 +00:00
|
|
|
'values' => array(
|
2018-02-15 13:27:45 +00:00
|
|
|
'19/01/2018',
|
|
|
|
'19/02/2018',
|
2018-02-14 18:52:50 +00:00
|
|
|
)
|
2018-01-19 14:55:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'PATCH',
|
2018-01-31 15:50:10 +00:00
|
|
|
$this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id()
|
2018-01-19 14:55:39 +00:00
|
|
|
);
|
|
|
|
$request->set_body($meta_values);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$item_metadata_updated = $response->get_data();
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field_updated = $item_metadata_updated['field'];
|
2018-01-19 14:55:39 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$this->assertEquals($field->get_id(), $field_updated['id']);
|
2018-01-19 14:55:39 +00:00
|
|
|
|
2018-02-15 11:24:44 +00:00
|
|
|
$this->assertEquals('19/01/2018', $item_metadata_updated['value'][0]);
|
|
|
|
$this->assertEquals('19/02/2018', $item_metadata_updated['value'][1]);
|
2018-01-19 16:56:36 +00:00
|
|
|
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
#### UPDATE FIELD IN COLLECTION ####
|
2018-01-30 14:01:24 +00:00
|
|
|
|
2018-01-19 16:56:36 +00:00
|
|
|
$values = json_encode([
|
|
|
|
'values' => [
|
|
|
|
'name' => 'Dia/Mês/Ano',
|
|
|
|
'description' => 'Continua descrevendo o dado do campo.'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'PATCH',
|
2018-01-31 15:50:10 +00:00
|
|
|
$this->namespace . '/collection/' . $collection->get_id() . '/fields/' . $field->get_id()
|
2018-01-19 16:56:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$request->set_body($values);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
2018-01-30 14:01:24 +00:00
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$this->assertEquals($field->get_id(), $data['id']);
|
2018-01-22 11:48:51 +00:00
|
|
|
$this->assertEquals('Dia/Mês/Ano', $data['name']);
|
2018-01-19 16:56:36 +00:00
|
|
|
|
|
|
|
// Mantém-se o valor antigo no item
|
|
|
|
$metav = get_post_meta($item->get_id(), $data['id'], true);
|
|
|
|
|
|
|
|
$this->assertEquals('19/01/2018', $metav);
|
2018-01-30 14:01:24 +00:00
|
|
|
|
2018-01-19 14:55:39 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 14:59:13 +00:00
|
|
|
public function test_trash_field(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Statement',
|
|
|
|
'description' => 'No Statement'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
|
|
|
array(
|
|
|
|
'name' => 'Field Statement',
|
|
|
|
'description' => 'No Statement',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish',
|
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
|
|
|
'multiple' => 'yes'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$trash_field_request = new \WP_REST_Request(
|
|
|
|
'DELETE',
|
|
|
|
$this->namespace . '/collection/'. $collection->get_id() . '/fields/' . $field->get_id()
|
|
|
|
);
|
|
|
|
|
|
|
|
$trash_field_response = $this->server->dispatch($trash_field_request);
|
|
|
|
$data1 = $trash_field_response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals($field->get_id(), $data1['id']);
|
|
|
|
|
|
|
|
$field_trashed = get_post($data1['id']);
|
|
|
|
$this->assertEquals('trash', $field_trashed->post_status);
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|