You are here

function NumberFieldTest::testNumberIntegerField in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/Number/NumberFieldTest.php \Drupal\field\Tests\Number\NumberFieldTest::testNumberIntegerField()

Test integer field.

File

core/modules/field/src/Tests/Number/NumberFieldTest.php, line 130
Contains \Drupal\field\Tests\Number\NumberFieldTest.

Class

NumberFieldTest
Tests the creation of numeric fields.

Namespace

Drupal\field\Tests\Number

Code

function testNumberIntegerField() {
  $minimum = rand(-4000, -2000);
  $maximum = rand(2000, 4000);

  // Create a field with settings to validate.
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $storage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'integer',
  ));
  $storage
    ->save();
  entity_create('field_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'settings' => array(
      'min' => $minimum,
      'max' => $maximum,
      'prefix' => 'ThePrefix',
    ),
  ))
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => 'number',
    'settings' => array(
      'placeholder' => '4',
    ),
  ))
    ->save();
  entity_get_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => 'number_integer',
    'settings' => array(
      'prefix_suffix' => FALSE,
    ),
  ))
    ->save();

  // Check the storage schema.
  $expected = array(
    'columns' => array(
      'value' => array(
        'type' => 'int',
        'unsigned' => '',
        'size' => 'normal',
      ),
    ),
    'unique keys' => array(),
    'indexes' => array(),
    'foreign keys' => array(),
  );
  $this
    ->assertEqual($storage
    ->getSchema(), $expected);

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
  $this
    ->assertRaw('placeholder="4"');

  // Submit a valid integer
  $value = rand($minimum, $maximum);
  $edit = array(
    "{$field_name}[0][value]" => $value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');

  // Try to set a value below the minimum value
  $this
    ->drupalGet('entity_test/add');
  $edit = array(
    "{$field_name}[0][value]" => $minimum - 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw(t('%name must be higher than or equal to %minimum.', array(
    '%name' => $field_name,
    '%minimum' => $minimum,
  )), 'Correctly failed to save integer value less than minimum allowed value.');

  // Try to set a decimal value
  $this
    ->drupalGet('entity_test/add');
  $edit = array(
    "{$field_name}[0][value]" => 1.5,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw(t('%name is not a valid number.', array(
    '%name' => $field_name,
  )), 'Correctly failed to save decimal value to integer field.');

  // Try to set a value above the maximum value
  $this
    ->drupalGet('entity_test/add');
  $edit = array(
    "{$field_name}[0][value]" => $maximum + 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw(t('%name must be lower than or equal to %maximum.', array(
    '%name' => $field_name,
    '%maximum' => $maximum,
  )), 'Correctly failed to save integer value greater than maximum allowed value.');

  // Try to set a wrong integer value.
  $this
    ->drupalGet('entity_test/add');
  $edit = array(
    "{$field_name}[0][value]" => '20-40',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw(t('%name must be a number.', array(
    '%name' => $field_name,
  )), 'Correctly failed to save wrong integer value.');

  // Test with valid entries.
  $valid_entries = array(
    '-1234',
    '0',
    '1234',
  );
  foreach ($valid_entries as $valid_entry) {
    $this
      ->drupalGet('entity_test/add');
    $edit = array(
      "{$field_name}[0][value]" => $valid_entry,
    );
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
    $id = $match[1];
    $this
      ->assertText(t('entity_test @id has been created.', array(
      '@id' => $id,
    )), 'Entity was created');
    $this
      ->assertRaw($valid_entry, 'Value is displayed.');
    $this
      ->assertNoFieldByXpath('//div[@content="' . $valid_entry . '"]', NULL, 'The "content" attribute is not present since the Prefix is not being displayed');
  }

  // Test for the content attribute when a Prefix is displayed. Presumably this also tests for the attribute when a Suffix is displayed.
  entity_get_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => 'number_integer',
    'settings' => array(
      'prefix_suffix' => TRUE,
    ),
  ))
    ->save();
  $integer_value = '123';
  $this
    ->drupalGet('entity_test/add');
  $edit = array(
    "{$field_name}[0][value]" => $integer_value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');
  $this
    ->drupalGet('entity_test/' . $id);
  $this
    ->assertFieldByXPath('//div[@content="' . $integer_value . '"]', 'ThePrefix' . $integer_value, 'The "content" attribute has been set to the value of the field, and the prefix is being displayed.');
}