Issue #2107309 by amateescu: Fixed Case (in)sensitivity for config entity query.

8.0.x
Nathaniel Catchpole 2013-10-11 14:22:31 +01:00
parent 7e644a2995
commit 015bb4300b
4 changed files with 55 additions and 16 deletions

View File

@ -7,6 +7,7 @@
namespace Drupal\Core\Config\Entity\Query; namespace Drupal\Core\Config\Entity\Query;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\Query\ConditionBase; use Drupal\Core\Entity\Query\ConditionBase;
use Drupal\Core\Entity\Query\ConditionInterface; use Drupal\Core\Entity\Query\ConditionInterface;
use Drupal\Core\Entity\Query\QueryException; use Drupal\Core\Entity\Query\QueryException;
@ -33,6 +34,15 @@ class Condition extends ConditionBase {
if (!isset($condition['operator'])) { if (!isset($condition['operator'])) {
$condition['operator'] = is_array($condition['value']) ? 'IN' : '='; $condition['operator'] = is_array($condition['value']) ? 'IN' : '=';
} }
// Lowercase condition value(s) for case-insensitive matches.
if (is_array($condition['value'])) {
$condition['value'] = array_map('Drupal\Component\Utility\Unicode::strtolower', $condition['value']);
}
elseif (!is_bool($condition['value'])) {
$condition['value'] = Unicode::strtolower($condition['value']);
}
$single_conditions[] = $condition; $single_conditions[] = $condition;
} }
} }
@ -150,6 +160,11 @@ class Condition extends ConditionBase {
*/ */
protected function match(array $condition, $value) { protected function match(array $condition, $value) {
if (isset($value)) { if (isset($value)) {
// We always want a case-insensitive match.
if (!is_bool($value)) {
$value = Unicode::strtolower($value);
}
switch ($condition['operator']) { switch ($condition['operator']) {
case '=': case '=':
return $value == $condition['value']; return $value == $condition['value'];

View File

@ -46,9 +46,9 @@ interface QueryInterface extends AlterableInterface {
* @endcode * @endcode
* *
* @param $value * @param $value
* The value for $field. In most cases, this is a scalar. For more complex * The value for $field. In most cases, this is a scalar and it's treated as
* options, it is an array. The meaning of each element in the array is * case-insensitive. For more complex options, it is an array. The meaning
* dependent on $operator. * of each element in the array is dependent on $operator.
* @param $operator * @param $operator
* Possible values: * Possible values:
* - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS', * - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS',

View File

@ -9,6 +9,7 @@ namespace Drupal\system\Form;
use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Config\Entity\ConfigStorageController;
use Drupal\Core\Datetime\Date; use Drupal\Core\Datetime\Date;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\DrupalDateTime;
@ -41,6 +42,13 @@ abstract class DateFormatFormBase extends EntityFormController {
*/ */
protected $dateService; protected $dateService;
/**
* The date format storage controller.
*
* @var \Drupal\Core\Config\Entity\ConfigStorageController
*/
protected $dateFormatStorage;
/** /**
* Constructs a new date format form. * Constructs a new date format form.
* *
@ -48,13 +56,16 @@ abstract class DateFormatFormBase extends EntityFormController {
* The entity query factory. * The entity query factory.
* @param \Drupal\Core\Datetime\Date $date_service * @param \Drupal\Core\Datetime\Date $date_service
* The date service. * The date service.
* @param \Drupal\Core\Config\Entity\ConfigStorageController $date_format_storage
* The date format storage controller.
*/ */
public function __construct(QueryFactory $query_factory, Date $date_service) { public function __construct(QueryFactory $query_factory, Date $date_service, ConfigStorageController $date_format_storage) {
$date = new DrupalDateTime(); $date = new DrupalDateTime();
$this->patternType = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP; $this->patternType = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
$this->queryFactory = $query_factory; $this->queryFactory = $query_factory;
$this->dateService = $date_service; $this->dateService = $date_service;
$this->dateFormatStorage = $date_format_storage;
} }
/** /**
@ -63,7 +74,8 @@ abstract class DateFormatFormBase extends EntityFormController {
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('entity.query'), $container->get('entity.query'),
$container->get('date') $container->get('date'),
$container->get('entity.manager')->getStorageController('date_format')
); );
} }
@ -183,16 +195,12 @@ abstract class DateFormatFormBase extends EntityFormController {
// The machine name field should already check to see if the requested // The machine name field should already check to see if the requested
// machine name is available. Regardless of machine_name or human readable // machine name is available. Regardless of machine_name or human readable
// name, check to see if the provided pattern exists. // name, check to see if the provided pattern exists.
$format = trim($form_state['values']['date_format_pattern']); $pattern = trim($form_state['values']['date_format_pattern']);
$formats = $this->queryFactory foreach ($this->dateFormatStorage->loadMultiple() as $format) {
->get($this->entity->entityType()) if ($format->getPattern() == $pattern) {
->condition('pattern.' . $this->patternType, $format) form_set_error('date_format_pattern', t('This format already exists. Enter a unique format string.'));
->execute(); continue;
}
// Exclude the current format.
unset($formats[$this->entity->id()]);
if (!empty($formats)) {
form_set_error('date_format_pattern', t('This format already exists. Enter a unique format string.'));
} }
} }

View File

@ -110,7 +110,7 @@ class ConfigEntityQueryTest extends DrupalUnitTestBase {
$array['level1']['level2'] = 3; $array['level1']['level2'] = 3;
$entity = entity_create('config_query_test', array( $entity = entity_create('config_query_test', array(
'label' => $this->randomName() . '_test_contains_' . $this->randomName(), 'label' => $this->randomName() . '_TEST_contains_' . $this->randomName(),
'id' => '5', 'id' => '5',
'number' => 53, 'number' => 53,
'array' => $array, 'array' => $array,
@ -461,6 +461,22 @@ class ConfigEntityQueryTest extends DrupalUnitTestBase {
$this->assertResults(array()); $this->assertResults(array());
} }
/**
* Tests case sensitivity.
*/
public function testCaseSensitivity() {
// Filter by label with a known containing case-sensitive word.
$this->queryResults = $this->factory->get('config_query_test')
->condition('label', 'TEST', 'CONTAINS')
->execute();
$this->assertResults(array('3', '4', '5'));
$this->queryResults = $this->factory->get('config_query_test')
->condition('label', 'test', 'CONTAINS')
->execute();
$this->assertResults(array('3', '4', '5'));
}
/** /**
* Asserts the results as expected regardless of order. * Asserts the results as expected regardless of order.
* *