annotate core/modules/link/tests/src/Functional/LinkFieldUITest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\link\Functional;
Chris@0 4
Chris@14 5 use Drupal\Component\Utility\Html;
Chris@14 6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
Chris@14 7 use Drupal\field\Entity\FieldConfig;
Chris@14 8 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 9 use Drupal\link\LinkItemInterface;
Chris@0 10 use Drupal\Tests\BrowserTestBase;
Chris@17 11 use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Tests link field UI functionality.
Chris@0 15 *
Chris@0 16 * @group link
Chris@0 17 */
Chris@0 18 class LinkFieldUITest extends BrowserTestBase {
Chris@0 19
Chris@0 20 use FieldUiTestTrait;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Modules to enable.
Chris@0 24 *
Chris@0 25 * @var array
Chris@0 26 */
Chris@0 27 public static $modules = ['node', 'link', 'field_ui', 'block'];
Chris@0 28
Chris@0 29 /**
Chris@0 30 * A user that can edit content types.
Chris@0 31 *
Chris@0 32 * @var \Drupal\user\UserInterface
Chris@0 33 */
Chris@0 34 protected $adminUser;
Chris@0 35
Chris@0 36 /**
Chris@14 37 * A user that should see the help texts.
Chris@14 38 *
Chris@14 39 * @var \Drupal\user\Entity\User
Chris@14 40 */
Chris@14 41 protected $helpTextUser;
Chris@14 42
Chris@14 43 /**
Chris@14 44 * The first content type to add fields to.
Chris@14 45 *
Chris@14 46 * @var \Drupal\node\Entity\NodeType
Chris@14 47 */
Chris@14 48 protected $firstContentType;
Chris@14 49
Chris@14 50 /**
Chris@14 51 * The second content type to add fields to.
Chris@14 52 *
Chris@14 53 * @var \Drupal\node\Entity\NodeType
Chris@14 54 */
Chris@14 55 protected $secondContentType;
Chris@14 56
Chris@14 57 /**
Chris@0 58 * {@inheritdoc}
Chris@0 59 */
Chris@0 60 protected function setUp() {
Chris@0 61 parent::setUp();
Chris@0 62
Chris@14 63 $this->firstContentType = $this->drupalCreateContentType();
Chris@14 64 $this->secondContentType = $this->drupalCreateContentType();
Chris@0 65 $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']);
Chris@14 66 $this->helpTextUser = $this->drupalCreateUser(['create ' . $this->secondContentType->id() . ' content']);
Chris@0 67 $this->drupalPlaceBlock('system_breadcrumb_block');
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Tests the link field UI.
Chris@0 72 */
Chris@0 73 public function testFieldUI() {
Chris@14 74 foreach ($this->providerTestFieldUI() as $item) {
Chris@14 75 list($cardinality, $link_type, $title, $label, $field_name) = $item;
Chris@14 76 $this->runFieldUIItem($cardinality, $link_type, $title, $label, $field_name);
Chris@14 77 }
Chris@14 78 }
Chris@14 79
Chris@14 80 /**
Chris@14 81 * Provides test data for ::testFieldUI().
Chris@14 82 */
Chris@14 83 protected function providerTestFieldUI() {
Chris@14 84 // There are many combinations of field settings: where the description
Chris@14 85 // should show: variation on internal, external, both; cardinality (where
Chris@14 86 // the fieldset is hidden or used); and link text shown (required or
Chris@14 87 // optional) or disabled. There are two descriptions: field and URL help
Chris@14 88 // text.
Chris@14 89 $cardinalities = [1, 2];
Chris@14 90 $title_settings = [
Chris@14 91 DRUPAL_DISABLED,
Chris@14 92 DRUPAL_OPTIONAL,
Chris@14 93 ];
Chris@14 94 $link_types = [
Chris@14 95 LinkItemInterface::LINK_EXTERNAL,
Chris@14 96 LinkItemInterface::LINK_GENERIC,
Chris@14 97 LinkItemInterface::LINK_INTERNAL,
Chris@14 98 ];
Chris@14 99
Chris@14 100 // Test all variations of link types on all cardinalities.
Chris@14 101 foreach ($cardinalities as $cardinality) {
Chris@14 102 foreach ($link_types as $link_type) {
Chris@14 103 // Now, test this with both the title enabled and disabled.
Chris@14 104 foreach ($title_settings as $title_setting) {
Chris@14 105 // Test both empty and non-empty labels.
Chris@14 106 foreach ([TRUE, FALSE] as $label_provided) {
Chris@14 107 // Generate a unique machine name for the field so it can be
Chris@14 108 // identified in the test.
Chris@14 109 $id = implode('_', [
Chris@14 110 'link',
Chris@14 111 $cardinality,
Chris@14 112 $link_type,
Chris@14 113 $title_setting,
Chris@14 114 (int) $label_provided,
Chris@14 115 ]);
Chris@14 116
Chris@14 117 // Use a unique label that contains some HTML.
Chris@14 118 $label = '<img src="http://example.com">' . $id;
Chris@14 119
Chris@14 120 yield [
Chris@14 121 $cardinality,
Chris@14 122 $link_type,
Chris@14 123 $title_setting,
Chris@14 124 $label_provided ? $label : '',
Chris@14 125 $id,
Chris@14 126 ];
Chris@14 127 }
Chris@14 128 }
Chris@14 129 }
Chris@14 130 }
Chris@14 131 }
Chris@14 132
Chris@14 133 /**
Chris@14 134 * Tests one link field UI item.
Chris@14 135 *
Chris@14 136 * @param int $cardinality
Chris@14 137 * The field cardinality.
Chris@14 138 * @param int $link_type
Chris@14 139 * Determine if the link is external, internal or both.
Chris@14 140 * @param int $title
Chris@14 141 * Determine if the field will display the link text field.
Chris@14 142 * @param string $label
Chris@14 143 * The field label.
Chris@14 144 * @param string $field_name
Chris@14 145 * The unique machine name for the field.
Chris@14 146 */
Chris@14 147 public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name) {
Chris@14 148 $this->drupalLogin($this->adminUser);
Chris@14 149 $type_path = 'admin/structure/types/manage/' . $this->firstContentType->id();
Chris@0 150
Chris@0 151 // Add a link field to the newly-created type. It defaults to allowing both
Chris@0 152 // internal and external links.
Chris@14 153 $field_label = str_replace('_', ' ', $field_name);
Chris@14 154 $description = 'link field description';
Chris@14 155 $field_edit = [
Chris@14 156 'description' => $description,
Chris@14 157 ];
Chris@14 158 $this->fieldUIAddNewField($type_path, $field_name, $field_label, 'link', [], $field_edit);
Chris@0 159
Chris@0 160 // Load the formatter page to check that the settings summary does not
Chris@0 161 // generate warnings.
Chris@0 162 // @todo Mess with the formatter settings a bit here.
Chris@0 163 $this->drupalGet("$type_path/display");
Chris@0 164 $this->assertText(t('Link text trimmed to @limit characters', ['@limit' => 80]));
Chris@0 165
Chris@14 166 $storage = FieldStorageConfig::create([
Chris@14 167 'field_name' => $field_name,
Chris@14 168 'entity_type' => 'node',
Chris@14 169 'type' => 'link',
Chris@14 170 'cardinality' => $cardinality,
Chris@14 171 ]);
Chris@14 172 $storage->save();
Chris@14 173
Chris@14 174 FieldConfig::create([
Chris@14 175 'field_storage' => $storage,
Chris@14 176 'label' => $label,
Chris@14 177 'bundle' => $this->secondContentType->id(),
Chris@14 178 'settings' => [
Chris@14 179 'title' => $title,
Chris@14 180 'link_type' => $link_type,
Chris@14 181 ],
Chris@14 182 ])->save();
Chris@14 183
Chris@14 184 // Make the fields visible in the form display.
Chris@14 185 $form_display_id = implode('.', ['node', $this->secondContentType->id(), 'default']);
Chris@14 186 $form_display = EntityFormDisplay::load($form_display_id);
Chris@14 187 $form_display->setComponent($field_name, ['region' => 'content']);
Chris@14 188 $form_display->save();
Chris@14 189
Chris@14 190 // Log in a user that is allowed to create this content type, see if
Chris@14 191 // the user can see the expected help text.
Chris@14 192 $this->drupalLogin($this->helpTextUser);
Chris@14 193
Chris@14 194 $add_path = 'node/add/' . $this->secondContentType->id();
Chris@0 195 $this->drupalGet($add_path);
Chris@0 196
Chris@14 197 $expected_help_texts = [
Chris@14 198 LinkItemInterface::LINK_EXTERNAL => 'This must be an external URL such as <em class="placeholder">http://example.com</em>.',
Chris@14 199 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">&lt;front&gt;</em> to link to the front page.',
Chris@14 200 LinkItemInterface::LINK_INTERNAL => rtrim(\Drupal::url('<front>', [], ['absolute' => TRUE]), '/'),
Chris@14 201 ];
Chris@0 202
Chris@14 203 // Check that the help texts we assume should be there, is there.
Chris@14 204 $this->assertFieldContainsRawText($field_name, $expected_help_texts[$link_type]);
Chris@14 205 if ($link_type === LinkItemInterface::LINK_INTERNAL) {
Chris@14 206 // Internal links have no "system" description. Test that none
Chris@14 207 // of the other help texts show here.
Chris@14 208 $this->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_EXTERNAL]);
Chris@14 209 $this->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_GENERIC]);
Chris@14 210 }
Chris@14 211 // Also assert that the description we made is here, no matter what the
Chris@14 212 // cardinality or link setting.
Chris@14 213 if (!empty($label)) {
Chris@14 214 $this->assertFieldContainsRawText($field_name, $label);
Chris@14 215 }
Chris@14 216 }
Chris@0 217
Chris@14 218 /**
Chris@14 219 * Checks that given field contains the given raw text.
Chris@14 220 *
Chris@14 221 * @param string $field_name
Chris@14 222 * The name of the field to check.
Chris@14 223 * @param string $text
Chris@14 224 * The text to check.
Chris@14 225 */
Chris@14 226 protected function assertFieldContainsRawText($field_name, $text) {
Chris@14 227 $this->assertTrue((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this->getFieldHtml($field_name)));
Chris@14 228 }
Chris@0 229
Chris@14 230 /**
Chris@14 231 * Checks that given field does not contain the given raw text.
Chris@14 232 *
Chris@14 233 * @param string $field_name
Chris@14 234 * The name of the field to check.
Chris@14 235 * @param string $text
Chris@14 236 * The text to check.
Chris@14 237 */
Chris@14 238 protected function assertNoFieldContainsRawText($field_name, $text) {
Chris@14 239 $this->assertFalse((bool) preg_match('/' . preg_quote($text, '/') . '/ui', $this->getFieldHtml($field_name)));
Chris@14 240 }
Chris@14 241
Chris@14 242 /**
Chris@14 243 * Returns the raw HTML for the given field.
Chris@14 244 *
Chris@14 245 * @param $field_name
Chris@14 246 * The name of the field for which to return the HTML.
Chris@14 247 *
Chris@14 248 * @return string
Chris@14 249 * The raw HTML.
Chris@14 250 */
Chris@14 251 protected function getFieldHtml($field_name) {
Chris@14 252 $css_id = Html::cleanCssIdentifier('edit-' . $field_name . '-wrapper');
Chris@14 253 return $this->xpath('//*[@id=:id]', [':id' => $css_id])[0]->getHtml();
Chris@0 254 }
Chris@0 255
Chris@0 256 }