Commit 5fb22f43 authored by webchick's avatar webchick

Issue #2532534 by phenaproxima, miguelc303: Migration Files for Drupal 7 Comments

parent 3d9ba61e
......@@ -39,7 +39,7 @@ migration_dependencies:
required:
- d6_node:*
- d6_comment_type
- d6_user
- d6_comment_entity_display
- d6_comment_entity_form_display
- d6_user
- d6_filter_format
id: d7_comment
label: Drupal 7 comments
migration_tags:
- Drupal 7
source:
plugin: d7_comment
constants:
entity_type: node
process:
cid: cid
pid:
plugin: migration
migration: d7_comment
source: pid
entity_id: nid
entity_type: 'constants/entity_type'
comment_type: comment_type
field_name: comment_type
subject: subject
uid: uid
name: name
mail: mail
homepage: homepage
hostname: hostname
created: created
changed: changed
status: status
thread: thread
comment_body: comment_body
destination:
plugin: entity:comment
migration_dependencies:
required:
- d7_node:*
- d7_comment_type
id: d7_comment_entity_display
label: Drupal 7 comment display configuration
migration_tags:
- Drupal 7
source:
plugin: d7_comment_type
constants:
entity_type: node
view_mode: default
options:
label: hidden
type: comment_default
weight: 20
process:
entity_type: 'constants/entity_type'
field_name: bundle
view_mode: 'constants/view_mode'
options: 'constants/options'
bundle: node_type
destination:
plugin: component_entity_display
migration_dependencies:
required:
- d7_comment_field_instance
id: d7_comment_entity_form_display
label: Drupal 7 comment field form display configuration
migration_tags:
- Drupal 7
source:
plugin: d7_comment_type
constants:
entity_type: node
field_name: comment
form_mode: default
options:
type: comment_default
weight: 20
process:
entity_type: 'constants/entity_type'
field_name: 'constants/field_name'
form_mode: 'constants/form_mode'
options: 'constants/options'
bundle: node_type
destination:
plugin: component_entity_form_display
migration_dependencies:
required:
- d7_comment_field_instance
id: d7_comment_entity_form_display_subject
label: Drupal 7 comment subject form display configuration
migration_tags:
- Drupal 7
source:
plugin: d7_comment_type
constants:
entity_type: comment
field_name: subject
form_mode: default
options:
type: string_textfield
weight: 10
process:
entity_type: 'constants/entity_type'
field_name: 'constants/field_name'
form_mode: 'constants/form_mode'
options: 'constants/options'
hidden:
plugin: static_map
source: subject
map:
0: true
1: false
bundle: bundle
destination:
plugin: component_entity_form_display
migration_dependencies:
required:
- d7_comment_type
id: d7_comment_field
label: Drupal 7 comment field configuration
migration_tags:
- Drupal 7
source:
plugin: d7_comment_type
constants:
entity_type: node
type: comment
process:
entity_type: 'constants/entity_type'
field_name: bundle
type: 'constants/type'
'settings/comment_type': bundle
destination:
plugin: entity:field_storage_config
migration_dependencies:
required:
- d7_comment_type
id: d7_comment_field_instance
label: Drupal 7 comment field instance configuration
migration_tags:
- Drupal 7
source:
plugin: d7_comment_type
constants:
entity_type: node
label: Comments
required: true
process:
entity_type: 'constants/entity_type'
label: 'constants/label'
required: 'constants/required'
field_name: bundle
bundle: node_type
'default_value/0/status': 'constants/required'
'settings/default_mode': default_mode
'settings/per_page': per_page
'settings/anonymous': anonymous
'settings/form_location': form_location
'settings/preview': preview
destination:
plugin: entity:field_config
migration_dependencies:
required:
- d7_comment_field
id: d7_comment_type
label: Drupal 7 comment type
migration_tags:
- Drupal 7
source:
plugin: d7_comment_type
constants:
entity_type: node
process:
target_entity_type_id: 'constants/entity_type'
id: bundle
label: label
destination:
plugin: entity:comment_type
migration_dependencies:
required:
- d7_node_type
<?php
/**
* @file
* Contains \Drupal\comment\Plugin\migrate\source\d7\Comment.
*/
namespace Drupal\comment\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* Drupal 7 comment source from database.
*
* @MigrateSource(
* id = "d7_comment",
* source_provider = "comment"
* )
*/
class Comment extends FieldableEntity {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('comment', 'c')->fields('c');
$query->innerJoin('node', 'n', 'c.nid = n.nid');
$query->addField('n', 'type', 'node_type');
$query->orderBy('c.created');
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$cid = $row->getSourceProperty('cid');
$node_type = $row->getSourceProperty('node_type');
$comment_type = 'comment_node_' . $node_type;
$row->setSourceProperty('comment_type', 'comment_node_' . $node_type);
foreach (array_keys($this->getFields('comment', $comment_type)) as $field) {
$row->setSourceProperty($field, $this->getFieldValues('comment', $field, $cid));
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
return array(
'cid' => $this->t('Comment ID.'),
'pid' => $this->t('Parent comment ID. If set to 0, this comment is not a reply to an existing comment.'),
'nid' => $this->t('The {node}.nid to which this comment is a reply.'),
'uid' => $this->t('The {users}.uid who authored the comment. If set to 0, this comment was created by an anonymous user.'),
'subject' => $this->t('The comment title.'),
'comment' => $this->t('The comment body.'),
'hostname' => $this->t("The author's host name."),
'created' => $this->t('The time that the comment was created, as a Unix timestamp.'),
'changed' => $this->t('The time that the comment was edited by its author, as a Unix timestamp.'),
'status' => $this->t('The published status of a comment. (0 = Published, 1 = Not Published)'),
'format' => $this->t('The {filter_formats}.format of the comment body.'),
'thread' => $this->t("The vancode representation of the comment's place in a thread."),
'name' => $this->t("The comment author's name. Uses {users}.name if the user is logged in, otherwise uses the value typed into the comment form."),
'mail' => $this->t("The comment author's email address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
'homepage' => $this->t("The comment author's home page address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on."),
'type' => $this->t("The {node}.type to which this comment is a reply."),
);
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['cid']['type'] = 'integer';
return $ids;
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Plugin\migrate\source\d7\CommentType.
*/
namespace Drupal\comment\Plugin\migrate\source\d7;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
* Drupal 7 comment type source from database.
*
* @MigrateSource(
* id = "d7_comment_type",
* source_provider = "comment"
* )
*/
class CommentType extends DrupalSqlBase {
/**
* A map of D7 node types to their labels.
*
* @var string[]
*/
protected $nodeTypes = array();
/**
* {@inheritdoc}
*/
public function query() {
return $this->select('field_config_instance', 'fci')
->fields('fci', array('bundle'))
->condition('entity_type', 'comment');
}
/**
* {@inheritdoc}
*/
protected function initializeIterator() {
$this->nodeTypes = $this->select('node_type', 'nt')
->fields('nt', array('type', 'name'))
->execute()
->fetchAllKeyed();
return parent::initializeIterator();
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$node_type = substr($row->getSourceProperty('bundle'), 13);
$row->setSourceProperty('node_type', $node_type);
$row->setSourceProperty('default_mode', $this->variableGet("comment_default_mode_$node_type", 1));
$row->setSourceProperty('per_page', $this->variableGet("comment_default_per_page_$node_type", 50));
$row->setSourceProperty('anonymous', $this->variableGet("comment_anonymous_$node_type", FALSE));
$row->setSourceProperty('form_location', $this->variableGet("comment_form_location_$node_type", CommentItemInterface::FORM_BELOW));
$row->setSourceProperty('preview', $this->variableGet("comment_preview_$node_type", TRUE));
$row->setSourceProperty('subject', $this->variableGet("comment_subject_field_$node_type", TRUE));
$label = $this->t('@node_type comment', [
'@node_type' => $this->nodeTypes[$node_type],
]);
$row->setSourceProperty('label', $label);
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
return array(
'label' => $this->t('The label of the comment type.'),
'bundle' => $this->t('Bundle ID of the comment type.'),
'node_type' => $this->t('The node type to which this comment type is attached.'),
'default_mode' => $this->t('Default comment mode.'),
'per_page' => $this->t('Number of comments visible per page.'),
'anonymous' => $this->t('Whether anonymous comments are allowed.'),
'form_location' => $this->t('Location of the comment form.'),
'preview' => $this->t('Whether previews are enabled for the comment type.'),
'subject' => $this->t('Whether a subject field is enabled for the comment type.'),
);
}
/**
* {@inheritdoc}
*/
public function getIds() {
return array(
'bundle' => array(
'type' => 'string',
),
);
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Migrate\d7\MigrateCommentEntityDisplayTest.
*/
namespace Drupal\comment\Tests\Migrate\d7;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\migrate_drupal\Tests\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->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_field');
$this->executeMigration('d7_comment_field_instance');
$this->executeMigration('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');
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Migrate\d7\MigrateCommentEntityFormDisplaySubjectTest.
*/
namespace Drupal\comment\Tests\Migrate\d7;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\migrate_drupal\Tests\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->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('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');
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Migrate\d7\MigrateCommentEntityFormDisplayTest.
*/
namespace Drupal\comment\Tests\Migrate\d7;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\migrate_drupal\Tests\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->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_field');
$this->executeMigration('d7_comment_field_instance');
$this->executeMigration('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');
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Migrate\d7\MigrateCommentFieldInstanceTest.
*/
namespace Drupal\comment\Tests\Migrate\d7;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\migrate_drupal\Tests\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->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_field');
$this->executeMigration('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);
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Migrate\d7\MigrateCommentFieldTest.
*/
namespace Drupal\comment\Tests\Migrate\d7;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\migrate_drupal\Tests\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() {