fix exclude param in rest api

This commit is contained in:
leogermani 2019-12-18 17:28:49 -03:00
parent f0e9f27af5
commit e94a08269d
3 changed files with 201 additions and 89 deletions

View File

@ -91,7 +91,7 @@ class REST_Controller extends \WP_REST_Controller {
'nopaging' => 'nopaging',
'metatype' => 'meta_type',
'hierarchical' => 'hierarchical',
'exclude' => 'exclude',
'exclude' => 'post__not_in',
'excludetree' => 'exclude_tree',
'include' => 'include'
];
@ -373,6 +373,14 @@ class REST_Controller extends \WP_REST_Controller {
'type' => 'integer',
);
$query_params['exclude'] = array(
'description' => __("Ensure result set excludes specific IDs.", 'tainacan'),
'type' => 'array',
'items' => [
'type' => 'integer'
]
);
return $query_params;
}

View File

@ -94,7 +94,7 @@ class REST_Items_Controller extends REST_Controller {
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_item_attachments'),
'permission_callback' => array($this, 'get_item_attachments_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::READABLE),
'args' => $this->get_wp_query_params(),
),
'schema' => [$this, 'get_attachments_schema'],
)

View File

@ -943,6 +943,110 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
}
public function test_fetch_items_exclude(){
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Agile',
'description' => 'Agile methods',
'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
);
$item3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'SCRUM3',
'description' => 'Um framework ágil para gerenciamento de tarefas.',
'collection' => $collection,
'status' => 'publish'
),
true
);
$item4 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'SCRUM4',
'description' => 'Um framework ágil para gerenciamento de tarefas.',
'collection' => $collection,
'status' => 'publish'
),
true
);
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'exclude' => [
$item1->get_id(),
$item2->get_id()
]
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(2, sizeof($data));
$items_titles = [$data[0]['title'], $data[1]['title']];
$this->assertContains($item3->get_title(), $items_titles);
$this->assertContains($item4->get_title(), $items_titles);
// test using an string
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'exclude' => implode(',', [
$item1->get_id(),
$item2->get_id()
])
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(2, sizeof($data));
$items_titles = [$data[0]['title'], $data[1]['title']];
$this->assertContains($item3->get_title(), $items_titles);
$this->assertContains($item4->get_title(), $items_titles);
}
}
?>