Skip to content

Commit b9e25bf

Browse files
committed
tests: check additional roles in testPasswordProtectedPost()
1 parent 3b326a5 commit b9e25bf

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

tests/wpunit/PostObjectQueriesTest.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,19 +2411,32 @@ public function testQueryPostBySlugWithNonAsciiSlug() {
24112411
}
24122412

24132413
public function testPasswordProtectedPost() {
2414+
$subscriber = $this->factory()->user->create(
2415+
[
2416+
'role' => 'subscriber',
2417+
]
2418+
);
2419+
2420+
$post_author_one = $this->factory()->user->create(
2421+
[
2422+
'role' => 'author',
2423+
]
2424+
);
2425+
2426+
$post_author_two = $this->factory()->user->create(
2427+
[
2428+
'role' => 'author',
2429+
]
2430+
);
2431+
24142432
$post = $this->factory()->post->create_and_get(
24152433
[
24162434
'post_type' => 'post',
24172435
'post_status' => 'publish',
24182436
'post_password' => 'mypassword',
24192437
'title' => 'Password Protected post',
24202438
'content' => 'Some content',
2421-
]
2422-
);
2423-
2424-
$subscriber = $this->factory()->user->create(
2425-
[
2426-
'role' => 'subscriber',
2439+
'post_author' => $post_author_one,
24272440
]
24282441
);
24292442

@@ -2453,6 +2466,7 @@ public function testPasswordProtectedPost() {
24532466
$this->assertNull( $actual['data']['post']['password'], 'Password should be null when unauthenticated' );
24542467

24552468
// Test password protected post as a subscriber.
2469+
wp_set_current_user( $subscriber );
24562470
$actual = $this->graphql( [
24572471
'query' => $query,
24582472
'variables' => [
@@ -2461,10 +2475,41 @@ public function testPasswordProtectedPost() {
24612475
] );
24622476

24632477
$this->assertArrayNotHasKey( 'errors', $actual );
2464-
$this->assertEquals( $post->post_title, $actual['data']['post']['title'], 'Title should be returned when unauthenticated' );
2465-
$this->assertEquals( 'publish', $actual['data']['post']['status'], 'Status should be "publish" when unauthenticated' );
2466-
$this->assertNull( $actual['data']['post']['content'], 'Content should be null when unauthenticated' );
2467-
$this->assertNull( $actual['data']['post']['password'], 'Password should be null when unauthenticated' );
2478+
$this->assertEquals( $post->post_title, $actual['data']['post']['title'], 'Title should be returned when lacking permissions' );
2479+
$this->assertEquals( 'publish', $actual['data']['post']['status'], 'Status should be "publish" when lacking permissions' );
2480+
$this->assertNull( $actual['data']['post']['content'], 'Content should be null when lacking permissions' );
2481+
$this->assertNull( $actual['data']['post']['password'], 'Password should be null when lacking permissions' );
2482+
2483+
// Test password protected post as different author.
2484+
wp_set_current_user( $post_author_two );
2485+
$actual = $this->graphql( [
2486+
'query' => $query,
2487+
'variables' => [
2488+
'id' => $post->ID,
2489+
],
2490+
] );
2491+
2492+
$this->assertArrayNotHasKey( 'errors', $actual );
2493+
$this->assertEquals( $post->post_title, $actual['data']['post']['title'], 'Title should be returned when lacking permissions' );
2494+
$this->assertEquals( 'publish', $actual['data']['post']['status'], 'Status should be "publish" when lacking permissions' );
2495+
$this->assertNull( $actual['data']['post']['content'], 'Content should be null when lacking permissions' );
2496+
$this->assertNull( $actual['data']['post']['password'], 'Password should be null when lacking permissions' );
2497+
2498+
// Test password protected post as current author.
2499+
wp_set_current_user( $post_author_one );
2500+
2501+
$actual = $this->graphql( [
2502+
'query' => $query,
2503+
'variables' => [
2504+
'id' => $post->ID,
2505+
],
2506+
] );
2507+
2508+
$this->assertArrayNotHasKey( 'errors', $actual );
2509+
$this->assertEquals( $post->post_title, $actual['data']['post']['title'], 'Title should be returned when authenticated' );
2510+
$this->assertEquals( 'publish', $actual['data']['post']['status'], 'Status should be "publish" when authenticated' );
2511+
$this->assertNotEmpty( $actual['data']['post']['content'], 'Content should be returned when authenticated' );
2512+
$this->assertEquals( $post->post_password, $actual['data']['post']['password'], 'Password should be returned when authenticated' );
24682513

24692514
// Test password protected post as admin.
24702515
wp_set_current_user( $this->admin );

0 commit comments

Comments
 (0)