Commit 3dd7e1fd authored by Erick Hitter's avatar Erick Hitter
Browse files

Update tests to reflect move to custom table

parent d32f9127
......@@ -30,7 +30,7 @@ class Events_Store extends Singleton {
*/
protected function class_init() {
// Check that the table exists and is the correct version
$this->prepare_db();
$this->prepare_tables();
// Option interception
add_filter( 'pre_option_cron', array( $this, 'get_option' ) );
......@@ -40,7 +40,7 @@ class Events_Store extends Singleton {
/**
* Build appropriate table name for this install
*/
protected function get_table_name() {
public function get_table_name() {
global $wpdb;
return $wpdb->base_prefix . self::TABLE_SUFFIX;
......@@ -49,12 +49,7 @@ class Events_Store extends Singleton {
/**
* Create the plugin's DB table when necessary
*/
protected function prepare_db() {
// Should be in admin context before using dbDelta
if ( ! is_admin() ) {
return;
}
protected function prepare_tables() {
// Nothing to do
if ( (int) get_option( self::DB_VERSION_OPTION ) === self::DB_VERSION ) {
return;
......@@ -165,10 +160,11 @@ class Events_Store extends Singleton {
$cron_array[ $timestamp ][ $action ][ $instance ] = array(
'schedule' => $jobs_post->schedule,
'args' => maybe_unserialize( $jobs_post->args ),
'interval' => 0,
);
if ( isset( $jobs_post->interval ) ) {
$cron_array[ $timestamp ][ $action ][ $instance ]['interval'] = $jobs_post->interval;
$cron_array[ $timestamp ][ $action ][ $instance ]['interval'] = (int) $jobs_post->interval;
}
}
......@@ -258,7 +254,7 @@ class Events_Store extends Singleton {
$offset = 0;
}
return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$this->get_table_name()} WHERE status = %s ORDER BY `timestamp` LIMIT %d,%d;", $args['status'], $offset, $args['quantity'] ), 'OBJECT' );
return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$this->get_table_name()} WHERE status = %s ORDER BY timestamp LIMIT %d,%d;", $args['status'], $offset, $args['quantity'] ), 'OBJECT' );
}
/**
......
......@@ -3,6 +3,13 @@
namespace Automattic\WP\Cron_Control\Tests;
class Utils {
/**
* Provide easy access to the plugin's table
*/
static function get_table_name() {
return \Automattic\WP\Cron_Control\Events_Store::instance()->get_table_name();
}
/**
* Build a test event
*/
......@@ -25,7 +32,7 @@ class Utils {
if ( $next ) {
$event['timestamp'] = $next;
} else {
wp_schedule_single_event( $event[ 'timestamp' ], $event[ 'action' ], $event[ 'args' ] );
wp_schedule_single_event( $event['timestamp'], $event['action'], $event['args'] );
}
return $event;
......@@ -34,24 +41,19 @@ class Utils {
/**
* Retrieve some events' post objects for use in testing
*/
static function get_events_from_post_objects() {
$events = get_posts( array(
'post_type' => \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE,
'post_status' => \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS_PENDING,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'ASC',
'suppress_filters' => false,
) );
static function get_events_from_store() {
global $wpdb;
$table_name = self::get_table_name();
$events = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE status = %s ORDER BY `timestamp` ASC LIMIT 10;", \Automattic\WP\Cron_Control\Events_Store::STATUS_PENDING ), 'OBJECT' );
$parsed_events = array();
foreach ( $events as $event ) {
$event_args = explode( '|', $event->post_title );
$parsed_events[] = array(
'timestamp' => (int) $event_args[0],
'action' => trim( $event_args[1] ),
'instance' => trim( $event_args[2] ),
'timestamp' => (int) $event->timestamp,
'action' => $event->action,
'instance' => $event->instance,
);
}
......
......@@ -10,13 +10,17 @@ namespace Automattic\WP\Cron_Control\Tests;
/**
* Sample test case.
*/
class Cron_Options_CPT_Tests extends \WP_UnitTestCase {
class Events_Store_Tests extends \WP_UnitTestCase {
/**
* Custom post type exists
* Custom table exists
*/
function test_cpt_exists() {
$this->assertTrue( post_type_exists( \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE ) );
function test_table_exists() {
global $wpdb;
$table_name = Utils::get_table_name();
$this->assertEquals( count( $wpdb->get_col( "SHOW TABLES LIKE '{$table_name}'" ) ), 1 );
}
/**
......@@ -25,19 +29,18 @@ class Cron_Options_CPT_Tests extends \WP_UnitTestCase {
function test_events_exist() {
global $wpdb;
$event = \Automattic\WP\Cron_Control\Tests\Utils::create_test_event();
$post_name = sprintf( '%s-%s-%s', $event['timestamp'], md5( $event['action'] ), md5( maybe_serialize( $event['args'] ) ) );
$event = Utils::create_test_event();
$table_name = Utils::get_table_name();
$entry = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = %s AND post_name = %s LIMIT 1", \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE, \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS_PENDING, $post_name ) );
$entry = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE timestamp = %d AND action = %s AND instance = %s AND status = %s LIMIT 1", $event['timestamp'], $event['action'], md5( maybe_serialize( $event['args'] ) ), \Automattic\WP\Cron_Control\Events_Store::STATUS_PENDING ) );
$this->assertEquals( count( $entry ), 1 );
$entry = array_shift( $entry );
$instance = maybe_unserialize( $entry->post_content_filtered );
$entry = array_shift( $entry );
$this->assertEquals( $event['action'], $instance['action'] );
$this->assertEquals( md5( maybe_serialize( $event['args'] ) ), $instance['instance'] );
Utils::compare_arrays( $event['args'], $instance['args'], $this );
$this->assertEquals( $event['action'], $entry->action );
$this->assertEquals( md5( maybe_serialize( $event['args'] ) ), $entry->instance );
Utils::compare_arrays( $event['args'], maybe_unserialize( $entry->args ), $this );
}
/**
......@@ -95,7 +98,7 @@ class Cron_Options_CPT_Tests extends \WP_UnitTestCase {
/**
* Test that events are unscheduled correctly by checking the CPT
*/
function test_event_unscheduling_against_cpt() {
function test_event_unscheduling_against_event_store() {
// Schedule two events and prepare their data a bit for further testing
$first_event = Utils::create_test_event();
$first_event['instance'] = md5( maybe_serialize( $first_event['args'] ) );
......@@ -110,16 +113,16 @@ class Cron_Options_CPT_Tests extends \WP_UnitTestCase {
unset( $second_event['args'] );
// First, check that posts were created for the two events
Utils::compare_arrays( array( $first_event, $second_event ), Utils::get_events_from_post_objects(), $this );
Utils::compare_arrays( array( $first_event, $second_event ), Utils::get_events_from_store(), $this );
// Second, unschedule an event and confirm that the post is removed
wp_unschedule_event( $first_event['timestamp'], $first_event['action'], $first_event_args );
Utils::compare_arrays( array( $second_event ), Utils::get_events_from_post_objects(), $this );
Utils::compare_arrays( array( $second_event ), Utils::get_events_from_store(), $this );
// Finally, unschedule the second event and confirm its post is also deleted
wp_unschedule_event( $second_event['timestamp'], $second_event['action'], $second_event_args );
$this->assertEmpty( Utils::get_events_from_post_objects() );
$this->assertEmpty( Utils::get_events_from_store() );
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment