You are here

public function BundleEntityDuplicatorTest::testDuplicateWithFieldAndDisplays in Entity API 8

@covers ::duplicate @covers ::duplicateFields @covers ::duplicateDisplays

File

tests/src/Kernel/BundleEntityDuplicatorTest.php, line 95

Class

BundleEntityDuplicatorTest
Tests the bundle entity duplicator.

Namespace

Drupal\Tests\entity\Kernel

Code

public function testDuplicateWithFieldAndDisplays() {
  $this
    ->createTextField('field_text', 'test', 'Test text');
  $form_display = $this
    ->getDisplay('entity_test_with_bundle', 'test', 'form');
  $form_display
    ->setComponent('field_text', [
    'type' => 'text_textfield',
    'weight' => 0,
  ]);
  $form_display
    ->save();
  $view_display = $this
    ->getDisplay('entity_test_with_bundle', 'test', 'view');
  $view_display
    ->setComponent('field_text', [
    'type' => 'text_default',
    'weight' => 0,
  ]);
  $view_display
    ->save();
  $duplicated_bundle_entity = $this->duplicator
    ->duplicate($this->bundleEntity, [
    'id' => 'test2',
    'label' => 'Test2',
  ]);
  $this
    ->assertFalse($duplicated_bundle_entity
    ->isNew());
  $this
    ->assertEquals('test2', $duplicated_bundle_entity
    ->id());
  $this
    ->assertEquals('Test2', $duplicated_bundle_entity
    ->label());
  $this
    ->assertEquals($this->bundleEntity
    ->get('description'), $duplicated_bundle_entity
    ->get('description'));

  // Confirm that the field was copied to the new bundle.
  $entity = EntityTestWithBundle::create([
    'type' => 'test2',
  ]);
  $this
    ->assertTrue($entity
    ->hasField('field_text'));

  // Confirm that the entity displays were copied.
  $form_display = $this
    ->getDisplay('entity_test_with_bundle', 'test2', 'form');
  $this
    ->assertNotEmpty($form_display
    ->getComponent('field_text'));
  $view_display = $this
    ->getDisplay('entity_test_with_bundle', 'test2', 'view');
  $this
    ->assertNotEmpty($view_display
    ->getComponent('field_text'));
}