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. 10 core/modules/forum/tests/src/Functional/ForumTest.php \Drupal\Tests\forum\Functional\ForumTest::createForum()
  3. 11.x 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.

2 calls to ForumTest::createForum()
ForumTest::doAdminTests in core/modules/forum/tests/src/Functional/ForumTest.php
Runs admin tests on the admin user.
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 427

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->drupalPostForm('admin/structure/forum/add/' . $type, $edit, t('Save'));
    $this->assertSession()
        ->statusCodeEquals(200);
    $type = $type == 'container' ? 'forum container' : 'forum';
    $this->assertText(t('Created new @type @term.', [
        '@term' => $name,
        '@type' => t($type),
    ]), new FormattableMarkup('@type was created', [
        '@type' => ucfirst($type),
    ]));
    // Verify that the creation message contains a link to a term.
    $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
        ':href' => 'term/',
    ]);
    $this->assert(isset($view_link), 'The message area contains a link to a 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->assertTrue(!empty($term), 'The ' . $type . ' exists 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->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
    $forum = $taxonomy_term_storage->load($tid);
    $this->assertEqual($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.