Commit 2d714fbe authored by alexpott's avatar alexpott

Issue #2357199 by andypost, naiduharish: Consider...

Issue #2357199 by andypost, naiduharish: Consider CommentManagerInterface::addDefaultField() as deprecated and remove in favour of CommentTestTrait
parent f1766f96
......@@ -113,99 +113,6 @@ public function getFields($entity_type_id) {
return isset($map[$entity_type_id]) ? $map[$entity_type_id] : array();
}
/**
* {@inheritdoc}
*/
public function addDefaultField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment') {
// Create the comment type if needed.
$comment_type_storage = $this->entityManager->getStorage('comment_type');
if ($comment_type = $comment_type_storage->load($comment_type_id)) {
if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
throw new \InvalidArgumentException(String::format('The given comment type id %id can only be used with the %entity_type entity type', array(
'%id' => $comment_type_id,
'%entity_type' => $entity_type,
)));
}
}
else {
$comment_type_storage->create(array(
'id' => $comment_type_id,
'label' => Unicode::ucfirst($comment_type_id),
'target_entity_type_id' => $entity_type,
'description' => 'Default comment field',
))->save();
}
// Add a body field to the comment type.
$this->addBodyField($comment_type_id);
// Add a comment field to the host entity type. Create the field storage if
// needed.
if (!array_key_exists($field_name, $this->entityManager->getFieldStorageDefinitions($entity_type))) {
$this->entityManager->getStorage('field_storage_config')->create(array(
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'comment',
'translatable' => TRUE,
'settings' => array(
'comment_type' => $comment_type_id,
),
))->save();
}
// Create the field if needed, and configure its form and view displays.
if (!array_key_exists($field_name, $this->entityManager->getFieldDefinitions($entity_type, $bundle))) {
$this->entityManager->getStorage('field_config')->create(array(
'label' => 'Comments',
'description' => '',
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'required' => 1,
'default_value' => array(
array(
'status' => $default_value,
'cid' => 0,
'last_comment_name' => '',
'last_comment_timestamp' => 0,
'last_comment_uid' => 0,
),
),
))->save();
// Entity form displays: assign widget settings for the 'default' form
// mode, and hide the field in all other form modes.
entity_get_form_display($entity_type, $bundle, 'default')
->setComponent($field_name, array(
'type' => 'comment_default',
'weight' => 20,
))
->save();
foreach ($this->entityManager->getFormModes($entity_type) as $id => $form_mode) {
$display = entity_get_form_display($entity_type, $bundle, $id);
// Only update existing displays.
if ($display && !$display->isNew()) {
$display->removeComponent($field_name)->save();
}
}
// Entity view displays: assign widget settings for the 'default' view
// mode, and hide the field in all other view modes.
entity_get_display($entity_type, $bundle, 'default')
->setComponent($field_name, array(
'label' => 'above',
'type' => 'comment_default',
'weight' => 20,
))
->save();
foreach ($this->entityManager->getViewModes($entity_type) as $id => $view_mode) {
$display = entity_get_display($entity_type, $bundle, $id);
// Only update existing displays.
if ($display && !$display->isNew()) {
$display->removeComponent($field_name)->save();
}
}
}
}
/**
* {@inheritdoc}
*/
......
......@@ -43,28 +43,6 @@ interface CommentManagerInterface {
*/
public function getFields($entity_type_id);
/**
* Utility method to add the default comment field to an entity.
*
* Attaches a comment field named 'comment' to the given entity type and
* bundle. Largely replicates the default behavior in Drupal 7 and earlier.
*
* @param string $entity_type
* The entity type to attach the default comment field to.
* @param string $bundle
* The bundle to attach the default comment field to.
* @param string $field_name
* (optional) Field name to use for the comment field. Defaults to
* 'comment'.
* @param int $default_value
* (optional) Default value, one of CommentItemInterface::HIDDEN,
* CommentItemInterface::OPEN, CommentItemInterface::CLOSED. Defaults to
* CommentItemInterface::OPEN.
* @param string $comment_type_id
* (optional) ID of comment type to use. Defaults to 'comment'.
*/
public function addDefaultField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment');
/**
* Creates a comment_body field.
*
......
......@@ -17,6 +17,8 @@
*/
class CommentBookTest extends WebTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -28,7 +30,7 @@ protected function setUp() {
parent::setUp();
// Create comment field on book.
\Drupal::service('comment.manager')->addDefaultField('node', 'book');
$this->addDefaultCommentField('node', 'book');
}
/**
......
......@@ -20,6 +20,8 @@
*/
class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
use CommentTestTrait;
/**
* {@inheritdoc}
*/
......@@ -47,7 +49,7 @@ protected function createEntity() {
entity_test_create_bundle($bundle, NULL, 'entity_test');
// Create a comment field on this bundle.
\Drupal::service('comment.manager')->addDefaultField('entity_test', 'bar', 'comment');
$this->addDefaultCommentField('entity_test', 'bar', 'comment');
// Display comments in a flat list; threaded comments are not render cached.
$field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
......
......@@ -19,6 +19,8 @@
*/
class CommentDefaultFormatterCacheTagsTest extends EntityUnitTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -48,7 +50,7 @@ protected function setUp() {
// Set up a field, so that the entity that'll be referenced bubbles up a
// cache tag when rendering it entirely.
\Drupal::service('comment.manager')->addDefaultField('entity_test', 'entity_test');
$this->addDefaultCommentField('entity_test', 'entity_test');
}
/**
......
......@@ -24,6 +24,8 @@
*/
class CommentFieldAccessTest extends EntityUnitTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -126,11 +128,9 @@ public function testAccessToAdministrativeFields() {
$anonymous_user = new AnonymousUserSession();
/** @var \Drupal\comment\CommentManagerInterface $manager */
$manager = \Drupal::service('comment.manager');
// Add two fields.
$manager->addDefaultField('entity_test', 'entity_test', 'comment');
$manager->addDefaultField('entity_test', 'entity_test', 'comment_other');
$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');
......
......@@ -32,7 +32,7 @@ function testCommentDefaultFields() {
// Do not make assumptions on default node types created by the test
// installation profile, and create our own.
$this->drupalCreateContentType(array('type' => 'test_node_type'));
$this->container->get('comment.manager')->addDefaultField('node', 'test_node_type');
$this->addDefaultCommentField('node', 'test_node_type');
// Check that the 'comment_body' field is present on the comment bundle.
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
......@@ -48,7 +48,7 @@ function testCommentDefaultFields() {
// Create a new content type.
$type_name = 'test_node_type_2';
$this->drupalCreateContentType(array('type' => $type_name));
$this->container->get('comment.manager')->addDefaultField('node', $type_name);
$this->addDefaultCommentField('node', $type_name);
// Check that the 'comment_body' field exists and has an instance on the
// new comment bundle.
......@@ -58,7 +58,7 @@ function testCommentDefaultFields() {
$this->assertTrue(isset($field), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
// Test adding a field that defaults to CommentItemInterface::CLOSED.
$this->container->get('comment.manager')->addDefaultField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
$this->addDefaultCommentField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
$field = FieldConfig::load('node.test_node_type.who_likes_ponies');
$this->assertEqual($field->default_value[0]['status'], CommentItemInterface::CLOSED);
}
......@@ -68,11 +68,11 @@ function testCommentDefaultFields() {
*/
public function testCommentFieldDelete() {
$this->drupalCreateContentType(array('type' => 'test_node_type'));
$this->container->get('comment.manager')->addDefaultField('node', 'test_node_type');
$this->addDefaultCommentField('node', 'test_node_type');
// We want to test the handling of removing the primary comment field, so we
// ensure there is at least one other comment field attached to a node type
// so that comment_entity_load() runs for nodes.
$this->container->get('comment.manager')->addDefaultField('node', 'test_node_type', 'comment2');
$this->addDefaultCommentField('node', 'test_node_type', 'comment2');
// Create a sample node.
$node = $this->drupalCreateNode(array(
......@@ -132,7 +132,7 @@ function testCommentInstallAfterContentModule() {
$this->assertTrue($this->container->get('module_handler')->moduleExists('comment'), 'Comment module enabled.');
// Create nodes of each type.
$this->container->get('comment.manager')->addDefaultField('node', 'book');
$this->addDefaultCommentField('node', 'book');
$book_node = $this->drupalCreateNode(array('type' => 'book'));
$this->drupalLogout();
......
......@@ -19,6 +19,8 @@
*/
class CommentLanguageTest extends WebTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -67,7 +69,7 @@ protected function setUp() {
$this->drupalPostForm("user/" . $admin_user->id() . "/edit", $edit, t('Save'));
// Create comment field on article.
$this->container->get('comment.manager')->addDefaultField('node', 'article');
$this->addDefaultCommentField('node', 'article');
// Make comment body translatable.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
......
......@@ -24,6 +24,7 @@
class CommentNonNodeTest extends WebTestBase {
use FieldUiTestTrait;
use CommentTestTrait;
public static $modules = array('comment', 'user', 'field_ui', 'entity_test', 'block');
......@@ -50,7 +51,7 @@ protected function setUp() {
'target_entity_type_id' => 'entity_test',
))->save();
// Create comment field on entity_test bundle.
$this->container->get('comment.manager')->addDefaultField('entity_test', 'entity_test');
$this->addDefaultCommentField('entity_test', 'entity_test');
// Verify that bundles are defined correctly.
$bundles = \Drupal::entityManager()->getBundleInfo('comment');
......
......@@ -273,7 +273,7 @@ function testCommentNewPageIndicator() {
*/
function testTwoPagers() {
// Add another field to article content-type.
$this->container->get('comment.manager')->addDefaultField('node', 'article', 'comment_2');
$this->addDefaultCommentField('node', 'article', 'comment_2');
// Set default to display comment list with unique pager id.
entity_get_display('node', 'article', 'default')
->setComponent('comment_2', array(
......
......@@ -19,6 +19,8 @@
*/
abstract class CommentTestBase extends WebTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -79,7 +81,7 @@ protected function setUp() {
));
// Create comment field on article.
$this->container->get('comment.manager')->addDefaultField('node', 'article');
$this->addDefaultCommentField('node', 'article');
// Create a test node authored by the web user.
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()));
......
<?php
/**
* @file
* Contains \Drupal\comment\Tests\CommentTestTrait.
*/
namespace Drupal\comment\Tests;
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
/**
* Provides common functionality for the Comment test classes.
*/
trait CommentTestTrait {
/**
* Adds the default comment field to an entity.
*
* Attaches a comment field named 'comment' to the given entity type and
* bundle. Largely replicates the default behavior in Drupal 7 and earlier.
*
* @param string $entity_type
* The entity type to attach the default comment field to.
* @param string $bundle
* The bundle to attach the default comment field to.
* @param string $field_name
* (optional) Field name to use for the comment field. Defaults to
* 'comment'.
* @param int $default_value
* (optional) Default value, one of CommentItemInterface::HIDDEN,
* CommentItemInterface::OPEN, CommentItemInterface::CLOSED. Defaults to
* CommentItemInterface::OPEN.
* @param string $comment_type_id
* (optional) ID of comment type to use. Defaults to 'comment'.
*/
public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment') {
$entity_manager = \Drupal::entityManager();
// Create the comment type if needed.
$comment_type_storage = $entity_manager->getStorage('comment_type');
if ($comment_type = $comment_type_storage->load($comment_type_id)) {
if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
throw new \InvalidArgumentException(String::format('The given comment type id %id can only be used with the %entity_type entity type', array(
'%id' => $comment_type_id,
'%entity_type' => $entity_type,
)));
}
}
else {
$comment_type_storage->create(array(
'id' => $comment_type_id,
'label' => Unicode::ucfirst($comment_type_id),
'target_entity_type_id' => $entity_type,
'description' => 'Default comment field',
))->save();
}
// Add a body field to the comment type.
\Drupal::service('comment.manager')->addBodyField($comment_type_id);
// Add a comment field to the host entity type. Create the field storage if
// needed.
if (!array_key_exists($field_name, $entity_manager->getFieldStorageDefinitions($entity_type))) {
$entity_manager->getStorage('field_storage_config')->create(array(
'entity_type' => $entity_type,
'field_name' => $field_name,
'type' => 'comment',
'translatable' => TRUE,
'settings' => array(
'comment_type' => $comment_type_id,
),
))->save();
}
// Create the field if needed, and configure its form and view displays.
if (!array_key_exists($field_name, $entity_manager->getFieldDefinitions($entity_type, $bundle))) {
$entity_manager->getStorage('field_config')->create(array(
'label' => 'Comments',
'description' => '',
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'required' => 1,
'default_value' => array(
array(
'status' => $default_value,
'cid' => 0,
'last_comment_name' => '',
'last_comment_timestamp' => 0,
'last_comment_uid' => 0,
),
),
))->save();
// Entity form displays: assign widget settings for the 'default' form
// mode, and hide the field in all other form modes.
entity_get_form_display($entity_type, $bundle, 'default')
->setComponent($field_name, array(
'type' => 'comment_default',
'weight' => 20,
))
->save();
foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) {
$display = entity_get_form_display($entity_type, $bundle, $id);
// Only update existing displays.
if ($display && !$display->isNew()) {
$display->removeComponent($field_name)->save();
}
}
// Entity view displays: assign widget settings for the 'default' view
// mode, and hide the field in all other view modes.
entity_get_display($entity_type, $bundle, 'default')
->setComponent($field_name, array(
'label' => 'above',
'type' => 'comment_default',
'weight' => 20,
))
->save();
foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) {
$display = entity_get_display($entity_type, $bundle, $id);
// Only update existing displays.
if ($display && !$display->isNew()) {
$display->removeComponent($field_name)->save();
}
}
}
}
}
......@@ -18,6 +18,8 @@
*/
class CommentTranslationUITest extends ContentTranslationUITest {
use CommentTestTrait;
/**
* The subject of the test comment.
*/
......@@ -53,12 +55,12 @@ function setupBundle() {
parent::setupBundle();
$this->drupalCreateContentType(array('type' => $this->nodeBundle, 'name' => $this->nodeBundle));
// Add a comment field to the article content type.
$this->container->get('comment.manager')->addDefaultField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
$this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
// Create a page content type.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'page'));
// Add a comment field to the page content type - this one won't be
// translatable.
$this->container->get('comment.manager')->addDefaultField('node', 'page', 'comment');
$this->addDefaultCommentField('node', 'page', 'comment');
// Mark this bundle as translatable.
$this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
}
......
......@@ -127,7 +127,7 @@ 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');
$this->addDefaultCommentField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo');
$field_storage = FieldStorageConfig::loadByName('node', 'foo');
$this->drupalLogin($this->adminUser);
......@@ -176,7 +176,7 @@ public function testCommentTypeDeletion() {
// Test exception thrown when re-using an existing comment type.
try {
\Drupal::service('comment.manager')->addDefaultField('comment', 'comment', 'bar');
$this->addDefaultCommentField('comment', 'comment', 'bar');
$this->fail('Exception not thrown.');
}
catch (\InvalidArgumentException $e) {
......
......@@ -17,6 +17,8 @@
*/
class CommentUninstallTest extends WebTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -30,7 +32,7 @@ protected function setUp() {
// Create an article content type.
$this->drupalCreateContentType(array('type' => 'article', 'name' => t('Article')));
// Create comment field on article so that adds 'comment_body' field.
$this->container->get('comment.manager')->addDefaultField('node', 'article');
$this->addDefaultCommentField('node', 'article');
}
/**
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment\Tests\Views;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
......@@ -15,6 +16,8 @@
*/
abstract class CommentTestBase extends ViewTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -69,7 +72,7 @@ protected function setUp() {
$this->drupalLogin($this->account);
$this->drupalCreateContentType(array('type' => 'page', 'name' => t('Basic page')));
$this->container->get('comment.manager')->addDefaultField('node', 'page');
$this->addDefaultCommentField('node', 'page');
$this->nodeUserPosted = $this->drupalCreateNode();
$this->nodeUserCommented = $this->drupalCreateNode(array('uid' => $this->account2->id()));
......
......@@ -8,6 +8,7 @@
namespace Drupal\comment\Tests\Views;
use Drupal\comment\CommentInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestBase;
......@@ -18,6 +19,8 @@
*/
class DefaultViewRecentCommentsTest extends ViewTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -71,7 +74,7 @@ protected function setUp() {
'type' => $content_type->id(),
);
$this->container->get('comment.manager')->addDefaultField('node', $content_type->id());
$this->addDefaultCommentField('node', $content_type->id());
$this->node = $this->drupalCreateNode($node_data);
// Force a flush of the in-memory storage.
......
......@@ -7,6 +7,7 @@
namespace Drupal\comment\Tests\Views;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Views;
use Drupal\views\Tests\Wizard\WizardTestBase;
......@@ -18,6 +19,8 @@
*/
class WizardTest extends WizardTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
......@@ -33,7 +36,7 @@ protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(array('type' => 'page', 'name' => t('Basic page')));
// Add comment field to page node type.
$this->container->get('comment.manager')->addDefaultField('node', 'page');
$this->addDefaultCommentField('node', 'page');
}
/**
......
......@@ -8,6 +8,7 @@
namespace Drupal\content_translation\Tests;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\Core\Language\Language;
use Drupal\field\Entity\FieldConfig;
......@@ -21,6 +22,8 @@
*/
class ContentTranslationSettingsTest extends WebTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
......@@ -35,8 +38,8 @@ protected function setUp() {
// bundles.
$this->drupalCreateContentType(array('type' => 'article'));
$this->drupalCreateContentType(array('type' => 'page'));
$this->container->get('comment.manager')->addDefaultField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
$this->container->get('comment.manager')->addDefaultField('node', 'page', 'comment_page');
$this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
$this->addDefaultCommentField('node', 'page', 'comment_page');
$admin_user = $this->drupalCreateUser(array('access administration pages', 'administer languages', 'administer content translation', 'administer content types', 'administer node fields', 'administer comment fields', 'administer comments', 'administer comment types', 'administer account settings'));
$this->drupalLogin($admin_user);
......
......@@ -8,6 +8,7 @@
namespace Drupal\file\Tests;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Component\Utility\Html;
use Drupal\field\Entity\FieldConfig;
use Drupal\field_ui\Tests\FieldUiTestTrait;
......@@ -20,6 +21,7 @@
*/
class FileFieldWidgetTest extends FileFieldTestBase {
use CommentTestTrait;
use FieldUiTestTrait;
/**
......@@ -268,7 +270,7 @@ function testPrivateFileComment() {
user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('post comments', 'skip comment approval'));
// Create a new field.
$this->container->get('comment.manager')->addDefaultField('node', 'article');
$this->addDefaultCommentField('node', 'article');
$name = strtolower($this->randomMachineName());
$label = $this->randomMachineName();
......
......@@ -7,6 +7,7 @@
namespace Drupal\filter\Tests;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\simpletest\WebTestBase;
......@@ -17,6 +18,8 @@
*/
class FilterHtmlImageSecureTest extends WebTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
......@@ -68,7 +71,7 @@ protected function setUp() {
// Setup a node to comment and test on.
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
// Add a comment field.
$this->container->get('comment.manager')->addDefaultField('node', 'page');
$this->addDefaultCommentField('node', 'page');
$this->node = $this->drupalCreateNode();
}
......
......@@ -7,6 +7,8 @@
namespace Drupal\hal\Tests;
use Drupal\comment\Tests\CommentTestTrait;
/**
* Tests that nodes and terms are correctly normalized and denormalized.
*
......@@ -14,6 +16,8 @@
*/
class EntityTest extends NormalizerTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
......@@ -50,7 +54,7 @@ public function testNode() {
'target_entity_type_id' => 'node',
))->save();
$this->container->get('comment.manager')->addDefaultField('node', 'example_type');
$this->addDefaultCommentField('node', 'example_type');
$node = entity_create('node', array(
'title' => $this->randomMachineName(),
......@@ -146,7 +150,7 @@ public function testComment() {
'target_entity_type_id' => 'node',
))->save();