Tests the link title settings of a link field.
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' => 'entity_test',
'bundle' => 'entity_test',
'label' => 'Read more about this entity',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
),
);
field_create_instance($this->instance);
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->field['field_name'], array(
'type' => 'link_default',
'settings' => array(
'placeholder_url' => 'http://example.com',
'placeholder_title' => 'Enter the text for this link',
),
))
->save();
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->field['field_name'], array(
'type' => 'link',
'label' => 'hidden',
))
->save();
$langcode = Language::LANGCODE_NOT_SPECIFIED;
// Verify that the link text field works according to the field setting.
foreach (array(
DRUPAL_DISABLED,
DRUPAL_REQUIRED,
DRUPAL_OPTIONAL,
) as $title_setting) {
// Update the link title field setting.
$this->instance['settings']['title'] = $title_setting;
field_update_instance($this->instance);
// Display creation form.
$this
->drupalGet('entity_test/add');
// Assert label is shown.
$this
->assertText('Read more about this entity');
$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]", '', 'Link text field not found.');
$this
->assertNoRaw('placeholder="Enter the text for this link"');
}
else {
$this
->assertRaw('placeholder="Enter the text for this link"');
$this
->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][title]", '', 'Link text field found.');
if ($title_setting === DRUPAL_REQUIRED) {
// Verify that the link text 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('Link text'),
)));
// Verify that the link text 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('Link text'),
)));
// Verify that a URL and link text meets requirements.
$this
->drupalGet('entity_test/add');
$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('Link text'),
)));
}
}
}
// Verify that a link without link text is rendered using the URL as text.
$value = 'http://www.example.com/';
$edit = array(
'user_id' => 1,
'name' => $this
->randomName(),
"{$this->field['field_name']}[{$langcode}][0][url]" => $value,
"{$this->field['field_name']}[{$langcode}][0][title]" => '',
);
$this
->drupalPost(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)/edit|', $this->url, $match);
$id = $match[1];
$this
->assertText(t('entity_test @id has been created.', array(
'@id' => $id,
)));
$this
->renderTestEntity($id);
$expected_link = l($value, $value);
$this
->assertRaw($expected_link);
// Verify that a link with text is rendered using the link text.
$title = $this
->randomName();
$edit = array(
'user_id' => 1,
'name' => $this
->randomName(),
"{$this->field['field_name']}[{$langcode}][0][title]" => $title,
);
$this
->drupalPost("entity_test/manage/{$id}/edit", $edit, t('Save'));
$this
->assertText(t('entity_test @id has been updated.', array(
'@id' => $id,
)));
$this
->renderTestEntity($id);
$expected_link = l($title, $value);
$this
->assertRaw($expected_link);
}