Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / CommentItemTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel;
4
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\Core\Field\FieldItemListInterface;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
11
12 /**
13  * Tests the new entity API for the comment field type.
14  *
15  * @group comment
16  */
17 class CommentItemTest extends FieldKernelTestBase {
18
19   use CommentTestTrait;
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['comment', 'entity_test', 'user'];
27
28   protected function setUp() {
29     parent::setUp();
30     $this->installSchema('comment', ['comment_entity_statistics']);
31     $this->installConfig(['comment']);
32   }
33
34   /**
35    * Tests using entity fields of the comment field type.
36    */
37   public function testCommentItem() {
38     $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
39
40     // Verify entity creation.
41     $entity = EntityTest::create();
42     $entity->name->value = $this->randomMachineName();
43     $entity->save();
44
45     // Verify entity has been created properly.
46     $id = $entity->id();
47     $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
48     $storage->resetCache([$id]);
49     $entity = $storage->load($id);
50     $this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
51     $this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
52
53     // Test sample item generation.
54     /** @var \Drupal\entity_test\Entity\EntityTest $entity */
55     $entity = EntityTest::create();
56     $entity->comment->generateSampleItems();
57     $this->entityValidateAndSave($entity);
58     $this->assertTrue(in_array($entity->get('comment')->status, [
59       CommentItemInterface::HIDDEN,
60       CommentItemInterface::CLOSED,
61       CommentItemInterface::OPEN,
62     ]), 'Comment status value in defined range');
63
64     $mainProperty = $entity->comment[0]->mainPropertyName();
65     $this->assertEqual('status', $mainProperty);
66   }
67
68   /**
69    * Tests comment author name.
70    */
71   public function testCommentAuthorName() {
72     $this->installEntitySchema('comment');
73
74     $host = EntityTest::create(['name' => $this->randomString()]);
75     $host->save();
76
77     // Create some comments.
78     $comment = Comment::create([
79       'subject' => 'My comment title',
80       'uid' => 1,
81       'name' => 'entity-test',
82       'mail' => 'entity@localhost',
83       'entity_type' => 'entity_test',
84       'entity_id' => $host->id(),
85       'comment_type' => 'entity_test',
86       'status' => 1,
87     ]);
88     $comment->save();
89
90     // The entity fields for name and mail have no meaning if the user is not
91     // Anonymous.
92     $this->assertNull($comment->name->value);
93     $this->assertNull($comment->mail->value);
94
95     $comment_anonymous = Comment::create([
96       'subject' => 'Anonymous comment title',
97       'uid' => 0,
98       'name' => 'barry',
99       'mail' => 'test@example.com',
100       'homepage' => 'https://example.com',
101       'entity_type' => 'entity_test',
102       'entity_id' => $host->id(),
103       'comment_type' => 'entity_test',
104       'status' => 1,
105     ]);
106     $comment_anonymous->save();
107
108     // The entity fields for name and mail have retained their values when
109     // comment belongs to an anonymous user.
110     $this->assertNotNull($comment_anonymous->name->value);
111     $this->assertNotNull($comment_anonymous->mail->value);
112
113     $comment_anonymous->setOwnerId(1)
114       ->save();
115     // The entity fields for name and mail have no meaning if the user is not
116     // Anonymous.
117     $this->assertNull($comment_anonymous->name->value);
118     $this->assertNull($comment_anonymous->mail->value);
119   }
120
121 }