tainacan/tests/test-api-metadata.php

152 lines
3.7 KiB
PHP
Raw Normal View History

<?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',
'description' => 'No description',
2017-12-12 11:59:50 +00:00
'collection' => $collection
),
true
);
$field = $this->tainacan_field_factory->create_field('text', '', true);
$metadata = json_encode(
array(
2017-12-12 11:59:50 +00:00
'name' => 'Moeda',
'description' => 'Descreve campo moeda.',
2017-12-12 11:59:50 +00:00
'field_type' => $field->get_primitive_type(),
)
);
$request = new \WP_REST_Request(
'POST',
$this->namespaced_route . '/metadata/collection/' . $collection->get_id()
);
$request->set_body($metadata);
$response = $this->server->dispatch($request);
$metadata_added = json_decode($response->get_data(), true);
$this->assertEquals('Moeda', $metadata_added['name']);
#################### Add value to metadata of item ##########################
$meta_values = json_encode(
array(
'metadata_id' => $metadata_added['id'],
'values' => 'Valorado'
)
);
$request = new \WP_REST_Request(
'POST',
$this->namespaced_route . '/metadata/item/' . $item->get_id()
);
$request->set_body($meta_values);
$response = $this->server->dispatch($request);
2017-12-13 15:07:01 +00:00
$item_metadata_updated = json_decode($response->get_data(), true);
$metadata = json_decode($item_metadata_updated['metadata'], true);
2017-12-13 15:07:01 +00:00
$this->assertEquals($metadata_added['id'], $metadata['id']);
2017-12-13 15:07:01 +00:00
$metav = get_post_meta($item->get_id(), $metadata['id'], true);
$this->assertEquals('Valorado', $metav);
}
public function test_get_item_and_collection_metadata(){
global $Tainacan_Item_Metadata;
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => '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 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',
$this->namespaced_route . '/metadata/collection/' . $collection->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertContainsOnly('string', $data);
$metadata = json_decode($data[0], true);
$this->assertEquals('Data', $metadata['name']);
################### Get metadata of item with value #######################
$request = new \WP_REST_Request(
'GET',
$this->namespaced_route . '/metadata/item/' . $item->get_id()
);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertContainsOnly('string', $data);
2017-12-13 15:07:01 +00:00
$item_metadata = json_decode($data[0], true);
$metadata = json_decode($item_metadata['metadata'], true);
$this->assertEquals('Data', $metadata['name']);
2017-12-13 15:07:01 +00:00
$this->assertEquals('12/12/2017', $item_metadata['value']);
}
}
?>