function LinkFieldTest::testLinkSeparateFormatter

Tests the 'link_separate' formatter.

This test is mostly the same as testLinkFormatter(), but they cannot be merged, since they involve different configuration and output.

File

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

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\link\Tests

Code

function testLinkSeparateFormatter() {

  // Create a field with settings to validate.
  $this->field = array(
    'field_name' => drupal_strtolower($this
      ->randomName()),
    'type' => 'link',
    'cardinality' => 2,
  );
  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',
    ),
    'display' => array(
      'full' => array(
        'type' => 'link_separate',
        'label' => 'hidden',
      ),
    ),
  );
  field_create_instance($this->instance);
  $langcode = LANGUAGE_NOT_SPECIFIED;

  // Create an entity with two link field values:
  // - The first field item uses a URL only.
  // - The second field item uses a URL and title.
  // For consistency in assertion code below, the URL is assigned to the title
  // variable for the first field.
  $this
    ->drupalGet('test-entity/add/test_bundle');
  $url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
  $url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';

  // Intentionally contains an ampersand that needs sanitization on output.
  $title2 = 'A very long & strange example title that could break the nice layout of the site';
  $edit = array(
    "{$this->field['field_name']}[{$langcode}][0][url]" => $url1,
    "{$this->field['field_name']}[{$langcode}][1][url]" => $url2,
    "{$this->field['field_name']}[{$langcode}][1][title]" => $title2,
  );
  $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,
  )));

  // Verify that the link is output according to the formatter settings.
  $options = array(
    'trim_length' => array(
      NULL,
      6,
    ),
    'rel' => array(
      NULL,
      'nofollow',
    ),
    'target' => array(
      NULL,
      '_blank',
    ),
  );
  foreach ($options as $setting => $values) {
    foreach ($values as $new_value) {

      // Update the field formatter settings.
      $this->instance['display']['full']['settings'] = array(
        $setting => $new_value,
      );
      field_update_instance($this->instance);
      $this
        ->renderTestEntity($id);
      switch ($setting) {
        case 'trim_length':
          $url = $url1;
          $url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
          $expected = '<div class="link-item">';
          $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>';
          $expected .= '</div>';
          $this
            ->assertRaw($expected);
          $url = $url2;
          $url_title = isset($new_value) ? truncate_utf8($url, $new_value, FALSE, TRUE) : $url;
          $title = isset($new_value) ? truncate_utf8($title2, $new_value, FALSE, TRUE) : $title2;
          $expected = '<div class="link-item">';
          $expected .= '<div class="link-title">' . check_plain($title) . '</div>';
          $expected .= '<div class="link-url"><a href="' . check_plain($url) . '">' . check_plain($url_title) . '</a></div>';
          $expected .= '</div>';
          $this
            ->assertRaw($expected);
          break;
        case 'rel':
          $rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
          $this
            ->assertRaw('<div class="link-url"><a href="' . check_plain($url1) . '"' . $rel . '>' . check_plain($url1) . '</a></div>');
          $this
            ->assertRaw('<div class="link-url"><a href="' . check_plain($url2) . '"' . $rel . '>' . check_plain($url2) . '</a></div>');
          break;
        case 'target':
          $target = isset($new_value) ? ' target="' . $new_value . '"' : '';
          $this
            ->assertRaw('<div class="link-url"><a href="' . check_plain($url1) . '"' . $target . '>' . check_plain($url1) . '</a></div>');
          $this
            ->assertRaw('<div class="link-url"><a href="' . check_plain($url2) . '"' . $target . '>' . check_plain($url2) . '</a></div>');
          break;
      }
    }
  }
}