Same name in other branches
  1. 2.x tests/src/Functional/EntityCloneContentTest.php \Drupal\Tests\entity_clone\Functional\EntityCloneContentTest::testCreatedAndChangedDate()

Test the cloned entity's created and changed dates.

For entities that support these kinds of dates, both are reset to the current time.

File

tests/src/Functional/EntityCloneContentTest.php, line 121

Class

EntityCloneContentTest
Create a content and test a clone.

Namespace

Drupal\Tests\entity_clone\Functional

Code

public function testCreatedAndChangedDate() {
    // Create the original node.
    $original_node_creation_date = new \DateTimeImmutable('1 year 1 month 1 day ago');
    $translation_creation_date = new \DateTimeImmutable('1 month 1 day ago');
    $original_node = Node::create([
        'type' => 'page',
        'title' => 'Test',
        'created' => $original_node_creation_date->getTimestamp(),
        'changed' => $original_node_creation_date->getTimestamp(),
    ]);
    $original_node->addTranslation('fr', $original_node->toArray());
    // The translation was created and updated later.
    $translation = $original_node->getTranslation('fr');
    $translation->setCreatedTime($translation_creation_date->getTimestamp());
    $translation->setChangedTime($translation_creation_date->getTimestamp());
    $original_node->save();
    $original_node = Node::load($original_node->id());
    $this->assertEquals($original_node_creation_date->getTimestamp(), $original_node->getCreatedTime());
    $this->assertEquals($original_node_creation_date->getTimestamp(), $original_node->getChangedTime());
    $this->assertEquals($translation_creation_date->getTimestamp(), $original_node->getTranslation('fr')
        ->getCreatedTime());
    $this->assertEquals($translation_creation_date->getTimestamp(), $original_node->getTranslation('fr')
        ->getChangedTime());
    $this->drupalGet('entity_clone/node/' . $original_node->id());
    // Clone the node.
    $this->submitForm([], $this->t('Clone'));
    // Find the cloned node.
    $nodes = \Drupal::entityTypeManager()->getStorage('node')
        ->loadByProperties([
        'title' => sprintf('%s - Cloned', $original_node->label()),
    ]);
    $this->assertGreaterThanOrEqual(1, count($nodes));
    
    /** @var \Drupal\node\NodeInterface $cloned_node */
    $cloned_node = reset($nodes);
    // Validate the cloned node's created time is more recent than the original
    // node.
    $this->assertNotEquals($original_node->getCreatedTime(), $cloned_node->getCreatedTime());
    $this->assertGreaterThanOrEqual($original_node->getCreatedTime(), $cloned_node->getCreatedTime());
    // Assert the changed time is equal to the newly created time since we
    // cannot have a changed date before it.
    $this->assertEquals($cloned_node->getCreatedTime(), $cloned_node->getChangedTime());
    // Validate the translation created and updated dates.
    $this->assertTrue($cloned_node->hasTranslation('fr'));
    $translation = $cloned_node->getTranslation('fr');
    // The created and updated times should be the same between the original
    // and the translation as both should be reset.
    $this->assertEquals($cloned_node->getCreatedTime(), $translation->getCreatedTime());
    $this->assertEquals($cloned_node->getChangedTime(), $translation->getChangedTime());
}