You are here

public function ForumValidationTest::testValidation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/forum/src/Tests/ForumValidationTest.php \Drupal\forum\Tests\ForumValidationTest::testValidation()

Tests the forum validation constraints.

File

core/modules/forum/src/Tests/ForumValidationTest.php, line 31
Contains \Drupal\forum\Tests\ForumValidationTest.

Class

ForumValidationTest
Tests forum validation constraints.

Namespace

Drupal\forum\Tests

Code

public function testValidation() {

  // Add a forum.
  $forum = Term::create([
    'name' => 'forum 1',
    'vid' => 'forums',
    'forum_container' => 0,
  ]);

  // Add a container.
  $container = Term::create([
    'name' => 'container 1',
    'vid' => 'forums',
    'forum_container' => 1,
  ]);

  // Add a forum post.
  $forum_post = Node::create([
    'type' => 'forum',
    'title' => 'Do these pants make my butt look big?',
  ]);
  $violations = $forum_post
    ->validate();
  $this
    ->assertEqual(count($violations), 1);
  $this
    ->assertEqual($violations[0]
    ->getMessage(), 'This value should not be null.');

  // Add the forum term.
  $forum_post
    ->set('taxonomy_forums', $forum);
  $violations = $forum_post
    ->validate();
  $this
    ->assertEqual(count($violations), 0);

  // Try to use a container.
  $forum_post
    ->set('taxonomy_forums', $container);
  $violations = $forum_post
    ->validate();
  $this
    ->assertEqual(count($violations), 1);
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', [
    '%forum' => $container
      ->label(),
  ]));
}