You are here

public function EntityCloneContentCreatedDate::testCreatedDateIsNotCloned in Entity Clone 8

Test that an entity's created date is not cloned.

File

tests/src/Functional/EntityCloneContentCreatedDate.php, line 49

Class

EntityCloneContentCreatedDate
Test whether cloning an entity also clones its created date.

Namespace

Drupal\Tests\entity_clone\Functional

Code

public function testCreatedDateIsNotCloned() {

  // Log in.
  $this
    ->drupalLogin($this->sutUser);

  // Create the original node.
  $originalNodeCreatedDate = new \DateTimeImmutable('1 year 1 month 1 day ago');
  $originalNode = $this
    ->drupalCreateNode([
    'created' => $originalNodeCreatedDate
      ->getTimestamp(),
  ]);
  $this
    ->assertEquals($originalNodeCreatedDate
    ->getTimestamp(), $originalNode
    ->getCreatedTime());

  // Clone the node.
  $this
    ->drupalGet(Url::fromRoute('entity.node.clone_form', [
    'node' => $originalNode
      ->id(),
  ])
    ->toString());
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Clone');

  // Find the cloned node.
  $originalNodeClones = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => sprintf('%s - Cloned', $originalNode
      ->label()),
  ]);
  $this
    ->assertGreaterThanOrEqual(1, count($originalNodeClones));
  $clonedNode = reset($originalNodeClones);

  // Validate the cloned node's created time is more recent than the original
  // node.
  $this
    ->assertNotEquals($originalNode
    ->getCreatedTime(), $clonedNode
    ->getCreatedTime());
  $this
    ->assertGreaterThanOrEqual($originalNode
    ->getCreatedTime(), $clonedNode
    ->getCreatedTime());
}