You are here

public function ReplicateUITest::testFunctionality in Replicate UI 8

Basic functional test for the replicate functionality.

File

tests/src/Functional/ReplicateUITest.php, line 91

Class

ReplicateUITest
Tests the UI functionality.

Namespace

Drupal\Tests\replicate_ui\Functional

Code

public function testFunctionality() {
  $this
    ->drupalGet($this->node
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextNotContains('Replicate');
  $this
    ->drupalLogin($this->user);

  // Test the Replicate operation.
  $test_node = Node::create([
    'title' => 'Replicate operation',
    'type' => 'page',
  ]);
  $test_node
    ->save();
  $this
    ->drupalGet('admin/content');
  $this
    ->getSession()
    ->getPage()
    ->fillField('title', 'Replicate operation');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Filter');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Replicate');
  $this
    ->getSession()
    ->getPage()
    ->fillField('new_label_' . $test_node
    ->language()
    ->getId(), 'Replicate operation (Copy)');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Replicate');
  $replicated_nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => 'Replicate operation (Copy)',
  ]);

  /** @var \Drupal\node\NodeInterface $replicated_node */
  $replicated_node = reset($replicated_nodes);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->responseContains('(<em class="placeholder">' . $test_node
    ->id() . '</em>) has been replicated to id <em class="placeholder">' . $replicated_node
    ->id() . '</em>!');
  $this
    ->assertUrl($replicated_node
    ->toUrl());
  $this
    ->drupalGet($this->node
    ->toUrl());
  $this
    ->assertSession()
    ->pageTextContains('Replicate');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('admin/content', [
    'query' => [
      'title' => $this->node
        ->getTitle(),
    ],
  ]);
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Replicate');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Test the new label form element on the confirmation page.
  $this
    ->assertSession()
    ->pageTextContains('New label');
  $this
    ->assertSession()
    ->pageTextContains('This text will be used as the label of the new entity being created');
  $langcode = $this->node
    ->language()
    ->getId();
  $new_element_label = $this
    ->assertSession()
    ->elementExists('css', 'input[name="new_label_' . $langcode . '"]');
  $this
    ->assertequals($this->node
    ->label() . ' (Copy)', $new_element_label
    ->getValue());
  $this
    ->getSession()
    ->getPage()
    ->fillField('new_label_' . $langcode, 'Overriden replicate label');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Replicate');
  $this
    ->assertSession()
    ->responseContains('<em class="placeholder">node</em> (<em class="placeholder">' . $this->node
    ->id() . '</em>) has been replicated');
  $this
    ->assertSession()
    ->titleEquals('Overriden replicate label | Drupal');

  // Check that we don't show the new label element on entities without a
  // label key.
  $this
    ->drupalGet("/user/{$this->user->id()}/replicate");
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Replicate');
  $this
    ->assertSession()
    ->pageTextContains('Are you sure you want to replicate user entity id');
  $this
    ->assertSession()
    ->elementNotExists('css', 'input[name="new_label"]');
  $this
    ->assertSession()
    ->pageTextNotContains('New label');
  $this
    ->assertSession()
    ->pageTextNotContains('This text will be used as the label of the new entity being created');
}