Skip to content

Commit a5c41fa

Browse files
Abilities API: Declare the uri format for the link output of core/content-query.
The post permalink in the `link` output was a plain string, while the `link` output of `core/users-query` declares the `uri` format. Declare the same format for the post permalink, so clients that check formats treat both links the same. The value is unchanged. Developed in #15987. Follow-up to [64388]. Props jorgefilipecosta, gziolo. Fixes #66268. git-svn-id: https://develop.svn.wordpress.org/trunk@65724 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3faf586 commit a5c41fa

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎src/wp-includes/abilities/class-wp-abilities-content.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ private function get_post_properties(): array {
10241024
),
10251025
'link' => array(
10261026
'type' => 'string',
1027+
'format' => 'uri',
10271028
'description' => __( 'The permalink URL.' ),
10281029
),
10291030
'title' => array(

‎tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ public function test_output_schema_describes_query_responses(): void {
780780
}
781781
$this->assertStringContainsString( "in ISO 8601 format using the site's timezone.", $post_schema['properties'][ $name ]['properties']['site']['description'], sprintf( 'The %s.site description should say the date uses the site timezone.', $name ) );
782782
}
783+
$this->assertSame( 'uri', $post_schema['properties']['link']['format'], 'The link should use the uri format.' );
783784

784785
$this->assertSame( 'object', $pagination['type'], 'The pagination should describe an object.' );
785786
$this->assertFalse( $pagination['additionalProperties'], 'The pagination should not allow keys besides the totals.' );
@@ -822,6 +823,83 @@ private function find_schema_keywords( array $schema, array $keywords, string $p
822823
return $found;
823824
}
824825

826+
/**
827+
* Returns posts whose permalinks take different forms.
828+
*
829+
* With pretty permalinks, a draft still links to its plain `?p=ID` form, while a
830+
* published post and a private page the current user can read link to their pretty
831+
* permalinks.
832+
*
833+
* @return array<string, array{post_type: string, post_status: string, plain_permalink: bool}> Post test cases.
834+
*/
835+
public static function data_posts_with_different_permalink_forms(): array {
836+
return array(
837+
'published post' => array(
838+
'post_type' => 'post',
839+
'post_status' => 'publish',
840+
'plain_permalink' => false,
841+
),
842+
'draft post' => array(
843+
'post_type' => 'post',
844+
'post_status' => 'draft',
845+
'plain_permalink' => true,
846+
),
847+
'private page' => array(
848+
'post_type' => 'page',
849+
'post_status' => 'private',
850+
'plain_permalink' => false,
851+
),
852+
);
853+
}
854+
855+
/**
856+
* Results validate against the output schema, which declares the `uri` format for the
857+
* link, whatever form the permalink takes.
858+
*
859+
* @ticket 66268
860+
* @since 7.2.0
861+
*
862+
* @dataProvider data_posts_with_different_permalink_forms
863+
*
864+
* @param string $post_type The post type to create.
865+
* @param string $post_status The post status to create.
866+
* @param bool $plain_permalink Whether the post links to its plain `?p=ID` permalink.
867+
*/
868+
public function test_results_validate_against_the_output_schema_for_each_permalink_form( string $post_type, string $post_status, bool $plain_permalink ): void {
869+
$post_id = self::factory()->post->create(
870+
array(
871+
'post_type' => $post_type,
872+
'post_status' => $post_status,
873+
)
874+
);
875+
876+
$this->login_as( 'administrator' );
877+
$this->register_ability();
878+
879+
$ability = wp_get_ability( 'core/content-query' );
880+
$result = $ability->execute(
881+
array(
882+
'where' => array(
883+
'type' => $post_type,
884+
'ids' => array( $post_id ),
885+
),
886+
'fields' => array( 'id', 'type', 'status', 'date', 'modified', 'slug', 'link', 'title', 'excerpt', 'content', 'author', 'parent' ),
887+
)
888+
);
889+
890+
$this->assertIsArray( $result, 'The query should succeed.' );
891+
$this->assertCount( 1, $result['items'], 'The query should return the post.' );
892+
893+
$link = $result['items'][0]['link'];
894+
if ( $plain_permalink ) {
895+
$this->assertSame( home_url( '?p=' . $post_id ), $link, 'Precondition: the post should link to its plain permalink.' );
896+
} else {
897+
$this->assertStringNotContainsString( '?', $link, 'Precondition: the post should link to its pretty permalink.' );
898+
}
899+
$this->assertSame( get_permalink( $post_id ), $link, 'The link should be the permalink.' );
900+
$this->assertTrue( rest_validate_value_from_schema( $result, $ability->get_output_schema(), 'result' ), 'The result should validate against the output schema.' );
901+
}
902+
825903
/**
826904
* A post type registered by another active plugin and flagged `show_in_abilities`
827905
* is exposed by the ability, both in the input enum and in query results.

0 commit comments

Comments
 (0)