You are here

public function UniqueFieldCoreTestCase::testUniqueAllInteger in Unique field 7

Test the unique requirement on an integer field in the all scope.

File

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

  // Create a content type with an integer field that is set to be unique
  $edit = array();
  $edit['name'] = 'Unique Integer';
  $edit['type'] = 'uf_int';
  $this
    ->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Integer has been added.', 'Content type added.');
  $edit = array();
  $edit['fields[_add_new_field][label]'] = 'Unique Integer Integer';
  $edit['fields[_add_new_field][field_name]'] = 'uf_int_int';
  $edit['fields[_add_new_field][type]'] = 'number_integer';
  $edit['fields[_add_new_field][widget_type]'] = 'number';
  $this
    ->drupalPost('admin/structure/types/manage/uf_int/fields', $edit, t('Save'));
  $this
    ->assertText('These settings apply to the Unique Integer Integer 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_int_int]'] = 'field_uf_int_int';
  $this
    ->drupalPost('admin/structure/types/manage/uf_int', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Integer has been updated.', 'Content type updated.');

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

  // Attempt to create 2 nodes with the same integer in different content types
  $num = mt_rand();
  $edit = array();
  $edit['title'] = $this
    ->randomName(24);
  $edit['body[und][0][value]'] = $this
    ->randomName(48);
  $edit['field_uf_int_int[und][0][value]'] = $num;
  $this
    ->drupalPost('node/add/uf-int', $edit, t('Save'));
  $this
    ->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
  $edit = array();
  $edit['title'] = $this
    ->randomName(24);
  $edit['body[und][0][value]'] = $this
    ->randomName(48);
  $edit['field_uf_int_int[und][0][value]'] = $num;
  $this
    ->drupalPost('node/add/uf-int2', $edit, t('Save'));
  $this
    ->assertText('The Unique Integer 2 Integer field requires a unique value, and the specified value is already used', 'Unique Integer 2 (uf_int2) 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_int_int[und][0][value]'] = $num - 1;
  $this
    ->drupalPost('node/add/uf-int2', $edit, t('Save'));
  $this
    ->assertText($edit['body[und][0][value]'], 'Unique Integer 2 (uf_int2) node has been created');
}