fix: return metadata section on delete

This commit is contained in:
vnmedeiros 2022-08-03 15:19:16 -03:00
parent 3b88491708
commit eda15fdc40
3 changed files with 73 additions and 8 deletions

View File

@ -387,6 +387,9 @@ class Metadata_Sections extends Repository {
*/ */
public function delete( Entities\Entity $entity, $permanent = true ) { public function delete( Entities\Entity $entity, $permanent = true ) {
//test if not exist a metadata using this section //test if not exist a metadata using this section
if ( !empty( $this->get_metadata_object_list($entity->get_id() ) ) ) {
throw new \Exception( 'The metadata section must not contain metadata before deleted' );
}
return parent::delete($entity, $permanent); return parent::delete($entity, $permanent);
} }

View File

@ -515,6 +515,7 @@ abstract class Repository {
$Tainacan_Metadata = Repositories\Metadata::get_instance(); $Tainacan_Metadata = Repositories\Metadata::get_instance();
$Tainacan_Taxonomies = Repositories\Taxonomies::get_instance(); $Tainacan_Taxonomies = Repositories\Taxonomies::get_instance();
$Tainacan_Terms = Repositories\Terms::get_instance(); $Tainacan_Terms = Repositories\Terms::get_instance();
$Tainacan_Metadata_Sections = Repositories\Metadata_Sections::get_instance();
$tnc_globals = [ $tnc_globals = [
$Tainacan_Collections, $Tainacan_Collections,
@ -522,7 +523,8 @@ abstract class Repository {
$Tainacan_Filters, $Tainacan_Filters,
$Tainacan_Taxonomies, $Tainacan_Taxonomies,
$Tainacan_Terms, $Tainacan_Terms,
$Tainacan_Logs $Tainacan_Logs,
$Tainacan_Metadata_Sections
]; ];
foreach ( $tnc_globals as $tnc_repository ) { foreach ( $tnc_globals as $tnc_repository ) {
$tnc_entity = new $tnc_repository->entities_type(); $tnc_entity = new $tnc_repository->entities_type();

View File

@ -9,15 +9,15 @@ use Tainacan\Metadata_Types;
*/ */
/** /**
* Metadatum test case. * MetadataSection test case.
* @group metadata * @group metadata
*/ */
class MetadataSection extends TAINACAN_UnitTestCase { class MetadataSection extends TAINACAN_UnitTestCase {
/** /**
* Test insert a regular metadatum with type * Test create a metadata section
*/ */
function test_add() { function test_create() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$Tainacan_Metadata_Section = \Tainacan\Repositories\Metadata_Sections::get_instance(); $Tainacan_Metadata_Section = \Tainacan\Repositories\Metadata_Sections::get_instance();
@ -55,7 +55,7 @@ class MetadataSection extends TAINACAN_UnitTestCase {
); );
$test = $Tainacan_Metadata->fetch($metadatum->get_id()); $test = $Tainacan_Metadata->fetch($metadatum->get_id());
$section = $Tainacan_Metadata_Section->fetch($metadata_section->get_id()); $Tainacan_Metadata_Section->fetch($metadata_section->get_id());
$this->assertEquals($test->get_name(), 'metadado'); $this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_description(), 'descricao'); $this->assertEquals($test->get_description(), 'descricao');
@ -69,9 +69,69 @@ class MetadataSection extends TAINACAN_UnitTestCase {
$this->assertTrue((bool) $test->get_accept_suggestion()); $this->assertTrue((bool) $test->get_accept_suggestion());
} }
// function test_remove() { /**
// return; * Test remove a metadata section
// } */
function test_remove() {
$Tainacan_Metadata_Section = \Tainacan\Repositories\Metadata_Sections::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste'
),
true
);
$metadata_section_to_delete = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'status' => 'publish'
),
true
);
$metadata_section_no_delete = $this->tainacan_entity_factory->create_entity(
'Metadata_Section',
array(
'name' => 'Section',
'description' => 'Section Description',
'collection' => $collection,
'status' => 'publish'
),
true
);
$this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadado',
'description' => 'descricao',
'collection' => $collection,
'accept_suggestion' => true,
'metadata_type' => 'Tainacan\Metadata_Types\Text',
'metadata_section_id' => $metadata_section_no_delete->get_id(),
),
true,
true
);
// $Tainacan_Metadata->fetch($metadatum->get_id());
$section_id = $metadata_section_to_delete->get_id();
$section = $Tainacan_Metadata_Section->fetch($section_id);
$section = $Tainacan_Metadata_Section->delete($section);
$section_empty = $Tainacan_Metadata_Section->fetch($section_id);
$this->assertEquals($section_id, $section->get_id() );
$this->assertTrue(empty($section_empty));
$this->setExpectedException(\Exception::class);
$section = $Tainacan_Metadata_Section->fetch($metadata_section_no_delete->get_id());
$Tainacan_Metadata_Section->delete($section);
}
function test_change_section() { function test_change_section() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();