View source
<?php
namespace Drupal\Tests\link\Functional;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\link\LinkItemInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
class LinkFieldUITest extends BrowserTestBase {
use FieldUiTestTrait;
protected static $modules = [
'node',
'link',
'field_ui',
'block',
];
protected $defaultTheme = 'classy';
protected $adminUser;
protected $helpTextUser;
protected $contentType;
protected function setUp() : void {
parent::setUp();
$this->contentType = $this
->drupalCreateContentType();
$this->adminUser = $this
->drupalCreateUser([
'administer content types',
'administer node fields',
'administer node display',
]);
$this->helpTextUser = $this
->drupalCreateUser([
'create ' . $this->contentType
->id() . ' content',
]);
$this
->drupalPlaceBlock('system_breadcrumb_block');
}
public function testFieldUI() {
foreach ($this
->providerTestFieldUI() as $item) {
list($cardinality, $link_type, $title, $label, $field_name, $default_uri) = $item;
$this
->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name, $default_uri);
}
}
protected function providerTestFieldUI() {
$cardinalities = [
1,
2,
];
$title_settings = [
DRUPAL_DISABLED,
DRUPAL_OPTIONAL,
];
$link_types = [
LinkItemInterface::LINK_EXTERNAL => 'http://drupal.org',
LinkItemInterface::LINK_GENERIC => '',
LinkItemInterface::LINK_INTERNAL => '<front>',
];
foreach ($cardinalities as $cardinality) {
foreach ($link_types as $link_type => $default_uri) {
foreach ($title_settings as $title_setting) {
foreach ([
TRUE,
FALSE,
] as $label_provided) {
$id = implode('_', [
'link',
$cardinality,
$link_type,
$title_setting,
(int) $label_provided,
]);
$label = '<img src="http://example.com">' . $id;
(yield [
$cardinality,
$link_type,
$title_setting,
$label_provided ? $label : '',
$id,
$default_uri,
]);
}
}
}
}
}
public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name, $default_uri) {
$this
->drupalLogin($this->adminUser);
$type_path = 'admin/structure/types/manage/' . $this->contentType
->id();
$description = 'link field description';
$field_edit = [
'description' => $description,
'settings[link_type]' => (int) $link_type,
];
if (!empty($default_uri)) {
$field_edit['default_value_input[field_' . $field_name . '][0][uri]'] = $default_uri;
$field_edit['default_value_input[field_' . $field_name . '][0][title]'] = 'Default title';
}
$storage_edit = [
'cardinality_number' => $cardinality,
];
$this
->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_edit, $field_edit);
$this
->drupalGet("{$type_path}/display");
$this
->assertSession()
->pageTextContains('Link text trimmed to 80 characters');
$form_display_id = implode('.', [
'node',
$this->contentType
->id(),
'default',
]);
$form_display = EntityFormDisplay::load($form_display_id);
$form_display
->setComponent($field_name, [
'region' => 'content',
]);
$form_display
->save();
$this
->drupalLogin($this->helpTextUser);
$add_path = 'node/add/' . $this->contentType
->id();
$this
->drupalGet($add_path);
$expected_help_texts = [
LinkItemInterface::LINK_EXTERNAL => 'This must be an external URL such as <em class="placeholder">http://example.com</em>.',
LinkItemInterface::LINK_GENERIC => '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>. Enter <em class="placeholder"><front></em> to link to the front page. Enter <em class="placeholder"><nolink></em> to display link text only',
LinkItemInterface::LINK_INTERNAL => rtrim(Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])
->toString(), '/'),
];
$this
->assertFieldContainsRawText($field_name, $expected_help_texts[$link_type]);
if ($link_type === LinkItemInterface::LINK_INTERNAL) {
$this
->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_EXTERNAL]);
$this
->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_GENERIC]);
}
if (!empty($label)) {
$this
->assertFieldContainsRawText($field_name, $label);
}
$this
->assertSession()
->fieldValueEquals('field_' . $field_name . '[0][uri]', $default_uri);
}
protected function assertFieldContainsRawText($field_name, $text) {
$this
->assertTrue((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this
->getFieldHtml($field_name)));
}
protected function assertNoFieldContainsRawText($field_name, $text) {
$this
->assertFalse((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this
->getFieldHtml($field_name)));
}
protected function getFieldHtml($field_name) {
$css_id = Html::cleanCssIdentifier('edit-field-' . $field_name . '-wrapper');
return $this
->xpath('//*[@id=:id]', [
':id' => $css_id,
])[0]
->getHtml();
}
}