Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize hooks tests to coding standards and consistency #5849

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
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
833 changes: 0 additions & 833 deletions tests/phpunit/tests/actions.php

This file was deleted.

542 changes: 0 additions & 542 deletions tests/phpunit/tests/filters.php

This file was deleted.

24 changes: 24 additions & 0 deletions tests/phpunit/tests/hooks/addAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Test add_action().
*
* @group hooks
* @covers ::add_action
*/
class Tests_Hooks_AddAction extends WP_UnitTestCase {
/**
* @ticket 23265
*/
public function test_action_callback_representations() {
$hook_name = __FUNCTION__;

$this->assertFalse( has_action( $hook_name ) );

add_action( $hook_name, array( 'Class', 'method' ) );

$this->assertSame( 10, has_action( $hook_name, array( 'Class', 'method' ) ) );

$this->assertSame( 10, has_action( $hook_name, 'Class::method' ) );
}
}
202 changes: 172 additions & 30 deletions tests/phpunit/tests/hooks/applyFilters.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,88 @@
<?php

/**
* Test the apply_filters method of WP_Hook
* Test apply_filters().
*
* @group hooks
* @covers WP_Hook::apply_filters
* @covers ::apply_filters
*/
class Tests_Hooks_ApplyFilters extends WP_UnitTestCase {
private $current_priority;

public function test_apply_filters_with_callback() {
$a = new MockAction();
$callback = array( $a, 'filter' );
$hook = new WP_Hook();
$hook_name = __FUNCTION__;
$priority = 1;
$accepted_args = 2;
$arg = __FUNCTION__ . '_arg';
public function test_simple_filter() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';

add_filter( $hook_name, array( $a, 'filter' ) );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );

// Only one event occurred for the hook, with empty args.
$this->assertSame( 1, $a->get_call_count() );
// Only our hook was called.
$this->assertSame( array( $hook_name ), $a->get_hook_names() );

$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
$argsvar = $a->get_args();
$args = array_pop( $argsvar );
$this->assertSame( array( $val ), $args );
}

// One tag with multiple filters.
public function test_multiple_filters() {
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';

// Add both filters to the hook.
add_filter( $hook_name, array( $a1, 'filter' ) );
add_filter( $hook_name, array( $a2, 'filter' ) );

$this->assertSame( $val, apply_filters( $hook_name, $val ) );

// Both filters called once each.
$this->assertSame( 1, $a1->get_call_count() );
$this->assertSame( 1, $a2->get_call_count() );
}

public function test_filter_args_1() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
$arg1 = __FUNCTION__ . '_arg1';

$returned = $hook->apply_filters( $arg, array( $arg ) );
add_filter( $hook_name, array( $a, 'filter' ), 10, 2 );
// Call the filter with a single argument.
$this->assertSame( $val, apply_filters( $hook_name, $val, $arg1 ) );

$this->assertSame( $returned, $arg );
$this->assertSame( 1, $a->get_call_count() );
$argsvar = $a->get_args();
$this->assertSame( array( $val, $arg1 ), array_pop( $argsvar ) );
}

public function test_apply_filters_with_multiple_calls() {
$a = new MockAction();
$callback = array( $a, 'filter' );
$hook = new WP_Hook();
$hook_name = __FUNCTION__;
$priority = 1;
$accepted_args = 2;
$arg = __FUNCTION__ . '_arg';
public function test_filter_args_2() {
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
$arg1 = __FUNCTION__ . '_arg1';
$arg2 = __FUNCTION__ . '_arg2';

$hook->add_filter( $hook_name, $callback, $priority, $accepted_args );
// $a1 accepts two arguments, $a2 doesn't.
add_filter( $hook_name, array( $a1, 'filter' ), 10, 3 );
add_filter( $hook_name, array( $a2, 'filter' ) );
// Call the filter with two arguments.
$this->assertSame( $val, apply_filters( $hook_name, $val, $arg1, $arg2 ) );

$returned_one = $hook->apply_filters( $arg, array( $arg ) );
$returned_two = $hook->apply_filters( $returned_one, array( $returned_one ) );
// $a1 should be called with both args.
$this->assertSame( 1, $a1->get_call_count() );
$argsvar1 = $a1->get_args();
$this->assertSame( array( $val, $arg1, $arg2 ), array_pop( $argsvar1 ) );

$this->assertSame( $returned_two, $arg );
$this->assertSame( 2, $a->get_call_count() );
// $a2 should be called with one only.
$this->assertSame( 1, $a2->get_call_count() );
$argsvar2 = $a2->get_args();
$this->assertSame( array( $val ), array_pop( $argsvar2 ) );
}

/**
Expand All @@ -49,6 +91,8 @@ public function test_apply_filters_with_multiple_calls() {
* @dataProvider data_priority_callback_order_with_integers
* @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
*
* @covers ::apply_filters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This @covers annotation is already on the test class.

*
* @param array $priorities {
* Indexed array of the priorities for the MockAction callbacks.
*
Expand All @@ -60,17 +104,16 @@ public function test_apply_filters_with_multiple_calls() {
*/
public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
$mock = new MockAction();
$hook = new WP_Hook();
$hook_name = __FUNCTION__;

if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
$this->expectDeprecation();
$this->expectDeprecationMessage( $expected_deprecation );
}

$hook->add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0], 1 );
$hook->add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1], 1 );
$hook->apply_filters( __FUNCTION__ . '_val', array( '' ) );
add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0] );
add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1] );
apply_filters( $hook_name, __FUNCTION__ . '_val' );

$this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );

Expand Down Expand Up @@ -170,4 +213,103 @@ public function data_priority_callback_order_with_unhappy_path_nonintegers() {
),
);
}

