Pull merge.
[yaffs-website] / web / core / modules / comment / tests / src / Functional / CommentTypeTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional;
4
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;
11
12 /**
13  * Ensures that comment type functions work correctly.
14  *
15  * @group comment
16  */
17 class CommentTypeTest extends CommentTestBase {
18
19   /**
20    * Admin user.
21    *
22    * @var \Drupal\Core\Session\AccountInterface
23    */
24   protected $adminUser;
25
26   /**
27    * Permissions to grant admin user.
28    *
29    * @var array
30    */
31   protected $permissions = [
32     'administer comments',
33     'administer comment fields',
34     'administer comment types',
35   ];
36
37   /**
38    * Sets the test up.
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     $this->drupalPlaceBlock('page_title_block');
44
45     $this->adminUser = $this->drupalCreateUser($this->permissions);
46   }
47
48   /**
49    * Tests creating a comment type programmatically and via a form.
50    */
51   public function testCommentTypeCreation() {
52     // Create a comment type programmatically.
53     $type = $this->createCommentType('other');
54
55     $comment_type = CommentType::load('other');
56     $this->assertTrue($comment_type, 'The new comment type has been created.');
57
58     // Log in a test user.
59     $this->drupalLogin($this->adminUser);
60
61     $this->drupalGet('admin/structure/comment/manage/' . $type->id());
62     $this->assertResponse(200, 'The new comment type can be accessed at the edit form.');
63
64     // Create a comment type via the user interface.
65     $edit = [
66       'id' => 'foo',
67       'label' => 'title for foo',
68       'description' => '',
69       'target_entity_type_id' => 'node',
70     ];
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.');
74
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);
78
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');
89   }
90
91   /**
92    * Tests editing a comment type using the UI.
93    */
94   public function testCommentTypeEditing() {
95     $this->drupalLogin($this->adminUser);
96
97     $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
98     $this->assertEqual($field->getLabel(), 'Comment', 'Comment body field was found.');
99
100     // Change the comment type name.
101     $this->drupalGet('admin/structure/comment');
102     $edit = [
103       'label' => 'Bar',
104     ];
105     $this->drupalPostForm('admin/structure/comment/manage/comment', $edit, t('Save'));
106
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.');
112
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.');
120   }
121
122   /**
123    * Tests deleting a comment type that still has content.
124    */
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');
131
132     $this->drupalLogin($this->adminUser);
133
134     // Create a node.
135     $node = Node::create([
136       'type' => 'page',
137       'title' => 'foo',
138     ]);
139     $node->save();
140
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(),
147     ]);
148     $comment->save();
149
150     // Attempt to delete the comment type, which should not be allowed.
151     $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
152     $this->assertRaw(
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.'
155     );
156     $this->assertRaw(
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.', [
158         '%label' => 'foo',
159         '%field' => 'node.foo',
160       ]),
161       'The comment type will not be deleted until all fields of that type are removed.'
162     );
163     $this->assertNoText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is not available.');
164
165     // Delete the comment and the field.
166     $comment->delete();
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');
170     $this->assertRaw(
171       t('Are you sure you want to delete the comment type %type?', ['%type' => $type->id()]),
172       'The comment type is available for deletion.'
173     );
174     $this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.');
175
176     // Test exception thrown when re-using an existing comment type.
177     try {
178       $this->addDefaultCommentField('comment', 'comment', 'bar');
179       $this->fail('Exception not thrown.');
180     }
181     catch (\InvalidArgumentException $e) {
182       $this->pass('Exception thrown if attempting to re-use comment-type from another entity type.');
183     }
184
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()]));
189   }
190
191 }