You are here

public function EntityCloneContentRecursiveCircularTest::testContentEntityClone in Entity Clone 8

Test clone a content entity with another entities attached.

File

tests/src/Functional/EntityCloneContentRecursiveCircularTest.php, line 78

Class

EntityCloneContentRecursiveCircularTest
Create a content and test a clone.

Namespace

Drupal\Tests\entity_clone\Functional

Code

public function testContentEntityClone() {
  $node1_title = $this
    ->randomMachineName(8);
  $node1 = Node::create([
    'type' => 'test_content_type',
    'title' => $node1_title,
  ]);
  $node1
    ->save();
  $node2_title = $this
    ->randomMachineName(8);
  $node2 = Node::create([
    'type' => 'test_content_type',
    'title' => $node2_title,
    'test_field_reference' => $node1,
  ]);
  $node2
    ->save();
  $node1
    ->set('test_field_reference', $node2
    ->id());
  $node1
    ->save();
  $settings = [
    'node' => [
      'default_value' => 1,
      'disable' => 0,
      'hidden' => 0,
    ],
  ];
  \Drupal::service('config.factory')
    ->getEditable('entity_clone.settings')
    ->set('form_settings', $settings)
    ->save();
  $this
    ->drupalPostForm('entity_clone/node/' . $node1
    ->id(), [], t('Clone'));
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => $node1_title . ' - Cloned',
  ]);

  /** @var \Drupal\node\Entity\Node $node1cloned */
  $node1cloned = reset($nodes);
  $this
    ->assertInstanceOf(Node::class, $node1cloned, 'Node 1 cloned found in database.');
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => $node2_title . ' - Cloned',
  ]);

  /** @var \Drupal\node\Entity\Node $node2cloned */
  $node2cloned = reset($nodes);
  $this
    ->assertInstanceOf(Node::class, $node2cloned, 'Node 2 cloned found in database.');
  $reference = $node2cloned
    ->get('test_field_reference')
    ->first()
    ->get('entity')
    ->getTarget()
    ->getValue();
  $this
    ->assertEquals($node1cloned
    ->id(), $reference
    ->id(), "Node 1 reference, from circular reference, is correctly referenced.");
  $node1cloned
    ->delete();
  $node2cloned
    ->delete();
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => $node1_title,
  ]);
  $node = reset($nodes);
  $this
    ->assertInstanceOf(Node::class, $node, 'Test original node 1 found in database.');
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => $node2_title,
  ]);
  $node = reset($nodes);
  $this
    ->assertInstanceOf(Node::class, $node, 'Test original node 2 found in database.');
}