annotate core/modules/comment/tests/src/Kernel/CommentIntegrationTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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\CommentType;
Chris@0 6 use Drupal\Core\Database\Database;
Chris@0 7 use Drupal\Core\Entity\Entity\EntityViewDisplay;
Chris@0 8 use Drupal\Core\Entity\Entity\EntityViewMode;
Chris@0 9 use Drupal\field\Entity\FieldConfig;
Chris@0 10 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 11 use Drupal\KernelTests\KernelTestBase;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Tests integration of comment with other components.
Chris@0 15 *
Chris@0 16 * @group comment
Chris@0 17 */
Chris@0 18 class CommentIntegrationTest extends KernelTestBase {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * {@inheritdoc}
Chris@0 22 */
Chris@0 23 public static $modules = ['comment', 'field', 'entity_test', 'user', 'system', 'dblog'];
Chris@0 24
Chris@0 25 /**
Chris@0 26 * {@inheritdoc}
Chris@0 27 */
Chris@0 28 protected function setUp() {
Chris@0 29 parent::setUp();
Chris@0 30 $this->installEntitySchema('entity_test');
Chris@0 31 $this->installEntitySchema('user');
Chris@0 32 $this->installEntitySchema('comment');
Chris@0 33 $this->installSchema('dblog', ['watchdog']);
Chris@0 34
Chris@0 35 // Create a new 'comment' comment-type.
Chris@0 36 CommentType::create([
Chris@0 37 'id' => 'comment',
Chris@0 38 'label' => $this->randomString(),
Chris@0 39 ])->save();
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Tests view mode setting integration.
Chris@0 44 *
Chris@0 45 * @see comment_entity_view_display_presave()
Chris@0 46 * @see CommentDefaultFormatter::calculateDependencies()
Chris@0 47 */
Chris@0 48 public function testViewMode() {
Chris@17 49 $mode = mb_strtolower($this->randomMachineName());
Chris@0 50 // Create a new comment view mode and a view display entity.
Chris@0 51 EntityViewMode::create([
Chris@0 52 'id' => "comment.$mode",
Chris@0 53 'targetEntityType' => 'comment',
Chris@0 54 'settings' => ['comment_type' => 'comment'],
Chris@0 55 ])->save();
Chris@0 56 EntityViewDisplay::create([
Chris@0 57 'targetEntityType' => 'comment',
Chris@0 58 'bundle' => 'comment',
Chris@0 59 'mode' => $mode,
Chris@0 60 ])->setStatus(TRUE)->save();
Chris@0 61
Chris@0 62 // Create a comment field attached to a host 'entity_test' entity.
Chris@0 63 FieldStorageConfig::create([
Chris@0 64 'entity_type' => 'entity_test',
Chris@0 65 'type' => 'comment',
Chris@17 66 'field_name' => $field_name = mb_strtolower($this->randomMachineName()),
Chris@0 67 'settings' => [
Chris@0 68 'comment_type' => 'comment',
Chris@0 69 ],
Chris@0 70 ])->save();
Chris@0 71 FieldConfig::create([
Chris@0 72 'entity_type' => 'entity_test',
Chris@0 73 'bundle' => 'entity_test',
Chris@0 74 'field_name' => $field_name,
Chris@0 75 ])->save();
Chris@0 76
Chris@0 77 $component = [
Chris@0 78 'type' => 'comment_default',
Chris@0 79 'settings' => ['view_mode' => $mode, 'pager_id' => 0],
Chris@0 80 ];
Chris@0 81 // Create a new 'entity_test' view display on host entity that uses the
Chris@0 82 // custom comment display in field formatter to show the field.
Chris@0 83 EntityViewDisplay::create([
Chris@0 84 'targetEntityType' => 'entity_test',
Chris@0 85 'bundle' => 'entity_test',
Chris@0 86 'mode' => 'default',
Chris@0 87 ])->setComponent($field_name, $component)->setStatus(TRUE)->save();
Chris@0 88
Chris@0 89 $host_display_id = 'entity_test.entity_test.default';
Chris@0 90 $comment_display_id = "comment.comment.$mode";
Chris@0 91
Chris@0 92 // Disable the "comment.comment.$mode" display.
Chris@0 93 EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save();
Chris@0 94
Chris@0 95 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */
Chris@0 96 $host_display = EntityViewDisplay::load($host_display_id);
Chris@0 97
Chris@0 98 // Check that the field formatter has been disabled on host view display.
Chris@0 99 $this->assertNull($host_display->getComponent($field_name));
Chris@0 100 $this->assertTrue($host_display->get('hidden')[$field_name]);
Chris@0 101
Chris@0 102 // Check that the proper warning has been logged.
Chris@0 103 $arguments = [
Chris@0 104 '@id' => $host_display_id,
Chris@0 105 '@name' => $field_name,
Chris@0 106 '@display' => EntityViewMode::load("comment.$mode")->label(),
Chris@0 107 '@mode' => $mode,
Chris@0 108 ];
Chris@0 109 $logged = (bool) Database::getConnection()->select('watchdog')
Chris@0 110 ->fields('watchdog', ['wid'])
Chris@0 111 ->condition('type', 'system')
Chris@0 112 ->condition('message', "View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.")
Chris@0 113 ->condition('variables', serialize($arguments))
Chris@0 114 ->execute()
Chris@0 115 ->fetchField();
Chris@0 116 $this->assertTrue($logged);
Chris@0 117
Chris@0 118 // Re-enable the comment view display.
Chris@0 119 EntityViewDisplay::load($comment_display_id)->setStatus(TRUE)->save();
Chris@0 120 // Re-enable the comment field formatter on host entity view display.
Chris@0 121 EntityViewDisplay::load($host_display_id)->setComponent($field_name, $component)->save();
Chris@0 122
Chris@0 123 // Delete the "comment.$mode" view mode.
Chris@0 124 EntityViewMode::load("comment.$mode")->delete();
Chris@0 125
Chris@0 126 // Check that the comment view display entity has been deleted too.
Chris@0 127 $this->assertNull(EntityViewDisplay::load($comment_display_id));
Chris@0 128
Chris@0 129 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
Chris@0 130 $host_display = EntityViewDisplay::load($host_display_id);
Chris@0 131
Chris@0 132 // Check that the field formatter has been disabled on host view display.
Chris@0 133 $this->assertNull($host_display->getComponent($field_name));
Chris@0 134 $this->assertTrue($host_display->get('hidden')[$field_name]);
Chris@0 135 }
Chris@0 136
Chris@0 137 }