Skip to content

Commit

Permalink
Prevent multiple quotations of the same word
Browse files Browse the repository at this point in the history
  • Loading branch information
denchev committed Oct 23, 2020
1 parent a1e5689 commit b21693a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/code/Magento/Search/Model/SynonymAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private function getSearchPattern(array $words): string
$patterns = [];
for ($lastItem = count($words); $lastItem > 0; $lastItem--) {
$words = array_map(function ($word) {
return preg_quote($word, '/');
return preg_quote(stripslashes($word), '/');
}, $words);
$phrase = implode("\s+", \array_slice($words, 0, $lastItem));
$patterns[] = '^' . $phrase . ',';
Expand Down
38 changes: 38 additions & 0 deletions app/code/Magento/Search/Test/Unit/Model/SynonymAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,42 @@ public function testGetSynonymsForPhraseEmptyPhrase()
$actual = $this->synonymAnalyzer->getSynonymsForPhrase($phrase);
$this->assertEquals($expected, $actual);
}

/**
* @test
*
* Phrase that is long and has quotes in it scenario
*/
public function testLongQuotedPhrase()
{
$phrase = 'LSS 3/8"X3/4"X25\' EZ-PULL 1/2" INS SWEAT LINESET W/90 END BEND SUCTION LINE INSULATED';
$expected = [
0 => [ 0 => "LSS" ],
1 => [ 0 => "3/8\"X3/4\"X25'" ],
2 => [ 0 => "EZ-PULL" ],
3 => [ 0 => "1/2\"" ],
4 => [ 0 => "INS" ],
5 => [ 0 => "SWEAT" ],
6 => [ 0 => "LINESET" ],
7 => [ 0 => "W/90" ],
8 => [ 0 => "END" ],
9 => [ 0 => "BEND", 1 => "TWIST" ],
10 => [ 0 => "SUCTION", 1 => "WEIGHT" ],
11 => [ 0 => "LINE" ],
12 => [ 0 => "INSULATED" ]
];
$this->synReaderModel->expects($this->once())
->method('loadByPhrase')
->with($phrase)
->willReturnSelf();
$this->synReaderModel->expects($this->once())
->method('getData')
->willReturn([
['synonyms' => 'BEND,TWIST'],
['synonyms' => 'SUCTION,WEIGHT'],
]);

$actual = $this->synonymAnalyzer->getSynonymsForPhrase($phrase);
$this->assertEquals($expected, $actual);
}
}

0 comments on commit b21693a

Please sign in to comment.