View source
<?php
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\Comment;
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;
class CommentItemTest extends FieldKernelTestBase {
use CommentTestTrait;
protected static $modules = [
'comment',
'entity_test',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('comment');
$this
->installSchema('comment', [
'comment_entity_statistics',
]);
$this
->installConfig([
'comment',
]);
}
public function testCommentItem() {
$this
->addDefaultCommentField('entity_test', 'entity_test', 'comment');
$entity = EntityTest::create();
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
$storage
->resetCache([
$id,
]);
$entity = $storage
->load($id);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->comment);
$this
->assertInstanceOf(CommentItemInterface::class, $entity->comment[0]);
$entity = EntityTest::create();
$entity->comment
->generateSampleItems();
$this
->entityValidateAndSave($entity);
$this
->assertContains($entity
->get('comment')->status, [
CommentItemInterface::HIDDEN,
CommentItemInterface::CLOSED,
CommentItemInterface::OPEN,
], 'Comment status value in defined range');
$mainProperty = $entity->comment[0]
->mainPropertyName();
$this
->assertEquals('status', $mainProperty);
}
public function testCommentAuthorName() {
$this
->installEntitySchema('comment');
$this
->addDefaultCommentField('entity_test', 'entity_test', 'comment');
$host = EntityTest::create([
'name' => $this
->randomString(),
]);
$host
->save();
$comment = Comment::create([
'subject' => 'My comment title',
'uid' => 1,
'name' => 'entity-test',
'mail' => 'entity@localhost',
'entity_type' => 'entity_test',
'field_name' => 'comment',
'entity_id' => $host
->id(),
'comment_type' => 'entity_test',
'status' => 1,
]);
$comment
->save();
$this
->assertNull($comment->name->value);
$this
->assertNull($comment->mail->value);
$comment_anonymous = Comment::create([
'subject' => 'Anonymous comment title',
'uid' => 0,
'name' => 'barry',
'mail' => 'test@example.com',
'homepage' => 'https://example.com',
'entity_type' => 'entity_test',
'field_name' => 'comment',
'entity_id' => $host
->id(),
'comment_type' => 'entity_test',
'status' => 1,
]);
$comment_anonymous
->save();
$this
->assertNotNull($comment_anonymous->name->value);
$this
->assertNotNull($comment_anonymous->mail->value);
$comment_anonymous
->setOwnerId(1)
->save();
$this
->assertNull($comment_anonymous->name->value);
$this
->assertNull($comment_anonymous->mail->value);
}
}