diff --git a/__tests__/unit-tests/test-events-store.php b/__tests__/unit-tests/test-events-store.php index 49f3fec..76eeb31 100644 --- a/__tests__/unit-tests/test-events-store.php +++ b/__tests__/unit-tests/test-events-store.php @@ -25,6 +25,37 @@ public function test_table_exists() { $this->assertTrue( Events_Store::is_installed() ); } + public function test_is_installed_ignores_same_named_table_in_other_database() { + // Use real tables, since temporary tables don't show up in information_schema. + remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); + remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); + + // Run the fixture DDL on its own connection: DDL implicitly commits, which would end the test's transaction. + $ddl = new \wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); + $ddl->suppress_errors(); + + // Unique name, created without IF NOT EXISTS, so cleanup only drops a database this test made. + $other_db = 'cron_control_tests_' . strtolower( wp_generate_password( 12, false ) ); + $table_name = Utils::get_table_name(); + + if ( false === $ddl->query( $ddl->prepare( 'CREATE DATABASE %i', $other_db ) ) ) { + $ddl->close(); + $this->markTestSkipped( 'The test DB user cannot create databases.' ); + } + + try { + $created_table = $ddl->query( $ddl->prepare( 'CREATE TABLE %i.%i (`ID` bigint(20) unsigned NOT NULL)', $other_db, $table_name ) ); + $this->assertNotFalse( $created_table, 'The same-named table could not be created in the other database.' ); + + wp_cache_delete( 'is_installed', 'cron-control' ); + $this->assertTrue( Events_Store::is_installed() ); + } finally { + $ddl->query( $ddl->prepare( 'DROP DATABASE %i', $other_db ) ); + $ddl->close(); + wp_cache_delete( 'is_installed', 'cron-control' ); + } + } + public function test_event_creation() { $store = Events_Store::instance(); diff --git a/includes/class-events-store.php b/includes/class-events-store.php index 90a9695..4437598 100644 --- a/includes/class-events-store.php +++ b/includes/class-events-store.php @@ -52,8 +52,7 @@ public static function is_installed(): bool { return $is_installed; } - $table_name = $wpdb->prefix . self::TABLE_SUFFIX; - $is_installed = 1 === count( $wpdb->get_col( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = %s', $table_name ) ) ); + $is_installed = self::table_exists( $wpdb->prefix . self::TABLE_SUFFIX ); // Cache the results, will be overridden by _prepare_table() during installation. wp_cache_add( 'is_installed', $is_installed, 'cron-control' ); @@ -61,6 +60,18 @@ public static function is_installed(): bool { return $is_installed; } + /** + * Check whether the table exists in the current database. + * + * Scoped to DATABASE() because the DB user can usually see every schema on the server, + * and a same-named table in another schema must not count. + */ + private static function table_exists( string $table_name ): bool { + global $wpdb; + + return null !== $wpdb->get_var( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s', $table_name ) ); + } + /** * Build appropriate table name for this site. */ @@ -113,8 +124,6 @@ public function maybe_install_during_shutdown() { * Create the plugin's DB table when necessary */ protected function _prepare_table() { - global $wpdb; - // Use Core's method of creating/updating tables. if ( ! function_exists( 'dbDelta' ) ) { require_once ABSPATH . '/wp-admin/includes/upgrade.php'; @@ -147,7 +156,7 @@ protected function _prepare_table() { dbDelta( $schema, true ); // Confirm that the table was created, and set the option to prevent further updates. - $is_installed = 1 === count( $wpdb->get_col( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = %s', $table_name ) ) ); + $is_installed = self::table_exists( $table_name ); wp_cache_set( 'is_installed', $is_installed, 'cron-control' ); if ( $is_installed ) {