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); }
/** * 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); }
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($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 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.'); }
/** * {@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(); }
/** * 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); }
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); }
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))); }
/** * 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'); }
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(); }
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'); }
/** * Tests comments in book export. */ public function testBookCommentPrint() { $book_node = Node::create(['type' => 'book', 'title' => 'Book title', 'body' => 'Book body']); $book_node->book['bid'] = 'new'; $book_node->save(); $comment_subject = $this->randomMachineName(8); $comment_body = $this->randomMachineName(8); $comment = Comment::create(array('subject' => $comment_subject, 'comment_body' => $comment_body, 'entity_id' => $book_node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'status' => CommentInterface::PUBLISHED)); $comment->save(); $commenting_user = $this->drupalCreateUser(array('access printer-friendly version', 'access comments', 'post comments')); $this->drupalLogin($commenting_user); $this->drupalGet('node/' . $book_node->id()); $this->assertText($comment_subject, 'Comment subject found'); $this->assertText($comment_body, 'Comment body found'); $this->assertText(t('Add new comment'), 'Comment form found'); $this->assertField('subject[0][value]', 'Comment form subject found'); $this->drupalGet('book/export/html/' . $book_node->id()); $this->assertText(t('Comments'), 'Comment thread found'); $this->assertText($comment_subject, 'Comment subject found'); $this->assertText($comment_body, 'Comment body found'); $this->assertNoText(t('Add new comment'), 'Comment form not found'); $this->assertNoField('subject[0][value]', 'Comment form subject not found'); }
protected function setUp() { parent::setUp(); // Install all available non-testing themes. $listing = new ExtensionDiscovery(\Drupal::root()); $this->themes = $listing->scan('theme', FALSE); \Drupal::service('theme_handler')->install(array_keys($this->themes)); // Create a test user. $this->user = $this->drupalCreateUser(array('access content', 'access user profiles')); $this->user->name = $this->xssLabel; $this->user->save(); $this->drupalLogin($this->user); // Create a test term. $this->term = Term::create(['name' => $this->xssLabel, 'vid' => 1]); $this->term->save(); // Add a comment field. $this->addDefaultCommentField('node', 'article', 'comment', CommentItemInterface::OPEN); // Create a test node tagged with the test term. $this->node = $this->drupalCreateNode(array('title' => $this->xssLabel, 'type' => 'article', 'promote' => NODE_PROMOTED, 'field_tags' => array(array('target_id' => $this->term->id())))); // Create a test comment on the test node. $this->comment = Comment::create(array('entity_id' => $this->node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'status' => CommentInterface::PUBLISHED, 'subject' => $this->xssLabel, 'comment_body' => array($this->randomMachineName()))); $this->comment->save(); }
/** * Tests ValidReferenceConstraint with newly created and unsaved entities. */ public function testAutocreateValidation() { // The term entity is unsaved here. $term = Term::create(array('name' => $this->randomMachineName(), 'vid' => $this->term->bundle(), 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $entity = EntityTest::create(['field_test_taxonomy_term' => ['entity' => $term, 'target_id' => NULL]]); $errors = $entity->validate(); // Using target_id of NULL is valid with an unsaved entity. $this->assertEqual(0, count($errors)); // Using target_id of NULL is not valid with a saved entity. $term->save(); $entity = EntityTest::create(['field_test_taxonomy_term' => ['entity' => $term, 'target_id' => NULL]]); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); $this->assertEqual($errors[0]->getMessage(), 'This value should not be null.'); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_taxonomy_term.0'); // This should rectify the issue, favoring the entity over the target_id. $entity->save(); $errors = $entity->validate(); $this->assertEqual(0, count($errors)); // Test with an unpublished and unsaved node. $title = $this->randomString(); $node = Node::create(['title' => $title, 'type' => 'node', 'status' => NODE_NOT_PUBLISHED]); $entity = EntityTest::create(['field_test_node' => ['entity' => $node]]); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity'); // Publish the node and try again. $node->setPublished(TRUE); $errors = $entity->validate(); $this->assertEqual(0, count($errors)); // Test with a mix of valid and invalid nodes. $unsaved_unpublished_node_title = $this->randomString(); $unsaved_unpublished_node = Node::create(['title' => $unsaved_unpublished_node_title, 'type' => 'node', 'status' => NODE_NOT_PUBLISHED]); $saved_unpublished_node_title = $this->randomString(); $saved_unpublished_node = Node::create(['title' => $saved_unpublished_node_title, 'type' => 'node', 'status' => NODE_NOT_PUBLISHED]); $saved_unpublished_node->save(); $saved_published_node_title = $this->randomString(); $saved_published_node = Node::create(['title' => $saved_published_node_title, 'type' => 'node', 'status' => NODE_PUBLISHED]); $saved_published_node->save(); $entity = EntityTest::create(['field_test_node' => [['entity' => $unsaved_unpublished_node], ['target_id' => $saved_unpublished_node->id()], ['target_id' => $saved_published_node->id()]]]); $errors = $entity->validate(); $this->assertEqual(2, count($errors)); $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity'); $this->assertEqual($errors[1]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $saved_unpublished_node->id()])); $this->assertEqual($errors[1]->getPropertyPath(), 'field_test_node.1.target_id'); // Publish one of the nodes and try again. $saved_unpublished_node->setPublished(TRUE); $saved_unpublished_node->save(); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'node', '%label' => $unsaved_unpublished_node_title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_node.0.entity'); // Publish the last invalid node and try again. $unsaved_unpublished_node->setPublished(TRUE); $errors = $entity->validate(); $this->assertEqual(0, count($errors)); // Test with an unpublished and unsaved comment. $title = $this->randomString(); $comment = Comment::create(['subject' => $title, 'comment_type' => 'comment', 'status' => 0]); $entity = EntityTest::create(['field_test_comment' => ['entity' => $comment]]); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'comment', '%label' => $title])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_comment.0.entity'); // Publish the comment and try again. $comment->setPublished(TRUE); $errors = $entity->validate(); $this->assertEqual(0, count($errors)); // Test with an inactive and unsaved user. $name = $this->randomString(); $user = User::create(['name' => $name, 'status' => 0]); $entity = EntityTest::create(['field_test_user' => ['entity' => $user]]); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'user', '%label' => $name])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_user.0.entity'); // Activate the user and try again. $user->activate(); $errors = $entity->validate(); $this->assertEqual(0, count($errors)); // Test with a temporary and unsaved file. $filename = $this->randomMachineName() . '.txt'; $file = File::create(['filename' => $filename, 'status' => 0]); $entity = EntityTest::create(['field_test_file' => ['entity' => $file]]); $errors = $entity->validate(); $this->assertEqual(1, count($errors)); $this->assertEqual($errors[0]->getMessage(), new FormattableMarkup('This entity (%type: %label) cannot be referenced.', ['%type' => 'file', '%label' => $filename])); $this->assertEqual($errors[0]->getPropertyPath(), 'field_test_file.0.entity'); // Set the file as permanent and try again. $file->setPermanent(); $errors = $entity->validate(); $this->assertEqual(0, count($errors)); }
/** * Tests deleting a comment type that still has content. */ public function testCommentTypeDeletion() { // Create a comment type programmatically. $type = $this->createCommentType('foo'); $this->drupalCreateContentType(array('type' => 'page')); \Drupal::service('comment.manager')->addDefaultField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo'); $field_storage = FieldStorageConfig::loadByName('node', 'foo'); $this->drupalLogin($this->adminUser); // Create a node. $node = Node::create(array('type' => 'page', 'title' => 'foo')); $node->save(); // Add a new comment of this type. $comment = Comment::create(array('comment_type' => 'foo', 'entity_type' => 'node', 'field_name' => 'foo', 'entity_id' => $node->id())); $comment->save(); // Attempt to delete the comment type, which should not be allowed. $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete'); $this->assertRaw(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.', array('%label' => $type->label())), 'The comment type will not be deleted until all comments of that type are removed.'); $this->assertRaw(t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', array('%label' => 'foo', '%field' => 'node.foo')), 'The comment type will not be deleted until all fields of that type are removed.'); $this->assertNoText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is not available.'); // Delete the comment and the field. $comment->delete(); $field_storage->delete(); // Attempt to delete the comment type, which should now be allowed. $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete'); $this->assertRaw(t('Are you sure you want to delete %type?', array('%type' => $type->id())), 'The comment type is available for deletion.'); $this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.'); // Test exception thrown when re-using an existing comment type. try { \Drupal::service('comment.manager')->addDefaultField('comment', 'comment', 'bar'); $this->fail('Exception not thrown.'); } catch (\InvalidArgumentException $e) { $this->pass('Exception thrown if attempting to re-use comment-type from another entity type.'); } // Delete the comment type. $this->drupalPostForm('admin/structure/comment/manage/' . $type->id() . '/delete', array(), t('Delete')); $this->assertNull(CommentType::load($type->id()), 'Comment type deleted.'); $this->assertRaw(t('Comment type %label has been deleted.', array('%label' => $type->label()))); }
/** * Test patching a comment using both HAL+JSON and JSON. */ public function testUpdateComment() { $entity_type = 'comment'; // Enables the REST service for 'comment' entity type. $this->enableService('entity:' . $entity_type, 'PATCH', ['hal_json', 'json']); $permissions = $this->entityPermissions($entity_type, 'update'); $account = $this->drupalCreateUser($permissions); $account->set('mail', '*****@*****.**'); $this->drupalLogin($account); // Create & save an entity to comment on, plus a comment. $entity_test = EntityTest::create(); $entity_test->save(); $entity_values = $this->entityValues($entity_type); $entity_values['entity_id'] = $entity_test->id(); $entity_values['uid'] = $account->id(); $comment = Comment::create($entity_values); $comment->save(); $this->pass('Test case 1: PATCH comment using HAL+JSON.'); $comment->setSubject('Initial subject')->save(); $read_only_fields = [ 'name', 'created', 'changed', 'status', 'thread', 'entity_type', 'field_name', 'entity_id', 'uid', ]; $this->assertNotEqual('Updated subject', $comment->getSubject()); $comment->setSubject('Updated subject'); $this->patchEntity($comment, $read_only_fields, $account, 'hal_json', 'application/hal+json'); $comment = Comment::load($comment->id()); $this->assertEqual('Updated subject', $comment->getSubject()); $this->pass('Test case 1: PATCH comment using JSON.'); $comment->setSubject('Initial subject')->save(); $read_only_fields = [ 'pid', // Extra compared to HAL+JSON. 'entity_id', 'uid', 'name', 'homepage', // Extra compared to HAL+JSON. 'created', 'changed', 'status', 'thread', 'entity_type', 'field_name', ]; $this->assertNotEqual('Updated subject', $comment->getSubject()); $comment->setSubject('Updated subject'); $this->patchEntity($comment, $read_only_fields, $account, 'json', 'application/json'); $comment = Comment::load($comment->id()); $this->assertEqual('Updated subject', $comment->getSubject()); }
/** * Delete account and anonymize all content. */ function testUserAnonymize() { $node_storage = $this->container->get('entity.manager')->getStorage('node'); $this->config('user.settings')->set('cancel_method', 'user_cancel_reassign')->save(); // Create comment field on page. $this->addDefaultCommentField('node', 'page'); $user_storage = $this->container->get('entity.manager')->getStorage('user'); // Create a user. $account = $this->drupalCreateUser(array('cancel account')); $this->drupalLogin($account); // Load a real user object. $user_storage->resetCache(array($account->id())); $account = $user_storage->load($account->id()); // Create a simple node. $node = $this->drupalCreateNode(array('uid' => $account->id())); // Add a comment to the page. $comment_subject = $this->randomMachineName(8); $comment_body = $this->randomMachineName(8); $comment = Comment::create(array('subject' => $comment_subject, 'comment_body' => $comment_body, 'entity_id' => $node->id(), 'entity_type' => 'node', 'field_name' => 'comment', 'status' => CommentInterface::PUBLISHED, 'uid' => $account->id())); $comment->save(); // Create a node with two revisions, the initial one belonging to the // cancelling user. $revision_node = $this->drupalCreateNode(array('uid' => $account->id())); $revision = $revision_node->getRevisionId(); $settings = get_object_vars($revision_node); $settings['revision'] = 1; $settings['uid'] = 1; // Set new/current revision to someone else. $revision_node = $this->drupalCreateNode($settings); // Attempt to cancel account. $this->drupalGet('user/' . $account->id() . '/edit'); $this->drupalPostForm(NULL, NULL, t('Cancel account')); $this->assertText(t('Are you sure you want to cancel your account?'), 'Confirmation form to cancel account displayed.'); $this->assertRaw(t('Your account will be removed and all account information deleted. All of your content will be assigned to the %anonymous-name user.', array('%anonymous-name' => $this->config('user.settings')->get('anonymous'))), 'Informs that all content will be attributed to anonymous account.'); // Confirm account cancellation. $timestamp = time(); $this->drupalPostForm(NULL, NULL, t('Cancel account')); $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.'); // Confirm account cancellation request. $this->drupalGet("user/" . $account->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp)); $user_storage->resetCache(array($account->id())); $this->assertFalse($user_storage->load($account->id()), 'User is not found in the database.'); // Confirm that user's content has been attributed to anonymous user. $anonymous_user = User::getAnonymousUser(); $node_storage->resetCache(array($node->id())); $test_node = $node_storage->load($node->id()); $this->assertTrue($test_node->getOwnerId() == 0 && $test_node->isPublished(), 'Node of the user has been attributed to anonymous user.'); $test_node = node_revision_load($revision, TRUE); $this->assertTrue($test_node->getRevisionUser()->id() == 0 && $test_node->isPublished(), 'Node revision of the user has been attributed to anonymous user.'); $node_storage->resetCache(array($revision_node->id())); $test_node = $node_storage->load($revision_node->id()); $this->assertTrue($test_node->getOwnerId() != 0 && $test_node->isPublished(), "Current revision of the user's node was not attributed to anonymous user."); $storage = \Drupal::entityManager()->getStorage('comment'); $storage->resetCache(array($comment->id())); $test_comment = $storage->load($comment->id()); $this->assertTrue($test_comment->getOwnerId() == 0 && $test_comment->isPublished(), 'Comment of the user has been attributed to anonymous user.'); $this->assertEqual($test_comment->getAuthorName(), $anonymous_user->getDisplayName(), 'Comment of the user has been attributed to anonymous user name.'); // Confirm that the confirmation message made it through to the end user. $this->assertRaw(t('%name has been deleted.', array('%name' => $account->getUsername())), "Confirmation message displayed to user."); }