function NumberFieldTest::testNumberIntegerField

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/Number/NumberFieldTest.php \Drupal\Tests\field\Functional\Number\NumberFieldTest::testNumberIntegerField()
  2. 10 core/modules/field/tests/src/Functional/Number/NumberFieldTest.php \Drupal\Tests\field\Functional\Number\NumberFieldTest::testNumberIntegerField()
  3. 11.x core/modules/field/tests/src/Functional/Number/NumberFieldTest.php \Drupal\Tests\field\Functional\Number\NumberFieldTest::testNumberIntegerField()

Test integer field.

File

core/modules/field/tests/src/Functional/Number/NumberFieldTest.php, line 132

Class

NumberFieldTest
Tests the creation of numeric fields.

Namespace

Drupal\Tests\field\Functional\Number

Code

public function testNumberIntegerField() {
    $minimum = rand(-4000, -2000);
    $maximum = rand(2000, 4000);
    // Create a field with settings to validate.
    $field_name = mb_strtolower($this->randomMachineName());
    $storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'integer',
    ]);
    $storage->save();
    FieldConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'bundle' => 'entity_test',
        'settings' => [
            'min' => $minimum,
            'max' => $maximum,
            'prefix' => 'ThePrefix',
        ],
    ])->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' => 'number',
        'settings' => [
            'placeholder' => '4',
        ],
    ])
        ->save();
    $display_repository->getViewDisplay('entity_test', 'entity_test')
        ->setComponent($field_name, [
        'type' => 'number_integer',
        'settings' => [
            'prefix_suffix' => FALSE,
        ],
    ])
        ->save();
    // Check the storage schema.
    $expected = [
        'columns' => [
            'value' => [
                'type' => 'int',
                'unsigned' => '',
                'size' => 'normal',
            ],
        ],
        'unique keys' => [],
        'indexes' => [],
        'foreign keys' => [],
    ];
    $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 = [
        "{$field_name}[0][value]" => $value,
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $this->assertText(t('entity_test @id has been created.', [
        '@id' => $id,
    ]), 'Entity was created');
    // Try to set a value below the minimum value
    $this->drupalGet('entity_test/add');
    $edit = [
        "{$field_name}[0][value]" => $minimum - 1,
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('%name must be higher than or equal to %minimum.', [
        '%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 = [
        "{$field_name}[0][value]" => 1.5,
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('%name is not a valid number.', [
        '%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 = [
        "{$field_name}[0][value]" => $maximum + 1,
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('%name must be lower than or equal to %maximum.', [
        '%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 = [
        "{$field_name}[0][value]" => '20-40',
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('%name must be a number.', [
        '%name' => $field_name,
    ]), 'Correctly failed to save wrong integer value.');
    // Test with valid entries.
    $valid_entries = [
        '-1234',
        '0',
        '1234',
    ];
    foreach ($valid_entries as $valid_entry) {
        $this->drupalGet('entity_test/add');
        $edit = [
            "{$field_name}[0][value]" => $valid_entry,
        ];
        $this->drupalPostForm(NULL, $edit, t('Save'));
        preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
        $id = $match[1];
        $this->assertText(t('entity_test @id has been created.', [
            '@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.
    $display_repository->getViewDisplay('entity_test', 'entity_test')
        ->setComponent($field_name, [
        'type' => 'number_integer',
        'settings' => [
            'prefix_suffix' => TRUE,
        ],
    ])
        ->save();
    $integer_value = '123';
    $this->drupalGet('entity_test/add');
    $edit = [
        "{$field_name}[0][value]" => $integer_value,
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $this->assertText(t('entity_test @id has been created.', [
        '@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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.