forked from WordPress/wordpress-develop
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPostsByAuthorSql.php
151 lines (116 loc) · 5.99 KB
/
getPostsByAuthorSql.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @group post
*/
class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
public function test_post_type_post() {
$maybe_string = get_posts_by_author_sql( 'post' );
$this->assertStringContainsString( "post_type = 'post'", $maybe_string );
}
public function test_post_type_page() {
$maybe_string = get_posts_by_author_sql( 'page' );
$this->assertStringContainsString( "post_type = 'page'", $maybe_string );
}
public function test_non_existent_post_type() {
$maybe_string = get_posts_by_author_sql( 'non_existent_post_type' );
$this->assertStringContainsString( '1 = 0', $maybe_string );
}
public function test_multiple_post_types() {
register_post_type( 'foo' );
register_post_type( 'bar' );
$maybe_string = get_posts_by_author_sql( array( 'foo', 'bar' ) );
$this->assertStringContainsString( "post_type = 'foo'", $maybe_string );
$this->assertStringContainsString( "post_type = 'bar'", $maybe_string );
_unregister_post_type( 'foo' );
_unregister_post_type( 'bar' );
}
public function test_full_true() {
$maybe_string = get_posts_by_author_sql( 'post', true );
$this->assertMatchesRegularExpression( '/^WHERE /', $maybe_string );
}
public function test_full_false() {
$maybe_string = get_posts_by_author_sql( 'post', false );
$this->assertDoesNotMatchRegularExpression( '/^WHERE /', $maybe_string );
}
public function test_post_type_clause_should_be_included_when_full_is_true() {
$maybe_string = get_posts_by_author_sql( 'post', true );
$this->assertStringContainsString( "post_type = 'post'", $maybe_string );
}
public function test_post_type_clause_should_be_included_when_full_is_false() {
$maybe_string = get_posts_by_author_sql( 'post', false );
$this->assertStringContainsString( "post_type = 'post'", $maybe_string );
}
public function test_post_author_should_create_post_author_clause() {
$maybe_string = get_posts_by_author_sql( 'post', true, 1 );
$this->assertStringContainsString( 'post_author = 1', $maybe_string );
}
public function test_public_only_true_should_not_allow_any_private_posts_for_loggedin_user() {
$current_user = get_current_user_id();
$u = self::factory()->user->create();
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( 'post', true, $u, true );
$this->assertStringNotContainsString( "post_status = 'private'", $maybe_string );
wp_set_current_user( $current_user );
}
public function test_public_only_should_default_to_false() {
$current_user = get_current_user_id();
$u = self::factory()->user->create();
wp_set_current_user( $u );
$this->assertSame( get_posts_by_author_sql( 'post', true, $u, false ), get_posts_by_author_sql( 'post', true, $u ) );
wp_set_current_user( $current_user );
}
public function test_public_only_false_should_allow_current_user_access_to_own_private_posts_when_current_user_matches_post_author() {
$current_user = get_current_user_id();
$u = self::factory()->user->create();
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
$this->assertStringContainsString( "post_status = 'private'", $maybe_string );
wp_set_current_user( $current_user );
}
public function test_public_only_false_should_not_allow_access_to_private_posts_if_current_user_is_not_post_author() {
$current_user = get_current_user_id();
$u1 = self::factory()->user->create();
$u2 = self::factory()->user->create();
wp_set_current_user( $u1 );
$maybe_string = get_posts_by_author_sql( 'post', true, $u2, false );
$this->assertStringNotContainsString( "post_status = 'private'", $maybe_string );
wp_set_current_user( $current_user );
}
public function test_public_only_false_should_allow_current_user_access_to_own_private_posts_when_post_author_is_not_provided() {
$current_user = get_current_user_id();
$u = self::factory()->user->create();
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
$this->assertStringContainsString( "post_status = 'private'", $maybe_string );
$this->assertStringContainsString( "post_author = $u", $maybe_string );
wp_set_current_user( $current_user );
}
public function test_administrator_should_have_access_to_private_posts_when_public_only_is_false() {
$current_user = get_current_user_id();
$u = self::factory()->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( 'post', true, null, false );
$this->assertStringContainsString( "post_status = 'private'", $maybe_string );
$this->assertStringNotContainsString( 'post_author', $maybe_string );
wp_set_current_user( $current_user );
}
public function test_user_has_access_only_to_private_posts_for_certain_post_types() {
register_post_type( 'foo', array( 'capabilities' => array( 'read_private_posts' => 'read_private_foo' ) ) );
register_post_type( 'bar', array( 'capabilities' => array( 'read_private_posts' => 'read_private_bar' ) ) );
register_post_type( 'baz', array( 'capabilities' => array( 'read_private_posts' => 'read_private_baz' ) ) );
$current_user = get_current_user_id();
$u = self::factory()->user->create( array( 'role' => 'editor' ) );
$editor_role = get_role( 'editor' );
$editor_role->add_cap( 'read_private_baz' );
wp_set_current_user( $u );
$maybe_string = get_posts_by_author_sql( array( 'foo', 'bar', 'baz' ) );
$editor_role->remove_cap( 'read_private_baz' );
$this->assertStringNotContainsString( "post_type = 'foo' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
$this->assertStringNotContainsString( "post_type = 'bar' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
$this->assertStringContainsString( "post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
_unregister_post_type( 'foo' );
_unregister_post_type( 'bar' );
_unregister_post_type( 'baz' );
wp_set_current_user( $current_user );
}
}