You are here

function ForumTest::createForum in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/forum/src/Tests/ForumTest.php \Drupal\forum\Tests\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/src/Tests/ForumTest.php
Runs admin tests on the admin user.
ForumTest::testForumWithNewPost in core/modules/forum/src/Tests/ForumTest.php
Tests a forum with a new post displays properly.

File

core/modules/forum/src/Tests/ForumTest.php, line 408
Contains \Drupal\forum\Tests\ForumTest.

Class

ForumTest
Create, view, edit, delete, and change forum entries and verify its consistency in the database.

Namespace

Drupal\forum\Tests

Code

function createForum($type, $parent = 0) {

  // Generate a random name/description.
  $name = $this
    ->randomMachineName(10);
  $description = $this
    ->randomMachineName(100);
  $edit = array(
    '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
    ->assertResponse(200);
  $type = $type == 'container' ? 'forum container' : 'forum';
  $this
    ->assertRaw(t('Created new @type %term.', array(
    '%term' => $name,
    '@type' => t($type),
  )), format_string('@type was created', array(
    '@type' => ucfirst($type),
  )));

  // Verify forum.
  $term = db_query("SELECT * FROM {taxonomy_term_field_data} t WHERE t.vid = :vid AND t.name = :name AND t.description__value = :desc AND t.default_langcode = 1", array(
    ':vid' => $this
      ->config('forum.settings')
      ->get('vocabulary'),
    ':name' => $name,
    ':desc' => $description,
  ))
    ->fetchAssoc();
  $this
    ->assertTrue(!empty($term), 'The ' . $type . ' exists in the database');

  // Verify forum hierarchy.
  $tid = $term['tid'];
  $parent_tid = db_query("SELECT t.parent FROM {taxonomy_term_hierarchy} t WHERE t.tid = :tid", array(
    ':tid' => $tid,
  ))
    ->fetchField();
  $this
    ->assertTrue($parent == $parent_tid, 'The ' . $type . ' is linked to its container');
  $forum = $this->container
    ->get('entity.manager')
    ->getStorage('taxonomy_term')
    ->load($tid);
  $this
    ->assertEqual($type == 'forum container', (bool) $forum->forum_container->value);
  return $term;
}