You are here

public function PreviewLinkTest::testEntities in Preview Link 2.0.x

Same name and namespace in other branches
  1. 2.x tests/src/Functional/PreviewLinkTest.php \Drupal\Tests\preview_link\Functional\PreviewLinkTest::testEntities()

Tests managing entities for a Preview Link.

File

tests/src/Functional/PreviewLinkTest.php, line 161

Class

PreviewLinkTest
Integration test for the preview link.

Namespace

Drupal\Tests\preview_link\Functional

Code

public function testEntities() : void {
  $this
    ->drupalLogin($this
    ->createUser([
    'generate preview links',
    'view test entity',
  ]));
  $entity1 = EntityTestRevPub::create([
    'name' => 'foo1',
  ]);
  $entity1
    ->save();
  $entity2 = EntityTestRevPub::create([
    'name' => 'foo2',
  ]);
  $entity2
    ->save();
  $generateUrl1 = $entity1
    ->toUrl('preview-link-generate');
  $this
    ->drupalGet($generateUrl1);
  $this
    ->assertSession()
    ->fieldValueEquals('entities[0][target_id]', 'foo1 (1)');
  $this
    ->assertSession()
    ->elementAttributeContains('css', '[name="entities[0][target_id]"]', 'disabled', 'disabled');
  $this
    ->assertSession()
    ->fieldExists('entities[1][target_id]');

  // Adding entity2, entity2 should remain editable. entity1 not editable.
  $edit = [
    'entities[1][target_id]' => 'foo2 (2)',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Preview Link saved.');
  $this
    ->assertSession()
    ->fieldValueEquals('entities[0][target_id]', 'foo1 (1)');
  $this
    ->assertSession()
    ->fieldValueEquals('entities[1][target_id]', 'foo2 (2)');
  $this
    ->assertSession()
    ->elementAttributeContains('css', '[name="entities[0][target_id]"]', 'disabled', 'disabled');
  $entity2Element = $this
    ->assertSession()
    ->elementExists('css', '[name="entities[1][target_id]"]');
  $this
    ->assertFalse($entity2Element
    ->hasAttribute('disabled'));
  $this
    ->assertSession()
    ->fieldExists('entities[2][target_id]');

  // Navigating to the other entity, entity1 now editable.
  $generateUrl2 = $entity2
    ->toUrl('preview-link-generate');
  $this
    ->drupalGet($generateUrl2);
  $this
    ->assertSession()
    ->fieldValueEquals('entities[0][target_id]', 'foo1 (1)');
  $this
    ->assertSession()
    ->fieldValueEquals('entities[1][target_id]', 'foo2 (2)');
  $entity1Element = $this
    ->assertSession()
    ->elementExists('css', '[name="entities[0][target_id]"]');
  $this
    ->assertFalse($entity1Element
    ->hasAttribute('disabled'));
  $this
    ->assertSession()
    ->elementAttributeContains('css', '[name="entities[1][target_id]"]', 'disabled', 'disabled');
}