Skip to content

Commit

Permalink
Merge pull request #2792 from jasonbahl/fix/#2791-node-by-uri-posts-page
Browse files Browse the repository at this point in the history
fix: uri field is null when querying the page for posts uri
  • Loading branch information
jasonbahl committed Apr 19, 2023
2 parents e52d00c + 87e19fb commit 5c88404
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/Data/NodeResolver.php
Expand Up @@ -201,8 +201,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {
return null;
}

/** @todo resolve to an Archive Type. */
$post_type_object = get_post_type_object( $queried_object->post_type );
$post_type_object = get_post_type_object( 'post' );

if ( ! $post_type_object ) {
return null;
Expand Down
57 changes: 54 additions & 3 deletions tests/wpunit/NodeByUriTest.php
Expand Up @@ -1084,13 +1084,13 @@ public function testPostFormatByUri() {
codecept_debug( $uri );

$term = get_term_by('slug', 'post-format-aside', 'post_format');

/**
* NodeResolver::parse_request() will generate the following query vars:
* uri => /type/aside/
* post_format => post-format-aside
* post_type => [ post ]
*
*
*/
$actual = $this->graphql([
'query' => $query,
Expand Down Expand Up @@ -1669,7 +1669,7 @@ public function testPageQueryWhenPageIsSetToHomePage() {
}

/**
* Tests the Posts Archive Page URI
* Tests the Posts Archive Page URI
*/
public function testPageForPostsByUri() {

Expand Down Expand Up @@ -1894,6 +1894,57 @@ public function testNodeByUriWithCustomPermalinkStructureAndFrontPageSet() {
// cleanup
update_option( 'page_on_front', 0 );
update_option( 'show_on_front', 0 );
wp_delete_post( $page_id, true );

}

public function testQueryPostsPageByUriReturnsExpectedUriOnNode() {

$page_id = $this->factory()->post->create([
'post_type' => 'page',
'post_status' => 'publish',
]);

$page_for_posts = $this->factory()->post->create([
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Page for Posts'
]);

update_option( 'page_on_front', $page_id );
update_option( 'show_on_front', 'page' );
update_option( 'page_for_posts', $page_for_posts );

$query = '
query GetPostsPage( $uri: String! ) {
nodeByUri(uri: $uri) {
__typename
uri
}
}
';

$uri = parse_url( get_permalink( $page_for_posts ), PHP_URL_PATH );

$variables = [
'uri' => $uri
];

codecept_debug( [
'variables' => $variables,
]);

$actual = $this->graphql([
'query' => $query,
'variables' => $variables
]);

self::assertQuerySuccessful( $actual, [
// the query should return a null value as the uri
// cannot be found
$this->expectedField( 'nodeByUri.__typename', 'ContentType' ),
$this->expectedField( 'nodeByUri.uri', $uri ),
] );

}

Expand Down

0 comments on commit 5c88404

Please sign in to comment.