You are here

public function ContentEntityChangedTest::testChanged in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php \Drupal\KernelTests\Core\Entity\ContentEntityChangedTest::testChanged()

Tests basic EntityChangedInterface functionality.

File

core/tests/Drupal/KernelTests/Core/Entity/ContentEntityChangedTest.php, line 65

Class

ContentEntityChangedTest
Tests basic EntityChangedInterface functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testChanged() {
  $user1 = $this
    ->createUser();
  $user2 = $this
    ->createUser();

  // Create a test entity.
  $entity = EntityTestMulChanged::create([
    'name' => $this
      ->randomString(),
    'not_translatable' => $this
      ->randomString(),
    'user_id' => $user1
      ->id(),
    'language' => 'en',
  ]);
  $entity
    ->save();
  $this
    ->assertTrue($entity
    ->getChangedTime() >= REQUEST_TIME, 'Changed time of original language is valid.');

  // We can't assert equality here because the created time is set to the
  // request time, while instances of ChangedTestItem use the current
  // timestamp every time. Therefore we check if the changed timestamp is
  // between the created time and now.
  $this
    ->assertTrue($entity
    ->getChangedTime() >= $entity
    ->get('created')->value && $entity
    ->getChangedTime() - $entity
    ->get('created')->value <= time() - REQUEST_TIME, 'Changed and created time of original language can be assumed to be identical.');
  $this
    ->assertEquals($entity
    ->getChangedTimeAcrossTranslations(), $entity
    ->getChangedTime(), 'Changed time of original language is the same as changed time across all translations.');
  $changed_en = $entity
    ->getChangedTime();

  /** @var \Drupal\entity_test\Entity\EntityTestMulRevChanged $german */
  $german = $entity
    ->addTranslation('de');
  $entity
    ->save();
  $this
    ->assertEquals($changed_en, $entity
    ->getChangedTime(), 'Changed time of original language did not change.');
  $this
    ->assertTrue($german
    ->getChangedTime() > $entity
    ->getChangedTime(), 'Changed time of the German translation is newer then the original language.');
  $this
    ->assertEquals($entity
    ->getChangedTimeAcrossTranslations(), $german
    ->getChangedTime(), 'Changed time of the German translation is the newest time across all translations.');
  $changed_de = $german
    ->getChangedTime();
  $entity
    ->save();
  $this
    ->assertEquals($changed_en, $entity
    ->getChangedTime(), 'Changed time of original language did not change.');
  $this
    ->assertEquals($changed_de, $german
    ->getChangedTime(), 'Changed time of the German translation did not change.');

  // Update a non-translatable field to make sure that the changed timestamp
  // is updated for all translations.
  $entity
    ->set('not_translatable', $this
    ->randomString())
    ->save();
  $this
    ->assertTrue($entity
    ->getChangedTime() > $changed_en, 'Changed time of original language did change.');
  $this
    ->assertTrue($german
    ->getChangedTime() > $changed_de, 'Changed time of the German translation did change.');
  $this
    ->assertEquals($entity
    ->getChangedTime(), $german
    ->getChangedTime(), 'When editing a non-translatable field the updated changed time is equal across all translations.');
  $changed_en = $entity
    ->getChangedTime();
  $changed_de = $german
    ->getChangedTime();
  $entity
    ->setOwner($user2);
  $entity
    ->save();
  $this
    ->assertTrue($entity
    ->getChangedTime() > $changed_en, 'Changed time of original language did change.');
  $this
    ->assertEquals($changed_de, $german
    ->getChangedTime(), 'Changed time of the German translation did not change.');
  $this
    ->assertTrue($entity
    ->getChangedTime() > $german
    ->getChangedTime(), 'Changed time of original language is newer then the German translation.');
  $this
    ->assertEquals($entity
    ->getChangedTimeAcrossTranslations(), $entity
    ->getChangedTime(), 'Changed time of the original language is the newest time across all translations.');
  $changed_en = $entity
    ->getChangedTime();

  // Save entity without any changes.
  $entity
    ->save();
  $this
    ->assertEquals($changed_en, $entity
    ->getChangedTime(), 'Changed time of original language did not change.');
  $this
    ->assertEquals($changed_de, $german
    ->getChangedTime(), 'Changed time of the German translation did not change.');

  // At this point the changed time of the original language (en) is newer
  // than the changed time of the German translation. Now test that entity
  // queries work as expected.
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_en)
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time of original language.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_en, '=', 'en')
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time of original language by setting the original language as condition.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_de, '=', 'en')
    ->execute();
  $this
    ->assertEmpty($ids, 'There\'s no original entity stored having the changed time of the German translation.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_en)
    ->condition('default_langcode', '1')
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time of default language.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_de)
    ->condition('default_langcode', '1')
    ->execute();
  $this
    ->assertEmpty($ids, 'There\'s no entity stored using the default language having the changed time of the German translation.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_de)
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time of the German translation.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_de, '=', 'de')
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time of the German translation.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_en, '=', 'de')
    ->execute();
  $this
    ->assertEmpty($ids, 'There\'s no German translation stored having the changed time of the original language.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_de, '>')
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time regardless of translation.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_en, '<')
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time regardless of translation.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', 0, '>')
    ->execute();
  $this
    ->assertEquals($entity
    ->id(), reset($ids), 'Entity query can access changed time regardless of translation.');
  $query = $this->mulChangedStorage
    ->getQuery()
    ->accessCheck(FALSE);
  $ids = $query
    ->condition('changed', $changed_en, '>')
    ->execute();
  $this
    ->assertEmpty($ids, 'Entity query can access changed time regardless of translation.');
}