CommentOrphanTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Drupal\Tests\comment\Kernel;
  3. use Drupal\Core\Datetime\Entity\DateFormat;
  4. use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
  5. use Drupal\Tests\EntityViewTrait;
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. /**
  8. * Tests loading and rendering orphan comments.
  9. *
  10. * @group comment
  11. */
  12. class CommentOrphanTest extends EntityKernelTestBase {
  13. use EntityViewTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static $modules = ['comment', 'node'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. parent::setUp();
  23. $this->installEntitySchema('date_format');
  24. $this->installEntitySchema('comment');
  25. $this->installSchema('comment', ['comment_entity_statistics']);
  26. }
  27. /**
  28. * Test loading/deleting/rendering orphaned comments.
  29. *
  30. * @dataProvider providerTestOrphan
  31. */
  32. public function testOrphan($property) {
  33. DateFormat::create([
  34. 'id' => 'fallback',
  35. 'label' => 'Fallback',
  36. 'pattern' => 'Y-m-d',
  37. ])->save();
  38. $comment_storage = $this->entityTypeManager->getStorage('comment');
  39. $node_storage = $this->entityTypeManager->getStorage('node');
  40. // Create a page node type.
  41. $this->entityTypeManager->getStorage('node_type')->create([
  42. 'type' => 'page',
  43. 'name' => 'page',
  44. ])->save();
  45. $node = $node_storage->create([
  46. 'type' => 'page',
  47. 'title' => 'test',
  48. ]);
  49. $node->save();
  50. // Create comment field.
  51. $this->entityTypeManager->getStorage('field_storage_config')->create([
  52. 'type' => 'text_long',
  53. 'entity_type' => 'node',
  54. 'field_name' => 'comment',
  55. ])->save();
  56. // Add comment field to page content.
  57. $this->entityTypeManager->getStorage('field_config')->create([
  58. 'field_storage' => FieldStorageConfig::loadByName('node', 'comment'),
  59. 'entity_type' => 'node',
  60. 'bundle' => 'page',
  61. 'label' => 'Comment',
  62. ])->save();
  63. // Make two comments
  64. $comment1 = $comment_storage->create([
  65. 'field_name' => 'comment',
  66. 'comment_body' => 'test',
  67. 'entity_id' => $node->id(),
  68. 'entity_type' => 'node',
  69. 'comment_type' => 'default',
  70. ])->save();
  71. $comment_storage->create([
  72. 'field_name' => 'comment',
  73. 'comment_body' => 'test',
  74. 'entity_id' => $node->id(),
  75. 'entity_type' => 'node',
  76. 'comment_type' => 'default',
  77. 'pid' => $comment1,
  78. ])->save();
  79. // Render the comments.
  80. $renderer = \Drupal::service('renderer');
  81. $comments = $comment_storage->loadMultiple();
  82. foreach ($comments as $comment) {
  83. $built = $this->buildEntityView($comment, 'full', NULL);
  84. $renderer->renderPlain($built);
  85. }
  86. // Make comment 2 an orphan by setting the property to an invalid value.
  87. \Drupal::database()->update('comment_field_data')
  88. ->fields([$property => 10])
  89. ->condition('cid', 2)
  90. ->execute();
  91. $comment_storage->resetCache();
  92. $node_storage->resetCache();
  93. // Render the comments with an orphan comment.
  94. $comments = $comment_storage->loadMultiple();
  95. foreach ($comments as $comment) {
  96. $built = $this->buildEntityView($comment, 'full', NULL);
  97. $renderer->renderPlain($built);
  98. }
  99. $node = $node_storage->load($node->id());
  100. $built = $this->buildEntityView($node, 'full', NULL);
  101. $renderer->renderPlain($built);
  102. }
  103. /**
  104. * Provides test data for testOrphan.
  105. */
  106. public function providerTestOrphan() {
  107. return [
  108. ['entity_id'],
  109. ['uid'],
  110. ['pid'],
  111. ];
  112. }
  113. }