I tried current stable release and Git snapshot, but the issue still exists. So I'm reporting it here.

Some background:

1. I use 'd7_' as prefix for table names.

2. I use $databases but not $db_url to specify database connections:

  $databases = array(
    'default' => array(
      'default' => array(
        'database' => 'my_db_name',
        'username' => DB_USERNAME,
        'password' => DB_PASSWORD,
        'host'     => DB_HOST,
        'port'     => DB_PORT,
        'driver'   => DB_DRIVER,
        'prefix'   => 'd7_',
      ),
    ),
  );

Bug:

When running a unit test case, following exception message shown up.

PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_name.d7_d7_simpletest592517registry' doesn't exist: SELECT filename FROM {registry} WHERE name = :name AND type = :type;

Possible reason:

In method DrupalUnitTestCase::setUp() we have

    // Generate temporary prefixed database to ensure that tests have a clean starting point.
    $this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');

	// ......

    // Clone the current connection and replace the current prefix.
    $connection_info = Database::getConnectionInfo('default');
    Database::renameConnection('default', 'simpletest_original_default');
    foreach ($connection_info as $target => $value) {
      $connection_info[$target]['prefix'] = array(
        'default' => $value['prefix']['default'] . $this->databasePrefix,
      );
    }
    Database::addConnectionInfo('default', 'default', $connection_info['default']);

Let me add some more comments on the code and you will see how prefix is added twice:

    $this->databasePrefix = Database::getConnection()->prefixTables('{simpletest' . mt_rand(1000, 1000000) . '}');
	// $this->databasePrefix now is "d7_simpletest970028".

	// ......

    $connection_info = Database::getConnectionInfo('default');
	// $connection_info now contains a copy of the variable $databases['default']
    Database::renameConnection('default', 'simpletest_original_default');
	// There is no array element $databases['default'] now.
    foreach ($connection_info as $target => $value) {
      $connection_info[$target]['prefix'] = array(
        'default' => $value['prefix']['default'] . $this->databasePrefix,
      );
	  // When $target is 'default' (yes, $connection_info has an element $connection_info['default']),
	  // $connection_info['default']['prefix'] is array('default' => 'd7_d7_simpletest970028').
    }
    Database::addConnectionInfo('default', 'default', $connection_info['default']);
	// Now you are adding a connection with incorrect table prefix.

Comments

deminy’s picture

The issue happened when I didn't have a setUp() function to include a class file, and can be resolved by having following function added:

function setUp() {
    parent::setUp();
    module_load_include('inc', 'module_name', 'test.page');
  }
alauzon’s picture

The problems happened for me in the "Batch percentage", "Get filename test" and "Overrinding server variables" tests. Can someone look into this and resolve the issues?