2018-01-10 15:56:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
2018-01-19 22:39:32 +00:00
|
|
|
/**
|
|
|
|
* @group api
|
|
|
|
*/
|
2018-01-10 15:56:55 +00:00
|
|
|
class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
|
|
|
|
|
|
|
|
public function test_create_filter(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Collection filtered',
|
|
|
|
'description' => 'Is filtered'
|
|
|
|
),
|
2018-01-31 13:06:46 +00:00
|
|
|
true,
|
2018-01-10 15:56:55 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2018-01-10 15:56:55 +00:00
|
|
|
array(
|
2018-01-31 13:06:46 +00:00
|
|
|
'name' => 'Metadata filtered',
|
|
|
|
'description' => 'Is filtered',
|
|
|
|
'collection_id' => $collection->get_id(),
|
2018-02-22 20:19:18 +00:00
|
|
|
'field_type' => 'Tainacan\Field_Types\Text',
|
2018-01-31 13:06:46 +00:00
|
|
|
),
|
|
|
|
true,
|
|
|
|
true
|
2018-01-10 15:56:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$request_body = json_encode(
|
|
|
|
array(
|
2018-03-01 19:32:42 +00:00
|
|
|
'filter_type' => 'custom_interval',
|
2018-01-10 15:56:55 +00:00
|
|
|
'filter' => [
|
|
|
|
'name' => 'Filter name',
|
2018-03-01 19:32:42 +00:00
|
|
|
'description' => 'This is CUSTOM INTERVAL!',
|
2018-01-10 15:56:55 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-02-22 16:12:06 +00:00
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/collection/' . $collection->get_id() . '/field/' . $field->get_id(). '/filters');
|
2018-01-10 15:56:55 +00:00
|
|
|
|
|
|
|
$request->set_body($request_body);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
2018-01-29 20:18:40 +00:00
|
|
|
$this->assertTrue(is_array($data) && array_key_exists('filter_type', $data), sprintf('cannot create a range, response: %s', print_r($data, true)));
|
2018-03-01 19:32:42 +00:00
|
|
|
$this->assertEquals('Tainacan\Filter_Types\Custom_Interval', $data['filter_type']);
|
2018-01-10 15:56:55 +00:00
|
|
|
$this->assertEquals('Filter name', $data['name']);
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:17:52 +00:00
|
|
|
public function test_delete_or_trash_filter(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Collection filtered',
|
2018-01-15 11:15:21 +00:00
|
|
|
'description' => 'Is filtered',
|
2018-01-12 16:17:52 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2018-01-12 16:17:52 +00:00
|
|
|
array(
|
2018-01-31 14:47:59 +00:00
|
|
|
'name' => 'Field filtered',
|
2018-01-15 11:15:21 +00:00
|
|
|
'description' => 'Is filtered',
|
2018-01-12 16:17:52 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-03-01 19:32:42 +00:00
|
|
|
$filter_type = $this->tainacan_filter_factory->create_filter('custom_interval');
|
2018-01-12 16:17:52 +00:00
|
|
|
|
|
|
|
$filter = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
|
|
|
'name' => 'filtro',
|
|
|
|
'collection' => $collection,
|
|
|
|
'description' => 'descricao',
|
2018-01-31 14:47:59 +00:00
|
|
|
'field' => $field,
|
2018-01-15 11:15:21 +00:00
|
|
|
'filter_type' => $filter_type,
|
2018-01-12 16:17:52 +00:00
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$is_permanently = json_encode([
|
2018-01-15 11:15:21 +00:00
|
|
|
'is_permanently' => false
|
2018-01-12 16:17:52 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'DELETE', $this->namespace . '/filters/' . $filter->get_id());
|
|
|
|
|
|
|
|
$request->set_body($is_permanently);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals('filtro', $data['name']);
|
|
|
|
|
|
|
|
$filter_status = get_post($filter->get_id())->post_status;
|
|
|
|
|
|
|
|
$this->assertEquals('trash', $filter_status);
|
|
|
|
|
2018-01-15 11:15:21 +00:00
|
|
|
##### DELETE #####
|
2018-01-12 16:17:52 +00:00
|
|
|
|
|
|
|
$is_permanently = json_encode([
|
2018-01-15 11:15:21 +00:00
|
|
|
'is_permanently' => true
|
2018-01-12 16:17:52 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'DELETE', $this->namespace . '/filters/' . $filter->get_id());
|
|
|
|
|
|
|
|
$request->set_body($is_permanently);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals('filtro', $data['name']);
|
|
|
|
$this->assertNull(get_post($filter->get_id()));
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:41:24 +00:00
|
|
|
public function test_update_filter(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Collection filtered',
|
|
|
|
'description' => 'Is filtered',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2018-01-16 13:41:24 +00:00
|
|
|
array(
|
2018-01-31 14:47:59 +00:00
|
|
|
'name' => 'Field filtered',
|
2018-01-16 13:41:24 +00:00
|
|
|
'description' => 'Is filtered',
|
2018-01-17 17:48:44 +00:00
|
|
|
'collection_id' => $collection->get_id()
|
2018-01-16 13:41:24 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-03-01 19:32:42 +00:00
|
|
|
$filter_type = $this->tainacan_filter_factory->create_filter('custom_interval');
|
2018-01-16 13:41:24 +00:00
|
|
|
|
|
|
|
$filter = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
|
|
|
'name' => 'filtro',
|
|
|
|
'collection' => $collection,
|
|
|
|
'description' => 'descricao',
|
2018-01-31 14:47:59 +00:00
|
|
|
'field' => $field,
|
2018-01-16 13:41:24 +00:00
|
|
|
'filter_type' => $filter_type,
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$new_attributes = json_encode([
|
|
|
|
'name' => 'Faceta',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'PATCH', $this->namespace . '/filters/' . $filter->get_id()
|
|
|
|
);
|
|
|
|
|
|
|
|
$request->set_body($new_attributes);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertNotEquals('filtro', $data['name']);
|
|
|
|
$this->assertEquals('Faceta', $data['name']);
|
|
|
|
}
|
2018-01-17 17:48:44 +00:00
|
|
|
|
|
|
|
public function test_fetch_filters(){
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'Collection filtered',
|
|
|
|
'description' => 'Is filtered',
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2018-01-17 17:48:44 +00:00
|
|
|
array(
|
2018-01-31 14:47:59 +00:00
|
|
|
'name' => 'Field filtered',
|
2018-01-17 17:48:44 +00:00
|
|
|
'description' => 'Is filtered',
|
|
|
|
'collection_id' => $collection->get_id()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-01-31 14:47:59 +00:00
|
|
|
$field2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'field',
|
2018-01-17 17:48:44 +00:00
|
|
|
array(
|
|
|
|
'name' => 'Other filtered',
|
|
|
|
'description' => 'Is filtered',
|
|
|
|
'collection_id' => $collection->get_id()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-03-01 19:32:42 +00:00
|
|
|
$filter_type = $this->tainacan_filter_factory->create_filter('custom_interval');
|
2018-01-17 17:48:44 +00:00
|
|
|
|
|
|
|
$filter = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
|
|
|
'name' => 'filtro',
|
|
|
|
'collection' => $collection,
|
|
|
|
'description' => 'descricao',
|
2018-01-31 14:47:59 +00:00
|
|
|
'field' => $field,
|
2018-01-17 17:48:44 +00:00
|
|
|
'filter_type' => $filter_type,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$filter2 = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'filter',
|
|
|
|
array(
|
|
|
|
'name' => 'filtro2',
|
|
|
|
'collection' => $collection,
|
|
|
|
'description' => 'descricao',
|
2018-01-31 14:47:59 +00:00
|
|
|
'field' => $field2,
|
2018-01-17 17:48:44 +00:00
|
|
|
'filter_type' => $filter_type,
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/filters');
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$first_filter = $data[0];
|
|
|
|
$second_filter = $data[1];
|
|
|
|
|
|
|
|
$this->assertEquals($filter2->get_name(), $first_filter['name']);
|
|
|
|
$this->assertEquals($filter->get_name(), $second_filter['name']);
|
2018-01-18 13:23:16 +00:00
|
|
|
|
2018-01-18 13:38:31 +00:00
|
|
|
#### FETCH A FILTER ####
|
2018-01-18 13:23:16 +00:00
|
|
|
|
|
|
|
$request = new \WP_REST_Request(
|
|
|
|
'GET', $this->namespace . '/filters/' . $filter->get_id()
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals('filtro', $data['name']);
|
2018-01-17 17:48:44 +00:00
|
|
|
}
|
2018-01-10 15:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|