3 namespace Drupal\Tests\comment\Kernel;
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;
13 * Tests the new entity API for the comment field type.
17 class CommentItemTest extends FieldKernelTestBase {
26 public static $modules = ['comment', 'entity_test', 'user'];
28 protected function setUp() {
30 $this->installSchema('comment', ['comment_entity_statistics']);
31 $this->installConfig(['comment']);
35 * Tests using entity fields of the comment field type.
37 public function testCommentItem() {
38 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
40 // Verify entity creation.
41 $entity = EntityTest::create();
42 $entity->name->value = $this->randomMachineName();
45 // Verify entity has been created properly.
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.');
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');
64 $mainProperty = $entity->comment[0]->mainPropertyName();
65 $this->assertEqual('status', $mainProperty);
69 * Tests comment author name.
71 public function testCommentAuthorName() {
72 $this->installEntitySchema('comment');
74 $host = EntityTest::create(['name' => $this->randomString()]);
77 // Create some comments.
78 $comment = Comment::create([
79 'subject' => 'My comment title',
81 'name' => 'entity-test',
82 'mail' => 'entity@localhost',
83 'entity_type' => 'entity_test',
84 'entity_id' => $host->id(),
85 'comment_type' => 'entity_test',
90 // The entity fields for name and mail have no meaning if the user is not
92 $this->assertNull($comment->name->value);
93 $this->assertNull($comment->mail->value);
95 $comment_anonymous = Comment::create([
96 'subject' => 'Anonymous comment title',
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',
106 $comment_anonymous->save();
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);
113 $comment_anonymous->setOwnerId(1)
115 // The entity fields for name and mail have no meaning if the user is not
117 $this->assertNull($comment_anonymous->name->value);
118 $this->assertNull($comment_anonymous->mail->value);