CommentItemTest.php 1.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\comment\Tests\CommentItemTest.
 */

namespace Drupal\comment\Tests;

use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
12
use Drupal\entity_test\Entity\EntityTest;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
use Drupal\field\Tests\FieldUnitTestBase;

/**
 * Tests the new entity API for the comment field type.
 *
 * @group comment
 */
class CommentItemTest extends FieldUnitTestBase {

  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.
44
    $entity = EntityTest::create();
45 46 47 48 49 50 51 52 53 54 55
    $entity->name->value = $this->randomMachineName();
    $entity->save();

    // Verify entity has been created properly.
    $id = $entity->id();
    $entity = entity_load('entity_test', $id, TRUE);
    $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 */
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
  }

}