You are here

public function UniqueFieldCoreTestCase::testUniqueCtypeInteger in Unique field 7

Test the unique requirement on an integer field in the content type scope.

File

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

  // 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_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.');

  // Attempt to create 2 nodes with the same integer value
  $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['body[und][0][value]'] = $this
    ->randomName(48);
  $this
    ->drupalPost('node/add/uf-int', $edit, t('Save'));
  $this
    ->assertText('The Unique Integer Integer field requires a unique value, and the specified value is already used', 'Unique Integer (uf_int) node with duplicate content could not be created');

  // Check for false negative: Attempt to create a node with a unique integer
  $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-int', $edit, t('Save'));
  $this
    ->assertText($edit['body[und][0][value]'], 'Unique Integer (uf_int) node has been created');
}