Version 1
[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\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;
10
11 /**
12  * Tests the new entity API for the comment field type.
13  *
14  * @group comment
15  */
16 class CommentItemTest extends FieldKernelTestBase {
17
18   use CommentTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['comment', 'entity_test', 'user'];
26
27   protected function setUp() {
28     parent::setUp();
29     $this->installSchema('comment', ['comment_entity_statistics']);
30     $this->installConfig(['comment']);
31   }
32
33   /**
34    * Tests using entity fields of the comment field type.
35    */
36   public function testCommentItem() {
37     $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
38
39     // Verify entity creation.
40     $entity = EntityTest::create();
41     $entity->name->value = $this->randomMachineName();
42     $entity->save();
43
44     // Verify entity has been created properly.
45     $id = $entity->id();
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.');
51
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');
62
63     $mainProperty = $entity->comment[0]->mainPropertyName();
64     $this->assertEqual('status', $mainProperty);
65   }
66
67 }