3 namespace Drupal\Tests\token\Functional;
5 use Drupal\block\Entity\Block;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
16 class UrlTest extends BrowserTestBase {
18 use AssertPageCacheContextsAndTagsTrait;
21 * The first testing node.
23 * @var \Drupal\node\NodeInterface
28 * The second testing node.
30 * @var \Drupal\node\NodeInterface
39 public static $modules = ['node', 'token', 'block'];
44 protected function setUp() {
47 $node_type = NodeType::create(['type' => 'article', 'name' => 'Article']);
50 $this->node1 = Node::create([
52 'title' => 'Test Node 1',
56 $this->node2 = Node::create([
58 'title' => 'Test Node 2',
65 * Creates a block with token for title and tests cache contexts.
67 * @throws \Behat\Mink\Exception\ElementHtmlException
68 * @throws \Drupal\Core\Entity\EntityStorageException
70 public function testBlockUrlTokenReplacement() {
72 $node1_url = $this->node1->toUrl();
73 $node2_url = $this->node2->toUrl();
75 // Using a @dataprovider causes repeated database installations and takes a
79 'token' => 'prefix_[current-page:url:path]_suffix',
80 'expected1' => 'prefix_/' . $node1_url->getInternalPath() . '_suffix',
81 'expected2' => 'prefix_/' . $node2_url->getInternalPath() . '_suffix',
82 // A path can only be generated from a routed path.
83 'expected3' => 'prefix_/_suffix',
86 'token' => 'prefix_[current-page:url]_suffix',
87 'expected1' => 'prefix_' . $node1_url->setAbsolute()->toString() . '_suffix',
88 'expected2' => 'prefix_' . $node2_url->setAbsolute()->toString() . '_suffix',
89 'expected3' => 'prefix_' . $this->getAbsoluteUrl('does-not-exist') . '_suffix',
92 // Place a standard block and use a token in the label.
94 'id' => 'token_url_test_block',
96 'label_display' => TRUE,
98 $this->placeBlock('system_powered_by_block', $edit);
99 $block = Block::load('token_url_test_block');
101 $assert_session = $this->assertSession();
103 foreach ($tests as $test) {
104 // Set the block label.
105 $block->getPlugin()->setConfigurationValue('label', $test['token']);
108 // Go to the first node page and test that the token is correct.
109 $this->drupalGet($node1_url);
110 $assert_session->elementContains('css', '#block-token-url-test-block', $test['expected1']);
112 // Go to the second node page and check that the block title has changed.
113 $this->drupalGet($node2_url);
114 $assert_session->elementContains('css', '#block-token-url-test-block', $test['expected2']);
116 // Test the current page url on a 404 page.
117 $this->drupalGet('does-not-exist');
118 $assert_session->statusCodeEquals(404);
119 $assert_session->elementContains('css', '#block-token-url-test-block', $test['expected3']);
123 // Can't do this test in the for loop above, it's too different.
124 $block->getPlugin()->setConfigurationValue('label', 'prefix_[current-page:query:unicorns]_suffix');
127 // Test the parameter token.
128 $this->drupalGet($node1_url->setOption('query', ['unicorns' => 'fluffy']));
129 $this->assertCacheContext('url.query_args');
130 $assert_session->elementContains('css', '#block-token-url-test-block', 'prefix_fluffy_suffix');
132 // Change the parameter on the same page.
133 $this->drupalGet($node1_url->setOption('query', ['unicorns' => 'dead']));
134 $assert_session->elementContains('css', '#block-token-url-test-block', 'prefix_dead_suffix');