You are here

class ForumValidationTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/forum/tests/src/Kernel/ForumValidationTest.php \Drupal\Tests\forum\Kernel\ForumValidationTest
  2. 9 core/modules/forum/tests/src/Kernel/ForumValidationTest.php \Drupal\Tests\forum\Kernel\ForumValidationTest

Tests forum validation constraints.

@group forum

Hierarchy

  • class \Drupal\Tests\forum\Kernel\ForumValidationTest extends \Drupal\KernelTests\Core\Entity\EntityKernelTestBase

Expanded class hierarchy of ForumValidationTest

File

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

Namespace

Drupal\Tests\forum\Kernel
View source
class ForumValidationTest extends EntityKernelTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  protected static $modules = [
    'node',
    'options',
    'comment',
    'taxonomy',
    'forum',
  ];

  /**
   * Tests the forum validation constraints.
   */
  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
      ->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(t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', [
      '%forum' => $container
        ->label(),
    ]), $violations[0]
      ->getMessage());
  }

}

Members