/**
  * {@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']);
 }
 /**
  * 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);
 }
예제 #3
0
 /**
  * Tests the Drupal 6 to Drupal 8 comment type migration.
  */
 public function testCommentType()
 {
     $comment_type = CommentType::load('comment');
     $this->assertIdentical('node', $comment_type->getTargetEntityTypeId());
     $comment_type = CommentType::load('comment_no_subject');
     $this->assertIdentical('node', $comment_type->getTargetEntityTypeId());
 }
 /**
  * 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();
 }
 /**
  * Asserts a comment type entity.
  *
  * @param string $id
  *   The entity ID.
  * @param string $label
  *   The entity label.
  */
 protected function assertEntity($id, $label)
 {
     $entity = CommentType::load($id);
     $this->assertTrue($entity instanceof CommentTypeInterface);
     /** @var \Drupal\comment\CommentTypeInterface $entity */
     $this->assertIdentical($label, $entity->label());
     $this->assertIdentical('node', $entity->getTargetEntityTypeId());
 }
예제 #6
0
 /**
  * {@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();
 }
예제 #7
0
  /**
   * 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();
  }
 /**
  * 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.');
     }
 }
예제 #9
0
 /**
  * {@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();
 }
예제 #10
0
 /**
  * {@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'));
 }
예제 #11
0
 /**
  * {@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');
 }
 /**
  * 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())));
 }
예제 #14
0
파일: Comment.php 프로젝트: brstde/gap1
 /**
  * {@inheritdoc}
  */
 public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions)
 {
     if ($comment_type = CommentType::load($bundle)) {
         $fields['entity_id'] = clone $base_field_definitions['entity_id'];
         $fields['entity_id']->setSetting('target_type', $comment_type->getTargetEntityTypeId());
         return $fields;
     }
     return array();
 }
 /**
  * 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'));
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     // Honeypot Configuration.
     $form['configuration'] = ['#type' => 'fieldset', '#title' => t('Honeypot Configuration'), '#collapsible' => TRUE, '#collapsed' => FALSE];
     $form['configuration']['protect_all_forms'] = ['#type' => 'checkbox', '#title' => t('Protect all forms with Honeypot'), '#description' => t('Enable Honeypot protection for ALL forms on this site (it is best to only enable Honeypot for the forms you need below).'), '#default_value' => $this->config('honeypot.settings')->get('protect_all_forms')];
     $form['configuration']['protect_all_forms']['#description'] .= '<br />' . t('<strong>Page caching will be disabled on any page where a form is present if the Honeypot time limit is not set to 0.</strong>');
     $form['configuration']['log'] = ['#type' => 'checkbox', '#title' => t('Log blocked form submissions'), '#description' => t('Log submissions that are blocked due to Honeypot protection.'), '#default_value' => $this->config('honeypot.settings')->get('log')];
     $form['configuration']['element_name'] = ['#type' => 'textfield', '#title' => t('Honeypot element name'), '#description' => t("The name of the Honeypot form field. It's usually most effective to use a generic name like email, homepage, or link, but this should be changed if it interferes with fields that are already in your forms. Must not contain spaces or special characters."), '#default_value' => $this->config('honeypot.settings')->get('element_name'), '#required' => TRUE, '#size' => 30];
     $form['configuration']['time_limit'] = ['#type' => 'textfield', '#title' => t('Honeypot time limit'), '#description' => t('Minimum time required before form should be considered entered by a human instead of a bot. Set to 0 to disable.'), '#default_value' => $this->config('honeypot.settings')->get('time_limit'), '#required' => TRUE, '#size' => 5, '#field_suffix' => t('seconds')];
     $form['configuration']['time_limit']['#description'] .= '<br />' . t('<strong>Page caching will be disabled if there is a form protected by time limit on the page.</strong>');
     // Honeypot Enabled forms.
     $form_settings = $this->config('honeypot.settings')->get('form_settings');
     $form['form_settings'] = ['#type' => 'fieldset', '#title' => t('Honeypot Enabled Forms'), '#description' => t("Check the boxes next to individual forms on which you'd like Honeypot protection enabled."), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#states' => ['invisible' => ['input[name="protect_all_forms"]' => ['checked' => TRUE]]]];
     // Generic forms.
     $form['form_settings']['general_forms'] = ['#markup' => '<h5>' . t('General Forms') . '</h5>'];
     // User register form.
     $form['form_settings']['user_register_form'] = ['#type' => 'checkbox', '#title' => t('User Registration form'), '#default_value' => $this->getFormSettingsValue($form_settings, 'user_register_form')];
     // User password form.
     $form['form_settings']['user_pass'] = ['#type' => 'checkbox', '#title' => t('User Password Reset form'), '#default_value' => $this->getFormSettingsValue($form_settings, 'user_pass')];
     // If webform.module enabled, add webforms.
     // TODO D8 - See if D8 version of Webform.module still uses this form ID.
     if (\Drupal::moduleHandler()->moduleExists('webform')) {
         $form['form_settings']['webforms'] = ['#type' => 'checkbox', '#title' => t('Webforms (all)'), '#default_value' => $this->getFormSettingsValue($form_settings, 'webforms')];
     }
     // If contact.module enabled, add contact forms.
     if (\Drupal::moduleHandler()->moduleExists('contact')) {
         $form['form_settings']['contact_forms'] = ['#markup' => '<h5>' . t('Contact Forms') . '</h5>'];
         $bundles = \Drupal::entityManager()->getBundleInfo('contact_message');
         $formController = \Drupal::entityManager()->getFormObject('contact_message', 'default');
         foreach ($bundles as $bundle_key => $bundle) {
             $stub = entity_create('contact_message', ['contact_form' => $bundle_key]);
             $formController->setEntity($stub);
             $form_id = $formController->getFormId();
             $form['form_settings'][$form_id] = ['#type' => 'checkbox', '#title' => SafeMarkup::checkPlain($bundle['label']), '#default_value' => $this->getFormSettingsValue($form_settings, $form_id)];
         }
     }
     // Node types for node forms.
     if (\Drupal::moduleHandler()->moduleExists('node')) {
         $types = NodeType::loadMultiple();
         if (!empty($types)) {
             // Node forms.
             $form['form_settings']['node_forms'] = ['#markup' => '<h5>' . t('Node Forms') . '</h5>'];
             foreach ($types as $type) {
                 $id = $type->getEntityTypeId() . '_node_form';
                 $form['form_settings'][$id] = ['#type' => 'checkbox', '#title' => t('@name node form', ['@name' => $type->label()]), '#default_value' => $this->getFormSettingsValue($form_settings, $id)];
             }
         }
     }
     // Comment types for comment forms.
     if (\Drupal::moduleHandler()->moduleExists('comment')) {
         $types = CommentType::loadMultiple();
         if (!empty($types)) {
             $form['form_settings']['comment_forms'] = ['#markup' => '<h5>' . t('Comment Forms') . '</h5>'];
             foreach ($types as $type) {
                 $id = 'comment_' . $type->id() . '_form';
                 $form['form_settings'][$id] = ['#type' => 'checkbox', '#title' => t('@name comment form', ['@name' => $type->label()]), '#default_value' => $this->getFormSettingsValue($form_settings, $id)];
             }
         }
     }
     // Store the keys we want to save in configuration when form is submitted.
     $keys_to_save = array_keys($form['configuration']);
     foreach ($keys_to_save as $key => $key_to_save) {
         if (strpos($key_to_save, '#') !== FALSE) {
             unset($keys_to_save[$key]);
         }
     }
     $form_state->setStorage(['keys' => $keys_to_save]);
     // For now, manually add submit button. Hopefully, by the time D8 is
     // released, there will be something like system_settings_form() in D7.
     $form['actions']['#type'] = 'container';
     $form['actions']['submit'] = ['#type' => 'submit', '#value' => t('Save configuration')];
     return $form;
 }
예제 #18
0
 /**
  * {@inheritdoc}
  */
 public function settingsForm(array &$form, FormStateInterface $form_state, $has_data)
 {
     $element = array();
     // @todo Inject entity storage once typed-data supports container injection.
     // See https://drupal.org/node/2053415 for more details.
     $comment_types = CommentType::loadMultiple();
     $options = array();
     $entity_type = $this->getEntity()->getEntityTypeId();
     foreach ($comment_types as $comment_type) {
         if ($comment_type->getTargetEntityTypeId() == $entity_type) {
             $options[$comment_type->id()] = $comment_type->label();
         }
     }
     $element['comment_type'] = array('#type' => 'select', '#title' => t('Comment type'), '#options' => $options, '#description' => t('Select the Comment type to use for this comment field.'), '#default_value' => $this->getSetting('comment_type'), '#disabled' => $has_data);
     return $element;
 }
예제 #19
0
 /**
  * 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.'));
 }
예제 #20
0
 /**
  * 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.');
 }
예제 #21
0
 /**
  * {@inheritdoc}
  */
 public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data)
 {
     $element = array();
     // @todo Inject entity storage once typed-data supports container injection.
     //   See https://www.drupal.org/node/2053415 for more details.
     $comment_types = CommentType::loadMultiple();
     $options = array();
     $entity_type = $this->getEntity()->getEntityTypeId();
     foreach ($comment_types as $comment_type) {
         if ($comment_type->getTargetEntityTypeId() == $entity_type) {
             $options[$comment_type->id()] = $comment_type->label();
         }
     }
     $element['comment_type'] = array('#type' => 'select', '#title' => t('Comment type'), '#options' => $options, '#required' => TRUE, '#description' => $this->t('Select the Comment type to use for this comment field. Manage the comment types from the <a href="@url">administration overview page</a>.', array('@url' => $this->url('entity.comment_type.collection'))), '#default_value' => $this->getSetting('comment_type'), '#disabled' => $has_data);
     return $element;
 }
예제 #22
0
 /**
  * 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'));
     }
 }