From 7abea139198a24f6e3705facceab8c1470871beb Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Mon, 31 Aug 2015 15:22:02 +0200
Subject: [PATCH] hotline #28796 fix generation of unique id_thesaurus

---
 .../cosmo/controllers/FacetsController.php     | 12 +++++++++---
 .../DynamicFacetsControllerTest.php            | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/cosmogramme/cosmozend/application/modules/cosmo/controllers/FacetsController.php b/cosmogramme/cosmozend/application/modules/cosmo/controllers/FacetsController.php
index 5ad208a1136..83f599228d8 100644
--- a/cosmogramme/cosmozend/application/modules/cosmo/controllers/FacetsController.php
+++ b/cosmogramme/cosmozend/application/modules/cosmo/controllers/FacetsController.php
@@ -64,10 +64,10 @@ class Cosmo_FacetsController extends Zend_Controller_Action {
 
 		if (!$model->getIdThesaurus()) {
 			$libelle = preg_replace('/[^a-zA-Z0-9]/', '', $this->_getParam('libelle_facette'));
-			$libelle = substr($libelle, 0, 4);
+
 			$id_thesaurus = $this->generateNewIdThesaurusForLabel($libelle);
 			$model->setIdThesaurus($id_thesaurus);
-			$model->setCode($libelle);
+			$model->setCode($id_thesaurus);
 		}
 
 		$model->save();
@@ -76,10 +76,16 @@ class Cosmo_FacetsController extends Zend_Controller_Action {
 
 
 	protected function generateNewIdThesaurusForLabel($label) {
+    $id = substr(strtoupper($label), 0, 4);
+
+    if (strlen($id) == 4 && !Class_CodifThesaurus::findFirstBy(['id_thesaurus' => $id]))
+      return $id;
+
+    $label = substr(strtoupper($label), 0, 3);
 		$suffixes = array_merge(range(0, 9), range('a', 'z'));
 		while(!empty($suffixes)) {
 			$suffix = strtoupper(array_shift($suffixes));;
-			$id = sprintf("%'".$suffix."-4s", strtoupper($label));
+			$id = sprintf("%'".$suffix."-4s", $label);
 			if (!Class_CodifThesaurus::findFirstBy(['id_thesaurus' => $id]))
 				return $id;
 		}
diff --git a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DynamicFacetsControllerTest.php b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DynamicFacetsControllerTest.php
index 3bc761728f3..c1e3cd08033 100644
--- a/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DynamicFacetsControllerTest.php
+++ b/cosmogramme/cosmozend/tests/application/modules/cosmo/controllers/DynamicFacetsControllerTest.php
@@ -189,6 +189,24 @@ class Cosmo_DynamicFacetsControllerValidateTest extends Cosmo_DynamicFacetsContr
 	}
 
 
+  /** @test */
+	public function withExistingIdThesaurusDOCUNewIdThesaurusShouldBeDOC0() {
+		$this->fixture('Class_CodifThesaurus',
+									 ['libelle' => 'doc that i love',
+										'id_thesaurus' => 'DOCU',
+										'rules' => '{"label":" 345$t "}']);
+
+		$this->postDispatch(
+												'/cosmo/facets/validate',
+												['libelle_facette' => 'docu',
+												 'rules' => '33$2']);
+
+		$this->assertEquals('DOC0',
+												Class_CodifThesaurus::findFirstBy(['libelle'=>'docu'])->getIdThesaurus());
+
+	}
+
+
 	/** @test */
 	public function withSpecialCharsNewIdThesaurusShouldBeFourLetters() {
 			$this->postDispatch(
-- 
GitLab