Problem/Motivation

On Drupal 12.x, SqlImportTest fails in the phpunit (next major) job:

Drupal\Tests\mongodb_storage\Kernel\SqlImportTest::testImportActual with data set #0
Failed asserting that 4 matches expected 0.
modules/mongodb_storage/tests/src/Kernel/SqlImportTest.php:171

Example job: https://git.drupalcode.org/issue/mongodb-3627745/-/jobs/12795721
The 8.x-2.x next major job passed on 2026-10-05: https://git.drupalcode.org/project/mongodb/-/jobs/12660720

The failure comes from a core change, not from a mongodb change. Core commit 5c8d732 (#3613347: Re-use container in kernel tests, committed 2026-10-10) removed the override in KernelTestBase::register() that replaced the keyvalue service with a KeyValueMemoryFactory. Kernel tests now use the real keyvalue service, which stores data in the SQL key_value table.

When the test container is compiled, HookCollectorKeyValueWritePass::process() writes the hook_data collection through that service. This leaves 4 rows in key_value before the test runs:

  • hook_data / hook_list
  • hook_data / packed_order_operations
  • hook_data / preprocess_for_suggestions
  • hook_data / theme_hook_list

KeyValueTestBase::setUp() only deletes its own mongodb.nonexistent collection, so testImportActual() finds those rows and fails its assertEquals(0, $actualPreDbCount) check. Data set #1 (key_value_expire) still passes because nothing writes to that table.

Only removing that assertion would not be enough. SqlImport::import() copies every row in the table, so the hook_data collection would also reach MongoDB and break the later "Collection names match" assertion.

Steps to reproduce

  1. Set up a site on Drupal core 12.x (main) at or after commit 5c8d732, with PHP 8.5 and ext-mongodb 2.x.
  2. Run the test against MySQL and a MongoDB server:
    SIMPLETEST_DB=mysql://... MONGODB_URI=mongodb://... vendor/bin/phpunit -c web/core/phpunit.xml.dist web/modules/contrib/mongodb/modules/mongodb_storage/tests/src/Kernel/SqlImportTest.php
  3. testImportActual with data set #0 fails with Failed asserting that 4 matches expected 0.

On core before 5c8d732, such as 12.0.0-beta1, the test passes.

Proposed resolution

In SqlImportTest::testImportActual(), stop assuming that the SQL table starts empty. Read the rows that are already there and add them to the expected import result:

// Core may already have written rows, e.g. the hook_data collection
// written by HookCollectorKeyValueWritePass on container compile: they
// are imported too, so expect them.
$preExisting = $this->database->select($table, 't')
  ->fields('t', ['collection', 'name', 'value'])
  ->execute()
  ->fetchAll();
...
$expectedCollections = [];
foreach ($preExisting as $row) {
  // @codingStandardsIgnoreStart
  $expectedCollections[$row->collection][$row->name] = unserialize($row->value);
  // @codingStandardsIgnoreEnd
}
foreach ($rows as $row) {
  ...

This replaces the countTable() and assertEquals(0, ...) lines. The check that MongoDB starts with no collections stays as it is.

This works on core both before and after 5c8d732. I tested it locally on 12.0.0-beta1, both with the core change simulated and without it.

Emptying key_value in setUp() is not recommended, because core reads hook_data at runtime.

Remaining tasks

  • Create an MR with the test change.
  • Confirm that the phpunit (next major) job passes.
  • Review.

User interface changes

None.

API changes

None.

Data model changes

None.

AI Disclosure: I used Claude Code to triage this issue and help write this bug report.

Comments

matthand created an issue. See original summary.

matthand’s picture

Issue summary: View changes