CommentLegacyTest.php 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
<?php

namespace Drupal\Tests\comment\Kernel;

use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;

/**
 * Tests legacy comment functionality.
 *
 * @group comment
 * @group legacy
 */
class CommentLegacyTest extends EntityKernelTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['comment'];

  /**
   * The entity to which the comments are attached to.
   *
   * @var \Drupal\entity_test\Entity\EntityTest
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installEntitySchema('comment');
    $this->installSchema('comment', ['comment_entity_statistics']);
    CommentType::create([
      'id' => 'comment',
      'label' => $this->randomString(),
      'target_entity_type_id' => 'entity_test',
    ])->save();
    FieldStorageConfig::create([
      'entity_type' => 'entity_test',
      'type' => 'comment',
      'field_name' => 'comments',
      'settings' => [
        'comment_type' => 'comment',
      ],
    ])->save();
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'field_name' => 'comments',
    ])->save();
    $this->entity = EntityTest::create(['name' => $this->randomString()]);
    $this->entity->save();
  }

  /**
   * Constructs comment entity.
   *
   * @return \Drupal\comment\CommentInterface
   *   Created comment entity.
   */
  protected function createComment() {
    return Comment::create([
      'entity_type' => 'entity_test',
      'field_name' => 'comments',
      'entity_id' => $this->entity->id(),
      'comment_type' => 'comment',
    ]);
  }

  /**
   * @expectedDeprecation comment_view() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->view() instead. See https://www.drupal.org/node/3033656
   * @expectedDeprecation comment_view_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('comment')->viewMultiple() instead. See https://www.drupal.org/node/3033656
   */
  public function testCommentView() {
    $entity = $this->createComment();
    $this->assertNotEmpty(comment_view($entity));
    $entities = [
      $this->createComment(),
      $this->createComment(),
    ];
    $this->assertEquals(4, count(comment_view_multiple($entities)));
  }

}