/** * Test arbitrary context actions given a context and type. * * @param string $context The context being tested. * @param string $type The subcontext being tested. */ protected function _context_action_assertions($context, $type) { $a = new MockAction(); if ($context) { add_action("fm_{$context}", array(&$a, 'action')); } if ($type) { add_action("fm_{$context}_{$type}", array(&$a, 'action')); } fm_get_context(true); fm_trigger_context_action(); if ($type) { // only two events occurred for the hook $this->assertEquals(2, $a->get_call_count()); // only our hooks were called $this->assertEquals(array("fm_{$context}_{$type}", "fm_{$context}"), $a->get_tags()); // The $type should have been passed as args $this->assertEquals(array(array($type), array($type)), $a->get_args()); } elseif ($context) { // only one event occurred for the hook $this->assertEquals(1, $a->get_call_count()); // only our hook was called $this->assertEquals(array("fm_{$context}"), $a->get_tags()); // null should have been passed as an arg $this->assertEquals(array(array(null)), $a->get_args()); } else { // No event should have fired $this->assertEquals(0, $a->get_call_count()); } }
public function test_do_action_with_multiple_callbacks_on_different_priorities() { $a = new MockAction(); $b = new MockAction(); $callback_one = array($a, 'filter'); $callback_two = array($b, 'filter'); $hook = new WP_Hook(); $tag = __FUNCTION__; $priority = rand(1, 100); $accepted_args = rand(1, 100); $arg = __FUNCTION__ . '_arg'; $hook->add_filter($tag, $callback_one, $priority, $accepted_args); $hook->add_filter($tag, $callback_two, $priority, $accepted_args); $hook->do_action(array($arg)); $this->assertEquals(1, $a->get_call_count()); $this->assertEquals(1, $a->get_call_count()); }
public function test_execute() { $mock = new MockAction(); $random = md5(rand()); add_action($random, array($mock, 'action')); $action = new ActionScheduler_Action($random, array($random)); $action->execute(); remove_action($random, array($mock, 'action')); $this->assertEquals(1, $mock->get_call_count()); $events = $mock->get_events(); $event = reset($events); $this->assertEquals($random, reset($event['args'])); }
public function test_do_all_hook_with_multiple_calls() { $a = new MockAction(); $callback = array($a, 'action'); $hook = new WP_Hook(); $tag = 'all'; $priority = rand(1, 100); $accepted_args = rand(1, 100); $arg = rand_str(); $hook->add_filter($tag, $callback, $priority, $accepted_args); $args = array($arg); $hook->do_all_hook($args); $hook->do_all_hook($args); $this->assertEquals(2, $a->get_call_count()); }
public function test_apply_filters_with_multiple_calls() { $a = new MockAction(); $callback = array($a, 'filter'); $hook = new WP_Hook(); $tag = rand_str(); $priority = rand(1, 100); $accepted_args = rand(1, 100); $arg = rand_str(); $hook->add_filter($tag, $callback, $priority, $accepted_args); $returned_one = $hook->apply_filters($arg, array($arg)); $returned_two = $hook->apply_filters($returned_one, array($returned_one)); $this->assertEquals($returned_two, $arg); $this->assertEquals(2, $a->get_call_count()); }
public function test_run_with_future_actions() { $store = new ActionScheduler_wpPostStore(); $runner = new ActionScheduler_QueueRunner($store); $mock = new MockAction(); $random = md5(rand()); add_action($random, array($mock, 'action')); $schedule = new ActionScheduler_SimpleSchedule(new DateTime('1 day ago')); for ($i = 0; $i < 3; $i++) { $action = new ActionScheduler_Action($random, array($random), $schedule); $store->save_action($action); } $schedule = new ActionScheduler_SimpleSchedule(new DateTime('tomorrow')); for ($i = 0; $i < 3; $i++) { $action = new ActionScheduler_Action($random, array($random), $schedule); $store->save_action($action); } $actions_run = $runner->run(); remove_action($random, array($mock, 'action')); $this->assertEquals(3, $mock->get_call_count()); $this->assertEquals(3, $actions_run); }
/** * @ticket 28651 */ function test_duplicate_network_active_plugin() { $path = "hello.php"; $mock = new MockAction(); add_action( 'activate_' . $path, array ( $mock, 'action' ) ); // should activate on the first try activate_plugin( $path, '', true ); $active_plugins = wp_get_active_network_plugins(); $this->assertCount( 1, $active_plugins ); $this->assertEquals( 1, $mock->get_call_count() ); // should do nothing on the second try activate_plugin( $path, '', true ); $active_plugins = wp_get_active_network_plugins(); $this->assertCount( 1, $active_plugins ); $this->assertEquals( 1, $mock->get_call_count() ); remove_action( 'activate_' . $path, array ( $mock, 'action' ) ); }
/** * @ticket 9968 * @ticket 17817 */ function test_action_callback_manipulation_while_running() { $tag = rand_str(); $a = new MockAction(); $b = new MockAction(); $c = new MockAction(); $d = new MockAction(); $e = new MockAction(); add_action($tag, array($a, 'action'), 11, 2); add_action($tag, array($this, 'action_that_manipulates_a_running_hook'), 12, 2); add_action($tag, array($b, 'action'), 12, 2); do_action($tag, $tag, array($a, $b, $c, $d, $e)); do_action($tag, $tag, array($a, $b, $c, $d, $e)); $this->assertEquals(2, $a->get_call_count(), 'callbacks should run unless otherwise instructed'); $this->assertEquals(1, $b->get_call_count(), 'callback removed by same priority callback should still get called'); $this->assertEquals(1, $c->get_call_count(), 'callback added by same priority callback should not get called'); $this->assertEquals(2, $d->get_call_count(), 'callback added by earlier priority callback should get called'); $this->assertEquals(1, $e->get_call_count(), 'callback added by later priority callback should not get called'); }
function test_remove_all_filter() { $a = new MockAction(); $tag = rand_str(); $val = rand_str(); add_filter('all', array($a, 'filterall')); $this->assertTrue(has_filter('all')); $this->assertEquals(10, has_filter('all', array($a, 'filterall'))); $this->assertEquals($val, apply_filters($tag, $val)); // make sure our hook was called correctly $this->assertEquals(1, $a->get_call_count()); $this->assertEquals(array($tag), $a->get_tags()); // now remove the filter, do it again, and make sure it's not called this time remove_filter('all', array($a, 'filterall')); $this->assertFalse(has_filter('all', array($a, 'filterall'))); $this->assertFalse(has_filter('all')); $this->assertEquals($val, apply_filters($tag, $val)); // call cound should remain at 1 $this->assertEquals(1, $a->get_call_count()); $this->assertEquals(array($tag), $a->get_tags()); }
/** * @ticket 35598 */ public function test_phpmailer_exception_thrown() { $to = 'an_invalid_address'; $subject = 'Testing'; $message = 'Test Message'; $ma = new MockAction(); add_action('wp_mail_failed', array(&$ma, 'action')); wp_mail($to, $subject, $message); $this->assertEquals(1, $ma->get_call_count()); $expected_error_data = array('to' => array('an_invalid_address'), 'subject' => 'Testing', 'message' => 'Test Message', 'headers' => array(), 'attachments' => array(), 'phpmailer_exception_code' => 2); //Retrieve the arguments passed to the 'wp_mail_failed' hook callbacks $all_args = $ma->get_args(); $call_args = array_pop($all_args); $this->assertEquals('wp_mail_failed', $call_args[0]->get_error_code()); $this->assertEquals($expected_error_data, $call_args[0]->get_error_data()); }
function test_remove_all_action() { $a = new MockAction(); $tag = rand_str(); add_action('all', array(&$a, 'action')); $this->assertEquals(10, has_filter('all', array(&$a, 'action'))); do_action($tag); // make sure our hook was called correctly $this->assertEquals(1, $a->get_call_count()); $this->assertEquals(array($tag), $a->get_tags()); // now remove the action, do it again, and make sure it's not called this time remove_action('all', array(&$a, 'action')); $this->assertFalse(has_filter('all', array(&$a, 'action'))); do_action($tag); $this->assertEquals(1, $a->get_call_count()); $this->assertEquals(array($tag), $a->get_tags()); }
/** * @ticket 30380 */ function test_nonexistent_key_old_timeout() { // Create a transient $key = 'test_transient'; set_transient($key, 'test', 60 * 10); $this->assertEquals('test', get_transient($key)); // Make sure the timeout option returns false $timeout = '_transient_timeout_' . $key; $transient_option = '_transient_' . $key; add_filter('option_' . $timeout, '__return_zero'); // Mock an action for tracking action calls $a = new MockAction(); // Add some actions to make sure options are deleted add_action('delete_option', array($a, 'action')); // Act get_transient($key); // Make sure delete option was called for both the transient and the timeout $this->assertEquals(2, $a->get_call_count()); $expected = array(array('action' => 'action', 'tag' => 'delete_option', 'args' => array($transient_option)), array('action' => 'action', 'tag' => 'delete_option', 'args' => array($timeout))); $this->assertEquals($expected, $a->get_events()); }
/** * @ticket 30380 */ function test_nonexistent_key_old_timeout() { if (is_multisite()) { $this->markTestSkipped('Not testable in MS: wpmu_create_blog() defines WP_INSTALLING.'); } if (wp_using_ext_object_cache()) { $this->markTestSkipped('Not testable with an external object cache.'); } // Create a transient $key = 'test_transient'; set_transient($key, 'test', 60 * 10); $this->assertEquals('test', get_transient($key)); // Make sure the timeout option returns false $timeout = '_transient_timeout_' . $key; $transient_option = '_transient_' . $key; add_filter('option_' . $timeout, '__return_zero'); // Mock an action for tracking action calls $a = new MockAction(); // Add some actions to make sure options are deleted add_action('delete_option', array($a, 'action')); // Act get_transient($key); // Make sure delete option was called for both the transient and the timeout $this->assertEquals(2, $a->get_call_count()); $expected = array(array('action' => 'action', 'tag' => 'delete_option', 'args' => array($transient_option)), array('action' => 'action', 'tag' => 'delete_option', 'args' => array($timeout))); $this->assertEquals($expected, $a->get_events()); }
/** * Test that hooks are fired when post gets stuck and unstuck. * * @ticket 35600 */ function test_hooks_fire_when_post_gets_stuck_and_unstuck() { $post_id = self::factory()->post->create(); $a1 = new MockAction(); $a2 = new MockAction(); $this->assertFalse(is_sticky($post_id)); add_action('post_stuck', array($a1, 'action')); add_action('post_unstuck', array($a2, 'action')); stick_post($post_id); $this->assertTrue(is_sticky($post_id)); unstick_post($post_id); $this->assertFalse(is_sticky($post_id)); remove_action('post_stuck', array($a1, 'action')); remove_action('post_unstuck', array($a2, 'action')); $this->assertEquals(1, $a1->get_call_count()); $this->assertEquals(1, $a2->get_call_count()); }
/** * @ticket 21169 */ function test_filter_removal_during_filter() { $tag = rand_str(); $a = new MockAction(); $b = new MockAction(); add_action( $tag, array($a, 'filter_append'), 11, 1 ); add_action( $tag, array($b, 'filter_append'), 12, 1 ); add_action( $tag, array($this, '_self_removal'), 10, 1 ); $result = apply_filters($tag, $tag); $this->assertEquals( 1, $a->get_call_count(), 'priority 11 filters should run after priority 10 empties itself' ); $this->assertEquals( 1, $b->get_call_count(), 'priority 12 filters should run after priority 10 empties itself and priority 11 runs' ); $this->assertEquals( $result, $tag . '_append_append', 'priority 11 and 12 filters should run after priority 10 empties itself' ); }
public function test_changing_batch_count_limit() { $store = new ActionScheduler_wpPostStore(); $runner = new ActionScheduler_QueueRunner($store); $random = md5(rand()); $schedule = new ActionScheduler_SimpleSchedule(new DateTime('1 day ago')); for ($i = 0; $i < 30; $i++) { $action = new ActionScheduler_Action($random, array($random), $schedule); $store->save_action($action); } $claims = array(); for ($i = 0; $i < 5; $i++) { $claims[] = $store->stake_claim(5); } $mock1 = new MockAction(); add_action($random, array($mock1, 'action')); $actions_run = $runner->run(); remove_action($random, array($mock1, 'action')); $this->assertEquals(0, $mock1->get_call_count()); $this->assertEquals(0, $actions_run); add_filter('action_scheduler_max_claims', array($this, 'return_6')); $mock2 = new MockAction(); add_action($random, array($mock2, 'action')); $actions_run = $runner->run(); remove_action($random, array($mock2, 'action')); $this->assertEquals(5, $mock2->get_call_count()); $this->assertEquals(5, $actions_run); remove_filter('action_scheduler_max_claims', array($this, 'return_6')); for ($i = 0; $i < 5; $i++) { // to make up for the actions we just processed $action = new ActionScheduler_Action($random, array($random), $schedule); $store->save_action($action); } $mock3 = new MockAction(); add_action($random, array($mock3, 'action')); $actions_run = $runner->run(); remove_action($random, array($mock3, 'action')); $this->assertEquals(0, $mock3->get_call_count()); $this->assertEquals(0, $actions_run); remove_filter('action_scheduler_max_claims', array($this, 'return_6')); }