PageRenderTime 66ms CodeModel.GetById 23ms RepoModel.GetById 3ms app.codeStats 0ms

/core/modules/link/src/Tests/LinkFieldTest.php

http://github.com/drupal/drupal
PHP | 635 lines | 456 code | 55 blank | 124 comment | 12 complexity | 3c07afa11b766e4fe104f88e75fcaed6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\link\Tests;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Component\Utility\Unicode;
  5. use Drupal\Core\Url;
  6. use Drupal\entity_test\Entity\EntityTest;
  7. use Drupal\field\Entity\FieldConfig;
  8. use Drupal\link\LinkItemInterface;
  9. use Drupal\simpletest\WebTestBase;
  10. use Drupal\field\Entity\FieldStorageConfig;
  11. /**
  12. * Tests link field widgets and formatters.
  13. *
  14. * @group link
  15. */
  16. class LinkFieldTest extends WebTestBase {
  17. /**
  18. * Modules to enable.
  19. *
  20. * @var array
  21. */
  22. public static $modules = ['entity_test', 'link', 'node'];
  23. /**
  24. * A field to use in this test class.
  25. *
  26. * @var \Drupal\field\Entity\FieldStorageConfig
  27. */
  28. protected $fieldStorage;
  29. /**
  30. * The instance used in this test class.
  31. *
  32. * @var \Drupal\field\Entity\FieldConfig
  33. */
  34. protected $field;
  35. protected function setUp() {
  36. parent::setUp();
  37. $this->drupalLogin($this->drupalCreateUser([
  38. 'view test entity',
  39. 'administer entity_test content',
  40. 'link to any page',
  41. ]));
  42. }
  43. /**
  44. * Tests link field URL validation.
  45. */
  46. function testURLValidation() {
  47. $field_name = Unicode::strtolower($this->randomMachineName());
  48. // Create a field with settings to validate.
  49. $this->fieldStorage = FieldStorageConfig::create(array(
  50. 'field_name' => $field_name,
  51. 'entity_type' => 'entity_test',
  52. 'type' => 'link',
  53. ));
  54. $this->fieldStorage->save();
  55. $this->field = FieldConfig::create([
  56. 'field_storage' => $this->fieldStorage,
  57. 'bundle' => 'entity_test',
  58. 'settings' => array(
  59. 'title' => DRUPAL_DISABLED,
  60. 'link_type' => LinkItemInterface::LINK_GENERIC,
  61. ),
  62. ]);
  63. $this->field->save();
  64. entity_get_form_display('entity_test', 'entity_test', 'default')
  65. ->setComponent($field_name, array(
  66. 'type' => 'link_default',
  67. 'settings' => array(
  68. 'placeholder_url' => 'http://example.com',
  69. ),
  70. ))
  71. ->save();
  72. entity_get_display('entity_test', 'entity_test', 'full')
  73. ->setComponent($field_name, array(
  74. 'type' => 'link',
  75. ))
  76. ->save();
  77. // Display creation form.
  78. $this->drupalGet('entity_test/add');
  79. $this->assertFieldByName("{$field_name}[0][uri]", '', 'Link URL field is displayed');
  80. $this->assertRaw('placeholder="http://example.com"');
  81. // Create a path alias.
  82. \Drupal::service('path.alias_storage')->save('/admin', '/a/path/alias');
  83. // Create a node to test the link widget.
  84. $node = $this->drupalCreateNode();
  85. // Create an entity with restricted view access.
  86. $entity_test_no_label_access = EntityTest::create([
  87. 'name' => 'forbid_access',
  88. ]);
  89. $entity_test_no_label_access->save();
  90. // Define some valid URLs (keys are the entered values, values are the
  91. // strings displayed to the user).
  92. $valid_external_entries = array(
  93. 'http://www.example.com/' => 'http://www.example.com/',
  94. // Strings within parenthesis without leading space char.
  95. 'http://www.example.com/strings_(string_within_parenthesis)' => 'http://www.example.com/strings_(string_within_parenthesis)',
  96. // Numbers within parenthesis without leading space char.
  97. 'http://www.example.com/numbers_(9999)' => 'http://www.example.com/numbers_(9999)',
  98. );
  99. $valid_internal_entries = array(
  100. '/entity_test/add' => '/entity_test/add',
  101. '/a/path/alias' => '/a/path/alias',
  102. // Front page, with query string and fragment.
  103. '/' => '&lt;front&gt;',
  104. '/?example=llama' => '&lt;front&gt;?example=llama',
  105. '/#example' => '&lt;front&gt;#example',
  106. // @todo '<front>' is valid input for BC reasons, may be removed by
  107. // https://www.drupal.org/node/2421941
  108. '<front>' => '&lt;front&gt;',
  109. '<front>#example' => '&lt;front&gt;#example',
  110. '<front>?example=llama' => '&lt;front&gt;?example=llama',
  111. // Query string and fragment.
  112. '?example=llama' => '?example=llama',
  113. '#example' => '#example',
  114. // Entity reference autocomplete value.
  115. $node->label() . ' (1)' => $node->label() . ' (1)',
  116. // Entity URI displayed as ER autocomplete value when displayed in a form.
  117. 'entity:node/1' => $node->label() . ' (1)',
  118. // URI for an entity that exists, but is not accessible by the user.
  119. 'entity:entity_test/' . $entity_test_no_label_access->id() => '- Restricted access - (' . $entity_test_no_label_access->id() . ')',
  120. // URI for an entity that doesn't exist, but with a valid ID.
  121. 'entity:user/999999' => 'entity:user/999999',
  122. );
  123. // Define some invalid URLs.
  124. $validation_error_1 = "The path '@link_path' is invalid.";
  125. $validation_error_2 = 'Manually entered paths should start with /, ? or #.';
  126. $validation_error_3 = "The path '@link_path' is inaccessible.";
  127. $invalid_external_entries = array(
  128. // Invalid protocol
  129. 'invalid://not-a-valid-protocol' => $validation_error_1,
  130. // Missing host name
  131. 'http://' => $validation_error_1,
  132. );
  133. $invalid_internal_entries = array(
  134. 'no-leading-slash' => $validation_error_2,
  135. 'entity:non_existing_entity_type/yar' => $validation_error_1,
  136. // URI for an entity that doesn't exist, with an invalid ID.
  137. 'entity:user/invalid-parameter' => $validation_error_1,
  138. );
  139. // Test external and internal URLs for 'link_type' = LinkItemInterface::LINK_GENERIC.
  140. $this->assertValidEntries($field_name, $valid_external_entries + $valid_internal_entries);
  141. $this->assertInvalidEntries($field_name, $invalid_external_entries + $invalid_internal_entries);
  142. // Test external URLs for 'link_type' = LinkItemInterface::LINK_EXTERNAL.
  143. $this->field->setSetting('link_type', LinkItemInterface::LINK_EXTERNAL);
  144. $this->field->save();
  145. $this->assertValidEntries($field_name, $valid_external_entries);
  146. $this->assertInvalidEntries($field_name, $valid_internal_entries + $invalid_external_entries);
  147. // Test external URLs for 'link_type' = LinkItemInterface::LINK_INTERNAL.
  148. $this->field->setSetting('link_type', LinkItemInterface::LINK_INTERNAL);
  149. $this->field->save();
  150. $this->assertValidEntries($field_name, $valid_internal_entries);
  151. $this->assertInvalidEntries($field_name, $valid_external_entries + $invalid_internal_entries);
  152. // Ensure that users with 'link to any page', don't apply access checking.
  153. $this->drupalLogin($this->drupalCreateUser([
  154. 'view test entity',
  155. 'administer entity_test content',
  156. ]));
  157. $this->assertValidEntries($field_name, ['/entity_test/add' => '/entity_test/add']);
  158. $this->assertInValidEntries($field_name, ['/admin' => $validation_error_3]);
  159. }
  160. /**
  161. * Asserts that valid URLs can be submitted.
  162. *
  163. * @param string $field_name
  164. * The field name.
  165. * @param array $valid_entries
  166. * An array of valid URL entries.
  167. */
  168. protected function assertValidEntries($field_name, array $valid_entries) {
  169. foreach ($valid_entries as $uri => $string) {
  170. $edit = array(
  171. "{$field_name}[0][uri]" => $uri,
  172. );
  173. $this->drupalPostForm('entity_test/add', $edit, t('Save'));
  174. preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
  175. $id = $match[1];
  176. $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
  177. $this->assertRaw($string);
  178. }
  179. }
  180. /**
  181. * Asserts that invalid URLs cannot be submitted.
  182. *
  183. * @param string $field_name
  184. * The field name.
  185. * @param array $invalid_entries
  186. * An array of invalid URL entries.
  187. */
  188. protected function assertInvalidEntries($field_name, array $invalid_entries) {
  189. foreach ($invalid_entries as $invalid_value => $error_message) {
  190. $edit = array(
  191. "{$field_name}[0][uri]" => $invalid_value,
  192. );
  193. $this->drupalPostForm('entity_test/add', $edit, t('Save'));
  194. $this->assertText(t($error_message, array('@link_path' => $invalid_value)));
  195. }
  196. }
  197. /**
  198. * Tests the link title settings of a link field.
  199. */
  200. function testLinkTitle() {
  201. $field_name = Unicode::strtolower($this->randomMachineName());
  202. // Create a field with settings to validate.
  203. $this->fieldStorage = FieldStorageConfig::create(array(
  204. 'field_name' => $field_name,
  205. 'entity_type' => 'entity_test',
  206. 'type' => 'link',
  207. ));
  208. $this->fieldStorage->save();
  209. $this->field = FieldConfig::create([
  210. 'field_storage' => $this->fieldStorage,
  211. 'bundle' => 'entity_test',
  212. 'label' => 'Read more about this entity',
  213. 'settings' => array(
  214. 'title' => DRUPAL_OPTIONAL,
  215. 'link_type' => LinkItemInterface::LINK_GENERIC,
  216. ),
  217. ]);
  218. $this->field->save();
  219. entity_get_form_display('entity_test', 'entity_test', 'default')
  220. ->setComponent($field_name, array(
  221. 'type' => 'link_default',
  222. 'settings' => array(
  223. 'placeholder_url' => 'http://example.com',
  224. 'placeholder_title' => 'Enter the text for this link',
  225. ),
  226. ))
  227. ->save();
  228. entity_get_display('entity_test', 'entity_test', 'full')
  229. ->setComponent($field_name, array(
  230. 'type' => 'link',
  231. 'label' => 'hidden',
  232. ))
  233. ->save();
  234. // Verify that the link text field works according to the field setting.
  235. foreach (array(DRUPAL_DISABLED, DRUPAL_REQUIRED, DRUPAL_OPTIONAL) as $title_setting) {
  236. // Update the link title field setting.
  237. $this->field->setSetting('title', $title_setting);
  238. $this->field->save();
  239. // Display creation form.
  240. $this->drupalGet('entity_test/add');
  241. // Assert label is shown.
  242. $this->assertText('Read more about this entity');
  243. $this->assertFieldByName("{$field_name}[0][uri]", '', 'URL field found.');
  244. $this->assertRaw('placeholder="http://example.com"');
  245. if ($title_setting === DRUPAL_DISABLED) {
  246. $this->assertNoFieldByName("{$field_name}[0][title]", '', 'Link text field not found.');
  247. $this->assertNoRaw('placeholder="Enter the text for this link"');
  248. }
  249. else {
  250. $this->assertRaw('placeholder="Enter the text for this link"');
  251. $this->assertFieldByName("{$field_name}[0][title]", '', 'Link text field found.');
  252. if ($title_setting === DRUPAL_REQUIRED) {
  253. // Verify that the link text is required, if the URL is non-empty.
  254. $edit = array(
  255. "{$field_name}[0][uri]" => 'http://www.example.com',
  256. );
  257. $this->drupalPostForm(NULL, $edit, t('Save'));
  258. $this->assertText(t('@name field is required.', array('@name' => t('Link text'))));
  259. // Verify that the link text is not required, if the URL is empty.
  260. $edit = array(
  261. "{$field_name}[0][uri]" => '',
  262. );
  263. $this->drupalPostForm(NULL, $edit, t('Save'));
  264. $this->assertNoText(t('@name field is required.', array('@name' => t('Link text'))));
  265. // Verify that a URL and link text meets requirements.
  266. $this->drupalGet('entity_test/add');
  267. $edit = array(
  268. "{$field_name}[0][uri]" => 'http://www.example.com',
  269. "{$field_name}[0][title]" => 'Example',
  270. );
  271. $this->drupalPostForm(NULL, $edit, t('Save'));
  272. $this->assertNoText(t('@name field is required.', array('@name' => t('Link text'))));
  273. }
  274. }
  275. }
  276. // Verify that a link without link text is rendered using the URL as text.
  277. $value = 'http://www.example.com/';
  278. $edit = array(
  279. "{$field_name}[0][uri]" => $value,
  280. "{$field_name}[0][title]" => '',
  281. );
  282. $this->drupalPostForm(NULL, $edit, t('Save'));
  283. preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
  284. $id = $match[1];
  285. $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
  286. $this->renderTestEntity($id);
  287. $expected_link = \Drupal::l($value, Url::fromUri($value));
  288. $this->assertRaw($expected_link);
  289. // Verify that a link with text is rendered using the link text.
  290. $title = $this->randomMachineName();
  291. $edit = array(
  292. "{$field_name}[0][title]" => $title,
  293. );
  294. $this->drupalPostForm("entity_test/manage/$id/edit", $edit, t('Save'));
  295. $this->assertText(t('entity_test @id has been updated.', array('@id' => $id)));
  296. $this->renderTestEntity($id);
  297. $expected_link = \Drupal::l($title, Url::fromUri($value));
  298. $this->assertRaw($expected_link);
  299. }
  300. /**
  301. * Tests the default 'link' formatter.
  302. */
  303. function testLinkFormatter() {
  304. $field_name = Unicode::strtolower($this->randomMachineName());
  305. // Create a field with settings to validate.
  306. $this->fieldStorage = FieldStorageConfig::create(array(
  307. 'field_name' => $field_name,
  308. 'entity_type' => 'entity_test',
  309. 'type' => 'link',
  310. 'cardinality' => 3,
  311. ));
  312. $this->fieldStorage->save();
  313. FieldConfig::create([
  314. 'field_storage' => $this->fieldStorage,
  315. 'label' => 'Read more about this entity',
  316. 'bundle' => 'entity_test',
  317. 'settings' => array(
  318. 'title' => DRUPAL_OPTIONAL,
  319. 'link_type' => LinkItemInterface::LINK_GENERIC,
  320. ),
  321. ])->save();
  322. entity_get_form_display('entity_test', 'entity_test', 'default')
  323. ->setComponent($field_name, array(
  324. 'type' => 'link_default',
  325. ))
  326. ->save();
  327. $display_options = array(
  328. 'type' => 'link',
  329. 'label' => 'hidden',
  330. );
  331. entity_get_display('entity_test', 'entity_test', 'full')
  332. ->setComponent($field_name, $display_options)
  333. ->save();
  334. // Create an entity with three link field values:
  335. // - The first field item uses a URL only.
  336. // - The second field item uses a URL and link text.
  337. // - The third field item uses a fragment-only URL with text.
  338. // For consistency in assertion code below, the URL is assigned to the title
  339. // variable for the first field.
  340. $this->drupalGet('entity_test/add');
  341. $url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
  342. $url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
  343. $url3 = '#net';
  344. $title1 = $url1;
  345. // Intentionally contains an ampersand that needs sanitization on output.
  346. $title2 = 'A very long & strange example title that could break the nice layout of the site';
  347. $title3 = 'Fragment only';
  348. $edit = array(
  349. "{$field_name}[0][uri]" => $url1,
  350. // Note that $title1 is not submitted.
  351. "{$field_name}[0][title]" => '',
  352. "{$field_name}[1][uri]" => $url2,
  353. "{$field_name}[1][title]" => $title2,
  354. "{$field_name}[2][uri]" => $url3,
  355. "{$field_name}[2][title]" => $title3,
  356. );
  357. // Assert label is shown.
  358. $this->assertText('Read more about this entity');
  359. $this->drupalPostForm(NULL, $edit, t('Save'));
  360. preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
  361. $id = $match[1];
  362. $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
  363. // Verify that the link is output according to the formatter settings.
  364. // Not using generatePermutations(), since that leads to 32 cases, which
  365. // would not test actual link field formatter functionality but rather
  366. // the link generator and options/attributes. Only 'url_plain' has a
  367. // dependency on 'url_only', so we have a total of ~10 cases.
  368. $options = array(
  369. 'trim_length' => array(NULL, 6),
  370. 'rel' => array(NULL, 'nofollow'),
  371. 'target' => array(NULL, '_blank'),
  372. 'url_only' => array(
  373. array('url_only' => FALSE),
  374. array('url_only' => FALSE, 'url_plain' => TRUE),
  375. array('url_only' => TRUE),
  376. array('url_only' => TRUE, 'url_plain' => TRUE),
  377. ),
  378. );
  379. foreach ($options as $setting => $values) {
  380. foreach ($values as $new_value) {
  381. // Update the field formatter settings.
  382. if (!is_array($new_value)) {
  383. $display_options['settings'] = array($setting => $new_value);
  384. }
  385. else {
  386. $display_options['settings'] = $new_value;
  387. }
  388. entity_get_display('entity_test', 'entity_test', 'full')
  389. ->setComponent($field_name, $display_options)
  390. ->save();
  391. $this->renderTestEntity($id);
  392. switch ($setting) {
  393. case 'trim_length':
  394. $url = $url1;
  395. $title = isset($new_value) ? Unicode::truncate($title1, $new_value, FALSE, TRUE) : $title1;
  396. $this->assertRaw('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>');
  397. $url = $url2;
  398. $title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2;
  399. $this->assertRaw('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>');
  400. $url = $url3;
  401. $title = isset($new_value) ? Unicode::truncate($title3, $new_value, FALSE, TRUE) : $title3;
  402. $this->assertRaw('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>');
  403. break;
  404. case 'rel':
  405. $rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
  406. $this->assertRaw('<a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($title1) . '</a>');
  407. $this->assertRaw('<a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($title2) . '</a>');
  408. $this->assertRaw('<a href="' . Html::escape($url3) . '"' . $rel . '>' . Html::escape($title3) . '</a>');
  409. break;
  410. case 'target':
  411. $target = isset($new_value) ? ' target="' . $new_value . '"' : '';
  412. $this->assertRaw('<a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($title1) . '</a>');
  413. $this->assertRaw('<a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($title2) . '</a>');
  414. $this->assertRaw('<a href="' . Html::escape($url3) . '"' . $target . '>' . Html::escape($title3) . '</a>');
  415. break;
  416. case 'url_only':
  417. // In this case, $new_value is an array.
  418. if (!$new_value['url_only']) {
  419. $this->assertRaw('<a href="' . Html::escape($url1) . '">' . Html::escape($title1) . '</a>');
  420. $this->assertRaw('<a href="' . Html::escape($url2) . '">' . Html::escape($title2) . '</a>');
  421. $this->assertRaw('<a href="' . Html::escape($url3) . '">' . Html::escape($title3) . '</a>');
  422. }
  423. else {
  424. if (empty($new_value['url_plain'])) {
  425. $this->assertRaw('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>');
  426. $this->assertRaw('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>');
  427. $this->assertRaw('<a href="' . Html::escape($url3) . '">' . Html::escape($url3) . '</a>');
  428. }
  429. else {
  430. $this->assertNoRaw('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>');
  431. $this->assertNoRaw('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>');
  432. $this->assertNoRaw('<a href="' . Html::escape($url3) . '">' . Html::escape($url3) . '</a>');
  433. $this->assertEscaped($url1);
  434. $this->assertEscaped($url2);
  435. $this->assertEscaped($url3);
  436. }
  437. }
  438. break;
  439. }
  440. }
  441. }
  442. }
  443. /**
  444. * Tests the 'link_separate' formatter.
  445. *
  446. * This test is mostly the same as testLinkFormatter(), but they cannot be
  447. * merged, since they involve different configuration and output.
  448. */
  449. function testLinkSeparateFormatter() {
  450. $field_name = Unicode::strtolower($this->randomMachineName());
  451. // Create a field with settings to validate.
  452. $this->fieldStorage = FieldStorageConfig::create(array(
  453. 'field_name' => $field_name,
  454. 'entity_type' => 'entity_test',
  455. 'type' => 'link',
  456. 'cardinality' => 3,
  457. ));
  458. $this->fieldStorage->save();
  459. FieldConfig::create([
  460. 'field_storage' => $this->fieldStorage,
  461. 'bundle' => 'entity_test',
  462. 'settings' => array(
  463. 'title' => DRUPAL_OPTIONAL,
  464. 'link_type' => LinkItemInterface::LINK_GENERIC,
  465. ),
  466. ])->save();
  467. $display_options = array(
  468. 'type' => 'link_separate',
  469. 'label' => 'hidden',
  470. );
  471. entity_get_form_display('entity_test', 'entity_test', 'default')
  472. ->setComponent($field_name, array(
  473. 'type' => 'link_default',
  474. ))
  475. ->save();
  476. entity_get_display('entity_test', 'entity_test', 'full')
  477. ->setComponent($field_name, $display_options)
  478. ->save();
  479. // Create an entity with three link field values:
  480. // - The first field item uses a URL only.
  481. // - The second field item uses a URL and link text.
  482. // - The third field item uses a fragment-only URL with text.
  483. // For consistency in assertion code below, the URL is assigned to the title
  484. // variable for the first field.
  485. $this->drupalGet('entity_test/add');
  486. $url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
  487. $url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
  488. $url3 = '#net';
  489. // Intentionally contains an ampersand that needs sanitization on output.
  490. $title2 = 'A very long & strange example title that could break the nice layout of the site';
  491. $title3 = 'Fragment only';
  492. $edit = array(
  493. "{$field_name}[0][uri]" => $url1,
  494. "{$field_name}[1][uri]" => $url2,
  495. "{$field_name}[1][title]" => $title2,
  496. "{$field_name}[2][uri]" => $url3,
  497. "{$field_name}[2][title]" => $title3,
  498. );
  499. $this->drupalPostForm(NULL, $edit, t('Save'));
  500. preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
  501. $id = $match[1];
  502. $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
  503. // Verify that the link is output according to the formatter settings.
  504. $options = array(
  505. 'trim_length' => array(NULL, 6),
  506. 'rel' => array(NULL, 'nofollow'),
  507. 'target' => array(NULL, '_blank'),
  508. );
  509. foreach ($options as $setting => $values) {
  510. foreach ($values as $new_value) {
  511. // Update the field formatter settings.
  512. $display_options['settings'] = array($setting => $new_value);
  513. entity_get_display('entity_test', 'entity_test', 'full')
  514. ->setComponent($field_name, $display_options)
  515. ->save();
  516. $this->renderTestEntity($id);
  517. switch ($setting) {
  518. case 'trim_length':
  519. $url = $url1;
  520. $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
  521. $expected = '<div class="link-item">';
  522. $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>';
  523. $expected .= '</div>';
  524. $this->assertRaw($expected);
  525. $url = $url2;
  526. $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
  527. $title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2;
  528. $expected = '<div class="link-item">';
  529. $expected .= '<div class="link-title">' . Html::escape($title) . '</div>';
  530. $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>';
  531. $expected .= '</div>';
  532. $this->assertRaw($expected);
  533. $url = $url3;
  534. $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
  535. $title = isset($new_value) ? Unicode::truncate($title3, $new_value, FALSE, TRUE) : $title3;
  536. $expected = '<div class="link-item">';
  537. $expected .= '<div class="link-title">' . Html::escape($title) . '</div>';
  538. $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>';
  539. $expected .= '</div>';
  540. $this->assertRaw($expected);
  541. break;
  542. case 'rel':
  543. $rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
  544. $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($url1) . '</a></div>');
  545. $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($url2) . '</a></div>');
  546. $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url3) . '"' . $rel . '>' . Html::escape($url3) . '</a></div>');
  547. break;
  548. case 'target':
  549. $target = isset($new_value) ? ' target="' . $new_value . '"' : '';
  550. $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($url1) . '</a></div>');
  551. $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($url2) . '</a></div>');
  552. $this->assertRaw('<div class="link-url"><a href="' . Html::escape($url3) . '"' . $target . '>' . Html::escape($url3) . '</a></div>');
  553. break;
  554. }
  555. }
  556. }
  557. }
  558. /**
  559. * Renders a test_entity and sets the output in the internal browser.
  560. *
  561. * @param int $id
  562. * The test_entity ID to render.
  563. * @param string $view_mode
  564. * (optional) The view mode to use for rendering.
  565. * @param bool $reset
  566. * (optional) Whether to reset the entity_test storage cache. Defaults to
  567. * TRUE to simplify testing.
  568. */
  569. protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
  570. if ($reset) {
  571. $this->container->get('entity.manager')->getStorage('entity_test')->resetCache(array($id));
  572. }
  573. $entity = EntityTest::load($id);
  574. $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), $view_mode);
  575. $content = $display->build($entity);
  576. $output = \Drupal::service('renderer')->renderRoot($content);
  577. $this->setRawContent($output);
  578. $this->verbose($output);
  579. }
  580. }