function FormTest::testFieldFormUnlimited

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormUnlimited()
  2. 8.9.x core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormUnlimited()
  3. 10 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormUnlimited()

File

core/modules/field/tests/src/Functional/FormTest.php, line 268

Class

FormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

public function testFieldFormUnlimited() : void {
    $field_storage = $this->fieldStorageUnlimited;
    $field_name = $field_storage['field_name'];
    $this->field['field_name'] = $field_name;
    FieldStorageConfig::create($field_storage)->save();
    FieldConfig::create($this->field)
        ->save();
    \Drupal::service('entity_display.repository')->getFormDisplay($this->field['entity_type'], $this->field['bundle'])
        ->setComponent($field_name)
        ->save();
    // Verify that only one "Default value" field
    // exists on the Manage field display.
    $this->drupalGet("entity_test/structure/entity_test/fields/entity_test.entity_test.{$field_name}");
    $this->assertSession()
        ->elementsCount('xpath', "//table[@id='field-unlimited-values']/tbody/tr//input[contains(@class, 'form-text')]", 1);
    // Display creation form -> 1 widget.
    $this->drupalGet('entity_test/add');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][value]", '');
    // Verify that no extraneous widget is displayed.
    $this->assertSession()
        ->fieldNotExists("{$field_name}[1][value]");
    // Check if aria-describedby attribute is placed on multiple value widgets.
    $this->assertSession()
        ->elementAttributeContains('xpath', '//table[@id="field-unlimited-values"]', 'aria-describedby', 'edit-field-unlimited--description');
    // Press 'add more' button -> 2 widgets.
    $this->submitForm([], 'Add another item');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][value]", '');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[1][value]", '');
    // Verify that no extraneous widget is displayed.
    $this->assertSession()
        ->fieldNotExists("{$field_name}[2][value]");
    // @todo check that non-field inputs are preserved ('title'), etc.
    // Yet another time so that we can play with more values -> 3 widgets.
    $this->submitForm([], 'Add another item');
    // Prepare values and weights.
    $count = 3;
    $delta_range = $count - 1;
    $values = $weights = $pattern = $expected_values = [];
    $edit = [];
    for ($delta = 0; $delta <= $delta_range; $delta++) {
        // Assign unique random values and weights.
        do {
            $value = mt_rand(1, 127);
        } while (in_array($value, $values));
        do {
            $weight = mt_rand(-$delta_range, $delta_range);
        } while (in_array($weight, $weights));
        $edit["{$field_name}[{$delta}][value]"] = $value;
        $edit["{$field_name}[{$delta}][_weight]"] = $weight;
        // We'll need three slightly different formats to check the values.
        $values[$delta] = $value;
        $weights[$delta] = $weight;
        $field_values[$weight]['value'] = (string) $value;
        $pattern[$weight] = "<input [^>]*value=\"{$value}\" [^>]*";
    }
    // Press 'add more' button -> 4 widgets
    $this->submitForm($edit, 'Add another item');
    for ($delta = 0; $delta <= $delta_range; $delta++) {
        $this->assertSession()
            ->fieldValueEquals("{$field_name}[{$delta}][value]", $values[$delta]);
        $this->assertSession()
            ->fieldValueEquals("{$field_name}[{$delta}][_weight]", $weights[$delta]);
    }
    ksort($pattern);
    $pattern = implode('.*', array_values($pattern));
    // Verify that the widgets are displayed in the correct order.
    $this->assertSession()
        ->responseMatches("|{$pattern}|s");
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[{$delta}][value]", '');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[{$delta}][_weight]", $delta);
    // Verify that no extraneous widget is displayed.
    $this->assertSession()
        ->fieldNotExists("{$field_name}[" . ($delta + 1) . '][value]');
    // Submit the form and create the entity.
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $this->assertSession()
        ->pageTextContains('entity_test ' . $id . ' has been created.');
    $entity = EntityTest::load($id);
    ksort($field_values);
    $field_values = array_values($field_values);
    $this->assertSame($field_values, $entity->{$field_name}
        ->getValue(), 'Field values were saved in the correct order');
    // Display edit form: check that the expected number of widgets is
    // displayed, with correct values change values, reorder, leave an empty
    // value in the middle.
    // Submit: check that the entity is updated with correct values
    // Re-submit: check that the field can be emptied.
    // Test with several multiple fields in a form
}

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