diff --git a/includes/handlers.inc b/includes/handlers.inc index 771c3612258..8ae09f0d0b6 100644 --- a/includes/handlers.inc +++ b/includes/handlers.inc @@ -1073,12 +1073,17 @@ function views_break_phrase_string($str, &$handler = NULL) { return $handler; } - if (preg_match('/^(\w+[+ ])+\w+$/', $str)) { - // The '+' character in a query string may be parsed as ' '. + // Determine if the string has 'or' operators (plus signs) or 'and' operators + // (commas) and split the string accordingly. If we have an 'and' operator, + // spaces are treated as part of the word being split, but otherwise they are + // treated the same as a plus sign. + $or_wildcard = '[^\s+,]'; + $and_wildcard = '[^+,]'; + if (preg_match("/^({$or_wildcard}+[+ ])+{$or_wildcard}+$/", $str)) { $handler->operator = 'or'; $handler->value = preg_split('/[+ ]/', $str); } - else if (preg_match('/^((\w|\s)+,)*(\w|\s)+$/', $str)) { + elseif (preg_match("/^({$and_wildcard}+,)*{$and_wildcard}+$/", $str)) { $handler->operator = 'and'; $handler->value = explode(',', $str); } diff --git a/lib/Drupal/views/Tests/HandlersTest.php b/lib/Drupal/views/Tests/HandlersTest.php index 610fd62b199..b44ce0cfda9 100644 --- a/lib/Drupal/views/Tests/HandlersTest.php +++ b/lib/Drupal/views/Tests/HandlersTest.php @@ -59,23 +59,42 @@ class HandlersTest extends ViewsSqlTest { $this->assertEqual($handler, views_break_phrase_string('', $handler)); // test ors - $this->assertEqualValue(array('word1', 'word2', 'word'), views_break_phrase_string('word1 word2+word', $handler)); + $handler = views_break_phrase_string('word1 word2+word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqual('or', $handler->operator); - $this->assertEqualValue(array('word1', 'word2', 'word'), views_break_phrase_string('word1+word2+word', $handler)); + $handler = views_break_phrase_string('word1+word2+word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqual('or', $handler->operator); - $this->assertEqualValue(array('word1', 'word2', 'word'), views_break_phrase_string('word1 word2 word', $handler)); + $handler = views_break_phrase_string('word1 word2 word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqual('or', $handler->operator); - $this->assertEqualValue(array('word1', 'word2', 'word'), views_break_phrase_string('word1 word2++word', $handler)); + $handler = views_break_phrase_string('word-1+word-2+word'); + $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler); + $this->assertEqual('or', $handler->operator); + $handler = views_break_phrase_string('wõrd1+wõrd2+wõrd'); + $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler); $this->assertEqual('or', $handler->operator); // test ands. - $this->assertEqualValue(array('word1', 'word2', 'word'), views_break_phrase_string('word1,word2,word', $handler)); + $handler = views_break_phrase_string('word1,word2,word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqual('and', $handler->operator); - $this->assertEqualValue(array('word1', 'word2', 'word'), views_break_phrase_string('word1,,word2,word', $handler)); + $handler = views_break_phrase_string('word1 word2,word'); + $this->assertEqualValue(array('word1 word2', 'word'), $handler); $this->assertEqual('and', $handler->operator); - $this->assertEqualValue(array('word1 word2', 'word'), views_break_phrase_string('word1 word2,word', $handler)); + $handler = views_break_phrase_string('word1,word2 word'); + $this->assertEqualValue(array('word1', 'word2 word'), $handler); $this->assertEqual('and', $handler->operator); - $this->assertEqualValue(array('word1', 'word2 word'), views_break_phrase_string('word1,word2 word', $handler)); + $handler = views_break_phrase_string('word-1,word-2,word'); + $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler); + $this->assertEqual('and', $handler->operator); + $handler = views_break_phrase_string('wõrd1,wõrd2,wõrd'); + $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler); + $this->assertEqual('and', $handler->operator); + + // test a single word + $handler = views_break_phrase_string('word'); + $this->assertEqualValue(array('word'), $handler); $this->assertEqual('and', $handler->operator); }