Generating database fixtures for migration tests

Last updated on
26 October 2022

The workflow here is applicable to contrib, custom and core migrations. The examples given here refer to core migrations.

When writing tests for Drupal 6 / Drupal 7 core migrations, use the database fixtures that are included in Drupal core:

  • core/modules/migrate_drupal/tests/fixtures/drupal6.php
  • core/modules/migrate_drupal/tests/fixtures/drupal7.php

This is your source data to write your tests against. If you need extra test data in these database sources, you can modify the fixture using the following method.

Creating a test site and importing the data from the fixture

Importing data from the fixture to your test database 

1. Prepare an empty database where you can import the data from the fixture. The database name in the example below is drupal_fixture but you can call the database whatever you want.

2. Define a database connection to your empty database in your Drupal settings.php.

$databases['fixture_connection']['default'] = array (
  'database' => 'drupal_fixture',
  'username' => 'username',
  'password' => 'password',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

3. Import the Drupal 6 or Drupal 7 fixture into this database.

Command to import Drupal 6 fixture: 
php core/scripts/db-tools.php import --database fixture_connection core/modules/migrate_drupal/tests/fixtures/drupal6.php

Command to import Drupal 7 fixture: 
php core/scripts/db-tools.php import --database fixture_connection core/modules/migrate_drupal/tests/fixtures/drupal7.php

4. Add a row for uid 0 to {users} table manually.

Set up Drupal 6 / Drupal 7 installation that uses your test database

a. Download the latest Drupal 6 / Drupal 7.

b. Edit the sites/default/settings.php of your Drupal 6 / Drupal 7 installation so that it points to your your test database which has the data from the fixture.

c. Install the following contributed modules to sites/all/modules of your Drupal 6 / Drupal 7 installation.

Contributed modules for Drupal 6:

Contributed modules for Drupal 7:

Attached is a drush makefile for Drupal7 fixture.

Commands to install Drupal 7 via composer:

composer create-project drupal-composer/drupal-project:7.x-dev drupal7
cd drupal7
composer require drupal/ctools drupal/date drupal/email drupal/entity drupal/entityreference drupal/entity_translation drupal/i18n drupal/link drupal/multiupload_filefield_widget drupal/multiupload_imagefield_widget drupal/phone drupal/references drupal/telephone drupal/title drupal/variable drupal/views

Log in to your test site and finalize the configuration

1. You can now log in to your test site as the admin user.

  • Drupal 6: username/password is root/root
  • Drupal 7: username/password is admin/root

2. Fix file directory to "sites/default/files", and temp directory to "sites/default/files/temp"

Make the changes and export to the changes to the fixture file 

1. Make the desired changes on your Drupal 6 / Drupal 7 test site that are needed for your additional test coverage.

2. Export your changes to the fixture file. Make sure that you are now working in the git branch of your patch.

Command for Drupal 6:
php core/scripts/db-tools.php dump --database fixture_connection > core/modules/migrate_drupal/tests/fixtures/drupal6.php

Command for Drupal 7:
php core/scripts/db-tools.php dump --database fixture_connection > core/modules/migrate_drupal/tests/fixtures/drupal7.php

3. View the diff of the fixture file and keep only relevant changes.

When you export the database dump, it contains a lot of noise which is not related to the changes that are needed for your additional test coverage. The patch should only include relevant changes which means that you need to discard the unwanted changes. This can be done using a git difftool of your choice. 

Execute the tests locally

Refer to Running PHPUnit tests documentation page on how to execute the tests locally.

Help improve this page

Page status: No known problems

You can: