From b84471234f04f0c75dd4082c0760bf9b5cfd0d78 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 17 Jan 2017 11:22:02 +0000
Subject: [PATCH] Issue #2673960 by Jo Fitzgerald, hussainweb, phenaproxima,
 mikeryan, quietone, vasi: Unable to migrate D7 User cck fields

---
 .../migrate_drupal/tests/fixtures/drupal7.php | 41 ++++++++++
 .../src/Tests/d7/MigrateUpgrade7Test.php      |  2 +-
 .../d7/files/sites/default/files/ds9.txt      | 79 +++++++++++++++++++
 .../user/migration_templates/d7_user.yml      |  1 +
 core/modules/user/src/Plugin/migrate/User.php | 27 ++++---
 .../src/Kernel/Migrate/d7/MigrateUserTest.php | 20 ++++-
 6 files changed, 156 insertions(+), 14 deletions(-)
 create mode 100644 core/modules/migrate_drupal_ui/src/Tests/d7/files/sites/default/files/ds9.txt

diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php
index 27d45250a447..a388f8e5a18c 100644
--- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php
+++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php
@@ -4485,6 +4485,18 @@
   'field_file_display' => '1',
   'field_file_description' => 'file desc',
 ))
+->values(array(
+  'entity_type' => 'user',
+  'bundle' => 'user',
+  'deleted' => '0',
+  'entity_id' => '2',
+  'revision_id' => '2',
+  'language' => 'und',
+  'delta' => '0',
+  'field_file_fid' => '2',
+  'field_file_display' => '1',
+  'field_file_description' => 'file desc',
+))
 ->execute();
 
 $connection->schema()->createTable('field_data_field_float', array(
@@ -6674,6 +6686,18 @@
   'field_file_display' => '1',
   'field_file_description' => 'file desc',
 ))
+->values(array(
+  'entity_type' => 'user',
+  'bundle' => 'user',
+  'deleted' => '0',
+  'entity_id' => '2',
+  'revision_id' => '2',
+  'language' => 'und',
+  'delta' => '0',
+  'field_file_fid' => '2',
+  'field_file_display' => '1',
+  'field_file_description' => 'file desc',
+))
 ->execute();
 
 $connection->schema()->createTable('field_revision_field_float', array(
@@ -8307,6 +8331,16 @@
   'status' => '1',
   'timestamp' => '1421727515',
 ))
+->values(array(
+  'fid' => '2',
+  'uid' => '1',
+  'filename' => 'ds9.txt',
+  'uri' => 'public://ds9.txt',
+  'filemime' => 'text/plain',
+  'filesize' => '4720',
+  'status' => '1',
+  'timestamp' => '1421727516',
+))
 ->execute();
 
 $connection->schema()->createTable('file_usage', array(
@@ -8375,6 +8409,13 @@
   'id' => '1',
   'count' => '1',
 ))
