function CommentFieldsTest::testCommentFieldCreate

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentFieldCreate()
  2. 10 core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentFieldCreate()
  3. 11.x core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentFieldCreate()

Tests creating a comment field through the interface.

File

core/modules/comment/tests/src/Functional/CommentFieldsTest.php, line 150

Class

CommentFieldsTest
Tests fields on comments.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentFieldCreate() {
    // Create user who can administer user fields.
    $user = $this->drupalCreateUser([
        'administer user fields',
    ]);
    $this->drupalLogin($user);
    // Create comment field in account settings.
    $edit = [
        'new_storage_type' => 'comment',
        'label' => 'User comment',
        'field_name' => 'user_comment',
    ];
    $this->drupalPostForm('admin/config/people/accounts/fields/add-field', $edit, 'Save and continue');
    // Try to save the comment field without selecting a comment type.
    $edit = [];
    $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
    // We should get an error message.
    $this->assertText(t('An illegal choice has been detected. Please contact the site administrator.'));
    // Create a comment type for users.
    $bundle = CommentType::create([
        'id' => 'user_comment_type',
        'label' => 'user_comment_type',
        'description' => '',
        'target_entity_type_id' => 'user',
    ]);
    $bundle->save();
    // Select a comment type and try to save again.
    $edit = [
        'settings[comment_type]' => 'user_comment_type',
    ];
    $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
    // We shouldn't get an error message.
    $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.'));
}

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