class MigrationTest

Same name in this branch
  1. 10 core/modules/migrate/tests/src/Kernel/MigrationTest.php \Drupal\Tests\migrate\Kernel\MigrationTest
  2. 10 core/modules/migrate/tests/src/Kernel/Plugin/MigrationTest.php \Drupal\Tests\migrate\Kernel\Plugin\MigrationTest
Same name in other branches
  1. 9 core/modules/migrate/tests/src/Unit/MigrationTest.php \Drupal\Tests\migrate\Unit\MigrationTest
  2. 9 core/modules/migrate/tests/src/Kernel/MigrationTest.php \Drupal\Tests\migrate\Kernel\MigrationTest
  3. 9 core/modules/migrate/tests/src/Kernel/Plugin/MigrationTest.php \Drupal\Tests\migrate\Kernel\Plugin\MigrationTest
  4. 8.9.x core/modules/migrate/tests/src/Unit/MigrationTest.php \Drupal\Tests\migrate\Unit\MigrationTest
  5. 8.9.x core/modules/migrate/tests/src/Unit/process/MigrationTest.php \Drupal\Tests\migrate\Unit\process\MigrationTest
  6. 8.9.x core/modules/migrate/tests/src/Kernel/MigrationTest.php \Drupal\Tests\migrate\Kernel\MigrationTest
  7. 8.9.x core/modules/migrate/tests/src/Kernel/Plugin/MigrationTest.php \Drupal\Tests\migrate\Kernel\Plugin\MigrationTest
  8. 11.x core/modules/migrate/tests/src/Unit/MigrationTest.php \Drupal\Tests\migrate\Unit\MigrationTest
  9. 11.x core/modules/migrate/tests/src/Kernel/MigrationTest.php \Drupal\Tests\migrate\Kernel\MigrationTest
  10. 11.x core/modules/migrate/tests/src/Kernel/Plugin/MigrationTest.php \Drupal\Tests\migrate\Kernel\Plugin\MigrationTest

@coversDefaultClass \Drupal\migrate\Plugin\Migration

@group migrate

Hierarchy

Expanded class hierarchy of MigrationTest

File

core/modules/migrate/tests/src/Unit/MigrationTest.php, line 25

Namespace

Drupal\Tests\migrate\Unit
View source
class MigrationTest extends UnitTestCase {
    
    /**
     * Tests checking migration dependencies in the constructor.
     *
     * @param array $dependencies
     *   An array of migration dependencies.
     *
     * @covers ::__construct
     *
     * @dataProvider getInvalidMigrationDependenciesProvider
     *
     * @group legacy
     */
    public function testMigrationDependenciesInConstructor(array $dependencies) : void {
        $configuration = [
            'migration_dependencies' => $dependencies,
        ];
        $plugin_id = 'test_migration';
        $migration_plugin_manager = $this->createMock('\\Drupal\\migrate\\Plugin\\MigrationPluginManagerInterface');
        $source_plugin_manager = $this->createMock('\\Drupal\\migrate\\Plugin\\MigratePluginManagerInterface');
        $process_plugin_manager = $this->createMock('\\Drupal\\migrate\\Plugin\\MigratePluginManagerInterface');
        $destination_plugin_manager = $this->createMock('\\Drupal\\migrate\\Plugin\\MigrateDestinationPluginManager');
        $id_map_plugin_manager = $this->createMock('\\Drupal\\migrate\\Plugin\\MigratePluginManagerInterface');
        $this->expectDeprecation("Invalid migration dependencies for {$plugin_id} is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3266691");
        new Migration($configuration, $plugin_id, [], $migration_plugin_manager, $source_plugin_manager, $process_plugin_manager, $destination_plugin_manager, $id_map_plugin_manager);
    }
    
    /**
     * Tests checking requirements for source plugins.
     *
     * @covers ::checkRequirements
     */
    public function testRequirementsForSourcePlugin() : void {
        $migration = new TestMigration();
        $source_plugin = $this->createMock('Drupal\\Tests\\migrate\\Unit\\RequirementsAwareSourceInterface');
        $source_plugin->expects($this->once())
            ->method('checkRequirements')
            ->willThrowException(new RequirementsException('Missing source requirement', [
            'key' => 'value',
        ]));
        $destination_plugin = $this->createMock('Drupal\\Tests\\migrate\\Unit\\RequirementsAwareDestinationInterface');
        $migration->setSourcePlugin($source_plugin);
        $migration->setDestinationPlugin($destination_plugin);
        $this->expectException(RequirementsException::class);
        $this->expectExceptionMessage('Missing source requirement');
        $migration->checkRequirements();
    }
    
    /**
     * Tests checking requirements for destination plugins.
     *
     * @covers ::checkRequirements
     */
    public function testRequirementsForDestinationPlugin() : void {
        $migration = new TestMigration();
        $source_plugin = $this->createMock('Drupal\\migrate\\Plugin\\MigrateSourceInterface');
        $destination_plugin = $this->createMock('Drupal\\Tests\\migrate\\Unit\\RequirementsAwareDestinationInterface');
        $destination_plugin->expects($this->once())
            ->method('checkRequirements')
            ->willThrowException(new RequirementsException('Missing destination requirement', [
            'key' => 'value',
        ]));
        $migration->setSourcePlugin($source_plugin);
        $migration->setDestinationPlugin($destination_plugin);
        $this->expectException(RequirementsException::class);
        $this->expectExceptionMessage('Missing destination requirement');
        $migration->checkRequirements();
    }
    
    /**
     * Tests checking requirements for destination plugins.
     *
     * @covers ::checkRequirements
     */
    public function testRequirementsForMigrations() : void {
        $migration = new TestMigration();
        // Setup source and destination plugins without any requirements.
        $source_plugin = $this->createMock('Drupal\\migrate\\Plugin\\MigrateSourceInterface');
        $destination_plugin = $this->createMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
        $migration->setSourcePlugin($source_plugin);
        $migration->setDestinationPlugin($destination_plugin);
        $plugin_manager = $this->createMock('Drupal\\migrate\\Plugin\\MigrationPluginManagerInterface');
        $migration->setMigrationPluginManager($plugin_manager);
        // We setup the requirements that test_a doesn't exist and test_c is not
        // completed yet.
        $migration->setRequirements([
            'test_a',
            'test_b',
            'test_c',
            'test_d',
        ]);
        $migration_b = $this->createMock(MigrationInterface::class);
        $migration_c = $this->createMock(MigrationInterface::class);
        $migration_d = $this->createMock(MigrationInterface::class);
        $migration_b->expects($this->once())
            ->method('allRowsProcessed')
            ->willReturn(TRUE);
        $migration_c->expects($this->once())
            ->method('allRowsProcessed')
            ->willReturn(FALSE);
        $migration_d->expects($this->once())
            ->method('allRowsProcessed')
            ->willReturn(TRUE);
        $plugin_manager->expects($this->once())
            ->method('createInstances')
            ->with([
            'test_a',
            'test_b',
            'test_c',
            'test_d',
        ])
            ->willReturn([
            'test_b' => $migration_b,
            'test_c' => $migration_c,
            'test_d' => $migration_d,
        ]);
        $this->expectException(RequirementsException::class);
        $this->expectExceptionMessage('Missing migrations test_a, test_c');
        $migration->checkRequirements();
    }
    
    /**
     * Tests getting requirement list.
     *
     * @covers ::getRequirements
     */
    public function testGetMigrations() : void {
        $migration = new TestMigration();
        $requirements = [
            'test_a',
            'test_b',
            'test_c',
            'test_d',
        ];
        $migration->setRequirements($requirements);
        $this->assertEquals($requirements, $migration->getRequirements());
    }
    
    /**
     * Tests valid migration dependencies configuration returns expected values.
     *
     * @param array|null $source
     *   The migration dependencies configuration being tested.
     * @param array $expected_value
     *   The migration dependencies configuration array expected.
     *
     * @covers ::getMigrationDependencies
     * @dataProvider getValidMigrationDependenciesProvider
     *
     * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
     */
    public function testMigrationDependenciesWithValidConfig($source, array $expected_value) : void {
        $migration = new TestMigration();
        // Set the plugin manager to support getMigrationDependencies().
        $plugin_manager = $this->createMock('Drupal\\migrate\\Plugin\\MigrationPluginManagerInterface');
        $migration->setMigrationPluginManager($plugin_manager);
        $plugin_manager->expects($this->exactly(2))
            ->method('expandPluginIds')
            ->willReturnArgument(0);
        if (!is_null($source)) {
            $migration->set('migration_dependencies', $source);
        }
        $this->assertSame($migration->getMigrationDependencies(TRUE), $expected_value);
    }
    
    /**
     * Tests that getting migration dependencies fails with invalid configuration.
     *
     * @param array $dependencies
     *   An array of migration dependencies.
     *
     * @covers ::getMigrationDependencies
     *
     * @dataProvider getInvalidMigrationDependenciesProvider
     *
     * @group legacy
     */
    public function testMigrationDependenciesWithInvalidConfig(array $dependencies) : void {
        $migration = new TestMigration();
        // Set the plugin ID to test the returned message.
        $plugin_id = 'test_migration';
        $migration->setPluginId($plugin_id);
        // Migration dependencies expects ['optional' => []] or ['required' => []]].
        $this->expectDeprecation("Invalid migration dependencies for {$plugin_id} is deprecated in drupal:10.1.0 and will cause an error in drupal:11.0.0. See https://www.drupal.org/node/3266691");
        $migration->set('migration_dependencies', $dependencies);
        $this->expectException(InvalidPluginDefinitionException::class);
        $this->expectExceptionMessage("Invalid migration dependencies configuration for migration {$plugin_id}");
        $migration->getMigrationDependencies(TRUE);
    }
    
