diff --git a/VERSIONS_WIP/56205 b/VERSIONS_WIP/56205 new file mode 100644 index 0000000000000000000000000000000000000000..c6af35426e8ca648e3ffc06d6853a024d075805c --- /dev/null +++ b/VERSIONS_WIP/56205 @@ -0,0 +1 @@ + - ticket #56205 : Résultat de recherche : les facettes sont surfiltrées par les paramètres de filtre des données du profil. \ No newline at end of file diff --git a/library/Class/CriteresRecherche.php b/library/Class/CriteresRecherche.php index d5caa04df504fef07c41f3497e4fe63010193fff..711bbc20148b6f18dd096df6298027a3b60e9a33 100644 --- a/library/Class/CriteresRecherche.php +++ b/library/Class/CriteresRecherche.php @@ -158,7 +158,7 @@ class Class_CriteresRecherche { $profil_param => $profil_param]); ksort($options); return $options; -} + } /** @@ -213,8 +213,9 @@ class Class_CriteresRecherche { $facettes = explode('-', $this->getParam('facettes', '')); $facettes[] = $this->getParam('facette', ''); - if ($this->_profil && ($selection_bib = $this->_profil->getIdSite())) - $facettes[]= 'B' . $selection_bib; + if ($this->_profil + && ($selection_bib = $this->_profil->getIdSite())) + $facettes[] = 'B' . $selection_bib; return array_unique(array_filter($facettes, [$this->getValidateFacette(), @@ -252,23 +253,22 @@ class Class_CriteresRecherche { public function getFiltres() { - $filtres = [ - $this->selectionToArray('G', $this->getGenre()), + $filtres = [ 'G' => $this->selectionToArray('G', $this->getGenre()), ]; if (!$this->isRecherchePanier() && !$this->isRechercheCatalogueEmpty() && $this->_profil) { $filtres = array_merge($filtres, [ - $this->selectionToArray('T', $this->_profil->getSelTypeDoc()), - $this->selectionToArray('Y', $this->_profil->getSelAnnexe()), - $this->selectionToArray('S', $this->_profil->getSelSection()), - ]); + 'T' => $this->selectionToArray('T', $this->_profil->getSelTypeDoc()), + 'Y' => $this->selectionToArray('Y', $this->_profil->getSelAnnexe()), + 'S' => $this->selectionToArray('S', $this->_profil->getSelSection()), + ]); } $filtres_array = array_filter(explode('-',$this->getParam('filtres',null))); foreach ($filtres_array as $filtre_str) { $filtres[]= array_filter(explode(';',$filtre_str), - [$this, 'validCodeOrNull']); + [$this, 'validCodeOrNull']); } if ($bibs=$this->getBibs()) { @@ -277,7 +277,7 @@ class Class_CriteresRecherche { foreach ($bibs_array as $bib) { $bib_ids[]= 'B'.$bib; } - $filtres[]=$bib_ids; + $filtres['B']=$bib_ids; } if ($geo_zone=$this->getGeoZone()) { @@ -286,12 +286,12 @@ class Class_CriteresRecherche { foreach ($all_bibs as $bib) { $bib_ids[]='B'.$bib->getId(); } - $filtres[]=$bib_ids; + $filtres['B'] = $bib_ids; } if (($geo_bib = $this->getGeoBib()) && $bib = Class_Bib::find((int)$geo_bib)) { - $filtres[] = ['B'.$bib->getId()]; + $filtres['B'] = ['B'.$bib->getId()]; } return array_filter($filtres); diff --git a/library/Class/MoteurRecherche/Facettes.php b/library/Class/MoteurRecherche/Facettes.php index aa3766189ab73051819d554cdd8aaf9e8c5ad3d2..a1de945fac3a970802fff1e78a390a2bd5d20fce 100644 --- a/library/Class/MoteurRecherche/Facettes.php +++ b/library/Class/MoteurRecherche/Facettes.php @@ -22,37 +22,43 @@ class Class_MoteurRecherche_Facettes { protected $_facettes_par_cle = [], - $_nb_facettes_par_cle = []; + $_nb_facettes_par_cle = [], + $_criterias, + $_preferences, + $_codes_count; + public function buildFacetsData($preferences, $facets, $criterias) { - $codes_count = $this->fetchFacetsRows($facets); - return ['facettes' => $this->_getFacets($preferences, $codes_count), - 'tags' => $this->_getTags($preferences, $codes_count), - 'suggests' => $this->extractSuggests($codes_count, $criterias, $preferences), - 'bookmarks' => $this->extractBookmarks($codes_count, $criterias, $preferences)]; + $this->_preferences = $preferences; + $this->_criterias = $criterias; + $this->_codes_count = $this->fetchFacetsRows($facets); + + return ['facettes' => $this->_getFacets(), + 'tags' => $this->_getTags(), + 'suggests' => $this->_extractSuggests(), + 'bookmarks' => $this->_extractBookmarks()]; } - protected function _getFacets($prefs, $codes_count) { - if (!$prefs['facettes_actif']) + protected function _getFacets() { + if (!$this->_preferences['facettes_actif']) return []; + $facet_separator = preg_match('/;/', $this->_preferences['facettes_codes']) ? ';' : ''; + $facet_codes = 'T' . $facet_separator . $this->_preferences['facettes_codes']; - $facet_separator = preg_match('/;/', $prefs['facettes_codes']) ? ';' : ''; - $facet_codes = 'T' . $facet_separator . $prefs['facettes_codes']; - - return $this->groupAndOrderFacets($codes_count, + return $this->groupAndOrderFacets($this->_codes_count, $this->_sanitizeCodes($facet_codes), - $prefs['facettes_nombre'] * 2); + $this->_preferences['facettes_nombre'] * 2); } - protected function _getTags($prefs, $codes_count) { - return !$prefs['tags_actif'] ? - [] : $this->extractTags($this->_sanitizeCodes($prefs['tags_codes']), - $prefs['tags_nombre'], - $codes_count); + protected function _getTags() { + return !$this->_preferences['tags_actif'] ? + [] : $this->extractTags($this->_sanitizeCodes($this->_preferences['tags_codes']), + $this->_preferences['tags_nombre'], + $this->_codes_count); } @@ -71,15 +77,32 @@ class Class_MoteurRecherche_Facettes { public function fetchFacetsRows($codes) { $all = array_count_values( - array_filter( - explode(' ', - implode(' ', - $codes)))); + array_filter( + explode(' ', + implode(' ', + $codes)))); + $all = $this->_filterByProfil($all); arsort($all); return $all; } + protected function _filterByProfil($facets) { + $filters = $this->_criterias->getFiltres(); + + foreach($facets as $facet => $count) { + $code = Class_Notice_Facette::extractCodeRubrique($facet); + if(!in_array($code, array_keys($filters))) + continue; + + if(!in_array($facet, $filters[$code])) + unset($facets[$facet]); + } + + return $facets; + } + + public function groupAndOrderFacets($codes, $rubriques, $nombre) { $facets = []; @@ -133,18 +156,18 @@ class Class_MoteurRecherche_Facettes { } - public function extractSuggests($codes_count, $criteres, $preferences) { - if (!$preferences['suggests_enabled']) + protected function _extractSuggests() { + if (!$this->_preferences['suggests_enabled']) return []; $codification = Class_Codification::getInstance(); $matches = []; $ix = new Class_Indexation(); - if (!$search_words = $ix->getMots($criteres->getExpressionRecherche())) + if (!$search_words = $ix->getMots($this->_criterias->getExpressionRecherche())) return []; - foreach($codes_count as $code => $count) { + foreach($this->_codes_count as $code => $count) { $facet = Class_Notice_Facette::find($code); if (!$facet_label = $facet->getLibelleFacette()) @@ -158,7 +181,7 @@ class Class_MoteurRecherche_Facettes { $matches[] = ['id' => $code, 'label' => $facet_label . ' (' . $facet->getCodeRubriqueLibelle() . ')']; - if (count($matches) >= $preferences['suggests_number']) + if (count($matches) >= $this->_preferences['suggests_number']) break; } @@ -166,8 +189,8 @@ class Class_MoteurRecherche_Facettes { } - public function extractBookmarks($codes_count, $criteres, $preferences) { - if (!$preferences['bookmarks_enabled']) + protected function _extractBookmarks() { + if (!$this->_preferences['bookmarks_enabled']) return []; if(!$user = Class_Users::getIdentity()) @@ -186,7 +209,7 @@ class Class_MoteurRecherche_Facettes { $user_bookmarks); foreach($user_bookmarks_codes as $bookmark_code) { - if(!isset($codes_count[$bookmark_code])) + if(!isset($this->_codes_count[$bookmark_code])) continue; if(!$facet = Class_Notice_Facette::find($bookmark_code)) @@ -199,13 +222,13 @@ class Class_MoteurRecherche_Facettes { 'label' => sprintf('%s : %s (%d)', $facet->getCodeRubriqueLibelle(), $facet_label, - $codes_count[$bookmark_code])]; + $this->_codes_count[$bookmark_code])]; } usort($matches, function($first, $second) - { - return strcmp($first['label'], $second['label']); - }); + { + return strcmp($first['label'], $second['label']); + }); return $matches; } diff --git a/tests/application/modules/opac/controllers/NoticeAjaxControllerItemsTest.php b/tests/application/modules/opac/controllers/NoticeAjaxControllerItemsTest.php index 9a8a84c39c30b5aa37c925904c96a80383c1fc22..686b630c532cda4aedd7319657de9cc5ed384a03 100644 --- a/tests/application/modules/opac/controllers/NoticeAjaxControllerItemsTest.php +++ b/tests/application/modules/opac/controllers/NoticeAjaxControllerItemsTest.php @@ -36,7 +36,9 @@ abstract class NoticeAjaxControllerItemsTestCase extends AbstractControllerTestC ['id' => 12, 'id_bib' => 3, 'id_int_bib' => 3, + 'section' => 14, 'cote' => 'A', + 'annexe' => 87, 'dispo' => 'Non', 'zone995' => serialize([['code' => '9', 'valeur' => '12']])]); @@ -184,4 +186,48 @@ class NoticeAjaxControllerItemsWithOrder995Dollar9DescTest $this->assertXPathContentOrder('//td[@class="cote"]', ['B', 'A', 'C']); } +} + + + + +class NoticeAjaxControllerItemsFiltredByProfilSectionTest extends NoticeAjaxControllerItemsTestCase { + protected function _prepareFixtures() { + Class_Profil::getCurrentProfil() + ->setSelSection('14;2'); + } + + + /** @test */ + public function itemAShouldBePresent() { + $this->assertXPathContentContains('//td[@class="cote"]', 'A'); + } + + + /** @test */ + public function itemBShouldNotBePresent() { + $this->assertNotXPathContentContains('//td[@class="cote"]', 'B'); + } +} + + + + +class NoticeAjaxControllerItemsFiltredByProfilAnnexTest extends NoticeAjaxControllerItemsTestCase { + protected function _prepareFixtures() { + Class_Profil::getCurrentProfil() + ->setSelAnnexe('87'); + } + + + /** @test */ + public function itemAShouldBePresent() { + $this->assertXPathContentContains('//td[@class="cote"]', 'A'); + } + + + /** @test */ + public function itemBShouldNotBePresent() { + $this->assertNotXPathContentContains('//td[@class="cote"]', 'B'); + } } \ No newline at end of file diff --git a/tests/scenarios/SearchResult/SearchResultTest.php b/tests/scenarios/SearchResult/SearchResultTest.php index 5da58fd0b92044139789b21e49200cbbc3042bbd..c0a3b390dee17192583235351dc569221ccd18ca 100644 --- a/tests/scenarios/SearchResult/SearchResultTest.php +++ b/tests/scenarios/SearchResult/SearchResultTest.php @@ -20,7 +20,7 @@ */ -class SearchResultTest extends AbstractControllerTestCase { +class SearchResultHeaderTest extends AbstractControllerTestCase { protected $_storm_default_to_volatile = true; @@ -85,4 +85,35 @@ class SearchResultTest extends AbstractControllerTestCase { public function domainSelectorShouldBeAnAutoCompleteSelector() { $this->assertXPath('//form//input[@name="rech_collection"]'); } +} + + + + +class SearchResultFilterFromProfilTest extends AbstractControllerTestCase { + protected $_storm_default_to_volatile = true; + + + public function setUp() { + parent::setUp(); + $profil = Class_Profil::getCurrentProfil(); + $profil->setSelTypeDoc(Class_TypeDoc::PERIODIQUE); + + $record = $this->fixture('Class_Notice', ['id' => 1]); + + $sql = $this->mock() + ->whenCalled('fetchAll') + ->with("select id_notice, facettes from notices Where MATCH(titres, auteurs, editeur, collection, matieres, dewey) AGAINST('+(TROLL TROLLS TROL) +(TROY TROYS TROI)' IN BOOLEAN MODE) and MATCH(facettes) AGAINST('+(T2)' IN BOOLEAN MODE) order by (MATCH(alpha_titre) AGAINST(' TROLLS TROY') * 1.5) + (MATCH(alpha_auteur) AGAINST(' TROLLS TROY')) desc", true, false) + ->answers([ ['1' , 'T1 T2']]); + + Zend_Registry::set('sql', $sql); + + $this->dispatch('/opac/recherche/trolls+de+troy', true); + } + + + /** @test */ + public function facetShouldNotContainsDocTypeBook() { + $this->assertNotXpath('//li[@class="facette"]//input[@name="multifacet_T1"]'); + } } \ No newline at end of file