From 294cfbece24be9206f0c7c0a3db1c25feaaa42fd Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Thu, 5 May 2022 16:28:35 -0300 Subject: [PATCH] fix: update section metadata list --- .../repositories/class-tainacan-metadata.php | 8 +++ tests/test-metadata-section.php | 71 ++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index fa4911401..8e8fbfefa 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -667,6 +667,7 @@ class Metadata extends Repository { */ public function insert( $metadatum ) { $this->pre_update_taxonomy_metadatum( $metadatum ); + $this->pre_update_metadata_section( $metadatum ); $new_metadatum = parent::insert( $metadatum ); $this->update_taxonomy_metadatum( $new_metadatum ); @@ -1738,6 +1739,13 @@ class Metadata extends Repository { } + public function pre_update_metadata_section(Entities\Metadatum $metadatum) { + if($metadatum->get_id() && $metadatum->get_collection_id() != 'default') { + $meta = $this->fetch($metadatum->get_id(), 'OBJECT'); + $this->update_metadata_section( $meta, true ); + } + } + public function update_metadata_section( Entities\Metadatum $metadatum, $remove = false ) { $metadata_section_repository = Metadata_Sections::get_instance(); if (!$remove) { diff --git a/tests/test-metadata-section.php b/tests/test-metadata-section.php index a3fcc4ccc..eb23735f4 100644 --- a/tests/test-metadata-section.php +++ b/tests/test-metadata-section.php @@ -72,9 +72,74 @@ class MetadataSection extends TAINACAN_UnitTestCase { // return; // } - // function test_change_section() { - // return; - // } + function test_change_section() { + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $Tainacan_Metadata_Section = \Tainacan\Repositories\Metadata_Sections::get_instance(); + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'teste' + ), + true + ); + + $metadata_section_a = $this->tainacan_entity_factory->create_entity( + 'Metadata_Section', + array( + 'name' => 'Section A', + 'description' => 'Section Description', + 'collection' => $collection, + 'status' => 'publish' + ), + true + ); + + $metadata_section_b = $this->tainacan_entity_factory->create_entity( + 'Metadata_Section', + array( + 'name' => 'Section B', + 'description' => 'Section Description', + 'collection' => $collection, + 'status' => 'publish' + ), + true + ); + + $metadatum = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'metadado', + 'description' => 'descricao', + 'collection' => $collection, + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'metadata_section_id' => $metadata_section_a->get_id(), + ), + true + ); + + $test = $Tainacan_Metadata->fetch($metadatum->get_id()); + + $section_a = $Tainacan_Metadata_Section->fetch($metadata_section_a->get_id()); + $section_b = $Tainacan_Metadata_Section->fetch($metadata_section_b->get_id()); + + $metadata_list_a = $section_a->get_metadata_list(); + $metadata_list_b = $section_b->get_metadata_list(); + $this->assertContains($test->get_id(), $metadata_list_a); + $this->assertNotContains($test->get_id(), $metadata_list_b); + + $test->set_metadata_section_id($metadata_section_b->get_id()); + $this->assertTrue($test->validate(), json_encode($test->get_errors())); + $Tainacan_Metadata->update($test); + + $test = $Tainacan_Metadata->fetch($metadatum->get_id()); + + $metadata_list_a = $section_a->get_metadata_list(); + $metadata_list_b = $section_b->get_metadata_list(); + $this->assertNotContains($test->get_id(), $metadata_list_a); + $this->assertContains($test->get_id(), $metadata_list_b); + + } /** *