Skip to content

Commit

Permalink
Issue #2988309 by Krzysztof Domański, Berdir, hchonov: Ensure that al…
Browse files Browse the repository at this point in the history
…l field types return TRUE on equals() for the same values

(cherry picked from commit 29e324693ea41b67042dc400cfe011242adf15ad)
  • Loading branch information
catch committed May 7, 2020
1 parent 277283f commit e75a35a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Drupal/Core/Field/FieldItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,15 @@ public function equals(FieldItemListInterface $list_to_compare) {
$callback = function (&$value) use ($non_computed_properties) {
if (is_array($value)) {
$value = array_intersect_key($value, $non_computed_properties);

// Also filter out properties with a NULL value as they might exist in
// one field item and not in the other, depending on how the values are
// set. Do not filter out empty strings or other false-y values as e.g.
// a NULL or FALSE in a boolean field is not the same.
$value = array_filter($value, function ($property) {
return $property !== NULL;
});

ksort($value);
}
};
Expand Down
19 changes: 19 additions & 0 deletions tests/Drupal/Tests/Core/Field/FieldItemListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ public function providerTestEquals() {
// not exist ('3').
$datasets[] = [TRUE, $field_item_h, $field_item_i];

/** @var \Drupal\Core\Field\FieldItemBase $field_item_j */
$field_item_j = $this->getMockForAbstractClass('Drupal\Core\Field\FieldItemBase', [], '', FALSE);
$field_item_j->setValue(['0' => 1]);
/** @var \Drupal\Core\Field\FieldItemBase $field_item_k */
$field_item_k = $this->getMockForAbstractClass('Drupal\Core\Field\FieldItemBase', [], '', FALSE);
$field_item_k->setValue(['0' => 1, '1' => NULL]);
/** @var \Drupal\Core\Field\FieldItemBase $field_item_l */
$field_item_l = $this->getMockForAbstractClass('Drupal\Core\Field\FieldItemBase', [], '', FALSE);
$field_item_l->setValue(['0' => 1, '1' => FALSE]);
/** @var \Drupal\Core\Field\FieldItemBase $field_item_m */
$field_item_m = $this->getMockForAbstractClass('Drupal\Core\Field\FieldItemBase', [], '', FALSE);
$field_item_m->setValue(['0' => 1, '1' => '']);

// Tests filter properties with a NULL value. Empty strings or other false-y
// values are not filtered.
$datasets[] = [TRUE, $field_item_j, $field_item_k];
$datasets[] = [FALSE, $field_item_j, $field_item_l];
$datasets[] = [FALSE, $field_item_j, $field_item_m];

return $datasets;
}

Expand Down

0 comments on commit e75a35a

Please sign in to comment.