You are here

public function UniqueFieldCoreTestCase::testUniqueSingleInteger in Unique field 7

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

File

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

  // Create a content type with two integer fields that are set to be unique
  $edit = array();
  $edit['name'] = 'Unique Integer Single';
  $edit['type'] = 'uf_int_single';
  $this
    ->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Integer Single has been added.', 'Content type added.');
  $edit = array();
  $edit['fields[_add_new_field][label]'] = 'Unique Integer Single Integer 1';
  $edit['fields[_add_new_field][field_name]'] = 'uf_int_single_int_1';
  $edit['fields[_add_new_field][type]'] = 'number_integer';
  $edit['fields[_add_new_field][widget_type]'] = 'number';
  $this
    ->drupalPost('admin/structure/types/manage/uf_int_single/fields', $edit, t('Save'));
  $this
    ->assertText('These settings apply to the Unique Integer Single Integer 1 field everywhere it is used.', 'Field added to content type.');
  $edit = array();
  $edit['fields[_add_new_field][label]'] = 'Unique Integer Single Integer 2';
  $edit['fields[_add_new_field][field_name]'] = 'uf_int_single_int_2';
  $edit['fields[_add_new_field][type]'] = 'number_integer';
  $edit['fields[_add_new_field][widget_type]'] = 'number';
  $this
    ->drupalPost('admin/structure/types/manage/uf_int_single/fields', $edit, t('Save'));
  $this
    ->assertText('These settings apply to the Unique Integer Single Integer 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_int_single_int_1]'] = 'field_uf_int_single_int_1';
  $edit['unique_field_fields[field_uf_int_single_int_2]'] = 'field_uf_int_single_int_2';
  $this
    ->drupalPost('admin/structure/types/manage/uf_int_single', $edit, t('Save content type'));
  $this
    ->assertText('The content type Unique Integer Single has been updated.', 'Content type updated.');

  // Attempt to create a node with the same integer in both fields
  $num = mt_rand();
  $edit = array();
  $edit['title'] = $this
    ->randomName(24);
  $edit['body[und][0][value]'] = $this
    ->randomName(48);
  $edit['field_uf_int_single_int_1[und][0][value]'] = $num;
  $edit['field_uf_int_single_int_2[und][0][value]'] = $num;
  $this
    ->drupalPost('node/add/uf-int-single', $edit, t('Save'));
  $this
    ->assertText('The Unique Integer Single Integer 2 fields must have unique values. The Unique Integer Single Integer 2 field has a value that is already used.', 'Unique Integer Single (uf_int_single) node with duplicate content could not be created');

  // Check for false negative: Attempt to create a node with unique numbers
  $edit = array();
  $edit['title'] = $this
    ->randomName(24);
  $edit['body[und][0][value]'] = $this
    ->randomName(48);
  $edit['field_uf_int_single_int_1[und][0][value]'] = $num;
  $edit['field_uf_int_single_int_2[und][0][value]'] = $num - 1;
  $this
    ->drupalPost('node/add/uf-int-single', $edit, t('Save'));
  $this
    ->assertText($edit['body[und][0][value]'], 'Unique Integer Single (uf_int_single) node has been created');
}