You are here

public function FractionFieldTest::testFractionWidgetFraction in Fraction 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/FractionFieldTest.php \Drupal\Tests\fraction\Functional\FractionFieldTest::testFractionWidgetFraction()

Test decimal widget field.

File

tests/src/Functional/FractionFieldTest.php, line 206

Class

FractionFieldTest
Tests the creation of fraction fields.

Namespace

Drupal\Tests\fraction\Functional

Code

public function testFractionWidgetFraction() {
  $max = 100;
  $min = 10;

  // Create a field with settings to validate.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'fraction',
  ])
    ->save();
  FieldConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'settings' => [
      'max' => $max,
      'min' => $min,
    ],
  ])
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'fraction',
  ])
    ->save();
  $display_repository
    ->getViewDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'fraction',
  ])
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][fraction][numerator]", '', 'Numerator is displayed');
  $this
    ->assertFieldByName("{$field_name}[0][fraction][denominator]", '', 'Denominator is displayed');

  // Submit fraction value.
  $edit = [
    "{$field_name}[0][fraction][numerator]" => 150,
    "{$field_name}[0][fraction][denominator]" => 10,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $this
    ->assertText($this
    ->t('entity_test @id has been created.', [
    '@id' => $id,
  ]), 'Entity was created');
  $this
    ->assertRaw('150', 'Numerator is displayed.');
  $this
    ->assertRaw('10', 'Denominator is displayed.');

  // Try to set a value above the maximum value.
  $this
    ->drupalGet('entity_test/add');
  $edit = [
    "{$field_name}[0][fraction][numerator]" => 15000,
    "{$field_name}[0][fraction][denominator]" => 10,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('%name: the value may be no greater than %maximum.', [
    '%name' => $field_name,
    '%maximum' => $max,
  ]), 'Correctly failed to save value greater than maximum allowed value.');

  // Try to set a value below the minimum value.
  $this
    ->drupalGet('entity_test/add');
  $edit = [
    "{$field_name}[0][fraction][numerator]" => 1,
    "{$field_name}[0][fraction][denominator]" => 10,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('%name: the value may be no less than %minimum.', [
    '%name' => $field_name,
    '%minimum' => $min,
  ]), 'Correctly failed to save value less than minimum allowed value.');

  // Empty denominator.
  $this
    ->drupalGet('entity_test/add');
  $edit = [
    "{$field_name}[0][fraction][numerator]" => 1,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('The denominator of a fraction cannot be zero or empty (if a numerator is provided).'));

  // Numerators must be between -9223372036854775808 and 9223372036854775807.
  $this
    ->drupalGet('entity_test/add');
  $edit = [
    "{$field_name}[0][fraction][numerator]" => '-9223372036854775809',
    "{$field_name}[0][fraction][denominator]" => 10,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('The numerator of a fraction must be between -9223372036854775808 and 9223372036854775807.'));
  $this
    ->drupalGet('entity_test/add');
  $edit = [
    "{$field_name}[0][fraction][numerator]" => '9223372036854775808',
    "{$field_name}[0][fraction][denominator]" => 10,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('The numerator of a fraction must be between -9223372036854775808 and 9223372036854775807.'));

  // Denominators must be between 0 and 2147483647.
  $this
    ->drupalGet('entity_test/add');
  $edit = [
    "{$field_name}[0][fraction][numerator]" => 10,
    "{$field_name}[0][fraction][denominator]" => -1,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('The denominator of a fraction must be greater than 0 and less than 2147483647.'));
  $this
    ->drupalGet('entity_test/add');
  $edit = [
    "{$field_name}[0][fraction][numerator]" => 10,
    "{$field_name}[0][fraction][denominator]" => 2147483648,
  ];
  $this
    ->drupalPostForm(NULL, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('The denominator of a fraction must be greater than 0 and less than 2147483647.'));
}