    /**
     * Provides data for valid migration configuration test.
     */
    public static function getValidMigrationDependenciesProvider() {
        return [
            [
                'source' => NULL,
                'expected_value' => [
                    'required' => [],
                    'optional' => [],
                ],
            ],
            [
                'source' => [],
                'expected_value' => [
                    'required' => [],
                    'optional' => [],
                ],
            ],
            [
                'source' => [
                    'required' => [
                        'test_migration',
                    ],
                ],
                'expected_value' => [
                    'required' => [
                        'test_migration',
                    ],
                    'optional' => [],
                ],
            ],
            [
                'source' => [
                    'optional' => [
                        'test_migration',
                    ],
                ],
                'expected_value' => [
                    'optional' => [
                        'test_migration',
                    ],
                    'required' => [],
                ],
            ],
            [
                'source' => [
                    'required' => [
                        'req_test_migration',
                    ],
                    'optional' => [
                        'opt_test_migration',
                    ],
                ],
                'expected_value' => [
                    'required' => [
                        'req_test_migration',
                    ],
                    'optional' => [
                        'opt_test_migration',
                    ],
                ],
            ],
        ];
    }
    
    /**
     * Provides invalid migration dependencies.
     */
    public static function getInvalidMigrationDependenciesProvider() {
        return [
            'invalid key' => [
                'dependencies' => [
                    'bogus' => [],
                ],
            ],
            'required not array' => [
                'dependencies' => [
                    'required' => 17,
                    'optional' => [],
                ],
            ],
            'optional not array' => [
                'dependencies' => [
                    'required' => [],
                    'optional' => 17,
                ],
            ],
        ];
    }
    
    /**
     * Test trackLastImported deprecation message in Migration constructor.
     *
     * @group legacy
     */
    public function testTrackLastImportedDeprecation() : void {
        $this->expectDeprecation("The key 'trackLastImported' is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3282894");
        $migration_plugin_manager = $this->createMock(MigrationPluginManagerInterface::class);
        $source_plugin_manager = $this->createMock(MigrateSourcePluginManager::class);
        $process_Plugin_manager = $this->createMock(MigratePluginManagerInterface::class);
        $destination_plugin_manager = $this->createMock(MigrateDestinationPluginManager::class);
        $id_map_plugin_manager = $this->createMock(MigratePluginManagerInterface::class);
        new Migration([], 'test', [
            'trackLastImported' => TRUE,
        ], $migration_plugin_manager, $source_plugin_manager, $process_Plugin_manager, $destination_plugin_manager, $id_map_plugin_manager);
    }
    
    /**
     * Tests deprecation of getMigrationDependencies(FALSE).
     *
     * @group legacy
     */
    public function testGetMigrationDependencies() : void {
        $migration = new TestMigration();
        $this->expectDeprecation('Calling Migration::getMigrationDependencies() without expanding the plugin IDs is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. In most cases, use getMigrationDependencies(TRUE). See https://www.drupal.org/node/3266691');
        $migration->getMigrationDependencies();
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
MigrationTest::getInvalidMigrationDependenciesProvider public static function Provides invalid migration dependencies.
MigrationTest::getValidMigrationDependenciesProvider public static function Provides data for valid migration configuration test.
MigrationTest::testGetMigrationDependencies public function Tests deprecation of getMigrationDependencies(FALSE).
MigrationTest::testGetMigrations public function Tests getting requirement list.
MigrationTest::testMigrationDependenciesInConstructor public function Tests checking migration dependencies in the constructor.
MigrationTest::testMigrationDependenciesWithInvalidConfig public function Tests that getting migration dependencies fails with invalid configuration.
MigrationTest::testMigrationDependenciesWithValidConfig public function Tests valid migration dependencies configuration returns expected values.
MigrationTest::testRequirementsForDestinationPlugin public function Tests checking requirements for destination plugins.
MigrationTest::testRequirementsForMigrations public function Tests checking requirements for destination plugins.
MigrationTest::testRequirementsForSourcePlugin public function Tests checking requirements for source plugins.
MigrationTest::testTrackLastImportedDeprecation public function Test trackLastImported deprecation message in Migration constructor.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.