2017-12-07 12:00:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
2018-01-19 22:39:32 +00:00
|
|
|
/**
|
|
|
|
* @group api
|
|
|
|
*/
|
2017-12-07 12:00:35 +00:00
|
|
|
class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
|
|
|
|
|
2017-12-07 12:46:31 +00:00
|
|
|
public function test_create_item_in_a_collection(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Javascript Frameworks',
|
2018-01-29 13:09:44 +00:00
|
|
|
'description' => 'The best framework to javascript',
|
2017-12-07 12:46:31 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item_json = json_encode([
|
|
|
|
'title' => 'Vue JS 2',
|
|
|
|
'description' => 'The Progressive JavasScript Framework'
|
|
|
|
]);
|
|
|
|
|
2018-01-25 13:47:09 +00:00
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
2017-12-07 12:46:31 +00:00
|
|
|
$request->set_body($item_json);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response->get_status());
|
|
|
|
|
2017-12-19 16:24:30 +00:00
|
|
|
$data = $response->get_data();
|
2017-12-07 12:46:31 +00:00
|
|
|
|
|
|
|
$this->assertEquals('Vue JS 2', $data['title']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_fetch_items_from_a_collection(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Agile',
|
2018-02-10 02:04:51 +00:00
|
|
|
'description' => 'Agile methods',
|
|
|
|
'status' => 'publish'
|
2017-12-07 12:46:31 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item1 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'Lean Startup',
|
2017-12-07 15:49:11 +00:00
|
|
|
'description' => 'Um processo ágil de criação de novos negócios.',
|
2018-01-29 13:09:44 +00:00
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
2017-12-07 12:46:31 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'SCRUM',
|
2017-12-07 15:49:11 +00:00
|
|
|
'description' => 'Um framework ágil para gerenciamento de tarefas.',
|
2018-01-29 13:09:44 +00:00
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
2017-12-07 12:46:31 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-25 13:47:09 +00:00
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
2017-12-07 12:46:31 +00:00
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
2017-12-19 16:24:30 +00:00
|
|
|
$data = $response->get_data();
|
2017-12-07 12:46:31 +00:00
|
|
|
|
2018-03-09 16:28:44 +00:00
|
|
|
$items_titles = [$data[0]['title'], $data[1]['title']];
|
2017-12-07 12:46:31 +00:00
|
|
|
|
2018-03-09 16:28:44 +00:00
|
|
|
$this->assertContains($item1->get_title(), $items_titles);
|
|
|
|
$this->assertContains($item2->get_title(), $items_titles);
|
2017-12-07 12:46:31 +00:00
|
|
|
}
|
2017-12-07 12:00:35 +00:00
|
|
|
|
2017-12-07 15:49:11 +00:00
|
|
|
public function test_delete_or_trash_item_from_a_collection(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
|
|
|
|
|
|
|
|
$item1 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'Lean Startup',
|
|
|
|
'description' => 'Um processo ágil de criação de novos negócios.',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// Move to trash
|
|
|
|
$delete_permanently = json_encode(['is_permanently' => false]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'DELETE',
|
2017-12-18 12:52:45 +00:00
|
|
|
$this->namespace . '/items/' . $item1->get_id()
|
2017-12-07 15:49:11 +00:00
|
|
|
);
|
|
|
|
$request->set_body($delete_permanently);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
|
2017-12-19 16:24:30 +00:00
|
|
|
$data = $response->get_data();
|
2017-12-07 15:49:11 +00:00
|
|
|
|
2017-12-08 14:53:55 +00:00
|
|
|
$this->assertEquals($item1->get_title(), $data['title']);
|
|
|
|
|
|
|
|
$post_meta = get_post_meta($item1->get_id(), '_wp_trash_meta_status', true);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($post_meta);
|
2017-12-07 15:49:11 +00:00
|
|
|
|
|
|
|
#######################################################################################
|
|
|
|
|
|
|
|
$item2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'SCRUM',
|
|
|
|
'description' => 'Um framework ágil para gerenciamento de tarefas.',
|
|
|
|
'collection' => $collection
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// Delete permanently
|
|
|
|
$delete_permanently = json_encode(['is_permanently' => true]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'DELETE',
|
2017-12-18 12:52:45 +00:00
|
|
|
$this->namespace . '/items/' . $item2->get_id()
|
2017-12-07 15:49:11 +00:00
|
|
|
);
|
|
|
|
$request->set_body($delete_permanently);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
|
2017-12-19 16:24:30 +00:00
|
|
|
$data = $response->get_data();
|
2017-12-07 15:49:11 +00:00
|
|
|
|
2017-12-08 14:53:55 +00:00
|
|
|
$this->assertEquals($item2->get_title(), $data['title']);
|
|
|
|
|
|
|
|
$no_post = get_post($item2->get_id());
|
|
|
|
|
|
|
|
$this->assertNull($no_post);
|
2017-12-07 15:49:11 +00:00
|
|
|
}
|
2018-01-16 12:47:29 +00:00
|
|
|
|
|
|
|
public function test_update_item(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
|
|
|
|
|
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'SCRUM e PMBOK',
|
|
|
|
'description' => 'Unidos no Gerenciamento de Projetos',
|
|
|
|
'collection' => $collection,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$new_attributes = json_encode([
|
|
|
|
'title' => 'SCRUM e XP',
|
|
|
|
'description' => 'Direto da trincheiras',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'PATCH', $this->namespace . '/items/' . $item->get_id()
|
|
|
|
);
|
|
|
|
|
|
|
|
$request->set_body($new_attributes);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertNotEquals($item->get_title(), $data['title']);
|
|
|
|
$this->assertEquals('SCRUM e XP', $data['title']);
|
|
|
|
}
|
2017-12-07 12:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|