public function test_all_filter() {
$a = new MockAction();
$hook_name1 = __FUNCTION__ . '_1';
$hook_name2 = __FUNCTION__ . '_2';
$val = __FUNCTION__ . '_val';

// Add an 'all' filter.
add_filter( 'all', array( $a, 'filterall' ) );
// Apply some filters.
$this->assertSame( $val, apply_filters( $hook_name1, $val ) );
$this->assertSame( $val, apply_filters( $hook_name2, $val ) );
$this->assertSame( $val, apply_filters( $hook_name1, $val ) );
$this->assertSame( $val, apply_filters( $hook_name1, $val ) );

// Our filter should have been called once for each apply_filters call.
$this->assertSame( 4, $a->get_call_count() );
// The right hooks should have been called in order.
$this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_hook_names() );

remove_filter( 'all', array( $a, 'filterall' ) );
$this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) );
}

/**
* @ticket 53218
*/
public function test_filter_with_ref_value() {
$obj = new stdClass();
$ref = &$obj;
$a = new MockAction();
$hook_name = __FUNCTION__;

add_action( $hook_name, array( $a, 'filter' ) );

$filtered = apply_filters( $hook_name, $ref );

$args = $a->get_args();
$this->assertSame( $args[0][0], $obj );
$this->assertSame( $filtered, $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][0]->foo );
$this->assertNotEmpty( $filtered->foo );
}

/**
* @ticket 53218
*/
public function test_filter_with_ref_argument() {
$obj = new stdClass();
$ref = &$obj;
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = 'Hello';

add_action( $hook_name, array( $a, 'filter' ), 10, 2 );

apply_filters( $hook_name, $val, $ref );

$args = $a->get_args();
$this->assertSame( $args[0][1], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][1]->foo );
}

/**
* @ticket 39007
*/
public function test_current_priority() {
add_action( 'test_current_priority', array( $this, 'current_priority_action' ), 99 );
do_action( 'test_current_priority' );
remove_action( 'test_current_priority', array( $this, 'current_priority_action' ), 99 );

$this->assertSame( 99, $this->current_priority );
}

public function current_priority_action() {
global $wp_filter;

$this->current_priority = $wp_filter[ current_filter() ]->current_priority();
}

/**
* @ticket 39007
*/
public function test_other_priority() {
add_action( 'test_current_priority', array( $this, 'other_priority_action' ), 99 );
do_action( 'test_current_priority' );
remove_action( 'test_current_priority', array( $this, 'other_priority_action' ), 99 );

$this->assertFalse( $this->current_priority );
}

public function other_priority_action() {
global $wp_filter;
$this->current_priority = $wp_filter['the_content']->current_priority();
}
}
63 changes: 63 additions & 0 deletions tests/phpunit/tests/hooks/applyFiltersDeprecated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Test apply_filters_deprecated().
*
* @group hooks
* @covers ::apply_filters_deprecated
*/
class Tests_Hooks_ApplyFiltersDeprecated extends WP_UnitTestCase {

/**
* @ticket 10441
* @expectedDeprecated tests_apply_filters_deprecated
*/
public function test_apply_filters_deprecated() {
$p = 'Foo';

add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
$p = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p ), '4.6.0' );
remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );

$this->assertSame( 'Bar', $p );
}

public static function deprecated_filter_callback( $p ) {
$p = 'Bar';
return $p;
}

/**
* @ticket 10441
* @expectedDeprecated tests_apply_filters_deprecated
*/
public function test_apply_filters_deprecated_with_multiple_params() {
$p1 = 'Foo1';
$p2 = 'Foo2';

add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
$p1 = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p1, $p2 ), '4.6.0' );
remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );

$this->assertSame( 'Bar1', $p1 );

// Not passed by reference, so not modified.
$this->assertSame( 'Foo2', $p2 );
}

public static function deprecated_filter_callback_multiple_params( $p1, $p2 ) {
$p1 = 'Bar1';
$p2 = 'Bar2';

return $p1;
}

/**
* @ticket 10441
*/
public function test_apply_filters_deprecated_without_filter() {
$val = 'Foobar';

$this->assertSame( $val, apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $val ), '4.6.0' ) );
}
}
58 changes: 58 additions & 0 deletions tests/phpunit/tests/hooks/applyFiltersRefArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Test apply_filters_ref_array().
*
* @group hooks
* @covers ::apply_filters_ref_array
*/
class Tests_Hooks_ApplyFiltersRefArray extends WP_UnitTestCase {

/**
* @ticket 9886
*/
public function test_filter_ref_array() {
$obj = new stdClass();
$a = new MockAction();
$hook_name = __FUNCTION__;

add_action( $hook_name, array( $a, 'filter' ) );

apply_filters_ref_array( $hook_name, array( &$obj ) );

$args = $a->get_args();
$this->assertSame( $args[0][0], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][0]->foo );
}

/**
* @ticket 12723
*/
public function test_filter_ref_array_result() {
$obj = new stdClass();
$a = new MockAction();
$b = new MockAction();
$hook_name = __FUNCTION__;

add_action( $hook_name, array( $a, 'filter_append' ), 10, 2 );
add_action( $hook_name, array( $b, 'filter_append' ), 10, 2 );

$result = apply_filters_ref_array( $hook_name, array( 'string', &$obj ) );

$this->assertSame( $result, 'string_append_append' );

$args = $a->get_args();
$this->assertSame( $args[0][1], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][1]->foo );

$args = $b->get_args();
$this->assertSame( $args[0][1], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][1]->foo );
}
}
Loading
Loading