From 34b817e01ddf6cff7114166d7121a7a8bdbf9cbd Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Thu, 10 Oct 2019 16:20:03 -0300 Subject: [PATCH 1/2] fix tests for filter types count #306 --- tests/test-filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-filters.php b/tests/test-filters.php index 92b9a9a33..dad464c75 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -97,7 +97,7 @@ class Filters extends TAINACAN_UnitTestCase { $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); $all_filter_types = $Tainacan_Filters->fetch_filter_types(); - $this->assertEquals( 9, count( $all_filter_types ) ); + $this->assertEquals( 11, count( $all_filter_types ) ); $float_filters = $Tainacan_Filters->fetch_supported_filter_types('float'); $this->assertTrue( count( $float_filters ) > 0 ); From 15b899af8cca5a09fffb7a5b15ef41cc0696fc89 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Thu, 10 Oct 2019 20:40:21 -0300 Subject: [PATCH 2/2] Do not allow public filter for private meta #315 --- .../entities/class-tainacan-filter.php | 15 +++ .../repositories/class-tainacan-filters.php | 36 ++++++- tests/test-filters.php | 93 +++++++++++++++++++ 3 files changed, 141 insertions(+), 3 deletions(-) diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index f34a4bdc2..ada699f51 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -275,6 +275,21 @@ class Filter extends Entity { if (false === $is_valid) return false; + + $status = $this->get_status(); + $status_obj = get_post_status_object($status); + + if ($status_obj->public) { + $metadatum = $this->get_metadatum(); + if ($metadatum) { + $metadatum_status_obj = get_post_status_object($metadatum->get_status()); + if ( ! $metadatum_status_obj->public ) { + $this->add_error('status', __('Filter can not be public because the related metadatum is private', 'tainacan')); + return false; + } + } + } + $fto = $this->get_filter_type_object(); if (is_object($fto)) { $is_valid = $fto->validate_options( $this ); diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index f8fbf7ef1..9015262b6 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -25,6 +25,7 @@ class Filters extends Repository { protected function __construct() { parent::__construct(); add_action( 'tainacan-deleted-tainacan-metadatum', array( &$this, 'hook_delete_when_metadata_deleted' ), 10, 2 ); + add_action( 'tainacan-insert-tainacan-metadatum', array( &$this, 'hook_update_when_metadata_saved_as_private' ), 10, 2 ); } protected function _get_map() { @@ -508,10 +509,10 @@ class Filters extends Repository { return $result; } - public function hook_delete_when_metadata_deleted($filter, $permanent) { + public function hook_delete_when_metadata_deleted($metadatum, $permanent) { - if ( $filter instanceof Entities\Metadatum ) { - $metadatum_id = $filter->get_id(); + if ( $metadatum instanceof Entities\Metadatum ) { + $metadatum_id = $metadatum->get_id(); $filters = $this->fetch(['metadatum_id' => $metadatum_id, 'post_status' => 'any'], 'OBJECT'); foreach ($filters as $filter) { if ($permanent) { @@ -525,4 +526,33 @@ class Filters extends Repository { } + public function hook_update_when_metadata_saved_as_private($metadatum) { + + if ( $metadatum instanceof Entities\Metadatum ) { + + $status_obj = get_post_status_object( $metadatum->get_status() ); + + if ( ! $status_obj->public ) { + + $stati = get_post_stati(['public' => true]); + + $metadatum_id = $metadatum->get_id(); + + $filters = $this->fetch(['metadatum_id' => $metadatum_id, 'post_status' => $stati], 'OBJECT'); + + foreach ($filters as $filter) { + $filter->set_status( $metadatum->get_status() ); + if ( $filter->validate() ) { + $this->insert($filter); + } + + } + + } + + + } + + } + } \ No newline at end of file diff --git a/tests/test-filters.php b/tests/test-filters.php index dad464c75..a63eea3bc 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -458,4 +458,97 @@ class Filters extends TAINACAN_UnitTestCase { } + function test_private_metadatum() { + + $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Collection filtered', + 'description' => 'Is filtered', + ), + true + ); + + $metadatum2 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Other filtered', + 'description' => 'Is filtered', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'status' => 'private', + 'collection_id' => $collection->get_id() + ), + true + ); + + $filter = new \Tainacan\Entities\Filter(); + $filter->set_name('teste'); + $filter->set_metadatum($metadatum2); + $filter->set_status('publish'); + + $this->assertFalse($filter->validate()); + $this->assertArrayHasKey('status', $filter->get_errors()[0]); + + $filter->set_status('private'); + + $this->assertTrue($filter->validate()); + + + } + + function test_private_metadatum_update() { + + $Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Collection filtered', + 'description' => 'Is filtered', + ), + true + ); + + $metadatum2 = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Other filtered', + 'description' => 'Is filtered', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'status' => 'publish', + 'collection_id' => $collection->get_id() + ), + true + ); + + $filter = $this->tainacan_entity_factory->create_entity( + 'filter', + array( + 'name' => 'filtro', + 'collection' => $collection, + 'description' => 'descricao', + 'metadatum' => $metadatum2, + 'status' => 'publish', + 'filter_type' => 'Tainacan\Filter_Types\Autocomplete' + ), + true + ); + + $this->assertEquals('publish', $filter->get_status()); + + $metadatum2->set_status('private'); + $metadatum2->validate(); + $Tainacan_Metadata->insert($metadatum2); + + $check_filter = $Tainacan_Filters->fetch( $filter->get_id() ); + + $this->assertEquals('private', $check_filter->get_status()); + + } + + } \ No newline at end of file