function ForumTest::createForum

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

Creates a forum container or a forum.

Parameters

string $type: The forum type (forum container or forum).

int $parent: The forum parent. This defaults to 0, indicating a root forum.

Return value

\Drupal\Core\Database\StatementInterface The created taxonomy term data.

3 calls to ForumTest::createForum()
ForumTest::doAdminTests in core/modules/forum/tests/src/Functional/ForumTest.php
Runs admin tests on the admin user.
ForumTest::testForumTopicButton in core/modules/forum/tests/src/Functional/ForumTest.php
Evaluate whether "Add new Forum topic" button is present or not.
ForumTest::testForumWithNewPost in core/modules/forum/tests/src/Functional/ForumTest.php
Tests a forum with a new post displays properly.

File

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

Class

ForumTest
Tests for forum.module.

Namespace

Drupal\Tests\forum\Functional

Code

public function createForum($type, $parent = 0) {
    // Generate a random name/description.
    $name = $this->randomMachineName(10);
    $description = $this->randomMachineName(100);
    $edit = [
        'name[0][value]' => $name,
        'description[0][value]' => $description,
        'parent[0]' => $parent,
        'weight' => '0',
    ];
    // Create forum.
    $this->drupalGet('admin/structure/forum/add/' . $type);
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    $type = $type == 'container' ? 'forum container' : 'forum';
    $this->assertSession()
        ->pageTextContains('Created new ' . $type . ' ' . $name . '.');
    // Verify that the creation message contains a link to a term.
    $this->assertSession()
        ->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "term/")]');
    
    /** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_term_storage */
    $taxonomy_term_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('taxonomy_term');
    // Verify forum.
    $term = $taxonomy_term_storage->loadByProperties([
        'vid' => $this->config('forum.settings')
            ->get('vocabulary'),
        'name' => $name,
        'description__value' => $description,
    ]);
    $term = array_shift($term);
    $this->assertNotEmpty($term, "The forum type '{$type}' should exist in the database.");
    // Verify forum hierarchy.
    $tid = $term->id();
    $parent_tid = $taxonomy_term_storage->loadParents($tid);
    $parent_tid = empty($parent_tid) ? 0 : array_shift($parent_tid)->id();
    $this->assertSame($parent, $parent_tid, 'The ' . $type . ' is linked to its container');
    $forum = $taxonomy_term_storage->load($tid);
    $this->assertEquals($type == 'forum container', (bool) $forum->forum_container->value);
    return [
        'tid' => $tid,
        'name' => $term->getName(),
        'vid' => $term->bundle(),
    ];
}

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