2018-02-16 22:41:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group api_item_meta
|
|
|
|
*/
|
|
|
|
class TAINACAN_REST_Item_Metadata_Controller extends TAINACAN_UnitApiTestCase {
|
2018-03-07 13:48:09 +00:00
|
|
|
protected $item;
|
|
|
|
protected $collection;
|
|
|
|
protected $field;
|
|
|
|
|
|
|
|
protected function create_meta_requirements() {
|
2018-02-16 22:41:43 +00:00
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'testeItemMetadata',
|
|
|
|
'description' => 'No description',
|
|
|
|
),
|
2018-03-01 18:19:46 +00:00
|
|
|
true,
|
2018-02-16 22:41:43 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$type = $this->tainacan_field_factory->create_field('text');
|
|
|
|
|
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
|
|
|
array(
|
|
|
|
'name' => 'teste_metadado',
|
|
|
|
'description' => 'descricao',
|
|
|
|
'collection' => $collection,
|
2018-02-22 20:06:52 +00:00
|
|
|
'field_type' => $type,
|
|
|
|
'accept_suggestion' => true
|
2018-02-16 22:41:43 +00:00
|
|
|
),
|
2018-03-01 18:19:46 +00:00
|
|
|
true,
|
2018-02-16 22:41:43 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
2018-03-01 18:19:46 +00:00
|
|
|
'title' => 'item_teste_metadado',
|
2018-02-16 22:41:43 +00:00
|
|
|
'description' => 'adasdasdsa',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
2018-03-01 18:19:46 +00:00
|
|
|
true,
|
2018-02-16 22:41:43 +00:00
|
|
|
true
|
|
|
|
);
|
2018-03-07 13:48:09 +00:00
|
|
|
$this->collection = $collection;
|
|
|
|
$this->item = $item;
|
|
|
|
$this->field = $field;
|
|
|
|
return ['collection' => $collection, 'item' => $item, 'field' => $field];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_create_suggestion_item_metadata_in_a_collection(){
|
|
|
|
|
|
|
|
extract($this->create_meta_requirements());
|
2018-02-16 22:41:43 +00:00
|
|
|
|
|
|
|
$item__metadata_json = json_encode([
|
2018-03-01 18:19:46 +00:00
|
|
|
'values' => 'TestValues_metadado',
|
2018-02-16 22:41:43 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
|
|
|
|
$request->set_body($item__metadata_json);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2018-02-22 20:06:52 +00:00
|
|
|
$this->assertEquals($item->get_id() , $data['item']['id']);
|
2018-03-01 18:19:46 +00:00
|
|
|
$this->assertEquals('TestValues_metadado', $data['value']);
|
2018-02-16 22:41:43 +00:00
|
|
|
|
2018-03-01 18:19:46 +00:00
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
|
2018-02-16 22:41:43 +00:00
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
2018-03-01 18:19:46 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'TestValues_metadado', $data['value'] );
|
2018-02-22 20:06:52 +00:00
|
|
|
|
|
|
|
// Test Suggestion
|
|
|
|
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber' ));
|
|
|
|
wp_set_current_user($new_user);
|
|
|
|
|
|
|
|
$item__metadata_json = json_encode([
|
2018-03-01 18:19:46 +00:00
|
|
|
'values' => 'TestValuesSuggestion_metadado',
|
2018-02-22 20:06:52 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
|
|
|
|
$request->set_body($item__metadata_json);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'pending', $data['status'] );
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance();
|
2018-02-22 20:06:52 +00:00
|
|
|
$query = $Tainacan_Logs->fetch(['post_status' => 'pending']);
|
|
|
|
|
|
|
|
$log = false;
|
|
|
|
while ($query->have_posts()) {
|
|
|
|
$query->the_post();
|
|
|
|
$post = get_post();
|
|
|
|
$log = $Tainacan_Logs->get_entity_by_post($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pending = $log->get_value();
|
|
|
|
|
2018-04-09 14:32:20 +00:00
|
|
|
$this->assertEquals('TestValuesSuggestion_metadado', $pending->get_value());
|
2018-03-01 15:34:53 +00:00
|
|
|
|
|
|
|
wp_set_current_user($this->user_id);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/logs/' . $log->get_id() . '/approve' );
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
2018-03-01 15:45:31 +00:00
|
|
|
$this->assertEquals(200, $response->get_status());
|
2018-03-01 18:19:46 +00:00
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'TestValuesSuggestion_metadado', $data['value'] );
|
|
|
|
|
2018-02-16 22:41:43 +00:00
|
|
|
}
|
2018-03-07 13:48:09 +00:00
|
|
|
|
|
|
|
function test_create_anonymous_suggestion_item_metadata_in_a_collection() {
|
|
|
|
extract($this->create_meta_requirements());
|
|
|
|
|
|
|
|
$item__metadata_json = json_encode([
|
|
|
|
'values' => 'TestValues_metadado',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
|
|
|
|
$request->set_body($item__metadata_json);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
|
|
|
|
// Test Anonymous Suggestion
|
|
|
|
wp_logout();
|
|
|
|
wp_set_current_user(0);
|
|
|
|
|
|
|
|
$this->assertEquals(0, get_current_user_id());
|
|
|
|
|
|
|
|
$item__metadata_json = json_encode([
|
|
|
|
'values' => 'TestValuesAnonymousSuggestion_metadado',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
|
|
|
|
$request->set_body($item__metadata_json);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'pending', $data['status'] );
|
2018-04-11 14:18:55 +00:00
|
|
|
$Tainacan_Logs = \Tainacan\Repositories\Logs::get_instance();
|
2018-03-07 13:48:09 +00:00
|
|
|
$query = $Tainacan_Logs->fetch(['post_status' => 'pending']);
|
|
|
|
|
|
|
|
$log = false;
|
|
|
|
while ($query->have_posts()) {
|
|
|
|
$query->the_post();
|
|
|
|
$post = get_post();
|
|
|
|
$log = $Tainacan_Logs->get_entity_by_post($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pending = $log->get_value();
|
|
|
|
|
2018-04-09 14:32:20 +00:00
|
|
|
$this->assertEquals('TestValuesAnonymousSuggestion_metadado', $pending->get_value());
|
2018-03-07 13:48:09 +00:00
|
|
|
|
|
|
|
wp_set_current_user($this->user_id);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/logs/' . $log->get_id() . '/approve' );
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'TestValuesAnonymousSuggestion_metadado', $data['value'] );
|
|
|
|
|
|
|
|
}
|
2018-02-16 22:41:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|