2017-12-11 16:46:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
|
|
|
use Tainacan\Repositories;
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
$field = $this->tainacan_field_factory->create_field('text', '', true);
|
|
|
|
|
|
|
|
$metadata = json_encode(
|
|
|
|
array(
|
2017-12-12 11:59:50 +00:00
|
|
|
'name' => 'Moeda',
|
2017-12-11 16:46:26 +00:00
|
|
|
'description' => 'Descreve campo moeda.',
|
2017-12-12 11:59:50 +00:00
|
|
|
'field_type' => $field->get_primitive_type(),
|
2017-12-11 16:46:26 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'POST',
|
2017-12-18 12:52:45 +00:00
|
|
|
$this->namespace . '/metadata/collection/' . $collection->get_id()
|
2017-12-11 16:46:26 +00:00
|
|
|
);
|
|
|
|
$request->set_body($metadata);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
2017-12-19 16:24:30 +00:00
|
|
|
$metadata_added = $response->get_data();
|
2017-12-11 16:46:26 +00:00
|
|
|
|
|
|
|
$this->assertEquals('Moeda', $metadata_added['name']);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
$field = $this->tainacan_field_factory->create_field('text', '', true);
|
|
|
|
|
|
|
|
$metadata = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadata',
|
|
|
|
array(
|
|
|
|
'name' => 'Data',
|
|
|
|
'description' => 'Descreve valor do campo data.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish',
|
|
|
|
'field_type' => $field->get_primitive_type(),
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
|
|
|
|
$item_metadata->set_value('12/12/2017');
|
|
|
|
|
|
|
|
$item_metadata->validate();
|
|
|
|
$Tainacan_Item_Metadata->insert($item_metadata);
|
|
|
|
|
|
|
|
#################### Get metadata of collection ######################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET',
|
2017-12-18 12:52:45 +00:00
|
|
|
$this->namespace . '/metadata/collection/' . $collection->get_id()
|
2017-12-12 17:17:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2017-12-19 16:24:30 +00:00
|
|
|
$metadata = $data[0];
|
2017-12-12 17:17:55 +00:00
|
|
|
|
|
|
|
$this->assertEquals('Data', $metadata['name']);
|
|
|
|
|
|
|
|
################### Get metadata of item with value #######################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET',
|
2017-12-18 12:52:45 +00:00
|
|
|
$this->namespace . '/metadata/item/' . $item->get_id()
|
2017-12-12 17:17:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2017-12-19 16:24:30 +00:00
|
|
|
$item_metadata = $data[0];
|
|
|
|
$metadata = $item_metadata['metadata'];
|
2017-12-12 17:17:55 +00:00
|
|
|
|
|
|
|
$this->assertEquals('Data', $metadata['name']);
|
2017-12-13 15:07:01 +00:00
|
|
|
$this->assertEquals('12/12/2017', $item_metadata['value']);
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
$field = $this->tainacan_field_factory->create_field('text', '', true);
|
|
|
|
|
|
|
|
$metadata = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadata',
|
|
|
|
array(
|
|
|
|
'name' => 'Data',
|
|
|
|
'description' => 'Descreve o dado do campo data.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish',
|
|
|
|
'field_type' => $field->get_primitive_type(),
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$meta_values = json_encode(
|
|
|
|
array(
|
|
|
|
'metadata_id' => $metadata->get_id(),
|
|
|
|
'values' => '19/01/2018'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'PATCH',
|
|
|
|
$this->namespace . '/metadata/item/' . $item->get_id()
|
|
|
|
);
|
|
|
|
$request->set_body($meta_values);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$item_metadata_updated = $response->get_data();
|
|
|
|
|
|
|
|
$metadata_updated = $item_metadata_updated['metadata'];
|
|
|
|
|
|
|
|
$this->assertEquals($metadata->get_id(), $metadata_updated['id']);
|
|
|
|
|
|
|
|
$metav = get_post_meta($item->get_id(), $metadata_updated['id'], true);
|
|
|
|
|
|
|
|
$this->assertEquals('19/01/2018', $metav);
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|