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());
|
2019-02-22 17:26:45 +00:00
|
|
|
$data = $response->get_data()['items'];
|
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(){
|
2019-06-28 01:53:14 +00:00
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Agile',
|
|
|
|
'description' => 'Agile methods',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
2017-12-07 15:49:11 +00:00
|
|
|
$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
|
2018-03-23 13:03:39 +00:00
|
|
|
$delete_permanently = ['permanently' => false];
|
2017-12-07 15:49:11 +00:00
|
|
|
|
|
|
|
$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
|
|
|
);
|
2018-03-23 13:03:39 +00:00
|
|
|
$request->set_query_params($delete_permanently);
|
2017-12-07 15:49:11 +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 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
|
2018-03-23 13:03:39 +00:00
|
|
|
$delete_permanently = ['permanently' => true];
|
2017-12-07 15:49:11 +00:00
|
|
|
|
|
|
|
$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
|
|
|
);
|
2018-03-23 13:03:39 +00:00
|
|
|
$request->set_query_params($delete_permanently);
|
2017-12-07 15:49:11 +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 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(){
|
2019-09-13 22:25:42 +00:00
|
|
|
$collection = $this->tainacan_entity_factory->create_entity('collection', array(
|
|
|
|
'name' => 'Agile',
|
|
|
|
'description' => 'Agile methods',
|
|
|
|
'status' => 'publish'
|
|
|
|
), true);
|
2018-01-16 12:47:29 +00:00
|
|
|
|
|
|
|
$item = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'SCRUM e PMBOK',
|
|
|
|
'description' => 'Unidos no Gerenciamento de Projetos',
|
|
|
|
'collection' => $collection,
|
2019-09-13 22:25:42 +00:00
|
|
|
'status' => 'publish'
|
2018-01-16 12:47:29 +00:00
|
|
|
),
|
|
|
|
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']);
|
|
|
|
}
|
2018-06-15 12:35:22 +00:00
|
|
|
|
2018-08-07 12:43:17 +00:00
|
|
|
public function test_get_items_with_metadata_not_filled(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Agile',
|
|
|
|
'description' => 'Agile methods',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$metadatum = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadatum',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item1 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'Lean Startup',
|
|
|
|
'description' => 'Um processo ágil para validação de ideias.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'SCRUM',
|
|
|
|
'description' => 'Um framework ágil para gerenciamento de produto.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$itemMetaRepo = \Tainacan\Repositories\Item_Metadata::get_instance();
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $metadatum);
|
|
|
|
|
|
|
|
// Fills item metadata only for item 1
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'metaquery' => [
|
|
|
|
'key' => $metadatum->get_id(),
|
|
|
|
'compare' => 'NOT EXISTS'
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET', $this->namespace . '/collection/'. $collection->get_id() .'/items'
|
|
|
|
);
|
|
|
|
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
2019-02-22 17:26:45 +00:00
|
|
|
$data = $response->get_data()['items'];
|
2018-08-07 12:43:17 +00:00
|
|
|
|
|
|
|
$this->assertCount(1, $data);
|
|
|
|
$this->assertEquals('SCRUM', $data[0]['title']);
|
|
|
|
$this->assertEquals($item2->get_id(), $data[0]['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_fetch_only() {
|
2018-06-15 12:35:22 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Agile',
|
|
|
|
'description' => 'Agile methods',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$private_meta = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'private_meta',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
'status' => 'private'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$public_meta = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'public_meta',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$discarded = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'discarded',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
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,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$item2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'SCRUM',
|
|
|
|
'description' => 'Um framework ágil para gerenciamento de tarefas.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$itemMetaRepo = \Tainacan\Repositories\Item_Metadata::get_instance();
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $public_meta);
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $private_meta);
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $discarded);
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item2, $public_meta);
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item2, $private_meta);
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item2, $discarded);
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'fetch_only' => [
|
|
|
|
'meta' => [
|
|
|
|
$public_meta->get_id(),
|
|
|
|
$private_meta->get_id()
|
|
|
|
]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// First without fetch only
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET', $this->namespace . '/items'
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch($request);
|
2019-02-22 17:26:45 +00:00
|
|
|
$data = $response->get_data()['items'];
|
2018-06-15 12:35:22 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 2, sizeof($data) );
|
|
|
|
$this->assertEquals( 5, sizeof($data[0]['metadata']) );
|
|
|
|
|
|
|
|
// Fetch only as admin
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET', $this->namespace . '/items'
|
|
|
|
);
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
2019-02-22 17:26:45 +00:00
|
|
|
$data = $response->get_data()['items'];
|
2018-06-15 12:35:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 2, sizeof($data) );
|
|
|
|
$this->assertEquals( 2, sizeof($data[0]['metadata']) );
|
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber' ));
|
|
|
|
wp_set_current_user($new_user);
|
|
|
|
|
|
|
|
// Fetch only as subscriber
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET', $this->namespace . '/items'
|
|
|
|
);
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
2019-02-22 17:26:45 +00:00
|
|
|
$data = $response->get_data()['items'];
|
2018-06-15 12:35:22 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 2, sizeof($data) );
|
|
|
|
$this->assertEquals( 1, sizeof($data[0]['metadata']) );
|
|
|
|
$this->assertEquals( 'public_meta', $data[0]['metadata']['public_meta']['name'] );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2019-03-08 20:48:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group api_item_author
|
|
|
|
*/
|
2019-03-13 00:25:04 +00:00
|
|
|
public function test_create_item_as_auhor()
|
|
|
|
{
|
2019-03-08 22:49:59 +00:00
|
|
|
|
2019-03-13 00:25:04 +00:00
|
|
|
//create user as tainacan author
|
2019-03-08 20:48:59 +00:00
|
|
|
|
2019-03-13 19:01:09 +00:00
|
|
|
$new_user = $this->factory()->user->create(array('role' => 'tainacan-author'));
|
2019-03-08 20:48:59 +00:00
|
|
|
//$new_user = $this->factory()->user->create(array( 'role' => 'administrator' ));
|
|
|
|
wp_set_current_user($new_user);
|
|
|
|
$user_id = get_current_user_id();
|
|
|
|
$this->assertEquals($new_user, $user_id);
|
|
|
|
|
2019-03-08 22:49:59 +00:00
|
|
|
// create collection as auto-draft
|
|
|
|
|
|
|
|
$collection_JSON = json_encode([
|
2019-03-13 00:25:04 +00:00
|
|
|
'name' => 'TesteJsonAdd',
|
|
|
|
'description' => 'Teste JSON',
|
2019-03-08 22:49:59 +00:00
|
|
|
"status" => "auto-draft"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/collections');
|
|
|
|
$request->set_body($collection_JSON);
|
|
|
|
|
2019-03-13 00:25:04 +00:00
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
$this->assertEquals(201, $response->get_status(), sprintf('response: %s', print_r($response, true)));
|
2019-03-08 22:49:59 +00:00
|
|
|
|
|
|
|
$collection = $response->get_data();
|
|
|
|
$id = $collection['id'];
|
|
|
|
|
|
|
|
// publish collection
|
|
|
|
|
|
|
|
$new_values = json_encode([
|
|
|
|
"status" => "publish"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'PATCH', $this->namespace . '/collections/' . $id
|
|
|
|
);
|
|
|
|
|
|
|
|
$request->set_body($new_values);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals('publish', $data['status']);
|
|
|
|
|
|
|
|
// try create item in own collection
|
|
|
|
|
2019-03-08 20:48:59 +00:00
|
|
|
$item_json = json_encode([
|
2019-03-13 00:25:04 +00:00
|
|
|
'title' => 'SCRUM',
|
2019-03-08 22:49:59 +00:00
|
|
|
'description' => 'Um framework ágil para gerenciamento de produto.',
|
2019-03-08 20:48:59 +00:00
|
|
|
"status" => "auto-draft"
|
|
|
|
]);
|
|
|
|
|
2019-03-13 00:25:04 +00:00
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $id . '/items');
|
2019-03-08 20:48:59 +00:00
|
|
|
$request->set_body($item_json);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
2019-03-13 00:25:04 +00:00
|
|
|
//$this->assertEquals(403, $response->get_status());
|
|
|
|
$this->assertEquals(201, $response->get_status());
|
2019-03-08 20:48:59 +00:00
|
|
|
}
|
2019-07-18 15:22:40 +00:00
|
|
|
|
2019-07-18 20:57:16 +00:00
|
|
|
/**
|
|
|
|
* @group duplicate
|
|
|
|
*/
|
2019-07-18 15:22:40 +00:00
|
|
|
function test_duplicate() {
|
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Agile',
|
|
|
|
'description' => 'Agile methods',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$public_meta = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'public_meta',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$multiple_meta = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'public_meta',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
'multiple' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'taxonomy_public',
|
|
|
|
'description' => 'taxonomy_public',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-07-18 20:57:16 +00:00
|
|
|
$taxonomy2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'taxonomy_public2',
|
|
|
|
'description' => 'taxonomy_public2',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-07-18 15:22:40 +00:00
|
|
|
$tax_meta = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadata-public',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
|
|
|
|
'metadata_type_options' => [
|
|
|
|
'allow_new_terms' => 'yes',
|
|
|
|
'taxonomy_id' => $taxonomy->get_id()
|
|
|
|
],
|
|
|
|
'multiple' => 'yes'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-07-18 20:57:16 +00:00
|
|
|
$tax_meta_single = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadata-public-single',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
|
|
|
|
'metadata_type_options' => [
|
|
|
|
'allow_new_terms' => 'yes',
|
|
|
|
'taxonomy_id' => $taxonomy2->get_id()
|
|
|
|
],
|
|
|
|
'multiple' => 'no'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2019-07-18 15:22:40 +00:00
|
|
|
$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,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$itemMetaRepo = \Tainacan\Repositories\Item_Metadata::get_instance();
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $public_meta);
|
|
|
|
$newMeta->set_value('test');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $multiple_meta);
|
|
|
|
$newMeta->set_value(['test1', 'test2']);
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $tax_meta);
|
|
|
|
$newMeta->set_value(['blue', 'red']);
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
2019-07-18 20:57:16 +00:00
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $tax_meta_single);
|
|
|
|
$newMeta->set_value('test term');
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
2019-07-18 15:22:40 +00:00
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/items/' . $item1->get_id() . '/duplicate');
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response->get_status());
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2019-07-22 18:47:00 +00:00
|
|
|
$this->assertEquals('Lean Startup', $data['items'][0]['title']);
|
2019-07-18 15:22:40 +00:00
|
|
|
|
|
|
|
$metadata_1 = $item1->get_metadata();
|
|
|
|
|
2019-07-22 18:47:00 +00:00
|
|
|
$duplicated = \Tainacan\Repositories\Items::get_instance()->fetch( (int) $data['items'][0]['id'] );
|
2019-07-18 15:22:40 +00:00
|
|
|
|
|
|
|
$metadata_2 = $duplicated->get_metadata();
|
|
|
|
|
|
|
|
foreach( $metadata_1 as $k => $m ) {
|
|
|
|
$this->assertEquals( $m->get_value(), $metadata_2[$k]->get_value() );
|
|
|
|
}
|
|
|
|
|
2019-07-22 18:47:00 +00:00
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/items/' . $item1->get_id() . '/duplicate');
|
|
|
|
|
|
|
|
$params = json_encode([
|
|
|
|
'copies' => 4,
|
|
|
|
'status' => 'publish'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request->set_body($params);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals(4, count($data['items']));
|
|
|
|
|
|
|
|
foreach ( $data['items'] as $created_item ) {
|
|
|
|
$duplicated = \Tainacan\Repositories\Items::get_instance()->fetch( (int) $created_item['id'] );
|
|
|
|
|
|
|
|
$this->assertEquals('publish', $created_item['status']);
|
|
|
|
|
|
|
|
$metadata_2 = $duplicated->get_metadata();
|
|
|
|
|
|
|
|
foreach( $metadata_1 as $k => $m ) {
|
|
|
|
$this->assertEquals( $m->get_value(), $metadata_2[$k]->get_value() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a required metadata
|
|
|
|
$required_meta = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'required_meta',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
|
|
|
'multiple' => 'no',
|
|
|
|
'required' => 'yes',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// $metas = \Tainacan\Repositories\Metadata::get_instance()->fetch_by_collection($collection, [], 'OBJECT');
|
|
|
|
//
|
|
|
|
// foreach ($metas as $m) {
|
|
|
|
// var_dump($m->get_name());
|
|
|
|
// }
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/items/' . $item1->get_id() . '/duplicate');
|
|
|
|
|
|
|
|
$params = json_encode([
|
|
|
|
'copies' => 3,
|
|
|
|
'status' => 'publish'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request->set_body($params);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals(3, count($data['items']));
|
|
|
|
|
|
|
|
foreach ( $data['items'] as $created_item ) {
|
|
|
|
$duplicated = \Tainacan\Repositories\Items::get_instance()->fetch( (int) $created_item['id'] );
|
|
|
|
|
|
|
|
// item does not validate because required meta is empty and stays draft
|
|
|
|
$this->assertEquals('draft', $duplicated->get_status());
|
|
|
|
$this->assertEquals('draft', $created_item['status']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-29 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @group tax_query
|
|
|
|
*/
|
|
|
|
function test_support_tax_query_like() {
|
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Agile',
|
|
|
|
'description' => 'Agile methods',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'taxonomy',
|
|
|
|
array(
|
|
|
|
'name' => 'taxonomy_public',
|
|
|
|
'description' => 'taxonomy_public',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$tax_meta = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'metadatum',
|
|
|
|
array(
|
|
|
|
'name' => 'metadata-public',
|
|
|
|
'status' => 'publish',
|
|
|
|
'collection' => $collection,
|
|
|
|
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
|
|
|
|
'metadata_type_options' => [
|
|
|
|
'allow_new_terms' => 'yes',
|
|
|
|
'taxonomy_id' => $taxonomy->get_id()
|
|
|
|
],
|
|
|
|
'multiple' => 'yes'
|
|
|
|
),
|
|
|
|
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,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$itemMetaRepo = \Tainacan\Repositories\Item_Metadata::get_instance();
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $tax_meta);
|
|
|
|
$newMeta->set_value(['blue', 'mellon']);
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
$item2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'XXLean Startup',
|
|
|
|
'description' => 'Um processo ágil de criação de novos negócios.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item2, $tax_meta);
|
|
|
|
$newMeta->set_value(['red', 'watermellon']);
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
$item3 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'item',
|
|
|
|
array(
|
|
|
|
'title' => 'XXLean Startup',
|
|
|
|
'description' => 'Um processo ágil de criação de novos negócios.',
|
|
|
|
'collection' => $collection,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item3, $tax_meta);
|
|
|
|
$newMeta->set_value(['red', 'blue']);
|
|
|
|
$newMeta->validate();
|
|
|
|
$itemMetaRepo->insert($newMeta);
|
|
|
|
|
|
|
|
|
|
|
|
####################################################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'taxquery' => [
|
|
|
|
[
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'operator' => 'LIKE',
|
|
|
|
'terms' => 'mellon'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data()['items'];
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($data));
|
|
|
|
$ids = array_map(function($e) { return $e['id']; }, $data);
|
|
|
|
$this->assertContains($item1->get_id(), $ids);
|
|
|
|
$this->assertContains($item2->get_id(), $ids);
|
|
|
|
|
|
|
|
####################################################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'taxquery' => [
|
|
|
|
[
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'operator' => 'LIKE',
|
|
|
|
'terms' => 'red'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data()['items'];
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($data));
|
|
|
|
$ids = array_map(function($e) { return $e['id']; }, $data);
|
|
|
|
$this->assertContains($item2->get_id(), $ids);
|
|
|
|
$this->assertContains($item3->get_id(), $ids);
|
|
|
|
|
|
|
|
####################################################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'taxquery' => [
|
|
|
|
[
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'operator' => 'LIKE',
|
|
|
|
'terms' => 'water'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data()['items'];
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($data));
|
|
|
|
$ids = array_map(function($e) { return $e['id']; }, $data);
|
|
|
|
$this->assertContains($item2->get_id(), $ids);
|
|
|
|
|
2019-07-30 18:35:23 +00:00
|
|
|
####################################################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'taxquery' => [
|
|
|
|
[
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'operator' => 'NOT LIKE',
|
|
|
|
'terms' => 'mellon'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data()['items'];
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($data));
|
|
|
|
$ids = array_map(function($e) { return $e['id']; }, $data);
|
|
|
|
$this->assertContains($item3->get_id(), $ids);
|
|
|
|
|
|
|
|
####################################################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'taxquery' => [
|
|
|
|
[
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'operator' => 'NOT LIKE',
|
|
|
|
'terms' => '__does_not_exists'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data()['items'];
|
|
|
|
|
|
|
|
$this->assertEquals(3, count($data));
|
|
|
|
|
|
|
|
####################################################
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
|
|
|
|
|
|
|
|
$attributes = [
|
|
|
|
'taxquery' => [
|
|
|
|
[
|
|
|
|
'taxonomy' => $taxonomy->get_db_identifier(),
|
|
|
|
'operator' => 'LIKE',
|
|
|
|
'terms' => '__does_not_exists'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$request->set_query_params($attributes);
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->get_status());
|
|
|
|
$data = $response->get_data()['items'];
|
|
|
|
|
|
|
|
$this->assertEquals(0, count($data));
|
|
|
|
|
2019-07-18 15:22:40 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 12:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|