+->values(array(
+  'fid' => '2',
+  'module' => 'file',
+  'type' => 'user',
+  'id' => '2',
+  'count' => '1',
+))
 ->execute();
 
 $connection->schema()->createTable('filter', array(
diff --git a/core/modules/migrate_drupal_ui/src/Tests/d7/MigrateUpgrade7Test.php b/core/modules/migrate_drupal_ui/src/Tests/d7/MigrateUpgrade7Test.php
index 1a0e334fdee7..77bc2ab86f8c 100644
--- a/core/modules/migrate_drupal_ui/src/Tests/d7/MigrateUpgrade7Test.php
+++ b/core/modules/migrate_drupal_ui/src/Tests/d7/MigrateUpgrade7Test.php
@@ -45,7 +45,7 @@ protected function getEntityCounts() {
       'editor' => 2,
       'field_config' => 49,
       'field_storage_config' => 37,
-      'file' => 1,
+      'file' => 2,
       'filter_format' => 7,
       'image_style' => 6,
       'language_content_settings' => 2,
diff --git a/core/modules/migrate_drupal_ui/src/Tests/d7/files/sites/default/files/ds9.txt b/core/modules/migrate_drupal_ui/src/Tests/d7/files/sites/default/files/ds9.txt
new file mode 100644
index 000000000000..b49b7f69a81c
--- /dev/null
+++ b/core/modules/migrate_drupal_ui/src/Tests/d7/files/sites/default/files/ds9.txt
@@ -0,0 +1,79 @@
+                __                ___               ___
+              ,' ,'              |   |              `. `.
+            ,' ,'                |===|                `. `.
+           / //                  |___|                  \\ \
+          / //                   |___|                   \\ \
+         ////                    |___|                    \\\\
+        /  /                    ||   ||                    \  \
+       /  /                     ||   ||                     \  \
+      /| |                      ||   ||                      | |\
+      || |                     | : o : |                     | ||
+     |  \|                     | .===. |                     |/  |
+     |  |\                    /| (___) |\                    /|  |
+    |__||.\         .-.      // /,_._,\ \\      .-.         /.||__|
+    |__||_.\        `-.\    //_ [:(|):] _\\    /.-'        /._||__|
+ __/|  ||___`._____ ___\\__/___/_ ||| _\___\__//___ _____.'___||_ |\__
+/___//__________/.-/_____________|.-.|_____________\-.\__________\\___\
+\___\\__\\\_____\`-\__\\\\__\____|_-_|____/_//_____/-'/__//______//__//
+   \|__||__..'         //  \ _ \__|||__/ _ /  \\         `..__||__|/
+    |__||_./        .-'/    \\   |(|)|   //    \`-.        \..||__|
+    |  || /         `-'      \\   \'/   //      `-'         \ ||  |
+     |  |/                    \| :(-): |/                    \|  |
+     |  /|                     | : o : |                     |\  |
+      || |                     | |___| |                     | ||
+      \| |                      ||   ||                      | |/
+       \  \                     ||   ||                     /  /
+        \  \                    ||___||                    /  /
+         \\\\                    |___|                    ////
+          \ \\                   |___|                   // /
+           \ \\                  |   |                  // /
+            `. `.                |===|                ,' ,'
+              `._`.              |___|              ,'_,'
+
+
+                                _     _
+                       _____---' \_n_/ `---_____
+                  _   /  ...  -----------  ...  \   _
+                 ( )-' .  '::.\__  V  __/.::'  . `-( )
+             _  .-'  ':::.  ____ \   / ____  .:::'  `-.  _
+          ,-'.`'      __.--'    \ | | /    `--.__      `'.`-.
+         / ''::..     \          || ||          /     ..::'' \
+        /  .....  ,'\,'          ||_||          `./`.  .....  \
+       / :::::' ,'               |   |               `. '::::: \
+       | '::: ,'                 |   |                 `. :::' |
+      _/  :: /                   |___|                   \ ::  \_
+     (/     /                 ,-'     `-.                 \     \)
+    _/      `.             ,-' ooo    oo `-.             ,'      \_
+   |      /`./          ,-'\               /`-.          \.'\      |
+  |  .:  /             /  \ \_ __.---.__ _/ /  \             \  :.  |
+ .' :;: |           _ / o  \[ '  \   /  ` ]/    \ _           | ::: `.
+ | ':: |           ( `-.   /      | |      \   ,-' )           | ::' |
+ |: ': |            `-./  /       ___       \  \,-'            | :' :|
+.':: ' |           |     / `-. .-' . `-. .-' \   o |           | ' ::`.
+| ::. |           | o  ,| `-. / \`_|_'/ \ .-' |.  o |           | .:: |
+ \    |_          |____|     | ` /   \ ' |     |____|          _|    /
+ (|    _]         ]____[     |- ( (O) ) -|     ]____[         [_    |)
+ /.:  |           |    |     | . \_ _/ . |     |    |           |  :.\
+| :'  |           | o  `|-,-' \ /..|..\ / `-.-|'  o |           |. ': |
+`.  :: |           | o   \ .-' `-.___.-' `-. /   o |           | ::  .'
+ | .:: |          _.\     \|      | |      \/     /._          | ::. |
+ |  ': |      _.-'   \ o   \      | |      /   o /   `-._      | :'  |
+ `.  __ |__.-'_     _.\    /[_.__ | | __._]\  o /._     _`-.__| __  .'
+  | \  \_.--''.' .-'   \  / /    `---'    \ \  /   `-. `.``--__/  / |
+   |_\ __   ,',-'       `-./  o            \,-'       `-.`.   __ /_|
+     \\ / .','             `-. oo .-. oo ,-'             `.`. \ //
+     (\\  \ \                 `-._| |_,-'                 / /  //)
+       \\  ) \                    (_)                    / (  //
+       / \/   `.                                       ,'   \/ \
+       \  .::. `.                                     ,' .:::  /
+        \  ':::. `-./`.                         .'\.-' '''''' /
+         \    '''     /_           _           _\    ::..    /
+          `-.'::'       `--.______| |______.--'           ,-'
+             `-'`-._   ..                   .: .:   _,-'`'
+                  (_)-. ::. ''::::     ::::::  : ,-(_)
+                      \_____   '' _ _    ' _____/
+                            ---._/ u \_.---
+
+Used with permission from:
+Orbital Space Station (Terok Nor - Deep Space 9) - Joe Reiss
+https://startrekasciiart.blogspot.co.uk/2011/05/deep-space-nine.html
diff --git a/core/modules/user/migration_templates/d7_user.yml b/core/modules/user/migration_templates/d7_user.yml
index 3910659fa391..d68e2971e777 100644
--- a/core/modules/user/migration_templates/d7_user.yml
+++ b/core/modules/user/migration_templates/d7_user.yml
@@ -48,6 +48,7 @@ migration_dependencies:
   required:
     - d7_user_role
   optional:
+    - d7_field_instance
     - d7_file
     - language
     - default_language
diff --git a/core/modules/user/src/Plugin/migrate/User.php b/core/modules/user/src/Plugin/migrate/User.php
index 986b58d2fdff..d89787c583f4 100644
--- a/core/modules/user/src/Plugin/migrate/User.php
+++ b/core/modules/user/src/Plugin/migrate/User.php
@@ -3,19 +3,12 @@
 namespace Drupal\user\Plugin\migrate;
 
 use Drupal\migrate\Exception\RequirementsException;
-use Drupal\migrate\Plugin\Migration;
+use Drupal\migrate_drupal\Plugin\migrate\CckMigration;
 
 /**
  * Plugin class for Drupal 7 user migrations dealing with fields and profiles.
  */
-class User extends Migration {
-
-  /**
-   * Flag indicating whether the CCK data has been filled already.
-   *
-   * @var bool
-   */
-  protected $init = FALSE;
+class User extends CckMigration {
 
   /**
    * {@inheritdoc}
@@ -33,7 +26,21 @@ public function getProcess() {
         $field_migration = $this->migrationPluginManager->createStubMigration($definition);
         foreach ($field_migration->getSourcePlugin() as $row) {
           $field_name = $row->getSourceProperty('field_name');
-          $this->process[$field_name] = $field_name;
+          $field_type = $row->getSourceProperty('type');
+          if (empty($field_type)) {
+            continue;
+          }
+          if ($this->cckPluginManager->hasDefinition($field_type)) {
+            if (!isset($this->cckPluginCache[$field_type])) {
+              $this->cckPluginCache[$field_type] = $this->cckPluginManager->createInstance($field_type, [], $this);
+            }
+            $info = $row->getSource();
+            $this->cckPluginCache[$field_type]
+              ->processCckFieldValues($this, $field_name, $info);
+          }
+          else {
+            $this->process[$field_name] = $field_name;
+          }
         }
       }
       try {
diff --git a/core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php b/core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php
index c21856e5b870..d18d0711b1ef 100644
--- a/core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php
+++ b/core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserTest.php
@@ -109,10 +109,12 @@ protected function createType($id) {
    *   Role IDs the user account is expected to have.
    * @param int $field_integer
    *   The value of the integer field.
+   * @param int|false $field_file_target_id
+   *   (optional) The target ID of the file field.
    * @param bool $has_picture
-   *   Whether the user is expected to have a picture attached.
+   *   (optional) Whether the user is expected to have a picture attached.
    */
-  protected function assertEntity($id, $label, $mail, $password, $created, $access, $login, $blocked, $langcode, $timezone, $init, $roles, $field_integer, $has_picture = FALSE) {
+  protected function assertEntity($id, $label, $mail, $password, $created, $access, $login, $blocked, $langcode, $timezone, $init, $roles, $field_integer, $field_file_target_id = FALSE, $has_picture = FALSE) {
     /** @var \Drupal\user\UserInterface $user */
     $user = User::load($id);
     $this->assertTrue($user instanceof UserInterface);
@@ -155,6 +157,10 @@ protected function assertEntity($id, $label, $mail, $password, $created, $access
       $this->assertTrue($user->hasField('field_integer'));
       $this->assertEquals($field_integer[0], $user->field_integer->value);
     }
+    if (!empty($field_file_target_id)) {
+      $this->assertTrue($user->hasField('field_file'));
+      $this->assertSame($field_file_target_id, $user->field_file->target_id);
+    }
   }
 
   /**
@@ -190,6 +196,13 @@ public function testUser() {
         ->fetchCol();
       $field_integer = !empty($field_integer) ? $field_integer : NULL;
 
+      $field_file = Database::getConnection('default', 'migrate')
+        ->select('field_data_field_file', 'ff')
+        ->fields('ff', ['field_file_fid'])
+        ->condition('ff.entity_id', $source->uid)
+        ->execute()
+        ->fetchField();
+
       $this->assertEntity(
         $source->uid,
         $source->name,
@@ -203,7 +216,8 @@ public function testUser() {
         $source->timezone,
         $source->init,
         $roles,
-        $field_integer
+        $field_integer,
+        $field_file
       );
 
       // Ensure that the user can authenticate.
-- 
GitLab