function ForumTest::testAddOrphanTopic

Same name and namespace in other branches
  1. 9 core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testAddOrphanTopic()
  2. 8.9.x core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testAddOrphanTopic()
  3. 11.x core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::testAddOrphanTopic()

Tests that forum nodes can't be added without a parent.

Verifies that forum nodes are not created without choosing "forum" from the select list.

File

core/modules/forum/tests/src/Functional/ForumTest.php, line 278

Class

ForumTest
Tests for forum.module.

Namespace

Drupal\Tests\forum\Functional

Code

public function testAddOrphanTopic() : void {
  // Must remove forum topics to test creating orphan topics.
  $vid = $this->config('forum.settings')
    ->get('vocabulary');
  $tids = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE)
    ->condition('vid', $vid)
    ->execute();
  $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  $terms = $term_storage->loadMultiple($tids);
  $term_storage->delete($terms);
  // Create an orphan forum item.
  $edit = [];
  $edit['title[0][value]'] = $this->randomMachineName(10);
  $edit['body[0][value]'] = $this->randomMachineName(120);
  $this->drupalLogin($this->adminUser);
  $this->drupalGet('node/add/forum');
  $this->submitForm($edit, 'Save');
  $nid_count = $this->container
    ->get('entity_type.manager')
    ->getStorage('node')
    ->getQuery()
    ->accessCheck(FALSE)
    ->count()
    ->execute();
  $this->assertEquals(0, $nid_count, 'A forum node was not created when missing a forum vocabulary.');
  // Reset the defaults for future tests.
  \Drupal::service('module_installer')->install([
    'forum',
  ]);
}

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