Do not allow core fields to be multiple #39

This commit is contained in:
Leo Germani 2018-05-11 17:43:13 -03:00
parent 6a98e79ece
commit decbf64661
3 changed files with 64 additions and 0 deletions

View File

@ -2,6 +2,8 @@
namespace Tainacan\Field_Types;
use Tainacan\Entities\Field;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
@ -59,5 +61,18 @@ class Core_Description extends Field_Type {
return $item->validate_prop('description');
}
public function validate_options( Field $field ) {
if ( !in_array($field->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
if ( $field->get_multiple() != 'no') {
return ['multiple' => __('Core Metadata can not accept multiple values', 'tainacan')];
}
return true;
}
}

View File

@ -2,6 +2,9 @@
namespace Tainacan\Field_Types;
use Tainacan\Entities\Field;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
@ -59,5 +62,18 @@ class Core_Title extends Field_Type {
return $item->validate_prop('title');
}
public function validate_options( Field $field ) {
if ( !in_array($field->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
return true;
if ( $field->get_multiple() != 'no') {
return ['multiple' => __('Core Metadata can not accept multiple values', 'tainacan')];
}
return true;
}
}

View File

@ -137,5 +137,38 @@ class CoreFieldTypes extends TAINACAN_UnitTestCase {
$this->assertTrue($i->validate(), 'Item with empy title should validate because core title field has value');
}
function test_dont_allow_multiple() {
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
$fields = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' ) ;
foreach ( $fields as $index => $field ){
if ( $field->get_field_type_object()->get_core() && $field->get_field_type_object()->get_related_mapped_prop() == 'title') {
$core_title = $field;
}
if ( $field->get_field_type_object()->get_core() && $field->get_field_type_object()->get_related_mapped_prop() == 'description') {
$core_description = $field;
}
}
$core_title->set_multiple('yes');
$core_description->set_multiple('yes');
$this->assertFalse($core_title->validate(), 'Core metadata should not validate because it can not allow it to have multiple');
$this->assertFalse($core_description->validate(), 'Core metadata should not validate because it can not allow it to have multiple');
}
}