3 namespace Drupal\comment\Tests;
4 use Drupal\comment\Entity\Comment;
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\node\Entity\Node;
12 * Ensures that comment type functions work correctly.
16 class CommentTypeTest extends CommentTestBase {
21 * @var \Drupal\Core\Session\AccountInterface
26 * Permissions to grant admin user.
30 protected $permissions = [
31 'administer comments',
32 'administer comment fields',
33 'administer comment types',
39 protected function setUp() {
42 $this->drupalPlaceBlock('page_title_block');
44 $this->adminUser = $this->drupalCreateUser($this->permissions);
48 * Tests creating a comment type programmatically and via a form.
50 public function testCommentTypeCreation() {
51 // Create a comment type programmatically.
52 $type = $this->createCommentType('other');
54 $comment_type = CommentType::load('other');
55 $this->assertTrue($comment_type, 'The new comment type has been created.');
57 // Log in a test user.
58 $this->drupalLogin($this->adminUser);
60 $this->drupalGet('admin/structure/comment/manage/' . $type->id());
61 $this->assertResponse(200, 'The new comment type can be accessed at the edit form.');
63 // Create a comment type via the user interface.
66 'label' => 'title for foo',
68 'target_entity_type_id' => 'node',
70 $this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save'));
71 $comment_type = CommentType::load('foo');
72 $this->assertTrue($comment_type, 'The new comment type has been created.');
74 // Check that the comment type was created in site default language.
75 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
76 $this->assertEqual($comment_type->language()->getId(), $default_langcode);
78 // Edit the comment-type and ensure that we cannot change the entity-type.
79 $this->drupalGet('admin/structure/comment/manage/foo');
80 $this->assertNoField('target_entity_type_id', 'Entity type file not present');
81 $this->assertText(t('Target entity type'));
82 // Save the form and ensure the entity-type value is preserved even though
83 // the field isn't present.
84 $this->drupalPostForm(NULL, [], t('Save'));
85 \Drupal::entityManager()->getStorage('comment_type')->resetCache(['foo']);
86 $comment_type = CommentType::load('foo');
87 $this->assertEqual($comment_type->getTargetEntityTypeId(), 'node');
91 * Tests editing a comment type using the UI.
93 public function testCommentTypeEditing() {
94 $this->drupalLogin($this->adminUser);
96 $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
97 $this->assertEqual($field->getLabel(), 'Comment', 'Comment body field was found.');
99 // Change the comment type name.
100 $this->drupalGet('admin/structure/comment');
104 $this->drupalPostForm('admin/structure/comment/manage/comment', $edit, t('Save'));
106 $this->drupalGet('admin/structure/comment');
107 $this->assertRaw('Bar', 'New name was displayed.');
108 $this->clickLink('Manage fields');
109 $this->assertUrl(\Drupal::url('entity.comment.field_ui_fields', ['comment_type' => 'comment'], ['absolute' => TRUE]), [], 'Original machine name was used in URL.');
110 $this->assertTrue($this->cssSelect('tr#comment-body'), 'Body field exists.');
112 // Remove the body field.
113 $this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete', [], t('Delete'));
114 // Resave the settings for this type.
115 $this->drupalPostForm('admin/structure/comment/manage/comment', [], t('Save'));
116 // Check that the body field doesn't exist.
117 $this->drupalGet('admin/structure/comment/manage/comment/fields');
118 $this->assertFalse($this->cssSelect('tr#comment-body'), 'Body field does not exist.');
122 * Tests deleting a comment type that still has content.
124 public function testCommentTypeDeletion() {
125 // Create a comment type programmatically.
126 $type = $this->createCommentType('foo');
127 $this->drupalCreateContentType(['type' => 'page']);
128 $this->addDefaultCommentField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo');
129 $field_storage = FieldStorageConfig::loadByName('node', 'foo');
131 $this->drupalLogin($this->adminUser);
134 $node = Node::create([
140 // Add a new comment of this type.
141 $comment = Comment::create([
142 'comment_type' => 'foo',
143 'entity_type' => 'node',
144 'field_name' => 'foo',
145 'entity_id' => $node->id(),
149 // Attempt to delete the comment type, which should not be allowed.
150 $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
152 t('%label is used by 1 comment on your site. You can not remove this comment type until you have removed all of the %label comments.', ['%label' => $type->label()]),
153 'The comment type will not be deleted until all comments of that type are removed.'
156 t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', [
158 '%field' => 'node.foo',
160 'The comment type will not be deleted until all fields of that type are removed.'
162 $this->assertNoText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is not available.');
164 // Delete the comment and the field.
166 $field_storage->delete();
167 // Attempt to delete the comment type, which should now be allowed.
168 $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
170 t('Are you sure you want to delete the comment type %type?', ['%type' => $type->id()]),
171 'The comment type is available for deletion.'
173 $this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.');
175 // Test exception thrown when re-using an existing comment type.
177 $this->addDefaultCommentField('comment', 'comment', 'bar');
178 $this->fail('Exception not thrown.');
180 catch (\InvalidArgumentException $e) {
181 $this->pass('Exception thrown if attempting to re-use comment-type from another entity type.');
184 // Delete the comment type.
185 $this->drupalPostForm('admin/structure/comment/manage/' . $type->id() . '/delete', [], t('Delete'));
186 $this->assertNull(CommentType::load($type->id()), 'Comment type deleted.');
187 $this->assertRaw(t('The comment type %label has been deleted.', ['%label' => $type->label()]));