function LinkFieldTest::testLinkTitle

Tests the title settings of a link field.

File

drupal/core/modules/field/modules/link/lib/Drupal/link/Tests/LinkFieldTest.php, line 112
Contains Drupal\link\Tests\LinkFieldTest.

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\link\Tests

Code

function testLinkTitle() {

  // Create a field with settings to validate.
  $this->field = array(
    'field_name' => drupal_strtolower($this
      ->randomName()),
    'type' => 'link',
  );
  field_create_field($this->field);
  $this->instance = array(
    'field_name' => $this->field['field_name'],
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
    'settings' => array(
      'title' => DRUPAL_OPTIONAL,
    ),
    'widget' => array(
      'type' => 'link_default',
      'settings' => array(
        'placeholder_url' => 'http://example.com',
        'placeholder_title' => 'Enter a title for this link',
      ),
    ),
    'display' => array(
      'full' => array(
        'type' => 'link',
        'label' => 'hidden',
      ),
    ),
  );
  field_create_instance($this->instance);
  $langcode = LANGUAGE_NOT_SPECIFIED;

  // Verify that the title field works according to the field setting.
  foreach (array(
    DRUPAL_DISABLED,
    DRUPAL_REQUIRED,
    DRUPAL_OPTIONAL,
  ) as $title_setting) {

    // Update the title field setting.
    $this->instance['settings']['title'] = $title_setting;
    field_update_instance($this->instance);

    // Display creation form.
    $this
      ->drupalGet('test-entity/add/test_bundle');
    $this
      ->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][url]", '', 'URL field found.');
    $this
      ->assertRaw('placeholder="http://example.com"');
    if ($title_setting === DRUPAL_DISABLED) {
      $this
        ->assertNoFieldByName("{$this->field['field_name']}[{$langcode}][0][title]", '', 'Title field not found.');
      $this
        ->assertNoRaw('placeholder="Enter a title for this link"');
    }
    else {
      $this
        ->assertRaw('placeholder="Enter a title for this link"');
      $this
        ->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][title]", '', 'Title field found.');
      if ($title_setting === DRUPAL_REQUIRED) {

        // Verify that the title is required, if the URL is non-empty.
        $edit = array(
          "{$this->field['field_name']}[{$langcode}][0][url]" => 'http://www.example.com',
        );
        $this
          ->drupalPost(NULL, $edit, t('Save'));
        $this
          ->assertText(t('!name field is required.', array(
          '!name' => t('Title'),
        )));

        // Verify that the title is not required, if the URL is empty.
        $edit = array(
          "{$this->field['field_name']}[{$langcode}][0][url]" => '',
        );
        $this
          ->drupalPost(NULL, $edit, t('Save'));
        $this
          ->assertNoText(t('!name field is required.', array(
          '!name' => t('Title'),
        )));

        // Verify that a URL and title meets requirements.
        $this
          ->drupalGet('test-entity/add/test_bundle');
        $edit = array(
          "{$this->field['field_name']}[{$langcode}][0][url]" => 'http://www.example.com',
          "{$this->field['field_name']}[{$langcode}][0][title]" => 'Example',
        );
        $this
          ->drupalPost(NULL, $edit, t('Save'));
        $this
          ->assertNoText(t('!name field is required.', array(
          '!name' => t('Title'),
        )));
      }
    }
  }

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

  // Verify that a link with title is rendered using the title as link text.
  $title = $this
    ->randomName();
  $edit = array(
    "{$this->field['field_name']}[{$langcode}][0][title]" => $title,
  );
  $this
    ->drupalPost("test-entity/manage/{$id}/edit", $edit, t('Save'));
  $this
    ->assertRaw(t('test_entity @id has been updated.', array(
    '@id' => $id,
  )));
  $this
    ->renderTestEntity($id);
  $expected_link = l($title, $value);
  $this
    ->assertRaw($expected_link);
}