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