Skip to content
Snippets Groups Projects
Commit 38b52938 authored by Ere Maijala's avatar Ere Maijala Committed by Demian Katz
Browse files

Changed different regexp character escaping methods to preg_quote.

parent d4acf33b
No related merge requests found
......@@ -149,8 +149,9 @@ class Query extends AbstractQuery
*/
public function containsTerm($needle)
{
// Escape slashes in $needle to avoid regular expression errors:
$needle = str_replace('/', '\/', $needle);
// Escape characters with special meaning in regular expressions to avoid
// errors:
$needle = preg_quote($needle);
return (bool)preg_match("/\b$needle\b/u", $this->getString());
}
......@@ -177,7 +178,7 @@ class Query extends AbstractQuery
{
// Escape $from so it is regular expression safe (just in case it
// includes any weird punctuation -- unlikely but possible):
$from = addcslashes($from, '\^$.[]|()?*+{}/');
$from = preg_quote($from);
// If our "from" pattern contains non-word characters, we can't use word
// boundaries for matching. We want to try to use word boundaries when
......
......@@ -49,11 +49,12 @@ class QueryTest extends PHPUnit_Framework_TestCase
*/
public function testContainsTerm()
{
$q = new Query('test query');
$q = new Query('test query we<(ird');
// Should contain both actual terms:
// Should contain all actual terms (even those containing regex chars):
$this->assertTrue($q->containsTerm('test'));
$this->assertTrue($q->containsTerm('query'));
$this->assertTrue($q->containsTerm('we<(ird'));
// Should not contain a non-present term:
$this->assertFalse($q->containsTerm('garbage'));
......@@ -62,6 +63,18 @@ class QueryTest extends PHPUnit_Framework_TestCase
$this->assertFalse($q->containsTerm('tes'));
}
/**
* Test replaceTerm() method
*
* @return void
*/
public function testReplaceTerm()
{
$q = new Query('test query we<(ird');
$q->replaceTerm('we<(ird', 'we>(ird');
$this->assertEquals('test query we>(ird', $q->getString());
}
/**
* Test setHandler() method
*
......
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