Commit 4dde9c81 authored by alexpott's avatar alexpott

Issue #2615016 by hchonov, Jo Fitzgerald, Pavan B S, tstoeckler, mkalkbrenner,...

Issue #2615016 by hchonov, Jo Fitzgerald, Pavan B S, tstoeckler, mkalkbrenner, plach, amateescu, Berdir:  ContentEntityBase::hasTranslationChanges should exclude the "changed" fields from the comparison
parent 85860ba3
......@@ -5,6 +5,7 @@
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Entity\Plugin\DataType\EntityReference;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\ChangedFieldItemList;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
......@@ -1326,8 +1327,14 @@ public function hasTranslationChanges() {
if (in_array($field_name, $skip_fields, TRUE)) {
continue;
}
if (!$definition->isComputed()) {
$items = $this->get($field_name)->filterEmptyItems();
$field = $this->get($field_name);
// When saving entities in the user interface, the changed timestamp is
// automatically incremented by ContentEntityForm::submitForm() even if
// nothing was actually changed. Thus, the changed time needs to be
// ignored when determining whether there are any actual changes in the
// entity.
if (!($field instanceof ChangedFieldItemList) && !$definition->isComputed()) {
$items = $field->filterEmptyItems();
$original_items = $translation->get($field_name)->filterEmptyItems();
if (!$items->equals($original_items)) {
return TRUE;
......
<?php
namespace Drupal\entity_test\Entity;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionLogEntityTrait;
use Drupal\Core\Entity\RevisionLogInterface;
/**
* Defines the test entity class.
*
* @ContentEntityType(
* id = "entity_test_mulrev_chnged_revlog",
* label = @Translation("Test entity - revisions log and data table"),
* base_table = "entity_test_mulrev_changed_revlog",
* revision_table = "entity_test_mulrev_changed_revlog_revision",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type",
* "revision" = "revision_id",
* "label" = "name",
* "langcode" = "langcode",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_user",
* "revision_created" = "revision_created",
* "revision_log_message" = "revision_log_message"
* },
* )
*/
class EntityTestMulRevChangedWithRevisionLog extends EntityTestMulRevChanged implements RevisionLogInterface {
use RevisionLogEntityTrait;
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::revisionLogBaseFieldDefinitions($entity_type);
return $fields;
}
}
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
/**
* Tests ContentEntityBase::hasTranslationChanges().
*
* @group Entity
*/
class ContentEntityHasChangesTest extends KernelTestBase {
/**
* Bundle of entity.
*
* @var string
*/
protected $bundle = 'test';
/**
* {@inheritdoc}
*/
public static $modules = ['system', 'user', 'entity_test'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test_mulrev_chnged_revlog');
$this->installSchema('system', 'sequences');
}
/**
* Tests the correct functionality of the hasTranslationChanges() function.
*/
public function testHasTranslationChanges() {
$user1 = User::create([
'name' => 'username1',
'status' => 1,
]);
$user1->save();
$user2 = User::create([
'name' => 'username2',
'status' => 1,
]);
$user2->save();
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = $this->container->get('entity_type.manager')
->getStorage('entity_test_mulrev_chnged_revlog');
/** @var \Drupal\entity_test\Entity\EntityTestMulRevChangedWithRevisionLog $entity */
$entity = $storage->create([
'name' => $this->randomString(),
]);
$entity->setRevisionUserId($user1->id());
$entity->save();
$this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');
// Update the revision metadata fields and the changed field, which should
// be skipped from checking for changes in
// ContentEntityBase::hasTranslationChanges().
$entity_previous_rev_id = $entity->getRevisionId();
// Revision metadata field revision_timestamp.
$entity->setRevisionCreationTime(time() + 1);
// Revision metadata field revision_uid.
$entity->setRevisionUserId($user2->id());
// Revision metadata field revision_log.
$entity->setRevisionLogMessage('test');
// Revision metadata field revision_translation_affected.
$entity->setRevisionTranslationAffected(TRUE);
// Changed field.
$entity->setChangedTime(time() + 1);
// Check that the revision metadata fields and the changed field have been
// skipped when comparing same revisions.
$this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
// Check that the revision metadata fields and the changed field have been
// skipped when comparing same revisions with enforced new revision to be
// created on save.
$entity->setNewRevision(TRUE);
$this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
// Save the entity in new revision with changes on the revision metadata
// fields.
$entity->save();
// Check that the revision metadata fields and the changed field have been
// skipped when comparing different revisions.
$entity = $storage->loadRevision($entity_previous_rev_id);
$this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes when comparing different revisions.');
}
}
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