public function UniqueFieldCoreTestCase::testUniqueSingleText in Unique field 7
Test the unique requirement on a text field in the single scope.
File
- tests/
core.test, line 298 - Functional tests for the Unique Field module with Drupal core field types.
Class
- UniqueFieldCoreTestCase
- @file Functional tests for the Unique Field module with Drupal core field types.
Code
public function testUniqueSingleText() {
// Create a content type with two text fields that are set to be unique
$edit = array();
$edit['name'] = 'Unique Text Single';
$edit['type'] = 'uf_text_single';
$this
->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
$this
->assertText('The content type Unique Text Single has been added.', 'Content type added.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Text Single Text 1';
$edit['fields[_add_new_field][field_name]'] = 'uf_text_single_text_1';
$edit['fields[_add_new_field][type]'] = 'text';
$edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
$this
->drupalPost('admin/structure/types/manage/uf_text_single/fields', $edit, t('Save'));
$this
->assertText('These settings apply to the Unique Text Single Text 1 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['fields[_add_new_field][label]'] = 'Unique Text Single Text 2';
$edit['fields[_add_new_field][field_name]'] = 'uf_text_single_text_2';
$edit['fields[_add_new_field][type]'] = 'text';
$edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
$this
->drupalPost('admin/structure/types/manage/uf_text_single/fields', $edit, t('Save'));
$this
->assertText('These settings apply to the Unique Text Single Text 2 field everywhere it is used.', 'Field added to content type.');
$edit = array();
$edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_NODE;
$edit['unique_field_fields[field_uf_text_single_text_1]'] = 'field_uf_text_single_text_1';
$edit['unique_field_fields[field_uf_text_single_text_2]'] = 'field_uf_text_single_text_2';
$this
->drupalPost('admin/structure/types/manage/uf_text_single', $edit, t('Save content type'));
$this
->assertText('The content type Unique Text Single has been updated.', 'Content type updated.');
// Attempt to create a node with the same text in both fields
$text = $this
->randomName(48);
$edit = array();
$edit['title'] = $this
->randomName(24);
$edit['body[und][0][value]'] = $this
->randomName(48);
$edit['field_uf_text_single_text_1[und][0][value]'] = $text;
$edit['field_uf_text_single_text_2[und][0][value]'] = $text;
$this
->drupalPost('node/add/uf-text-single', $edit, t('Save'));
$this
->assertText('The Unique Text Single Text 2 fields must have unique values. The Unique Text Single Text 2 field has a value that is already used.', 'Unique Text Single (uf_text_single) node with duplicate content could not be created');
// Check for false negative: Attempt to create a node with unique text
$edit = array();
$edit['title'] = $this
->randomName(24);
$edit['body[und][0][value]'] = $this
->randomName(48);
$edit['field_uf_text_single_text_1[und][0][value]'] = $this
->randomName(48);
$edit['field_uf_text_single_text_2[und][0][value]'] = $this
->randomName(48);
$this
->drupalPost('node/add/uf-text-single', $edit, t('Save'));
$this
->assertText($edit['body[und][0][value]'], 'Unique Text Single (uf_text_single) node has been created');
}