/** * 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); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('node'); $this->installEntitySchema('comment'); $this->installEntitySchema('taxonomy_term'); CommentType::create(['id' => 'comment_node_page', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_article', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_blog', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_book', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_forum', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_test_content_type', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'page', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'article', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'blog', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'book', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'forum', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'test_content_type', 'label' => $this->randomMachineName()])->save(); Vocabulary::create(['vid' => 'test_vocabulary'])->save(); // Give one unfortunate field instance invalid display settings to ensure // that the migration provides an empty array as a default (thus avoiding // an "unsupported operand types" fatal). Database::getConnection('default', 'migrate')->update('field_config_instance')->fields(array('data' => serialize(array('label' => 'Body', 'widget' => array('type' => 'text_textarea_with_summary', 'settings' => array('rows' => 20, 'summary_rows' => 5), 'weight' => -4, 'module' => 'text'), 'settings' => array('display_summary' => TRUE, 'text_processing' => 1, 'user_register_form' => FALSE), 'display' => array('default' => array('label' => 'hidden', 'type' => 'text_default', 'settings' => array(), 'module' => 'text', 'weight' => 0), 'teaser' => array('label' => 'hidden', 'type' => 'text_summary_or_trimmed', 'settings' => NULL, 'module' => 'text', 'weight' => 0)), 'required' => FALSE, 'description' => ''))))->condition('entity_type', 'node')->condition('bundle', 'article')->condition('field_name', 'body')->execute(); $this->executeMigrations(['d7_field', 'd7_field_instance', 'd7_view_modes', 'd7_field_formatter_settings']); }
/** * Creates the necessary node bundles for the default configuration. */ protected function createNodeBundles() { if ($this->profile != 'standard') { $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article')); } // Add comments to article. $comment_type = CommentType::create(array('id' => 'comment', 'target_entity_type_id' => 'node')); $comment_type->save(); $this->addDefaultCommentField('node', 'article'); // Add Image field to article. $field_name = strtolower('field_image'); $min_resolution = 50; $max_resolution = 100; $field_settings = array('max_resolution' => $max_resolution . 'x' . $max_resolution, 'min_resolution' => $min_resolution . 'x' . $min_resolution, 'alt_field' => 0); $this->createImageField($field_name, 'article', array(), $field_settings); // Add tags field to Article. // Create a tags vocabulary for the 'article' content type. $vocabulary = entity_create('taxonomy_vocabulary', array('name' => 'Tags', 'vid' => 'tags')); $vocabulary->save(); $field_name = 'field_' . $vocabulary->id(); $handler_settings = array('target_bundles' => array($vocabulary->id() => $vocabulary->id()), 'auto_create' => TRUE); $this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); entity_get_form_display('node', 'article', 'default')->setComponent($field_name, array('type' => 'entity_reference_autocomplete_tags', 'weight' => -4))->save(); entity_get_display('node', 'article', 'default')->setComponent($field_name, array('type' => 'entity_reference_label', 'weight' => 10))->save(); entity_get_display('node', 'article', 'teaser')->setComponent($field_name, array('type' => 'entity_reference_label', 'weight' => 10))->save(); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test'); $this->installEntitySchema('user'); $this->installEntitySchema('comment'); $this->installSchema('dblog', ['watchdog']); // Create a new 'comment' comment-type. CommentType::create(['id' => 'comment', 'label' => $this->randomString()])->save(); }
/** * Tests that comment fields cannot be added entities with non-integer IDs. */ public function testCommentFieldNonStringId() { try { $bundle = CommentType::create(array('id' => 'foo', 'label' => 'foo', 'description' => '', 'target_entity_type_id' => 'entity_test_string_id')); $bundle->save(); $field_storage = entity_create('field_storage_config', array('field_name' => 'foo', 'entity_type' => 'entity_test_string_id', 'settings' => array('comment_type' => 'entity_test_string_id'), 'type' => 'comment')); $field_storage->save(); $this->fail('Did not throw an exception as expected.'); } catch (\UnexpectedValueException $e) { $this->pass('Exception thrown when trying to create comment field on Entity Type with string ID.'); } }
/** * Creates a node type with a corresponding comment type. * * @param string $id * The node type ID. */ protected function createType($id) { NodeType::create([ 'type' => $id, 'label' => $this->randomString(), ])->save(); CommentType::create([ 'id' => 'comment_node_' . $id, 'label' => $this->randomString(), 'target_entity_type_id' => 'node', ])->save(); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('comment'); $this->installEntitySchema('node'); // Make sure uid 0 is created (default uid for comments is 0). $storage = \Drupal::entityManager()->getStorage('user'); // Insert a row for the anonymous user. $storage->create(array('uid' => 0, 'status' => 0, 'name' => ''))->save(); // Need at least one node type and comment type present. NodeType::create(['type' => 'testnodetype', 'name' => 'Test node type'])->save(); CommentType::create(['id' => 'testcommenttype', 'label' => 'Test comment type', 'target_entity_type_id' => 'node'])->save(); }
/** * {@inheritdoc} */ protected function setUp($import_test_views = TRUE) { parent::setUp(FALSE); // Install the necessary dependencies for node type creation to work. $this->installEntitySchema('node'); $this->installConfig(array('field', 'node')); $comment_type = CommentType::create(array('id' => 'comment', 'label' => 'Comment settings', 'description' => 'Comment settings', 'target_entity_type_id' => 'node')); $comment_type->save(); $content_type = NodeType::create(['type' => $this->randomMachineName(), 'name' => $this->randomString()]); $content_type->save(); $field_storage = FieldStorageConfig::create(array('field_name' => Unicode::strtolower($this->randomMachineName()), 'entity_type' => 'node', 'type' => 'comment')); $field_storage->save(); FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $content_type->id(), 'label' => $this->randomMachineName() . '_label', 'description' => $this->randomMachineName() . '_description', 'settings' => array('comment_type' => $comment_type->id())])->save(); FieldConfig::create(['field_storage' => FieldStorageConfig::loadByName('node', 'body'), 'bundle' => $content_type->id(), 'label' => $this->randomMachineName() . '_body', 'settings' => array('display_summary' => TRUE)])->save(); ViewTestData::createTestViews(get_class($this), array('views_test_config')); }
/** * {@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()); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installEntitySchema('comment'); CommentType::create(['id' => 'comment_node_page', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_article', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_blog', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_book', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_forum', 'label' => $this->randomMachineName()])->save(); CommentType::create(['id' => 'comment_node_test_content_type', 'label' => $this->randomMachineName()])->save(); $this->installEntitySchema('node'); NodeType::create(['type' => 'page', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'article', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'blog', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'book', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'forum', 'label' => $this->randomMachineName()])->save(); NodeType::create(['type' => 'test_content_type', 'label' => $this->randomMachineName()])->save(); $this->executeMigration('d7_field'); $this->executeMigration('d7_field_instance'); $this->executeMigration('d7_view_modes'); $this->executeMigration('d7_field_formatter_settings'); }
/** * Creates a comment comment type (bundle). * * @param string $label * The comment type label. * * @return \Drupal\comment\Entity\CommentType * Created comment type. */ protected function createCommentType($label) { $bundle = CommentType::create(array('id' => $label, 'label' => $label, 'description' => '', 'target_entity_type_id' => 'node')); $bundle->save(); return $bundle; }
/** * Tests anonymous comment functionality. */ function testCommentFunctionality() { $limited_user = $this->drupalCreateUser(array('administer entity_test fields')); $this->drupalLogin($limited_user); // Test that default field exists. $this->drupalGet('entity_test/structure/entity_test/fields'); $this->assertText(t('Comments')); $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); // Test widget hidden option is not visible when there's no comments. $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); $this->assertResponse(200); $this->assertNoField('edit-default-value-input-comment-und-0-status-0'); // Test that field to change cardinality is not available. $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment/storage'); $this->assertResponse(200); $this->assertNoField('cardinality_number'); $this->assertNoField('cardinality'); $this->drupalLogin($this->adminUser); // Test breadcrumb on comment add page. $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; $this->assertEqual(current($this->xpath($xpath)), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.'); // Post a comment. /** @var \Drupal\comment\CommentInterface $comment1 */ $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName()); $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); // Test breadcrumb on comment reply page. $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.'); // Test breadcrumb on comment edit page. $this->drupalGet('comment/' . $comment1->id() . '/edit'); $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.'); // Test breadcrumb on comment delete page. $this->drupalGet('comment/' . $comment1->id() . '/delete'); $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.'); // Unpublish the comment. $this->performCommentOperation($comment1, 'unpublish'); $this->drupalGet('admin/content/comment/approval'); $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.'); // Publish the comment. $this->performCommentOperation($comment1, 'publish', TRUE); $this->drupalGet('admin/content/comment'); $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); // Delete the comment. $this->performCommentOperation($comment1, 'delete'); $this->drupalGet('admin/content/comment'); $this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.'); // Post another comment. $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName()); $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); // Check that the comment was found. $this->drupalGet('admin/content/comment'); $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); // Check that entity access applies to administrative page. $this->assertText($this->entity->label(), 'Name of commented account found.'); $limited_user = $this->drupalCreateUser(array('administer comments')); $this->drupalLogin($limited_user); $this->drupalGet('admin/content/comment'); $this->assertNoText($this->entity->label(), 'No commented account name found.'); $this->drupalLogout(); // Deny anonymous users access to comments. user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => FALSE, 'post comments' => FALSE, 'skip comment approval' => FALSE, 'view test entity' => TRUE)); // Attempt to view comments while disallowed. $this->drupalGet('entity-test/' . $this->entity->id()); $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); $this->assertNoLink('Add new comment', 'Link to add comment was found.'); // Attempt to view test entity comment form while disallowed. $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); $this->assertResponse(403); $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => TRUE, 'post comments' => FALSE, 'view test entity' => TRUE, 'skip comment approval' => FALSE)); $this->drupalGet('entity_test/' . $this->entity->id()); $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.'); $this->assertLink('Log in', 0, 'Link to log in was found.'); $this->assertLink('register', 0, 'Link to register was found.'); $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); // Test the combination of anonymous users being able to post, but not view // comments, to ensure that access to post comments doesn't grant access to // view them. user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => FALSE, 'post comments' => TRUE, 'skip comment approval' => TRUE, 'view test entity' => TRUE)); $this->drupalGet('entity_test/' . $this->entity->id()); $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); $this->assertFieldByName('subject[0][value]', '', 'Subject field found.'); $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.'); $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); $this->assertResponse(403); $this->assertNoText($comment1->getSubject(), 'Comment not displayed.'); // Test comment field widget changes. $limited_user = $this->drupalCreateUser(array('administer entity_test fields', 'view test entity', 'administer entity_test content')); $this->drupalLogin($limited_user); $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1'); $this->assertFieldChecked('edit-default-value-input-comment-0-status-2'); // Test comment option change in field settings. $edit = array('default_value_input[comment][0][status]' => CommentItemInterface::CLOSED, 'settings[anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT); $this->drupalPostForm(NULL, $edit, t('Save settings')); $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); $this->assertFieldChecked('edit-default-value-input-comment-0-status-1'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2'); $this->assertFieldByName('settings[anonymous]', COMMENT_ANONYMOUS_MAY_CONTACT); // Add a new comment-type. $bundle = CommentType::create(array('id' => 'foobar', 'label' => 'Foobar', 'description' => '', 'target_entity_type_id' => 'entity_test')); $bundle->save(); // Add a new comment field. $storage_edit = array('settings[comment_type]' => 'foobar'); $this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit); // Add a third comment field. $this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit); // Check the field contains the correct comment type. $field_storage = FieldStorageConfig::load('entity_test.field_barfoo'); $this->assertTrue($field_storage); $this->assertEqual($field_storage->getSetting('comment_type'), 'foobar'); $this->assertEqual($field_storage->getCardinality(), 1); // Test the new entity commenting inherits default. $random_label = $this->randomMachineName(); $data = array('bundle' => 'entity_test', 'name' => $random_label); $new_entity = entity_create('entity_test', $data); $new_entity->save(); $this->drupalGet('entity_test/manage/' . $new_entity->id()); $this->assertNoFieldChecked('edit-field-foobar-0-status-1'); $this->assertFieldChecked('edit-field-foobar-0-status-2'); $this->assertNoField('edit-field-foobar-0-status-0'); // @todo Check proper url and form https://www.drupal.org/node/2458323 $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id()); $this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.'); $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.'); // Test removal of comment_body field. $limited_user = $this->drupalCreateUser(array('administer entity_test fields', 'post comments', 'administer comment fields', 'administer comment types')); $this->drupalLogin($limited_user); $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); $this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.'); $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings'); $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.'); // Set subject field to autogenerate it. $edit = ['subject[0][value]' => '']; $this->drupalPostForm(NULL, $edit, t('Save')); }
/** * Tests creating a comment field through the interface. */ public function testCommentFieldCreate() { // Create user who can administer user fields. $user = $this->drupalCreateUser(array('administer user fields')); $this->drupalLogin($user); // Create comment field in account settings. $edit = array('new_storage_type' => 'comment', 'label' => 'User comment', 'field_name' => 'user_comment'); $this->drupalPostForm('admin/config/people/accounts/fields/add-field', $edit, 'Save and continue'); // Try to save the comment field without selecting a comment type. $edit = array(); $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings')); // We should get an error message. $this->assertText(t('An illegal choice has been detected. Please contact the site administrator.')); // Create a comment type for users. $bundle = CommentType::create(array('id' => 'user_comment_type', 'label' => 'user_comment_type', 'description' => '', 'target_entity_type_id' => 'user')); $bundle->save(); // Select a comment type and try to save again. $edit = array('settings[comment_type]' => 'user_comment_type'); $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings')); // We shouldn't get an error message. $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.')); }
/** * Test permissions on comment fields. */ public function testAccessToAdministrativeFields() { // Create a comment type. $comment_type = CommentType::create(['id' => 'comment', 'label' => 'Default comments', 'description' => 'Default comment field', 'target_entity_type_id' => 'entity_test']); $comment_type->save(); // Create a comment against a test entity. $host = EntityTest::create(); $host->save(); // An administrator user. No user exists yet, ensure that the first user // does not have UID 1. $comment_admin_user = $this->createUser(['uid' => 2, 'name' => 'admin'], ['administer comments', 'access comments']); // Two comment enabled users, one with edit access. $comment_enabled_user = $this->createUser(['name' => 'enabled'], ['post comments', 'skip comment approval', 'edit own comments', 'access comments']); $comment_no_edit_user = $this->createUser(['name' => 'no edit'], ['post comments', 'skip comment approval', 'access comments']); // An unprivileged user. $comment_disabled_user = $this->createUser(['name' => 'disabled'], ['access content']); $role = Role::load(RoleInterface::ANONYMOUS_ID); $role->grantPermission('post comments')->save(); $anonymous_user = new AnonymousUserSession(); // Add two fields. $this->addDefaultCommentField('entity_test', 'entity_test', 'comment'); $this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other'); // Change the second field's anonymous contact setting. $instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other'); // Default is 'May not contact', for this field - they may contact. $instance->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT); $instance->save(); // Create three "Comments". One is owned by our edit-enabled user. $comment1 = Comment::create(['entity_type' => 'entity_test', 'name' => 'Tony', 'hostname' => 'magic.example.com', 'mail' => '*****@*****.**', 'subject' => 'Bruce the Mesopotamian moose', 'entity_id' => $host->id(), 'comment_type' => 'comment', 'field_name' => 'comment', 'pid' => 0, 'uid' => 0, 'status' => 1]); $comment1->save(); $comment2 = Comment::create(['entity_type' => 'entity_test', 'hostname' => 'magic.example.com', 'subject' => 'Brian the messed up lion', 'entity_id' => $host->id(), 'comment_type' => 'comment', 'field_name' => 'comment', 'status' => 1, 'pid' => 0, 'uid' => $comment_enabled_user->id()]); $comment2->save(); $comment3 = Comment::create(['entity_type' => 'entity_test', 'hostname' => 'magic.example.com', 'status' => 0, 'subject' => 'Gail the minky whale', 'entity_id' => $host->id(), 'comment_type' => 'comment', 'field_name' => 'comment_other', 'pid' => $comment2->id(), 'uid' => $comment_no_edit_user->id()]); $comment3->save(); // Note we intentionally don't save this comment so it remains 'new'. $comment4 = Comment::create(['entity_type' => 'entity_test', 'hostname' => 'magic.example.com', 'status' => 0, 'subject' => 'Daniel the Cocker-Spaniel', 'entity_id' => $host->id(), 'comment_type' => 'comment', 'field_name' => 'comment_other', 'pid' => 0, 'uid' => $anonymous_user->id()]); // Generate permutations. $combinations = ['comment' => [$comment1, $comment2, $comment3, $comment4], 'user' => [$comment_admin_user, $comment_enabled_user, $comment_no_edit_user, $comment_disabled_user, $anonymous_user]]; $permutations = TestBase::generatePermutations($combinations); // Check access to administrative fields. foreach ($this->administrativeFields as $field) { foreach ($permutations as $set) { $may_view = $set['comment']->{$field}->access('view', $set['user']); $may_update = $set['comment']->{$field}->access('edit', $set['user']); $this->assertTrue($may_view, SafeMarkup::format('User @user can view field @field on comment @comment', ['@user' => $set['user']->getUsername(), '@comment' => $set['comment']->getSubject(), '@field' => $field])); $this->assertEqual($may_update, $set['user']->hasPermission('administer comments'), SafeMarkup::format('User @user @state update field @field on comment @comment', ['@user' => $set['user']->getUsername(), '@state' => $may_update ? 'can' : 'cannot', '@comment' => $set['comment']->getSubject(), '@field' => $field])); } } // Check access to normal field. foreach ($permutations as $set) { $may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']); $this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || $set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId(), SafeMarkup::format('User @user @state update field subject on comment @comment', ['@user' => $set['user']->getUsername(), '@state' => $may_update ? 'can' : 'cannot', '@comment' => $set['comment']->getSubject()])); } // Check read-only fields. foreach ($this->readOnlyFields as $field) { // Check view operation. foreach ($permutations as $set) { $may_view = $set['comment']->{$field}->access('view', $set['user']); $may_update = $set['comment']->{$field}->access('edit', $set['user']); // Nobody has access to view the hostname field. if ($field === 'hostname') { $view_access = FALSE; $state = 'cannot'; } else { $view_access = TRUE; $state = 'can'; } $this->assertEqual($may_view, $view_access, SafeMarkup::format('User @user @state view field @field on comment @comment', ['@user' => $set['user']->getUsername(), '@comment' => $set['comment']->getSubject(), '@field' => $field, '@state' => $state])); $this->assertFalse($may_update, SafeMarkup::format('User @user @state update field @field on comment @comment', ['@user' => $set['user']->getUsername(), '@state' => $may_update ? 'can' : 'cannot', '@comment' => $set['comment']->getSubject(), '@field' => $field])); } } // Check create-only fields. foreach ($this->createOnlyFields as $field) { // Check view operation. foreach ($permutations as $set) { $may_view = $set['comment']->{$field}->access('view', $set['user']); $may_update = $set['comment']->{$field}->access('edit', $set['user']); $this->assertEqual($may_view, TRUE, SafeMarkup::format('User @user can view field @field on comment @comment', ['@user' => $set['user']->getUsername(), '@comment' => $set['comment']->getSubject(), '@field' => $field])); $this->assertEqual($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), SafeMarkup::format('User @user @state update field @field on comment @comment', ['@user' => $set['user']->getUsername(), '@state' => $may_update ? 'can' : 'cannot', '@comment' => $set['comment']->getSubject(), '@field' => $field])); } } // Check contact fields. foreach ($this->contactFields as $field) { // Check view operation. foreach ($permutations as $set) { $may_update = $set['comment']->{$field}->access('edit', $set['user']); // To edit the 'mail' or 'name' field, either the user has the // "administer comments" permissions or the user is anonymous and // adding a new comment using a field that allows contact details. $this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || $set['user']->isAnonymous() && $set['comment']->isNew() && $set['user']->hasPermission('post comments') && $set['comment']->getFieldName() == 'comment_other', SafeMarkup::format('User @user @state update field @field on comment @comment', ['@user' => $set['user']->getUsername(), '@state' => $may_update ? 'can' : 'cannot', '@comment' => $set['comment']->getSubject(), '@field' => $field])); } } foreach ($permutations as $set) { // Check no view-access to mail field for other than admin. $may_view = $set['comment']->mail->access('view', $set['user']); $this->assertEqual($may_view, $set['user']->hasPermission('administer comments')); } }
/** * Tests anonymous comment functionality. */ function testCommentFunctionality() { $limited_user = $this->drupalCreateUser(array('administer entity_test fields')); $this->drupalLogin($limited_user); // Test that default field exists. $this->drupalGet('entity_test/structure/entity_test/fields'); $this->assertText(t('Comment settings')); $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); // Test widget hidden option is not visible when there's no comments. $this->drupalGet('entity_test/structure/entity_test/entity-test/fields/entity_test.entity_test.comment'); $this->assertNoField('edit-default-value-input-comment-und-0-status-0'); $this->drupalLogin($this->admin_user); // Post a comment. $comment1 = $this->postComment($this->entity, $this->randomName(), $this->randomName()); $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); // Assert the breadcrumb is valid. $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); $this->assertLink($this->entity->label()); // Unpublish the comment. $this->performCommentOperation($comment1, 'unpublish'); $this->drupalGet('admin/content/comment/approval'); $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.'); // Publish the comment. $this->performCommentOperation($comment1, 'publish', TRUE); $this->drupalGet('admin/content/comment'); $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); // Delete the comment. $this->performCommentOperation($comment1, 'delete'); $this->drupalGet('admin/content/comment'); $this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.'); // Post another comment. $comment1 = $this->postComment($this->entity, $this->randomName(), $this->randomName()); $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); // Check that the comment was found. $this->drupalGet('admin/content/comment'); $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); // Check that entity access applies to administrative page. $this->assertText($this->entity->label(), 'Name of commented account found.'); $limited_user = $this->drupalCreateUser(array('administer comments')); $this->drupalLogin($limited_user); $this->drupalGet('admin/content/comment'); $this->assertNoText($this->entity->label(), 'No commented account name found.'); $this->drupalLogout(); // Deny anonymous users access to comments. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access comments' => FALSE, 'post comments' => FALSE, 'skip comment approval' => FALSE, 'view test entity' => TRUE)); // Attempt to view comments while disallowed. $this->drupalGet('entity-test/' . $this->entity->id()); $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); $this->assertNoLink('Add new comment', 'Link to add comment was found.'); // Attempt to view test entity comment form while disallowed. $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); $this->assertText('You are not authorized to post comments', 'Error attempting to post comment.'); $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access comments' => TRUE, 'post comments' => FALSE, 'view test entity' => TRUE, 'skip comment approval' => FALSE)); $this->drupalGet('entity_test/' . $this->entity->id()); $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.'); $this->assertLink('Log in', 0, 'Link to log in was found.'); $this->assertLink('register', 0, 'Link to register was found.'); $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); // Test the combination of anonymous users being able to post, but not view // comments, to ensure that access to post comments doesn't grant access to // view them. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array('access comments' => FALSE, 'post comments' => TRUE, 'skip comment approval' => TRUE, 'view test entity' => TRUE)); $this->drupalGet('entity_test/' . $this->entity->id()); $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); $this->assertFieldByName('subject[0][value]', '', 'Subject field found.'); $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.'); $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); $this->assertText('You are not authorized to view comments'); $this->assertNoText($comment1->getSubject(), 'Comment not displayed.'); // Test comment field widget changes. $limited_user = $this->drupalCreateUser(array('administer entity_test fields', 'view test entity', 'administer entity_test content')); $this->drupalLogin($limited_user); $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1'); $this->assertFieldChecked('edit-default-value-input-comment-0-status-2'); // Test comment option change in field settings. $edit = array('default_value_input[comment][0][status]' => CommentItemInterface::CLOSED); $this->drupalPostForm(NULL, $edit, t('Save settings')); $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); $this->assertFieldChecked('edit-default-value-input-comment-0-status-1'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2'); // Add a new comment-type. $bundle = CommentType::create(array('id' => 'foobar', 'label' => 'Foobar', 'description' => '', 'target_entity_type_id' => 'entity_test')); $bundle->save(); // Add a new comment field. $this->drupalGet('entity_test/structure/entity_test/fields'); $edit = array('fields[_add_new_field][label]' => 'Foobar', 'fields[_add_new_field][field_name]' => 'foobar', 'fields[_add_new_field][type]' => 'comment'); $this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, array('field[settings][comment_type]' => 'foobar'), t('Save field settings')); $this->drupalPostForm(NULL, array(), t('Save settings')); $this->assertRaw(t('Saved %name configuration', array('%name' => 'Foobar'))); // Add a third comment field. $this->drupalGet('entity_test/structure/entity_test/fields'); $edit = array('fields[_add_new_field][label]' => 'Barfoo', 'fields[_add_new_field][field_name]' => 'barfoo', 'fields[_add_new_field][type]' => 'comment'); $this->drupalPostForm(NULL, $edit, t('Save')); // Re-use another comment type. $this->drupalPostForm(NULL, array('field[settings][comment_type]' => 'foobar'), t('Save field settings')); $this->drupalPostForm(NULL, array(), t('Save settings')); $this->assertRaw(t('Saved %name configuration', array('%name' => 'Barfoo'))); // Check the field contains the correct comment type. $field_storage = entity_load('field_storage_config', 'entity_test.field_barfoo'); $this->assertTrue($field_storage); $this->assertEqual($field_storage->getSetting('comment_type'), 'foobar'); // Test the new entity commenting inherits default. $random_label = $this->randomName(); $data = array('bundle' => 'entity_test', 'name' => $random_label); $new_entity = entity_create('entity_test', $data); $new_entity->save(); $this->drupalGet('entity_test/manage/' . $new_entity->id()); $this->assertNoFieldChecked('edit-field-foobar-0-status-1'); $this->assertFieldChecked('edit-field-foobar-0-status-2'); $this->assertNoField('edit-field-foobar-0-status-0'); $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id()); $this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.'); $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.'); }