Unverified Commit 17c9135b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3024460 by quietone, Gábor Hojtsy, masipila, heddn: Migrate D7 comment...

Issue #3024460 by quietone, Gábor Hojtsy, masipila, heddn: Migrate D7 comment type language settings

(cherry picked from commit f3216900)
parent 20ebfa43
Loading
Loading
Loading
Loading
+61 −0
Changes for core/modules/content_translation/migrations/d7_language_content_comment_settings.yml: 61 added lines, 0 removed lines.
Original line number Diff line number Diff line
id: d7_language_content_comment_settings
label: Drupal 7 language content comment settings
migration_tags:
  - Drupal 7
  - Configuration
  - Multilingual
source:
  plugin: d7_language_content_settings
  constants:
    target_type: comment
    id_prefix: comment_node_
    label_suffix: comment
process:
  target_bundle:
    -
      plugin: concat
      source:
        - constants/id_prefix
        - type
    -
      plugin: static_map
      bypass: true
      # The Forum module provides its own comment type (comment_forum), which we
      # want to reuse if it exists.
      map:
        comment_node_forum: comment_forum
  target_entity_type_id: constants/target_type
  default_langcode:
    -
      plugin: static_map
      source: language_content_type
      map:
        0: site_default
        1: current_interface
        2: current_interface
        4: current_interface
      default_value: NULL
    -
      plugin: skip_on_empty
      method: row
  language_alterable:
    plugin: static_map
    source: language_content_type
    map:
      0: false
      1: true
      2: true
      4: true
  third_party_settings/content_translation/enabled:
    plugin: content_translation_enabled_setting
    source:
      - language_content_type
      - entity_translation_entity_types
      - constants/target_type
destination:
  plugin: entity:language_content_settings
  content_translation_update_definitions:
    - comment
migration_dependencies:
  required:
    - d7_comment_type
+65 −0
Changes for core/modules/language/src/Plugin/migrate/process/ContentTranslationEnabledSetting.php: 65 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\language\Plugin\migrate\process;

use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * Determines the content translation setting.
 *
 * The source value is an indexed array of three values:
 * - The language content type, e.g. '1'
 * - The entity_translation_entity_types, an array of entity types.
 * - An entity type used with entity translation, e.g. comment.
 *
 * @MigrateProcessPlugin(
 *   id = "content_translation_enabled_setting"
 * )
 */
class ContentTranslationEnabledSetting extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (!is_array($value)) {
      throw new MigrateException('Input should be an array');
    }

    list($language_content_type, $entity_translation_entity_types, $entity_type) = $value;

    switch ($language_content_type) {
      // In the case of being 0, it will be skipped. We are not actually setting
      // a null value.
      case 0;
        $setting = NULL;
        break;

      case 1:
        $setting = FALSE;
        break;

      case 2:
        $setting = FALSE;
        break;

      case 4:
        // If entity translation is enabled return the status of comment
        // translations.
        $setting = FALSE;
        if (!empty($entity_translation_entity_types[$entity_type])) {
          $setting = TRUE;
        }
        break;

      default:
        $setting = NULL;
        break;
    }
    return $setting;
  }

}
+4 −0
Changes for core/modules/language/src/Plugin/migrate/source/d7/LanguageContentSettings.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -50,6 +50,10 @@ public function prepareRow(Row $row) {
    else {
      $row->setSourceProperty('i18n_lock_node', 0);
    }

    // Get the entity translation entity settings.
    $entity_translation_entity_types = $this->variableGet('entity_translation_entity_types', NULL);
    $row->setSourceProperty('entity_translation_entity_types', $entity_translation_entity_types);
    return parent::prepareRow($row);
  }

+116 −0
Changes for core/modules/language/tests/src/Kernel/Migrate/d7/MigrateLanguageContentCommentSettingsNoEntityTranslationTest.php: 116 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\language\Kernel\Migrate\d7;

use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;

/**
 * Tests language content comment settings migration with no entity translation.
 *
 * @group migrate_drupal_7
 */
