annotate core/modules/system/src/Tests/Theme/FunctionsTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Theme;
Chris@0 4
Chris@0 5 use Drupal\Component\Serialization\Json;
Chris@0 6 use Drupal\Component\Utility\Html;
Chris@0 7 use Drupal\Component\Utility\SafeMarkup;
Chris@0 8 use Drupal\Core\Session\UserSession;
Chris@0 9 use Drupal\Core\Url;
Chris@0 10 use Drupal\simpletest\WebTestBase;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Tests for common theme functions.
Chris@0 14 *
Chris@0 15 * @group Theme
Chris@0 16 */
Chris@0 17 class FunctionsTest extends WebTestBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Modules to enable.
Chris@0 21 *
Chris@0 22 * @var array
Chris@0 23 */
Chris@0 24 public static $modules = ['router_test'];
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Tests item-list.html.twig.
Chris@0 28 */
Chris@0 29 public function testItemList() {
Chris@0 30 // Verify that empty items produce no output.
Chris@0 31 $variables = [];
Chris@0 32 $expected = '';
Chris@0 33 $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates no output.');
Chris@0 34
Chris@0 35 // Verify that empty items with title produce no output.
Chris@0 36 $variables = [];
Chris@0 37 $variables['title'] = 'Some title';
Chris@0 38 $expected = '';
Chris@0 39 $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback with title generates no output.');
Chris@0 40
Chris@0 41 // Verify that empty items produce the empty string.
Chris@0 42 $variables = [];
Chris@0 43 $variables['empty'] = 'No items found.';
Chris@0 44 $expected = '<div class="item-list">No items found.</div>';
Chris@0 45 $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates empty string.');
Chris@0 46
Chris@0 47 // Verify that empty items produce the empty string with title.
Chris@0 48 $variables = [];
Chris@0 49 $variables['title'] = 'Some title';
Chris@0 50 $variables['empty'] = 'No items found.';
Chris@0 51 $expected = '<div class="item-list"><h3>Some title</h3>No items found.</div>';
Chris@0 52 $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates empty string with title.');
Chris@0 53
Chris@0 54 // Verify that title set to 0 is output.
Chris@0 55 $variables = [];
Chris@0 56 $variables['title'] = 0;
Chris@0 57 $variables['empty'] = 'No items found.';
Chris@0 58 $expected = '<div class="item-list"><h3>0</h3>No items found.</div>';
Chris@0 59 $this->assertThemeOutput('item_list', $variables, $expected, '%callback with title set to 0 generates a title.');
Chris@0 60
Chris@0 61 // Verify that title set to a render array is output.
Chris@0 62 $variables = [];
Chris@0 63 $variables['title'] = [
Chris@0 64 '#markup' => '<span>Render array</span>',
Chris@0 65 ];
Chris@0 66 $variables['empty'] = 'No items found.';
Chris@0 67 $expected = '<div class="item-list"><h3><span>Render array</span></h3>No items found.</div>';
Chris@0 68 $this->assertThemeOutput('item_list', $variables, $expected, '%callback with title set to a render array generates a title.');
Chris@0 69
Chris@0 70 // Verify that empty text is not displayed when there are list items.
Chris@0 71 $variables = [];
Chris@0 72 $variables['title'] = 'Some title';
Chris@0 73 $variables['empty'] = 'No items found.';
Chris@0 74 $variables['items'] = ['Un', 'Deux', 'Trois'];
Chris@0 75 $expected = '<div class="item-list"><h3>Some title</h3><ul><li>Un</li><li>Deux</li><li>Trois</li></ul></div>';
Chris@0 76 $this->assertThemeOutput('item_list', $variables, $expected, '%callback does not print empty text when there are list items.');
Chris@0 77
Chris@0 78 // Verify nested item lists.
Chris@0 79 $variables = [];
Chris@0 80 $variables['title'] = 'Some title';
Chris@0 81 $variables['attributes'] = [
Chris@0 82 'id' => 'parentlist',
Chris@0 83 ];
Chris@0 84 $variables['items'] = [
Chris@0 85 // A plain string value forms an own item.
Chris@0 86 'a',
Chris@0 87 // Items can be fully-fledged render arrays with their own attributes.
Chris@0 88 [
Chris@0 89 '#wrapper_attributes' => [
Chris@0 90 'id' => 'item-id-b',
Chris@0 91 ],
Chris@0 92 '#markup' => 'b',
Chris@0 93 'childlist' => [
Chris@0 94 '#theme' => 'item_list',
Chris@0 95 '#attributes' => ['id' => 'blist'],
Chris@0 96 '#list_type' => 'ol',
Chris@0 97 '#items' => [
Chris@0 98 'ba',
Chris@0 99 [
Chris@0 100 '#markup' => 'bb',
Chris@0 101 '#wrapper_attributes' => ['class' => ['item-class-bb']],
Chris@0 102 ],
Chris@0 103 ],
Chris@0 104 ],
Chris@0 105 ],
Chris@0 106 // However, items can also be child #items.
Chris@0 107 [
Chris@0 108 '#markup' => 'c',
Chris@0 109 'childlist' => [
Chris@0 110 '#attributes' => ['id' => 'clist'],
Chris@0 111 'ca',
Chris@0 112 [
Chris@0 113 '#markup' => 'cb',
Chris@0 114 '#wrapper_attributes' => ['class' => ['item-class-cb']],
Chris@0 115 'children' => [
Chris@0 116 'cba',
Chris@0 117 'cbb',
Chris@0 118 ],
Chris@0 119 ],
Chris@0 120 'cc',
Chris@0 121 ],
Chris@0 122 ],
Chris@0 123 // Use #markup to be able to specify #wrapper_attributes.
Chris@0 124 [
Chris@0 125 '#markup' => 'd',
Chris@0 126 '#wrapper_attributes' => ['id' => 'item-id-d'],
Chris@0 127 ],
Chris@0 128 // An empty item with attributes.
Chris@0 129 [
Chris@0 130 '#wrapper_attributes' => ['id' => 'item-id-e'],
Chris@0 131 ],
Chris@0 132 // Lastly, another plain string item.
Chris@0 133 'f',
Chris@0 134 ];
Chris@0 135
Chris@0 136 $inner_b = '<div class="item-list"><ol id="blist">';
Chris@0 137 $inner_b .= '<li>ba</li>';
Chris@0 138 $inner_b .= '<li class="item-class-bb">bb</li>';
Chris@0 139 $inner_b .= '</ol></div>';
Chris@0 140
Chris@0 141 $inner_cb = '<div class="item-list"><ul>';
Chris@0 142 $inner_cb .= '<li>cba</li>';
Chris@0 143 $inner_cb .= '<li>cbb</li>';
Chris@0 144 $inner_cb .= '</ul></div>';
Chris@0 145
Chris@0 146 $inner_c = '<div class="item-list"><ul id="clist">';
Chris@0 147 $inner_c .= '<li>ca</li>';
Chris@0 148 $inner_c .= '<li class="item-class-cb">cb' . $inner_cb . '</li>';
Chris@0 149 $inner_c .= '<li>cc</li>';
Chris@0 150 $inner_c .= '</ul></div>';
Chris@0 151
Chris@0 152 $expected = '<div class="item-list">';
Chris@0 153 $expected .= '<h3>Some title</h3>';
Chris@0 154 $expected .= '<ul id="parentlist">';
Chris@0 155 $expected .= '<li>a</li>';
Chris@0 156 $expected .= '<li id="item-id-b">b' . $inner_b . '</li>';
Chris@0 157 $expected .= '<li>c' . $inner_c . '</li>';
Chris@0 158 $expected .= '<li id="item-id-d">d</li>';
Chris@0 159 $expected .= '<li id="item-id-e"></li>';
Chris@0 160 $expected .= '<li>f</li>';
Chris@0 161 $expected .= '</ul></div>';
Chris@0 162
Chris@0 163 $this->assertThemeOutput('item_list', $variables, $expected);
Chris@0 164 }
Chris@0 165
Chris@0 166 /**
Chris@0 167 * Tests links.html.twig.
Chris@0 168 */
Chris@0 169 public function testLinks() {
Chris@0 170 // Turn off the query for the
Chris@0 171 // \Drupal\Core\Utility\LinkGeneratorInterface::generate() method to compare
Chris@0 172 // the active link correctly.
Chris@0 173 $original_query = \Drupal::request()->query->all();
Chris@0 174 \Drupal::request()->query->replace([]);
Chris@0 175 // Verify that empty variables produce no output.
Chris@0 176 $variables = [];
Chris@0 177 $expected = '';
Chris@0 178 $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback generates no output.');
Chris@0 179
Chris@0 180 $variables = [];
Chris@0 181 $variables['heading'] = 'Some title';
Chris@0 182 $expected = '';
Chris@0 183 $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback with heading generates no output.');
Chris@0 184
Chris@0 185 // Verify that a list of links is properly rendered.
Chris@0 186 $variables = [];
Chris@0 187 $variables['attributes'] = ['id' => 'somelinks'];
Chris@0 188 $variables['links'] = [
Chris@0 189 'a link' => [
Chris@0 190 'title' => 'A <link>',
Chris@0 191 'url' => Url::fromUri('base:a/link'),
Chris@0 192 ],
Chris@0 193 'plain text' => [
Chris@0 194 'title' => 'Plain "text"',
Chris@0 195 ],
Chris@0 196 'html text' => [
Chris@0 197 'title' => SafeMarkup::format('<span class="unescaped">@text</span>', ['@text' => 'potentially unsafe text that <should> be escaped']),
Chris@0 198 ],
Chris@0 199 'front page' => [
Chris@0 200 'title' => 'Front page',
Chris@0 201 'url' => Url::fromRoute('<front>'),
Chris@0 202 ],
Chris@0 203 'router-test' => [
Chris@0 204 'title' => 'Test route',
Chris@0 205 'url' => Url::fromRoute('router_test.1'),
Chris@0 206 ],
Chris@0 207 'query-test' => [
Chris@0 208 'title' => 'Query test route',
Chris@0 209 'url' => Url::fromRoute('router_test.1'),
Chris@0 210 'query' => [
Chris@0 211 'key' => 'value',
Chris@0 212 ]
Chris@0 213 ],
Chris@0 214 ];
Chris@0 215
Chris@0 216 $expected_links = '';
Chris@0 217 $expected_links .= '<ul id="somelinks">';
Chris@0 218 $expected_links .= '<li class="a-link"><a href="' . Url::fromUri('base:a/link')->toString() . '">' . Html::escape('A <link>') . '</a></li>';
Chris@0 219 $expected_links .= '<li class="plain-text">' . Html::escape('Plain "text"') . '</li>';
Chris@0 220 $expected_links .= '<li class="html-text"><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
Chris@0 221 $expected_links .= '<li class="front-page"><a href="' . Url::fromRoute('<front>')->toString() . '">' . Html::escape('Front page') . '</a></li>';
Chris@0 222 $expected_links .= '<li class="router-test"><a href="' . \Drupal::urlGenerator()->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
Chris@0 223 $query = ['key' => 'value'];
Chris@0 224 $expected_links .= '<li class="query-test"><a href="' . \Drupal::urlGenerator()->generate('router_test.1', $query) . '">' . Html::escape('Query test route') . '</a></li>';
Chris@0 225 $expected_links .= '</ul>';
Chris@0 226
Chris@0 227 // Verify that passing a string as heading works.
Chris@0 228 $variables['heading'] = 'Links heading';
Chris@0 229 $expected_heading = '<h2>Links heading</h2>';
Chris@0 230 $expected = $expected_heading . $expected_links;
Chris@0 231 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 232
Chris@0 233 // Restore the original request's query.
Chris@0 234 \Drupal::request()->query->replace($original_query);
Chris@0 235
Chris@0 236 // Verify that passing an array as heading works (core support).
Chris@0 237 $variables['heading'] = [
Chris@0 238 'text' => 'Links heading',
Chris@0 239 'level' => 'h3',
Chris@0 240 'attributes' => ['class' => ['heading']],
Chris@0 241 ];
Chris@0 242 $expected_heading = '<h3 class="heading">Links heading</h3>';
Chris@0 243 $expected = $expected_heading . $expected_links;
Chris@0 244 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 245
Chris@0 246 // Verify that passing attributes for the heading works.
Chris@0 247 $variables['heading'] = ['text' => 'Links heading', 'level' => 'h3', 'attributes' => ['id' => 'heading']];
Chris@0 248 $expected_heading = '<h3 id="heading">Links heading</h3>';
Chris@0 249 $expected = $expected_heading . $expected_links;
Chris@0 250 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 251
Chris@0 252 // Verify that passing attributes for the links work.
Chris@0 253 $variables['links']['plain text']['attributes'] = [
Chris@0 254 'class' => ['a/class'],
Chris@0 255 ];
Chris@0 256 $expected_links = '';
Chris@0 257 $expected_links .= '<ul id="somelinks">';
Chris@0 258 $expected_links .= '<li class="a-link"><a href="' . Url::fromUri('base:a/link')->toString() . '">' . Html::escape('A <link>') . '</a></li>';
Chris@0 259 $expected_links .= '<li class="plain-text"><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
Chris@0 260 $expected_links .= '<li class="html-text"><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
Chris@0 261 $expected_links .= '<li class="front-page"><a href="' . Url::fromRoute('<front>')->toString() . '">' . Html::escape('Front page') . '</a></li>';
Chris@0 262 $expected_links .= '<li class="router-test"><a href="' . \Drupal::urlGenerator()->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
Chris@0 263 $query = ['key' => 'value'];
Chris@0 264 $expected_links .= '<li class="query-test"><a href="' . \Drupal::urlGenerator()->generate('router_test.1', $query) . '">' . Html::escape('Query test route') . '</a></li>';
Chris@0 265 $expected_links .= '</ul>';
Chris@0 266 $expected = $expected_heading . $expected_links;
Chris@0 267 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 268
Chris@0 269 // Verify the data- attributes for setting the "active" class on links.
Chris@0 270 \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
Chris@0 271 $variables['set_active_class'] = TRUE;
Chris@0 272 $expected_links = '';
Chris@0 273 $expected_links .= '<ul id="somelinks">';
Chris@0 274 $expected_links .= '<li class="a-link"><a href="' . Url::fromUri('base:a/link')->toString() . '">' . Html::escape('A <link>') . '</a></li>';
Chris@0 275 $expected_links .= '<li class="plain-text"><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
Chris@0 276 $expected_links .= '<li class="html-text"><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
Chris@0 277 $expected_links .= '<li data-drupal-link-system-path="&lt;front&gt;" class="front-page"><a href="' . Url::fromRoute('<front>')->toString() . '" data-drupal-link-system-path="&lt;front&gt;">' . Html::escape('Front page') . '</a></li>';
Chris@0 278 $expected_links .= '<li data-drupal-link-system-path="router_test/test1" class="router-test"><a href="' . \Drupal::urlGenerator()->generate('router_test.1') . '" data-drupal-link-system-path="router_test/test1">' . Html::escape('Test route') . '</a></li>';
Chris@0 279 $query = ['key' => 'value'];
Chris@0 280 $encoded_query = Html::escape(Json::encode($query));
Chris@0 281 $expected_links .= '<li data-drupal-link-query="' . $encoded_query . '" data-drupal-link-system-path="router_test/test1" class="query-test"><a href="' . \Drupal::urlGenerator()->generate('router_test.1', $query) . '" data-drupal-link-query="' . $encoded_query . '" data-drupal-link-system-path="router_test/test1">' . Html::escape('Query test route') . '</a></li>';
Chris@0 282 $expected_links .= '</ul>';
Chris@0 283 $expected = $expected_heading . $expected_links;
Chris@0 284 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * Tests links.html.twig using links with indexed keys.
Chris@0 289 */
Chris@0 290 public function testIndexedKeyedLinks() {
Chris@0 291 // Turn off the query for the
Chris@0 292 // \Drupal\Core\Utility\LinkGeneratorInterface::generate() method to compare
Chris@0 293 // the active link correctly.
Chris@0 294 $original_query = \Drupal::request()->query->all();
Chris@0 295 \Drupal::request()->query->replace([]);
Chris@0 296 // Verify that empty variables produce no output.
Chris@0 297 $variables = [];
Chris@0 298 $expected = '';
Chris@0 299 $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback generates no output.');
Chris@0 300
Chris@0 301 $variables = [];
Chris@0 302 $variables['heading'] = 'Some title';
Chris@0 303 $expected = '';
Chris@0 304 $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback with heading generates no output.');
Chris@0 305
Chris@0 306 // Verify that a list of links is properly rendered.
Chris@0 307 $variables = [];
Chris@0 308 $variables['attributes'] = ['id' => 'somelinks'];
Chris@0 309 $variables['links'] = [
Chris@0 310 [
Chris@0 311 'title' => 'A <link>',
Chris@0 312 'url' => Url::fromUri('base:a/link'),
Chris@0 313 ],
Chris@0 314 [
Chris@0 315 'title' => 'Plain "text"',
Chris@0 316 ],
Chris@0 317 [
Chris@0 318 'title' => SafeMarkup::format('<span class="unescaped">@text</span>', ['@text' => 'potentially unsafe text that <should> be escaped']),
Chris@0 319 ],
Chris@0 320 [
Chris@0 321 'title' => 'Front page',
Chris@0 322 'url' => Url::fromRoute('<front>'),
Chris@0 323 ],
Chris@0 324 [
Chris@0 325 'title' => 'Test route',
Chris@0 326 'url' => Url::fromRoute('router_test.1'),
Chris@0 327 ],
Chris@0 328 [
Chris@0 329 'title' => 'Query test route',
Chris@0 330 'url' => Url::fromRoute('router_test.1'),
Chris@0 331 'query' => [
Chris@0 332 'key' => 'value',
Chris@0 333 ]
Chris@0 334 ],
Chris@0 335 ];
Chris@0 336
Chris@0 337 $expected_links = '';
Chris@0 338 $expected_links .= '<ul id="somelinks">';
Chris@0 339 $expected_links .= '<li><a href="' . Url::fromUri('base:a/link')->toString() . '">' . Html::escape('A <link>') . '</a></li>';
Chris@0 340 $expected_links .= '<li>' . Html::escape('Plain "text"') . '</li>';
Chris@0 341 $expected_links .= '<li><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
Chris@0 342 $expected_links .= '<li><a href="' . Url::fromRoute('<front>')->toString() . '">' . Html::escape('Front page') . '</a></li>';
Chris@0 343 $expected_links .= '<li><a href="' . \Drupal::urlGenerator()->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
Chris@0 344 $query = ['key' => 'value'];
Chris@0 345 $expected_links .= '<li><a href="' . \Drupal::urlGenerator()->generate('router_test.1', $query) . '">' . Html::escape('Query test route') . '</a></li>';
Chris@0 346 $expected_links .= '</ul>';
Chris@0 347
Chris@0 348 // Verify that passing a string as heading works.
Chris@0 349 $variables['heading'] = 'Links heading';
Chris@0 350 $expected_heading = '<h2>Links heading</h2>';
Chris@0 351 $expected = $expected_heading . $expected_links;
Chris@0 352 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 353
Chris@0 354 // Restore the original request's query.
Chris@0 355 \Drupal::request()->query->replace($original_query);
Chris@0 356
Chris@0 357 // Verify that passing an array as heading works (core support).
Chris@0 358 $variables['heading'] = [
Chris@0 359 'text' => 'Links heading',
Chris@0 360 'level' => 'h3',
Chris@0 361 'attributes' => ['class' => ['heading']],
Chris@0 362 ];
Chris@0 363 $expected_heading = '<h3 class="heading">Links heading</h3>';
Chris@0 364 $expected = $expected_heading . $expected_links;
Chris@0 365 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 366
Chris@0 367 // Verify that passing attributes for the heading works.
Chris@0 368 $variables['heading'] = ['text' => 'Links heading', 'level' => 'h3', 'attributes' => ['id' => 'heading']];
Chris@0 369 $expected_heading = '<h3 id="heading">Links heading</h3>';
Chris@0 370 $expected = $expected_heading . $expected_links;
Chris@0 371 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 372
Chris@0 373 // Verify that passing attributes for the links work.
Chris@0 374 $variables['links'][1]['attributes'] = [
Chris@0 375 'class' => ['a/class'],
Chris@0 376 ];
Chris@0 377 $expected_links = '';
Chris@0 378 $expected_links .= '<ul id="somelinks">';
Chris@0 379 $expected_links .= '<li><a href="' . Url::fromUri('base:a/link')->toString() . '">' . Html::escape('A <link>') . '</a></li>';
Chris@0 380 $expected_links .= '<li><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
Chris@0 381 $expected_links .= '<li><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
Chris@0 382 $expected_links .= '<li><a href="' . Url::fromRoute('<front>')->toString() . '">' . Html::escape('Front page') . '</a></li>';
Chris@0 383 $expected_links .= '<li><a href="' . \Drupal::urlGenerator()->generate('router_test.1') . '">' . Html::escape('Test route') . '</a></li>';
Chris@0 384 $query = ['key' => 'value'];
Chris@0 385 $expected_links .= '<li><a href="' . \Drupal::urlGenerator()->generate('router_test.1', $query) . '">' . Html::escape('Query test route') . '</a></li>';
Chris@0 386 $expected_links .= '</ul>';
Chris@0 387 $expected = $expected_heading . $expected_links;
Chris@0 388 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 389
Chris@0 390 // Verify the data- attributes for setting the "active" class on links.
Chris@0 391 \Drupal::currentUser()->setAccount(new UserSession(['uid' => 1]));
Chris@0 392 $variables['set_active_class'] = TRUE;
Chris@0 393 $expected_links = '';
Chris@0 394 $expected_links .= '<ul id="somelinks">';
Chris@0 395 $expected_links .= '<li><a href="' . Url::fromUri('base:a/link')->toString() . '">' . Html::escape('A <link>') . '</a></li>';
Chris@0 396 $expected_links .= '<li><span class="a/class">' . Html::escape('Plain "text"') . '</span></li>';
Chris@0 397 $expected_links .= '<li><span class="unescaped">' . Html::escape('potentially unsafe text that <should> be escaped') . '</span></li>';
Chris@0 398 $expected_links .= '<li data-drupal-link-system-path="&lt;front&gt;"><a href="' . Url::fromRoute('<front>')->toString() . '" data-drupal-link-system-path="&lt;front&gt;">' . Html::escape('Front page') . '</a></li>';
Chris@0 399 $expected_links .= '<li data-drupal-link-system-path="router_test/test1"><a href="' . \Drupal::urlGenerator()->generate('router_test.1') . '" data-drupal-link-system-path="router_test/test1">' . Html::escape('Test route') . '</a></li>';
Chris@0 400 $query = ['key' => 'value'];
Chris@0 401 $encoded_query = Html::escape(Json::encode($query));
Chris@0 402 $expected_links .= '<li data-drupal-link-query="' . $encoded_query . '" data-drupal-link-system-path="router_test/test1"><a href="' . \Drupal::urlGenerator()->generate('router_test.1', $query) . '" data-drupal-link-query="' . $encoded_query . '" data-drupal-link-system-path="router_test/test1">' . Html::escape('Query test route') . '</a></li>';
Chris@0 403 $expected_links .= '</ul>';
Chris@0 404 $expected = $expected_heading . $expected_links;
Chris@0 405 $this->assertThemeOutput('links', $variables, $expected);
Chris@0 406 }
Chris@0 407
Chris@0 408 /**
Chris@0 409 * Test the use of drupal_pre_render_links() on a nested array of links.
Chris@0 410 */
Chris@0 411 public function testDrupalPreRenderLinks() {
Chris@0 412 // Define the base array to be rendered, containing a variety of different
Chris@0 413 // kinds of links.
Chris@0 414 $base_array = [
Chris@0 415 '#theme' => 'links',
Chris@0 416 '#pre_render' => ['drupal_pre_render_links'],
Chris@0 417 '#links' => [
Chris@0 418 'parent_link' => [
Chris@0 419 'title' => 'Parent link original',
Chris@0 420 'url' => Url::fromRoute('router_test.1'),
Chris@0 421 ],
Chris@0 422 ],
Chris@0 423 'first_child' => [
Chris@0 424 '#theme' => 'links',
Chris@0 425 '#links' => [
Chris@0 426 // This should be rendered if 'first_child' is rendered separately,
Chris@0 427 // but ignored if the parent is being rendered (since it duplicates
Chris@0 428 // one of the parent's links).
Chris@0 429 'parent_link' => [
Chris@0 430 'title' => 'Parent link copy',
Chris@0 431 'url' => Url::fromRoute('router_test.6'),
Chris@0 432 ],
Chris@0 433 // This should always be rendered.
Chris@0 434 'first_child_link' => [
Chris@0 435 'title' => 'First child link',
Chris@0 436 'url' => Url::fromRoute('router_test.7'),
Chris@0 437 ],
Chris@0 438 ],
Chris@0 439 ],
Chris@0 440 // This should always be rendered as part of the parent.
Chris@0 441 'second_child' => [
Chris@0 442 '#theme' => 'links',
Chris@0 443 '#links' => [
Chris@0 444 'second_child_link' => [
Chris@0 445 'title' => 'Second child link',
Chris@0 446 'url' => Url::fromRoute('router_test.8'),
Chris@0 447 ],
Chris@0 448 ],
Chris@0 449 ],
Chris@0 450 // This should never be rendered, since the user does not have access to
Chris@0 451 // it.
Chris@0 452 'third_child' => [
Chris@0 453 '#theme' => 'links',
Chris@0 454 '#links' => [
Chris@0 455 'third_child_link' => [
Chris@0 456 'title' => 'Third child link',
Chris@0 457 'url' => Url::fromRoute('router_test.9'),
Chris@0 458 ],
Chris@0 459 ],
Chris@0 460 '#access' => FALSE,
Chris@0 461 ],
Chris@0 462 ];
Chris@0 463
Chris@0 464 // Start with a fresh copy of the base array, and try rendering the entire
Chris@0 465 // thing. We expect a single <ul> with appropriate links contained within
Chris@0 466 // it.
Chris@0 467 $render_array = $base_array;
Chris@0 468 $html = \Drupal::service('renderer')->renderRoot($render_array);
Chris@0 469 $dom = new \DOMDocument();
Chris@0 470 $dom->loadHTML($html);
Chris@0 471 $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered HTML.');
Chris@0 472 $list_elements = $dom->getElementsByTagName('li');
Chris@0 473 $this->assertEqual($list_elements->length, 3, 'Three "li" tags found in the rendered HTML.');
Chris@0 474 $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
Chris@0 475 $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.');
Chris@0 476 $this->assertEqual($list_elements->item(2)->nodeValue, 'Second child link', 'Third expected link found.');
Chris@0 477 $this->assertIdentical(strpos($html, 'Parent link copy'), FALSE, '"Parent link copy" link not found.');
Chris@0 478 $this->assertIdentical(strpos($html, 'Third child link'), FALSE, '"Third child link" link not found.');
Chris@0 479
Chris@0 480 // Now render 'first_child', followed by the rest of the links, and make
Chris@0 481 // sure we get two separate <ul>'s with the appropriate links contained
Chris@0 482 // within each.
Chris@0 483 $render_array = $base_array;
Chris@0 484 $child_html = \Drupal::service('renderer')->renderRoot($render_array['first_child']);
Chris@0 485 $parent_html = \Drupal::service('renderer')->renderRoot($render_array);
Chris@0 486 // First check the child HTML.
Chris@0 487 $dom = new \DOMDocument();
Chris@0 488 $dom->loadHTML($child_html);
Chris@0 489 $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered child HTML.');
Chris@0 490 $list_elements = $dom->getElementsByTagName('li');
Chris@0 491 $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered child HTML.');
Chris@0 492 $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link copy', 'First expected link found.');
Chris@0 493 $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.');
Chris@0 494 // Then check the parent HTML.
Chris@0 495 $dom = new \DOMDocument();
Chris@0 496 $dom->loadHTML($parent_html);
Chris@0 497 $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered parent HTML.');
Chris@0 498 $list_elements = $dom->getElementsByTagName('li');
Chris@0 499 $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered parent HTML.');
Chris@0 500 $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
Chris@0 501 $this->assertEqual($list_elements->item(1)->nodeValue, 'Second child link', 'Second expected link found.');
Chris@0 502 $this->assertIdentical(strpos($parent_html, 'First child link'), FALSE, '"First child link" link not found.');
Chris@0 503 $this->assertIdentical(strpos($parent_html, 'Third child link'), FALSE, '"Third child link" link not found.');
Chris@0 504 }
Chris@0 505
Chris@0 506 /**
Chris@0 507 * Tests theme_image().
Chris@0 508 */
Chris@0 509 public function testImage() {
Chris@0 510 // Test that data URIs work with theme_image().
Chris@0 511 $variables = [];
Chris@0 512 $variables['uri'] = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
Chris@0 513 $variables['alt'] = 'Data URI image of a red dot';
Chris@0 514 $expected = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Data URI image of a red dot" />' . "\n";
Chris@0 515 $this->assertThemeOutput('image', $variables, $expected);
Chris@0 516 }
Chris@0 517
Chris@0 518 }