Skip to content

Commit b5e25aa

Browse files
committed
- add failing test
1 parent e8b265b commit b5e25aa

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/wpunit/NodeByUriTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,4 +2508,74 @@ public function testQueryPostsOfMultiplePostTypesBySameSlug() {
25082508
]
25092509
);
25102510
}
2511+
2512+
/**
2513+
* @see: https://github.com/wp-graphql/wp-graphql/issues/3240
2514+
* @return void
2515+
*/
2516+
public function testQueryPostWithCustomPermalinkStructure() {
2517+
2518+
$post_id = $this->factory()->post->create([
2519+
'post_type' => 'post',
2520+
'post_status' => 'publish',
2521+
'post_name' => 'test-post',
2522+
]);
2523+
2524+
$this->set_permalink_structure( '/news/%postname%/' );
2525+
2526+
$query = '
2527+
query GetNodeByUri($uri: String!) {
2528+
nodeByUri(uri: $uri) {
2529+
__typename
2530+
... on Page {
2531+
id
2532+
title
2533+
slug
2534+
}
2535+
... on Post {
2536+
id
2537+
title
2538+
slug
2539+
}
2540+
uri
2541+
}
2542+
}
2543+
';
2544+
2545+
$actual = $this->graphql(
2546+
[
2547+
'query' => $query,
2548+
'variables' => [
2549+
'uri' => '/news/test-post',
2550+
],
2551+
]
2552+
);
2553+
2554+
$this->assertArrayNotHasKey( 'errors', $actual );
2555+
$this->assertQuerySuccessful( $actual, [
2556+
$this->expectedField( 'nodeByUri.__typename', 'Post' ),
2557+
$this->expectedField( 'nodeByUri.slug', 'test-post' ),
2558+
] );
2559+
2560+
// query again with just the slug instead of full uri
2561+
$query2 = $this->graphql(
2562+
[
2563+
'query' => $query,
2564+
'variables' => [
2565+
'uri' => 'test-post',
2566+
],
2567+
]
2568+
);
2569+
2570+
// The query should succeed, but should return null as no post should have been found.
2571+
// WordPress will 404 here as there is no post at `/test` due to the permalink configuration,
2572+
// so we should return null
2573+
$this->assertArrayNotHasKey( 'errors', $query2 );
2574+
$this->assertQuerySuccessful( $query2, [
2575+
$this->expectedField( 'nodeByUri', self::IS_NULL ),
2576+
] );
2577+
2578+
wp_delete_post( $post_id, true );
2579+
2580+
}
25112581
}

0 commit comments

Comments
 (0)