add validate metadata order #17

This commit is contained in:
vnmedeiros 2020-03-05 14:02:48 -03:00
parent c00e99f639
commit 966aa2adf1
3 changed files with 165 additions and 3 deletions

View File

@ -746,10 +746,38 @@ class Collection extends Entity {
return true;
}
return parent::validate();
return $this->validate_metadata_order() && parent::validate();
}
/**
* Validate metadata order collection
*
* @return bool
*/
function validate_metadata_order() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$metadata_order = $this->get_metadata_order();
if( empty($metadata_order) ) {
return true;
}
$parent = 0;
for($pos = 0; $pos < sizeof($metadata_order); $pos++ ) {
$metadatum_id = $metadata_order[$pos]['id'];
$metadatum = $Tainacan_Metadata->fetch( $metadatum_id );
if ($metadatum->get_parent() != 0) {
if ($parent != $metadatum->get_parent()) {
$this->add_error('metadata_order', __('collection order is invalid', 'tainacan'));
return false;
}
} else {
$parent = $metadatum_id;
}
}
return true;
}
/**
* Checks if an user have permission on any of the collections capabilities
* defined in Tainacan\Roles class.

View File

@ -846,14 +846,22 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase {
* @group compound_metadatum
*/
public function test_create_compound_metadatum_API() {
$collection = $this->tainacan_entity_factory->create_entity('collection', '', true);
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'quadrado',
'status' => 'publish'
),
true
);
$this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'No name',
'description' => 'No description',
'collection' => $collection
'collection' => $collection,
'status' => 'publish'
),
true
);

View File

@ -336,5 +336,131 @@ class CompoundMetadatumTypes extends TAINACAN_UnitTestCase {
}
function teste_validations_metadada_order() {
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
'status' => 'publish'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Compound',
'status' => 'publish',
),
true
);
$metadatum_child1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta2',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'parent' => $metadatum->get_id(),
),
true
);
$metadatum_child2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta3',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish',
'parent' => $metadatum->get_id(),
),
true
);
$metadatum1 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta3',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$metadatum2 = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta4',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'status' => 'publish'
),
true
);
$order = [
['id' => $metadatum->get_id(), 'enabled' => true],
['id' => $metadatum_child1->get_id(), 'enabled' => true],
['id' => $metadatum_child2->get_id(), 'enabled' => true],
['id' => $metadatum1->get_id(), 'enabled' => true],
['id' => $metadatum2->get_id(), 'enabled' => true]
];
$collection->set_metadata_order($order);
$collection->validate();
\tainacan_collections()->insert($collection);
$metadata_order = $Tainacan_Collections->fetch( $collection->get_id(), 'OBJECT' )->get_metadata_order();
$this->assertEquals($metadata_order[0]['id'], $order[0]['id']);
$this->assertEquals($metadata_order[1]['id'], $order[1]['id']);
$this->assertEquals($metadata_order[2]['id'], $order[2]['id']);
$this->assertEquals($metadata_order[3]['id'], $order[3]['id']);
$this->assertEquals($metadata_order[4]['id'], $order[4]['id']);
$order = [
['id' => $metadatum1->get_id(), 'enabled' => true],
['id' => $metadatum2->get_id(), 'enabled' => true],
['id' => $metadatum->get_id(), 'enabled' => true],
['id' => $metadatum_child1->get_id(), 'enabled' => true],
['id' => $metadatum_child2->get_id(), 'enabled' => true]
];
$collection->set_metadata_order($order);
$collection->validate();
\tainacan_collections()->insert($collection);
$metadata_order = $Tainacan_Collections->fetch( $collection->get_id(), 'OBJECT' )->get_metadata_order();
$this->assertEquals($metadata_order[0]['id'], $order[0]['id']);
$this->assertEquals($metadata_order[1]['id'], $order[1]['id']);
$this->assertEquals($metadata_order[2]['id'], $order[2]['id']);
$this->assertEquals($metadata_order[3]['id'], $order[3]['id']);
$this->assertEquals($metadata_order[4]['id'], $order[4]['id']);
$order = [
['id' => $metadatum1->get_id(), 'enabled' => true],
['id' => $metadatum->get_id(), 'enabled' => true],
['id' => $metadatum2->get_id(), 'enabled' => true],
['id' => $metadatum_child1->get_id(), 'enabled' => true],
['id' => $metadatum_child2->get_id(), 'enabled' => true]
];
$collection->set_metadata_order($order);
$this->assertFalse($collection->validate());
}
}