annotate core/modules/comment/tests/src/Kernel/CommentIntegrationTest.php @ 0:c75dbcec494b

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