Skip to content
Closed
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
33 changes: 33 additions & 0 deletions tests/phpunit/tests/post/getPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @group query
*/
class Tests_Post_GetPosts extends WP_UnitTestCase {
private $posts_where_filter_fired = false;

public function test_offset_should_be_null_by_default() {
$p1 = self::factory()->post->create(
array(
Expand Down Expand Up @@ -215,4 +217,35 @@ public function test_should_return_parent_ids_keyed_by_post_id_when_cached(): vo
$this->assertSame( $expected, get_posts( $args ), 'The uncached call is not of the expected form.' );
$this->assertSame( $expected, get_posts( $args ), 'The cached call is not of the expected form.' );
}

/**
* @ticket 7326
*/
public function test_should_suppress_posts_where_filter_by_default() {
self::factory()->post->create();

add_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );
get_posts();
remove_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );

$this->assertFalse( $this->posts_where_filter_fired );
Comment on lines +227 to +231

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

MockAction can do the counting here, which would let the extra property and callback go, and the remove_filter() isn't needed since hooks are restored in tear_down().

Suggested change
add_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );
get_posts();
remove_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );
$this->assertFalse( $this->posts_where_filter_fired );
$filter = new MockAction();
add_filter( 'posts_where', array( $filter, 'filter' ) );
get_posts();
$this->assertSame( 0, $filter->get_call_count() );

The second test could do the same with assertSame( 1, ... ). What do you think?

}

/**
* @ticket 7326
*/
public function test_suppress_filters_false_should_apply_posts_where_filter() {
self::factory()->post->create();

add_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );
get_posts( array( 'suppress_filters' => false ) );
remove_filter( 'posts_where', array( $this, 'filter_posts_where_set_flag' ) );

$this->assertTrue( $this->posts_where_filter_fired );
}

public function filter_posts_where_set_flag( $where ) {
$this->posts_where_filter_fired = true;
return $where;
}
}
Loading