function ManageFieldsLifecycleTest::cardinalitySettings

Same name in other branches
  1. 10 core/modules/field_ui/tests/src/Functional/ManageFieldsLifecycleTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsLifecycleTest::cardinalitySettings()

Tests the cardinality settings of a field.

We do not test if the number can be submitted with anything else than a numeric value. That is tested already in FormTest::testNumber().

1 call to ManageFieldsLifecycleTest::cardinalitySettings()
ManageFieldsLifecycleTest::testCRUDFields in core/modules/field_ui/tests/src/Functional/ManageFieldsLifecycleTest.php
Runs the field CRUD tests.

File

core/modules/field_ui/tests/src/Functional/ManageFieldsLifecycleTest.php, line 137

Class

ManageFieldsLifecycleTest
Tests the Field UI "Manage fields" screen.

Namespace

Drupal\Tests\field_ui\Functional

Code

protected function cardinalitySettings() {
    $field_edit_path = 'admin/structure/types/manage/article/fields/node.article.body';
    // Assert the cardinality other field cannot be empty when cardinality is
    // set to 'number'.
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => '',
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->assertSession()
        ->pageTextContains('Number of values is required.');
    // Submit a custom number.
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 6,
    ];
    $this->submitForm($edit, 'Update settings');
    $this->submitForm([], 'Save settings');
    $this->drupalGet($field_edit_path);
    $this->assertSession()
        ->fieldValueEquals('field_storage[subform][cardinality]', 'number');
    $this->assertSession()
        ->fieldValueEquals('field_storage[subform][cardinality_number]', 6);
    // Add two entries in the body.
    $edit = [
        'title[0][value]' => 'Cardinality',
        'body[0][value]' => 'Body 1',
        'body[1][value]' => 'Body 2',
    ];
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    // Assert that you can't set the cardinality to a lower number than the
    // highest delta of this field.
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 1,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->assertSession()
        ->pageTextContains("There is 1 entity with 2 or more values in this field");
    // Create a second entity with three values.
    $edit = [
        'title[0][value]' => 'Cardinality 3',
        'body[0][value]' => 'Body 1',
        'body[1][value]' => 'Body 2',
        'body[2][value]' => 'Body 3',
    ];
    $this->drupalGet('node/add/article');
    $this->submitForm($edit, 'Save');
    // Set to unlimited.
    $edit = [
        'field_storage[subform][cardinality]' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->submitForm([], 'Save settings');
    $this->drupalGet($field_edit_path);
    $this->assertSession()
        ->fieldValueEquals('field_storage[subform][cardinality]', FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $this->assertSession()
        ->fieldValueEquals('field_storage[subform][cardinality_number]', 1);
    // Assert that you can't set the cardinality to a lower number then the
    // highest delta of this field but can set it to the same.
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 1,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->submitForm([], 'Save settings');
    $this->assertSession()
        ->pageTextContains("There are 2 entities with 2 or more values in this field");
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 2,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->assertSession()
        ->pageTextContains("There is 1 entity with 3 or more values in this field");
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 3,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    // Test the cardinality validation is not access sensitive.
    // Remove the cardinality limit 4 so we can add a node the user doesn't have access to.
    $edit = [
        'field_storage[subform][cardinality]' => (string) FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $node = $this->drupalCreateNode([
        'private' => TRUE,
        'uid' => 0,
        'type' => 'article',
    ]);
    $node->body
        ->appendItem('body 1');
    $node->body
        ->appendItem('body 2');
    $node->body
        ->appendItem('body 3');
    $node->body
        ->appendItem('body 4');
    $node->save();
    // Assert that you can't set the cardinality to a lower number then the
    // highest delta of this field (including inaccessible entities) but can
    // set it to the same.
    $this->drupalGet($field_edit_path);
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 2,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->assertSession()
        ->pageTextContains("There are 2 entities with 3 or more values in this field");
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 3,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->assertSession()
        ->pageTextContains("There is 1 entity with 4 or more values in this field");
    $edit = [
        'field_storage[subform][cardinality]' => 'number',
        'field_storage[subform][cardinality_number]' => 4,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Update settings');
    $this->submitForm([], 'Save settings');
}

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