diff --git a/VERSIONS_HOTLINE/127919 b/VERSIONS_HOTLINE/127919 new file mode 100644 index 0000000000000000000000000000000000000000..a553b357c4ae486d03984ec1153aa1895e02f67b --- /dev/null +++ b/VERSIONS_HOTLINE/127919 @@ -0,0 +1 @@ + - ticket #127919 : Résultat de recherche : Correction d'une erreur possible lors du surlignage des termes recherchés \ No newline at end of file diff --git a/library/Class/MoteurRecherche/Terms.php b/library/Class/MoteurRecherche/Terms.php index 814d48ffce1730fcdc5ba43766302f4629a84cf5..97c5d6270a5de6ce711b9ba8e23f2ee447c2d485 100644 --- a/library/Class/MoteurRecherche/Terms.php +++ b/library/Class/MoteurRecherche/Terms.php @@ -76,8 +76,9 @@ class Class_MoteurRecherche_Terms { return $search_phonetixs; } + protected function _injectRecordsPhonetix($record, $phonetixs) { - $words = preg_split('/[\s\-,\.\;\b\']+/', + $words = preg_split('/\W+/u', mb_strtolower($record->getTitrePrincipal(' '). ' ' . $record->getAuteurPrincipal())); diff --git a/library/ZendAfi/View/Helper/Highlight.php b/library/ZendAfi/View/Helper/Highlight.php index 0b99c3f551526e46d1442c39979c98cb3f290781..dcd525d08cfe0da3c995c32b7e6add2f2719bd4a 100644 --- a/library/ZendAfi/View/Helper/Highlight.php +++ b/library/ZendAfi/View/Helper/Highlight.php @@ -24,7 +24,7 @@ class ZendAfi_View_Helper_Highlight extends ZendAfi_View_Helper_BaseHelper { public function highlight($content, $words) { $patterns = array_map(function($word) { - return '/(^|[^\w])(' . $word . ')($|[^\w])/ius'; + return '/(^|\W)(' . preg_quote($word) . ')($|\W)/ius'; }, $words); diff --git a/tests/library/Class/MoteurRecherche/MatchingTermsTest.php b/tests/library/Class/MoteurRecherche/MatchingTermsTest.php index 8257e494041b8c9058cc9fc9c53ceecac03435e2..7b2476ca7a5cf8514e1e9af87f46a235d435f425 100644 --- a/tests/library/Class/MoteurRecherche/MatchingTermsTest.php +++ b/tests/library/Class/MoteurRecherche/MatchingTermsTest.php @@ -21,90 +21,73 @@ class MoteurRechercheMatchingTermsTest extends ModelTestCase { - protected $_storm_default_to_volatile = true; + protected $_next_record_id = 1; - /** @test */ - public function termsFromRecordsShouldAnswersPelAndMelForms() { - $this->fixture('Class_Album', - ['id' => 2, - 'titre' => 'Pèle mêle', - 'status' => Class_Album::STATUS_VALIDATED])->index(); - - $this->fixture('Class_Album', - ['id' => 3, - 'titre' => 'peler les pommes mélées', - 'status' => Class_Album::STATUS_VALIDATED])->index(); - - $this->assertEquals(['pèle', 'peler', 'mêle','mélées'], - (new Class_MoteurRecherche_Terms()) - ->matchingSearchTermsFromRecords('Pèle mêle', - Class_Notice::findAll())); + protected function _prepareRecord($title, $author='') { + $marc = (new Class_NoticeUnimarc_Fluent) + ->zoneWithChildren('200', ['a' => $title]) + ->zoneWithChildren('700', ['a' => $author]); + + $this->fixture('Class_Notice', + ['id' => $this->_next_record_id++, + 'unimarc' => $marc->render()]); + + return $this; + } + + + protected function assertSearchTermsMatch($search, $match) { + $this + ->assertEquals($match, + (new Class_MoteurRecherche_Terms()) + ->matchingSearchTermsFromRecords($search, Class_Notice::findAll())); } /** @test */ - public function termsFromRecordsShouldAnswersPelWithDashForms() { - $this->fixture('Class_Album', - ['id' => 2, - 'titre' => 'Pêle-mêle', - 'status' => Class_Album::STATUS_VALIDATED])->index(); - - $this->assertEquals(['pêle', 'mêle'], - (new Class_MoteurRecherche_Terms()) - ->matchingSearchTermsFromRecords('Pele mele', - Class_Notice::findAll())); + public function peleMeleSearchShouldMatchPeleMelePelerMeleesRecords() { + $this->_prepareRecord('Pèle mêle') + ->_prepareRecord('peler les pommes mélées') + ->assertSearchTermsMatch('Pèle mêle', + ['pèle', 'peler', 'mêle', 'mélées']); } /** @test */ - public function termsFromRecordsShouldAnswersAuthorsWords() { - $this->fixture('Class_Album', - ['id' => 2, - 'titre' => 'Astérix chez les phonétix', - 'status' => Class_Album::STATUS_VALIDATED]) - ->addAuthor('Goscinny')->index(); - - $this->fixture('Class_Album', - ['id' => 3, - 'titre' => 'peler les pommes mélées', - 'status' => Class_Album::STATUS_VALIDATED])->index(); - - $this->assertEquals(['goscinny'], - (new Class_MoteurRecherche_Terms()) - ->matchingSearchTermsFromRecords('Goscinni', - Class_Notice::findAll())); + public function peleMeleSearchShouldMatchPeleDashMeleRecord() { + $this->_prepareRecord('Pêle-mêle') + ->assertSearchTermsMatch('Pele mele', ['pêle', 'mêle']); } /** @test */ - public function termsFromRecordsWithEteShouldAnswerEte() { - $this->fixture('Class_Album', - ['id' => 2, - 'titre' => 'Été 67', - 'status' => Class_Album::STATUS_VALIDATED]) - ->addAuthor('des étés')->index(); - - $this->assertEquals(['été', 'étés'], - (new Class_MoteurRecherche_Terms()) - ->matchingSearchTermsFromRecords('été', - Class_Notice::findAll())); + public function goscinniSearchShouldMatchGoscinnyAuthor() { + $this->_prepareRecord('Astérix chez les phonétix', 'Goscinny') + ->_prepareRecord('peler les pommes mélées') + ->assertSearchTermsMatch('goscinni', ['goscinny']); } + /** @test */ - public function termsFromRecordsWithEtrangerShouldAnswer() { - $this->fixture('Class_Album', - ['id' => 2, - 'titre' => 'L\'étranger', - 'status' => Class_Album::STATUS_VALIDATED]) - ->addAuthor('Albert camus')->index(); - - $this->assertEquals(['étranger', 'albert','camus'], - (new Class_MoteurRecherche_Terms()) - ->matchingSearchTermsFromRecords('étranger alber camu', - Class_Notice::findAll())); + public function eteSearchShouldMatchTitleAndAuthor() { + $this->_prepareRecord('Été 67', 'des étés') + ->assertSearchTermsMatch('été', ['été', 'étés']); } + /** @test */ + public function etrangerAlberCamuSearchShouldMatchWithPhonetic() { + $this->_prepareRecord('L\'étranger', 'Albert Camus') + ->assertSearchTermsMatch('étranger alber camu', ['étranger', 'albert', 'camus']); + } + + /** + * @test + * @see http://forge.afi-sa.fr/issues/127919 + */ + public function robinHobbSearchShouldMatchWithoutParenthesis() { + $this->_prepareRecord('Le Dieu dans l\'Ombre', 'Lindholm, Megan (Alias Hobb Robin)') + ->assertSearchTermsMatch('robin hobb', ['robin', 'hobb']); + } } -?> \ No newline at end of file diff --git a/tests/library/ZendAfi/View/Helper/HighlightTest.php b/tests/library/ZendAfi/View/Helper/HighlightTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4f0cdbafed133a0bef82a6f33369f81ef3a89b4f --- /dev/null +++ b/tests/library/ZendAfi/View/Helper/HighlightTest.php @@ -0,0 +1,45 @@ +<?php +/** + * Copyright (c) 2012-2021, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class ZendAfi_View_Helper_HighlightTest extends ViewHelperTestCase { + protected + $_storm_default_to_volatile = true, + $_helper; + + + public function setUp() { + parent::setUp(); + $this->_helper = (new ZendAfi_View_Helper_Highlight)->setView($this->view); + } + + + /** + * @test + * @see http://forge.afi-sa.fr/issues/127919 + **/ + public function shouldWorkDespitePregMetaCharacter() { + $this + ->assertEquals('About <span class="highlight">Robin</span> <span class="highlight">Hobb</span> lazyness', + $this->_helper->highlight('About Robin Hobb lazyness', + ['robin', 'robin)', 'hobb'])); + } +}