Version 1
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / CommentItemTest.php
diff --git a/web/core/modules/comment/tests/src/Kernel/CommentItemTest.php b/web/core/modules/comment/tests/src/Kernel/CommentItemTest.php
new file mode 100644 (file)
index 0000000..7b9a263
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+namespace Drupal\Tests\comment\Kernel;
+
+use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
+use Drupal\comment\Tests\CommentTestTrait;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\entity_test\Entity\EntityTest;
+use Drupal\Tests\field\Kernel\FieldKernelTestBase;
+
+/**
+ * Tests the new entity API for the comment field type.
+ *
+ * @group comment
+ */
+class CommentItemTest extends FieldKernelTestBase {
+
+  use CommentTestTrait;
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = ['comment', 'entity_test', 'user'];
+
+  protected function setUp() {
+    parent::setUp();
+    $this->installSchema('comment', ['comment_entity_statistics']);
+    $this->installConfig(['comment']);
+  }
+
+  /**
+   * Tests using entity fields of the comment field type.
+   */
+  public function testCommentItem() {
+    $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
+
+    // Verify entity creation.
+    $entity = EntityTest::create();
+    $entity->name->value = $this->randomMachineName();
+    $entity->save();
+
+    // Verify entity has been created properly.
+    $id = $entity->id();
+    $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
+    $storage->resetCache([$id]);
+    $entity = $storage->load($id);
+    $this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
+    $this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
+
+    // Test sample item generation.
+    /** @var \Drupal\entity_test\Entity\EntityTest $entity */
+    $entity = EntityTest::create();
+    $entity->comment->generateSampleItems();
+    $this->entityValidateAndSave($entity);
+    $this->assertTrue(in_array($entity->get('comment')->status, [
+      CommentItemInterface::HIDDEN,
+      CommentItemInterface::CLOSED,
+      CommentItemInterface::OPEN,
+    ]), 'Comment status value in defined range');
+
+    $mainProperty = $entity->comment[0]->mainPropertyName();
+    $this->assertEqual('status', $mainProperty);
+  }
+
+}