3 namespace Drupal\Tests\link\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\field_ui\Tests\FieldUiTestTrait;
7 use Drupal\link\LinkItemInterface;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests link field UI functionality.
15 class LinkFieldUITest extends BrowserTestBase {
24 public static $modules = ['node', 'link', 'field_ui', 'block'];
27 * A user that can edit content types.
29 * @var \Drupal\user\UserInterface
36 protected function setUp() {
39 $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']);
40 $this->drupalLogin($this->adminUser);
41 $this->drupalPlaceBlock('system_breadcrumb_block');
45 * Tests the link field UI.
47 public function testFieldUI() {
48 // Add a content type.
49 $type = $this->drupalCreateContentType();
50 $type_path = 'admin/structure/types/manage/' . $type->id();
51 $add_path = 'node/add/' . $type->id();
53 // Add a link field to the newly-created type. It defaults to allowing both
54 // internal and external links.
55 $label = $this->randomMachineName();
56 $field_name = Unicode::strtolower($label);
57 $this->fieldUIAddNewField($type_path, $field_name, $label, 'link');
59 // Load the formatter page to check that the settings summary does not
61 // @todo Mess with the formatter settings a bit here.
62 $this->drupalGet("$type_path/display");
63 $this->assertText(t('Link text trimmed to @limit characters', ['@limit' => 80]));
65 // Test the help text displays when the link field allows both internal and
67 $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
68 $this->drupalGet($add_path);
69 $this->assertRaw('You can also enter an internal path such as <em class="placeholder">/node/add</em> or an external URL such as <em class="placeholder">http://example.com</em>.');
71 // Log in an admin to set up the next content type.
72 $this->drupalLogin($this->adminUser);
74 // Add a different content type.
75 $type = $this->drupalCreateContentType();
76 $type_path = 'admin/structure/types/manage/' . $type->id();
77 $add_path = 'node/add/' . $type->id();
79 // Add a link field to the newly-created type. Specify it must allow
80 // external only links.
81 $label = $this->randomMachineName();
82 $field_name = Unicode::strtolower($label);
83 $field_edit = ['settings[link_type]' => LinkItemInterface::LINK_EXTERNAL];
84 $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', [], $field_edit);
86 // Test the help text displays when link allows only external links.
87 $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
88 $this->drupalGet($add_path);
89 $this->assertRaw('This must be an external URL such as <em class="placeholder">http://example.com</em>.');