diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 0848a34c8..8dccc6d4e 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -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. diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index d4cdfada8..06525444c 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -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 ); diff --git a/tests/test-compound-metadatum-types.php b/tests/test-compound-metadatum-types.php index de988e331..46fa44910 100644 --- a/tests/test-compound-metadatum-types.php +++ b/tests/test-compound-metadatum-types.php @@ -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()); + } } \ No newline at end of file