function ManageFieldsFunctionalTest::cardinalitySettings

Same name in other branches
  1. 9 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 277

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->drupalPostForm($field_edit_path, $edit, t('Save field settings'));
    $this->assertText('Number of values is required.');
    // Submit a custom number.
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 6,
    ];
    $this->drupalPostForm($field_edit_path, $edit, t('Save field settings'));
    $this->assertText('Updated field Body field settings.');
    $this->drupalGet($field_edit_path);
    $this->assertFieldByXPath("//select[@name='cardinality']", 'number');
    $this->assertFieldByXPath("//input[@name='cardinality_number']", 6);
    // Check that tabs displayed.
    $this->assertSession()
        ->linkExists(t('Edit'));
    $this->assertLinkByHref('admin/structure/types/manage/article/fields/node.article.body');
    $this->assertSession()
        ->linkExists(t('Field settings'));
    $this->assertLinkByHref($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->drupalPostForm('node/add/article', $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->drupalPostForm($field_edit_path, $edit, t('Save field settings'));
    $this->assertRaw(t('There is @count entity with @delta or more values in this field.', [
        '@count' => 1,
        '@delta' => 2,
    ]), 'Correctly failed to set cardinality lower than highest delta.');
    // 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->drupalPostForm('node/add/article', $edit, 'Save');
    // Set to unlimited.
    $edit = [
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ];
    $this->drupalPostForm($field_edit_path, $edit, t('Save field settings'));
    $this->assertText('Updated field Body field settings.');
    $this->drupalGet($field_edit_path);
    $this->assertFieldByXPath("//select[@name='cardinality']", FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $this->assertFieldByXPath("//input[@name='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->drupalPostForm($field_edit_path, $edit, t('Save field settings'));
    $this->assertRaw(t('There are @count entities with @delta or more values in this field.', [
        '@count' => 2,
        '@delta' => 2,
    ]), 'Correctly failed to set cardinality lower than highest delta.');
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 2,
    ];
    $this->drupalPostForm($field_edit_path, $edit, t('Save field settings'));
    $this->assertRaw(t('There is @count entity with @delta or more values in this field.', [
        '@count' => 1,
        '@delta' => 3,
    ]), 'Correctly failed to set cardinality lower than highest delta.');
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 3,
    ];
    $this->drupalPostForm($field_edit_path, $edit, t('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->drupalPostForm($field_edit_path, $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 than 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->drupalPostForm($field_edit_path, $edit, 'Save field settings');
    $this->assertRaw(t('There are @count entities with @delta or more values in this field.', [
        '@count' => 2,
        '@delta' => 3,
    ]));
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 3,
    ];
    $this->drupalPostForm($field_edit_path, $edit, 'Save field settings');
    $this->assertRaw(t('There is @count entity with @delta or more values in this field.', [
        '@count' => 1,
        '@delta' => 4,
    ]));
    $edit = [
        'cardinality' => 'number',
        'cardinality_number' => 4,
    ];
    $this->drupalPostForm($field_edit_path, $edit, 'Save field settings');
}

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