function ManageFieldsFunctionalTest::cardinalitySettings

Same name in other branches
  1. 8.9.x core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php \Drupal\Tests\field_ui\Functional\ManageFieldsFunctionalTest::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 ManageFieldsFunctionalTest::cardinalitySettings()
ManageFieldsFunctionalTest::testCRUDFields in core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php
Runs the field CRUD tests.

File

core/modules/field_ui/tests/src/Functional/ManageFieldsFunctionalTest.php, line 272

Class

ManageFieldsFunctionalTest
Tests the Field UI "Manage fields" screen.

Namespace

Drupal\Tests\field_ui\Functional

Code

public function cardinalitySettings() {
    $field_edit_path = 'admin/structure/types/manage/article/fields/node.article.body/storage';
    // Assert the cardinality other field cannot be empty when cardinality is
    // set to 'number'.
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => '',
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains('Number of values is required.');
    // Submit a custom number.
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 6,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains('Updated field Body field settings.');
    $this->drupalGet($field_edit_path);
    $this->assertSession()
        ->fieldValueEquals('cardinality', 'number');
    $this->assertSession()
        ->fieldValueEquals('cardinality_number', 6);
    // Check that tabs displayed.
    $this->assertSession()
        ->linkExists('Edit');
    $this->assertSession()
        ->linkByHrefExists('admin/structure/types/manage/article/fields/node.article.body');
    $this->assertSession()
        ->linkExists('Field settings');
    $this->assertSession()
        ->linkByHrefExists($field_edit_path);
    // 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 = [
        'cardinality' => 'number',
        'cardinality_number' => 1,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field 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 = [
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains('Updated field Body field settings.');
    $this->drupalGet($field_edit_path);
    $this->assertSession()
        ->fieldValueEquals('cardinality', FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $this->assertSession()
        ->fieldValueEquals('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 = [
        'cardinality' => 'number',
        'cardinality_number' => 1,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains("There are 2 entities with 2 or more values in this field.");
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 2,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains("There is 1 entity with 3 or more values in this field.");
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 3,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field 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 = [
        'cardinality' => (string) FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field 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 = [
        'cardinality' => 'number',
        'cardinality_number' => 2,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains("There are 2 entities with 3 or more values in this field.");
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 3,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
    $this->assertSession()
        ->pageTextContains("There is 1 entity with 4 or more values in this field.");
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 4,
    ];
    $this->drupalGet($field_edit_path);
    $this->submitForm($edit, 'Save field settings');
}

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