Version 1
[yaffs-website] / web / core / modules / forum / tests / src / Kernel / ForumValidationTest.php
1 <?php
2
3 namespace Drupal\Tests\forum\Kernel;
4
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\taxonomy\Entity\Term;
8
9 /**
10  * Tests forum validation constraints.
11  *
12  * @group forum
13  */
14 class ForumValidationTest extends EntityKernelTestBase {
15
16   /**
17    * Modules to install.
18    *
19    * @var array
20    */
21   public static $modules = ['node', 'options', 'comment', 'taxonomy', 'forum'];
22
23   /**
24    * Tests the forum validation constraints.
25    */
26   public function testValidation() {
27     // Add a forum.
28     $forum = Term::create([
29       'name' => 'forum 1',
30       'vid' => 'forums',
31       'forum_container' => 0,
32     ]);
33
34     // Add a container.
35     $container = Term::create([
36       'name' => 'container 1',
37       'vid' => 'forums',
38       'forum_container' => 1,
39     ]);
40
41     // Add a forum post.
42     $forum_post = Node::create([
43       'type' => 'forum',
44       'title' => 'Do these pants make my butt look big?',
45     ]);
46
47     $violations = $forum_post->validate();
48     $this->assertEqual(count($violations), 1);
49     $this->assertEqual($violations[0]->getMessage(), 'This value should not be null.');
50
51     // Add the forum term.
52     $forum_post->set('taxonomy_forums', $forum);
53     $violations = $forum_post->validate();
54     $this->assertEqual(count($violations), 0);
55
56     // Try to use a container.
57     $forum_post->set('taxonomy_forums', $container);
58     $violations = $forum_post->validate();
59     $this->assertEqual(count($violations), 1);
60     $this->assertEqual($violations[0]->getMessage(), t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', [
61       '%forum' => $container->label(),
62     ]));
63   }
64
65 }