ForumValidationTest.php 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
<?php

/**
 * @file
 * Contains \Drupal\forum\Tests\ForumValidationTest.
 */

namespace Drupal\forum\Tests;

use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;
use Drupal\system\Tests\Entity\EntityUnitTestBase;

/**
 * Tests forum validation constraints.
 *
 * @group forum
 */
class ForumValidationTest extends EntityUnitTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  public 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->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(),
    ]));
  }

}