PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/comment/src/Tests/CommentFieldsTest.php

https://gitlab.com/reasonat/test8
PHP | 235 lines | 130 code | 39 blank | 66 comment | 1 complexity | 18070afd863e34f941be38ec96e83461 MD5 | raw file
  1. <?php
  2. namespace Drupal\comment\Tests;
  3. use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
  4. use Drupal\field\Entity\FieldStorageConfig;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\comment\Entity\CommentType;
  7. /**
  8. * Tests fields on comments.
  9. *
  10. * @group comment
  11. */
  12. class CommentFieldsTest extends CommentTestBase {
  13. /**
  14. * Install the field UI.
  15. *
  16. * @var array
  17. */
  18. public static $modules = array('field_ui');
  19. /**
  20. * Tests that the default 'comment_body' field is correctly added.
  21. */
  22. function testCommentDefaultFields() {
  23. // Do not make assumptions on default node types created by the test
  24. // installation profile, and create our own.
  25. $this->drupalCreateContentType(array('type' => 'test_node_type'));
  26. $this->addDefaultCommentField('node', 'test_node_type');
  27. // Check that the 'comment_body' field is present on the comment bundle.
  28. $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
  29. $this->assertTrue(!empty($field), 'The comment_body field is added when a comment bundle is created');
  30. $field->delete();
  31. // Check that the 'comment_body' field is not deleted since it is persisted
  32. // even if it has no fields.
  33. $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
  34. $this->assertTrue($field_storage, 'The comment_body field storage was not deleted');
  35. // Create a new content type.
  36. $type_name = 'test_node_type_2';
  37. $this->drupalCreateContentType(array('type' => $type_name));
  38. $this->addDefaultCommentField('node', $type_name);
  39. // Check that the 'comment_body' field exists and has an instance on the
  40. // new comment bundle.
  41. $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
  42. $this->assertTrue($field_storage, 'The comment_body field exists');
  43. $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
  44. $this->assertTrue(isset($field), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
  45. // Test adding a field that defaults to CommentItemInterface::CLOSED.
  46. $this->addDefaultCommentField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
  47. $field = FieldConfig::load('node.test_node_type.who_likes_ponies');;
  48. $this->assertEqual($field->getDefaultValueLiteral()[0]['status'], CommentItemInterface::CLOSED);
  49. }
  50. /**
  51. * Tests that you can remove a comment field.
  52. */
  53. public function testCommentFieldDelete() {
  54. $this->drupalCreateContentType(array('type' => 'test_node_type'));
  55. $this->addDefaultCommentField('node', 'test_node_type');
  56. // We want to test the handling of removing the primary comment field, so we
  57. // ensure there is at least one other comment field attached to a node type
  58. // so that comment_entity_load() runs for nodes.
  59. $this->addDefaultCommentField('node', 'test_node_type', 'comment2');
  60. // Create a sample node.
  61. $node = $this->drupalCreateNode(array(
  62. 'title' => 'Baloney',
  63. 'type' => 'test_node_type',
  64. ));
  65. $this->drupalLogin($this->webUser);
  66. $this->drupalGet('node/' . $node->nid->value);
  67. $elements = $this->cssSelect('.field--type-comment');
  68. $this->assertEqual(2, count($elements), 'There are two comment fields on the node.');
  69. // Delete the first comment field.
  70. FieldStorageConfig::loadByName('node', 'comment')->delete();
  71. $this->drupalGet('node/' . $node->nid->value);
  72. $elements = $this->cssSelect('.field--type-comment');
  73. $this->assertEqual(1, count($elements), 'There is one comment field on the node.');
  74. }
  75. /**
  76. * Tests link building with non-default comment field names.
  77. */
  78. public function testCommentFieldLinksNonDefaultName() {
  79. $this->drupalCreateContentType(['type' => 'test_node_type']);
  80. $this->addDefaultCommentField('node', 'test_node_type', 'comment2');
  81. $web_user2 = $this->drupalCreateUser([
  82. 'access comments',
  83. 'post comments',
  84. 'create article content',
  85. 'edit own comments',
  86. 'skip comment approval',
  87. 'access content',
  88. ]);
  89. // Create a sample node.
  90. $node = $this->drupalCreateNode([
  91. 'title' => 'Baloney',
  92. 'type' => 'test_node_type',
  93. ]);
  94. // Go to the node first so that webuser2 see new comments.
  95. $this->drupalLogin($web_user2);
  96. $this->drupalGet($node->urlInfo());
  97. $this->drupalLogout();
  98. // Test that buildCommentedEntityLinks() does not break when the 'comment'
  99. // field does not exist. Requires at least one comment.
  100. $this->drupalLogin($this->webUser);
  101. $this->postComment($node, 'Here is a comment', '', NULL, 'comment2');
  102. $this->drupalLogout();
  103. $this->drupalLogin($web_user2);
  104. // We want to check the attached drupalSettings of
  105. // \Drupal\comment\CommentLinkBuilder::buildCommentedEntityLinks. Therefore
  106. // we need a node listing, let's use views for that.
  107. $this->container->get('module_installer')->install(['views'], TRUE);
  108. // We also need a router rebuild, as the router is lazily rebuild in the
  109. // module installer.
  110. \Drupal::service('router.builder')->rebuild();
  111. $this->drupalGet('node');
  112. $link_info = $this->getDrupalSettings()['comment']['newCommentsLinks']['node']['comment2']['2'];
  113. $this->assertIdentical($link_info['new_comment_count'], 1);
  114. $this->assertIdentical($link_info['first_new_comment_link'], $node->url('canonical', ['fragment' => 'new']));
  115. }
  116. /**
  117. * Tests creating a comment field through the interface.
  118. */
  119. public function testCommentFieldCreate() {
  120. // Create user who can administer user fields.
  121. $user = $this->drupalCreateUser(array(
  122. 'administer user fields',
  123. ));
  124. $this->drupalLogin($user);
  125. // Create comment field in account settings.
  126. $edit = array(
  127. 'new_storage_type' => 'comment',
  128. 'label' => 'User comment',
  129. 'field_name' => 'user_comment',
  130. );
  131. $this->drupalPostForm('admin/config/people/accounts/fields/add-field', $edit, 'Save and continue');
  132. // Try to save the comment field without selecting a comment type.
  133. $edit = array();
  134. $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
  135. // We should get an error message.
  136. $this->assertText(t('An illegal choice has been detected. Please contact the site administrator.'));
  137. // Create a comment type for users.
  138. $bundle = CommentType::create(array(
  139. 'id' => 'user_comment_type',
  140. 'label' => 'user_comment_type',
  141. 'description' => '',
  142. 'target_entity_type_id' => 'user',
  143. ));
  144. $bundle->save();
  145. // Select a comment type and try to save again.
  146. $edit = array(
  147. 'settings[comment_type]' => 'user_comment_type',
  148. );
  149. $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
  150. // We shouldn't get an error message.
  151. $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.'));
  152. }
  153. /**
  154. * Tests that comment module works when installed after a content module.
  155. */
  156. function testCommentInstallAfterContentModule() {
  157. // Create a user to do module administration.
  158. $this->adminUser = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
  159. $this->drupalLogin($this->adminUser);
  160. // Drop default comment field added in CommentTestBase::setup().
  161. FieldStorageConfig::loadByName('node', 'comment')->delete();
  162. if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
  163. $field_storage->delete();
  164. }
  165. // Purge field data now to allow comment module to be uninstalled once the
  166. // field has been deleted.
  167. field_purge_batch(10);
  168. // Uninstall the comment module.
  169. $edit = array();
  170. $edit['uninstall[comment]'] = TRUE;
  171. $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
  172. $this->drupalPostForm(NULL, array(), t('Uninstall'));
  173. $this->rebuildContainer();
  174. $this->assertFalse($this->container->get('module_handler')->moduleExists('comment'), 'Comment module uninstalled.');
  175. // Install core content type module (book).
  176. $edit = array();
  177. $edit['modules[Core][book][enable]'] = 'book';
  178. $this->drupalPostForm('admin/modules', $edit, t('Install'));
  179. // Now install the comment module.
  180. $edit = array();
  181. $edit['modules[Core][comment][enable]'] = 'comment';
  182. $this->drupalPostForm('admin/modules', $edit, t('Install'));
  183. $this->rebuildContainer();
  184. $this->assertTrue($this->container->get('module_handler')->moduleExists('comment'), 'Comment module enabled.');
  185. // Create nodes of each type.
  186. $this->addDefaultCommentField('node', 'book');
  187. $book_node = $this->drupalCreateNode(array('type' => 'book'));
  188. $this->drupalLogout();
  189. // Try to post a comment on each node. A failure will be triggered if the
  190. // comment body is missing on one of these forms, due to postComment()
  191. // asserting that the body is actually posted correctly.
  192. $this->webUser = $this->drupalCreateUser(array('access content', 'access comments', 'post comments', 'skip comment approval'));
  193. $this->drupalLogin($this->webUser);
  194. $this->postComment($book_node, $this->randomMachineName(), $this->randomMachineName());
  195. }
  196. }