Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions __tests__/unit-tests/test-events-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
19 changes: 14 additions & 5 deletions includes/class-events-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,26 @@ 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' );

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.
*/
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 ) {
Expand Down
Loading