1 link.ui.test LinkUITest::testCRUDCreateFieldWithClasses()

Tests adding custom classes (single and multiple) to link fields.

File

core/modules/link/tests/link.ui.test, line 245
Testing CRUD API in the browser.

Class

LinkUITest
Testing that users can not input bad URLs or labels

Code

function testCRUDCreateFieldWithClasses() {
  // Disable the page cache for this test.
  config_set('system.core', 'cache', 0);
  $name = strtolower($this->randomName());
  $edit = array(
    'fields[_add_new_field][label]' => $name,
    'fields[_add_new_field][field_name]' => $name,
    'fields[_add_new_field][type]' => 'link_field',
    'fields[_add_new_field][widget_type]' => 'link_field',
  );
  $this->backdropPost('admin/structure/types/manage/page/fields', $edit, t('Save'));

  $link_single_class_name = 'basic-link-' . strtolower($this->randomName());
  $edit = array(
    'instance[settings][attributes][class]' => $link_single_class_name,
  );
  $this->backdropPost(NULL, $edit, t('Save settings'));

  field_info_cache_clear();
  $instances = field_info_instances('node', 'page');

  $instance = $instances['field_' . $name];
  $this->assertEqual($instance['settings']['attributes']['class'], $link_single_class_name, 'One class should be set.');

  // Create a node and verify the class is output.
  $field_name = 'field_' . $name;
  $this->backdropGet('node/add/page');

  $edit = array(
    'title' => $field_name,
    $field_name . '[und][0][title]' => 'This & That',
    $field_name . '[und][0][url]' => 'http://www.example.com/',
  );
  $this->backdropPost(NULL, $edit, t('Save'));
  $node_url = $this->getUrl();

  $this->assertRaw('This & That');
  $this->assertPattern('|class\s?=\s?"' . $link_single_class_name . '"|', "Class $link_single_class_name exists on page.");

  // Edit the field and try with multiple classes.
  $link_multiple_class_name = 'basic-link ' . strtoupper($this->randomName());
  $edit = array(
    'instance[settings][attributes][class]' => $link_multiple_class_name,
  );
  $this->backdropPost('admin/structure/types/manage/page/fields/' . $field_name, $edit, t('Save settings'));

  field_info_cache_clear();
  $instances = field_info_instances('node', 'page');

  $instance = $instances['field_' . $name];
  $this->assertEqual($instance['settings']['attributes']['class'], $link_multiple_class_name, 'Two classes should be set.');

  // Visit the previously created node and check that the displayed classes
  // are updated.
  $this->backdropGet($node_url);
  $this->assertRaw('This & That');
  $this->assertPattern('|class\s?=\s?"' . $link_multiple_class_name . '"|', "Classes $link_multiple_class_name exist on page.");
}