Update to Drupal 8.1.0. For more information, see https://www.drupal.org/drupal-8.1.0-release-notes
This commit is contained in:
parent
b11a755ba8
commit
c0a0d5a94c
6920 changed files with 64395 additions and 57312 deletions
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment_test\Controller\CommentTestController.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment_test\Controller;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
|
|
|
@ -0,0 +1,158 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel;
|
||||
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
|
||||
/**
|
||||
* Tests the bubbling up of comment cache tags when using the Comment list
|
||||
* formatter on an entity.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('entity_test', 'comment');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$session = new Session();
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->setSession($session);
|
||||
|
||||
/** @var \Symfony\Component\HttpFoundation\RequestStack $stack */
|
||||
$stack = $this->container->get('request_stack');
|
||||
$stack->pop();
|
||||
$stack->push($request);
|
||||
|
||||
// Set the current user to one that can access comments. Specifically, this
|
||||
// user does not have access to the 'administer comments' permission, to
|
||||
// ensure only published comments are visible to the end user.
|
||||
$current_user = $this->container->get('current_user');
|
||||
$current_user->setAccount($this->createUser(array(), array('access comments')));
|
||||
|
||||
// Install tables and config needed to render comments.
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
$this->installConfig(array('system', 'filter', 'comment'));
|
||||
|
||||
// Comment rendering generates links, so build the router.
|
||||
$this->container->get('router.builder')->rebuild();
|
||||
|
||||
// Set up a field, so that the entity that'll be referenced bubbles up a
|
||||
// cache tag when rendering it entirely.
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the bubbling of cache tags.
|
||||
*/
|
||||
public function testCacheTags() {
|
||||
/** @var \Drupal\Core\Render\RendererInterface $renderer */
|
||||
$renderer = $this->container->get('renderer');
|
||||
|
||||
// Create the entity that will be commented upon.
|
||||
$commented_entity = EntityTest::create(array('name' => $this->randomMachineName()));
|
||||
$commented_entity->save();
|
||||
|
||||
// Verify cache tags on the rendered entity before it has comments.
|
||||
$build = \Drupal::entityManager()
|
||||
->getViewBuilder('entity_test')
|
||||
->view($commented_entity);
|
||||
$renderer->renderRoot($build);
|
||||
$expected_cache_tags = [
|
||||
'entity_test_view',
|
||||
'entity_test:' . $commented_entity->id(),
|
||||
'config:core.entity_form_display.comment.comment.default',
|
||||
'config:field.field.comment.comment.comment_body',
|
||||
'config:field.field.entity_test.entity_test.comment',
|
||||
'config:field.storage.comment.comment_body',
|
||||
'config:user.settings',
|
||||
];
|
||||
sort($expected_cache_tags);
|
||||
$this->assertEqual($build['#cache']['tags'], $expected_cache_tags);
|
||||
|
||||
// Create a comment on that entity. Comment loading requires that the uid
|
||||
// also exists in the {users} table.
|
||||
$user = $this->createUser();
|
||||
$user->save();
|
||||
$comment = Comment::create(array(
|
||||
'subject' => 'Llama',
|
||||
'comment_body' => array(
|
||||
'value' => 'Llamas are cool!',
|
||||
'format' => 'plain_text',
|
||||
),
|
||||
'entity_id' => $commented_entity->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'comment',
|
||||
'comment_type' => 'comment',
|
||||
'status' => CommentInterface::PUBLISHED,
|
||||
'uid' => $user->id(),
|
||||
));
|
||||
$comment->save();
|
||||
|
||||
// Load commented entity so comment_count gets computed.
|
||||
// @todo Remove the $reset = TRUE parameter after
|
||||
// https://www.drupal.org/node/597236 lands. It's a temporary work-around.
|
||||
$commented_entity = entity_load('entity_test', $commented_entity->id(), TRUE);
|
||||
|
||||
// Verify cache tags on the rendered entity when it has comments.
|
||||
$build = \Drupal::entityManager()
|
||||
->getViewBuilder('entity_test')
|
||||
->view($commented_entity);
|
||||
$renderer->renderRoot($build);
|
||||
$expected_cache_tags = [
|
||||
'entity_test_view',
|
||||
'entity_test:' . $commented_entity->id(),
|
||||
'comment_view',
|
||||
'comment:' . $comment->id(),
|
||||
'config:filter.format.plain_text',
|
||||
'user_view',
|
||||
'user:2',
|
||||
'config:core.entity_form_display.comment.comment.default',
|
||||
'config:field.field.comment.comment.comment_body',
|
||||
'config:field.field.entity_test.entity_test.comment',
|
||||
'config:field.storage.comment.comment_body',
|
||||
'config:user.settings',
|
||||
];
|
||||
sort($expected_cache_tags);
|
||||
$this->assertEqual($build['#cache']['tags'], $expected_cache_tags);
|
||||
|
||||
// Build a render array with the entity in a sub-element so that lazy
|
||||
// builder elements bubble up outside of the entity and we can check that
|
||||
// it got the correct cache max age.
|
||||
$build = ['#type' => 'container'];
|
||||
$build['entity'] = \Drupal::entityManager()
|
||||
->getViewBuilder('entity_test')
|
||||
->view($commented_entity);
|
||||
$renderer->renderRoot($build);
|
||||
|
||||
// The entity itself was cached but the top-level element is max-age 0 due
|
||||
// to the bubbled up max age due to the lazy-built comment form.
|
||||
$this->assertIdentical(Cache::PERMANENT, $build['entity']['#cache']['max-age']);
|
||||
$this->assertIdentical(0, $build['#cache']['max-age'], 'Top level render array has max-age 0');
|
||||
|
||||
// The children (fields) of the entity render array are only built in case
|
||||
// of a cache miss.
|
||||
$this->assertFalse(isset($build['entity']['comment']), 'Cache hit');
|
||||
}
|
||||
|
||||
}
|
315
core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php
Normal file
315
core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php
Normal file
|
@ -0,0 +1,315 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Session\AnonymousUserSession;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\simpletest\TestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests comment field level access.
|
||||
*
|
||||
* @group comment
|
||||
* @group Access
|
||||
*/
|
||||
class CommentFieldAccessTest extends EntityKernelTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('comment', 'entity_test', 'user');
|
||||
|
||||
/**
|
||||
* Fields that only users with administer comments permissions can change.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $administrativeFields = array(
|
||||
'uid',
|
||||
'status',
|
||||
'created',
|
||||
);
|
||||
|
||||
/**
|
||||
* These fields are automatically managed and can not be changed by any user.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $readOnlyFields = array(
|
||||
'changed',
|
||||
'hostname',
|
||||
'cid',
|
||||
'thread',
|
||||
);
|
||||
|
||||
/**
|
||||
* These fields can be edited on create only.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $createOnlyFields = [
|
||||
'uuid',
|
||||
'pid',
|
||||
'comment_type',
|
||||
'entity_id',
|
||||
'entity_type',
|
||||
'field_name',
|
||||
];
|
||||
|
||||
/**
|
||||
* These fields can only be edited by the admin or anonymous users if allowed.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $contactFields = array(
|
||||
'name',
|
||||
'mail',
|
||||
'homepage',
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(array('user', 'comment'));
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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' => 'tonythemagicalpony@example.com',
|
||||
'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',
|
||||
// Unpublished.
|
||||
'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',
|
||||
// Unpublished.
|
||||
'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'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
65
core/modules/comment/tests/src/Kernel/CommentItemTest.php
Normal file
65
core/modules/comment/tests/src/Kernel/CommentItemTest.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the new entity API for the comment field type.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentItemTest extends FieldKernelTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['comment', 'entity_test', 'user'];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('comment', ['comment_entity_statistics']);
|
||||
$this->installConfig(['comment']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests using entity fields of the comment field type.
|
||||
*/
|
||||
public function testCommentItem() {
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
|
||||
|
||||
// Verify entity creation.
|
||||
$entity = EntityTest::create();
|
||||
$entity->name->value = $this->randomMachineName();
|
||||
$entity->save();
|
||||
|
||||
// Verify entity has been created properly.
|
||||
$id = $entity->id();
|
||||
$entity = entity_load('entity_test', $id, TRUE);
|
||||
$this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
|
||||
|
||||
// Test sample item generation.
|
||||
/** @var \Drupal\entity_test\Entity\EntityTest $entity */
|
||||
$entity = EntityTest::create();
|
||||
$entity->comment->generateSampleItems();
|
||||
$this->entityValidateAndSave($entity);
|
||||
$this->assertTrue(in_array($entity->get('comment')->status, [
|
||||
CommentItemInterface::HIDDEN,
|
||||
CommentItemInterface::CLOSED,
|
||||
CommentItemInterface::OPEN,
|
||||
]), 'Comment status value in defined range');
|
||||
|
||||
$mainProperty = $entity->comment[0]->mainPropertyName();
|
||||
$this->assertEqual('status', $mainProperty);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel;
|
||||
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Tests that comment fields cannot be added to entities with non-integer IDs.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentStringIdEntitiesTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array(
|
||||
'comment',
|
||||
'user',
|
||||
'field',
|
||||
'field_ui',
|
||||
'entity_test',
|
||||
'text',
|
||||
);
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('comment');
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
// Create the comment body field storage.
|
||||
$this->installConfig(array('field'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = FieldStorageConfig::create(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.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
201
core/modules/comment/tests/src/Kernel/CommentValidationTest.php
Normal file
201
core/modules/comment/tests/src/Kernel/CommentValidationTest.php
Normal file
|
@ -0,0 +1,201 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* Tests comment validation constraints.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentValidationTest extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('comment', 'node');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the comment validation constraints.
|
||||
*/
|
||||
public function testValidation() {
|
||||
// Add a user.
|
||||
$user = User::create(array('name' => 'test', 'status' => TRUE));
|
||||
$user->save();
|
||||
|
||||
// Add comment type.
|
||||
$this->entityManager->getStorage('comment_type')->create(array(
|
||||
'id' => 'comment',
|
||||
'label' => 'comment',
|
||||
'target_entity_type_id' => 'node',
|
||||
))->save();
|
||||
|
||||
// Add comment field to content.
|
||||
$this->entityManager->getStorage('field_storage_config')->create(array(
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'type' => 'comment',
|
||||
'settings' => array(
|
||||
'comment_type' => 'comment',
|
||||
)
|
||||
))->save();
|
||||
|
||||
// Create a page node type.
|
||||
$this->entityManager->getStorage('node_type')->create(array(
|
||||
'type' => 'page',
|
||||
'name' => 'page',
|
||||
))->save();
|
||||
|
||||
// Add comment field to page content.
|
||||
/** @var \Drupal\field\FieldConfigInterface $field */
|
||||
$field = $this->entityManager->getStorage('field_config')->create(array(
|
||||
'field_name' => 'comment',
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'label' => 'Comment settings',
|
||||
));
|
||||
$field->save();
|
||||
|
||||
$node = $this->entityManager->getStorage('node')->create(array(
|
||||
'type' => 'page',
|
||||
'title' => 'test',
|
||||
));
|
||||
$node->save();
|
||||
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
));
|
||||
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 0, 'No violations when validating a default comment.');
|
||||
|
||||
$comment->set('subject', $this->randomString(65));
|
||||
$this->assertLengthViolation($comment, 'subject', 64);
|
||||
|
||||
// Make the subject valid.
|
||||
$comment->set('subject', $this->randomString());
|
||||
$comment->set('name', $this->randomString(61));
|
||||
$this->assertLengthViolation($comment, 'name', 60);
|
||||
|
||||
// Validate a name collision between an anonymous comment author name and an
|
||||
// existing user account name.
|
||||
$comment->set('name', 'test');
|
||||
$comment->set('uid', 0);
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, "Violation found on author name collision");
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), "name");
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The name you used (%name) belongs to a registered user.', array('%name' => 'test')));
|
||||
|
||||
// Make the name valid.
|
||||
$comment->set('name', 'valid unused name');
|
||||
$comment->set('mail', 'invalid');
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when email is invalid');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.'));
|
||||
|
||||
$comment->set('mail', NULL);
|
||||
$comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237));
|
||||
$this->assertLengthViolation($comment, 'homepage', 255);
|
||||
|
||||
$comment->set('homepage', 'invalid');
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when homepage is invalid');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'homepage.0.value');
|
||||
|
||||
// @todo This message should be improved in
|
||||
// https://www.drupal.org/node/2012690.
|
||||
$this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.'));
|
||||
|
||||
$comment->set('homepage', NULL);
|
||||
$comment->set('hostname', $this->randomString(129));
|
||||
$this->assertLengthViolation($comment, 'hostname', 128);
|
||||
|
||||
$comment->set('hostname', NULL);
|
||||
$comment->set('thread', $this->randomString(256));
|
||||
$this->assertLengthViolation($comment, 'thread', 255);
|
||||
|
||||
$comment->set('thread', NULL);
|
||||
|
||||
// Force anonymous users to enter contact details.
|
||||
$field->setSetting('anonymous', COMMENT_ANONYMOUS_MUST_CONTACT);
|
||||
$field->save();
|
||||
// Reset the node entity.
|
||||
\Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]);
|
||||
$node = Node::load($node->id());
|
||||
// Create a new comment with the new field.
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'uid' => 0,
|
||||
'name' => '',
|
||||
));
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when name is required, but empty and UID is anonymous.');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('You have to specify a valid author.'));
|
||||
|
||||
// Test creating a default comment with a given user id works.
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'uid' => $user->id(),
|
||||
));
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 0, 'No violations when validating a default comment with an author.');
|
||||
|
||||
// Test specifying a wrong author name does not work.
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'uid' => $user->id(),
|
||||
'name' => 'not-test',
|
||||
));
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when author name and comment author do not match.');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The specified author name does not match the comment author.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that a length violation exists for the given field.
|
||||
*
|
||||
* @param \Drupal\comment\CommentInterface $comment
|
||||
* The comment object to validate.
|
||||
* @param string $field_name
|
||||
* The field that violates the maximum length.
|
||||
* @param int $length
|
||||
* Number of characters that was exceeded.
|
||||
*/
|
||||
protected function assertLengthViolation(CommentInterface $comment, $field_name, $length) {
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, "Violation found when $field_name is too long.");
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
|
||||
$field_label = $comment->get($field_name)->getFieldDefinition()->getLabel();
|
||||
$this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => $length)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate;
|
||||
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\MigrateDrupalTestBase;
|
||||
use Drupal\migrate_drupal\Tests\StubTestTrait;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
|
||||
/**
|
||||
* Test stub creation for comment entities.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentStubTest extends MigrateDrupalTestBase {
|
||||
|
||||
use StubTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment', 'node'];
|
||||
|
||||
/**
|
||||
* {@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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creation of comment stubs.
|
||||
*/
|
||||
public function testStub() {
|
||||
try {
|
||||
// We expect an exception, because there's no node to reference.
|
||||
$this->performStubTest('comment');
|
||||
$this->fail('Expected exception has not been thrown.');
|
||||
}
|
||||
catch (MigrateException $e) {
|
||||
$this->assertIdentical($e->getMessage(),
|
||||
'Stubbing failed, unable to generate value for field entity_id');
|
||||
}
|
||||
|
||||
// The stub should pass when there's a node to point to.
|
||||
$this->createStub('node');
|
||||
$this->performStubTest('comment');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade comments.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateCommentTest extends MigrateDrupal6TestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('node');
|
||||
$this->installEntitySchema('comment');
|
||||
$this->installSchema('comment', ['comment_entity_statistics']);
|
||||
$this->installConfig(['comment']);
|
||||
|
||||
// The entity.node.canonical route must exist when the RDF hook is called.
|
||||
$this->container->get('router.builder')->rebuild();
|
||||
|
||||
$this->migrateContent();
|
||||
$this->executeMigrations([
|
||||
'd6_node',
|
||||
'd6_comment_type',
|
||||
'd6_comment_field',
|
||||
'd6_comment_field_instance',
|
||||
'd6_comment_entity_display',
|
||||
'd6_comment_entity_form_display',
|
||||
'd6_comment',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the Drupal 6 to Drupal 8 comment migration.
|
||||
*/
|
||||
public function testComments() {
|
||||
/** @var \Drupal\Core\Entity\EntityStorageInterface $comment_storage */
|
||||
$comment_storage = $this->container->get('entity.manager')->getStorage('comment');
|
||||
/** @var \Drupal\comment\CommentInterface $comment */
|
||||
$comment = $comment_storage->load(1);
|
||||
$this->assertIdentical('The first comment.', $comment->getSubject());
|
||||
$this->assertIdentical('The first comment body.', $comment->comment_body->value);
|
||||
$this->assertIdentical('filtered_html', $comment->comment_body->format);
|
||||
$this->assertIdentical('0', $comment->pid->target_id);
|
||||
$this->assertIdentical('1', $comment->getCommentedEntityId());
|
||||
$this->assertIdentical('node', $comment->getCommentedEntityTypeId());
|
||||
$this->assertIdentical('en', $comment->language()->getId());
|
||||
$this->assertIdentical('comment_no_subject', $comment->getTypeId());
|
||||
|
||||
$comment = $comment_storage->load(2);
|
||||
$this->assertIdentical('The response to the second comment.', $comment->subject->value);
|
||||
$this->assertIdentical('3', $comment->pid->target_id);
|
||||
|
||||
$comment = $comment_storage->load(3);
|
||||
$this->assertIdentical('The second comment.', $comment->subject->value);
|
||||
$this->assertIdentical('0', $comment->pid->target_id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade comment type.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateCommentTypeTest extends MigrateDrupal6TestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('node');
|
||||
$this->installEntitySchema('comment');
|
||||
$this->installConfig(['node', 'comment']);
|
||||
$this->executeMigration('d6_comment_type');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Base class for Drupal 6 comment variables to Drupal 8 entity display tests.
|
||||
*/
|
||||
abstract class MigrateCommentVariableDisplayBase extends MigrateDrupal6TestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(['comment']);
|
||||
$this->migrateContentTypes();
|
||||
$this->executeMigrations([
|
||||
'd6_comment_type',
|
||||
'd6_comment_field',
|
||||
'd6_comment_field_instance',
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
|
||||
/**
|
||||
* Upgrade comment variables to entity.display.node.*.default.yml.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateCommentVariableEntityDisplayTest extends MigrateCommentVariableDisplayBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->executeMigration('d6_comment_entity_display');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment variables migrated into an entity display.
|
||||
*/
|
||||
public function testCommentEntityDisplay() {
|
||||
foreach (['page', 'story', 'article'] as $type) {
|
||||
$component = EntityViewDisplay::load('node.' . $type . '.default')->getComponent('comment');
|
||||
$this->assertIdentical('hidden', $component['label']);
|
||||
$this->assertIdentical('comment_default', $component['type']);
|
||||
$this->assertIdentical(20, $component['weight']);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade comment subject variable to core.entity_form_display.comment.*.default.yml
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateCommentVariableEntityFormDisplaySubjectTest extends MigrateDrupal6TestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(['comment']);
|
||||
$this->executeMigrations([
|
||||
'd6_comment_type',
|
||||
'd6_comment_entity_form_display_subject',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment subject variable migrated into an entity display.
|
||||
*/
|
||||
public function testCommentEntityFormDisplay() {
|
||||
$component = EntityFormDisplay::load('comment.comment.default')
|
||||
->getComponent('subject');
|
||||
$this->assertIdentical('string_textfield', $component['type']);
|
||||
$this->assertIdentical(10, $component['weight']);
|
||||
$component = EntityFormDisplay::load('comment.comment_no_subject.default')
|
||||
->getComponent('subject');
|
||||
$this->assertNull($component);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
|
||||
/**
|
||||
* Upgrade comment variables to core.entity_form_display.node.*.default.yml.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateCommentVariableEntityFormDisplayTest extends MigrateCommentVariableDisplayBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->executeMigration('d6_comment_entity_form_display');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment variables migrated into an entity display.
|
||||
*/
|
||||
public function testCommentEntityFormDisplay() {
|
||||
foreach (['page', 'article', 'story'] as $type) {
|
||||
$component = EntityFormDisplay::load('node.' . $type . '.default')
|
||||
->getComponent('comment');
|
||||
$this->assertIdentical('comment_default', $component['type']);
|
||||
$this->assertIdentical(20, $component['weight']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade comment variables to field.storage.node.comment.yml.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateCommentVariableFieldTest extends MigrateDrupal6TestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(['comment']);
|
||||
$this->migrateContentTypes();
|
||||
$this->executeMigrations(['d6_comment_type', 'd6_comment_field']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment variables migrated into a field entity.
|
||||
*/
|
||||
public function testCommentField() {
|
||||
$this->assertTrue(is_object(FieldStorageConfig::load('node.comment')));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
|
||||
/**
|
||||
* Upgrade comment variables to field.instance.node.*.comment.yml.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateCommentVariableInstanceTest extends MigrateDrupal6TestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(['comment']);
|
||||
$this->migrateContentTypes();
|
||||
$this->executeMigrations([
|
||||
'd6_comment_type',
|
||||
'd6_comment_field',
|
||||
'd6_comment_field_instance',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the migrated field instance values.
|
||||
*/
|
||||
public function testCommentFieldInstance() {
|
||||
$node = Node::create(['type' => 'page']);
|
||||
$this->assertIdentical(0, $node->comment->status);
|
||||
$this->assertIdentical('comment', $node->comment->getFieldDefinition()->getName());
|
||||
$settings = $node->comment->getFieldDefinition()->getSettings();
|
||||
$this->assertIdentical(CommentManagerInterface::COMMENT_MODE_THREADED, $settings['default_mode']);
|
||||
$this->assertIdentical(50, $settings['per_page']);
|
||||
$this->assertFalse($settings['anonymous']);
|
||||
$this->assertFalse($settings['form_location']);
|
||||
$this->assertTrue($settings['preview']);
|
||||
|
||||
$node = Node::create(['type' => 'story']);
|
||||
$this->assertIdentical(2, $node->comment_no_subject->status);
|
||||
$this->assertIdentical('comment_no_subject', $node->comment_no_subject->getFieldDefinition()->getName());
|
||||
$settings = $node->comment_no_subject->getFieldDefinition()->getSettings();
|
||||
$this->assertIdentical(CommentManagerInterface::COMMENT_MODE_FLAT, $settings['default_mode']);
|
||||
$this->assertIdentical(70, $settings['per_page']);
|
||||
$this->assertTrue($settings['anonymous']);
|
||||
$this->assertFalse($settings['form_location']);
|
||||
$this->assertFalse($settings['preview']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Tests migration of comment display configuration.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentEntityDisplayTest extends MigrateDrupal7TestBase {
|
||||
|
||||
public static $modules = ['node', 'comment', 'text'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigrations([
|
||||
'd7_node_type',
|
||||
'd7_comment_type',
|
||||
'd7_comment_field',
|
||||
'd7_comment_field_instance',
|
||||
'd7_comment_entity_display',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts a display entity.
|
||||
*
|
||||
* @param string $id
|
||||
* The entity ID.
|
||||
* @param string $component_id
|
||||
* The ID of the display component.
|
||||
*/
|
||||
protected function assertDisplay($id, $component_id) {
|
||||
$component = EntityViewDisplay::load($id)->getComponent($component_id);
|
||||
$this->assertTrue(is_array($component));
|
||||
$this->assertIdentical('hidden', $component['label']);
|
||||
$this->assertIdentical('comment_default', $component['type']);
|
||||
$this->assertIdentical(20, $component['weight']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the migrated display configuration.
|
||||
*/
|
||||
public function testMigration() {
|
||||
$this->assertDisplay('node.page.default', 'comment_node_page');
|
||||
$this->assertDisplay('node.article.default', 'comment_node_article');
|
||||
$this->assertDisplay('node.book.default', 'comment_node_book');
|
||||
$this->assertDisplay('node.blog.default', 'comment_node_blog');
|
||||
$this->assertDisplay('node.forum.default', 'comment_node_forum');
|
||||
$this->assertDisplay('node.test_content_type.default', 'comment_node_test_content_type');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Tests migration of comment form's subject display configuration.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentEntityFormDisplaySubjectTest extends MigrateDrupal7TestBase {
|
||||
|
||||
public static $modules = ['node', 'comment', 'text'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigrations([
|
||||
'd7_node_type',
|
||||
'd7_comment_type',
|
||||
'd7_comment_entity_form_display_subject',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts a display entity.
|
||||
*
|
||||
* @param string $id
|
||||
* The entity ID.
|
||||
*/
|
||||
protected function assertDisplay($id) {
|
||||
$component = EntityFormDisplay::load($id)->getComponent('subject');
|
||||
$this->assertTrue(is_array($component));
|
||||
$this->assertIdentical('string_textfield', $component['type']);
|
||||
$this->assertIdentical(10, $component['weight']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the migrated display configuration.
|
||||
*/
|
||||
public function testMigration() {
|
||||
$this->assertDisplay('comment.comment_node_page.default');
|
||||
$this->assertDisplay('comment.comment_node_article.default');
|
||||
$this->assertDisplay('comment.comment_node_book.default');
|
||||
$this->assertDisplay('comment.comment_node_blog.default');
|
||||
$this->assertDisplay('comment.comment_node_forum.default');
|
||||
$this->assertDisplay('comment.comment_node_test_content_type.default');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Tests migration of comment form display configuration.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentEntityFormDisplayTest extends MigrateDrupal7TestBase {
|
||||
|
||||
public static $modules = ['node', 'comment', 'text'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigrations([
|
||||
'd7_node_type',
|
||||
'd7_comment_type',
|
||||
'd7_comment_field',
|
||||
'd7_comment_field_instance',
|
||||
'd7_comment_entity_form_display',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts a display entity.
|
||||
*
|
||||
* @param string $id
|
||||
* The entity ID.
|
||||
* @param string $component
|
||||
* The ID of the form component.
|
||||
*/
|
||||
protected function assertDisplay($id, $component_id) {
|
||||
$component = EntityFormDisplay::load($id)->getComponent($component_id);
|
||||
$this->assertTrue(is_array($component));
|
||||
$this->assertIdentical('comment_default', $component['type']);
|
||||
$this->assertIdentical(20, $component['weight']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the migrated display configuration.
|
||||
*/
|
||||
public function testMigration() {
|
||||
$this->assertDisplay('node.page.default', 'comment');
|
||||
$this->assertDisplay('node.article.default', 'comment');
|
||||
$this->assertDisplay('node.book.default', 'comment');
|
||||
$this->assertDisplay('node.blog.default', 'comment');
|
||||
$this->assertDisplay('node.forum.default', 'comment');
|
||||
$this->assertDisplay('node.test_content_type.default', 'comment');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\Core\Field\FieldConfigInterface;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Tests creation of comment reference fields for each comment type defined
|
||||
* in Drupal 7.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentFieldInstanceTest extends MigrateDrupal7TestBase {
|
||||
|
||||
public static $modules = ['node', 'comment', 'text'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigrations([
|
||||
'd7_node_type',
|
||||
'd7_comment_type',
|
||||
'd7_comment_field',
|
||||
'd7_comment_field_instance',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts a comment field entity.
|
||||
*
|
||||
* @param string $id
|
||||
* The entity ID.
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param string $bundle
|
||||
* The bundle ID.
|
||||
* @param int $default_mode
|
||||
* The field's default_mode setting.
|
||||
* @param int $per_page
|
||||
* The field's per_page setting.
|
||||
* @param bool $anonymous
|
||||
* The field's anonymous setting.
|
||||
* @param int $form_location
|
||||
* The field's form_location setting.
|
||||
* @param bool $preview
|
||||
* The field's preview setting.
|
||||
*/
|
||||
protected function assertEntity($id, $field_name, $bundle, $default_mode, $per_page, $anonymous, $form_location, $preview) {
|
||||
$entity = FieldConfig::load($id);
|
||||
$this->assertTrue($entity instanceof FieldConfigInterface);
|
||||
/** @var \Drupal\field\FieldConfigInterface $entity */
|
||||
$this->assertIdentical('node', $entity->getTargetEntityTypeId());
|
||||
$this->assertIdentical('Comments', $entity->label());
|
||||
$this->assertTrue($entity->isRequired());
|
||||
$this->assertIdentical($field_name, $entity->getFieldStorageDefinition()->getName());
|
||||
$this->assertIdentical($bundle, $entity->getTargetBundle());
|
||||
$this->assertTrue($entity->get('default_value')[0]['status']);
|
||||
$this->assertEqual($default_mode, $entity->getSetting('default_mode'));
|
||||
$this->assertIdentical($per_page, $entity->getSetting('per_page'));
|
||||
$this->assertEqual($anonymous, $entity->getSetting('anonymous'));
|
||||
// This assertion fails because 1 !== TRUE. It's extremely strange that
|
||||
// the form_location setting is returning a boolean, but this appears to
|
||||
// be a problem with the entity, not with the migration.
|
||||
// $this->asserIdentical($form_location, $entity->getSetting('form_location'));
|
||||
$this->assertEqual($preview, $entity->getSetting('preview'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the migrated fields.
|
||||
*/
|
||||
public function testMigration() {
|
||||
$this->assertEntity('node.page.comment_node_page', 'comment_node_page', 'page', TRUE, 50, FALSE, CommentItemInterface::FORM_BELOW, TRUE);
|
||||
$this->assertEntity('node.article.comment_node_article', 'comment_node_article', 'article', TRUE, 50, FALSE, CommentItemInterface::FORM_BELOW, TRUE);
|
||||
$this->assertEntity('node.blog.comment_node_blog', 'comment_node_blog', 'blog', TRUE, 50, FALSE, CommentItemInterface::FORM_BELOW, TRUE);
|
||||
$this->assertEntity('node.book.comment_node_book', 'comment_node_book', 'book', TRUE, 50, FALSE, CommentItemInterface::FORM_BELOW, TRUE);
|
||||
$this->assertEntity('node.forum.comment_node_forum', 'comment_node_forum', 'forum', TRUE, 50, FALSE, CommentItemInterface::FORM_BELOW, TRUE);
|
||||
$this->assertEntity('node.test_content_type.comment_node_test_content_type', 'comment_node_test_content_type', 'test_content_type', TRUE, 30, FALSE, CommentItemInterface::FORM_BELOW, TRUE);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\field\FieldStorageConfigInterface;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Tests creation of comment reference fields for each comment type defined
|
||||
* in Drupal 7.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentFieldTest extends MigrateDrupal7TestBase {
|
||||
|
||||
public static $modules = ['node', 'comment', 'text'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigrations([
|
||||
'd7_node_type',
|
||||
'd7_comment_type',
|
||||
'd7_comment_field',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts a comment field entity.
|
||||
*
|
||||
* @param string $id
|
||||
* The entity ID.
|
||||
* @param string $comment_type
|
||||
* The comment type (bundle ID) the field references.
|
||||
*/
|
||||
protected function assertEntity($id, $comment_type) {
|
||||
$entity = FieldStorageConfig::load($id);
|
||||
$this->assertTrue($entity instanceof FieldStorageConfigInterface);
|
||||
/** @var \Drupal\field\FieldStorageConfigInterface $entity */
|
||||
$this->assertIdentical('node', $entity->getTargetEntityTypeId());
|
||||
$this->assertIdentical('comment', $entity->getType());
|
||||
$this->assertIdentical($comment_type, $entity->getSetting('comment_type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the migrated fields.
|
||||
*/
|
||||
public function testMigration() {
|
||||
$this->assertEntity('node.comment_node_page', 'comment_node_page');
|
||||
$this->assertEntity('node.comment_node_article', 'comment_node_article');
|
||||
$this->assertEntity('node.comment_node_blog', 'comment_node_blog');
|
||||
$this->assertEntity('node.comment_node_book', 'comment_node_book');
|
||||
$this->assertEntity('node.comment_node_forum', 'comment_node_forum');
|
||||
$this->assertEntity('node.comment_node_test_content_type', 'comment_node_test_content_type');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
/**
|
||||
* Tests migration of comments from Drupal 7.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentTest extends MigrateDrupal7TestBase {
|
||||
|
||||
public static $modules = ['filter', 'node', 'comment', 'text'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installConfig(static::$modules);
|
||||
$this->installEntitySchema('node');
|
||||
$this->installEntitySchema('comment');
|
||||
|
||||
$this->executeMigrations([
|
||||
'd7_filter_format',
|
||||
'd7_user_role',
|
||||
'd7_user',
|
||||
]);
|
||||
$this->executeMigration('d7_node_type');
|
||||
// We only need the test_content_type node migration to run for real, so
|
||||
// mock all the others.
|
||||
$this->prepareMigrations(array(
|
||||
'd7_node' => array(
|
||||
array(array(0), array(0)),
|
||||
),
|
||||
));
|
||||
$this->executeMigrations([
|
||||
'd7_node:test_content_type',
|
||||
'd7_comment_type',
|
||||
'd7_comment',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests migration of comments from Drupal 7.
|
||||
*/
|
||||
public function testCommentMigration() {
|
||||
$comment = Comment::load(1);
|
||||
$this->assertTrue($comment instanceof CommentInterface);
|
||||
/** @var \Drupal\comment\CommentInterface $comment */
|
||||
$this->assertIdentical('A comment', $comment->getSubject());
|
||||
$this->assertIdentical('1421727536', $comment->getCreatedTime());
|
||||
$this->assertIdentical('1421727536', $comment->getChangedTime());
|
||||
$this->assertTrue($comment->getStatus());
|
||||
$this->assertIdentical('admin', $comment->getAuthorName());
|
||||
$this->assertIdentical('admin@local.host', $comment->getAuthorEmail());
|
||||
$this->assertIdentical('This is a comment', $comment->comment_body->value);
|
||||
$this->assertIdentical('filtered_html', $comment->comment_body->format);
|
||||
|
||||
$node = $comment->getCommentedEntity();
|
||||
$this->assertTrue($node instanceof NodeInterface);
|
||||
$this->assertIdentical('1', $node->id());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\comment\CommentTypeInterface;
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Tests migration of comment types from Drupal 7.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class MigrateCommentTypeTest extends MigrateDrupal7TestBase {
|
||||
|
||||
public static $modules = ['node', 'comment', 'text'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigrations([
|
||||
'd7_node_type',
|
||||
'd7_comment_type',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the migrated comment types.
|
||||
*/
|
||||
public function testMigration() {
|
||||
$this->assertEntity('comment_node_page', 'Basic page comment');
|
||||
$this->assertEntity('comment_node_article', 'Article comment');
|
||||
$this->assertEntity('comment_node_blog', 'Blog entry comment');
|
||||
$this->assertEntity('comment_node_book', 'Book page comment');
|
||||
$this->assertEntity('comment_node_forum', 'Forum topic comment');
|
||||
$this->assertEntity('comment_node_test_content_type', 'Test content type comment');
|
||||
|
||||
$migration = $this->getMigration('d7_comment_type');
|
||||
// Validate that the source count and processed count match up.
|
||||
$this->assertIdentical($migration->getSourcePlugin()->count(), $migration->getIdMap()->processedCount());
|
||||
}
|
||||
|
||||
}
|
175
core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php
Normal file
175
core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php
Normal file
|
@ -0,0 +1,175 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Views;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\Core\Session\AnonymousUserSession;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests the comment link field handlers.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentLinksTest extends CommentViewsKernelTestBase {
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = ['test_comment'];
|
||||
|
||||
/**
|
||||
* Test the comment approve link.
|
||||
*/
|
||||
public function testLinkApprove() {
|
||||
|
||||
// Create an unapproved comment.
|
||||
$comment = $this->commentStorage->create([
|
||||
'uid' => $this->adminUser->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
'status' => 0,
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$view = Views::getView('test_comment');
|
||||
$view->setDisplay();
|
||||
|
||||
$view->displayHandlers->get('default')->overrideOption('fields', [
|
||||
'approve_comment' => [
|
||||
'table' => 'comment',
|
||||
'field' => 'approve_comment',
|
||||
'id' => 'approve_comment',
|
||||
'plugin_id' => 'comment_link_approve',
|
||||
],
|
||||
]);
|
||||
$view->save();
|
||||
|
||||
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
|
||||
$account_switcher = \Drupal::service('account_switcher');
|
||||
$account_switcher->switchTo($this->adminUser);
|
||||
|
||||
$view->preview();
|
||||
|
||||
// Check if I can see the comment approve link on an unapproved comment.
|
||||
$approve_comment = $view->style_plugin->getField(0, 'approve_comment');
|
||||
$options = ['query' => ['destination' => '/']];
|
||||
$url = Url::fromRoute('comment.approve', ['comment' => $comment->id()], $options);
|
||||
$this->assertEqual(\Drupal::l('Approve', $url), (string) $approve_comment, 'Found a comment approve link for an unapproved comment.');
|
||||
|
||||
// Approve the comment.
|
||||
$comment->setPublished(CommentInterface::PUBLISHED);
|
||||
$comment->save();
|
||||
$view = Views::getView('test_comment');
|
||||
$view->preview();
|
||||
|
||||
// Check if I can see the comment approve link on an approved comment.
|
||||
$approve_comment = $view->style_plugin->getField(1, 'approve_comment');
|
||||
$this->assertFalse((string) $approve_comment, "Didn't find a comment approve link for an already approved comment.");
|
||||
|
||||
// Check if I can see the comment approve link on an approved comment as an
|
||||
// anonymous user.
|
||||
$account_switcher->switchTo(new AnonymousUserSession());
|
||||
// Set the comment as unpublished again.
|
||||
$comment->setPublished(CommentInterface::NOT_PUBLISHED);
|
||||
$comment->save();
|
||||
|
||||
$view = Views::getView('test_comment');
|
||||
$view->preview();
|
||||
$replyto_comment = $view->style_plugin->getField(0, 'approve_comment');
|
||||
$this->assertFalse((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the comment reply link.
|
||||
*/
|
||||
public function testLinkReply() {
|
||||
$this->enableModules(['field', 'entity_test']);
|
||||
$this->installEntitySchema('entity_test');
|
||||
$this->installSchema('comment', ['comment_entity_statistics']);
|
||||
$this->installConfig(['field']);
|
||||
|
||||
$field_storage_comment = FieldStorageConfig::create([
|
||||
'field_name' => 'comment',
|
||||
'type' => 'comment',
|
||||
'entity_type' => 'entity_test',
|
||||
]);
|
||||
$field_storage_comment->save();
|
||||
// Create a comment field which allows threading.
|
||||
$field_comment = FieldConfig::create([
|
||||
'field_name' => 'comment',
|
||||
'entity_type' => 'entity_test',
|
||||
'bundle' => 'entity_test',
|
||||
'settings' => [
|
||||
'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
|
||||
],
|
||||
]);
|
||||
$field_comment->save();
|
||||
|
||||
$host = EntityTest::create(['name' => $this->randomString()]);
|
||||
$host->save();
|
||||
// Attach an unapproved comment to the test entity.
|
||||
$comment = $this->commentStorage->create([
|
||||
'uid' => $this->adminUser->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'entity_id' => $host->id(),
|
||||
'comment_type' => 'entity_test',
|
||||
'field_name' => $field_storage_comment->getName(),
|
||||
'status' => 0,
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$view = Views::getView('test_comment');
|
||||
$view->setDisplay();
|
||||
|
||||
$view->displayHandlers->get('default')->overrideOption('fields', [
|
||||
'replyto_comment' => [
|
||||
'table' => 'comment',
|
||||
'field' => 'replyto_comment',
|
||||
'id' => 'replyto_comment',
|
||||
'plugin_id' => 'comment_link_reply',
|
||||
'entity_type' => 'comment',
|
||||
],
|
||||
]);
|
||||
$view->save();
|
||||
|
||||
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
|
||||
$account_switcher = \Drupal::service('account_switcher');
|
||||
$account_switcher->switchTo($this->adminUser);
|
||||
$view->preview();
|
||||
|
||||
// Check if I can see the reply link on an unapproved comment.
|
||||
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
|
||||
$this->assertFalse((string) $replyto_comment, "I can't reply to an unapproved comment.");
|
||||
|
||||
// Approve the comment.
|
||||
$comment->setPublished(CommentInterface::PUBLISHED);
|
||||
$comment->save();
|
||||
$view = Views::getView('test_comment');
|
||||
$view->preview();
|
||||
|
||||
// Check if I can see the reply link on an approved comment.
|
||||
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
|
||||
$url = Url::fromRoute('comment.reply', [
|
||||
'entity_type' => 'entity_test',
|
||||
'entity' => $host->id(),
|
||||
'field_name' => 'comment',
|
||||
'pid' => $comment->id(),
|
||||
]);
|
||||
$this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
|
||||
|
||||
// Check if I can see the reply link as an anonymous user.
|
||||
$account_switcher->switchTo(new AnonymousUserSession());
|
||||
$view = Views::getView('test_comment');
|
||||
$view->preview();
|
||||
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
|
||||
$this->assertFalse((string) $replyto_comment, "Didn't find the comment reply link as an anonymous user.");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Views;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\Core\Session\AnonymousUserSession;
|
||||
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\views\Entity\View;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests comment user name field
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentUserNameTest extends ViewsKernelTestBase {
|
||||
|
||||
/**
|
||||
* Admin user.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['user', 'comment', 'entity_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('comment');
|
||||
// Create the anonymous role.
|
||||
$this->installConfig(['user']);
|
||||
|
||||
// Create an anonymous user.
|
||||
$storage = \Drupal::entityManager()->getStorage('user');
|
||||
// Insert a row for the anonymous user.
|
||||
$storage
|
||||
->create(array(
|
||||
'uid' => 0,
|
||||
'name' => '',
|
||||
'status' => 0,
|
||||
))
|
||||
->save();
|
||||
|
||||
$admin_role = Role::create([
|
||||
'id' => 'admin',
|
||||
'permissions' => ['administer comments', 'access user profiles'],
|
||||
]);
|
||||
$admin_role->save();
|
||||
|
||||
/* @var \Drupal\user\RoleInterface $anonymous_role */
|
||||
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
|
||||
$anonymous_role->grantPermission('access comments');
|
||||
$anonymous_role->save();
|
||||
|
||||
$this->adminUser = User::create([
|
||||
'name' => $this->randomMachineName(),
|
||||
'roles' => [$admin_role->id()],
|
||||
]);
|
||||
$this->adminUser->save();
|
||||
|
||||
// Create some comments.
|
||||
$comment = Comment::create([
|
||||
'subject' => 'My comment title',
|
||||
'uid' => $this->adminUser->id(),
|
||||
'name' => $this->adminUser->label(),
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
'status' => 1,
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$comment_anonymous = Comment::create([
|
||||
'subject' => 'Anonymous comment title',
|
||||
'uid' => 0,
|
||||
'name' => 'barry',
|
||||
'mail' => 'test@example.com',
|
||||
'homepage' => 'https://example.com',
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
'created' => 123456,
|
||||
'status' => 1,
|
||||
]);
|
||||
$comment_anonymous->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the username formatter.
|
||||
*/
|
||||
public function testUsername() {
|
||||
$view_id = $this->randomMachineName();
|
||||
$view = View::create([
|
||||
'id' => $view_id,
|
||||
'base_table' => 'comment_field_data',
|
||||
'display' => [
|
||||
'default' => [
|
||||
'display_plugin' => 'default',
|
||||
'id' => 'default',
|
||||
'display_options' => [
|
||||
'fields' => [
|
||||
'name' => [
|
||||
'table' => 'comment_field_data',
|
||||
'field' => 'name',
|
||||
'id' => 'name',
|
||||
'plugin_id' => 'field',
|
||||
'type' => 'comment_username'
|
||||
],
|
||||
'subject' => [
|
||||
'table' => 'comment_field_data',
|
||||
'field' => 'subject',
|
||||
'id' => 'subject',
|
||||
'plugin_id' => 'field',
|
||||
'type' => 'string',
|
||||
'settings' => [
|
||||
'link_to_entity' => TRUE,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
$view->save();
|
||||
|
||||
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
|
||||
$account_switcher = \Drupal::service('account_switcher');
|
||||
|
||||
/* @var \Drupal\Core\Render\RendererInterface $renderer */
|
||||
$renderer = \Drupal::service('renderer');
|
||||
|
||||
$account_switcher->switchTo($this->adminUser);
|
||||
$executable = Views::getView($view_id);
|
||||
$build = $executable->preview();
|
||||
$this->setRawContent($renderer->renderRoot($build));
|
||||
$this->verbose($this->getRawContent());
|
||||
|
||||
$this->assertLink('My comment title');
|
||||
$this->assertLink('Anonymous comment title');
|
||||
$this->assertLink($this->adminUser->label());
|
||||
$this->assertLink('barry (not verified)');
|
||||
|
||||
$account_switcher->switchTo(new AnonymousUserSession());
|
||||
$executable = Views::getView($view_id);
|
||||
$executable->storage->invalidateCaches();
|
||||
|
||||
$build = $executable->preview();
|
||||
$this->setRawContent($renderer->renderRoot($build));
|
||||
|
||||
// No access to user-profiles, so shouldn't be able to see links.
|
||||
$this->assertNoLink($this->adminUser->label());
|
||||
// Note: External users aren't pointing to drupal user profiles.
|
||||
$this->assertLink('barry (not verified)');
|
||||
$this->verbose($this->getRawContent());
|
||||
$this->assertLink('My comment title');
|
||||
$this->assertLink('Anonymous comment title');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Views;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\Tests\views\Kernel\Handler\FieldFieldAccessTestBase;
|
||||
|
||||
/**
|
||||
* Tests base field access in Views for the comment entity.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentViewsFieldAccessTest extends FieldFieldAccessTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment', 'entity_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
$this->installEntitySchema('comment');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check access for comment fields.
|
||||
*/
|
||||
public function testCommentFields() {
|
||||
$user = User::create([
|
||||
'name' => 'test user',
|
||||
]);
|
||||
$user->save();
|
||||
|
||||
$comment = Comment::create([
|
||||
'subject' => 'My comment title',
|
||||
'uid' => $user->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$comment_anonymous = Comment::create([
|
||||
'subject' => 'Anonymous comment title',
|
||||
'uid' => 0,
|
||||
'name' => 'anonymous',
|
||||
'mail' => 'test@example.com',
|
||||
'homepage' => 'https://example.com',
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
'created' => 123456,
|
||||
'status' => 1,
|
||||
]);
|
||||
$comment_anonymous->save();
|
||||
|
||||
// @todo Expand the test coverage in https://www.drupal.org/node/2464635
|
||||
|
||||
$this->assertFieldAccess('comment', 'cid', $comment->id());
|
||||
$this->assertFieldAccess('comment', 'cid', $comment_anonymous->id());
|
||||
$this->assertFieldAccess('comment', 'uuid', $comment->uuid());
|
||||
$this->assertFieldAccess('comment', 'subject', 'My comment title');
|
||||
$this->assertFieldAccess('comment', 'subject', 'Anonymous comment title');
|
||||
$this->assertFieldAccess('comment', 'name', 'anonymous');
|
||||
$this->assertFieldAccess('comment', 'mail', 'test@example.com');
|
||||
$this->assertFieldAccess('comment', 'homepage', 'https://example.com');
|
||||
$this->assertFieldAccess('comment', 'uid', $user->getUsername());
|
||||
// $this->assertFieldAccess('comment', 'created', \Drupal::service('date.formatter')->format(123456));
|
||||
// $this->assertFieldAccess('comment', 'changed', \Drupal::service('date.formatter')->format(REQUEST_TIME));
|
||||
$this->assertFieldAccess('comment', 'status', 'On');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\comment\Kernel\Views;
|
||||
|
||||
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\views\Tests\ViewTestData;
|
||||
|
||||
/**
|
||||
* Provides a common test base for comment views tests.
|
||||
*/
|
||||
abstract class CommentViewsKernelTestBase extends ViewsKernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['comment_test_views', 'user', 'comment'];
|
||||
|
||||
/**
|
||||
* Admin user.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* The entity storage for comments.
|
||||
*
|
||||
* @var \Drupal\comment\CommentStorageInterface
|
||||
*/
|
||||
protected $commentStorage;
|
||||
|
||||
/**
|
||||
* The entity storage for users.
|
||||
*
|
||||
* @var \Drupal\user\UserStorageInterface
|
||||
*/
|
||||
protected $userStorage;
|
||||
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
ViewTestData::createTestViews(get_class($this), ['comment_test_views']);
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('comment');
|
||||
$this->installConfig(['user']);
|
||||
|
||||
$entity_manager = $this->container->get('entity.manager');
|
||||
$this->commentStorage = $entity_manager->getStorage('comment');
|
||||
$this->userStorage = $entity_manager->getStorage('user');
|
||||
|
||||
// Insert a row for the anonymous user.
|
||||
$this->userStorage
|
||||
->create([
|
||||
'uid' => 0,
|
||||
'name' => '',
|
||||
'status' => 0,
|
||||
])
|
||||
->save();
|
||||
|
||||
$admin_role = Role::create(['id' => 'admin']);
|
||||
$admin_role->grantPermission('administer comments');
|
||||
$admin_role->save();
|
||||
|
||||
/* @var \Drupal\user\RoleInterface $anonymous_role */
|
||||
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
|
||||
$anonymous_role->grantPermission('access comments');
|
||||
$anonymous_role->save();
|
||||
|
||||
$this->adminUser = $this->userStorage->create(['name' => $this->randomMachineName()]);
|
||||
$this->adminUser->addRole('admin');
|
||||
$this->adminUser->save();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\CommentLinkBuilderTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit {
|
||||
|
||||
use Drupal\comment\CommentLinkBuilder;
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\CommentManagerTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit;
|
||||
|
||||
use Drupal\comment\CommentManager;
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\CommentStatisticsUnitTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit;
|
||||
|
||||
use Drupal\comment\CommentStatistics;
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Entity\CommentLockTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Entity;
|
||||
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Migrate\d6\CommentSourceWithHighWaterTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Migrate\d6;
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Migrate\d6\CommentTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Migrate\d6;
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Migrate\d6\CommentTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Migrate\d6;
|
||||
|
||||
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Migrate\d6\CommentVariablePerCommentTypeTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Migrate\d6;
|
||||
|
||||
use Drupal\comment\Plugin\migrate\source\d6\CommentVariablePerCommentType;
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Migrate\d6\CommentVariableTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Migrate\d6;
|
||||
|
||||
use Drupal\comment\Plugin\migrate\source\d6\CommentVariable;
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Migrate\d7\CommentTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Migrate\d7;
|
||||
|
||||
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\comment\Unit\Migrate\d7\CommentTypeTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\comment\Unit\Migrate\d7;
|
||||
|
||||
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;
|
||||
|
|
Reference in a new issue