annotate core/modules/comment/tests/src/Kernel/CommentItemTest.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\comment\Kernel;
Chris@0 4
Chris@0 5 use Drupal\comment\Entity\Comment;
Chris@0 6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
Chris@0 7 use Drupal\comment\Tests\CommentTestTrait;
Chris@0 8 use Drupal\Core\Field\FieldItemListInterface;
Chris@0 9 use Drupal\entity_test\Entity\EntityTest;
Chris@0 10 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Tests the new entity API for the comment field type.
Chris@0 14 *
Chris@0 15 * @group comment
Chris@0 16 */
Chris@0 17 class CommentItemTest extends FieldKernelTestBase {
Chris@0 18
Chris@0 19 use CommentTestTrait;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Modules to enable.
Chris@0 23 *
Chris@0 24 * @var array
Chris@0 25 */
Chris@0 26 public static $modules = ['comment', 'entity_test', 'user'];
Chris@0 27
Chris@0 28 protected function setUp() {
Chris@0 29 parent::setUp();
Chris@0 30 $this->installSchema('comment', ['comment_entity_statistics']);
Chris@0 31 $this->installConfig(['comment']);
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Tests using entity fields of the comment field type.
Chris@0 36 */
Chris@0 37 public function testCommentItem() {
Chris@0 38 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
Chris@0 39
Chris@0 40 // Verify entity creation.
Chris@0 41 $entity = EntityTest::create();
Chris@0 42 $entity->name->value = $this->randomMachineName();
Chris@0 43 $entity->save();
Chris@0 44
Chris@0 45 // Verify entity has been created properly.
Chris@0 46 $id = $entity->id();
Chris@0 47 $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
Chris@0 48 $storage->resetCache([$id]);
Chris@0 49 $entity = $storage->load($id);
Chris@0 50 $this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
Chris@0 51 $this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
Chris@0 52
Chris@0 53 // Test sample item generation.
Chris@0 54 /** @var \Drupal\entity_test\Entity\EntityTest $entity */
Chris@0 55 $entity = EntityTest::create();
Chris@0 56 $entity->comment->generateSampleItems();
Chris@0 57 $this->entityValidateAndSave($entity);
Chris@0 58 $this->assertTrue(in_array($entity->get('comment')->status, [
Chris@0 59 CommentItemInterface::HIDDEN,
Chris@0 60 CommentItemInterface::CLOSED,
Chris@0 61 CommentItemInterface::OPEN,
Chris@0 62 ]), 'Comment status value in defined range');
Chris@0 63
Chris@0 64 $mainProperty = $entity->comment[0]->mainPropertyName();
Chris@0 65 $this->assertEqual('status', $mainProperty);
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Tests comment author name.
Chris@0 70 */
Chris@0 71 public function testCommentAuthorName() {
Chris@0 72 $this->installEntitySchema('comment');
Chris@0 73
Chris@0 74 $host = EntityTest::create(['name' => $this->randomString()]);
Chris@0 75 $host->save();
Chris@0 76
Chris@0 77 // Create some comments.
Chris@0 78 $comment = Comment::create([
Chris@0 79 'subject' => 'My comment title',
Chris@0 80 'uid' => 1,
Chris@0 81 'name' => 'entity-test',
Chris@0 82 'mail' => 'entity@localhost',
Chris@0 83 'entity_type' => 'entity_test',
Chris@0 84 'entity_id' => $host->id(),
Chris@0 85 'comment_type' => 'entity_test',
Chris@0 86 'status' => 1,
Chris@0 87 ]);
Chris@0 88 $comment->save();
Chris@0 89
Chris@0 90 // The entity fields for name and mail have no meaning if the user is not
Chris@0 91 // Anonymous.
Chris@0 92 $this->assertNull($comment->name->value);
Chris@0 93 $this->assertNull($comment->mail->value);
Chris@0 94
Chris@0 95 $comment_anonymous = Comment::create([
Chris@0 96 'subject' => 'Anonymous comment title',
Chris@0 97 'uid' => 0,
Chris@0 98 'name' => 'barry',
Chris@0 99 'mail' => 'test@example.com',
Chris@0 100 'homepage' => 'https://example.com',
Chris@0 101 'entity_type' => 'entity_test',
Chris@0 102 'entity_id' => $host->id(),
Chris@0 103 'comment_type' => 'entity_test',
Chris@0 104 'status' => 1,
Chris@0 105 ]);
Chris@0 106 $comment_anonymous->save();
Chris@0 107
Chris@0 108 // The entity fields for name and mail have retained their values when
Chris@0 109 // comment belongs to an anonymous user.
Chris@0 110 $this->assertNotNull($comment_anonymous->name->value);
Chris@0 111 $this->assertNotNull($comment_anonymous->mail->value);
Chris@0 112
Chris@0 113 $comment_anonymous->setOwnerId(1)
Chris@0 114 ->save();
Chris@0 115 // The entity fields for name and mail have no meaning if the user is not
Chris@0 116 // Anonymous.
Chris@0 117 $this->assertNull($comment_anonymous->name->value);
Chris@0 118 $this->assertNull($comment_anonymous->mail->value);
Chris@0 119 }
Chris@0 120
Chris@0 121 }