1.20.x link.ui.test LinkUITest::testCRUDCreateFieldWithClass()

If we're creating a new field and just hit 'save' on the default options, we want to make sure they are set to the expected results.

File

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

Class

LinkUITest
Testing that users can not input bad URLs or labels

Code

function testCRUDCreateFieldWithClass() {
  $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_class_name = 'basic-link-' . strtolower($this->randomName());
  $edit = array(
    'instance[settings][attributes][class]' => $link_class_name,
  );
  $this->backdropPost(NULL, $edit, t('Save settings'));

  // Is field created?
  $this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');

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

  $instance = $instances['field_' . $name];
  $this->assertFalse($instance['required'], 'Make sure field is not required.');
  $this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
  $this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
  //$this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
  $this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
  $this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
  $this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
  $this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'One class should be set.');
  $this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');

  // Now, let's create a node with this field and make sure the link shows up:
  $field_name = 'field_' . $name;
  $this->backdropGet('node/add/page');
  $this->assertField($field_name . '[und][0][url]', 'URL found');

  $input = array(
    'title' => 'This & That',
    'href' => 'http://www.example.com/',
  );

  $edit = array(
    'title' => $field_name,
    $field_name . '[und][0][title]' => $input['title'],
    $field_name . '[und][0][url]' => $input['href'],
  );
  $this->backdropPost(NULL, $edit, t('Save'));

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