Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: querying posts by slug or uri when the post name is non-ascii #2851

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Data/NodeResolver.php
Expand Up @@ -74,9 +74,9 @@ public function validate_post( WP_Post $post ) {
return $post;
}

// if the uri doesn't have the post's name or ID in it, we must've found something we didn't expect
// if the uri doesn't have the post's urlencoded name or ID in it, we must've found something we didn't expect
// so we will return null
if ( false === strpos( $this->wp->query_vars['uri'], $post->post_name ) && false === strpos( $this->wp->query_vars['uri'], (string) $post->ID ) ) {
if ( false === strpos( $this->wp->query_vars['uri'], (string) $post->ID ) && false === strpos( $this->wp->query_vars['uri'], urldecode( sanitize_title( $post->post_name ) ) ) ) {
return null;
}

Expand Down Expand Up @@ -210,6 +210,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {
return $node;
}


// Resolve Post Objects.
if ( $queried_object instanceof WP_Post ) {
// If Page for Posts is set, we need to return the Page archive, not the page.
Expand All @@ -230,6 +231,7 @@ public function resolve_uri( string $uri, $extra_query_vars = '' ) {

// Validate the post before returning it.
if ( ! $this->validate_post( $queried_object ) ) {

return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Model/Post.php
Expand Up @@ -527,7 +527,7 @@ protected function init() {
return ! empty( $this->data->ping_status ) ? $this->data->ping_status : null;
},
'slug' => function () {
return ! empty( $this->data->post_name ) ? $this->data->post_name : null;
return ! empty( $this->data->post_name ) ? urldecode( $this->data->post_name ) : null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid too many return statements within this method.

},
'template' => function () {

Expand Down Expand Up @@ -683,7 +683,7 @@ protected function init() {
$link = get_permalink( $this->data->ID );
}

return ! empty( $link ) ? $link : null;
return ! empty( $link ) ? urldecode( $link ) : null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid too many return statements within this method.

},
'uri' => function () {
$uri = $this->link;
Expand Down
100 changes: 100 additions & 0 deletions tests/wpunit/PostObjectQueriesTest.php
Expand Up @@ -2243,4 +2243,104 @@ public function testQueryNonPostsAsPostReturnsNull() {
$this->assertNull( $actual['data']['post'] );
}

// create a post using emoji
public function testQueryPostBySlugWithEmojiSlug() {

$raw_title = '🍌';

// the $encoded_slug will have a dash between words i.e. 'سلا-دنیا'
$encoded_slug = urldecode( sanitize_title( $raw_title ) );

$non_ascii_post = $this->factory()->post->create_and_get([
'post_title' => $raw_title,
'post_status' => 'publish',
'post_author' => $this->admin,
]);

codecept_debug( [
'$non_ascii_string' => $raw_title,
'$encoded_slug' => $encoded_slug,
'$post_name' => $non_ascii_post->post_name,
'post' => $non_ascii_post,
]);

$query = '
query getPostBySlug( $id: ID! ) {
post( id: $id idType: SLUG ) {
__typename
title
slug
databaseId
}
}
';

// query the post by (encoded) slug
$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => $encoded_slug, // سلام-دنیا //
]
]);

// assert that the response is what we expect
self::assertQuerySuccessful($actual, [
$this->expectedField( 'post.__typename', 'Post' ),
$this->expectedField( 'post.title', $raw_title ),
$this->expectedField( 'post.slug', $encoded_slug ),
$this->expectedField( 'post.databaseId', $non_ascii_post->ID ),
]);

}

// create a post using unicode
public function testQueryPostBySlugWithNonAsciiSlug() {

$raw_title = 'سلام دنیا';

// the $encoded_slug will have a dash between words i.e. 'سلا-دنیا'
$encoded_slug = urldecode( sanitize_title( $raw_title ) );

$non_ascii_post = $this->factory()->post->create_and_get([
'post_title' => $raw_title,
'post_status' => 'publish',
'post_author' => $this->admin,
]);

codecept_debug( [
'$non_ascii_string' => $raw_title,
'$encoded_slug' => $encoded_slug,
'$post_name' => $non_ascii_post->post_name,
'post' => $non_ascii_post,
]);

$query = '
query getPostBySlug( $id: ID! ) {
post( id: $id idType: SLUG ) {
__typename
title
slug
databaseId
}
}
';

// query the post by (encoded) slug
$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => $encoded_slug, // سلام-دنیا //
]
]);

// assert that the response is what we expect
self::assertQuerySuccessful($actual, [
$this->expectedField( 'post.__typename', 'Post' ),
$this->expectedField( 'post.title', $raw_title ),
$this->expectedField( 'post.slug', $encoded_slug ),
$this->expectedField( 'post.databaseId', $non_ascii_post->ID ),
]);

}

}