function ForumValidationTest::testValidation

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

Tests the forum validation constraints.

File

core/modules/forum/tests/src/Kernel/ForumValidationTest.php, line 33

Class

ForumValidationTest
Tests forum validation constraints.

Namespace

Drupal\Tests\forum\Kernel

Code

public function testValidation() {
    $this->installConfig('forum');
    // 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->assertCount(1, $violations);
    $this->assertEquals('This value should not be null.', $violations[0]->getMessage());
    // Add the forum term.
    $forum_post->set('taxonomy_forums', $forum);
    $violations = $forum_post->validate();
    $this->assertCount(0, $violations);
    // Try to use a container.
    $forum_post->set('taxonomy_forums', $container);
    $violations = $forum_post->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals(sprintf('The item %s is a forum container, not a forum. Select one of the forums below instead.', $container->label()), $violations[0]->getMessage());
}

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