Commit 49f5b153 authored by alexpott's avatar alexpott
Browse files

Issue #2527816 by jhedstrom, pfrenssen, chx, catch: Logic error in...

Issue #2527816 by jhedstrom, pfrenssen, chx, catch: Logic error in SqlContentEntityStorage::countFieldData() attempts to drop `name` column
parent d404b260
...@@ -1623,9 +1623,11 @@ public function countFieldData($storage_definition, $as_bool = FALSE) { ...@@ -1623,9 +1623,11 @@ public function countFieldData($storage_definition, $as_bool = FALSE) {
$or->isNotNull($table_mapping->getFieldColumnName($storage_definition, $property_name)); $or->isNotNull($table_mapping->getFieldColumnName($storage_definition, $property_name));
} }
$query->condition($or); $query->condition($or);
$query if (!$as_bool) {
->fields('t', array($this->idKey)) $query
->distinct(TRUE); ->fields('t', array($this->idKey))
->distinct(TRUE);
}
} }
// @todo Find a way to count field data also for fields having custom // @todo Find a way to count field data also for fields having custom
...@@ -1635,7 +1637,9 @@ public function countFieldData($storage_definition, $as_bool = FALSE) { ...@@ -1635,7 +1637,9 @@ public function countFieldData($storage_definition, $as_bool = FALSE) {
// If we are performing the query just to check if the field has data // If we are performing the query just to check if the field has data
// limit the number of rows. // limit the number of rows.
if ($as_bool) { if ($as_bool) {
$query->range(0, 1); $query
->range(0, 1)
->addExpression('1');
} }
else { else {
// Otherwise count the number of rows. // Otherwise count the number of rows.
......
...@@ -29,6 +29,11 @@ class FieldDataCountTest extends FieldUnitTestBase { ...@@ -29,6 +29,11 @@ class FieldDataCountTest extends FieldUnitTestBase {
*/ */
protected $storageRev; protected $storageRev;
/**
* @var \Drupal\Core\Entity\DynamicallyFieldableEntityStorageInterface
*/
protected $storageUser;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -37,6 +42,7 @@ protected function setUp() { ...@@ -37,6 +42,7 @@ protected function setUp() {
$this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_rev');
$this->storage = \Drupal::entityManager()->getStorage('entity_test'); $this->storage = \Drupal::entityManager()->getStorage('entity_test');
$this->storageRev = \Drupal::entityManager()->getStorage('entity_test_rev'); $this->storageRev = \Drupal::entityManager()->getStorage('entity_test_rev');
$this->storageUser = \Drupal::entityManager()->getStorage('user');
} }
/** /**
...@@ -136,4 +142,22 @@ public function testEntityCountAndHasData() { ...@@ -136,4 +142,22 @@ public function testEntityCountAndHasData() {
$this->assertEqual(count($entity->{$this->fieldTestData->field_name_2}), $cardinality, format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $first_revision))); $this->assertEqual(count($entity->{$this->fieldTestData->field_name_2}), $cardinality, format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $first_revision)));
} }
/**
* Verify that we can count a table that contains an entry with index 0.
*/
public function testCountWithIndex0() {
// Create an entry for the anonymous user, who has user ID 0.
$user = $this->storageUser
->create(array(
'uid' => 0,
'name' => 'anonymous',
'mail' => NULL,
'status' => FALSE,
));
$user->save();
$storage = $user->getFieldDefinition('name')->getFieldStorageDefinition();
$this->assertIdentical(TRUE, $this->storageUser->countFieldData($storage, TRUE));
}
} }
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