diff --git a/library/Class/CodifThesaurus.php b/library/Class/CodifThesaurus.php index 29a597dcfa47763538ddd10199f01a6b7719cb9d..8a3e53661e51b9a150197e74cb93f6c368c0de13 100644 --- a/library/Class/CodifThesaurus.php +++ b/library/Class/CodifThesaurus.php @@ -151,6 +151,10 @@ class Class_CodifThesaurusLoader extends Storm_Model_Loader { class Class_CodifThesaurus extends Storm_Model_Abstract { const CODE_FACETTE = 'H'; + const MODE_HIERARCHY_CONTAINS = 1; + const MODE_LABEL_STARTS_WITH = 2; + const MODE_LABEL_CONTAINS = 3; + const MODE_INDEX_STARTS_WITH = 4; protected $_loader_class = 'Class_CodifThesaurusLoader'; protected $_table_name = 'codif_thesaurus'; @@ -158,26 +162,31 @@ class Class_CodifThesaurus extends Storm_Model_Abstract { protected $_default_attribute_values = ['libelle_facette' => '']; public function getListeSuggestion($recherche,$mode,$limite_resultat,$theme) { - // Lancer la recherche - if($mode=="1") { - if (strlen($recherche)<3) - return []; - $req = "select distinct(id),id_thesaurus,id_origine from codif_thesaurus where id_thesaurus regexp (select group_concat(concat(id_thesaurus,'.*') separator '|') from codif_thesaurus where id_thesaurus like '".$theme."%' and id_origine is not null and libelle like '%".$recherche."%') order by id_thesaurus"; - } - if($mode=="2") + switch ((int) $mode) { + case self::MODE_HIERARCHY_CONTAINS: + if (strlen($recherche)<3) + return []; + $req = "select distinct(id),id_thesaurus,id_origine from codif_thesaurus where id_thesaurus regexp (select group_concat(concat(id_thesaurus,'.*') separator '|') from codif_thesaurus where id_thesaurus like '".$theme."%' and id_origine is not null and libelle like '%".$recherche."%') order by id_thesaurus"; + break; + + case self::MODE_LABEL_STARTS_WITH: $req="select id,id_thesaurus,libelle,id_origine from codif_thesaurus where id_thesaurus like '".$theme."%' and libelle like'".addslashes($recherche)."%' order by id_thesaurus limit ".$limite_resultat; + break; - if($mode=="3") + case self::MODE_LABEL_CONTAINS: $req="select id,libelle,id_origine from codif_thesaurus where id_thesaurus like '".$theme."%' and libelle like'%".addslashes($recherche)."%' order by id_thesaurus limit ".$limite_resultat; + break; - if ($mode=="4") + case self::MODE_INDEX_STARTS_WITH: $req="select id,id_thesaurus,libelle,id_origine from codif_thesaurus where id_thesaurus like '".$theme."%' and id_origine like '".$recherche."%' order by id_thesaurus limit ".$limite_resultat; + break; + } $resultat=fetchAll($req); if (!$resultat) - return false; + return []; foreach($resultat as $enreg) { $thesaurus = Class_CodifThesaurus::getLoader()->find($enreg["id"]); diff --git a/tests/application/modules/admin/controllers/AjaxControllerTest.php b/tests/application/modules/admin/controllers/AjaxControllerTest.php index 7f976fecd8c6714cf3720ea75391cc8da383c084..aabd4d5522c2668ab1f6e4de098a7b6033b4a797 100644 --- a/tests/application/modules/admin/controllers/AjaxControllerTest.php +++ b/tests/application/modules/admin/controllers/AjaxControllerTest.php @@ -40,10 +40,41 @@ class AjaxControllerAuteurTest extends AbstractControllerTestCase { class AjaxControllerThesaurusTest extends AbstractControllerTestCase { + public function setUp() { + parent::setUp(); + $this->mock_sql = $this->mock(); + Zend_Registry::set('sql', $this->mock_sql); + $this->fixture('Class_CodifThesaurus', ['id' => 1, + 'id_origine' => 1, + 'id_thesaurus' => 'PUBL', + 'libelle' => 'pirate']); + + } + /** @test */ - public function getListeSuggesionForThesaurusShouldNotCrash() { - $mock_sql = $this->mock(); - $mock_sql + public function getListeSuggestionForModeHierarchyContains() { + + $this->mock_sql + ->whenCalled('fetchAll') + ->with("select distinct(id),id_thesaurus,id_origine from codif_thesaurus where id_thesaurus regexp (select group_concat(concat(id_thesaurus,'.*') separator '|') from codif_thesaurus where id_thesaurus like 'PUBL%' and id_origine is not null and libelle like '%pirate%') order by id_thesaurus", + false) + ->answers([ + ['id_thesaurus' => 1, + 'id_origine' => 1, + 'id' => 1] + ]) + ->beStrict(); + + + + $this->dispatch('/admin/ajax/listesuggestion/type_autorite/thesaurus/mode/1/valeur/pirate/id_champ/PUBL', true); + $this->assertXPathContentContains('//div[@class="tag_liste"][@clef="1"]', '1 : pirate', $this->_response->getBody()); + } + + /** @test */ + public function getListeSuggestionForModeLabelStartsWith() { + + $this->mock_sql ->whenCalled('fetchAll') ->with("select id,id_thesaurus,libelle,id_origine from codif_thesaurus where id_thesaurus like 'PUBL%' and libelle like'pirate%' order by id_thesaurus limit 100", false) @@ -53,16 +84,50 @@ class AjaxControllerThesaurusTest extends AbstractControllerTestCase { 'id' => 1] ]) ->beStrict(); - Zend_Registry::set('sql', $mock_sql); - $this->fixture('Class_CodifThesaurus', ['id' => 1, - 'id_origine' => 1, - 'id_thesaurus' => 'PUBL', - 'libelle' => 'pirate']); + $this->dispatch('/admin/ajax/listesuggestion/type_autorite/thesaurus/mode/2/valeur/pirate/id_champ/PUBL', true); $this->assertXPathContentContains('//div[@class="tag_liste"][@clef="1"]', '1 : pirate', $this->_response->getBody()); } + + /** @test */ + public function getListeSuggestionForModeLabelContains() { + + $this->mock_sql + ->whenCalled('fetchAll') + ->with("select id,libelle,id_origine from codif_thesaurus where id_thesaurus like 'PUBL%' and libelle like'%pirate%' order by id_thesaurus limit 100", + false) + ->answers([ + ['id_thesaurus' => 1, + 'id_origine' => 1, + 'id' => 1] + ]) + ->beStrict(); + + $this->dispatch('/admin/ajax/listesuggestion/type_autorite/thesaurus/mode/3/valeur/pirate/id_champ/PUBL', true); + $this->assertXPathContentContains('//div[@class="tag_liste"][@clef="1"]', '1 : pirate', $this->_response->getBody()); + } + + /** @test */ + public function getListeSuggestionForModeIndexStartsWith() { + + $this->mock_sql + ->whenCalled('fetchAll') + ->with("select id,id_thesaurus,libelle,id_origine from codif_thesaurus where id_thesaurus like 'PUBL%' and id_origine like 'pirate%' order by id_thesaurus limit 100", + false) + ->answers([ + ['id_thesaurus' => 1, + 'id_origine' => 1, + 'id' => 1] + ]) + ->beStrict(); + + $this->dispatch('/admin/ajax/listesuggestion/type_autorite/thesaurus/mode/4/valeur/pirate/id_champ/PUBL', true); + $this->assertXPathContentContains('//div[@class="tag_liste"][@clef="1"]', '1 : pirate', $this->_response->getBody()); + } + + } ?> \ No newline at end of file