You are here

public function CommentFieldsTest::testCommentInstallAfterContentModule in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentInstallAfterContentModule()

Tests that comment module works when installed after a content module.

File

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

Class

CommentFieldsTest
Tests fields on comments.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentInstallAfterContentModule() {

  // Create a user to do module administration.
  $this->adminUser = $this
    ->drupalCreateUser([
    'access administration pages',
    'administer modules',
  ]);
  $this
    ->drupalLogin($this->adminUser);

  // Drop default comment field added in CommentTestBase::setup().
  FieldStorageConfig::loadByName('node', 'comment')
    ->delete();
  if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
    $field_storage
      ->delete();
  }

  // Purge field data now to allow comment module to be uninstalled once the
  // field has been deleted.
  field_purge_batch(10);

  // Uninstall the comment module.
  $edit = [];
  $edit['uninstall[comment]'] = TRUE;
  $this
    ->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
  $this
    ->drupalPostForm(NULL, [], t('Uninstall'));
  $this
    ->rebuildContainer();
  $this
    ->assertFalse($this->container
    ->get('module_handler')
    ->moduleExists('comment'), 'Comment module uninstalled.');

  // Install core content type module (book).
  $edit = [];
  $edit['modules[book][enable]'] = 'book';
  $this
    ->drupalPostForm('admin/modules', $edit, t('Install'));

  // Now install the comment module.
  $edit = [];
  $edit['modules[comment][enable]'] = 'comment';
  $this
    ->drupalPostForm('admin/modules', $edit, t('Install'));
  $this
    ->rebuildContainer();
  $this
    ->assertTrue($this->container
    ->get('module_handler')
    ->moduleExists('comment'), 'Comment module enabled.');

  // Create nodes of each type.
  $this
    ->addDefaultCommentField('node', 'book');
  $book_node = $this
    ->drupalCreateNode([
    'type' => 'book',
  ]);
  $this
    ->drupalLogout();

  // Try to post a comment on each node. A failure will be triggered if the
  // comment body is missing on one of these forms, due to postComment()
  // asserting that the body is actually posted correctly.
  $this->webUser = $this
    ->drupalCreateUser([
    'access content',
    'access comments',
    'post comments',
    'skip comment approval',
  ]);
  $this
    ->drupalLogin($this->webUser);
  $this
    ->postComment($book_node, $this
    ->randomMachineName(), $this
    ->randomMachineName());
}