Skip to content
Snippets Groups Projects
Commit d4d71633 authored by Henri-Damien LAURENT's avatar Henri-Damien LAURENT
Browse files

Merge branch 'hotline#127919_recherche_catalogue' into 'hotline'

hotline #127919 : fix preg patterns and quoting in search results highlighting

See merge request !3911
parents 002914e5 1d31641d
Branches
Tags
1 merge request!3911hotline #127919 : fix preg patterns and quoting in search results highlighting
Pipeline #12758 passed with stage
in 52 minutes and 22 seconds
- ticket #127919 : Résultat de recherche : Correction d'une erreur possible lors du surlignage des termes recherchés
\ No newline at end of file
......@@ -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()));
......
......@@ -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);
......
......@@ -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
<?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']));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment