Skip to content

Commit 6e75e36

Browse files
authored
Fix private posts check for non-permitted user (#39)
* Test case proves an error. * Fix * Additional test case for privileged user to see drafts
1 parent 255f8a2 commit 6e75e36

File tree

6 files changed

+147
-3
lines changed

6 files changed

+147
-3
lines changed

includes/class-wp-request-processor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public function pre_handle_404( $preempt, $wp_query ) {
8787
}
8888

8989
$post = $wp_query->post;
90+
if ( null === $post ) {
91+
return false;
92+
}
9093

9194
// Analyse only if custom field used in query.
9295
if ( ! array_key_exists( self::PARAM_CUSTOMFIELD_PARAMS, $wp_query->query_vars )

test/integration/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function _manually_load_plugin() {
3333
require 'class-basetestcase.php';
3434
require 'class-permalinksteps.php';
3535
require 'class-customposttypesteps.php';
36+
require 'class-authsteps.php';
3637
require 'class-permalinkasserter.php';
3738
require 'class-navigationasserter.php';
3839

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Tests util file.
4+
*
5+
* @package WordPress_Custom_Fields_Permalink
6+
*/
7+
8+
/**
9+
* Class AuthSteps contains utility methods for authentication.
10+
*/
11+
class AuthSteps {
12+
13+
/**
14+
* AuthSteps constructor.
15+
*/
16+
public function __construct() {
17+
}
18+
19+
/**
20+
* Logged as given user and password.
21+
*
22+
* @param string $username User name.
23+
* @throws Exception When authentication fails.
24+
*/
25+
public function given_logged_as( $username ) {
26+
$result = get_user_by( 'login', $username );
27+
28+
if ( ! ( $result instanceof WP_User ) ) {
29+
throw new Exception( "Couldn't login user" );
30+
}
31+
32+
wp_set_current_user( $result->ID, $result->user_login );
33+
}
34+
35+
/**
36+
* Logged as admin.
37+
*/
38+
public function given_logged_as_admin() {
39+
$this->given_logged_as( 'admin', 'password' );
40+
}
41+
}

test/integration/class-basetestcase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class BaseTestCase extends WP_UnitTestCase {
2424
*/
2525
protected $custom_post_type_steps;
2626

27+
/**
28+
* The AuthSteps.
29+
*
30+
* @var AuthSteps
31+
*/
32+
protected $auth_steps;
33+
2734
/**
2835
* The PermalinkAsserter.
2936
*
@@ -46,6 +53,7 @@ public function setUp() {
4653

4754
$this->permalink_steps = new PermalinkSteps( $this );
4855
$this->custom_post_type_steps = new CustomPostTypeSteps( $this );
56+
$this->auth_steps = new AuthSteps( $this );
4957
$this->permalink_asserter = new PermalinkAsserter( $this );
5058
$this->navigation_asserter = new NavigationAsserter( $this );
5159
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Tests case.
4+
*
5+
* @package WordPress_Custom_Fields_Permalink
6+
*/
7+
8+
namespace CustomFieldsPermalink\Tests\Integration\MetaKeyPermalinkStructure;
9+
10+
use BaseTestCase;
11+
12+
/**
13+
* Class PrivatePostWithMetaKey
14+
*/
15+
class PrivatePostWithMetaKey extends BaseTestCase {
16+
17+
/**
18+
* Test case.
19+
*/
20+
function test_generates_permalink_to_private_post() {
21+
// given.
22+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
23+
24+
$post_params = array(
25+
'post_title' => 'Some post title',
26+
'post_status' => 'private',
27+
'meta_input' => array(
28+
'some_meta_key' => 'Some meta value',
29+
'some_other_meta_key' => 'Some other meta value',
30+
),
31+
);
32+
$created_post_id = $this->factory()->post->create( $post_params );
33+
34+
// when & then.
35+
$this->permalink_asserter->has_permalink( $created_post_id, '/some-meta-value/some-post-title/' );
36+
}
37+
38+
/**
39+
* Test case.
40+
*/
41+
function test_not_go_to_private_post_using_meta_key_permalink_structure_as_anonymous_user() {
42+
// given.
43+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
44+
45+
$post_params = array(
46+
'post_title' => 'Some post title',
47+
'post_status' => 'private',
48+
'meta_input' => array(
49+
'some_meta_key' => 'Some meta value',
50+
'some_other_meta_key' => 'Some other meta value',
51+
),
52+
);
53+
$created_post_id = $this->factory()->post->create( $post_params );
54+
55+
// when.
56+
$this->go_to( '/some-meta-value/some-post-title/' );
57+
58+
// then.
59+
$this->navigation_asserter->then_not_displayed_post( $created_post_id )
60+
->and_also()
61+
->then_is_404();
62+
}
63+
64+
/**
65+
* Test case.
66+
*/
67+
function test_go_to_private_post_using_meta_key_permalink_structure_as_admin_user() {
68+
// given.
69+
$this->auth_steps->given_logged_as_admin();
70+
$this->permalink_steps->given_permalink_structure( '/%field_some_meta_key%/%postname%/' );
71+
72+
$post_params = array(
73+
'post_title' => 'Some post title',
74+
'post_status' => 'private',
75+
'meta_input' => array(
76+
'some_meta_key' => 'Some meta value',
77+
'some_other_meta_key' => 'Some other meta value',
78+
),
79+
);
80+
$created_post_id = $this->factory()->post->create( $post_params );
81+
82+
// when.
83+
$this->go_to( '/some-meta-value/some-post-title/' );
84+
85+
// then.
86+
$this->navigation_asserter->then_displayed_post( $created_post_id );
87+
}
88+
}

test/integration/suites/PermalinkWithAttributesStructure/PostWithMetaKey.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ function test_generates_permalink_to_post_using_meta_key() {
7272
$this->permalink_asserter->has_permalink( $created_post_id, '/some-meta-value/some-post-title/' );
7373

7474
$this->assertThatHookWasCalledWith(
75-
'some_meta_key', 'Some meta value',
75+
'some_meta_key',
76+
'Some meta value',
7677
array( 'some_attribute' => true ),
7778
$created_post_id
7879
);
@@ -103,7 +104,8 @@ function test_go_to_post_using_meta_key_permalink_structure() {
103104
$this->navigation_asserter->then_displayed_post( $created_post_id );
104105

105106
$this->assertThatHookWasCalledWith(
106-
'some_meta_key', 'Some meta value',
107+
'some_meta_key',
108+
'Some meta value',
107109
array( 'some_attribute' => true ),
108110
$created_post_id
109111
);
@@ -134,7 +136,8 @@ function test_go_to_post_using_meta_key_permalink_structure_multiple_attributes(
134136
$this->navigation_asserter->then_displayed_post( $created_post_id );
135137

136138
$this->assertThatHookWasCalledWith(
137-
'some_meta_key', 'Some meta value',
139+
'some_meta_key',
140+
'Some meta value',
138141
array(
139142
'some_attribute' => true,
140143
'some_second_attribute' => 'some value',

0 commit comments

Comments
 (0)