MigrateTableTest.php 6.17 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\migrate_plus\Kernel;

use Drupal\migrate\MigrateExecutable;
6 7
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
8 9 10 11 12 13 14 15 16
use Drupal\Tests\migrate\Kernel\MigrateTestBase;

/**
 * Tests migration destination table.
 *
 * @group migrate
 */
class MigrateTableTest extends MigrateTestBase {

17 18
  const SOURCE_TABLE_NAME = 'migrate_test_source_table';
  const DEST_TABLE_NAME = 'migrate_test_destination_table';
19 20

  /**
21 22
   * The database connection.
   *
23 24 25 26
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

27 28 29 30 31 32 33
  /**
   * The batch size to configure (a size of 1 disables batching).
   *
   * @var int
   */
  protected $batchSize = 1;

34 35 36
  /**
   * {@inheritdoc}
   */
37 38
  public static $modules = ['migrate_plus'];

39 40 41
  /**
   * {@inheritdoc}
   */
42
  protected function setUp(): void {
43 44
    parent::setUp();

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    $this->connection = $this->container->get('database');
    $connections = [
      static::SOURCE_TABLE_NAME => $this->sourceDatabase,
      static::DEST_TABLE_NAME => $this->connection,
    ];
    foreach ($connections as $table => $connection) {
      $connection->schema()->createTable($table, [
        'description' => 'Test table',
        'fields' => [
          'data' => [
            'type' => 'varchar',
            'length' => '32',
            'not null' => TRUE,
          ],
          'data2' => [
            'type' => 'varchar',
            'length' => '32',
            'not null' => TRUE,
          ],
          'data3' => [
            'type' => 'varchar',
            'length' => '32',
            'not null' => TRUE,
          ],
69
        ],
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        'primary key' => ['data'],
      ]);
    }
    $query = $this->sourceDatabase->insert(static::SOURCE_TABLE_NAME)
      ->fields(['data', 'data2', 'data3']);
    $values = [
      [
        'data' => 'dummy value',
        'data2' => 'dummy2 value',
        'data3' => 'dummy3 value',
      ],
      [
        'data' => 'dummy value2',
        'data2' => 'dummy2 value2',
        'data3' => 'dummy3 value2',
      ],
      [
        'data' => 'dummy value3',
        'data2' => 'dummy2 value3',
        'data3' => 'dummy3 value3',
90
      ],
91 92 93 94 95
    ];
    foreach ($values as $record) {
      $query->values($record);
    }
    $query->execute();
96 97
  }

98 99 100
  /**
   * {@inheritdoc}
   */
101
  protected function tearDown(): void {
102 103
    $this->sourceDatabase->schema()->dropTable(static::SOURCE_TABLE_NAME);
    $this->connection->schema()->dropTable(static::DEST_TABLE_NAME);
104 105 106
    parent::tearDown();
  }

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  /**
   * Create a minimally valid migration with some source data.
   *
   * @return array
   *   The migration definition.
   */
  public function tableDestinationMigration() {
    return [
      'dummy table' => [
        [
          'id' => 'migration_table_test',
          'migration_tags' => ['Testing'],
          'source' => [
            'plugin' => 'embedded_data',
            'data_rows' => [
              [
                'data' => 'dummy1 value1',
                'data2' => 'dummy2 value1',
              ],
              [
                'data' => 'dummy1 value2',
                'data2' => 'dummy2 value2',
              ],
              [
                'data' => 'dummy1 value3',
                'data2' => 'dummy2 value3',
              ],
            ],
            'ids' => [
              'data' => ['type' => 'string'],
            ],
          ],
          'destination' => [
            'plugin' => 'table',
            'table_name' => static::DEST_TABLE_NAME,
            'id_fields' => [
              'data' => [
                'type' => 'string',
              ],
            ],
          ],
          'process' => [
            'data' => 'data',
            'data2' => 'data2',
          ],
        ],
      ],
    ];
  }

157
  /**
158
   * Tests table migration.
159
   */
160
  public function testTableMigration(): void {
161 162 163 164
    $definition = [
      'id' => 'migration_table_test',
      'migration_tags' => ['Testing'],
      'source' => [
165 166 167
        'plugin' => 'table',
        'table_name' => static::SOURCE_TABLE_NAME,
        'id_fields' => [
168 169
          'data' => ['type' => 'string'],
        ],
170
        'ignore_map' => TRUE,
171 172 173
      ],
      'destination' => [
        'plugin' => 'table',
174 175 176 177
        'table_name' => static::DEST_TABLE_NAME,
        'id_fields' => [
          'data' => ['type' => 'string'],
        ],
178
        'batch_size' => $this->batchSize,
179 180 181 182 183 184 185
      ],
      'process' => [
        'data' => 'data',
        'data2' => 'data2',
        'data3' => 'data3',
      ],
    ];
186
    $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
187 188 189 190

    $executable = new MigrateExecutable($migration, $this);
    $executable->import();

191 192
    $values = $this->connection->select(static::DEST_TABLE_NAME)
      ->fields(static::DEST_TABLE_NAME)
193 194 195 196 197 198 199 200 201 202 203
      ->execute()
      ->fetchAllAssoc('data');

    $this->assertEquals('dummy value', $values['dummy value']->data);
    $this->assertEquals('dummy2 value', $values['dummy value']->data2);
    $this->assertEquals('dummy2 value2', $values['dummy value2']->data2);
    $this->assertEquals('dummy3 value3', $values['dummy value3']->data3);
    $this->assertEquals(3, count($values));

    // Now rollback.
    $executable->rollback();
204 205
    $values = $this->connection->select(static::DEST_TABLE_NAME)
      ->fields(static::DEST_TABLE_NAME)
206 207 208 209 210 211
      ->execute()
      ->fetchAllAssoc('data');

    $this->assertEquals(0, count($values));
  }

212 213 214 215 216
  /**
   * Tests table update.
   *
   * @dataProvider tableDestinationMigration
   */
217
  public function testTableUpdate(array $definition): void {
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    // Make sure migration overwrites the original data for the first row.
    $original_values = [
      'data' => 'dummy value',
      'data2' => 'original value 2',
      'data3' => 'original value 3',
    ];
    $this->connection->insert(static::DEST_TABLE_NAME)
      ->fields($original_values)
      ->execute();

    /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
    $migration = \Drupal::service('plugin.manager.migration')
      ->createStubMigration($definition);
    $migration->getIdMap()->saveIdMapping(new Row($original_values,
      ['data' => 'dummy value']), ['data' => 'dummy value'], MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
    $this->testTableMigration();
  }

236
}