Commit 0946b702 authored by alexpott's avatar alexpott

Issue #1792836 by damiankloip, alansaviolobo, Kars-T | tim.plunkett: Merge...

Issue #1792836 by damiankloip, alansaviolobo, Kars-T | tim.plunkett: Merge HandlerBase::breakPhrase() and HandlerBase::breakPhraseString() and clean up.
parent 6f9ceadb
...@@ -73,21 +73,13 @@ public function query($group_by = FALSE) { ...@@ -73,21 +73,13 @@ public function query($group_by = FALSE) {
$this->ensureMyTable(); $this->ensureMyTable();
if (!empty($this->options['break_phrase'])) { if (!empty($this->options['break_phrase'])) {
$tids = new \stdClass(); $break = static::breakString($this->argument);
$tids->value = $this->argument; if ($break->value === array(-1)) {
$tids = $this->breakPhrase($this->argument, $tids);
if ($tids->value == array(-1)) {
return FALSE; return FALSE;
} }
if (count($tids->value) > 1) { $operator = (count($break->value) > 1) ? 'IN' : '=';
$operator = 'IN'; $tids = $break->value;
}
else {
$operator = '=';
}
$tids = $tids->value;
} }
else { else {
$operator = "="; $operator = "=";
......
...@@ -50,6 +50,11 @@ public function testArgumentTitle() { ...@@ -50,6 +50,11 @@ public function testArgumentTitle() {
$this->executeView($view, array($account->id() . ',0')); $this->executeView($view, array($account->id() . ',0'));
$this->assertEqual($view->getTitle(), $account->label() . ', ' . $anonymous); $this->assertEqual($view->getTitle(), $account->label() . ', ' . $anonymous);
$view->destroy(); $view->destroy();
$view->getDisplay()->getHandler('argument', 'uid')->options['break_phrase'] = TRUE;
$this->executeView($view, array('0,' . $account->id()));
$this->assertEqual($view->getTitle(), $anonymous . ', ' . $account->label());
$view->destroy();
} }
} }
...@@ -772,117 +772,42 @@ public function getEntityType() { ...@@ -772,117 +772,42 @@ public function getEntityType() {
} }
/** /**
* Breaks x,y,z and x+y+z into an array. Numeric only. * Breaks x,y,z and x+y+z into an array.
* *
* @param string $str * @param string $str
* The string to parse. * The string to split.
* @param \Drupal\views\Plugin\views\HandlerBase|null $handler * @param bool $force_int
* The handler object to use as a base. If not specified one will * Enforce a numeric check.
* be created.
* *
* @return \Drupal\views\Plugin\views\HandlerBase|stdClass $handler * @return \stdClass
* The new handler object. * A stdClass object containing value and operator properties.
*/ */
public static function breakPhrase($str, &$handler = NULL) { public static function breakString($str, $force_int = FALSE) {
if (!$handler) { $operator = NULL;
$handler = new \stdClass(); $value = array();
}
// Set up defaults:
if (!isset($handler->value)) {
$handler->value = array();
}
if (!isset($handler->operator)) {
$handler->operator = 'or';
}
if (empty($str)) {
return $handler;
}
if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) { // Determine if the string has 'or' operators (plus signs) or 'and'
// operators (commas) and split the string accordingly.
if (preg_match('/^([\w0-9-_]+[+ ]+)+[\w0-9-_]+$/u', $str)) {
// The '+' character in a query string may be parsed as ' '. // The '+' character in a query string may be parsed as ' '.
$handler->operator = 'or'; $operator = 'or';
$handler->value = preg_split('/[+ ]/', $str); $value = preg_split('/[+ ]/', $str);
}
elseif (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) {
$handler->operator = 'and';
$handler->value = explode(',', $str);
}
// Keep an 'error' value if invalid strings were given.
if (!empty($str) && (empty($handler->value) || !is_array($handler->value))) {
$handler->value = array(-1);
return $handler;
}
// Doubly ensure that all values are numeric only.
foreach ($handler->value as $id => $value) {
$handler->value[$id] = intval($value);
}
return $handler;
}
/**
* Breaks x,y,z and x+y+z into an array. Works for strings.
*
* @param string $str
* The string to parse.
* @param \Drupal\views\Plugin\views\HandlerBase|null $handler
* The object to use as a base. If not specified one will
* be created.
*
* @return \Drupal\views\Plugin\views\HandlerBase|stdClass $handler
* The new handler object.
*/
public static function breakPhraseString($str, &$handler = NULL) {
if (!$handler) {
$handler = new \stdClass();
}
// Set up defaults:
if (!isset($handler->value)) {
$handler->value = array();
} }
elseif (preg_match('/^([\w0-9-_]+[, ]+)*[\w0-9-_]+$/u', $str)) {
if (!isset($handler->operator)) { $operator = 'and';
$handler->operator = 'or'; $value = explode(',', $str);
}
if ($str == '') {
return $handler;
}
// 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);
}
elseif (preg_match("/^({$and_wildcard}+,)*{$and_wildcard}+$/", $str)) {
$handler->operator = 'and';
$handler->value = explode(',', $str);
} }
// Keep an 'error' value if invalid strings were given. // Filter any empty matches (Like from '++' in a string) and reset the
if (!empty($str) && (empty($handler->value) || !is_array($handler->value))) { // array keys. 'strlen' is used as the filter callback so we do not lose
$handler->value = array(-1); // 0 values (would otherwise evaluate == FALSE).
return $handler; $value = array_values(array_filter($value, 'strlen'));
}
// Doubly ensure that all values are strings only. if ($force_int) {
foreach ($handler->value as $id => $value) { $value = array_map('intval', $value);
$handler->value[$id] = (string) $value;
} }
return $handler; return (object) array('value' => $value, 'operator' => $operator);
} }
/** /**
...@@ -966,5 +891,4 @@ public function submitTemporaryForm($form, FormStateInterface $form_state) { ...@@ -966,5 +891,4 @@ public function submitTemporaryForm($form, FormStateInterface $form_state) {
// Write to cache // Write to cache
$form_state['view']->cacheSet(); $form_state['view']->cacheSet();
} }
} }
...@@ -1151,6 +1151,18 @@ public static function encodeValidatorId($id) { ...@@ -1151,6 +1151,18 @@ public static function encodeValidatorId($id) {
public static function decodeValidatorId($id) { public static function decodeValidatorId($id) {
return str_replace('---', ':', $id); return str_replace('---', ':', $id);
} }
/**
* Splits an argument into value and operator properties on this instance.
*
* @param bool $force_int
* Enforce that values should be numeric.
*/
protected function unpackArgumentValue($force_int = FALSE) {
$break = static::breakString($this->argument, $force_int);
$this->value = $break->value;
$this->operator = $break->operator;
}
} }
/** /**
......
...@@ -120,12 +120,8 @@ public function query($group_by = FALSE) { ...@@ -120,12 +120,8 @@ public function query($group_by = FALSE) {
} }
if (!empty($this->options['break_phrase'])) { if (!empty($this->options['break_phrase'])) {
if (!empty($this->definition['numeric'])) { $force_int = !empty($this->definition['numeric']);
$this->breakPhrase($this->argument, $this); $this->unpackArgumentValue($force_int);
}
else {
$this->breakPhraseString($this->argument, $this);
}
} }
else { else {
$this->value = array($this->argument); $this->value = array($this->argument);
...@@ -141,7 +137,8 @@ function title() { ...@@ -141,7 +137,8 @@ function title() {
} }
if (!empty($this->options['break_phrase'])) { if (!empty($this->options['break_phrase'])) {
$this->breakPhrase($this->argument, $this); $force_int = !empty($this->definition['numeric']);
$this->unpackArgumentValue($force_int);
} }
else { else {
$this->value = array($this->argument); $this->value = array($this->argument);
......
...@@ -67,7 +67,9 @@ function title() { ...@@ -67,7 +67,9 @@ function title() {
} }
if (!empty($this->options['break_phrase'])) { if (!empty($this->options['break_phrase'])) {
$this->breakPhrase($this->argument, $this); $break = static::breakString($this->argument, TRUE);
$this->value = $break->value;
$this->operator = $break->operator;
} }
else { else {
$this->value = array($this->argument); $this->value = array($this->argument);
...@@ -98,7 +100,9 @@ public function query($group_by = FALSE) { ...@@ -98,7 +100,9 @@ public function query($group_by = FALSE) {
$this->ensureMyTable(); $this->ensureMyTable();
if (!empty($this->options['break_phrase'])) { if (!empty($this->options['break_phrase'])) {
$this->breakPhrase($this->argument, $this); $break = static::breakString($this->argument, TRUE);
$this->value = $break->value;
$this->operator = $break->operator;
} }
else { else {
$this->value = array($this->argument); $this->value = array($this->argument);
......
...@@ -189,7 +189,7 @@ public function query($group_by = FALSE) { ...@@ -189,7 +189,7 @@ public function query($group_by = FALSE) {
} }
if (!empty($this->options['break_phrase'])) { if (!empty($this->options['break_phrase'])) {
$this->breakPhraseString($argument, $this); $this->unpackArgumentValue();
} }
else { else {
$this->value = array($argument); $this->value = array($argument);
...@@ -263,7 +263,7 @@ function title() { ...@@ -263,7 +263,7 @@ function title() {
} }
if (!empty($this->options['break_phrase'])) { if (!empty($this->options['break_phrase'])) {
$this->breakPhraseString($this->argument, $this); $this->unpackArgumentValue();
} }
else { else {
$this->value = array($this->argument); $this->value = array($this->argument);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/** /**
* @file * @file
* Definition of Drupal\views\Tests\Handler\HandlerTest. * Contains \Drupal\views\Tests\Handler\HandlerTest.
*/ */
namespace Drupal\views\Tests\Handler; namespace Drupal\views\Tests\Handler;
...@@ -85,102 +85,99 @@ function testFilterInOperatorUi() { ...@@ -85,102 +85,99 @@ function testFilterInOperatorUi() {
} }
/** /**
* Tests the breakPhraseString() method. * Tests the breakString method.
*/ */
function testBreakPhraseString() { public function testBreakString() {
$empty_stdclass = new \stdClass(); // Check defaults.
$empty_stdclass->operator = 'or'; $this->assertEqual((object) array('value' => array(), 'operator' => NULL), HandlerBase::breakString(''));
$empty_stdclass->value = array();
// check defaults
$null = NULL;
$this->assertEqual($empty_stdclass, HandlerBase::breakPhraseString('', $null));
$item = array(
'table' => 'node',
'field' => 'title',
);
$handler = $this->container->get('plugin.manager.views.argument')->getHandler($item);
$this->assertEqual($handler, HandlerBase::breakPhraseString('', $handler), 'The breakPhraseString() method works correctly.');
// test ors // Test ors
$handler = HandlerBase::breakPhraseString('word1 word2+word'); $handler = HandlerBase::breakString('word1 word2+word');
$this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
$this->assertEqual('or', $handler->operator); $this->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakPhraseString('word1+word2+word'); $handler = HandlerBase::breakString('word1+word2+word');
$this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
$this->assertEqual('or', $handler->operator); $this->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakPhraseString('word1 word2 word'); $handler = HandlerBase::breakString('word1 word2 word');
$this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
$this->assertEqual('or', $handler->operator); $this->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakPhraseString('word-1+word-2+word'); $handler = HandlerBase::breakString('word-1+word-2+word');
$this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler); $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler);
$this->assertEqual('or', $handler->operator); $this->assertEqual('or', $handler->operator);
$handler = HandlerBase::breakPhraseString('wõrd1+wõrd2+wõrd'); $handler = HandlerBase::breakString('wõrd1+wõrd2+wõrd');
$this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler); $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler);
$this->assertEqual('or', $handler->operator); $this->assertEqual('or', $handler->operator);
// test ands. // Test ands.
$handler = HandlerBase::breakPhraseString('word1,word2,word'); $handler = HandlerBase::breakString('word1,word2,word');
$this->assertEqualValue(array('word1', 'word2', 'word'), $handler); $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
$this->assertEqual('and', $handler->operator); $this->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakPhraseString('word1 word2,word'); $handler = HandlerBase::breakString('word1 word2,word');
$this->assertEqualValue(array('word1 word2', 'word'), $handler); $this->assertEqualValue(array('word1 word2', 'word'), $handler);
$this->assertEqual('and', $handler->operator); $this->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakPhraseString('word1,word2 word'); $handler = HandlerBase::breakString('word1,word2 word');
$this->assertEqualValue(array('word1', 'word2 word'), $handler); $this->assertEqualValue(array('word1', 'word2 word'), $handler);
$this->assertEqual('and', $handler->operator); $this->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakPhraseString('word-1,word-2,word'); $handler = HandlerBase::breakString('word-1,word-2,word');
$this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler); $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler);
$this->assertEqual('and', $handler->operator); $this->assertEqual('and', $handler->operator);
$handler = HandlerBase::breakPhraseString('wõrd1,wõrd2,wõrd'); $handler = HandlerBase::breakString('wõrd1,wõrd2,wõrd');
$this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler); $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler);
$this->assertEqual('and', $handler->operator); $this->assertEqual('and', $handler->operator);
// test a single word // Test a single word
$handler = HandlerBase::breakPhraseString('word'); $handler = HandlerBase::breakString('word');
$this->assertEqualValue(array('word'), $handler); $this->assertEqualValue(array('word'), $handler);
$this->assertEqual('and', $handler->operator); $this->assertEqual('and', $handler->operator);
}
/**
* Tests Drupal\views\Plugin\views\HandlerBase::breakPhrase() function.
*/
function testBreakPhrase() {
$empty_stdclass = new \stdClass();
$empty_stdclass->operator = 'or';
$empty_stdclass->value = array();
$null = NULL;
// check defaults
$this->assertEqual($empty_stdclass, HandlerBase::breakPhrase('', $null));
$item = array(
'table' => 'node',
'field' => 'title',
);
$handler = $this->container->get('plugin.manager.views.argument')->getHandler($item);
$this->assertEqual($handler, HandlerBase::breakPhrase('', $handler), 'The breakPhrase() method works correctly.');
$s1 = $this->randomMachineName();
// Generate three random numbers which can be used below; // Generate three random numbers which can be used below;
$n1 = rand(0, 100); $n1 = rand(0, 100);
$n2 = rand(0, 100); $n2 = rand(0, 100);
$n3 = rand(0, 100); $n3 = rand(0, 100);
// test ors
$this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1 $n2+$n3", $handler));
$this->assertEqual('or', $handler->operator);
$this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1+$n2+$n3", $handler));
$this->assertEqual('or', $handler->operator);
$this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1 $n2 $n3", $handler));
$this->assertEqual('or', $handler->operator);
$this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1 $n2++$n3", $handler));
$this->assertEqual('or', $handler->operator);
// test ands. // Test "or"s.
$this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1,$n2,$n3", $handler)); $handlerBase = HandlerBase::breakString("$s1 $n2+$n3");
$this->assertEqual('and', $handler->operator); $this->assertEqualValue(array($s1, $n2, $n3), $handlerBase);
$this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1,,$n2,$n3", $handler)); $this->assertEqual('or', $handlerBase->operator);
$this->assertEqual('and', $handler->operator);
$handlerBase = HandlerBase::breakString("$s1+$n2+$n3");
$this->assertEqualValue(array($s1, $n2, $n3), $handlerBase);
$this->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("$s1 $n2 $n3");
$this->assertEqualValue(array($s1, $n2, $n3), $handlerBase);
$this->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("$s1 $n2++$n3");
$this->assertEqualValue(array($s1, $n2, $n3), $handlerBase);
$this->assertEqual('or', $handlerBase->operator);
// Test "and"s.
$handlerBase = HandlerBase::breakString("$s1,$n2,$n3");
$this->assertEqualValue(array($s1, $n2, $n3), $handlerBase);
$this->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("$s1,,$n2,$n3");
$this->assertEqualValue(array($s1, $n2, $n3), $handlerBase);
$this->assertEqual('and', $handlerBase->operator);
// Enforce int values.
$handlerBase = HandlerBase::breakString("$n1,$n2,$n3", TRUE);
$this->assertEqualValue(array($n1, $n2, $n3), $handlerBase);
$this->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("$n1+$n2+$n3", TRUE);
$this->assertEqualValue(array($n1, $n2, $n3), $handlerBase);
$this->assertEqual('or', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("$s1,$n2,$n3", TRUE);
$this->assertEqualValue(array((int) $s1, $n2, $n3), $handlerBase);
$this->assertEqual('and', $handlerBase->operator);
$handlerBase = HandlerBase::breakString("$s1+$n2+$n3", TRUE);
$this->assertEqualValue(array((int) $s1, $n2, $n3), $handlerBase);
$this->assertEqual('or', $handlerBase->operator);
} }
/** /**
...@@ -214,8 +211,8 @@ public function testHandlerWeights() { ...@@ -214,8 +211,8 @@ public function testHandlerWeights() {
/** /**
* Check to see if a value is the same as the value on a certain handler. * Check to see if a value is the same as the value on a certain handler.
* *
* @param $first * @param $expected
* The first value to check. * The expected value to check.
* @param \Drupal\views\Plugin\views\HandlerBase $handler * @param \Drupal\views\Plugin\views\HandlerBase $handler
* The handler that has the $handler->value property to compare with first. * The handler that has the $handler->value property to compare with first.
* @param string $message * @param string $message
...@@ -226,8 +223,12 @@ public function testHandlerWeights() { ...@@ -226,8 +223,12 @@ public function testHandlerWeights() {
* @return bool * @return bool
* TRUE if the assertion succeeded, FALSE otherwise. * TRUE if the assertion succeeded, FALSE otherwise.
*/ */
protected function assertEqualValue($first, $handler, $message = '', $group = 'Other') { protected function assertEqualValue($expected, $handler, $message = '', $group = 'Other') {
return $this->assert($first == $handler->value, $message ? $message : t('First value is equal to second value'), $group); if (empty($message)) {
$message = t('Comparing @first and @second', array('@first' => implode(',', $expected), '@second' => implode(',', $handler->value)));
}
return $this->assert($expected == $handler->value, $message, $group);
} }
/** /**
......
Markdown is supported
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