Chris@0: drupalCreateContentType(['type' => 'test_node_type']); Chris@0: $this->addDefaultCommentField('node', 'test_node_type'); Chris@0: Chris@0: // Check that the 'comment_body' field is present on the comment bundle. Chris@0: $field = FieldConfig::loadByName('comment', 'comment', 'comment_body'); Chris@0: $this->assertTrue(!empty($field), 'The comment_body field is added when a comment bundle is created'); Chris@0: Chris@0: $field->delete(); Chris@0: Chris@0: // Check that the 'comment_body' field is not deleted since it is persisted Chris@0: // even if it has no fields. Chris@0: $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); Chris@0: $this->assertTrue($field_storage, 'The comment_body field storage was not deleted'); Chris@0: Chris@0: // Create a new content type. Chris@0: $type_name = 'test_node_type_2'; Chris@0: $this->drupalCreateContentType(['type' => $type_name]); Chris@0: $this->addDefaultCommentField('node', $type_name); Chris@0: Chris@0: // Check that the 'comment_body' field exists and has an instance on the Chris@0: // new comment bundle. Chris@0: $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); Chris@0: $this->assertTrue($field_storage, 'The comment_body field exists'); Chris@0: $field = FieldConfig::loadByName('comment', 'comment', 'comment_body'); Chris@0: $this->assertTrue(isset($field), format_string('The comment_body field is present for comments on type @type', ['@type' => $type_name])); Chris@0: Chris@0: // Test adding a field that defaults to CommentItemInterface::CLOSED. Chris@0: $this->addDefaultCommentField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies'); Chris@18: $field = FieldConfig::load('node.test_node_type.who_likes_ponies'); Chris@0: $this->assertEqual($field->getDefaultValueLiteral()[0]['status'], CommentItemInterface::CLOSED); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that you can remove a comment field. Chris@0: */ Chris@0: public function testCommentFieldDelete() { Chris@0: $this->drupalCreateContentType(['type' => 'test_node_type']); Chris@0: $this->addDefaultCommentField('node', 'test_node_type'); Chris@0: // We want to test the handling of removing the primary comment field, so we Chris@0: // ensure there is at least one other comment field attached to a node type Chris@0: // so that comment_entity_load() runs for nodes. Chris@0: $this->addDefaultCommentField('node', 'test_node_type', 'comment2'); Chris@0: Chris@0: // Create a sample node. Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'title' => 'Baloney', Chris@0: 'type' => 'test_node_type', Chris@0: ]); Chris@0: Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: $this->drupalGet('node/' . $node->nid->value); Chris@0: $elements = $this->cssSelect('.field--type-comment'); Chris@0: $this->assertEqual(2, count($elements), 'There are two comment fields on the node.'); Chris@0: Chris@0: // Delete the first comment field. Chris@0: FieldStorageConfig::loadByName('node', 'comment')->delete(); Chris@0: $this->drupalGet('node/' . $node->nid->value); Chris@0: $elements = $this->cssSelect('.field--type-comment'); Chris@0: $this->assertEqual(1, count($elements), 'There is one comment field on the node.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests link building with non-default comment field names. Chris@0: */ Chris@0: public function testCommentFieldLinksNonDefaultName() { Chris@0: $this->drupalCreateContentType(['type' => 'test_node_type']); Chris@0: $this->addDefaultCommentField('node', 'test_node_type', 'comment2'); Chris@0: Chris@0: $web_user2 = $this->drupalCreateUser([ Chris@0: 'access comments', Chris@0: 'post comments', Chris@0: 'create article content', Chris@0: 'edit own comments', Chris@0: 'skip comment approval', Chris@0: 'access content', Chris@0: ]); Chris@0: Chris@0: // Create a sample node. Chris@0: $node = $this->drupalCreateNode([ Chris@0: 'title' => 'Baloney', Chris@0: 'type' => 'test_node_type', Chris@0: ]); Chris@0: Chris@0: // Go to the node first so that webuser2 see new comments. Chris@0: $this->drupalLogin($web_user2); Chris@18: $this->drupalGet($node->toUrl()); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Test that buildCommentedEntityLinks() does not break when the 'comment' Chris@0: // field does not exist. Requires at least one comment. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->postComment($node, 'Here is a comment', '', NULL, 'comment2'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: $this->drupalLogin($web_user2); Chris@0: Chris@0: // We want to check the attached drupalSettings of Chris@0: // \Drupal\comment\CommentLinkBuilder::buildCommentedEntityLinks. Therefore Chris@0: // we need a node listing, let's use views for that. Chris@0: $this->container->get('module_installer')->install(['views'], TRUE); Chris@0: // We also need a router rebuild, as the router is lazily rebuild in the Chris@0: // module installer. Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: $this->drupalGet('node'); Chris@0: Chris@0: $link_info = $this->getDrupalSettings()['comment']['newCommentsLinks']['node']['comment2']['2']; Chris@0: $this->assertIdentical($link_info['new_comment_count'], 1); Chris@18: $this->assertIdentical($link_info['first_new_comment_link'], $node->toUrl('canonical', ['fragment' => 'new'])->toString()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests creating a comment field through the interface. Chris@0: */ Chris@0: public function testCommentFieldCreate() { Chris@0: // Create user who can administer user fields. Chris@0: $user = $this->drupalCreateUser([ Chris@0: 'administer user fields', Chris@0: ]); Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: // Create comment field in account settings. Chris@0: $edit = [ Chris@0: 'new_storage_type' => 'comment', Chris@0: 'label' => 'User comment', Chris@0: 'field_name' => 'user_comment', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/people/accounts/fields/add-field', $edit, 'Save and continue'); Chris@0: Chris@0: // Try to save the comment field without selecting a comment type. Chris@0: $edit = []; Chris@0: $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings')); Chris@0: // We should get an error message. Chris@0: $this->assertText(t('An illegal choice has been detected. Please contact the site administrator.')); Chris@0: Chris@0: // Create a comment type for users. Chris@0: $bundle = CommentType::create([ Chris@0: 'id' => 'user_comment_type', Chris@0: 'label' => 'user_comment_type', Chris@0: 'description' => '', Chris@0: 'target_entity_type_id' => 'user', Chris@0: ]); Chris@0: $bundle->save(); Chris@0: Chris@0: // Select a comment type and try to save again. Chris@0: $edit = [ Chris@0: 'settings[comment_type]' => 'user_comment_type', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings')); Chris@0: // We shouldn't get an error message. Chris@0: $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that comment module works when installed after a content module. Chris@0: */ Chris@0: public function testCommentInstallAfterContentModule() { Chris@0: // Create a user to do module administration. Chris@0: $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer modules']); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Drop default comment field added in CommentTestBase::setup(). Chris@0: FieldStorageConfig::loadByName('node', 'comment')->delete(); Chris@0: if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) { Chris@0: $field_storage->delete(); Chris@0: } Chris@0: Chris@0: // Purge field data now to allow comment module to be uninstalled once the Chris@0: // field has been deleted. Chris@0: field_purge_batch(10); Chris@0: Chris@0: // Uninstall the comment module. Chris@0: $edit = []; Chris@0: $edit['uninstall[comment]'] = TRUE; Chris@0: $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall')); Chris@0: $this->drupalPostForm(NULL, [], t('Uninstall')); Chris@0: $this->rebuildContainer(); Chris@0: $this->assertFalse($this->container->get('module_handler')->moduleExists('comment'), 'Comment module uninstalled.'); Chris@0: Chris@0: // Install core content type module (book). Chris@0: $edit = []; Chris@0: $edit['modules[book][enable]'] = 'book'; Chris@0: $this->drupalPostForm('admin/modules', $edit, t('Install')); Chris@0: Chris@0: // Now install the comment module. Chris@0: $edit = []; Chris@0: $edit['modules[comment][enable]'] = 'comment'; Chris@0: $this->drupalPostForm('admin/modules', $edit, t('Install')); Chris@0: $this->rebuildContainer(); Chris@0: $this->assertTrue($this->container->get('module_handler')->moduleExists('comment'), 'Comment module enabled.'); Chris@0: Chris@0: // Create nodes of each type. Chris@0: $this->addDefaultCommentField('node', 'book'); Chris@0: $book_node = $this->drupalCreateNode(['type' => 'book']); Chris@0: Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Try to post a comment on each node. A failure will be triggered if the Chris@0: // comment body is missing on one of these forms, due to postComment() Chris@0: // asserting that the body is actually posted correctly. Chris@0: $this->webUser = $this->drupalCreateUser(['access content', 'access comments', 'post comments', 'skip comment approval']); Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->postComment($book_node, $this->randomMachineName(), $this->randomMachineName()); Chris@0: } Chris@0: Chris@0: }