Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
vnmedeiros 2019-10-11 11:02:20 -03:00
commit 6d10cf31d5
3 changed files with 142 additions and 4 deletions

View File

@ -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 );

View File

@ -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);
}
}
}
}
}
}

View File

@ -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 );
@ -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());
}
}