diff --git a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php index 18936bcba..515575848 100644 --- a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php +++ b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php @@ -284,9 +284,12 @@ class REST_Taxonomies_Controller extends REST_Controller { * @return bool|\WP_Error */ public function get_items_permissions_check( $request ) { - if('edit' === $request['context'] && !$this->taxonomy_repository->can_read($this->taxonomy)) { + if('edit' === $request['context'] && !is_user_logged_in()) { return false; } + // if(!$this->taxonomy_repository->can_read($this->taxonomy)) { + // return false; + // } return true; } diff --git a/tests/test-api-visibility-objects.php b/tests/test-api-visibility-objects.php new file mode 100644 index 000000000..c97c460ec --- /dev/null +++ b/tests/test-api-visibility-objects.php @@ -0,0 +1,474 @@ +tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'taxonomy_public', + 'description' => 'taxonomy_public', + 'status' => 'publish' + ), + true + ); + $this->taxonomy_public = $taxonomy_public; + + $term_a_public = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy_public->get_db_identifier(), + 'name' => 'term_a_public' + ), + true + ); + $this->term_public = $term_a_public; + + $term_b_public = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy_public->get_db_identifier(), + 'name' => 'term_b_public' + ), + true + ); + + $taxonomy_private = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'taxonomy_private', + 'description' => 'taxonomy_private', + 'status' => 'private' + ), + true + ); + $this->taxonomy_private = $taxonomy_private; + + $term_a_private = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy_private->get_db_identifier(), + 'name' => 'term_a_private' + ), + true + ); + $this->term_private = $term_a_private; + + $term_b_private = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $taxonomy_private->get_db_identifier(), + 'name' => 'term_b_private' + ), + true + ); + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'collection', + 'status' => 'publish' + ), + true + ); + $this->collection = $collection; + + $metadata_tax_public = $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' => true, + 'taxonomy_id' => $taxonomy_public->get_id() + ], + 'multiple' => 'yes' + ), + true + ); + + $metadata_tax_private = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'metadata-private', + 'status' => 'publish', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + 'metadata_type_options' => [ + 'allow_new_terms' => true, + 'taxonomy_id' => $taxonomy_private->get_id() + ], + 'multiple' => 'yes' + ), + true + ); + + $item_a = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'item-a', + 'collection' => $collection, + 'status' => 'publish' + ), + true + ); + + $item_b = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'item-b', + 'collection' => $collection, + 'status' => 'publish' + ), + true + ); + + $this->tainacan_item_metadata_factory->create_item_metadata( + $item_a, $metadata_tax_public, $term_a_public->get_id()); + $this->tainacan_item_metadata_factory->create_item_metadata( + $item_a, $metadata_tax_public, $term_b_public->get_id()); + + $this->tainacan_item_metadata_factory->create_item_metadata( + $item_b, $metadata_tax_private, $term_a_private->get_id()); + $this->tainacan_item_metadata_factory->create_item_metadata( + $item_b, $metadata_tax_private, $term_b_private->get_id()); + } + + public function test_get_terms_of_taxonomy_logged() { + //tax public + $request_public = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_public->get_id() . '/terms' + ); + $request_public->set_query_params(['hideempty' => false]); + $response = $this->server->dispatch($request_public); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + $this->assertEquals(2, sizeof($data)); + + //tax private: + $request_private = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_private->get_id() . '/terms' + ); + $request_private->set_query_params(['hideempty' => false]); + $response = $this->server->dispatch($request_private); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + $this->assertEquals(2, sizeof($data)); + + //tax public - context=edit: + $request_public_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_public->get_id() . '/terms' + ); + $request_public_edit->set_query_params(['context' => 'edit']); + $response = $this->server->dispatch($request_public_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + // $this->assertEquals(2, sizeof($data)); + + //tax private - context=edit: + $request_private_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_private->get_id() . '/terms' + ); + $request_public->set_query_params(['context' => 'edit']); + $response = $this->server->dispatch($request_private_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + //$this->assertEquals(2, sizeof($data)); + } + + public function test_get_terms_of_taxonomy_not_logged() { + wp_logout(); + wp_set_current_user(0); + //tax public + $request_public = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_public->get_id() . '/terms' + ); + $request_public->set_query_params(['hideempty' => false]); + $response = $this->server->dispatch($request_public); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + $this->assertEquals(2, sizeof($data)); + + //tax private: + $request_private = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_private->get_id() . '/terms' + ); + $request_private->set_query_params(['hideempty' => false]); + $response = $this->server->dispatch($request_private); + $status = $response->status; + $this->assertEquals(401, $status); + + //tax public - context=edit: + $request_public_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_public->get_id() . '/terms' + ); + $request_public_edit->set_query_params(['context' => 'edit']); + $response = $this->server->dispatch($request_public_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(401, $status); + + //tax private - context=edit: + $request_private_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomy/' . $this->taxonomy_private->get_id() . '/terms' + ); + $request_public->set_query_params(['context' => 'edit']); + $response = $this->server->dispatch($request_private_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(401, $status); + } + + public function test_get_taxonomies_logged() { + + $request_public = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomies' + ); + $response = $this->server->dispatch($request_public); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + $this->assertEquals(2, sizeof($data)); + + + $request_public_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomies' + ); + $request_public_edit->set_query_params(['context' => 'edit']); + $response = $this->server->dispatch($request_public_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + $this->assertEquals(2, sizeof($data)); + } + + public function test_get_taxonomies_not_logged() { + wp_logout(); + wp_set_current_user(0); + + $request_public = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomies' + ); + $response = $this->server->dispatch($request_public); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + $this->assertEquals(1, sizeof($data)); + + $request_public_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/taxonomies' + ); + $request_public_edit->set_query_params(['context' => 'edit']); + $response = $this->server->dispatch($request_public_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(401, $status); + } + + public function test_get_items_logged() { + //tax public + $request_public = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + + $tax_query = [[ + 'taxonomy'=> $this->taxonomy_public->get_db_identifier(), + 'terms' => [$this->term_public->get_id()], + 'compare' => 'IN' + ]]; + + $request_public->set_query_params(['hideempty' => false]); + $request_public->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_public); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + //$this->assertEquals(1, sizeof($data)); + + //tax public - context=edit: + $request_public_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + $request_public_edit->set_query_params(['context' => 'edit']); + $request_public_edit->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_public_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + //$this->assertEquals(2, sizeof($data)); + + //tax private: + $request_private = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + + $tax_query = [[ + 'taxonomy'=> $this->taxonomy_private->get_db_identifier(), + 'terms' => [$this->term_private->get_id()], + 'compare' => 'IN' + ]]; + + $request_private->set_query_params(['hideempty' => false]); + $request_private->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_private); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + //$this->assertEquals(2, sizeof($data)); + + //tax private - context=edit: + $request_private_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + $request_private_edit->set_query_params(['context' => 'edit']); + $request_private_edit->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_private_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + //$this->assertEquals(2, sizeof($data)); + } + + public function test_get_items_not_logged() { + wp_logout(); + wp_set_current_user(0); + //tax public + $request_public = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + + $tax_query = [[ + 'taxonomy'=> $this->taxonomy_public->get_db_identifier(), + 'terms' => [$this->term_public->get_id()], + 'compare' => 'IN' + ]]; + + $request_public->set_query_params(['hideempty' => false]); + $request_public->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_public); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + //$this->assertEquals(1, sizeof($data)); + + //tax public - context=edit: + $request_public_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + $request_public_edit->set_query_params(['context' => 'edit']); + $request_public_edit->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_public_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(200, $status); + //$this->assertEquals(2, sizeof($data)); + + //tax private: + $request_private = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + + $tax_query = [[ + 'taxonomy'=> $this->taxonomy_private->get_db_identifier(), + 'terms' => [$this->term_private->get_id()], + 'compare' => 'IN' + ]]; + + $request_private->set_query_params(['hideempty' => false]); + $request_private->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_private); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(401, $status); + //$this->assertEquals(2, sizeof($data)); + + //tax private - context=edit: + $request_private_edit = new \WP_REST_Request( + 'GET', $this->namespace . '/items' + ); + $request_private_edit->set_query_params(['context' => 'edit']); + $request_private_edit->set_query_params(['taxquery' => $tax_query]); + $response = $this->server->dispatch($request_private_edit); + $status = $response->status; + $data = $response->get_data(); + $this->assertEquals(401, $status); + //$this->assertEquals(2, sizeof($data)); + } +} + +?> \ No newline at end of file