-
Notifications
You must be signed in to change notification settings - Fork 6
/
FilterWPQueryTest.php
executable file
·124 lines (111 loc) · 3.39 KB
/
FilterWPQueryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace CalderaLearn\RestSearch\Tests\Integration;
use CalderaLearn\RestSearch\FilterWPQuery;
use CalderaLearn\RestSearch\Tests\Mock\AlwaysFilterWPQuery;
/**
* Class FilterWPQueryTest
*
* Test interaction of FilterWPQuery with WordPress plugins API
*
* @package CalderaLearn\RestSearch\Tests\Integration
*/
class FilterWPQueryTest extends IntegrationTestCase
{
/**
* Test adding the filter
*
* @covers FilterWPQuery::addFilter()
*/
public function testAddFilter()
{
//Add filter
$this->assertTrue(FilterWPQuery::addFilter());
//Make sure addFilter() had the right effect -- it was added with priority 10
$this->assertEquals(
FilterWPQuery::getFilterPriority(),
has_filter('posts_pre_query', [FilterWPQuery::class, 'filterPreQuery'])
);
}
/**
* Test removing the filter
*
* @covers FilterWPQuery::shouldFilter()
*/
public function testFilterRemoved()
{
//Add filter
FilterWPQuery::addFilter();
//Remove and test return type
$this->assertTrue(FilterWPQuery::removeFilter());
//Make sure removeFilter() had the right effect -- the filter was removed
$this->assertFalse(has_filter('posts_pre_query', [FilterWPQuery::class, 'filterPreQuery']));
}
/**
* Test that by default this class does not do anything by default
*
* @covers FilterWPQuery::shouldFilter()
* @covers FilterWPQuery::filterPreQuery()
*/
public function testNotFilteringByDefault()
{
//Add one post and save its title and ID in variables for comparing to
$postTitle = 'The expected post title';
$postId = $this->factory->post->create(['post_title' => $postTitle]);
//Add filter
FilterWPQuery::addFilter();
//Test that the filter SHOULD not do anything
$this->assertFalse(FilterWPQuery::shouldFilter([]));
//Query for all posts -- should only be one post, the one we just created.
$query = new \WP_Query(['post_type' => 'post']);
$this->assertFalse(empty($query->posts));
$this->assertEquals(1, count($query->posts[0]));
$this->assertEquals($postId, $query->posts[0]->ID);
$this->assertEquals($postTitle, $query->posts[0]->post_title);
}
/**
* Test that the getPosts method return an array
*
* @covers \CalderaLearn\RestSearch\FilterWPQuery::getPosts()
*/
public function testGetPosts()
{
//Get the mock posts
$results = FilterWPQuery::getPosts();
//Make sure results are an array
$this->assertTrue(is_array($results));
}
/**
* Test that the getPosts method returns an array of WP_Posts.
*
* @covers \CalderaLearn\RestSearch\FilterWPQuery::getPosts()
*/
public function testGetPostsArePosts()
{
//Get the mock posts
$results = FilterWPQuery::getPosts();
$this->assertFalse(empty($results));
//Make sure results are an array of WP_Posts
$looped = false;
foreach ($results as $result) {
$looped = true;
//Make sure all results are WP_Posts
$this->assertTrue(is_a($result, '\WP_Post'), get_class($result));
}
//Make sure loop ran
$this->assertTrue($looped);
}
/**
* Test that the getPosts method does filter when it is explicitly set to so.
*
* @covers \CalderaLearn\RestSearch\FilterWPQuery::getPosts()
*/
public function testGetPostsArePostsShouldFilter()
{
//Get the mock posts
$results = AlwaysFilterWPQuery::filterPreQuery(null);
$this->assertTrue(is_array($results));
$this->assertFalse(empty($results));
$expected = AlwaysFilterWPQuery::getPosts();
$this->assertEquals(count($expected), count($results));
}
}