You are here

function NumberFieldTest::testNumberFormatter 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::testNumberFormatter()

Test default formatter behavior

File

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

Class

NumberFieldTest
Tests the creation of numeric fields.

Namespace

Drupal\field\Tests\Number

Code

function testNumberFormatter() {
  $type = Unicode::strtolower($this
    ->randomMachineName());
  $float_field = Unicode::strtolower($this
    ->randomMachineName());
  $integer_field = Unicode::strtolower($this
    ->randomMachineName());
  $thousand_separators = array(
    '',
    '.',
    ',',
    ' ',
    chr(8201),
    "'",
  );
  $decimal_separators = array(
    '.',
    ',',
  );
  $prefix = $this
    ->randomMachineName();
  $suffix = $this
    ->randomMachineName();
  $random_float = rand(0, pow(10, 6));
  $random_integer = rand(0, pow(10, 6));

  // Create a content type containing float and integer fields.
  $this
    ->drupalCreateContentType(array(
    'type' => $type,
  ));
  entity_create('field_storage_config', array(
    'field_name' => $float_field,
    'entity_type' => 'node',
    'type' => 'float',
  ))
    ->save();
  entity_create('field_storage_config', array(
    'field_name' => $integer_field,
    'entity_type' => 'node',
    'type' => 'integer',
  ))
    ->save();
  entity_create('field_config', array(
    'field_name' => $float_field,
    'entity_type' => 'node',
    'bundle' => $type,
    'settings' => array(
      'prefix' => $prefix,
      'suffix' => $suffix,
    ),
  ))
    ->save();
  entity_create('field_config', array(
    'field_name' => $integer_field,
    'entity_type' => 'node',
    'bundle' => $type,
    'settings' => array(
      'prefix' => $prefix,
      'suffix' => $suffix,
    ),
  ))
    ->save();
  entity_get_form_display('node', $type, 'default')
    ->setComponent($float_field, array(
    'type' => 'number',
    'settings' => array(
      'placeholder' => '0.00',
    ),
  ))
    ->setComponent($integer_field, array(
    'type' => 'number',
    'settings' => array(
      'placeholder' => '0.00',
    ),
  ))
    ->save();
  entity_get_display('node', $type, 'default')
    ->setComponent($float_field, array(
    'type' => 'number_decimal',
  ))
    ->setComponent($integer_field, array(
    'type' => 'number_unformatted',
  ))
    ->save();

  // Create a node to test formatters.
  $node = entity_create('node', array(
    'type' => $type,
    'title' => $this
      ->randomMachineName(),
    $float_field => array(
      'value' => $random_float,
    ),
    $integer_field => array(
      'value' => $random_integer,
    ),
  ));
  $node
    ->save();

  // Go to manage display page.
  $this
    ->drupalGet("admin/structure/types/manage/{$type}/display");

  // Configure number_decimal formatter for the 'float' field type.
  $thousand_separator = $thousand_separators[array_rand($thousand_separators)];
  $decimal_separator = $decimal_separators[array_rand($decimal_separators)];
  $scale = rand(0, 10);
  $this
    ->drupalPostAjaxForm(NULL, array(), "{$float_field}_settings_edit");
  $edit = array(
    "fields[{$float_field}][settings_edit_form][settings][prefix_suffix]" => TRUE,
    "fields[{$float_field}][settings_edit_form][settings][scale]" => $scale,
    "fields[{$float_field}][settings_edit_form][settings][decimal_separator]" => $decimal_separator,
    "fields[{$float_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator,
  );
  $this
    ->drupalPostAjaxForm(NULL, $edit, "{$float_field}_plugin_settings_update");
  $this
    ->drupalPostForm(NULL, array(), t('Save'));

  // Check number_decimal and number_unformatted formatters behavior.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $float_formatted = number_format($random_float, $scale, $decimal_separator, $thousand_separator);
  $this
    ->assertRaw("{$prefix}{$float_formatted}{$suffix}", 'Prefix and suffix added');
  $this
    ->assertRaw((string) $random_integer);

  // Configure the number_decimal formatter.
  entity_get_display('node', $type, 'default')
    ->setComponent($integer_field, array(
    'type' => 'number_integer',
  ))
    ->save();
  $this
    ->drupalGet("admin/structure/types/manage/{$type}/display");
  $thousand_separator = $thousand_separators[array_rand($thousand_separators)];
  $this
    ->drupalPostAjaxForm(NULL, array(), "{$integer_field}_settings_edit");
  $edit = array(
    "fields[{$integer_field}][settings_edit_form][settings][prefix_suffix]" => FALSE,
    "fields[{$integer_field}][settings_edit_form][settings][thousand_separator]" => $thousand_separator,
  );
  $this
    ->drupalPostAjaxForm(NULL, $edit, "{$integer_field}_plugin_settings_update");
  $this
    ->drupalPostForm(NULL, array(), t('Save'));

  // Check number_integer formatter behavior.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $integer_formatted = number_format($random_integer, 0, '', $thousand_separator);
  $this
    ->assertRaw($integer_formatted, 'Random integer formatted');
}