wp_insert_site() is triggering database error in a unit test

Question

I have a WP "unit" test, run in multisite mode, that looks like this:

class EffectsTest extends WP_UnitTestCase {
  public function test_wp_insert_site() {
    $id = wp_insert_site([
      'title'             => 'My Site Title',
      'domain'            => 'foo.multicare.org',
      'meta'              => [
        'mc_central_data' => serialize([
          'id'            => 2,
          'site'          => 'My Site Title',
        ]),
      ],
    ]);

    $this->assertNotEmpty($id);
  }
}

The test passes but it’s triggering the error:

WordPress database error Can't reopen table: 'a' for query DELETE a, b FROM wptests_2_options a, wptests_2_options b
                        WHERE a.option_name LIKE '_transient_%'
                        AND a.option_name NOT LIKE '_transient_timeout_%'
                        AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
                        AND b.option_value < 1605216174
made by PHPUnitTextUICommand::main, PHPUnitTextUICommand->run, PHPUnitTextUITestRunner->doRun,
PHPUnitFrameworkTestSuite->run, PHPUnitFrameworkTestSuite->run, PHPUnitFrameworkTestSuite->run,
PHPUnitFrameworkTestCase->run, PHPUnitFrameworkTestResult->run, PHPUnitFrameworkTestCase->runBare,
PHPUnitFrameworkTestCase->runTest, MultiCareIntegrationEffectsTest->test_wp_insert_site, 
wp_insert_site, do_action('wp_initialize_site'), WP_Hook->do_action, WP_Hook->apply_filters, 
wp_initialize_site, populate_options, delete_expired_transients

The actual error originates in delete_expired_transients(), which appears to be deleting from two aliases to the same table, $wpdb->options:

 "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b..."

MySQL doesn’t seem to like that. So my question is, is there a way to prevent this without patching WP Core? Seems like a core bug to me, but perhaps I’m missing something.

(The actual test calls a function from my application code, so I’m not just testing wp_insert_site(). But that’s irrelevant here because it’s the call to wp_insert_site() that’s triggering the error.)

0
acobster 2 months 0 Answers 20 views 0

Leave an answer