You are here

public function UniqueFieldCoreTestCase::testUniqueAllText in Unique field 7

Test the unique requirement on a text field in the all scope.

File

tests/core.test, line 233
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 testUniqueAllText() {

  // Create a content type with a text field that is set to be unique
  $edit = array();
  $edit['name'] = 'Unique Text';
  $edit['type'] = 'uf_text';
  $this
    ->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Text has been added.', 'Content type added.');
  $edit = array();
  $edit['fields[_add_new_field][label]'] = 'Unique Text Text';
  $edit['fields[_add_new_field][field_name]'] = 'uf_text_text';
  $edit['fields[_add_new_field][type]'] = 'text';
  $edit['fields[_add_new_field][widget_type]'] = 'text_textfield';
  $this
    ->drupalPost('admin/structure/types/manage/uf_text/fields', $edit, t('Save'));
  $this
    ->assertText('These settings apply to the Unique Text Text field everywhere it is used.', 'Field added to content type.');
  $edit = array();
  $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  $edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
  $this
    ->drupalPost('admin/structure/types/manage/uf_text', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Text has been updated.', 'Content type updated.');

  // Create another content type with the same text field and set it to unique
  $edit = array();
  $edit['name'] = 'Unique Text 2';
  $edit['type'] = 'uf_text2';
  $this
    ->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Text 2 has been added.', 'Content type added.');
  $edit = array();
  $edit['fields[_add_existing_field][label]'] = 'Unique Text 2 Text';
  $edit['fields[_add_existing_field][field_name]'] = 'field_uf_text_text';
  $edit['fields[_add_existing_field][widget_type]'] = 'text_textfield';
  $this
    ->drupalPost('admin/structure/types/manage/uf_text2/fields', $edit, t('Save'));
  $this
    ->assertText('These settings apply only to the Unique Text 2 Text field when used in the Unique Text 2 type.', 'Field added to content type.');
  $edit = array();
  $edit['unique_field_scope'] = UNIQUE_FIELD_SCOPE_ALL;
  $edit['unique_field_fields[field_uf_text_text]'] = 'field_uf_text_text';
  $this
    ->drupalPost('admin/structure/types/manage/uf_text2', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Text 2 has been updated.', 'Content type updated.');

  // Attempt to create 2 nodes with the same text in different content types
  $text = $this
    ->randomName(48);
  $edit = array();
  $edit['title'] = $this
    ->randomName(24);
  $edit['body[und][0][value]'] = $this
    ->randomName(48);
  $edit['field_uf_text_text[und][0][value]'] = $text;
  $this
    ->drupalPost('node/add/uf-text', $edit, t('Save'));
  $this
    ->assertText($edit['body[und][0][value]'], 'Unique Text (uf_text) node has been created');
  $edit = array();
  $edit['title'] = $this
    ->randomName(24);
  $edit['body[und][0][value]'] = $this
    ->randomName(48);
  $edit['field_uf_text_text[und][0][value]'] = $text;
  $this
    ->drupalPost('node/add/uf-text2', $edit, t('Save'));
  $this
    ->assertText('The Unique Text 2 Text field requires a unique value, and the specified value is already used', 'Unique Text 2 (uf_text2) node with duplicate content could not be created');

  // Check for false negative: Attempt to create a node with a unique text
  $edit = array();
  $edit['title'] = $this
    ->randomName(24);
  $edit['body[und][0][value]'] = $this
    ->randomName(48);
  $edit['field_uf_text_text[und][0][value]'] = $this
    ->randomName(48);
  $this
    ->drupalPost('node/add/uf-text2', $edit, t('Save'));
  $this
    ->assertText($edit['body[und][0][value]'], 'Unique Text 2 (uf_text2) node has been created');
}