LinkFieldUITest.php 3.08 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains Drupal\link\Tests\LinkFieldUITest.
 */

namespace Drupal\link\Tests;

10
use Drupal\Component\Utility\Unicode;
11
use Drupal\field_ui\Tests\FieldUiTestTrait;
12
use Drupal\link\LinkItemInterface;
13 14 15 16
use Drupal\simpletest\WebTestBase;

/**
 * Tests link field UI functionality.
17 18
 *
 * @group link
19 20 21
 */
class LinkFieldUITest extends WebTestBase {

22 23
  use FieldUiTestTrait;

24 25 26 27 28
  /**
   * Modules to enable.
   *
   * @var array
   */
29
  public static $modules = ['node', 'link', 'field_ui', 'block'];
30

31 32 33 34 35 36 37
  /**
   * A user that can edit content types.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

38 39 40
  /**
   * {@inheritdoc}
   */
41
  protected function setUp() {
42 43
    parent::setUp();

44 45
    $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer node fields', 'administer node display']);
    $this->drupalLogin($this->adminUser);
46
    $this->drupalPlaceBlock('system_breadcrumb_block');
47 48 49
  }

  /**
50
   * Tests the link field UI.
51 52 53 54
   */
  function testFieldUI() {
    // Add a content type.
    $type = $this->drupalCreateContentType();
55
    $type_path = 'admin/structure/types/manage/' . $type->id();
56
    $add_path = 'node/add/' . $type->id();
57

58 59
    // Add a link field to the newly-created type. It defaults to allowing both
    // internal and external links.
60
    $label = $this->randomMachineName();
61
    $field_name = Unicode::strtolower($label);
62
    $this->fieldUIAddNewField($type_path, $field_name, $label, 'link');
63 64 65 66 67 68

    // Load the formatter page to check that the settings summary does not
    // generate warnings.
    // @todo Mess with the formatter settings a bit here.
    $this->drupalGet("$type_path/display");
    $this->assertText(t('Link text trimmed to @limit characters', array('@limit' => 80)));
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    // Test the help text displays when the link field allows both internal and
    // external links.
    $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
    $this->drupalGet($add_path);
    $this->assertRaw('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>.');

    // Log in an admin to set up the next content type.
    $this->drupalLogin($this->adminUser);

    // Add a different content type.
    $type = $this->drupalCreateContentType();
    $type_path = 'admin/structure/types/manage/' . $type->id();
    $add_path = 'node/add/' . $type->id();

    // Add a link field to the newly-created type. Specify it must allow
    // external only links.
    $label = $this->randomMachineName();
    $field_name = Unicode::strtolower($label);
    $field_edit = ['field[settings][link_type]' => LinkItemInterface::LINK_EXTERNAL];
    $this->fieldUIAddNewField($type_path, $field_name, $label, 'link', array(), $field_edit);

    // Test the help text displays when link allows only external links.
    $this->drupalLogin($this->drupalCreateUser(['create ' . $type->id() . ' content']));
    $this->drupalGet($add_path);
    $this->assertRaw('This must be an external URL such as <em class="placeholder">http://example.com</em>.');
95 96 97
  }

}