protected function setUp() { parent::setUp(); $this->drupalPlaceBlock('page_title_block'); // Create Basic page node type. $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); $vocabulary = Vocabulary::create(['name' => $this->randomMachineName(), 'description' => $this->randomMachineName(), 'vid' => Unicode::strtolower($this->randomMachineName()), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'help' => '', 'nodes' => array('page' => 'page'), 'weight' => mt_rand(0, 10)]); $vocabulary->save(); // Create a field. $field_name = Unicode::strtolower($this->randomMachineName()); $handler_settings = array('target_bundles' => array($vocabulary->id() => $vocabulary->id()), 'auto_create' => TRUE); $this->createEntityReferenceField('node', 'page', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); // Create a time in the past for the archive. $time = REQUEST_TIME - 3600; $this->addDefaultCommentField('node', 'page'); for ($i = 0; $i <= 10; $i++) { $user = $this->drupalCreateUser(); $term = $this->createTerm($vocabulary); $values = array('created' => $time, 'type' => 'page'); $values[$field_name][]['target_id'] = $term->id(); // Make every other node promoted. if ($i % 2) { $values['promote'] = TRUE; } $values['body'][]['value'] = \Drupal::l('Node ' . 1, new Url('entity.node.canonical', ['node' => 1])); $node = $this->drupalCreateNode($values); $comment = array('uid' => $user->id(), 'status' => CommentInterface::PUBLISHED, 'entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment'); Comment::create($comment)->save(); } // Some views, such as the "Who's Online" view, only return results if at // least one user is logged in. $account = $this->drupalCreateUser(array()); $this->drupalLogin($account); }
protected function setUp() { parent::setUp(); // Create a new content type $content_type = $this->drupalCreateContentType(); // Add a node of the new content type. $node_data = array('type' => $content_type->id()); $this->addDefaultCommentField('node', $content_type->id()); $this->node = $this->drupalCreateNode($node_data); // Force a flush of the in-memory storage. $this->container->get('views.views_data')->clear(); // Create some comments and attach them to the created node. for ($i = 0; $i < $this->masterDisplayResults; $i++) { /** @var \Drupal\comment\CommentInterface $comment */ $comment = Comment::create(array('status' => CommentInterface::PUBLISHED, 'field_name' => 'comment', 'entity_type' => 'node', 'entity_id' => $this->node->id())); $comment->setOwnerId(0); $comment->setSubject('Test comment ' . $i); $comment->comment_body->value = 'Test body ' . $i; $comment->comment_body->format = 'full_html'; // Ensure comments are sorted in ascending order. $time = REQUEST_TIME + ($this->masterDisplayResults - $i); $comment->setCreatedTime($time); $comment->changed->value = $time; $comment->save(); } // Store all the nodes just created to access their properties on the tests. $this->commentsCreated = Comment::loadMultiple(); // Sort created comments in descending order. ksort($this->commentsCreated, SORT_NUMERIC); }
/** * {@inheritdoc} */ protected function setUp($import_test_views = TRUE) { parent::setUp($import_test_views); $this->installEntitySchema('user'); $this->installEntitySchema('comment'); // Create the anonymous role. $this->installConfig(['user']); // Create an anonymous user. $storage = \Drupal::entityManager()->getStorage('user'); // Insert a row for the anonymous user. $storage->create(array('uid' => 0, 'name' => '', 'status' => 0))->save(); $admin_role = Role::create(['id' => 'admin', 'permissions' => ['administer comments', 'access user profiles']]); $admin_role->save(); /* @var \Drupal\user\RoleInterface $anonymous_role */ $anonymous_role = Role::load(Role::ANONYMOUS_ID); $anonymous_role->grantPermission('access comments'); $anonymous_role->save(); $this->adminUser = User::create(['name' => $this->randomMachineName(), 'roles' => [$admin_role->id()]]); $this->adminUser->save(); // Create some comments. $comment = Comment::create(['subject' => 'My comment title', 'uid' => $this->adminUser->id(), 'name' => $this->adminUser->label(), 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'status' => 1]); $comment->save(); $comment_anonymous = Comment::create(['subject' => 'Anonymous comment title', 'uid' => 0, 'name' => 'barry', 'mail' => '*****@*****.**', 'homepage' => 'https://example.com', 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'created' => 123456, 'status' => 1]); $comment_anonymous->save(); }
/** * Performs setup tasks before each individual test method is run. */ public function setUp() { parent::setUp('content_access'); // The parent method already installs most needed node and comment schemas, // but here we also need the comment statistics. $this->installSchema('comment', array('comment_entity_statistics')); // Create a node type for testing. $type = NodeType::create(array('type' => 'page', 'name' => 'page')); $type->save(); // Create anonymous user role. $role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous')); $role->save(); // Insert the anonymous user into the database, as the user table is inner // joined by \Drupal\comment\CommentStorage. User::create(array('uid' => 0, 'name' => ''))->save(); // Create a node with attached comment. $this->nodes[0] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'test title')); $this->nodes[0]->save(); $comment_type = CommentType::create(array('id' => 'comment', 'target_entity_type_id' => 'node')); $comment_type->save(); $this->installConfig(array('comment')); $this->addDefaultCommentField('node', 'page'); $comment = Comment::create(array('entity_type' => 'node', 'entity_id' => $this->nodes[0]->id(), 'field_name' => 'comment', 'body' => 'test body', 'comment_type' => $comment_type->id())); $comment->save(); $this->comments[] = $comment; $this->nodes[1] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'some title')); $this->nodes[1]->save(); $this->nodes[2] = Node::create(array('status' => NODE_NOT_PUBLISHED, 'type' => 'page', 'title' => 'other title')); $this->nodes[2]->save(); // Also index users, to verify that they are unaffected by the processor. $this->index->set('datasources', array('entity:comment', 'entity:node', 'entity:user')); $this->index->save(); $this->index = entity_load('search_api_index', $this->index->id(), TRUE); }
/** * Tests the node comment statistics. */ function testCommentNodeCommentStatistics() { $node_storage = $this->container->get('entity.manager')->getStorage('node'); // Set comments to have subject and preview disabled. $this->drupalLogin($this->adminUser); $this->setCommentPreview(DRUPAL_DISABLED); $this->setCommentForm(TRUE); $this->setCommentSubject(FALSE); $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.'); $this->drupalLogout(); // Checks the initial values of node comment statistics with no comment. $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->last_comment_timestamp, $this->node->getCreatedTime(), 'The initial value of node last_comment_timestamp is the node created date.'); $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.'); $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser->id(), 'The initial value of node last_comment_uid is the node uid.'); $this->assertEqual($node->get('comment')->comment_count, 0, 'The initial value of node comment_count is zero.'); // Post comment #1 as web_user2. $this->drupalLogin($this->webUser2); $comment_text = $this->randomMachineName(); $this->postComment($this->node, $comment_text); // Checks the new values of node comment statistics with comment #1. // The node cache needs to be reset before reload. $node_storage->resetCache(array($this->node->id())); $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is NULL.'); $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is the comment #1 uid.'); $this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is 1.'); // Prepare for anonymous comment submission (comment approval enabled). $this->drupalLogin($this->adminUser); user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => TRUE, 'post comments' => TRUE, 'skip comment approval' => FALSE)); // Ensure that the poster can leave some contact info. $this->setCommentAnonymous('1'); $this->drupalLogout(); // Post comment #2 as anonymous (comment approval enabled). $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', TRUE); // Checks the new values of node comment statistics with comment #2 and // ensure they haven't changed since the comment has not been moderated. // The node needs to be reloaded with the cache reset. $node_storage->resetCache(array($this->node->id())); $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.'); $this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is still the comment #1 uid.'); $this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is still 1.'); // Prepare for anonymous comment submission (no approval required). $this->drupalLogin($this->adminUser); user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => TRUE, 'post comments' => TRUE, 'skip comment approval' => TRUE)); $this->drupalLogout(); // Post comment #3 as anonymous. $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', array('name' => $this->randomMachineName())); $comment_loaded = Comment::load($anonymous_comment->id()); // Checks the new values of node comment statistics with comment #3. // The node needs to be reloaded with the cache reset. $node_storage->resetCache(array($this->node->id())); $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->last_comment_name, $comment_loaded->getAuthorName(), 'The value of node last_comment_name is the name of the anonymous user.'); $this->assertEqual($node->get('comment')->last_comment_uid, 0, 'The value of node last_comment_uid is zero.'); $this->assertEqual($node->get('comment')->comment_count, 2, 'The value of node comment_count is 2.'); }
/** * Ensure comment form works with history and big_pipe modules. * * @see https://www.drupal.org/node/2698811 */ public function testCommentForm_2698811() { $this->assertTrue($this->container->get('module_installer')->install(['comment', 'history', 'ckeditor'], TRUE), 'Installed modules.'); // Ensure an `article` node type exists. $this->createContentType(['type' => 'article']); $this->addDefaultCommentField('node', 'article'); // Enable CKEditor. $format = $this->randomMachineName(); FilterFormat::create(['format' => $format, 'name' => $this->randomString(), 'weight' => 1, 'filters' => []])->save(); $settings['toolbar']['rows'] = [[['name' => 'Links', 'items' => ['DrupalLink', 'DrupalUnlink']]]]; $editor = Editor::create(['format' => $format, 'editor' => 'ckeditor']); $editor->setSettings($settings); $editor->save(); $admin_user = $this->drupalCreateUser(['access comments', 'post comments', 'use text format ' . $format]); $this->drupalLogin($admin_user); $node = $this->createNode(['type' => 'article', 'comment' => CommentItemInterface::OPEN]); // Create some comments. foreach (range(1, 5) as $i) { $comment = Comment::create(['status' => CommentInterface::PUBLISHED, 'field_name' => 'comment', 'entity_type' => 'node', 'entity_id' => $node->id()]); $comment->save(); } $this->drupalGet($node->toUrl()->toString()); // Confirm that CKEditor loaded. $javascript = <<<JS (function(){ return Object.keys(CKEDITOR.instances).length > 0; }()); JS; $this->assertJsCondition($javascript); }
/** * Tests the getEntity method. */ public function testGetEntity() { // The view is a view of comments, their nodes and their authors, so there // are three layers of entities. $account = User::create(['name' => $this->randomMachineName(), 'bundle' => 'user']); $account->save(); $node = Node::create(['uid' => $account->id(), 'type' => 'page', 'title' => $this->randomString()]); $node->save(); $comment = Comment::create(array('uid' => $account->id(), 'entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment')); $comment->save(); $user = $this->drupalCreateUser(['access comments']); $this->drupalLogin($user); $view = Views::getView('test_field_get_entity'); $this->executeView($view); $row = $view->result[0]; // Tests entities on the base level. $entity = $view->field['cid']->getEntity($row); $this->assertEqual($entity->id(), $comment->id(), 'Make sure the right comment entity got loaded.'); // Tests entities as relationship on first level. $entity = $view->field['nid']->getEntity($row); $this->assertEqual($entity->id(), $node->id(), 'Make sure the right node entity got loaded.'); // Tests entities as relationships on second level. $entity = $view->field['uid']->getEntity($row); $this->assertEqual($entity->id(), $account->id(), 'Make sure the right user entity got loaded.'); }
function setUp() { parent::setUp(); $this->drupalLogin($this->drupalCreateUser(['access comments'])); // Add two new languages. ConfigurableLanguage::createFromLangcode('fr')->save(); ConfigurableLanguage::createFromLangcode('es')->save(); // Set up comment titles. $this->commentTitles = array('en' => 'Food in Paris', 'es' => 'Comida en Paris', 'fr' => 'Nouriture en Paris'); // Create a new comment. Using the one created earlier will not work, // as it predates the language set-up. $comment = array('uid' => $this->loggedInUser->id(), 'entity_id' => $this->nodeUserCommented->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'cid' => '', 'pid' => '', 'node_type' => ''); $this->comment = Comment::create($comment); // Add field values and translate the comment. $this->comment->subject->value = $this->commentTitles['en']; $this->comment->comment_body->value = $this->commentTitles['en']; $this->comment->langcode = 'en'; $this->comment->save(); foreach (array('es', 'fr') as $langcode) { $translation = $this->comment->addTranslation($langcode, array()); $translation->comment_body->value = $this->commentTitles[$langcode]; $translation->subject->value = $this->commentTitles[$langcode]; } $this->comment->save(); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->addDefaultCommentField('node', 'page', $this->fieldName); $this->customComment = Comment::create(['entity_id' => $this->nodeUserCommented->id(), 'entity_type' => 'node', 'field_name' => $this->fieldName]); $this->customComment->save(); }
protected function setUp() { parent::setUp(); ViewTestData::createTestViews(get_class($this), array('comment_test_views')); // Add two users, create a node with the user1 as author and another node // with user2 as author. For the second node add a comment from user1. $this->account = $this->drupalCreateUser(array('skip comment approval')); $this->account2 = $this->drupalCreateUser(); $this->drupalLogin($this->account); $this->drupalCreateContentType(array('type' => 'page', 'name' => t('Basic page'))); $this->container->get('comment.manager')->addDefaultField('node', 'page'); $this->node_user_posted = $this->drupalCreateNode(); $this->node_user_commented = $this->drupalCreateNode(array('uid' => $this->account2->id())); $comment = array('uid' => $this->loggedInUser->id(), 'entity_id' => $this->node_user_commented->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'cid' => '', 'pid' => '', 'node_type' => ''); $this->comment = entity_create('comment', $comment); $this->comment->save(); }
protected function setUp() { parent::setUp(); ViewTestData::createTestViews(get_class($this), array('comment_test_views')); // Add two users, create a node with the user1 as author and another node // with user2 as author. For the second node add a comment from user1. $this->account = $this->drupalCreateUser(array('skip comment approval')); $this->account2 = $this->drupalCreateUser(); $this->drupalLogin($this->account); $this->drupalCreateContentType(array('type' => 'page', 'name' => t('Basic page'))); $this->addDefaultCommentField('node', 'page'); $this->nodeUserPosted = $this->drupalCreateNode(); $this->nodeUserCommented = $this->drupalCreateNode(array('uid' => $this->account2->id())); $comment = array('uid' => $this->loggedInUser->id(), 'entity_id' => $this->nodeUserCommented->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'subject' => 'How much wood would a woodchuck chuck', 'cid' => '', 'pid' => '', 'mail' => '*****@*****.**'); $this->comment = Comment::create($comment); $this->comment->save(); }
/** * Tests if forum module uninstallation properly deletes the field. */ public function testForumUninstallWithField() { $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'administer nodes', 'administer modules', 'delete any forum content', 'administer content types'])); // Ensure that the field exists before uninstallation. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.'); // Create a taxonomy term. $term = Term::create(['name' => t('A term'), 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(), 'description' => '', 'parent' => array(0), 'vid' => 'forums', 'forum_container' => 0]); $term->save(); // Create a forum node. $node = $this->drupalCreateNode(array('title' => 'A forum post', 'type' => 'forum', 'taxonomy_forums' => array(array('target_id' => $term->id())))); // Create at least one comment against the forum node. $comment = Comment::create(array('entity_id' => $node->nid->value, 'entity_type' => 'node', 'field_name' => 'comment_forum', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomMachineName(), 'hostname' => '127.0.0.1')); $comment->save(); // Attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is required. $this->assertNoFieldByName('uninstall[forum]'); $this->assertText('To uninstall Forum, first delete all Forum content'); // Delete the node. $this->drupalPostForm('node/' . $node->id() . '/delete', array(), t('Delete')); // Attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is still required. $this->assertNoFieldByName('uninstall[forum]'); $this->assertText('To uninstall Forum, first delete all Forums terms'); // Delete any forum terms. $vid = $this->config('forum.settings')->get('vocabulary'); $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]); foreach ($terms as $term) { $term->delete(); } // Ensure that the forum node type can not be deleted. $this->drupalGet('admin/structure/types/manage/forum'); $this->assertNoLink(t('Delete')); // Now attempt to uninstall forum. $this->drupalGet('admin/modules/uninstall'); // Assert forum is no longer required. $this->assertFieldByName('uninstall[forum]'); $this->drupalPostForm('admin/modules/uninstall', array('uninstall[forum]' => 1), t('Uninstall')); $this->drupalPostForm(NULL, [], t('Uninstall')); // Check that the field is now deleted. $field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums'); $this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.'); // Check that a node type with a machine name of forum can be created after // uninstalling the forum module and the node type is not locked. $edit = array('name' => 'Forum', 'title_label' => 'title for forum', 'type' => 'forum'); $this->drupalPostForm('admin/structure/types/add', $edit, t('Save content type')); $this->assertTrue((bool) NodeType::load('forum'), 'Node type with machine forum created.'); $this->drupalGet('admin/structure/types/manage/forum'); $this->clickLink(t('Delete')); $this->drupalPostForm(NULL, array(), t('Delete')); $this->assertResponse(200); $this->assertFalse((bool) NodeType::load('forum'), 'Node type with machine forum deleted.'); // Double check everything by reinstalling the forum module again. $this->drupalPostForm('admin/modules', ['modules[Core][forum][enable]' => 1], 'Install'); $this->assertText('Module Forum has been enabled.'); }
protected function setUp() { parent::setUp(); // Add another anonymous comment. $comment = array('uid' => 0, 'entity_id' => $this->nodeUserCommented->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'subject' => 'A lot, apparently', 'cid' => '', 'pid' => $this->comment->id(), 'mail' => '*****@*****.**', 'name' => 'bobby tables', 'hostname' => 'public.example.com'); $this->comment = Comment::create($comment); $this->comment->save(); $user = $this->drupalCreateUser(['access comments']); $this->drupalLogin($user); }
protected function setUp() { parent::setUp(); ViewTestData::createTestViews(get_class($this), array('tracker_test_views')); $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); // Add a comment field. $this->addDefaultCommentField('node', 'page'); $permissions = array('access comments', 'create page content', 'post comments', 'skip comment approval'); $account = $this->drupalCreateUser($permissions); $this->drupalLogin($account); $this->node = $this->drupalCreateNode(array('title' => $this->randomMachineName(8), 'uid' => $account->id(), 'status' => 1)); $this->comment = Comment::create(array('entity_id' => $this->node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'subject' => $this->randomMachineName(), 'comment_body[' . LanguageInterface::LANGCODE_NOT_SPECIFIED . '][0][value]' => $this->randomMachineName(20))); }
function testCommentUserUIDTest() { // Add an additional comment which is not created by the user. $new_user = User::create(['name' => 'new user']); $new_user->save(); $comment = Comment::create(['uid' => $new_user->uid->value, 'entity_id' => $this->nodeUserCommented->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'subject' => 'if a woodchuck could chuck wood.']); $comment->save(); $view = Views::getView('test_comment_user_uid'); $this->executeView($view, array($this->account->id())); $result_set = array(array('nid' => $this->nodeUserPosted->id()), array('nid' => $this->nodeUserCommented->id())); $column_map = array('nid' => 'nid'); $this->assertIdenticalResultset($view, $result_set, $column_map); }
/** * Tests the unpublish comment by keyword action. */ function testCommentUnpublishByKeyword() { $this->drupalLogin($this->adminUser); $keyword_1 = $this->randomMachineName(); $keyword_2 = $this->randomMachineName(); $action = entity_create('action', array('id' => 'comment_unpublish_by_keyword_action', 'label' => $this->randomMachineName(), 'type' => 'comment', 'configuration' => array('keywords' => array($keyword_1, $keyword_2)), 'plugin' => 'comment_unpublish_by_keyword_action')); $action->save(); $comment = $this->postComment($this->node, $keyword_2, $this->randomMachineName()); // Load the full comment so that status is available. $comment = Comment::load($comment->id()); $this->assertTrue($comment->isPublished() === TRUE, 'The comment status was set to published.'); $action->execute(array($comment)); $this->assertTrue($comment->isPublished() === FALSE, 'The comment status was set to not published.'); }
/** * Tests new comment marker. */ public function testCommentNewCommentsIndicator() { // Test if the right links are displayed when no comment is present for the // node. $this->drupalLogin($this->adminUser); $this->drupalGet('node'); $this->assertNoLink(t('@count comments', array('@count' => 0))); $this->assertLink(t('Read more')); // Verify the data-history-node-last-comment-timestamp attribute, which is // used by the drupal.node-new-comments-link library to determine whether // a "x new comments" link might be necessary or not. We do this in // JavaScript to prevent breaking the render cache. $this->assertIdentical(0, count($this->xpath('//*[@data-history-node-last-comment-timestamp]')), 'data-history-node-last-comment-timestamp attribute is not set.'); // Create a new comment. This helper function may be run with different // comment settings so use $comment->save() to avoid complex setup. /** @var \Drupal\comment\CommentInterface $comment */ $comment = Comment::create(array('cid' => NULL, 'entity_id' => $this->node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'pid' => 0, 'uid' => $this->loggedInUser->id(), 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomMachineName(), 'hostname' => '127.0.0.1', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'comment_body' => array(LanguageInterface::LANGCODE_NOT_SPECIFIED => array($this->randomMachineName())))); $comment->save(); $this->drupalLogout(); // Log in with 'web user' and check comment links. $this->drupalLogin($this->webUser); $this->drupalGet('node'); // Verify the data-history-node-last-comment-timestamp attribute. Given its // value, the drupal.node-new-comments-link library would determine that the // node received a comment after the user last viewed it, and hence it would // perform an HTTP request to render the "new comments" node link. $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-last-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-history-node-last-comment-timestamp attribute is set to the correct value.'); $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-field-name="comment"]')), 'data-history-node-field-name attribute is set to the correct value.'); // The data will be pre-seeded on this particular page in drupalSettings, to // avoid the need for the client to make a separate request to the server. $settings = $this->getDrupalSettings(); $this->assertEqual($settings['history'], ['lastReadTimestamps' => [1 => 0]]); $this->assertEqual($settings['comment'], ['newCommentsLinks' => ['node' => ['comment' => [1 => ['new_comment_count' => 1, 'first_new_comment_link' => Url::fromRoute('entity.node.canonical', ['node' => 1])->setOptions(['fragment' => 'new'])->toString()]]]]]); // Pretend the data was not present in drupalSettings, i.e. test the // separate request to the server. $response = $this->renderNewCommentsNodeLinks(array($this->node->id())); $this->assertResponse(200); $json = Json::decode($response); $expected = array($this->node->id() => array('new_comment_count' => 1, 'first_new_comment_link' => $this->node->url('canonical', array('fragment' => 'new')))); $this->assertIdentical($expected, $json); // Failing to specify node IDs for the endpoint should return a 404. $this->renderNewCommentsNodeLinks(array()); $this->assertResponse(404); // Accessing the endpoint as the anonymous user should return a 403. $this->drupalLogout(); $this->renderNewCommentsNodeLinks(array($this->node->id())); $this->assertResponse(403); $this->renderNewCommentsNodeLinks(array()); $this->assertResponse(403); }
/** * Tests that comment links are output and can be hidden. */ public function testCommentLinks() { // Bartik theme alters comment links, so use a different theme. \Drupal::service('theme_handler')->install(array('stark')); $this->config('system.theme')->set('default', 'stark')->save(); // Remove additional user permissions from $this->webUser added by setUp(), // since this test is limited to anonymous and authenticated roles only. $roles = $this->webUser->getRoles(); entity_delete_multiple('user_role', array(reset($roles))); // Create a comment via CRUD API functionality, since // $this->postComment() relies on actual user permissions. $comment = Comment::create(array('cid' => NULL, 'entity_id' => $this->node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomMachineName(), 'hostname' => '127.0.0.1', 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, 'comment_body' => array(LanguageInterface::LANGCODE_NOT_SPECIFIED => array($this->randomMachineName())))); $comment->save(); $this->comment = $comment; // Change comment settings. $this->setCommentSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location'); $this->setCommentAnonymous(TRUE); $this->node->comment = CommentItemInterface::OPEN; $this->node->save(); // Change user permissions. $perms = array('access comments' => 1, 'post comments' => 1, 'skip comment approval' => 1, 'edit own comments' => 1); user_role_change_permissions(RoleInterface::ANONYMOUS_ID, $perms); $nid = $this->node->id(); // Assert basic link is output, actual functionality is unit-tested in // \Drupal\comment\Tests\CommentLinkBuilderTest. foreach (array('node', "node/{$nid}") as $path) { $this->drupalGet($path); // In teaser view, a link containing the comment count is always // expected. if ($path == 'node') { $this->assertLink(t('1 comment')); } $this->assertLink('Add new comment'); } // Make sure we can hide node links. entity_get_display('node', $this->node->bundle(), 'default')->removeComponent('links')->save(); $this->drupalGet($this->node->urlInfo()); $this->assertNoLink('1 comment'); $this->assertNoLink('Add new comment'); // Visit the full node, make sure there are links for the comment. $this->drupalGet('node/' . $this->node->id()); $this->assertText($comment->getSubject()); $this->assertLink('Reply'); // Make sure we can hide comment links. entity_get_display('comment', 'comment', 'default')->removeComponent('links')->save(); $this->drupalGet('node/' . $this->node->id()); $this->assertText($comment->getSubject()); $this->assertNoLink('Reply'); }
/** * Tests that comments are deleted with the node. */ function testNodeDeletion() { $this->drupalLogin($this->webUser); $comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); $this->assertTrue($comment->id(), 'The comment could be loaded.'); $this->node->delete(); $this->assertFalse(Comment::load($comment->id()), 'The comment could not be loaded after the node was deleted.'); // Make sure the comment field storage and all its fields are deleted when // the node type is deleted. $this->assertNotNull(FieldStorageConfig::load('node.comment'), 'Comment field storage exists'); $this->assertNotNull(FieldConfig::load('node.article.comment'), 'Comment field exists'); // Delete the node type. entity_delete_multiple('node_type', array($this->node->bundle())); $this->assertNull(FieldStorageConfig::load('node.comment'), 'Comment field storage deleted'); $this->assertNull(FieldConfig::load('node.article.comment'), 'Comment field deleted'); }
/** * Tests that comments are deleted with the node. */ function testNodeDeletion() { $this->drupalLogin($this->web_user); $comment = $this->postComment($this->node, $this->randomName(), $this->randomName()); $this->assertTrue($comment->id(), 'The comment could be loaded.'); $this->node->delete(); $this->assertFalse(Comment::load($comment->id()), 'The comment could not be loaded after the node was deleted.'); // Make sure the comment field and all its instances are deleted when node // type is deleted. $this->assertNotNull(entity_load('field_storage_config', 'node.comment'), 'Comment field exists'); $this->assertNotNull(entity_load('field_instance_config', 'node.article.comment'), 'Comment instance exists'); // Delete the node type. entity_delete_multiple('node_type', array($this->node->bundle())); $this->assertNull(entity_load('field_storage_config', 'node.comment'), 'Comment field deleted'); $this->assertNull(entity_load('field_instance_config', 'node.article.comment'), 'Comment instance deleted'); }
/** * Tests migration of comments from Drupal 7. */ public function testCommentMigration() { $comment = Comment::load(1); $this->assertTrue($comment instanceof CommentInterface); /** @var \Drupal\comment\CommentInterface $comment */ $this->assertIdentical('A comment', $comment->getSubject()); $this->assertIdentical('1421727536', $comment->getCreatedTime()); $this->assertIdentical('1421727536', $comment->getChangedTime()); $this->assertTrue($comment->getStatus()); $this->assertIdentical('admin', $comment->getAuthorName()); $this->assertIdentical('*****@*****.**', $comment->getAuthorEmail()); $this->assertIdentical('This is a comment', $comment->comment_body->value); $this->assertIdentical('filtered_html', $comment->comment_body->format); $node = $comment->getCommentedEntity(); $this->assertTrue($node instanceof NodeInterface); $this->assertIdentical('1', $node->id()); }
function testCommentTokens() { $node = Node::create(['type' => 'page', 'title' => $this->randomMachineName()]); $node->save(); $parent_comment = Comment::create(['entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'name' => 'anonymous user', 'mail' => '*****@*****.**', 'subject' => $this->randomMachineName(), 'body' => $this->randomMachineName()]); $parent_comment->save(); // Fix http://example.com/index.php/comment/1 fails 'url:path' test. $parent_comment_path = $parent_comment->url(); $tokens = array('url' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->setAbsolute()->toString(), 'url:absolute' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->setAbsolute()->toString(), 'url:relative' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->toString(), 'url:path' => $parent_comment_path, 'parent:url:absolute' => NULL); $this->assertTokens('comment', array('comment' => $parent_comment), $tokens); $comment = Comment::create(['entity_id' => $node->id(), 'pid' => $parent_comment->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'uid' => 1, 'name' => 'anonymous user', 'mail' => '*****@*****.**', 'subject' => $this->randomMachineName(), 'body' => $this->randomMachineName()]); $comment->save(); // Fix http://example.com/index.php/comment/1 fails 'url:path' test. $comment_path = \Drupal::url('entity.comment.canonical', array('comment' => $comment->id())); $tokens = array('url' => $comment->urlInfo('canonical', ['fragment' => "comment-{$comment->id()}"])->setAbsolute()->toString(), 'url:absolute' => $comment->urlInfo('canonical', ['fragment' => "comment-{$comment->id()}"])->setAbsolute()->toString(), 'url:relative' => $comment->urlInfo('canonical', ['fragment' => "comment-{$comment->id()}"])->toString(), 'url:path' => $comment_path, 'parent:url:absolute' => $parent_comment->urlInfo('canonical', ['fragment' => "comment-{$parent_comment->id()}"])->setAbsolute()->toString()); $this->assertTokens('comment', array('comment' => $comment), $tokens); }
function testCommentUserUIDTest() { $view = Views::getView('test_comment_user_uid'); $view->setDisplay(); $view->removeHandler('default', 'argument', 'uid_touch'); // Add an additional comment which is not created by the user. $new_user = User::create(['name' => 'new user']); $new_user->save(); $comment = Comment::create(['uid' => $new_user->uid->value, 'entity_id' => $this->nodeUserCommented->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'subject' => 'if a woodchuck could chuck wood.']); $comment->save(); $options = array('id' => 'uid_touch', 'table' => 'node_field_data', 'field' => 'uid_touch', 'value' => array($this->loggedInUser->id())); $view->addHandler('default', 'filter', 'node_field_data', 'uid_touch', $options); $this->executeView($view, array($this->account->id())); $result_set = array(array('nid' => $this->nodeUserPosted->id()), array('nid' => $this->nodeUserCommented->id())); $column_map = array('nid' => 'nid'); $this->assertIdenticalResultset($view, $result_set, $column_map); }
/** * Tests the bubbling of cache tags. */ public function testCacheTags() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = $this->container->get('renderer'); // Create the entity that will be commented upon. $commented_entity = EntityTest::create(array('name' => $this->randomMachineName())); $commented_entity->save(); // Verify cache tags on the rendered entity before it has comments. $build = \Drupal::entityManager()->getViewBuilder('entity_test')->view($commented_entity); $renderer->renderRoot($build); $expected_cache_tags = ['entity_test_view', 'entity_test:' . $commented_entity->id(), 'config:core.entity_form_display.comment.comment.default', 'config:field.field.comment.comment.comment_body', 'config:field.field.entity_test.entity_test.comment', 'config:field.storage.comment.comment_body', 'config:user.settings']; sort($expected_cache_tags); $this->assertEqual($build['#cache']['tags'], $expected_cache_tags); // Create a comment on that entity. Comment loading requires that the uid // also exists in the {users} table. $user = $this->createUser(); $user->save(); $comment = Comment::create(array('subject' => 'Llama', 'comment_body' => array('value' => 'Llamas are cool!', 'format' => 'plain_text'), 'entity_id' => $commented_entity->id(), 'entity_type' => 'entity_test', 'field_name' => 'comment', 'comment_type' => 'comment', 'status' => CommentInterface::PUBLISHED, 'uid' => $user->id())); $comment->save(); // Load commented entity so comment_count gets computed. // @todo Remove the $reset = TRUE parameter after // https://www.drupal.org/node/597236 lands. It's a temporary work-around. $storage = $this->container->get('entity_type.manager')->getStorage('entity_test'); $storage->resetCache([$commented_entity->id()]); $commented_entity = $storage->load($commented_entity->id()); // Verify cache tags on the rendered entity when it has comments. $build = \Drupal::entityManager()->getViewBuilder('entity_test')->view($commented_entity); $renderer->renderRoot($build); $expected_cache_tags = ['entity_test_view', 'entity_test:' . $commented_entity->id(), 'comment_view', 'comment:' . $comment->id(), 'config:filter.format.plain_text', 'user_view', 'user:2', 'config:core.entity_form_display.comment.comment.default', 'config:field.field.comment.comment.comment_body', 'config:field.field.entity_test.entity_test.comment', 'config:field.storage.comment.comment_body', 'config:user.settings']; sort($expected_cache_tags); $this->assertEqual($build['#cache']['tags'], $expected_cache_tags); // Build a render array with the entity in a sub-element so that lazy // builder elements bubble up outside of the entity and we can check that // it got the correct cache max age. $build = ['#type' => 'container']; $build['entity'] = \Drupal::entityManager()->getViewBuilder('entity_test')->view($commented_entity); $renderer->renderRoot($build); // The entity itself was cached but the top-level element is max-age 0 due // to the bubbled up max age due to the lazy-built comment form. $this->assertIdentical(Cache::PERMANENT, $build['entity']['#cache']['max-age']); $this->assertIdentical(0, $build['#cache']['max-age'], 'Top level render array has max-age 0'); // The children (fields) of the entity render array are only built in case // of a cache miss. $this->assertFalse(isset($build['entity']['comment']), 'Cache hit'); }
/** * Tests the comment pager for nodes with multiple grants per realm. */ public function testCommentPager() { // Create a node. $node = $this->drupalCreateNode(); // Create 60 comments. for ($i = 0; $i < 60; $i++) { $comment = Comment::create(array('entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'subject' => $this->randomMachineName(), 'comment_body' => array(array('value' => $this->randomMachineName())), 'status' => CommentInterface::PUBLISHED)); $comment->save(); } $this->drupalLogin($this->webUser); // View the node page. With the default 50 comments per page there should // be two pages (0, 1) but no third (2) page. $this->drupalGet('node/' . $node->id()); $this->assertText($node->label()); $this->assertText(t('Comments')); $this->assertRaw('page=1'); $this->assertNoRaw('page=2'); }
/** * Tests the "Active forum topics" block. */ public function testActiveForumTopicsBlock() { $this->drupalLogin($this->adminUser); // Create 10 forum topics. $topics = $this->createForumTopics(10); // Comment on the first 5 topics. $date = new DrupalDateTime(); for ($index = 0; $index < 5; $index++) { // Get the node from the topic title. $node = $this->drupalGetNodeByTitle($topics[$index]); $date->modify('+1 minute'); $comment = Comment::create(array('entity_id' => $node->id(), 'field_name' => 'comment_forum', 'entity_type' => 'node', 'node_type' => 'node_type_' . $node->bundle(), 'subject' => $this->randomString(20), 'comment_body' => $this->randomString(256), 'created' => $date->getTimestamp())); $comment->save(); } // Enable the block. $block = $this->drupalPlaceBlock('forum_active_block'); $this->drupalGet(''); $this->assertLink(t('More'), 0, 'Active forum topics block has a "more"-link.'); $this->assertLinkByHref('forum', 0, 'Active forum topics block has a "more"-link.'); // We expect the first 5 forum topics to appear in the "Active forum topics" // block. $this->drupalGet('<front>'); for ($index = 0; $index < 10; $index++) { if ($index < 5) { $this->assertLink($topics[$index], 0, format_string('Forum topic @topic found in the "Active forum topics" block.', array('@topic' => $topics[$index]))); } else { $this->assertNoText($topics[$index], format_string('Forum topic @topic not found in the "Active forum topics" block.', array('@topic' => $topics[$index]))); } } // Configure the active forum block to only show 2 topics. $block->getPlugin()->setConfigurationValue('block_count', 2); $block->save(); $this->drupalGet(''); // We expect only the 2 forum topics with most recent comments to appear in // the "Active forum topics" block. for ($index = 0; $index < 10; $index++) { if (in_array($index, array(3, 4))) { $this->assertLink($topics[$index], 0, 'Forum topic found in the "Active forum topics" block.'); } else { $this->assertNoText($topics[$index], 'Forum topic not found in the "Active forum topics" block.'); } } }
/** * {@inheritdoc} */ public function setUp($processor = NULL) { parent::setUp('content_access'); // The parent method already installs most needed node and comment schemas, // but here we also need the comment statistics. $this->installSchema('comment', array('comment_entity_statistics')); // Create a node type for testing. $type = NodeType::create(array('type' => 'page', 'name' => 'page')); $type->save(); // Create anonymous user role. $role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous')); $role->save(); // Insert the anonymous user into the database, as the user table is inner // joined by \Drupal\comment\CommentStorage. User::create(array('uid' => 0, 'name' => ''))->save(); // Create a node with attached comment. $this->nodes[0] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'test title')); $this->nodes[0]->save(); $comment_type = CommentType::create(array('id' => 'comment', 'target_entity_type_id' => 'node')); $comment_type->save(); $this->installConfig(array('comment')); $this->addDefaultCommentField('node', 'page'); $comment = Comment::create(array('entity_type' => 'node', 'entity_id' => $this->nodes[0]->id(), 'field_name' => 'comment', 'body' => 'test body', 'comment_type' => $comment_type->id())); $comment->save(); $this->comments[] = $comment; $this->nodes[1] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'some title')); $this->nodes[1]->save(); $this->nodes[2] = Node::create(array('status' => NODE_NOT_PUBLISHED, 'type' => 'page', 'title' => 'other title')); $this->nodes[2]->save(); // Also index users, to verify that they are unaffected by the processor. $manager = \Drupal::getContainer()->get('plugin.manager.search_api.datasource'); $datasources['entity:comment'] = $manager->createInstance('entity:comment', array('index' => $this->index)); $datasources['entity:node'] = $manager->createInstance('entity:node', array('index' => $this->index)); $datasources['entity:user'] = $manager->createInstance('entity:user', array('index' => $this->index)); $this->index->setDatasources($datasources); $this->index->save(); \Drupal::getContainer()->get('search_api.index_task_manager')->addItemsAll($this->index); $index_storage = \Drupal::entityTypeManager()->getStorage('search_api_index'); $index_storage->resetCache([$this->index->id()]); $this->index = $index_storage->load($this->index->id()); }
/** * Test that comments correctly invalidate the cache tag of their host entity. */ public function testCommentEntity() { $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS'); $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT'); // Create a "Hippopotamus" comment. $this->entityTestHippopotamidae = EntityTest::create(array('name' => 'Hippopotamus', 'type' => 'bar')); $this->entityTestHippopotamidae->save(); $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS'); $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT'); $hippo_comment = Comment::create(array('subject' => 'Hippopotamus', 'comment_body' => array('value' => 'The common hippopotamus (Hippopotamus amphibius), or hippo, is a large, mostly herbivorous mammal in sub-Saharan Africa', 'format' => 'plain_text'), 'entity_id' => $this->entityTestHippopotamidae->id(), 'entity_type' => 'entity_test', 'field_name' => 'comment', 'status' => CommentInterface::PUBLISHED)); $hippo_comment->save(); // Ensure that a new comment only invalidates the commented entity. $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT'); $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS'); $this->assertText($hippo_comment->getSubject()); // Ensure that updating an existing comment only invalidates the commented // entity. $this->entity->save(); $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS'); $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT'); }
/** * Tests the node comment statistics. */ function testCommentNodeCommentStatistics() { $node_storage = $this->container->get('entity.manager')->getStorage('node'); $this->drupalGet('<front>'); $this->assertNoLink(t('1 comment')); $this->assertEqual($this->node->get('comment')->comment_count, 0, 'The number of comments for the node is correct (0 comments)'); // Test comment statistic when creating comments. $comment1 = Comment::create(['entity_type' => 'node', 'field_name' => 'comment', 'subject' => 'How much wood would a woodchuck chuck', 'comment_body' => $this->randomMachineName(128), 'entity_id' => $this->node->id()]); $comment1->save(); $node_storage->resetCache([$this->node->id()]); $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->comment_count, 1, 'The number of comments for the node is correct (1 comment)'); $this->drupalGet('<front>'); $this->assertLink(t('1 comment')); $comment2 = Comment::create(['entity_type' => 'node', 'field_name' => 'comment', 'subject' => 'A big black bug bit a big black dog', 'comment_body' => $this->randomMachineName(128), 'entity_id' => $this->node->id()]); $comment2->save(); $comment3 = Comment::create(['entity_type' => 'node', 'field_name' => 'comment', 'subject' => 'How much pot, could a pot roast roast', 'comment_body' => $this->randomMachineName(128), 'entity_id' => $this->node->id()]); $comment3->save(); $node_storage->resetCache([$this->node->id()]); $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->comment_count, 3, 'The number of comments for the node is correct (3 comments)'); $this->drupalGet('<front>'); $this->assertLink(t('3 comments')); // Test comment statistic when deleting comments. $comment1->delete(); $comment2->delete(); $node_storage->resetCache([$this->node->id()]); $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->comment_count, 1, 'The number of comments for the node is correct (1 comment)'); $this->drupalGet('<front>'); $this->assertLink(t('1 comment')); $comment3->delete(); $node_storage->resetCache([$this->node->id()]); $node = $node_storage->load($this->node->id()); $this->assertEqual($node->get('comment')->comment_count, 0, 'The number of comments for the node is correct (0 comments)'); $this->drupalGet('<front>'); $this->assertNoLink(t('1 comment')); $this->assertNoLink(t('comments')); }
/** * Check access for comment fields. */ public function testCommentFields() { $user = User::create(['name' => 'test user']); $user->save(); $comment = Comment::create(['subject' => 'My comment title', 'uid' => $user->id(), 'entity_type' => 'entity_test', 'comment_type' => 'entity_test']); $comment->save(); $comment_anonymous = Comment::create(['subject' => 'Anonymous comment title', 'uid' => 0, 'name' => 'anonymous', 'mail' => '*****@*****.**', 'homepage' => 'https://example.com', 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'created' => 123456, 'status' => 1]); $comment_anonymous->save(); // @todo Expand the test coverage in https://www.drupal.org/node/2464635 $this->assertFieldAccess('comment', 'cid', $comment->id()); $this->assertFieldAccess('comment', 'cid', $comment_anonymous->id()); $this->assertFieldAccess('comment', 'uuid', $comment->uuid()); $this->assertFieldAccess('comment', 'subject', 'My comment title'); $this->assertFieldAccess('comment', 'subject', 'Anonymous comment title'); $this->assertFieldAccess('comment', 'name', 'anonymous'); $this->assertFieldAccess('comment', 'mail', '*****@*****.**'); $this->assertFieldAccess('comment', 'homepage', 'https://example.com'); $this->assertFieldAccess('comment', 'uid', $user->getUsername()); // $this->assertFieldAccess('comment', 'created', \Drupal::service('date.formatter')->format(123456)); // $this->assertFieldAccess('comment', 'changed', \Drupal::service('date.formatter')->format(REQUEST_TIME)); $this->assertFieldAccess('comment', 'status', 'On'); }