You are here

public function AETRecursionWebTest::testCyclicEmbedding in Advanced Entity Tokens 2.x

Test that it's not OK to set up a cyclic inclusion pattern.

Tries to embed alpha into the body of beta, and the beta into the body of alpha. We expect this to be caught and stopped.

Throws

\Behat\Mink\Exception\ResponseTextException

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Functional/AETRecursionWebTest.php, line 187

Class

AETRecursionWebTest
Tests for Advanced Entity Tokens.

Namespace

Drupal\Tests\aet\Functional

Code

public function testCyclicEmbedding() {
  $this
    ->drupalLogin($this->editor_user);
  $alpha_node_data = [
    'type' => 'page',
    'title' => 'I am the alpha',
    'body' => [
      [
        'value' => "I will try to embed data from the beta node into my body.",
        'format' => 'simple_tokens',
      ],
    ],
  ];

  // Save once so I know what my nid is.
  $alpha_node = $this
    ->drupalCreateNode($alpha_node_data);
  $beta_node_data = [
    'type' => 'page',
    'title' => 'I am the beta',
    'body' => [
      [
        'value' => "This is the beta content. I will try to embed data from the alpha node named '[aet:node:{$alpha_node->id()}:title]' into my body. <blockquote>[aet:node:{$alpha_node->id()}:body]</blockquote>",
        'format' => 'simple_tokens',
      ],
    ],
  ];
  $beta_node = $this
    ->drupalCreateNode($beta_node_data);

  // Update the alpha with a ref to the beta now we know its ID.
  $alpha_node->body = "This is the alpha content. I will try to embed data from the beta node named '[aet:node:{$beta_node->id()}:title]' into my body. <blockquote>[aet:node:{$beta_node->id()}:body]</blockquote>";
  $alpha_node
    ->save();
  $this
    ->drupalGet("node/" . $alpha_node
    ->id());

  // This should still have been able to embed the TITLE of the beta,
  // But trying to embed the body should trigger a warning.
  // Alpha node should embed the beta nodes title OK.
  $this
    ->assertSession()
    ->pageTextContains("'I am the beta'");

  // Recursive token use was accurately detected when creating an embed loop.
  $this
    ->assertSession()
    ->pageTextContains("Recursive token use");

  // The beta content was not shown in the alpha body when there is recursion happening.
  $this
    ->assertSession()
    ->pageTextNotContains("This is the beta content");
}