diff --git a/library/Class/CodifThesaurus.php b/library/Class/CodifThesaurus.php index 0519dd1c66e528dc0659f962778a0e47a07055b7..41166914d859ed5e92c69e3abc61ca04175d514b 100644 --- a/library/Class/CodifThesaurus.php +++ b/library/Class/CodifThesaurus.php @@ -46,7 +46,7 @@ class Class_CodifThesaurusLoader extends Storm_Model_Loader { public function getOrCreateForCustomFieldMeta($meta) { return Class_CodifThesaurus::findCustomFieldsRoot() - ->getOrCreateChild($meta->getId(), $meta->getLabel()); + ->getOrCreateChild($meta->getId(), $meta->getLabel()); } @@ -129,7 +129,7 @@ class Class_CodifThesaurusLoader extends Storm_Model_Loader { public function findNextRacineCatalogue() { $last_thesaurus = Class_CodifThesaurus::findFirstBy(['where' => 'code like "catalogue" and LENGTH(id_thesaurus) in (1,8)', - 'order' => 'id_thesaurus desc']); + 'order' => 'id_thesaurus desc']); if (!$last_thesaurus) { return Class_CodifThesaurus::ROOT_DOMAINS_ID . '0001'; } @@ -148,7 +148,7 @@ class Class_CodifThesaurusLoader extends Storm_Model_Loader { public function deleteAllFrom($thesaurus) { if ($thesaurus && '' != $thesaurus->getIdThesaurus()) - sqlExecute('delete from codif_thesaurus where id_thesaurus like "' . $thesaurus->getIdThesaurus() . '%"'); + Zend_Registry::get('sql')->query('delete from codif_thesaurus where id_thesaurus like "' . $thesaurus->getIdThesaurus() . '%"'); } @@ -208,8 +208,19 @@ class Class_CodifThesaurusLoader extends Storm_Model_Loader { $thesauri = array_filter(Class_CodifThesaurus::getIndices('root', true), $filter); - foreach(Class_CodifThesaurus::findCustomFieldsRoot()->getChildren() as $item) + foreach(Class_CodifThesaurus::findCustomFieldsRoot()->getChildren() as $item) { + if(!$meta = Class_CustomField_Meta::find($item->getIdOrigine())) { + $item->deleteMeAndMyChildren(); + continue; + } + + if(!Class_CustomField::findFirstBy(['meta_id' => $meta->getId()])) { + $meta->delete(); + continue; + } + $thesauri[] = $item; + } return $thesauri; } @@ -273,6 +284,7 @@ class Class_CodifThesaurus extends Storm_Model_Abstract { protected $_table_primary = 'id'; protected $_default_attribute_values = ['libelle_facette' => '', 'id_thesaurus' =>null, + 'id_origine' => null, 'rules' => null]; public function getListeSuggestion($recherche, $mode, $limite_resultat, $theme) { @@ -335,19 +347,20 @@ class Class_CodifThesaurus extends Storm_Model_Abstract { public function getOrCreateChild($id_origine, $label) { - $entry = $this->getLoader() - ->findFirstBy(['code' => $this->getCode(), - 'id_origine' => mb_substr($id_origine, - 0, - self::COLUMN_ORIGIN_SIZE, - 'UTF-8')]); + $entry = Class_CodifThesaurus::getLoader() + ->findFirstBy(['code' => $this->getCode(), + 'id_origine' => mb_substr($id_origine, + 0, + self::COLUMN_ORIGIN_SIZE, + 'UTF-8')]); + if ($entry) return $entry; $entry = $this ->newChildEntry() ->updateAttributes(['id_origine' => $id_origine, - 'libelle' => $label]); + 'libelle' => $label]); $entry->assertSave(); return $entry; diff --git a/library/Class/Codification.php b/library/Class/Codification.php index 526446b228cfeaae0fab1c0bb0ccad0b68e33f16..56abd4322503c35282828d5fa143b9cc19cb4a55 100644 --- a/library/Class/Codification.php +++ b/library/Class/Codification.php @@ -304,8 +304,6 @@ class Class_Codification { } - - public function cleanDeletedFacets($concat_facets) { $codes = []; $facets = $this->getCodesLibellesFacette($concat_facets); diff --git a/library/Class/CustomField.php b/library/Class/CustomField.php index 212f10d246b47ec2c23ef595cb59594ec4651e81..6773bb6b12fe0bfadf39e2208a6180901aa0b38d 100644 --- a/library/Class/CustomField.php +++ b/library/Class/CustomField.php @@ -189,4 +189,10 @@ class Class_CustomField extends Storm_Model_Abstract { public function getThesaurusIdOrigine() { return $this->getMeta()->getId(); } + + + public function afterDelete() { + if($meta = $this->getMeta()) + $meta->delete(); + } } \ No newline at end of file diff --git a/tests/application/modules/admin/controllers/CmsControllerCustomFieldsTest.php b/tests/application/modules/admin/controllers/CmsControllerCustomFieldsTest.php index e71267254c8bbce02ae02b66defc897d8ff6de5d..023f4bcb1379c67b30d7bf8f09d5953c498046a7 100644 --- a/tests/application/modules/admin/controllers/CmsControllerCustomFieldsTest.php +++ b/tests/application/modules/admin/controllers/CmsControllerCustomFieldsTest.php @@ -186,7 +186,7 @@ class CmsControllerCustomFieldsNotIndexableMetaPostEditActionTest Zend_Registry::set('sql', $this->mock() - ->whenCalled('execute') + ->whenCalled('query') ->with('delete from codif_thesaurus where id_thesaurus like "CFCF0001%"') ->willDo( function() { diff --git a/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php b/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php index 3037737197208e2dafa62763aee8143d006814de..59de566e1d7adb304d5d5d7c242c52d7a8d547a2 100644 --- a/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php +++ b/tests/application/modules/admin/controllers/CustomFieldsControllerTest.php @@ -266,7 +266,8 @@ class CustomFieldsControllerAddIndexTest extends CustomFieldControllerTestCase { class CustomFieldsControllerPostAddActionTest extends AbstractControllerTestCase { - protected $_storm_default_to_volatile = true; + protected $_storm_default_to_volatile = true, + $thesaurus; public function setup() { parent::setup(); @@ -278,6 +279,7 @@ class CustomFieldsControllerPostAddActionTest extends AbstractControllerTestCase 'model' => 'Article']); $this->field = Class_CustomField::find(1); + $this->thesaurus = $this->field->getMeta()->getThesaurus(); } @@ -310,6 +312,32 @@ class CustomFieldsControllerPostAddActionTest extends AbstractControllerTestCase public function shouldBeIndexable() { $this->assertEquals(1, $this->field->getIndexable()); } + + + /** @test */ + public function thesaurusShouldBeCreated() { + $this->assertNotNull($this->thesaurus); + } + + + /** @test */ + public function deleteCustomFieldShouldDeleteThesaurus() { + Zend_Registry::set('sql', + $this->mock() + ->whenCalled('query') + ->with('delete from codif_thesaurus where id_thesaurus like "CFCF0001%"') + ->willDo( + function() { + Class_CodifThesaurus::findForCustomField($this->field) + ->delete(); + }) + ->beStrict()); + + $thesaurus_id = $this->thesaurus->getId(); + $this->field->delete(); + Class_CodifThesaurus::clearCache(); + $this->assertNull(Class_CodifThesaurus::find($thesaurus_id)); + } } diff --git a/tests/application/modules/admin/controllers/ModulesControllerTest.php b/tests/application/modules/admin/controllers/ModulesControllerTest.php index 99a6187e77fb804377902bae2a58c8836783b41d..570c8c47163b1bbc2b4dea9ce8c9c29073c821cd 100644 --- a/tests/application/modules/admin/controllers/ModulesControllerTest.php +++ b/tests/application/modules/admin/controllers/ModulesControllerTest.php @@ -488,11 +488,28 @@ class ModulesControllerConfigRechercheResultatTest 'libelle' => 'Champs personnalisés', 'id_thesaurus' => 'CFCF']); - $custom_public = $this->fixture('Class_CodifThesaurus', - ['id' => 3, - 'code' => 'Custom fields', - 'libelle' => 'Custom public', - 'id_thesaurus' => 'CFCF0001']); + $public = $this->fixture('Class_CustomField', + ['id' => 5, + 'priority' => 3, + 'label' => 'Custom public', + 'field_type' => Class_CustomField_Meta::MULTI_CHECKBOX, + 'options_list' => 'culture;bd;logiciels libres', + 'indexable' => 1, + 'model' => 'Article']); + $public->save(); + + $custom_public = Class_CodifThesaurus::findFirstBy(['id_thesaurus' => 'CFCF0005']); + + $custom_pro = $this->fixture('Class_CodifThesaurus', + ['id' => 9, + 'code' => 'Custom fields', + 'libelle' => 'Custom pro', + 'id_thesaurus' => 'CFCF0009']); + + Zend_Registry::set('sql', + $this->mock() + ->whenCalled('query') + ->answers(true)); $this ->onLoaderOfModel('Class_CodifThesaurus') @@ -502,7 +519,10 @@ class ModulesControllerConfigRechercheResultatTest ->whenCalled('findChildrenOf') ->with($custom_root) - ->answers([$custom_public]); + ->answers([$custom_public]) + + ->whenCalled('deleteMeAndMyChildren') + ->answers(true); Class_Profil::getCurrentProfil()->setCfgModules([]); @@ -553,7 +573,7 @@ class ModulesControllerConfigRechercheResultatTest /** @test */ - public function shouldDisplayRootCustomFieldsChoice() { + public function shouldNotDisplayRootCustomFieldsChoice() { $this->assertNotXPath('//div[@data-heading="facettes"]//li[@data-code="HCFCF"]'); } @@ -562,6 +582,12 @@ class ModulesControllerConfigRechercheResultatTest public function shouldDisplayCustomPublicChoice() { $this->assertXPath('//div[@data-heading="facettes"]//li[@data-code="HCFCF0001"][@data-label="Custom public"]'); } + + + /** @test */ + public function shouldNotDisplayCustomProChoice() { + $this->assertNotXPath('//div[@data-heading="facettes"]//li[@data-code="HCFCF0009"][@data-label="Custom pro"]'); + } }