3 namespace Drupal\Tests\comment\Kernel;
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
12 * Tests the new entity API for the comment field type.
16 class CommentItemTest extends FieldKernelTestBase {
25 public static $modules = ['comment', 'entity_test', 'user'];
27 protected function setUp() {
29 $this->installSchema('comment', ['comment_entity_statistics']);
30 $this->installConfig(['comment']);
34 * Tests using entity fields of the comment field type.
36 public function testCommentItem() {
37 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
39 // Verify entity creation.
40 $entity = EntityTest::create();
41 $entity->name->value = $this->randomMachineName();
44 // Verify entity has been created properly.
46 $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
47 $storage->resetCache([$id]);
48 $entity = $storage->load($id);
49 $this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
50 $this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
52 // Test sample item generation.
53 /** @var \Drupal\entity_test\Entity\EntityTest $entity */
54 $entity = EntityTest::create();
55 $entity->comment->generateSampleItems();
56 $this->entityValidateAndSave($entity);
57 $this->assertTrue(in_array($entity->get('comment')->status, [
58 CommentItemInterface::HIDDEN,
59 CommentItemInterface::CLOSED,
60 CommentItemInterface::OPEN,
61 ]), 'Comment status value in defined range');
63 $mainProperty = $entity->comment[0]->mainPropertyName();
64 $this->assertEqual('status', $mainProperty);