CommentItemTest.php 3.87 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\comment\Kernel;
4

5
use Drupal\comment\Entity\Comment;
6
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7
use Drupal\comment\Tests\CommentTestTrait;
8
use Drupal\Core\Field\FieldItemListInterface;
9
use Drupal\entity_test\Entity\EntityTest;
10
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
11 12 13 14 15 16

/**
 * Tests the new entity API for the comment field type.
 *
 * @group comment
 */
17
class CommentItemTest extends FieldKernelTestBase {
18 19 20 21 22 23 24 25

  use CommentTestTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  protected static $modules = ['comment', 'entity_test', 'user'];
27

28
  protected function setUp(): void {
29
    parent::setUp();
30
    $this->installEntitySchema('comment');
31 32 33 34 35 36 37 38 39 40 41
    $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.
42
    $entity = EntityTest::create();
43 44 45 46 47
    $entity->name->value = $this->randomMachineName();
    $entity->save();

    // Verify entity has been created properly.
    $id = $entity->id();
48 49 50
    $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
    $storage->resetCache([$id]);
    $entity = $storage->load($id);
51 52
    $this->assertInstanceOf(FieldItemListInterface::class, $entity->comment, 'Field implements interface.');
    $this->assertInstanceOf(CommentItemInterface::class, $entity->comment[0], 'Field item implements interface.');
53 54 55

    // Test sample item generation.
    /** @var \Drupal\entity_test\Entity\EntityTest $entity */
56
    $entity = EntityTest::create();
57 58 59 60 61 62 63
    $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');
64 65 66

    $mainProperty = $entity->comment[0]->mainPropertyName();
    $this->assertEqual('status', $mainProperty);
67 68
  }

69 70 71 72 73
  /**
   * Tests comment author name.
   */
  public function testCommentAuthorName() {
    $this->installEntitySchema('comment');
74
    $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
75

76 77 78
    $host = EntityTest::create(['name' => $this->randomString()]);
    $host->save();

79 80 81 82 83 84 85
    // Create some comments.
    $comment = Comment::create([
      'subject' => 'My comment title',
      'uid' => 1,
      'name' => 'entity-test',
      'mail' => 'entity@localhost',
      'entity_type' => 'entity_test',
86
      'field_name' => 'comment',
87
      'entity_id' => $host->id(),
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      'comment_type' => 'entity_test',
      'status' => 1,
    ]);
    $comment->save();

    // The entity fields for name and mail have no meaning if the user is not
    // Anonymous.
    $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',
105
      'field_name' => 'comment',
106
      'entity_id' => $host->id(),
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      'comment_type' => 'entity_test',
      'status' => 1,
    ]);
    $comment_anonymous->save();

    // The entity fields for name and mail have retained their values when
    // comment belongs to an anonymous user.
    $this->assertNotNull($comment_anonymous->name->value);
    $this->assertNotNull($comment_anonymous->mail->value);

    $comment_anonymous->setOwnerId(1)
      ->save();
    // The entity fields for name and mail have no meaning if the user is not
    // Anonymous.
    $this->assertNull($comment_anonymous->name->value);
    $this->assertNull($comment_anonymous->mail->value);
  }

125
}