function ForumTest::createForumTopic

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

Creates a forum topic.

Parameters

array $forum: A forum array.

bool $container: TRUE if $forum is a container; FALSE otherwise.

Return value

object|null The created topic node or NULL if the forum is a container.

4 calls to ForumTest::createForumTopic()
ForumTest::doBasicTests in core/modules/forum/tests/src/Functional/ForumTest.php
Runs basic tests on the indicated user.
ForumTest::generateForumTopics in core/modules/forum/tests/src/Functional/ForumTest.php
Generates forum topics.
ForumTest::testForum in core/modules/forum/tests/src/Functional/ForumTest.php
Tests forum functionality through the admin and user interfaces.
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 568

Class

ForumTest
Tests for forum.module.

Namespace

Drupal\Tests\forum\Functional

Code

public function createForumTopic($forum, $container = FALSE) {
  // Generate a random subject/body.
  $title = $this->randomMachineName(20);
  $body = $this->randomMachineName(200);
  $edit = [
    'title[0][value]' => $title,
    'body[0][value]' => $body,
  ];
  $tid = $forum['tid'];
  // Create the forum topic, preselecting the forum ID via a URL parameter.
  $this->drupalPostForm('node/add/forum', $edit, t('Save'), [
    'query' => [
      'forum_id' => $tid,
    ],
  ]);
  $type = t('Forum topic');
  if ($container) {
    $this->assertNoText(t('@type @title has been created.', [
      '@type' => $type,
      '@title' => $title,
    ]), 'Forum topic was not created');
    $this->assertRaw(t('The item %title is a forum container, not a forum.', [
      '%title' => $forum['name'],
    ]), 'Error message was shown');
    return;
  }
  else {
    $this->assertText(t('@type @title has been created.', [
      '@type' => $type,
      '@title' => $title,
    ]), 'Forum topic was created');
    $this->assertNoRaw(t('The item %title is a forum container, not a forum.', [
      '%title' => $forum['name'],
    ]), 'No error message was shown');
    // 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');
  }
  // Retrieve node object, ensure that the topic was created and in the proper forum.
  $node = $this->drupalGetNodeByTitle($title);
  $this->assertTrue($node != NULL, new FormattableMarkup('Node @title was loaded', [
    '@title' => $title,
  ]));
  $this->assertEqual($node->taxonomy_forums->target_id, $tid, 'Saved forum topic was in the expected forum');
  // View forum topic.
  $this->drupalGet('node/' . $node->id());
  $this->assertRaw($title, 'Subject was found');
  $this->assertRaw($body, 'Body was found');
  return $node;
}

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