You are here

public function CommentFieldsTest::testCommentFieldCreate in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Tests creating a comment field through the interface.

File

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

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
    ->drupalGet('admin/config/people/accounts/fields/add-field');
  $this
    ->submitForm($edit, 'Save and continue');

  // Try to save the comment field without selecting a comment type.
  $edit = [];
  $this
    ->drupalGet('admin/config/people/accounts/fields/user.user.field_user_comment/storage');
  $this
    ->submitForm($edit, 'Save field settings');

  // We should get an error message.
  $this
    ->assertSession()
    ->pageTextContains('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
    ->drupalGet('admin/config/people/accounts/fields/user.user.field_user_comment/storage');
  $this
    ->submitForm($edit, 'Save field settings');

  // We shouldn't get an error message.
  $this
    ->assertSession()
    ->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
}