You are here

public function LinkFieldTest::testLinkTitle in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::testLinkTitle()
  2. 10 core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::testLinkTitle()

Tests the link title settings of a link field.

File

core/modules/link/tests/src/Functional/LinkFieldTest.php, line 249

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\Tests\link\Functional

Code

public function testLinkTitle() {
  $field_name = mb_strtolower($this
    ->randomMachineName());

  // Create a field with settings to validate.
  $this->fieldStorage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'link',
  ]);
  $this->fieldStorage
    ->save();
  $this->field = FieldConfig::create([
    'field_storage' => $this->fieldStorage,
    'bundle' => 'entity_test',
    'label' => 'Read more about this entity',
    'settings' => [
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkItemInterface::LINK_GENERIC,
    ],
  ]);
  $this->field
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'link_default',
    'settings' => [
      'placeholder_url' => 'http://example.com',
      'placeholder_title' => 'Enter the text for this link',
    ],
  ])
    ->save();
  $display_repository
    ->getViewDisplay('entity_test', 'entity_test', 'full')
    ->setComponent($field_name, [
    'type' => 'link',
    'label' => 'hidden',
  ])
    ->save();

  // Verify that the link text field works according to the field setting.
  foreach ([
    DRUPAL_DISABLED,
    DRUPAL_REQUIRED,
    DRUPAL_OPTIONAL,
  ] as $title_setting) {

    // Update the link title field setting.
    $this->field
      ->setSetting('title', $title_setting);
    $this->field
      ->save();

    // Display creation form.
    $this
      ->drupalGet('entity_test/add');

    // Assert label is shown.
    $this
      ->assertText('Read more about this entity');
    $this
      ->assertFieldByName("{$field_name}[0][uri]", '', 'URL field found.');
    $this
      ->assertRaw('placeholder="http://example.com"');
    if ($title_setting === DRUPAL_DISABLED) {
      $this
        ->assertNoFieldByName("{$field_name}[0][title]", '', 'Link text field not found.');
      $this
        ->assertNoRaw('placeholder="Enter the text for this link"');
    }
    else {
      $this
        ->assertRaw('placeholder="Enter the text for this link"');
      $this
        ->assertFieldByName("{$field_name}[0][title]", '', 'Link text field found.');
      if ($title_setting === DRUPAL_OPTIONAL) {

        // Verify that the URL is required, if the link text is non-empty.
        $edit = [
          "{$field_name}[0][title]" => 'Example',
        ];
        $this
          ->drupalPostForm(NULL, $edit, t('Save'));
        $this
          ->assertText(t('The URL field is required when the @title field is specified.', [
          '@title' => t('Link text'),
        ]));
      }
      if ($title_setting === DRUPAL_REQUIRED) {

        // Verify that the link text is required, if the URL is non-empty.
        $edit = [
          "{$field_name}[0][uri]" => 'http://www.example.com',
        ];
        $this
          ->drupalPostForm(NULL, $edit, t('Save'));
        $this
          ->assertText(t('@title field is required if there is @uri input.', [
          '@title' => t('Link text'),
          '@uri' => t('URL'),
        ]));

        // Verify that the link text is not required, if the URL is empty.
        $edit = [
          "{$field_name}[0][uri]" => '',
        ];
        $this
          ->drupalPostForm(NULL, $edit, t('Save'));
        $this
          ->assertNoText(t('@name field is required.', [
          '@name' => t('Link text'),
        ]));

        // Verify that a URL and link text meets requirements.
        $this
          ->drupalGet('entity_test/add');
        $edit = [
          "{$field_name}[0][uri]" => 'http://www.example.com',
          "{$field_name}[0][title]" => 'Example',
        ];
        $this
          ->drupalPostForm(NULL, $edit, t('Save'));
        $this
          ->assertNoText(t('@name field is required.', [
          '@name' => t('Link text'),
        ]));
      }
    }
  }

  // Verify that a link without link text is rendered using the URL as text.
  $value = 'http://www.example.com/';
  $edit = [
    "{$field_name}[0][uri]" => $value,
    "{$field_name}[0][title]" => '',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', [
    '@id' => $id,
  ]));
  $output = $this
    ->renderTestEntity($id);
  $expected_link = (string) Link::fromTextAndUrl($value, Url::fromUri($value))
    ->toString();
  $this
    ->assertStringContainsString($expected_link, $output);

  // Verify that a link with text is rendered using the link text.
  $title = $this
    ->randomMachineName();
  $edit = [
    "{$field_name}[0][title]" => $title,
  ];
  $this
    ->drupalPostForm("entity_test/manage/{$id}/edit", $edit, t('Save'));
  $this
    ->assertText(t('entity_test @id has been updated.', [
    '@id' => $id,
  ]));
  $output = $this
    ->renderTestEntity($id);
  $expected_link = (string) Link::fromTextAndUrl($title, Url::fromUri($value))
    ->toString();
  $this
    ->assertStringContainsString($expected_link, $output);
}