Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / tests / src / Functional / UrlTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Functional;
4
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;
10
11 /**
12  * Tests URL tokens.
13  *
14  * @group token
15  */
16 class UrlTest extends BrowserTestBase {
17
18   use AssertPageCacheContextsAndTagsTrait;
19
20   /**
21    * The first testing node.
22    *
23    * @var \Drupal\node\NodeInterface
24    */
25   protected $node1;
26
27   /**
28    * The second testing node.
29    *
30    * @var \Drupal\node\NodeInterface
31    */
32   protected $node2;
33
34   /**
35    * Modules to install.
36    *
37    * @var string[]
38    */
39   public static $modules = ['node', 'token', 'block'];
40
41   /**
42    * {@inheritdoc}
43    */
44   protected function setUp() {
45     parent::setUp();
46
47     $node_type = NodeType::create(['type' => 'article', 'name' => 'Article']);
48     $node_type->save();
49
50     $this->node1 = Node::create([
51       'type' => 'article',
52       'title' => 'Test Node 1',
53     ]);
54     $this->node1->save();
55
56     $this->node2 = Node::create([
57       'type' => 'article',
58       'title' => 'Test Node 2',
59     ]);
60     $this->node2->save();
61
62   }
63
64   /**
65    * Creates a block with token for title and tests cache contexts.
66    *
67    * @throws \Behat\Mink\Exception\ElementHtmlException
68    * @throws \Drupal\Core\Entity\EntityStorageException
69    */
70   public function testBlockUrlTokenReplacement() {
71
72     $node1_url = $this->node1->toUrl();
73     $node2_url = $this->node2->toUrl();
74
75     // Using a @dataprovider causes repeated database installations and takes a
76     // very long time.
77     $tests = [];
78     $tests[] = [
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',
84     ];
85     $tests[] = [
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',
90     ];
91
92     // Place a standard block and use a token in the label.
93     $edit = [
94       'id' => 'token_url_test_block',
95       'label' => 'label',
96       'label_display' => TRUE,
97     ];
98     $this->placeBlock('system_powered_by_block', $edit);
99     $block = Block::load('token_url_test_block');
100
101     $assert_session = $this->assertSession();
102
103     foreach ($tests as $test) {
104       // Set the block label.
105       $block->getPlugin()->setConfigurationValue('label', $test['token']);
106       $block->save();
107
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']);
111
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']);
115
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']);
120     }
121
122
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');
125     $block->save();
126
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');
131
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');
135   }
136
137 }