3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Entity\CommentType;
7 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\node\Entity\Node;
13 * Ensures that comment type functions work correctly.
17 class CommentTypeTest extends CommentTestBase {
22 * @var \Drupal\Core\Session\AccountInterface
27 * Permissions to grant admin user.
31 protected $permissions = [
32 'administer comments',
33 'administer comment fields',
34 'administer comment types',
40 protected function setUp() {
43 $this->drupalPlaceBlock('page_title_block');
45 $this->adminUser = $this->drupalCreateUser($this->permissions);
49 * Tests creating a comment type programmatically and via a form.
51 public function testCommentTypeCreation() {
52 // Create a comment type programmatically.
53 $type = $this->createCommentType('other');
55 $comment_type = CommentType::load('other');
56 $this->assertTrue($comment_type, 'The new comment type has been created.');
58 // Log in a test user.
59 $this->drupalLogin($this->adminUser);
61 $this->drupalGet('admin/structure/comment/manage/' . $type->id());
62 $this->assertResponse(200, 'The new comment type can be accessed at the edit form.');
64 // Create a comment type via the user interface.
67 'label' => 'title for foo',
69 'target_entity_type_id' => 'node',
71 $this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save'));
72 $comment_type = CommentType::load('foo');
73 $this->assertTrue($comment_type, 'The new comment type has been created.');
75 // Check that the comment type was created in site default language.
76 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
77 $this->assertEqual($comment_type->language()->getId(), $default_langcode);
79 // Edit the comment-type and ensure that we cannot change the entity-type.
80 $this->drupalGet('admin/structure/comment/manage/foo');
81 $this->assertNoField('target_entity_type_id', 'Entity type file not present');
82 $this->assertText(t('Target entity type'));
83 // Save the form and ensure the entity-type value is preserved even though
84 // the field isn't present.
85 $this->drupalPostForm(NULL, [], t('Save'));
86 \Drupal::entityManager()->getStorage('comment_type')->resetCache(['foo']);
87 $comment_type = CommentType::load('foo');
88 $this->assertEqual($comment_type->getTargetEntityTypeId(), 'node');
92 * Tests editing a comment type using the UI.
94 public function testCommentTypeEditing() {
95 $this->drupalLogin($this->adminUser);
97 $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
98 $this->assertEqual($field->getLabel(), 'Comment', 'Comment body field was found.');
100 // Change the comment type name.
101 $this->drupalGet('admin/structure/comment');
105 $this->drupalPostForm('admin/structure/comment/manage/comment', $edit, t('Save'));
107 $this->drupalGet('admin/structure/comment');
108 $this->assertRaw('Bar', 'New name was displayed.');
109 $this->clickLink('Manage fields');
110 $this->assertUrl(\Drupal::url('entity.comment.field_ui_fields', ['comment_type' => 'comment'], ['absolute' => TRUE]), [], 'Original machine name was used in URL.');
111 $this->assertTrue($this->cssSelect('tr#comment-body'), 'Body field exists.');
113 // Remove the body field.
114 $this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete', [], t('Delete'));
115 // Resave the settings for this type.
116 $this->drupalPostForm('admin/structure/comment/manage/comment', [], t('Save'));
117 // Check that the body field doesn't exist.
118 $this->drupalGet('admin/structure/comment/manage/comment/fields');
119 $this->assertFalse($this->cssSelect('tr#comment-body'), 'Body field does not exist.');
123 * Tests deleting a comment type that still has content.
125 public function testCommentTypeDeletion() {
126 // Create a comment type programmatically.
127 $type = $this->createCommentType('foo');
128 $this->drupalCreateContentType(['type' => 'page']);
129 $this->addDefaultCommentField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo');
130 $field_storage = FieldStorageConfig::loadByName('node', 'foo');
132 $this->drupalLogin($this->adminUser);
135 $node = Node::create([
141 // Add a new comment of this type.
142 $comment = Comment::create([
143 'comment_type' => 'foo',
144 'entity_type' => 'node',
145 'field_name' => 'foo',
146 'entity_id' => $node->id(),
150 // Attempt to delete the comment type, which should not be allowed.
151 $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
153 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()]),
154 'The comment type will not be deleted until all comments of that type are removed.'
157 t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', [
159 '%field' => 'node.foo',
161 'The comment type will not be deleted until all fields of that type are removed.'
163 $this->assertNoText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is not available.');
165 // Delete the comment and the field.
167 $field_storage->delete();
168 // Attempt to delete the comment type, which should now be allowed.
169 $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
171 t('Are you sure you want to delete the comment type %type?', ['%type' => $type->id()]),
172 'The comment type is available for deletion.'
174 $this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.');
176 // Test exception thrown when re-using an existing comment type.
178 $this->addDefaultCommentField('comment', 'comment', 'bar');
179 $this->fail('Exception not thrown.');
181 catch (\InvalidArgumentException $e) {
182 $this->pass('Exception thrown if attempting to re-use comment-type from another entity type.');
185 // Delete the comment type.
186 $this->drupalPostForm('admin/structure/comment/manage/' . $type->id() . '/delete', [], t('Delete'));
187 $this->assertNull(CommentType::load($type->id()), 'Comment type deleted.');
188 $this->assertRaw(t('The comment type %label has been deleted.', ['%label' => $type->label()]));