class MigrateLanguageContentCommentSettingsNoEntityTranslationTest extends MigrateDrupal7TestBase implements MigrateDumpAlterInterface {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'comment',
    'content_translation',
    'language',
    'node',
    'text',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->migrateCommentTypes();
    $this->executeMigration('d7_language_content_comment_settings');
  }

  /**
   * {@inheritdoc}
   */
  public static function migrateDumpAlter(KernelTestBase $test) {
    // Disable comment entity translation.
    $db = Database::getConnection('default', 'migrate');

    $db->update('variable')
      ->condition('name', 'entity_translation_entity_types')
      ->fields([
        'value' => 'a:4:{s:4:"node";s:4:"node";s:13:"taxonomy_term";i:0;s:7:"comment";i:0;s:4:"user";i:0;}',
      ])
      ->execute();
  }

  /**
   * Tests migration of content language settings.
   */
  public function testLanguageCommentSettings() {
    // Article and Blog content type have multilingual settings of 'Enabled,
    // with Translation'. Assert that comments are translatable and the default
    // language is 'current_interface'.
    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_article');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_article', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $third_party_settings = [
      'content_translation' => [
        'enabled' => FALSE,
      ],
    ];
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_blog');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_blog', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    // Page content type has multilingual settings of 'Enabled'. Assert that
    // comments are translatable and default language is 'current_interface'.
    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_page');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_page', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    // Test content type has multilingual settings of 'Enabled, with field
    // translation'.
    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_test_content_type');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_test_content_type', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $third_party_settings = [
      'content_translation' => [
        'enabled' => FALSE,
      ],
    ];
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    // Assert that non-translatable content is not translatable and the default
    // language is 'site_default.
    $not_translatable = [
      'comment_node_book',
      'comment_forum',
    ];
    foreach ($not_translatable as $bundle) {
      $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', $bundle);
      $this->assertTrue($config->isDefaultConfiguration());
      $this->assertFalse($config->isLanguageAlterable());
      $this->assertSame('site_default', $config->getDefaultLangcode(), "Default language is not 'site_default' for comment bundle $bundle");
    }
  }

}
+98 −0
Changes for core/modules/language/tests/src/Kernel/Migrate/d7/MigrateLanguageContentCommentSettingsTest.php: 98 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\language\Kernel\Migrate\d7;

use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;

/**
 * Tests migration of language comment settings.
 *
 * @group migrate_drupal_7
 */
class MigrateLanguageContentCommentSettingsTest extends MigrateDrupal7TestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'comment',
    'content_translation',
    'language',
    'node',
    'text',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->migrateCommentTypes();
    $this->executeMigration('d7_language_content_comment_settings');
  }

  /**
   * Tests migration of content language settings.
   */
  public function testLanguageCommentSettings() {
    // Article and Blog content type have multilingual settings of 'Enabled,
    // with Translation'. Assert that comments are translatable and the default
    // language is 'current_interface'.
    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_article');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_article', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $third_party_settings = [
      'content_translation' => [
        'enabled' => FALSE,
      ],
    ];
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_blog');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_blog', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    // Page content type has multilingual settings of 'Enabled'. Assert that
    // comments are translatable and default language is 'current_interface'.
    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_page');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_page', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    // Test content type has multilingual settings of 'Enabled, with field
    // translation'.
    $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment_node_test_content_type');
    $this->assertSame('comment', $config->getTargetEntityTypeId());
    $this->assertSame('comment_node_test_content_type', $config->getTargetBundle());
    $this->assertSame('current_interface', $config->getDefaultLangcode());
    $this->assertTrue($config->isLanguageAlterable());
    $third_party_settings = [
      'content_translation' => [
        'enabled' => TRUE,
      ],
    ];
    $this->assertSame($third_party_settings, $config->get('third_party_settings'));

    // Assert that non-translatable content is not translatable and the default
    // language is 'site_default.
    $not_translatable = [
      'comment_node_book',
      'comment_forum',
    ];
    foreach ($not_translatable as $bundle) {
      $config = ContentLanguageSettings::loadByEntityTypeBundle('comment', $bundle);
      $this->assertTrue($config->isDefaultConfiguration());
      $this->assertFalse($config->isLanguageAlterable());
      $this->assertSame('site_default', $config->getDefaultLangcode(), "Default language is not 'site_default' for comment bundle $bundle");
    }
  }

}
Loading