You are here

function LinkFieldTest::testLinkTitle in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/link/src/Tests/LinkFieldTest.php \Drupal\link\Tests\LinkFieldTest::testLinkTitle()

Tests the link title settings of a link field.

File

core/modules/link/src/Tests/LinkFieldTest.php, line 218
Contains \Drupal\link\Tests\LinkFieldTest.

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\link\Tests

Code

function testLinkTitle() {
  $field_name = Unicode::strtolower($this
    ->randomMachineName());

  // Create a field with settings to validate.
  $this->fieldStorage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'link',
  ));
  $this->fieldStorage
    ->save();
  $this->field = entity_create('field_config', array(
    'field_storage' => $this->fieldStorage,
    'bundle' => 'entity_test',
    'label' => 'Read more about this entity',
    'settings' => array(
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkItemInterface::LINK_GENERIC,
    ),
  ));
  $this->field
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => 'link_default',
    'settings' => array(
      'placeholder_url' => 'http://example.com',
      'placeholder_title' => 'Enter the text for this link',
    ),
  ))
    ->save();
  entity_get_display('entity_test', 'entity_test', 'full')
    ->setComponent($field_name, array(
    'type' => 'link',
    'label' => 'hidden',
  ))
    ->save();

  // Verify that the link text field works according to the field setting.
  foreach (array(
    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_REQUIRED) {

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

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

        // Verify that a URL and link text meets requirements.
        $this
          ->drupalGet('entity_test/add');
        $edit = array(
          "{$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.', array(
          '@name' => t('Link text'),
        )));
      }
    }
  }

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

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