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