add teste `test_create_compound_metadatum_API` #17

This commit is contained in:
vnmedeiros 2020-03-24 08:46:53 -03:00
parent c08b6b32b7
commit 85b7568d34
1 changed files with 222 additions and 0 deletions

View File

@ -0,0 +1,222 @@
<?php
namespace Tainacan\Tests;
/**
* @group api
*/
class TAINACAN_REST_Compound_Metadata_Controller extends TAINACAN_UnitApiTestCase {
function setUp() {
parent::setUp();
$this->collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection-1',
'status' => 'publish'
),
true
);
$this->item = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item-1',
'description' => 'description-1',
'collection' => $this->collection,
'status' => 'publish'
),
true
);
// $this->item2 = $this->tainacan_entity_factory->create_entity(
// 'item',
// array(
// 'title' => 'item-2',
// 'description' => 'description-2',
// 'collection' => $this->collection,
// 'status' => 'publish'
// ),
// true
// );
}
/**
* @group compound_metadatum
*/
public function test_create_compound_metadatum_API() {
$metadatum_compound = json_encode(
array(
'name' => 'quadrados',
'description' => 'Descrição de um quadrado.',
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Compound',
)
);
$URL = $this->namespace . '/collection/' . $this->collection->get_id() . '/metadata';
$request = new \WP_REST_Request('POST', $URL);
$request->set_body($metadatum_compound);
$response = $this->server->dispatch($request);
$metadatum_compound_added = $response->get_data();
$this->assertTrue(is_array($metadatum_compound_added) && array_key_exists('name', $metadatum_compound_added), sprintf('cannot create metadatum, response: %s', print_r($metadatum_compound_added, true)));
$this->assertEquals('quadrados', $metadatum_compound_added['name']);
$this->assertEquals($this->collection->get_id(), $metadatum_compound_added['collection_id']);
$metadatum_id = json_encode(
array(
'name' => 'id',
'description' => 'id',
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
'parent' => $metadatum_compound_added['id'],
'collection_key' => 'yes',
'required' => 'yes'
)
);
$metadatum_largura = json_encode(
array(
'name' => 'largura',
'description' => 'largura',
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
'parent' => $metadatum_compound_added['id']
)
);
$metadatum_altura = json_encode(
array(
'name' => 'altura',
'description' => 'altura',
'status' => 'publish',
'metadata_type' => 'Tainacan\Metadata_Types\Numeric',
'parent' => $metadatum_compound_added['id']
)
);
$request = new \WP_REST_Request('POST', $URL);
$request->set_body($metadatum_largura);
$response = $this->server->dispatch($request);
$metadatum_largura = $response->get_data();
$this->assertTrue(is_array($metadatum_largura) && array_key_exists('name', $metadatum_largura), sprintf('cannot create metadatum, response: %s', print_r($metadatum_largura, true)));
$this->assertEquals('largura', $metadatum_largura['name']);
$this->assertEquals($this->collection->get_id(), $metadatum_largura['collection_id']);
$request = new \WP_REST_Request('POST', $URL);
$request->set_body($metadatum_altura);
$response = $this->server->dispatch($request);
$metadatum_altura = $response->get_data();
$this->assertTrue(is_array($metadatum_altura) && array_key_exists('name', $metadatum_altura), sprintf('cannot create metadatum, response: %s', print_r($metadatum_altura, true)));
$this->assertEquals('altura', $metadatum_altura['name']);
$this->assertEquals($this->collection->get_id(), $metadatum_altura['collection_id']);
$request = new \WP_REST_Request('POST', $URL);
$request->set_body($metadatum_id);
$response = $this->server->dispatch($request);
$metadatum_id = $response->get_data();
$this->assertTrue(is_array($metadatum_id) && array_key_exists('name', $metadatum_id), sprintf('cannot create metadatum, response: %s', print_r($metadatum_id, true)));
$this->assertEquals('id', $metadatum_id['name']);
$this->assertEquals($this->collection->get_id(), $metadatum_id['collection_id']);
$insert_item_json = json_encode(
array(
'collection_id' => $this->collection->get_id(),
'status' => 'auto-draft',
'comment_status' => 'closed'
)
);
$URL = $this->namespace . '/collection/' . $this->collection->get_id() . '/items';
$request = new \WP_REST_Request('POST', $URL);
$request->set_body($insert_item_json);
$response = $this->server->dispatch($request);
$insert_item = $response->get_data();
$update_item_json = json_encode(
array(
'id' => $insert_item['id'],
'status' => "publish",
'comment_status' => 'closed'
)
);
$URL = $this->namespace . '/items/' . $insert_item['id'];
$request = new \WP_REST_Request('POST', $URL);
$request->set_body($update_item_json);
$response = $this->server->dispatch($request);
$insert_item_err = $response->get_data();
$this->assertEquals(400, $response->get_status());
$this->assertArrayHasKey($metadatum_id['id'], $insert_item_err['errors'][0]);
$item_metadatum_id = json_encode(
array(
'values' => ['1']
)
);
$URL = $this->namespace . '/item/' . $insert_item['id'] . '/metadata/' . $metadatum_id['id'];
$request = new \WP_REST_Request('PATCH', $URL);
$request->set_body($item_metadatum_id);
$response = $this->server->dispatch($request);
$item_metadatum_id = $response->get_data();
$this->assertEquals(200, $response->get_status());
$item_metadatum_largura = json_encode(
array(
'parent_meta_id' => $item_metadatum_id['parent_meta_id'],
'values' => ['10']
)
);
$URL = $this->namespace . '/item/' . $insert_item['id'] . '/metadata/' . $metadatum_largura['id'];
$request = new \WP_REST_Request('PATCH', $URL);
$request->set_body($item_metadatum_largura);
$response = $this->server->dispatch($request);
$item_metadatum_largura = $response->get_data();
$this->assertEquals(200, $response->get_status());
$item_metadatum_altura = json_encode(
array(
'parent_meta_id' => $item_metadatum_id['parent_meta_id'],
'values' => ['10']
)
);
$URL = $this->namespace . '/item/' . $insert_item['id'] . '/metadata/' . $metadatum_altura['id'];
$request = new \WP_REST_Request('PATCH', $URL);
$request->set_body($item_metadatum_altura);
$response = $this->server->dispatch($request);
$item_metadatum_altura = $response->get_data();
$this->assertEquals(200, $response->get_status());
$URL = $this->namespace . '/items/' . $insert_item['id'];
$request = new \WP_REST_Request('POST', $URL);
$request->set_body($update_item_json);
$response = $this->server->dispatch($request);
$insert_item = $response->get_data();
$this->assertEquals(200, $response->get_status());
$item_metadatum_id_duplicate = json_encode(
array(
'values' => ['1']
)
);
$URL = $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $metadatum_id['id'];
$request = new \WP_REST_Request('PATCH', $URL);
$request->set_body($item_metadatum_id_duplicate);
$response = $this->server->dispatch($request);
$item_metadatum_id_duplicate = $response->get_data();
$this->assertEquals(400, $response->get_status());
}
}
?>