From 787ddad1e12c97dc406c6393e08d12aafeca88d7 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 16 Nov 2026 13:51:28 +0000 Subject: [PATCH 01/24] Abilities API: Add the `core/content-create`, `core/content-update` and `core/content-delete` abilities. Add three abilities to the `content` category, for the post types registered with `show_in_abilities`: - `core/content-create` creates a post from `data` and returns it. - `core/content-update` updates the given attributes of the post matched by `where` and returns it. - `core/content-delete` moves the post matched by `where` to the Trash, or deletes it permanently when `permanent` is true. The attributes that can be written are the title, content, excerpt, status, slug, date, author and parent. Each call checks the capabilities of the current user for the post and for the values it sets. --- src/wp-includes/abilities.php | 3 +- .../abilities/class-wp-abilities-content.php | 1292 +++++- .../wpRegisterCoreContentAbility.php | 252 +- .../wpRegisterCoreContentCreateAbility.php | 3062 +++++++++++++ .../wpRegisterCoreContentDeleteAbility.php | 1656 +++++++ .../wpRegisterCoreContentUpdateAbility.php | 3954 +++++++++++++++++ .../wpRestAbilitiesContentController.php | 529 ++- 7 files changed, 10639 insertions(+), 109 deletions(-) create mode 100644 tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php create mode 100644 tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php create mode 100644 tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php diff --git a/src/wp-includes/abilities.php b/src/wp-includes/abilities.php index 835c19e04f949..c5ee2a5ca8c17 100644 --- a/src/wp-includes/abilities.php +++ b/src/wp-includes/abilities.php @@ -50,6 +50,7 @@ function wp_register_core_ability_categories(): void { * * @since 6.9.0 * @since 7.2.0 Added the `core/content-query`, `core/settings-get`, and `core/users-query` abilities. + * @since 7.3.0 Added the `core/content-create`, `core/content-update`, and `core/content-delete` abilities. * * @global wpdb $wpdb WordPress database abstraction object. */ @@ -377,6 +378,6 @@ function wp_register_core_abilities(): void { // Register the user abilities (currently the read-only `core/users-query`). ( new WP_Abilities_Users() )->register(); - // Register the content abilities (currently the read-only `core/content-query`). + // Register the content abilities: `core/content-query`, `core/content-create`, `core/content-update`, and `core/content-delete`. ( new WP_Abilities_Content() )->register(); } diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index a8d9954e30965..614db42083706 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -17,12 +17,15 @@ * status, author, and parent. Raw fields are only returned for posts the current user can * edit, and the author's name and slug only for authors the current user may see. * + * Also registers `core/content-create`, `core/content-update`, and `core/content-delete`, + * which create, update, and trash or delete one post of such a post type, and return the + * written post in the same shape as a `core/content-query` item. + * * Unlike the core abilities registered directly in wp_register_core_abilities() as - * self-contained closures, the content ability lives in a dedicated class because - * its callbacks and schemas share helpers: the permission and execute callbacks read the + * self-contained closures, the content abilities live in a dedicated class because + * their callbacks and schemas share helpers: the permission and execute callbacks read the * post type and the filters through the same code, and the input schema, output schema, - * and field projection are built from the same field definitions. Future write-oriented - * content abilities can reuse them as well. + * and field projection are built from the same field definitions. * * Only register() is public. The ability callbacks are closures that call private * methods, so callers go through the Abilities API, such as @@ -93,7 +96,17 @@ final class WP_Abilities_Content { private const LOOP_GLOBALS = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); /** - * Whether each author seen in the current query has posts the current user may read. + * The `data` attributes the create and update abilities can write. + * + * Only these reach the save, each mapped to one post field. Other keys are never written. + * + * @since 7.3.0 + * @var list + */ + private const WRITE_ATTRIBUTES = array( 'title', 'content', 'excerpt', 'status', 'slug', 'date', 'author', 'parent' ); + + /** + * Whether each author seen in the current call has posts the current user may read. * * @since 7.2.0 * @var array @@ -106,38 +119,44 @@ final class WP_Abilities_Content { * Must run on the `wp_abilities_api_init` hook. * * @since 7.2.0 + * @since 7.3.0 Registers the `core/content-create`, `core/content-update`, and `core/content-delete` abilities. */ public function register(): void { - $this->register_content_query(); - /* - * Future write-oriented content abilities can be registered here, reusing the - * shared helpers below (get_exposed_post_type(), format_post(), check_permission()). + * Post types must be registered with `show_in_abilities` before the abilities are + * registered so they are included in the input schemas. Without one, no content + * ability is registered. */ + $post_types = array_values( get_post_types( array( 'show_in_abilities' => true ) ) ); + if ( empty( $post_types ) ) { + return; + } + + // The statuses that can be set. The query accepts `trash` as well. + $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); + + $this->register_content_query( $post_types ); + $this->register_content_create( $post_types, $statuses ); + $this->register_content_update( $post_types, $statuses ); + $this->register_content_delete( $post_types ); } /** * Registers the read-only `core/content-query` ability. * * @since 7.2.0 + * @since 7.3.0 Added the `$post_types` parameter. + * + * @param list $post_types Exposed post type names. */ - private function register_content_query(): void { - /* - * Post types must be registered with `show_in_abilities` before the ability is - * registered so they are included in its input schema. - */ - $post_types = array_values( get_post_types( array( 'show_in_abilities' => true ) ) ); - if ( empty( $post_types ) ) { - return; - } - + private function register_content_query( array $post_types ): void { /* - * Internal statuses (e.g. `inherit`) are excluded here and from the statuses a query - * covers by default, so the posts of post types that rely on them (attachments) - * cannot be queried, not even by ID. Revisit if such a post type is ever exposed via - * `show_in_abilities`. + * Internal statuses (e.g. `inherit`) are excluded here, other than `trash`, and from + * the statuses a query covers by default, so the posts of post types that rely on them + * (attachments) cannot be queried, not even by ID. Revisit if such a post type is ever + * exposed via `show_in_abilities`. */ - $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); + $statuses = $this->get_queryable_statuses(); wp_register_ability( 'core/content-query', @@ -165,6 +184,110 @@ private function register_content_query(): void { ); } + /** + * Registers the `core/content-create` ability. + * + * @since 7.3.0 + * + * @param list $post_types Exposed post type names. + * @param list $statuses Post status slugs that can be set. + */ + private function register_content_create( array $post_types, array $statuses ): void { + wp_register_ability( + 'core/content-create', + array( + 'label' => __( 'Create Content' ), + 'description' => __( 'Creates a post of a post type exposed to abilities and returns it. Requires an authenticated user who can create posts of that type. The post is saved as a draft unless another status is given.' ), + 'category' => self::CATEGORY, + 'input_schema' => $this->get_content_create_input_schema( $post_types, $statuses ), + 'output_schema' => $this->get_content_output_schema(), + 'execute_callback' => function ( $input = array() ) { + return $this->execute_content_create( $input ); + }, + 'permission_callback' => function ( $input = array() ): bool { + return $this->check_create_permission( $input ); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => false, + 'destructive' => false, + 'idempotent' => false, + ), + 'public' => true, + ), + ) + ); + } + + /** + * Registers the `core/content-update` ability. + * + * @since 7.3.0 + * + * @param list $post_types Exposed post type names. + * @param list $statuses Post status slugs that can be set. + */ + private function register_content_update( array $post_types, array $statuses ): void { + wp_register_ability( + 'core/content-update', + array( + 'label' => __( 'Update Content' ), + 'description' => __( 'Updates the given attributes of a post of a post type exposed to abilities and returns it. Attributes that are left out keep their stored values. Requires an authenticated user who can edit the post.' ), + 'category' => self::CATEGORY, + 'input_schema' => $this->get_content_update_input_schema( $post_types, $statuses ), + 'output_schema' => $this->get_content_output_schema(), + 'execute_callback' => function ( $input = array() ) { + return $this->execute_content_update( $input ); + }, + 'permission_callback' => function ( $input = array() ): bool { + return $this->check_post_permission( $input ); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => false, + 'destructive' => false, + 'idempotent' => true, + ), + 'public' => true, + ), + ) + ); + } + + /** + * Registers the `core/content-delete` ability. + * + * @since 7.3.0 + * + * @param list $post_types Exposed post type names. + */ + private function register_content_delete( array $post_types ): void { + wp_register_ability( + 'core/content-delete', + array( + 'label' => __( 'Delete Content' ), + 'description' => __( 'Moves a post of a post type exposed to abilities to the Trash, or deletes it permanently when `permanent` is true. Requires an authenticated user who can delete the post.' ), + 'category' => self::CATEGORY, + 'input_schema' => $this->get_content_delete_input_schema( $post_types ), + 'output_schema' => $this->get_content_delete_output_schema(), + 'execute_callback' => function ( $input = array() ) { + return $this->execute_content_delete( $input ); + }, + 'permission_callback' => function ( $input = array() ): bool { + return $this->check_post_permission( $input ); + }, + 'meta' => array( + 'annotations' => array( + 'readonly' => false, + 'destructive' => true, + 'idempotent' => true, + ), + 'public' => true, + ), + ) + ); + } + /** * Permission callback for the `core/content-query` ability. * @@ -203,6 +326,50 @@ private function check_permission( $input = array() ): bool { return $this->can_query_statuses( $where, $post_type_object ); } + /** + * Permission callback for the `core/content-create` ability. + * + * A coarse gate: a logged-in user, a `data.type` that is exposed to abilities, and the + * capability to create posts of that type. {@see self::execute_content_create()} checks + * the capability again and makes every other decision. + * + * @since 7.3.0 + * + * @param mixed $input Optional. The ability input. Default empty array. + * @return bool True if the request may proceed, false otherwise. + */ + private function check_create_permission( $input = array() ): bool { + if ( ! is_user_logged_in() ) { + return false; + } + + $data = $this->get_required_input_object( rest_sanitize_object( $input ), 'data' ) ?? array(); + $post_type_object = $this->get_exposed_post_type( $data['type'] ?? null ); + + return null !== $post_type_object && current_user_can( $post_type_object->cap->create_posts ); + } + + /** + * Permission callback for the `core/content-update` and `core/content-delete` abilities. + * + * A coarse gate: a logged-in user and a `where.type` that is exposed to abilities. The + * post and the capabilities on it are checked by the execute callbacks. + * + * @since 7.3.0 + * + * @param mixed $input Optional. The ability input. Default empty array. + * @return bool True if the request may proceed, false otherwise. + */ + private function check_post_permission( $input = array() ): bool { + if ( ! is_user_logged_in() ) { + return false; + } + + $where = $this->get_required_input_object( rest_sanitize_object( $input ), 'where' ) ?? array(); + + return null !== $this->get_exposed_post_type( $where['type'] ?? null ); + } + /** * Parses a raw input value into an integer of at least a minimum, or null when invalid. * @@ -372,9 +539,10 @@ private function check_read_permission( WP_Post $post, array $checked_post_ids = /* * Mirror the REST posts controller's inherited-parent behavior, but keep the * ability fail-closed for missing parents or parent loops. No input can request - * `inherit`, since internal statuses are left out of the input schema and of the - * statuses a query covers by default, but a query filter such as a `pre_get_posts` - * callback can still add it, so such posts keep their parent's read check. + * `inherit`, since internal statuses other than `trash` are left out of the input + * schema, and all of them of the statuses a query covers by default, but a query + * filter such as a `pre_get_posts` callback can still add it, so such posts keep + * their parent's read check. */ if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) { $parent = get_post( $post->post_parent ); @@ -427,11 +595,7 @@ private function execute_content_query( $input = array() ) { */ $post_type_object = $this->get_exposed_post_type( $where['type'] ?? null ); if ( ! $post_type_object ) { - return new WP_Error( - 'content_not_found', - __( 'The requested content was not found.' ), - array( 'status' => 404 ) - ); + return $this->not_found_error(); } $post_type = $post_type_object->name; @@ -577,9 +741,9 @@ private function execute_content_query( $input = array() ) { * The input schema lists the statuses registered when the ability was, so check them * again: WP_Query ignores a status that is not registered, and drops the status clause * when none is left, which would cover every status, including internal ones such as - * `trash`. + * `trash`. Of the internal statuses, only `trash` can be requested, by name. */ - if ( ! empty( array_diff( $statuses, get_post_stati( array( 'internal' => false ) ) ) ) ) { + if ( ! empty( array_diff( $statuses, $this->get_queryable_statuses() ) ) ) { return $this->invalid_filter_error( 'where[status]', /* translators: %s: Parameter. */ @@ -612,89 +776,733 @@ private function execute_content_query( $input = array() ) { 'update_post_term_cache' => $prime_post_caches, ); - if ( null !== $ids ) { - $query_args['post__in'] = $ids; + if ( null !== $ids ) { + $query_args['post__in'] = $ids; + } + + if ( null !== $slugs ) { + $query_args['post_name__in'] = $slugs; + } + + if ( is_int( $author_id ) ) { + $query_args['author'] = $author_id; + } + + if ( null !== $parent ) { + $query_args['post_parent'] = $parent; + } + + /* + * A slug that names no user or a user the current user may not see, or an ID and a slug + * that name different users, match no post, like IDs and slugs that name no post, rather + * than failing the query. An `id` is not looked up: the query matches the stored author + * ID, so the posts of a deleted user still match it. + */ + if ( false === $author_id ) { + return $this->prepare_output( array(), 0, 0 ); + } + + $query = new WP_Query( $query_args ); + $total = $this->get_query_total( $query, $query_args, $page ); + + /* + * Count the pages with the page size the query ran with, as the REST posts controller + * does, since query filters such as `pre_get_posts` callbacks can change it. Without + * paging, the query returns every post on one page. + */ + $query_per_page = (int) $query->get( 'posts_per_page' ); + if ( $query->get( 'nopaging' ) || $query_per_page < 1 ) { + $total_pages = $total > 0 ? 1 : 0; + } else { + $total_pages = (int) ceil( $total / $query_per_page ); + } + + /* + * A page past the last one does not exist, so report it as not found instead of + * returning a bare empty list. A genuinely empty result set still returns zero + * totals and no error. + */ + if ( $total > 0 && $page > $total_pages ) { + return $this->invalid_page_number_error(); + } + + /* + * Prime the parent and author caches with a single query each instead of one + * lookup per post, as the REST posts controller does. Hierarchical permalinks and + * inherited read permissions read the parent, and format_post() sets up every post + * with setup_postdata(), which reads the author. + */ + update_post_parent_caches( $query->posts ); + update_post_author_caches( $query->posts ); + + $posts = array(); + foreach ( $query->posts as $post ) { + /* + * Skip posts of other post types, which query filters can add, such as a + * `pre_get_posts` callback that adds post types to the blog home without an + * is_main_query() check. + */ + if ( ! $post instanceof WP_Post || $post_type !== $post->post_type ) { + continue; + } + if ( ! $this->check_read_permission( $post ) ) { + continue; + } + $posts[] = $this->format_post( $post, $fields ); + } + + /* + * Mirror the REST posts controller: totals come from the underlying WP_Query, + * while the row-level checks above may withhold individual returned rows. The + * totals can therefore count posts the current user cannot read, such as another + * author's draft listed in `ids`. This is deliberate: it only reveals that such a + * post exists, as a REST collection request with `include` and `status` does. + */ + return $this->prepare_output( $posts, $total, $total_pages ); + } + + /** + * Executes the `core/content-create` ability. + * + * The post type and the capability to create posts of it are checked first, then every + * attribute rule, and only then is the post saved, so a rejected call writes nothing. + * + * @since 7.3.0 + * + * @param mixed $input Optional. The ability input. Default empty array. + * @return array|WP_Error The created post, or a WP_Error on failure. + */ + private function execute_content_create( $input = array() ) { + $input = rest_sanitize_object( $input ); + + // The author visibility memo is per call; see get_visible_author(). + $this->visible_authors = array(); + + $data = $this->get_required_input_object( $input, 'data' ) ?? array(); + $post_type_object = $this->get_exposed_post_type( $data['type'] ?? null ); + if ( ! $post_type_object ) { + return $this->not_found_error(); + } + + if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { + return new WP_Error( + 'content_cannot_create', + __( 'Sorry, you are not allowed to create posts as this user.' ), + array( 'status' => 403 ) + ); + } + + $postarr = $this->prepare_post_data( $data, $post_type_object, null ); + if ( is_wp_error( $postarr ) ) { + return $postarr; + } + + return $this->save_post( $postarr, $this->normalize_fields( $input ), false ); + } + + /** + * Executes the `core/content-update` ability. + * + * The post is looked up first, then the capability to edit it is checked, then every + * attribute rule, and only then is the post saved, so a rejected call writes nothing. + * + * @since 7.3.0 + * + * @param mixed $input Optional. The ability input. Default empty array. + * @return array|WP_Error The updated post, or a WP_Error on failure. + */ + private function execute_content_update( $input = array() ) { + $input = rest_sanitize_object( $input ); + + // The author visibility memo is per call; see get_visible_author(). + $this->visible_authors = array(); + + $post = $this->get_requested_post( $input ); + if ( ! $post ) { + return $this->not_found_error(); + } + + if ( ! current_user_can( 'edit_post', $post->ID ) ) { + return new WP_Error( + 'content_cannot_edit', + __( 'Sorry, you are not allowed to edit this post.' ), + array( 'status' => 403 ) + ); + } + + /* + * A `data` that is not an object, or that sends no attribute that can be written, + * has nothing to apply, so it is rejected instead of saving the post unchanged. + */ + $data = $this->get_input_object( $input, 'data' ); + if ( empty( $data ) || empty( array_intersect_key( $data, array_flip( self::WRITE_ATTRIBUTES ) ) ) ) { + return $this->invalid_attribute_error( 'data', __( 'The data must be an object with at least one attribute to write.' ) ); + } + + $post_type_object = $this->get_exposed_post_type( $post->post_type ); + if ( ! $post_type_object ) { + return $this->not_found_error(); + } + + $postarr = $this->prepare_post_data( $data, $post_type_object, $post ); + if ( is_wp_error( $postarr ) ) { + return $postarr; + } + + return $this->save_post( $postarr, $this->normalize_fields( $input ), true ); + } + + /** + * Executes the `core/content-delete` ability. + * + * The post is looked up first, then the capability to delete it is checked. The post is + * deleted permanently only when `permanent` is boolean true; otherwise it is moved to the + * Trash. The outcome is confirmed against the stored post afterwards. + * + * @since 7.3.0 + * + * @param mixed $input Optional. The ability input. Default empty array. + * @return array|WP_Error The outcome, or a WP_Error on failure. + */ + private function execute_content_delete( $input = array() ) { + $input = rest_sanitize_object( $input ); + + $post = $this->get_requested_post( $input ); + if ( ! $post ) { + return $this->not_found_error(); + } + + if ( ! current_user_can( 'delete_post', $post->ID ) ) { + return new WP_Error( + 'content_cannot_delete', + __( 'Sorry, you are not allowed to delete this post.' ), + array( 'status' => 403 ) + ); + } + + /* + * Intentionally strict: only boolean true deletes permanently. Any other value, such + * as "true" or 1, trashes. + */ + if ( true === ( $input['permanent'] ?? null ) ) { + $deleted = wp_delete_post( $post->ID, true ); + + return $deleted && null === get_post( $post->ID ) ? array( 'outcome' => 'deleted' ) : $this->delete_failed_error(); + } + + /* + * Checked before wp_trash_post() is called: without a Trash, it deletes the post + * permanently instead. + */ + if ( ! $this->can_trash( $post ) ) { + return new WP_Error( + 'content_trash_not_supported', + __( 'The post does not support trashing. Set "permanent" to delete it.' ), + array( 'status' => 501 ) + ); + } + + if ( 'trash' === $post->post_status ) { + return new WP_Error( + 'content_already_trashed', + __( 'The post has already been trashed.' ), + array( 'status' => 410 ) + ); + } + + $trashed = wp_trash_post( $post->ID ); + + return $trashed && 'trash' === get_post_status( $post->ID ) ? array( 'outcome' => 'trashed' ) : $this->delete_failed_error(); + } + + /** + * Returns the post that `where` names, when it exists and has the named exposed type. + * + * The ID is read once, with {@see self::parse_filter_int()}, and that same integer is used + * for the lookup, the capability checks, and the write. A value that is not a whole number + * of at least 1 names no post and is never cast. + * + * @since 7.3.0 + * + * @param array $input The ability input. + * @return WP_Post|null The post, or null when `where` names no post of an exposed type. + */ + private function get_requested_post( array $input ): ?WP_Post { + $where = $this->get_required_input_object( $input, 'where' ); + if ( null === $where ) { + return null; + } + + $post_type_object = $this->get_exposed_post_type( $where['type'] ?? null ); + $post_id = $this->parse_filter_int( $where['id'] ?? null, 1 ); + if ( ! $post_type_object || null === $post_id ) { + return null; + } + + $post = get_post( $post_id ); + + return $post instanceof WP_Post && $post_type_object->name === $post->post_type ? $post : null; + } + + /** + * Checks the `data` attributes and builds the array passed to the save. + * + * Shared by the create and update abilities. Only the attributes of + * {@see self::WRITE_ATTRIBUTES} that were sent reach the array, each mapped to one post + * field, next to the values the server sets: the post type on create, the post ID on + * update. The first rule that fails is returned and nothing is written. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param WP_Post_Type $post_type_object The post type of the post being written. + * @param WP_Post|null $post The stored post on update, or null on create. + * @return array|WP_Error The unslashed post array, or a WP_Error. + */ + private function prepare_post_data( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ) { + $post_type = $post_type_object->name; + + $error = $this->check_supported_attributes( $data, $post_type ); + if ( $error ) { + return $error; + } + + $postarr = array(); + $text_fields = array( + 'title' => 'post_title', + 'content' => 'post_content', + 'excerpt' => 'post_excerpt', + ); + + foreach ( $text_fields as $attribute => $field ) { + if ( ! array_key_exists( $attribute, $data ) ) { + continue; + } + if ( ! is_string( $data[ $attribute ] ) ) { + return $this->invalid_attribute_error( $attribute, __( 'The value must be a string.' ) ); + } + $postarr[ $field ] = $data[ $attribute ]; + } + + $status = $this->prepare_status( $data, $post_type_object, $post ); + if ( is_wp_error( $status ) ) { + return $status; + } + if ( null !== $status ) { + $postarr['post_status'] = $status; + } + + $author = $this->prepare_author( $data, $post_type_object, $post ); + if ( is_wp_error( $author ) ) { + return $author; + } + if ( null !== $author ) { + $postarr['post_author'] = $author; + } + + $parent = $this->prepare_parent( $data, $post_type, $post ); + if ( is_wp_error( $parent ) ) { + return $parent; + } + if ( null !== $parent ) { + $postarr['post_parent'] = $parent; + } + + $dates = $this->prepare_date( $data, $post, $status ); + if ( is_wp_error( $dates ) ) { + return $dates; + } + $postarr = array_merge( $postarr, $dates ); + + if ( $post ) { + $final_status = $postarr['post_status'] ?? $post->post_status; + $final_parent = $postarr['post_parent'] ?? (int) $post->post_parent; + } else { + $final_status = $postarr['post_status'] ?? 'draft'; + $final_parent = $postarr['post_parent'] ?? 0; + } + + $slug = $this->prepare_slug( $data, $post_type, $post, $final_status, $final_parent ); + if ( is_wp_error( $slug ) ) { + return $slug; + } + if ( null !== $slug ) { + $postarr['post_name'] = $slug; + } + + if ( $post ) { + $postarr['ID'] = $post->ID; + + // A stored page template that no longer exists must not fail an unrelated update. + $postarr['page_template'] = null; + } else { + $postarr['post_type'] = $post_type; + $postarr['post_status'] = $final_status; + } + + return $postarr; + } + + /** + * Rejects the sent attributes that the post type does not support. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param string $post_type The post type name. + * @return WP_Error|null The error for the first unsupported attribute, or null. + */ + private function check_supported_attributes( array $data, string $post_type ): ?WP_Error { + /* + * Intentionally strict: an attribute that cannot be stored is an error and is never + * ignored, even when its value would change nothing. + */ + $features = array( + 'title' => 'title', + 'content' => 'editor', + 'excerpt' => 'excerpt', + 'author' => 'author', + ); + foreach ( $features as $attribute => $feature ) { + if ( array_key_exists( $attribute, $data ) && ! post_type_supports( $post_type, $feature ) ) { + return $this->invalid_attribute_error( + $attribute, + /* translators: %s: Post type feature, such as "editor". */ + sprintf( __( 'The post type does not support the %s feature.' ), $feature ) + ); + } + } + + if ( array_key_exists( 'parent', $data ) && ! is_post_type_hierarchical( $post_type ) ) { + return $this->invalid_attribute_error( 'parent', __( 'The post type is not hierarchical.' ) ); + } + + return null; + } + + /** + * Checks the sent `status`. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param WP_Post_Type $post_type_object The post type of the post being written. + * @param WP_Post|null $post The stored post on update, or null on create. + * @return string|WP_Error|null The status, null when none is sent, or a WP_Error. + */ + private function prepare_status( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ) { + if ( ! array_key_exists( 'status', $data ) ) { + return null; + } + + $status = $data['status']; + $status_object = is_string( $status ) ? get_post_status_object( $status ) : null; + + /* + * Intentionally strict: a status that is not registered is an error and is never + * replaced by another status. Internal statuses, such as `trash`, can never be set, + * not even when the post already has them. + */ + if ( ! is_string( $status ) || ! $status_object instanceof stdClass || ! empty( $status_object->internal ) ) { + return $this->invalid_attribute_error( 'status', __( 'The status must be a registered post status that is not internal.' ) ); + } + + // Keeping the stored status changes nothing. + if ( $post && $post->post_status === $status ) { + return $status; + } + + /* + * Intentionally strict: every status that makes a post visible needs the capability + * to publish posts of the type, including custom ones. + */ + $makes_visible = 'future' === $status || ! empty( $status_object->public ) || ! empty( $status_object->private ) || is_post_status_viewable( $status_object ); + if ( $makes_visible && ! current_user_can( $post_type_object->cap->publish_posts ) ) { + return new WP_Error( + 'content_cannot_publish', + 'private' === $status + ? __( 'Sorry, you are not allowed to create private posts in this post type.' ) + : __( 'Sorry, you are not allowed to publish posts in this post type.' ), + array( 'status' => 403 ) + ); + } + + return $status; + } + + /** + * Checks the sent `author`. + * + * The permission to name another user is checked before the user is looked up, so the + * error never tells whether a user ID exists. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param WP_Post_Type $post_type_object The post type of the post being written. + * @param WP_Post|null $post The stored post on update, or null on create. + * @return int|WP_Error|null The author ID, null when none is sent or it is unchanged, or a WP_Error. + */ + private function prepare_author( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ) { + if ( ! array_key_exists( 'author', $data ) ) { + return null; + } + + $author_id = $this->parse_filter_int( $data['author'], 1 ); + if ( null === $author_id ) { + return $this->invalid_attribute_error( 'author', __( 'The author must be a user ID.' ) ); + } + + // Keeping the stored author changes nothing. + if ( $post && (int) $post->post_author === $author_id ) { + return null; + } + + if ( get_current_user_id() === $author_id ) { + return $author_id; + } + + if ( ! current_user_can( $post_type_object->cap->edit_others_posts ) ) { + return new WP_Error( + 'content_cannot_edit_others', + $post + ? __( 'Sorry, you are not allowed to update posts as this user.' ) + : __( 'Sorry, you are not allowed to create posts as this user.' ), + array( 'status' => 403 ) + ); + } + + // Intentionally strict: the author must be an existing user who belongs to this site. + if ( ! get_userdata( $author_id ) || ( is_multisite() && ! is_user_member_of_blog( $author_id ) ) ) { + return $this->invalid_attribute_error( 'author', __( 'The author must be an existing user of this site.' ) ); + } + + return $author_id; + } + + /** + * Checks the sent `parent`. + * + * Every failure returns the same error, so it never tells whether a post ID exists. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param string $post_type The post type name. + * @param WP_Post|null $post The stored post on update, or null on create. + * @return int|WP_Error|null The parent ID, null when none is sent or it is unchanged, or a WP_Error. + */ + private function prepare_parent( array $data, string $post_type, ?WP_Post $post ) { + if ( ! array_key_exists( 'parent', $data ) ) { + return null; + } + + $reason = __( 'The parent must be 0, or a readable post of the same post type that is neither the post itself nor one of its descendants.' ); + $parent_id = $this->parse_filter_int( $data['parent'], 0 ); + if ( null === $parent_id ) { + return $this->invalid_attribute_error( 'parent', $reason ); + } + + // Keeping the stored parent changes nothing. + if ( $post && (int) $post->post_parent === $parent_id ) { + return null; + } + + if ( 0 === $parent_id ) { + return 0; + } + + /* + * Intentionally strict: a parent must be readable, of the same type and free of + * loops; it is never silently reset. + */ + $parent = get_post( $parent_id ); + if ( ! $parent instanceof WP_Post || $post_type !== $parent->post_type || ! $this->check_read_permission( $parent ) ) { + return $this->invalid_attribute_error( 'parent', $reason ); + } + + if ( $post && ( $post->ID === $parent->ID || in_array( $post->ID, get_post_ancestors( $parent ), true ) ) ) { + return $this->invalid_attribute_error( 'parent', $reason ); + } + + return $parent_id; + } + + /** + * Checks the sent `date` and returns the date fields to save. + * + * `site` wins when both `site` and `gmt` are sent. On update, a sent date that equals the + * stored one counts as not sent, so a draft with a floating date keeps floating, unless + * the call moves it to a status whose dates do not float with a date that is still ahead. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param WP_Post|null $post The stored post on update, or null on create. + * @param string|null $status The sent status, or null when none is sent. + * @return array|WP_Error The date fields to save, empty for none, or a WP_Error. + */ + private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { + if ( ! array_key_exists( 'date', $data ) ) { + return array(); + } + + $reason = __( 'The date must be an object with a valid `site` or `gmt` date.' ); + $date = $this->get_input_object( $data, 'date' ); + if ( empty( $date ) ) { + return $this->invalid_attribute_error( 'date', $reason ); + } + + foreach ( array( 'site', 'gmt' ) as $key ) { + if ( array_key_exists( $key, $date ) && ! is_string( $date[ $key ] ) ) { + return $this->invalid_attribute_error( 'date', $reason ); + } + } + + if ( isset( $date['site'] ) ) { + $dates = rest_get_date_with_gmt( $date['site'] ); + } elseif ( isset( $date['gmt'] ) ) { + $dates = rest_get_date_with_gmt( $date['gmt'], true ); + } else { + $dates = null; + } + + if ( null === $dates ) { + return $this->invalid_attribute_error( 'date', $reason ); + } + + $fields = array( + 'post_date' => $dates[0], + 'post_date_gmt' => $dates[1], + ); + + if ( ! $post ) { + return $fields; } - if ( null !== $slugs ) { - $query_args['post_name__in'] = $slugs; - } + $fields['edit_date'] = true; - if ( is_int( $author_id ) ) { - $query_args['author'] = $author_id; + $is_floating = '0000-00-00 00:00:00' === $post->post_date_gmt; + if ( isset( $date['site'] ) ) { + $is_unchanged = $post->post_date === $dates[0]; + } else { + $stored_gmt = $is_floating ? get_gmt_from_date( $post->post_date ) : $post->post_date_gmt; + $is_unchanged = $stored_gmt === $dates[1]; } - if ( null !== $parent ) { - $query_args['post_parent'] = $parent; + if ( ! $is_unchanged ) { + return $fields; } /* - * A slug that names no user or a user the current user may not see, or an ID and a slug - * that name different users, match no post, like IDs and slugs that name no post, rather - * than failing the query. An `id` is not looked up: the query matches the stored author - * ID, so the posts of a deleted user still match it. + * Intentionally strict: sending back the date a floating draft shows, in either form, + * never pins it. The one exception is a call that also moves the draft to a status + * whose dates do not float, with a date still ahead: the date is stored, so the post + * is scheduled for the date it shows instead of being published at once. */ - if ( false === $author_id ) { - return $this->prepare_output( array(), 0, 0 ); + $schedules = $is_floating + && null !== $status + && ! in_array( $status, get_post_stati( array( 'date_floating' => true ) ), true ) + && $dates[1] > gmdate( 'Y-m-d H:i:s' ); + + return $schedules ? $fields : array(); + } + + /** + * Checks the sent `slug` and returns the slug to save. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param string $post_type The post type name. + * @param WP_Post|null $post The stored post on update, or null on create. + * @param string $status The status the post will have. + * @param int $parent_id The parent ID the post will have. + * @return string|WP_Error|null The slug, null when none is sent, or a WP_Error. + */ + private function prepare_slug( array $data, string $post_type, ?WP_Post $post, string $status, int $parent_id ) { + if ( ! array_key_exists( 'slug', $data ) ) { + return null; } - $query = new WP_Query( $query_args ); - $total = $this->get_query_total( $query, $query_args, $page ); + $slug = is_string( $data['slug'] ) ? sanitize_title( $data['slug'] ) : ''; - /* - * Count the pages with the page size the query ran with, as the REST posts controller - * does, since query filters such as `pre_get_posts` callbacks can change it. Without - * paging, the query returns every post on one page. - */ - $query_per_page = (int) $query->get( 'posts_per_page' ); - if ( $query->get( 'nopaging' ) || $query_per_page < 1 ) { - $total_pages = $total > 0 ? 1 : 0; - } else { - $total_pages = (int) ceil( $total / $query_per_page ); + // Intentionally strict: a slug that sanitizes to nothing is an error. + if ( '' === $slug ) { + return $this->invalid_attribute_error( 'slug', __( 'The slug must contain characters that are valid in a slug.' ) ); } /* - * A page past the last one does not exist, so report it as not found instead of - * returning a bare empty list. A genuinely empty result set still returns zero - * totals and no error. + * Intentionally strict: a draft never takes the slug of another post that shares its + * parent. The save keeps the slugs of drafts and pending posts as given, so they are + * made unique here as if the post were published. */ - if ( $total > 0 && $page > $total_pages ) { - return $this->invalid_page_number_error(); + if ( in_array( $status, array( 'draft', 'pending' ), true ) ) { + $slug = wp_unique_post_slug( $slug, $post ? $post->ID : 0, 'publish', $post_type, $parent_id ); } - /* - * Prime the parent and author caches with a single query each instead of one - * lookup per post, as the REST posts controller does. Hierarchical permalinks and - * inherited read permissions read the parent, and format_post() sets up every post - * with setup_postdata(), which reads the author. - */ - update_post_parent_caches( $query->posts ); - update_post_author_caches( $query->posts ); + return $slug; + } - $posts = array(); - foreach ( $query->posts as $post ) { - /* - * Skip posts of other post types, which query filters can add, such as a - * `pre_get_posts` callback that adds post types to the blog home without an - * is_main_query() check. - */ - if ( ! $post instanceof WP_Post || $post_type !== $post->post_type ) { - continue; - } - if ( ! $this->check_read_permission( $post ) ) { - continue; - } - $posts[] = $this->format_post( $post, $fields ); + /** + * Saves a prepared post and returns it in the requested fields. + * + * The post is saved through the regular save functions, as the current user, so the + * default filters, such as the markup filtering for users without `unfiltered_html`, and + * the default hooks apply as in any other save. + * + * @since 7.3.0 + * + * @param array $postarr The unslashed post array from prepare_post_data(). + * @param list $fields The requested field paths. + * @param bool $update Whether the post is updated rather than created. + * @return array|WP_Error The saved post, only its ID when the current user + * may not read it, or a WP_Error on failure. + */ + private function save_post( array $postarr, array $fields, bool $update ) { + $postarr = wp_slash( $postarr ); + $post_id = $update ? wp_update_post( $postarr, true ) : wp_insert_post( $postarr, true ); + + if ( is_wp_error( $post_id ) ) { + $is_database_error = in_array( $post_id->get_error_code(), array( 'db_insert_error', 'db_update_error' ), true ); + $post_id->add_data( array( 'status' => $is_database_error ? 500 : 400 ) ); + + return $post_id; + } + + // get_post() would return the global post for an ID of 0. + $post = $post_id > 0 ? get_post( $post_id ) : null; + if ( ! $post instanceof WP_Post ) { + return $this->not_found_error(); } /* - * Mirror the REST posts controller: totals come from the underlying WP_Query, - * while the row-level checks above may withhold individual returned rows. The - * totals can therefore count posts the current user cannot read, such as another - * author's draft listed in `ids`. This is deliberate: it only reveals that such a - * post exists, as a REST collection request with `include` and `status` does. + * Intentionally strict: the saved post passes the same read check as every query + * row. When the current user may no longer read it, for example after moving it to + * a status they cannot read, the write stands but only its ID is returned. */ - return $this->prepare_output( $posts, $total, $total_pages ); + if ( ! $this->check_read_permission( $post ) ) { + return array( 'id' => $post->ID ); + } + + return $this->format_post( $post, $fields ); + } + + /** + * Checks whether the site can move a post to the Trash. + * + * @since 7.3.0 + * + * @param WP_Post $post The post. + * @return bool Whether the post can be trashed. + */ + private function can_trash( WP_Post $post ): bool { + $can_trash = (int) constant( 'EMPTY_TRASH_DAYS' ) > 0; + if ( 'attachment' === $post->post_type ) { + $can_trash = $can_trash && (bool) constant( 'MEDIA_TRASH' ); + } + + return $can_trash; } /** @@ -719,6 +1527,19 @@ private function get_input_object( array $input, string $key ): ?array { return rest_is_object( $input[ $key ] ) ? rest_sanitize_object( $input[ $key ] ) : null; } + /** + * Reads an object member of the input that must be sent, such as `where` or `data`. + * + * @since 7.3.0 + * + * @param array $input The ability input. + * @param string $key The member name. + * @return array|null The member as an array, or null when it is omitted or not an object. + */ + private function get_required_input_object( array $input, string $key ): ?array { + return array_key_exists( $key, $input ) ? $this->get_input_object( $input, $key ) : null; + } + /** * Normalizes the requested per-page value to the supported bounds. * @@ -841,6 +1662,25 @@ private function get_requested_statuses( array $where ): array { return array_map( 'sanitize_key', $this->parse_list_input( $where, 'status' ) ); } + /** + * Returns the statuses the `status` filter accepts. + * + * These are the statuses that are not internal, followed by `trash`, so trashed posts can + * be queried by name. `trash` is never one of the statuses a query covers by default, see + * {@see self::normalize_statuses()}, and like every status that is not publicly viewable + * it requires edit access, see {@see self::can_query_status()}. + * + * @since 7.3.0 + * + * @return list Post status slugs. + */ + private function get_queryable_statuses(): array { + $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); + $statuses[] = 'trash'; + + return $statuses; + } + /** * Normalizes the statuses a query covers. * @@ -1216,7 +2056,7 @@ private function get_content_query_input_schema( array $post_types, array $statu 'type' => 'string', 'enum' => $statuses, ), - 'description' => __( 'Filter readable posts by one or more post statuses. Defaults to publish, or, when `ids` or `slugs` is given, to every status the current user may query. Statuses that are not publicly viewable, other than private, require edit access.' ), + 'description' => __( 'Filter readable posts by one or more post statuses. Defaults to publish, or, when `ids` or `slugs` is given, to every status the current user may query. Statuses that are not publicly viewable, other than private, require edit access. Trashed posts are only covered when `trash` is requested.' ), ), 'author' => array( 'type' => 'object', @@ -1272,6 +2112,207 @@ private function get_content_query_input_schema( array $post_types, array $statu ); } + /** + * Returns the `data` attributes shared by the create and update input schemas. + * + * @since 7.3.0 + * + * @param list $statuses Post status slugs that can be set. + * @return array The attribute schemas, keyed by attribute name. + */ + private function get_content_write_properties( array $statuses ): array { + return array( + 'title' => array( + 'type' => 'string', + 'description' => __( 'The raw post title. Only supported for post types that support titles.' ), + ), + 'content' => array( + 'type' => 'string', + 'description' => __( 'The raw post content, such as block markup. Only supported for post types that support the editor.' ), + ), + 'excerpt' => array( + 'type' => 'string', + 'description' => __( 'The raw post excerpt. Only supported for post types that support excerpts.' ), + ), + 'status' => array( + 'type' => 'string', + 'enum' => $statuses, + 'description' => __( 'The post status. A status that makes the post visible, such as publish, future, or private, requires the capability to publish posts of the type. A published post with a future date is scheduled, and a scheduled post with a past date is published.' ), + ), + 'slug' => array( + 'type' => 'string', + 'minLength' => 1, + 'description' => __( 'The post slug. It is sanitized as a post slug is, and made unique among the posts of the type that share its parent.' ), + ), + 'date' => array( + 'type' => 'object', + 'additionalProperties' => false, + 'minProperties' => 1, + 'properties' => array( + 'site' => array( + 'type' => 'string', + 'format' => 'date-time', + 'description' => __( "The publication date, in ISO 8601 format. Read in the site's timezone unless it has a UTC offset." ), + ), + 'gmt' => array( + 'type' => 'string', + 'format' => 'date-time', + 'description' => __( 'The publication date, in ISO 8601 format. Read as GMT unless it has a UTC offset.' ), + ), + ), + 'description' => __( 'The publication date. When both `site` and `gmt` are given, `site` is used.' ), + ), + 'author' => array( + 'type' => 'integer', + 'minimum' => 1, + 'description' => __( 'The user ID of the author. Naming a user other than the current one requires the capability to edit the posts of others. Only supported for post types that support authors.' ), + ), + 'parent' => array( + 'type' => 'integer', + 'minimum' => 0, + 'description' => __( 'The parent post ID: a readable post of the same type, or 0 for a top-level post. Only supported for hierarchical post types.' ), + ), + ); + } + + /** + * Builds the `where` schema that names one post, shared by the update and delete abilities. + * + * @since 7.3.0 + * + * @param list $post_types Exposed post type names. + * @param string $description The description of `where`. + * @return array The `where` JSON Schema. + */ + private function get_content_where_post_schema( array $post_types, string $description ): array { + return array( + 'type' => 'object', + 'additionalProperties' => false, + 'required' => array( 'type', 'id' ), + 'properties' => array( + 'type' => array( + 'type' => 'string', + 'enum' => $post_types, + 'description' => __( 'The post type of the post.' ), + ), + 'id' => array( + 'type' => 'integer', + 'minimum' => 1, + 'description' => __( 'The post ID.' ), + ), + ), + 'description' => $description, + ); + } + + /** + * Builds the input schema for the `core/content-create` ability. + * + * @since 7.3.0 + * + * @param list $post_types Exposed post type names. + * @param list $statuses Post status slugs that can be set. + * @return array The input JSON Schema. + */ + private function get_content_create_input_schema( array $post_types, array $statuses ): array { + return array( + 'type' => 'object', + 'additionalProperties' => false, + 'required' => array( 'data' ), + 'properties' => array( + 'data' => array( + 'type' => 'object', + 'additionalProperties' => false, + 'required' => array( 'type' ), + 'properties' => array_merge( + array( + 'type' => array( + 'type' => 'string', + 'enum' => $post_types, + 'description' => __( 'The post type of the post to create.' ), + ), + ), + $this->get_content_write_properties( $statuses ) + ), + 'description' => __( 'The post to create. The status defaults to draft.' ), + ), + 'fields' => $this->get_fields_input_schema(), + ), + ); + } + + /** + * Builds the input schema for the `core/content-update` ability. + * + * @since 7.3.0 + * + * @param list $post_types Exposed post type names. + * @param list $statuses Post status slugs that can be set. + * @return array The input JSON Schema. + */ + private function get_content_update_input_schema( array $post_types, array $statuses ): array { + return array( + 'type' => 'object', + 'additionalProperties' => false, + 'required' => array( 'where', 'data' ), + 'properties' => array( + 'where' => $this->get_content_where_post_schema( $post_types, __( 'Selects the post to update, by its type and ID.' ) ), + 'data' => array( + 'type' => 'object', + 'additionalProperties' => false, + 'minProperties' => 1, + 'properties' => $this->get_content_write_properties( $statuses ), + 'description' => __( 'The attributes to change. Attributes that are left out keep their stored values.' ), + ), + 'fields' => $this->get_fields_input_schema(), + ), + ); + } + + /** + * Builds the input schema for the `core/content-delete` ability. + * + * @since 7.3.0 + * + * @param list $post_types Exposed post type names. + * @return array The input JSON Schema. + */ + private function get_content_delete_input_schema( array $post_types ): array { + return array( + 'type' => 'object', + 'additionalProperties' => false, + 'required' => array( 'where' ), + 'properties' => array( + 'where' => $this->get_content_where_post_schema( $post_types, __( 'Selects the post to delete, by its type and ID.' ) ), + 'permanent' => array( + 'type' => 'boolean', + 'description' => __( 'Whether to delete the post permanently instead of moving it to the Trash. Defaults to false.' ), + ), + ), + ); + } + + /** + * Builds the output schema for the `core/content-delete` ability. + * + * @since 7.3.0 + * + * @return array The output JSON Schema. + */ + private function get_content_delete_output_schema(): array { + return array( + 'type' => 'object', + 'additionalProperties' => false, + 'required' => array( 'outcome' ), + 'properties' => array( + 'outcome' => array( + 'type' => 'string', + 'description' => __( 'What happened to the post: `trashed` when it was moved to the Trash, `deleted` when it was deleted permanently.' ), + ), + ), + ); + } + /** * Builds the output schema of a single post, shared by all content abilities. * @@ -1343,7 +2384,7 @@ private function get_content_query_output_schema(): array { * the current user may not see is reported like a missing one. * * The `author` field and the `author` slug filter share this rule, so the filter never - * confirms a slug the field withholds. The post count is kept per author for the query, + * confirms a slug the field withholds. The post count is kept per author for the call, * since count_user_posts() is not cached. * * @since 7.2.0 @@ -1834,4 +2875,63 @@ private function invalid_filter_error( string $param, string $message ): WP_Erro ) ); } + + /** + * Builds the error for a post that cannot be found. + * + * Returned alike for a missing post, a post of another type, and a post type that is not + * exposed to abilities, so it never tells which one applies. + * + * @since 7.3.0 + * + * @return WP_Error The not found error. + */ + private function not_found_error(): WP_Error { + return new WP_Error( + 'content_not_found', + __( 'The requested content was not found.' ), + array( 'status' => 404 ) + ); + } + + /** + * Builds the error for a `data` attribute that cannot be applied. + * + * The error data maps the attribute's input path, such as `data[title]`, to the reason + * under `params`, so callers can tell which attribute failed without parsing the message. + * + * @since 7.3.0 + * + * @param string $attribute The attribute name, such as `title`, or `data` for the whole object. + * @param string $reason Why the attribute cannot be applied. + * @return WP_Error The invalid attribute error. + */ + private function invalid_attribute_error( string $attribute, string $reason ): WP_Error { + $param = 'data' === $attribute ? 'data' : "data[{$attribute}]"; + + return new WP_Error( + 'content_invalid_attribute', + /* translators: %s: Attribute name. */ + sprintf( __( 'Invalid attribute: %s.' ), $attribute ), + array( + 'status' => 400, + 'params' => array( $param => $reason ), + ) + ); + } + + /** + * Builds the error for a delete or trash call that failed or could not be confirmed. + * + * @since 7.3.0 + * + * @return WP_Error The delete failed error. + */ + private function delete_failed_error(): WP_Error { + return new WP_Error( + 'content_cannot_delete', + __( 'The post cannot be deleted.' ), + array( 'status' => 500 ) + ); + } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index cf537f5cf0911..31ddeaa7d64f3 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -142,8 +142,10 @@ public static function wpSetUpBeforeClass( $factory ): void { public function set_up(): void { parent::set_up(); - if ( wp_has_ability( 'core/content-query' ) ) { - wp_unregister_ability( 'core/content-query' ); + foreach ( array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ) as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } } if ( ! wp_has_ability_category( 'content' ) ) { @@ -165,8 +167,10 @@ public function set_up(): void { * @since 7.2.0 */ public function tear_down(): void { - if ( wp_has_ability( 'core/content-query' ) ) { - wp_unregister_ability( 'core/content-query' ); + foreach ( array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ) as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } } if ( wp_has_ability_category( 'content' ) ) { @@ -356,7 +360,7 @@ public function test_input_schema_models_a_flat_where(): void { $this->assertTrue( $status['uniqueItems'], 'The status filter should reject duplicate statuses.' ); $this->assertSame( 1, $status['minItems'], 'The status filter should require at least one status.' ); $this->assertSame( 'string', $status['items']['type'], 'The status filter should contain status names.' ); - $this->assertSameSets( array_values( get_post_stati( array( 'internal' => false ) ) ), $status['items']['enum'], 'The status filter should expose the statuses that are not internal.' ); + $this->assertSame( array_merge( array_values( get_post_stati( array( 'internal' => false ) ) ), array( 'trash' ) ), $status['items']['enum'], 'The status filter should expose the statuses that are not internal, then trash.' ); $author = $where['properties']['author']; @@ -6397,12 +6401,6 @@ public static function data_where_filters_with_statuses_that_cannot_be_queried() 'status' => array( 'unregistered_status' ), ), ), - 'trash' => array( - 'where' => array( - 'type' => 'post', - 'status' => array( 'trash' ), - ), - ), 'inherit' => array( 'where' => array( 'type' => 'post', @@ -6828,4 +6826,236 @@ public function test_author_filter_names_authors_who_are_not_site_members(): voi $this->assertIsArray( $by_id, 'An author with published posts on the site should be filterable by ID without being a member.' ); $this->assertSame( array( $post_id ), wp_list_pluck( $by_id['items'], 'id' ), 'The ID should return the author\'s posts on the site.' ); } + + /** + * Creates a post in the Trash. + * + * @since 7.3.0 + * + * @param string $author The author, as a role or fixture name. + * @return int The post ID. + */ + private function create_trashed_post( string $author ): int { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids[ $author ], + 'post_status' => 'publish', + ) + ); + wp_trash_post( $post_id ); + + $this->assertSame( 'trash', get_post_status( $post_id ), 'Precondition: the post should be in the Trash.' ); + + return $post_id; + } + + /** + * Queries trashed posts and returns the IDs of the items. + * + * @since 7.3.0 + * + * @param array $where The `where` filters. + * @return list The IDs of the returned items. + */ + private function query_ids( array $where ): array { + $result = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => $where, + 'fields' => array( 'id' ), + ) + ); + + $this->assertIsArray( $result, 'The query should succeed.' ); + + return wp_list_pluck( $result['items'], 'id' ); + } + + /** + * `trash` requested by name returns the trashed posts to roles that may edit them. + * + * Covers S14. + * + * @dataProvider data_roles_with_edit_access_to_other_users_posts + * @since 7.3.0 + * + * @param string $role The role to query as. + */ + public function test_trash_requested_by_name_returns_trashed_posts( string $role ): void { + $post_id = $this->create_trashed_post( 'author' ); + + $this->login_as( $role ); + $this->register_ability(); + + $this->assertSame( + array( $post_id ), + $this->query_ids( + array( + 'type' => 'post', + 'status' => array( 'trash' ), + ) + ), + 'The trashed post should be returned.' + ); + } + + /** + * `trash` can be requested next to another status. + * + * Covers S14. + * + * @since 7.3.0 + */ + public function test_trash_can_be_requested_next_to_another_status(): void { + $post_id = $this->create_trashed_post( 'author' ); + + $this->login_as( 'editor' ); + $this->register_ability(); + + $this->assertSameSets( + array( $post_id, self::$post_ids['published'] ), + $this->query_ids( + array( + 'type' => 'post', + 'ids' => array( $post_id, self::$post_ids['published'] ), + 'status' => array( 'publish', 'trash' ), + ) + ), + 'The trashed and the published post should be returned.' + ); + } + + /** + * A query without a status does not cover `trash`, also not when it lists slugs. + * + * @since 7.3.0 + */ + public function test_slugs_without_a_status_do_not_cover_trashed_posts(): void { + $post_id = $this->create_trashed_post( 'author' ); + + $this->login_as( 'administrator' ); + $this->register_ability(); + + $result = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => array( + 'type' => 'post', + 'slugs' => array( get_post( $post_id )->post_name ), + ), + 'fields' => array( 'id' ), + ) + ); + + $this->assertSame( self::EMPTY_RESULT, $result, 'A query by slug without a status should neither return nor count a trashed post.' ); + } + + /** + * A query without a status does not cover `trash`. + * + * @since 7.3.0 + */ + public function test_query_without_a_status_does_not_cover_trashed_posts(): void { + $post_id = $this->create_trashed_post( 'administrator' ); + + $this->login_as( 'administrator' ); + $this->register_ability(); + + $this->assertNotContains( $post_id, $this->query_ids( array( 'type' => 'post' ) ), 'A query without a status should not return a trashed post.' ); + } + + /** + * Querying `trash` needs `edit_posts`. + * + * Covers S14. + * + * @since 7.3.0 + */ + public function test_trash_needs_edit_posts(): void { + $this->create_trashed_post( 'author' ); + + $this->login_as( 'subscriber' ); + $this->register_ability(); + + $result = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => array( + 'type' => 'post', + 'status' => array( 'trash' ), + ), + ) + ); + + $this->assertWPError( $result, 'A subscriber should not be able to query the Trash.' ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a subscriber.' ); + } + + /** + * Returns roles that may query the Trash but only edit their own posts. + * + * @return array The roles. + */ + public static function data_roles_that_edit_only_their_own_trashed_posts(): array { + return array( + 'author' => array( 'author_secondary' ), + 'contributor' => array( 'contributor' ), + ); + } + + /** + * Trashed rows pass the per-post read check: a role that edits only its own posts gets its + * own trashed post and not the trashed post of another user. + * + * Covers S14. + * + * @dataProvider data_roles_that_edit_only_their_own_trashed_posts + * @since 7.3.0 + * + * @param string $role The role to query as. + */ + public function test_trash_returns_only_the_trashed_posts_the_user_may_read( string $role ): void { + $own_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids[ $role ], + 'post_status' => 'draft', + ) + ); + $other_id = $this->create_trashed_post( 'author' ); + wp_trash_post( $own_id ); + + $this->login_as( $role ); + $this->register_ability(); + + $this->assertSame( + array( $own_id ), + $this->query_ids( + array( + 'type' => 'post', + 'ids' => array( $own_id, $other_id ), + 'status' => array( 'trash' ), + ) + ), + 'Only the own trashed post should be returned.' + ); + } + + /** + * Other internal statuses are still rejected. + * + * @since 7.3.0 + */ + public function test_other_internal_statuses_are_still_rejected(): void { + $this->login_as( 'administrator' ); + $this->register_ability(); + + $result = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => array( + 'type' => 'post', + 'status' => array( 'auto-draft' ), + ), + ) + ); + + $this->assertWPError( $result, 'An auto-draft query should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject auto-draft.' ); + } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php new file mode 100644 index 0000000000000..6fc3e729d35c3 --- /dev/null +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -0,0 +1,3062 @@ + + */ + private const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); + + /** + * A user ID that no user has. + * + * @since 7.3.0 + * + * @var int + */ + private const MISSING_ID = 987654321; + + /** + * A date that stays in the future. + * + * @since 7.3.0 + * + * @var string + */ + private const FUTURE_GMT = '2035-06-01T12:00:00'; + + /** + * Shared user IDs keyed by role or fixture name. + * + * @since 7.3.0 + * + * @var array + */ + private static $user_ids = array(); + + /** + * Shared post IDs keyed by fixture name. + * + * @since 7.3.0 + * + * @var array + */ + private static $post_ids = array(); + + /** + * Creates shared users and posts. + * + * @since 7.3.0 + * + * @param WP_UnitTest_Factory $factory The unit test factory. + */ + public static function wpSetUpBeforeClass( $factory ): void { + self::$user_ids = array( + 'administrator' => $factory->user->create( array( 'role' => 'administrator' ) ), + 'editor' => $factory->user->create( array( 'role' => 'editor' ) ), + 'author' => $factory->user->create( array( 'role' => 'author' ) ), + 'contributor' => $factory->user->create( array( 'role' => 'contributor' ) ), + 'subscriber' => $factory->user->create( array( 'role' => 'subscriber' ) ), + 'page_writer' => $factory->user->create( array( 'role' => 'subscriber' ) ), + 'publisher' => $factory->user->create( array( 'role' => 'contributor' ) ), + ); + + // A user who may create pages but not read the drafts of other users. + get_userdata( self::$user_ids['page_writer'] )->add_cap( 'edit_pages' ); + + // A user who may publish posts but not edit published posts. + get_userdata( self::$user_ids['publisher'] )->add_cap( 'publish_posts' ); + + self::$post_ids = array( + 'published_post' => $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_name' => 'shared-slug', + ) + ), + 'published_page' => $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ), + 'other_parent_page' => $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ), + 'editor_draft_page' => $factory->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_type' => 'page', + 'post_status' => 'draft', + ) + ), + ); + + self::$post_ids['child_page'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_parent' => self::$post_ids['published_page'], + 'post_name' => 'child', + ) + ); + + // A page of the administrator in the Trash. + self::$post_ids['trashed_page'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + wp_trash_post( self::$post_ids['trashed_page'] ); + + $revisioned_id = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_content' => 'First version.', + ) + ); + wp_update_post( + array( + 'ID' => $revisioned_id, + 'post_content' => 'Second version.', + ) + ); + self::$post_ids['page_revision'] = (int) array_key_first( wp_get_post_revisions( $revisioned_id ) ); + } + + /** + * Unregisters the content abilities and registers the content category. + * + * @since 7.3.0 + */ + public function set_up(): void { + parent::set_up(); + + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( ! wp_has_ability_category( 'content' ) ) { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + wp_register_ability_category( + 'content', + array( + 'label' => 'Content', + 'description' => 'Content.', + ) + ); + } + } + + /** + * Unregisters the content abilities and the category, and resets the post types. + * + * @since 7.3.0 + */ + public function tear_down(): void { + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( wp_has_ability_category( 'content' ) ) { + wp_unregister_ability_category( 'content' ); + } + + /* + * A test may turn off `show_in_abilities` on `post` and `page`, and the next class's + * wpSetUpBeforeClass() runs before the next set_up() resets the post types. + */ + $this->reset_post_types(); + + parent::tear_down(); + } + + /** + * Registers the content abilities inside a faked init action. + * + * @since 7.3.0 + */ + private function register_abilities(): void { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + ( new WP_Abilities_Content() )->register(); + } + + /** + * Registers the content abilities and returns the callbacks of the create ability. + * + * The callbacks call private methods, so tests that skip input validation or the + * permission check capture them from the registration arguments. + * + * @since 7.3.0 + * + * @return array The permission and execute callbacks, keyed by argument name. + */ + private function get_ability_callbacks(): array { + $callbacks = array(); + + add_filter( + 'wp_register_ability_args', + static function ( array $args, string $name ) use ( &$callbacks ): array { + if ( self::ABILITY === $name ) { + $callbacks = array( + 'permission_callback' => $args['permission_callback'], + 'execute_callback' => $args['execute_callback'], + ); + } + + return $args; + }, + 10, + 2 + ); + + $this->register_abilities(); + + $this->assertArrayHasKey( 'execute_callback', $callbacks, 'The core/content-create ability should be registered with callbacks.' ); + + return $callbacks; + } + + /** + * Runs the registered create ability with the given input. + * + * @since 7.3.0 + * + * @param mixed $input The ability input. + * @return mixed The ability result. + */ + private function run_ability( $input ) { + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The core/content-create ability should be registered.' ); + + return wp_get_ability( self::ABILITY )->execute( $input ); + } + + /** + * Logs in as the shared user with the given role or fixture name. + * + * @since 7.3.0 + * + * @param string $role The role or fixture name. + */ + private function login_as( string $role ): void { + wp_set_current_user( self::$user_ids[ $role ] ); + } + + /** + * Takes a snapshot of the stored content: the number of posts per type and status, + * revisions included, and the number of meta rows and term relationships. + * + * @since 7.3.0 + * + * @return array The snapshot. + */ + private function snapshot(): array { + global $wpdb; + + return array( + 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), + 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), + 'terms' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->term_relationships}" ), + ); + } + + /** + * Asserts that a result is an error with the given code and status. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $code The expected error code. + * @param int $status The expected error status. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_error( $result, string $code, int $status, string $context ): void { + $this->assertWPError( $result, "{$context}: the call should fail." ); + $this->assertSame( $code, $result->get_error_code(), "{$context}: the error code should be {$code}." ); + + $data = $result->get_error_data(); + $this->assertIsArray( $data, "{$context}: the error should carry data." ); + $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); + } + + /** + * Asserts that a result is the invalid attribute error for the given attribute. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $attribute The attribute name. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_invalid_attribute( $result, string $attribute, string $context ): void { + $this->assert_error( $result, 'content_invalid_attribute', 400, $context ); + $this->assertSame( "Invalid attribute: {$attribute}.", $result->get_error_message(), "{$context}: the message should name the attribute." ); + $this->assertSame( array( "data[{$attribute}]" ), array_keys( $result->get_error_data()['params'] ?? array() ), "{$context}: the params should hold only the input path of the attribute." ); + } + + /** + * Returns the newest post created by a call, or null when the call created none. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @return WP_Post|null The created post. + */ + private function get_created_post( $result ): ?WP_Post { + $this->assertIsArray( $result, 'The create call should succeed.' ); + $this->assertArrayHasKey( 'id', $result, 'The created post should be returned with its ID.' ); + + return get_post( $result['id'] ); + } + + /** + * Removes the `description` keywords of a schema at every depth. + * + * @since 7.3.0 + * + * @param array $schema The schema. + * @return array The schema without descriptions. + */ + private function strip_descriptions( array $schema ): array { + unset( $schema['description'] ); + foreach ( $schema as $key => $value ) { + if ( is_array( $value ) ) { + $schema[ $key ] = $this->strip_descriptions( $value ); + } + } + + return $schema; + } + + /** + * Registers a post type exposed to abilities with the given supported features. + * + * @since 7.3.0 + * + * @param string $name The post type name. + * @param list $supports The supported features. + */ + private function register_exposed_post_type( string $name, array $supports ): void { + register_post_type( + $name, + array( + 'public' => true, + 'show_in_abilities' => true, + 'supports' => $supports, + ) + ); + } + + /** + * The ability is registered in the content category with its label and annotations. + * + * @since 7.3.0 + */ + public function test_registers_the_ability(): void { + $this->register_abilities(); + + $ability = wp_get_ability( self::ABILITY ); + + $this->assertInstanceOf( WP_Ability::class, $ability, 'The core/content-create ability should be registered.' ); + $this->assertSame( 'Create Content', $ability->get_label(), 'The ability should have its label.' ); + $this->assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); + $this->assertTrue( $ability->get_meta_item( 'public', false ), 'The ability should be marked public.' ); + + $annotations = $ability->get_meta_item( 'annotations', array() ); + $this->assertFalse( $annotations['readonly'], 'The ability should not be marked read-only.' ); + $this->assertFalse( $annotations['destructive'], 'The ability should not be marked destructive.' ); + $this->assertFalse( $annotations['idempotent'], 'The ability should not be marked idempotent.' ); + } + + /** + * The ability is not registered when no post type is exposed to abilities. + * + * @since 7.3.0 + */ + public function test_is_not_registered_without_exposed_post_types(): void { + foreach ( array( 'post', 'page' ) as $post_type ) { + get_post_type_object( $post_type )->show_in_abilities = false; + } + + $this->register_abilities(); + + $this->assertFalse( wp_has_ability( self::ABILITY ), 'The create ability should not be registered without an exposed post type.' ); + } + + /** + * The input schema takes `data`, with the post type and the allowed attributes, and `fields`. + * + * @since 7.3.0 + */ + public function test_input_schema(): void { + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The create ability should be registered.' ); + + $schema = wp_get_ability( self::ABILITY )->get_input_schema(); + + $this->assertSame( 'object', $schema['type'], 'The input should be an object.' ); + $this->assertFalse( $schema['additionalProperties'], 'The input should reject unknown keys.' ); + $this->assertSame( array( 'data' ), $schema['required'], 'The input should require data.' ); + $this->assertSame( array( 'data', 'fields' ), array_keys( $schema['properties'] ), 'The input should take data and fields.' ); + + $data = $schema['properties']['data']; + + $this->assertSame( 'object', $data['type'], 'The data should be an object.' ); + $this->assertFalse( $data['additionalProperties'], 'The data should reject unknown keys.' ); + $this->assertSame( array( 'type' ), $data['required'], 'The data should require the post type.' ); + $this->assertSame( array( 'type', 'title', 'content', 'excerpt', 'status', 'slug', 'date', 'author', 'parent' ), array_keys( $data['properties'] ), 'The data should take the post type and the allowed attributes.' ); + + $properties = $data['properties']; + + $this->assertSame( 'string', $properties['type']['type'], 'The post type should be a string.' ); + $this->assertSameSets( array( 'post', 'page' ), $properties['type']['enum'], 'The post type should list the exposed post types.' ); + $this->assertSame( 'string', $properties['title']['type'], 'The title should be a string.' ); + $this->assertSame( 'string', $properties['content']['type'], 'The content should be a string.' ); + $this->assertSame( 'string', $properties['excerpt']['type'], 'The excerpt should be a string.' ); + $this->assertSame( 'string', $properties['status']['type'], 'The status should be a string.' ); + $this->assertSame( array( 'publish', 'future', 'draft', 'pending', 'private' ), $properties['status']['enum'], 'The status should list the statuses that are not internal.' ); + $this->assertSame( 'string', $properties['slug']['type'], 'The slug should be a string.' ); + $this->assertSame( 1, $properties['slug']['minLength'], 'The slug should not be empty.' ); + $this->assertSame( 'integer', $properties['author']['type'], 'The author should be an integer.' ); + $this->assertSame( 1, $properties['author']['minimum'], 'The author should be at least 1.' ); + $this->assertSame( 'integer', $properties['parent']['type'], 'The parent should be an integer.' ); + $this->assertSame( 0, $properties['parent']['minimum'], 'The parent should be at least 0.' ); + + $date = $properties['date']; + + $this->assertSame( 'object', $date['type'], 'The date should be an object.' ); + $this->assertFalse( $date['additionalProperties'], 'The date should reject unknown keys.' ); + $this->assertSame( 1, $date['minProperties'], 'The date should hold at least one value.' ); + $this->assertSame( array( 'site', 'gmt' ), array_keys( $date['properties'] ), 'The date should take a site and a GMT value.' ); + $this->assertSame( 'string', $date['properties']['site']['type'], 'The site date should be a string.' ); + $this->assertSame( 'date-time', $date['properties']['site']['format'], 'The site date should be a date-time.' ); + $this->assertSame( 'string', $date['properties']['gmt']['type'], 'The GMT date should be a string.' ); + $this->assertSame( 'date-time', $date['properties']['gmt']['format'], 'The GMT date should be a date-time.' ); + + $this->assertTrue( wp_has_ability( 'core/content-query' ), 'The query ability should be registered next to the create ability.' ); + + $query_schema = wp_get_ability( 'core/content-query' )->get_input_schema(); + + $this->assertSame( + $this->strip_descriptions( $query_schema['properties']['fields'] ), + $this->strip_descriptions( $schema['properties']['fields'] ), + 'The fields should be the same list as the fields of the query.' + ); + } + + /** + * The output is one post item, the same object as an item of the query. + * + * Covers S11. + * + * @since 7.3.0 + */ + public function test_output_schema_is_a_query_item(): void { + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The create ability should be registered.' ); + + $schema = wp_get_ability( self::ABILITY )->get_output_schema(); + $item = wp_get_ability( 'core/content-query' )->get_output_schema()['properties']['items']['items']; + + $this->assertSame( 'object', $schema['type'], 'The output should be an object.' ); + $this->assertFalse( $schema['additionalProperties'], 'The output should not take unknown keys.' ); + $this->assertSame( array( 'id' ), $schema['required'], 'The output should always hold the ID.' ); + $this->assertSame( $this->strip_descriptions( $item ), $this->strip_descriptions( $schema ), 'The output should be the same object as a query item.' ); + } + + /** + * The schema enums are built from the site when the ability is registered. + * + * @since 7.3.0 + */ + public function test_schema_enums_list_the_exposed_post_types_and_registered_statuses(): void { + $this->register_exposed_post_type( 'cw_book', array( 'title' ) ); + register_post_type( 'cw_hidden', array( 'public' => true ) ); + register_post_status( + 'cw_archived', + array( + 'label' => 'Archived', + 'public' => true, + ) + ); + + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The create ability should be registered.' ); + + $properties = wp_get_ability( self::ABILITY )->get_input_schema()['properties']['data']['properties']; + + $this->assertSameSets( array( 'post', 'page', 'cw_book' ), $properties['type']['enum'], 'The post type should list every exposed post type and no other.' ); + $this->assertSameSets( array_values( get_post_stati( array( 'internal' => false ) ) ), $properties['status']['enum'], 'The status should list every registered status that is not internal.' ); + $this->assertContains( 'cw_archived', $properties['status']['enum'], 'The status should list a custom status.' ); + } + + /** + * Example 1: a draft with the default fields. + * + * @since 7.3.0 + */ + public function test_example_creates_a_draft_with_the_default_fields(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Launch notes', + ), + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertSame( array( 'id', 'type', 'status', 'date', 'slug', 'title' ), array_keys( $result ), 'The default fields should be returned.' ); + $this->assertSame( $post->ID, $result['id'], 'The ID should be the ID of the new post.' ); + $this->assertSame( 'post', $result['type'], 'The type should be post.' ); + $this->assertSame( 'draft', $result['status'], 'The post should be a draft.' ); + $this->assertSame( array( 'site', 'gmt' ), array_keys( $result['date'] ), 'The date should be given in site time and GMT.' ); + $this->assertNotFalse( rest_parse_date( $result['date']['site'] ), 'The site date should be a date-time.' ); + $this->assertNotFalse( rest_parse_date( $result['date']['gmt'] ), 'The GMT date should be a date-time.' ); + $this->assertSame( '', $result['slug'], 'A draft without a slug should have an empty slug.' ); + $this->assertSame( array( 'rendered' => 'Launch notes' ), $result['title'], 'The title should be returned rendered.' ); + $this->assertSame( 'Launch notes', $post->post_title, 'The title should be stored.' ); + } + + /** + * Example 2: a published child page with chosen fields. + * + * @since 7.3.0 + */ + public function test_example_creates_a_published_child_page_with_chosen_fields(): void { + $this->set_permalink_structure( '/%postname%/' ); + + $parent_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_name' => 'products', + ) + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Pricing', + 'content' => "\n

Plans start at 9 euro per month.

\n", + 'status' => 'publish', + 'parent' => $parent_id, + 'slug' => 'pricing', + ), + 'fields' => array( 'status', 'link', 'title.rendered', 'content.rendered', 'parent' ), + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertSame( array( 'id', 'status', 'link', 'title', 'content', 'parent' ), array_keys( $result ), 'The chosen fields should be returned.' ); + $this->assertSame( $post->ID, $result['id'], 'The ID should be the ID of the new page.' ); + $this->assertSame( 'publish', $result['status'], 'The page should be published.' ); + $this->assertSame( home_url( '/products/pricing/' ), $result['link'], 'The link should include the parent.' ); + $this->assertSame( array( 'rendered' => 'Pricing' ), $result['title'], 'The title should be returned rendered.' ); + $this->assertSame( $parent_id, $result['parent'], 'The parent should be returned.' ); + + // The rendered markup comes from the regular content filters, as for any query item. + $this->assertSame( $this->query_item( 'page', $post->ID, array( 'content.rendered' ) )['content'], $result['content'], 'The content should be rendered as the query renders it.' ); + $this->assertStringContainsString( 'Plans start at 9 euro per month.

', $result['content']['rendered'], 'The rendered content should hold the paragraph.' ); + } + + /** + * Returns the item that core/content-query gives for one post and the given fields. + * + * @since 7.3.0 + * + * @param string $type The post type. + * @param int $post_id The post ID. + * @param list $fields The fields. + * @return array The query item. + */ + private function query_item( string $type, int $post_id, array $fields ): array { + $this->assertTrue( wp_has_ability( 'core/content-query' ), 'The query ability should be registered.' ); + + $result = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => array( + 'type' => $type, + 'ids' => array( $post_id ), + ), + 'fields' => $fields, + ) + ); + + $this->assertIsArray( $result, 'The query for the post should succeed.' ); + $this->assertCount( 1, $result['items'], 'The query should return the post.' ); + + return $result['items'][0]; + } + + /** + * Example 3: an attribute the post type does not support. + * + * Covers S1 and S5. + * + * @since 7.3.0 + */ + public function test_example_rejects_a_parent_for_a_post_type_that_is_not_hierarchical(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Launch notes', + 'parent' => self::$post_ids['published_post'], + ), + ) + ); + + $this->assertWPError( $result, 'A parent for a post should be rejected.' ); + $this->assertSame( 'content_invalid_attribute', $result->get_error_code(), 'The error should be the invalid attribute error.' ); + $this->assertSame( 'Invalid attribute: parent.', $result->get_error_message(), 'The message should name the parent.' ); + $this->assertSame( 400, $result->get_error_data()['status'], 'The status should be 400.' ); + $this->assertSame( array( 'data[parent]' => 'The post type is not hierarchical.' ), $result->get_error_data()['params'], 'The params should give the fixed reason for the parent.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns users that the permission callback refuses. + * + * @return array The users. + */ + public static function data_users_refused_by_the_permission_callback(): array { + return array( + 'logged out' => array( + 'role' => null, + ), + 'subscriber' => array( + 'role' => 'subscriber', + ), + ); + } + + /** + * A logged-out user and a user without `create_posts` are refused before the ability runs. + * + * Covers S4 and S5. + * + * @dataProvider data_users_refused_by_the_permission_callback + * + * @since 7.3.0 + * + * @param string|null $role The role to log in as, or null to stay logged out. + */ + public function test_permission_callback_refuses_users_who_cannot_create_posts( ?string $role ): void { + if ( null !== $role ) { + $this->login_as( $role ); + } + $this->register_abilities(); + + $before = $this->snapshot(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Refused', + ), + ) + ); + + $this->assertWPError( $result, 'The call should be refused.' ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse the call.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns inputs for which the permission callback returns false, even for an administrator. + * + * @return array The inputs. + */ + public static function data_inputs_refused_by_the_permission_callback(): array { + return array( + 'no input' => array( + 'input' => null, + ), + 'no data' => array( + 'input' => array(), + ), + 'data that is a string' => array( + 'input' => array( 'data' => 'post' ), + ), + 'no type' => array( + 'input' => array( 'data' => array( 'title' => 'x' ) ), + ), + 'type that is a list' => array( + 'input' => array( 'data' => array( 'type' => array( 'post' ) ) ), + ), + 'type that is a number' => array( + 'input' => array( 'data' => array( 'type' => 1 ) ), + ), + 'unexposed type' => array( + 'input' => array( 'data' => array( 'type' => 'attachment' ) ), + ), + 'unregistered type' => array( + 'input' => array( 'data' => array( 'type' => 'cw_missing' ) ), + ), + ); + } + + /** + * The permission callback returns false for missing or malformed input. + * + * Covers S1 and S3. + * + * @dataProvider data_inputs_refused_by_the_permission_callback + * + * @since 7.3.0 + * + * @param mixed $input The input. + */ + public function test_permission_callback_returns_false_for_malformed_input( $input ): void { + $this->login_as( 'administrator' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertFalse( $permission( $input ), 'The permission callback should return false.' ); + } + + /** + * The permission callback returns true for a user who may create posts of an exposed type. + * + * @since 7.3.0 + */ + public function test_permission_callback_returns_true_for_a_user_who_may_create_posts(): void { + $this->login_as( 'contributor' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertTrue( $permission( array( 'data' => array( 'type' => 'post' ) ) ), 'The permission callback should return true.' ); + } + + /** + * Returns the outcome of creating a post per role and requested status. + * + * @return array The cases. + */ + public static function data_create_by_role_and_status(): array { + $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; + $private_message = 'Sorry, you are not allowed to create private posts in this post type.'; + + return array( + 'administrator, no status' => array( 'administrator', null, 'draft', null ), + 'administrator, draft' => array( 'administrator', 'draft', 'draft', null ), + 'administrator, pending' => array( 'administrator', 'pending', 'pending', null ), + 'administrator, publish' => array( 'administrator', 'publish', 'publish', null ), + 'administrator, future' => array( 'administrator', 'future', 'future', null ), + 'administrator, private' => array( 'administrator', 'private', 'private', null ), + 'editor, no status' => array( 'editor', null, 'draft', null ), + 'editor, draft' => array( 'editor', 'draft', 'draft', null ), + 'editor, pending' => array( 'editor', 'pending', 'pending', null ), + 'editor, publish' => array( 'editor', 'publish', 'publish', null ), + 'editor, future' => array( 'editor', 'future', 'future', null ), + 'editor, private' => array( 'editor', 'private', 'private', null ), + 'author, no status' => array( 'author', null, 'draft', null ), + 'author, draft' => array( 'author', 'draft', 'draft', null ), + 'author, pending' => array( 'author', 'pending', 'pending', null ), + 'author, publish' => array( 'author', 'publish', 'publish', null ), + 'author, future' => array( 'author', 'future', 'future', null ), + 'author, private' => array( 'author', 'private', 'private', null ), + 'contributor, no status' => array( 'contributor', null, 'draft', null ), + 'contributor, draft' => array( 'contributor', 'draft', 'draft', null ), + 'contributor, pending' => array( 'contributor', 'pending', 'pending', null ), + 'contributor, publish' => array( 'contributor', 'publish', 'content_cannot_publish', $publish_message ), + 'contributor, future' => array( 'contributor', 'future', 'content_cannot_publish', $publish_message ), + 'contributor, private' => array( 'contributor', 'private', 'content_cannot_publish', $private_message ), + ); + } + + /** + * Each role may create posts in the statuses it may set, as their own author. + * + * `future`, `publish` and `private` need `publish_posts`. A scheduled post is sent with a + * date in the future. + * + * Covers S4, S5 and S7. + * + * @dataProvider data_create_by_role_and_status + * + * @since 7.3.0 + * + * @param string $role The role to create as. + * @param string|null $status The requested status, or null to send none. + * @param string $expected The stored status, or the error code. + * @param string|null $message The error message, or null on success. + */ + public function test_create_by_role_and_status( string $role, ?string $status, string $expected, ?string $message ): void { + $this->login_as( $role ); + $this->register_abilities(); + + $data = array( + 'type' => 'post', + 'title' => 'Matrix post', + ); + if ( null !== $status ) { + $data['status'] = $status; + } + if ( 'future' === $status ) { + $data['date'] = array( 'gmt' => self::FUTURE_GMT ); + } + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => $data, + 'fields' => array( 'status' ), + ) + ); + + if ( null !== $message ) { + $this->assert_error( $result, $expected, 403, 'Creating a post in a status the role may not set' ); + $this->assertSame( $message, $result->get_error_message(), 'The message should name what the role may not do.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + return; + } + + $post = $this->get_created_post( $result ); + + $this->assertSame( $expected, $result['status'], 'The returned status should be the stored status.' ); + $this->assertSame( $expected, $post->post_status, 'The post should be stored in the expected status.' ); + $this->assertSame( self::$user_ids[ $role ], (int) $post->post_author, 'The current user should be the author.' ); + $this->assertSame( 'post', $post->post_type, 'The post should have the requested type.' ); + } + + /** + * A type whose `show_in_abilities` is turned off after registration cannot be created. + * + * Covers S3 and S5. + * + * @since 7.3.0 + */ + public function test_type_no_longer_exposed_cannot_be_created(): void { + $this->login_as( 'administrator' ); + $callbacks = $this->get_ability_callbacks(); + + get_post_type_object( 'post' )->show_in_abilities = false; + + $before = $this->snapshot(); + $input = array( + 'data' => array( + 'type' => 'post', + 'title' => 'Hidden', + ), + ); + + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, 'The regular call should fail.' ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a type that is no longer exposed.' ); + + $direct = $callbacks['execute_callback']( $input ); + + $this->assert_error( $direct, 'content_not_found', 404, 'A direct call for a type that is no longer exposed' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns direct-call inputs without an exposed post type. + * + * @return array The inputs. + */ + public static function data_direct_inputs_without_an_exposed_type(): array { + return array( + 'no input' => array( + 'input' => null, + ), + 'no data' => array( + 'input' => array(), + ), + 'data that is a string' => array( + 'input' => array( 'data' => 'post' ), + ), + 'no type' => array( + 'input' => array( 'data' => array( 'title' => 'x' ) ), + ), + 'type that is a list' => array( + 'input' => array( + 'data' => array( + 'type' => array( 'post' ), + 'title' => 'x', + ), + ), + ), + 'attachment' => array( + 'input' => array( + 'data' => array( + 'type' => 'attachment', + 'title' => 'x', + ), + ), + ), + 'revision' => array( + 'input' => array( + 'data' => array( + 'type' => 'revision', + 'title' => 'x', + ), + ), + ), + 'unexposed type' => array( + 'input' => array( + 'data' => array( + 'type' => 'cw_hidden', + 'title' => 'x', + ), + ), + ), + ); + } + + /** + * Called directly, input without an exposed post type is the not-found error. + * + * Covers S1, S3, S5 and S13. + * + * @dataProvider data_direct_inputs_without_an_exposed_type + * + * @since 7.3.0 + * + * @param mixed $input The input. + */ + public function test_direct_call_without_an_exposed_type_is_not_found( $input ): void { + register_post_type( 'cw_hidden', array( 'public' => true ) ); + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot(); + $result = $execute( $input ); + + $this->assert_error( $result, 'content_not_found', 404, 'A direct call without an exposed type' ); + $this->assertSame( 'The requested content was not found.', $result->get_error_message(), 'The message should be the not-found message.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns `data` values that are invalid in some way. + * + * @return array}> The data. + */ + public static function data_invalid_data(): array { + return array( + 'parent on a post' => array( + 'data' => array( + 'type' => 'post', + 'parent' => 1, + ), + ), + 'unregistered status' => array( + 'data' => array( + 'type' => 'post', + 'status' => 'cw_unregistered', + ), + ), + 'publish' => array( + 'data' => array( + 'type' => 'post', + 'status' => 'publish', + ), + ), + 'empty slug' => array( + 'data' => array( + 'type' => 'post', + 'title' => 'x', + 'slug' => '!!!', + ), + ), + 'missing author' => array( + 'data' => array( + 'type' => 'post', + 'title' => 'x', + 'author' => self::MISSING_ID, + ), + ), + 'title that is a list' => array( + 'data' => array( + 'type' => 'post', + 'title' => array( 'x' ), + ), + ), + 'invalid date' => array( + 'data' => array( + 'type' => 'post', + 'title' => 'x', + 'date' => array( 'site' => 'not a date' ), + ), + ), + ); + } + + /** + * Called directly by a user without `create_posts`, the call fails on the capability before + * anything about the attributes is checked. + * + * Covers S4, S5 and O2. + * + * @dataProvider data_invalid_data + * + * @since 7.3.0 + * + * @param array $data The data. + */ + public function test_direct_call_without_create_posts_fails_before_the_attributes( array $data ): void { + $this->login_as( 'subscriber' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot(); + $result = $execute( array( 'data' => $data ) ); + + $this->assert_error( $result, 'content_cannot_create', 403, 'A direct call without create_posts' ); + $this->assertSame( 'Sorry, you are not allowed to create posts as this user.', $result->get_error_message(), 'The message should be the create message.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns keys that are not part of the input. + * + * @return array The keys and values. + */ + public static function data_keys_outside_the_allow_list(): array { + return array( + 'ID' => array( 'ID', 1 ), + 'post_type' => array( 'post_type', 'page' ), + 'post_password' => array( 'post_password', 'secret' ), + 'meta_input' => array( 'meta_input', array( 'cw_key' => 'value' ) ), + 'tax_input' => array( 'tax_input', array( 'category' => array( 1 ) ) ), + 'comment_status' => array( 'comment_status', 'closed' ), + 'post_author' => array( 'post_author', 1 ), + 'import_id' => array( 'import_id', 7654321 ), + 'guid' => array( 'guid', 'https://example.org/forged' ), + 'page_template' => array( 'page_template', 'cw-template.php' ), + 'modified' => array( 'modified', '2001-01-01T00:00:00' ), + 'post_modified' => array( 'post_modified', '2001-01-01 00:00:00' ), + 'post_modified_gmt' => array( 'post_modified_gmt', '2001-01-01 00:00:00' ), + 'sticky' => array( 'sticky', true ), + 'tags_input' => array( 'tags_input', array( 'cw-tag' ) ), + 'post_category' => array( 'post_category', array( 2 ) ), + 'menu_order' => array( 'menu_order', 5 ), + 'post_mime_type' => array( 'post_mime_type', 'image/jpeg' ), + ); + } + + /** + * A key outside the allow-list fails input validation. + * + * Covers S1, S5 and S6. + * + * @dataProvider data_keys_outside_the_allow_list + * + * @since 7.3.0 + * + * @param string $key The key. + * @param mixed $value The value. + */ + public function test_key_outside_the_allow_list_fails_input_validation( string $key, $value ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Extra key', + $key => $value, + ), + ) + ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the key.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Called directly, keys outside the allow-list are ignored and never written. + * + * Covers S6. + * + * @since 7.3.0 + */ + public function test_direct_call_ignores_keys_outside_the_allow_list(): void { + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $existing_before = get_post( self::$post_ids['published_post'] )->to_array(); + $category_id = self::factory()->category->create(); + + $result = $execute( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Allow-list', + 'ID' => self::$post_ids['published_post'], + 'post_type' => 'page', + 'post_password' => 'secret', + 'meta_input' => array( 'cw_key' => 'value' ), + 'tax_input' => array( 'category' => array( 1 ) ), + 'comment_status' => 'closed', + 'post_author' => self::$user_ids['editor'], + 'import_id' => 7654321, + 'guid' => 'https://example.org/forged', + 'page_template' => 'cw-template.php', + 'modified' => '2001-01-01T00:00:00', + 'post_modified' => '2001-01-01 00:00:00', + 'post_modified_gmt' => '2001-01-01 00:00:00', + 'sticky' => true, + 'tags_input' => array( 'cw-tag' ), + 'post_category' => array( $category_id ), + 'menu_order' => 5, + 'post_mime_type' => 'image/jpeg', + ), + 'unknown' => 'ignored', + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertNotSame( self::$post_ids['published_post'], $post->ID, 'A new post should be created.' ); + $this->assertNotSame( 7654321, $post->ID, 'The import ID should not be used.' ); + $this->assertSame( 'post', $post->post_type, 'The post type should come from data.type.' ); + $this->assertSame( 'Allow-list', $post->post_title, 'The title should be stored.' ); + $this->assertSame( '', $post->post_password, 'No password should be stored.' ); + $this->assertSame( array(), get_post_meta( $post->ID, 'cw_key' ), 'No meta should be stored.' ); + $this->assertSame( 'open', $post->comment_status, 'The comment status should be the default.' ); + $this->assertSame( self::$user_ids['administrator'], (int) $post->post_author, 'The author should be the current user.' ); + $this->assertStringNotContainsString( 'forged', $post->guid, 'The GUID should not be the sent one.' ); + $this->assertSame( '', get_post_meta( $post->ID, '_wp_page_template', true ), 'No page template should be stored.' ); + // A new draft without a date floats, so only the site-time modified date holds the save time. + $this->assertNotSame( '2001-01-01 00:00:00', $post->post_modified, 'The sent site-time modified date should not be stored.' ); + $this->assertNotSame( '2001-01-01 00:00:00', $post->post_modified_gmt, 'The sent GMT modified date should not be stored.' ); + $this->assertEqualsWithDelta( time(), strtotime( get_gmt_from_date( $post->post_modified ) . ' UTC' ), 60, 'The stored site-time modified date should be the time of the save.' ); + $this->assertFalse( is_sticky( $post->ID ), 'The post should not be sticky.' ); + $this->assertSame( array(), wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ), 'No tag should be stored.' ); + $this->assertSame( array( (int) get_option( 'default_category' ) ), wp_get_post_categories( $post->ID ), 'Only the default category should be stored.' ); + $this->assertSame( 0, $post->menu_order, 'The menu order should be 0.' ); + $this->assertSame( '', $post->post_mime_type, 'No MIME type should be stored.' ); + $this->assertSame( $existing_before, get_post( self::$post_ids['published_post'] )->to_array(), 'The post named by ID should not change.' ); + } + + /** + * Returns statuses that can never be set. + * + * @return array The statuses. + */ + public static function data_statuses_that_can_never_be_set(): array { + return array( + 'trash' => array( 'trash' ), + 'auto-draft' => array( 'auto-draft' ), + 'inherit' => array( 'inherit' ), + 'unregistered' => array( 'cw_unregistered' ), + 'request-pending' => array( 'request-pending' ), + 'request-confirmed' => array( 'request-confirmed' ), + 'upper case' => array( 'PUBLISH' ), + 'trailing space' => array( 'publish ' ), + 'empty string' => array( '' ), + 'comma-joined list' => array( 'draft,publish' ), + ); + } + + /** + * An internal or unregistered status fails input validation. + * + * Covers S5 and S7. + * + * @dataProvider data_statuses_that_can_never_be_set + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_status_that_can_never_be_set_fails_input_validation( string $status ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Status', + 'status' => $status, + ), + ) + ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the status.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Called directly, an internal or unregistered status is an invalid status. + * + * Covers S1, S5 and S7. + * + * @dataProvider data_statuses_that_can_never_be_set + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_direct_call_rejects_a_status_that_can_never_be_set( string $status ): void { + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot(); + $result = $execute( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Status', + 'status' => $status, + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'status', 'A direct call with a status that can never be set' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * A status that is no longer registered when the call runs is an invalid status. + * + * Covers S1, S5 and S7. + * + * @since 7.3.0 + */ + public function test_status_no_longer_registered_is_rejected(): void { + register_post_status( + 'cw_temporary', + array( + 'label' => 'Temporary', + 'public' => true, + ) + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + _unregister_post_status( 'cw_temporary' ); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Temporary', + 'status' => 'cw_temporary', + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'status', 'A status that is no longer registered' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Registers custom statuses of every visibility, before the abilities are registered. + * + * @since 7.3.0 + */ + private function register_custom_statuses(): void { + register_post_status( + 'cw_public', + array( + 'label' => 'Public', + 'public' => true, + ) + ); + register_post_status( + 'cw_private', + array( + 'label' => 'Private', + 'private' => true, + ) + ); + register_post_status( + 'cw_viewable', + array( + 'label' => 'Viewable', + 'internal' => false, + 'public' => false, + 'publicly_queryable' => true, + ) + ); + + $this->assertTrue( is_post_status_viewable( 'cw_viewable' ), 'Precondition: the viewable status should be publicly viewable.' ); + $this->assertFalse( get_post_status_object( 'cw_viewable' )->internal, 'Precondition: the viewable status should not be internal.' ); + register_post_status( + 'cw_protected', + array( + 'label' => 'Protected', + 'protected' => true, + ) + ); + } + + /** + * Returns custom statuses that make a post visible. + * + * @return array The statuses. + */ + public static function data_custom_statuses_that_make_a_post_visible(): array { + return array( + 'public' => array( 'cw_public' ), + 'private' => array( 'cw_private' ), + 'publicly viewable' => array( 'cw_viewable' ), + ); + } + + /** + * A custom status that makes a post visible needs `publish_posts`, and the refusal uses the + * general message, also for a custom private status (C6). + * + * Covers S5 and S7. + * + * @dataProvider data_custom_statuses_that_make_a_post_visible + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_custom_visible_status_needs_publish_posts( string $status ): void { + $this->register_custom_statuses(); + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Custom status', + 'status' => $status, + ), + ) + ); + + $this->assert_error( $result, 'content_cannot_publish', 403, 'A custom visible status without publish_posts' ); + $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $result->get_error_message(), 'A custom status, also a private one, should get the general message.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * A user with `publish_posts` may set a custom status that makes a post visible. + * + * Covers S7. + * + * @dataProvider data_custom_statuses_that_make_a_post_visible + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_custom_visible_status_is_set_with_publish_posts( string $status ): void { + $this->register_custom_statuses(); + + $this->login_as( 'author' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Custom status', + 'status' => $status, + ), + ) + ); + + $this->assertSame( $status, $this->get_created_post( $result )->post_status, 'The custom status should be stored.' ); + } + + /** + * A custom status that is neither public, private nor publicly viewable needs nothing beyond + * `create_posts`. + * + * Covers S7. + * + * @since 7.3.0 + */ + public function test_custom_status_that_keeps_a_post_hidden_needs_no_publish_posts(): void { + $this->register_custom_statuses(); + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Protected status', + 'status' => 'cw_protected', + ), + ) + ); + + $this->assertSame( 'cw_protected', $this->get_created_post( $result )->post_status, 'The custom status should be stored.' ); + } + + /** + * Returns dates that change the stored status of a new post. + * + * @return array The cases. + */ + public static function data_statuses_reported_as_stored(): array { + return array( + 'publish with a future date' => array( 'publish', self::FUTURE_GMT, 'future' ), + 'future with a past date' => array( 'future', '2016-12-12T19:00:00', 'publish' ), + ); + } + + /** + * The regular save rules decide the stored status, and the output reports it. + * + * @dataProvider data_statuses_reported_as_stored + * + * @since 7.3.0 + * + * @param string $status The requested status. + * @param string $gmt The requested GMT date. + * @param string $expected The stored status. + */ + public function test_output_reports_the_stored_status( string $status, string $gmt, string $expected ): void { + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Scheduled', + 'status' => $status, + 'date' => array( 'gmt' => $gmt ), + ), + 'fields' => array( 'status' ), + ) + ); + + $this->assertSame( $expected, $this->get_created_post( $result )->post_status, 'The post should be stored in the expected status.' ); + $this->assertSame( $expected, $result['status'], 'The output should report the stored status.' ); + } + + /** + * Returns authors that a user without `edit_others_posts` may not name. + * + * @return array The authors, as fixture names. + */ + public static function data_authors_named_without_edit_others_posts(): array { + return array( + 'existing user' => array( 'editor' ), + 'missing user' => array( 'missing' ), + ); + } + + /** + * A user without `edit_others_posts` gets the same error for an existing and a missing + * author, so the error never tells whether a user exists. + * + * Covers S5, S8 and O3. + * + * @dataProvider data_authors_named_without_edit_others_posts + * + * @since 7.3.0 + * + * @param string $author The author, as a fixture name. + */ + public function test_naming_another_author_without_edit_others_posts_is_refused( string $author ): void { + $this->login_as( 'author' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Ghost written', + 'author' => self::$user_ids[ $author ] ?? self::MISSING_ID, + ), + ) + ); + + $this->assertWPError( $result, 'Naming another author should fail.' ); + $this->assertSame( + array( + 'code' => 'content_cannot_edit_others', + 'message' => 'Sorry, you are not allowed to create posts as this user.', + 'data' => array( 'status' => 403 ), + ), + array( + 'code' => $result->get_error_code(), + 'message' => $result->get_error_message(), + 'data' => $result->get_error_data(), + ), + 'The error should be the same whether the user exists or not.' + ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * A user without `edit_others_posts` may name themselves as the author. + * + * @since 7.3.0 + */ + public function test_naming_oneself_as_the_author_needs_no_edit_others_posts(): void { + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Own post', + 'author' => self::$user_ids['contributor'], + ), + ) + ); + + $this->assertSame( self::$user_ids['contributor'], (int) $this->get_created_post( $result )->post_author, 'The current user should be the author.' ); + } + + /** + * A user with `edit_others_posts` may name another existing user as the author. + * + * @since 7.3.0 + */ + public function test_naming_another_author_with_edit_others_posts(): void { + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'For the author', + 'author' => self::$user_ids['author'], + ), + ) + ); + + $this->assertSame( self::$user_ids['author'], (int) $this->get_created_post( $result )->post_author, 'The named user should be the author.' ); + } + + /** + * A user with `edit_others_posts` who names a missing user gets the invalid author error. + * + * Covers S5 and S8. + * + * @since 7.3.0 + */ + public function test_naming_a_missing_author_with_edit_others_posts_is_an_invalid_author(): void { + $this->login_as( 'editor' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Nobody', + 'author' => self::MISSING_ID, + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'author', 'A missing author' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * On multisite, a user who is not a member of the site cannot be the author. + * + * Covers S5 and S8. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_invalid_author(): void { + $non_member = self::factory()->user->create( array( 'role' => 'author' ) ); + remove_user_from_blog( $non_member, get_current_blog_id() ); + + $this->assertFalse( is_user_member_of_blog( $non_member ), 'Precondition: the user should not be a member of the site.' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Not a member', + 'author' => $non_member, + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * On multisite, a user without `edit_others_posts` who names a user who is not a member of the + * site gets the same error as for a missing user. + * + * Covers S8 and O3. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_naming_a_non_member_without_edit_others_posts_is_refused_like_a_missing_user(): void { + $non_member = self::factory()->user->create( array( 'role' => 'author' ) ); + remove_user_from_blog( $non_member, get_current_blog_id() ); + + $this->login_as( 'author' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $errors = array(); + foreach ( array( + 'non-member' => $non_member, + 'missing' => self::MISSING_ID, + ) as $key => $author ) { + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Ghost written', + 'author' => $author, + ), + ) + ); + $this->assertWPError( $result, "Naming the {$key} user should fail." ); + $errors[ $key ] = array( $result->get_error_code(), $result->get_error_message(), $result->get_error_data() ); + } + + $this->assertSame( 'content_cannot_edit_others', $errors['missing'][0], 'The error should be the authorship error.' ); + $this->assertSame( $errors['missing'], $errors['non-member'], 'The error should be the same for a non-member and a missing user.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for either user.' ); + } + + /** + * Returns parents that a page may not have. + * + * @return array The parents, as fixture names. + */ + public static function data_parents_that_cannot_be_used(): array { + return array( + 'missing post' => array( 'missing' ), + 'post of another type' => array( 'published_post' ), + 'page the user may not read' => array( 'editor_draft_page' ), + 'revision of a page' => array( 'page_revision' ), + ); + } + + /** + * A missing parent, a parent of another type and a parent the user may not read give one + * identical error, so the error never tells whether a post exists. + * + * Covers S5 and S9. + * + * @dataProvider data_parents_that_cannot_be_used + * + * @since 7.3.0 + * + * @param string $parent_key The parent, as a fixture name. + */ + public function test_parent_that_cannot_be_used_gives_one_identical_error( string $parent_key ): void { + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $this->assertTrue( current_user_can( get_post_type_object( 'page' )->cap->create_posts ), 'Precondition: the user should be able to create pages.' ); + $this->assertFalse( current_user_can( 'read_post', self::$post_ids['editor_draft_page'] ), 'Precondition: the user should not be able to read the draft page of the editor.' ); + + $create = function ( int $parent_id ) { + return $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Child', + 'parent' => $parent_id, + ), + ) + ); + }; + + $before = $this->snapshot(); + $reference = $create( self::MISSING_ID ); + $result = $create( self::$post_ids[ $parent_key ] ?? self::MISSING_ID ); + + $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); + $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for a missing parent.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * A readable page of the same type can be the parent, and `0` makes a top-level page. + * + * @since 7.3.0 + */ + public function test_parent_is_stored(): void { + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $child = $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Child', + 'parent' => self::$post_ids['published_page'], + ), + 'fields' => array( 'parent' ), + ) + ); + $top = $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Top', + 'parent' => 0, + ), + 'fields' => array( 'parent' ), + ) + ); + + $this->assertSame( self::$post_ids['published_page'], $this->get_created_post( $child )->post_parent, 'The parent should be stored.' ); + $this->assertSame( self::$post_ids['published_page'], $child['parent'], 'The parent should be returned.' ); + $this->assertSame( 0, $this->get_created_post( $top )->post_parent, 'A parent of 0 should make a top-level page.' ); + } + + /** + * Returns attributes that a post type does not support. + * + * @return array The cases. + */ + public static function data_unsupported_attributes(): array { + return array( + 'excerpt of a page' => array( 'page', 'excerpt', 'An excerpt' ), + 'parent of a post' => array( 'post', 'parent', 1 ), + 'parent 0 of a post' => array( 'post', 'parent', 0 ), + 'title without title support' => array( 'cw_no_title', 'title', 'A title' ), + 'content without editor support' => array( 'cw_title_only', 'content', 'Some content' ), + 'excerpt without excerpt support' => array( 'cw_title_only', 'excerpt', 'An excerpt' ), + 'author without author support' => array( 'cw_title_only', 'author', 1 ), + 'empty title without title support' => array( 'cw_no_title', 'title', '' ), + ); + } + + /** + * An attribute the post type does not support is rejected, even when it would change nothing. + * + * Covers S1, S5 and O4: the first failure is returned and nothing is written. + * + * @dataProvider data_unsupported_attributes + * + * @since 7.3.0 + * + * @param string $type The post type. + * @param string $attribute The attribute. + * @param mixed $value The value. + */ + public function test_unsupported_attribute_is_rejected( string $type, string $attribute, $value ): void { + $this->register_exposed_post_type( 'cw_no_title', array( 'editor' ) ); + $this->register_exposed_post_type( 'cw_title_only', array( 'title' ) ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $data = array( + 'type' => $type, + $attribute => $value, + ); + if ( 'title' !== $attribute && 'cw_no_title' !== $type ) { + $data['title'] = 'Unsupported'; + } + if ( 'cw_no_title' === $type ) { + $data['content'] = 'Unsupported'; + } + if ( 'author' === $attribute ) { + $data['author'] = self::$user_ids['administrator']; + } + + $before = $this->snapshot(); + $result = $this->run_ability( array( 'data' => $data ) ); + + $this->assert_invalid_attribute( $result, $attribute, 'An unsupported attribute' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns attribute values of the wrong type. + * + * @return array The cases. + */ + public static function data_values_of_the_wrong_type(): array { + return array( + 'title that is a number' => array( 'post', 'title', 123 ), + 'title that is a list' => array( 'post', 'title', array( 'x' ) ), + 'title that is null' => array( 'post', 'title', null ), + 'content that is a boolean' => array( 'post', 'content', true ), + 'excerpt that is a number' => array( 'post', 'excerpt', 5 ), + 'status that is a number' => array( 'post', 'status', 1 ), + 'slug that is a list' => array( 'post', 'slug', array( 'slug' ) ), + 'date that is a string' => array( 'post', 'date', '2016-12-12T14:00:00' ), + 'date with a number' => array( 'post', 'date', array( 'site' => 123 ) ), + 'date that cannot be parsed' => array( 'post', 'date', array( 'site' => 'not a date' ) ), + 'GMT date that cannot be parsed' => array( 'post', 'date', array( 'gmt' => 'not a date' ) ), + 'author that is text' => array( 'post', 'author', 'abc' ), + 'author 0' => array( 'post', 'author', 0 ), + 'author that is a fraction' => array( 'post', 'author', 1.5 ), + 'parent that is negative' => array( 'page', 'parent', -1 ), + 'parent that is text' => array( 'page', 'parent', 'abc' ), + ); + } + + /** + * Called directly, a value of the wrong type is an invalid attribute. + * + * Covers S1 and S5. + * + * @dataProvider data_values_of_the_wrong_type + * + * @since 7.3.0 + * + * @param string $type The post type. + * @param string $attribute The attribute. + * @param mixed $value The value. + */ + public function test_direct_call_rejects_a_value_of_the_wrong_type( string $type, string $attribute, $value ): void { + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $data = array( 'type' => $type ); + if ( 'title' !== $attribute ) { + $data['title'] = 'Wrong type'; + } + $data[ $attribute ] = $value; + + $before = $this->snapshot(); + $result = $execute( array( 'data' => $data ) ); + + $this->assert_invalid_attribute( $result, $attribute, 'A value of the wrong type' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * An empty title is valid next to other content. + * + * @since 7.3.0 + */ + public function test_empty_title_is_valid(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => '', + 'content' => 'Only content.', + ), + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertSame( '', $post->post_title, 'The empty title should be stored.' ); + $this->assertSame( 'Only content.', $post->post_content, 'The content should be stored.' ); + } + + /** + * A save error keeps its own code and message, with status 400 when the post is empty. + * + * Covers S5. + * + * @since 7.3.0 + */ + public function test_empty_post_is_the_save_error(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => '', + 'content' => '', + 'excerpt' => '', + ), + ) + ); + + $this->assert_error( $result, 'empty_content', 400, 'An empty post' ); + $this->assertSame( 'Content, title, and excerpt are empty.', $result->get_error_message(), 'The message should be the message of the save error.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * A database error during the save keeps its own code and message, with status 500. + * + * @since 7.3.0 + */ + public function test_database_error_is_the_save_error(): void { + global $wpdb; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot(); + + $wpdb->suppress_errors = true; + add_filter( + 'query', + static function ( $query ) { + return 0 === strpos( $query, 'INSERT' ) ? '],' : $query; + } + ); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Database error', + ), + ) + ); + + $wpdb->suppress_errors = false; + + $this->assert_error( $result, 'db_insert_error', 500, 'A database error' ); + $this->assertSame( 'Could not insert post into the database.', $result->get_error_message(), 'The message should be the message of the save error.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written after a database error.' ); + } + + /** + * The after-insert hooks fire once, as in any regular save. + * + * @since 7.3.0 + */ + public function test_after_insert_hooks_fire_once(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = did_action( 'wp_after_insert_post' ); + + $this->get_created_post( + $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Hooks', + ), + ) + ) + ); + + $this->assertSame( 1, did_action( 'wp_after_insert_post' ) - $before, 'wp_after_insert_post should fire once.' ); + } + + /** + * Returns slugs and the slug they are stored as. + * + * @return array The cases. + */ + public static function data_slugs(): array { + return array( + 'plain' => array( 'launch-notes', 'launch-notes' ), + 'with capitals' => array( 'Launch Notes!', 'launch-notes' ), + 'accented characters' => array( 'tęst-acceńted-chäræcters', 'test-accented-charaecters' ), + ); + } + + /** + * A slug is sanitized and stored. + * + * @dataProvider data_slugs + * + * @since 7.3.0 + * + * @param string $slug The sent slug. + * @param string $expected The stored slug. + */ + public function test_slug_is_sanitized_and_stored( string $slug, string $expected ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Slug', + 'slug' => $slug, + ), + 'fields' => array( 'slug' ), + ) + ); + + $this->assertSame( $expected, $this->get_created_post( $result )->post_name, 'The sanitized slug should be stored.' ); + $this->assertSame( $expected, $result['slug'], 'The stored slug should be returned.' ); + } + + /** + * A slug that sanitizes to nothing is an invalid slug. + * + * Covers S1 and S5. + * + * @since 7.3.0 + */ + public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Slug', + 'slug' => '!!!', + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'slug', 'A slug that sanitizes to nothing' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns statuses for which the slug of a published post is made unique. + * + * @return array The statuses, or null for the default. + */ + public static function data_statuses_with_unique_slugs(): array { + return array( + 'default status' => array( null ), + 'draft' => array( 'draft' ), + 'pending' => array( 'pending' ), + 'publish' => array( 'publish' ), + ); + } + + /** + * A new post never takes the slug of another post, also as a draft or a pending post. + * + * @dataProvider data_statuses_with_unique_slugs + * + * @since 7.3.0 + * + * @param string|null $status The status, or null to send none. + */ + public function test_slug_of_another_post_is_made_unique( ?string $status ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $data = array( + 'type' => 'post', + 'title' => 'Shared', + 'slug' => 'shared-slug', + ); + if ( null !== $status ) { + $data['status'] = $status; + } + + $result = $this->run_ability( array( 'data' => $data ) ); + + $this->assertSame( 'shared-slug-2', $this->get_created_post( $result )->post_name, 'The slug should be made unique.' ); + } + + /** + * Returns parents for a draft page with the slug of an existing child page. + * + * @return array The cases. + */ + public static function data_draft_page_slugs_by_parent(): array { + return array( + 'shared parent' => array( 'published_page', 'child-2' ), + 'different parent' => array( 'other_parent_page', 'child' ), + ); + } + + /** + * A draft page is made unique among the pages that share its parent only. + * + * @dataProvider data_draft_page_slugs_by_parent + * + * @since 7.3.0 + * + * @param string $parent_key The parent, as a fixture name. + * @param string $expected The stored slug. + */ + public function test_draft_page_slug_is_unique_among_its_siblings( string $parent_key, string $expected ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Child', + 'slug' => 'child', + 'parent' => self::$post_ids[ $parent_key ], + ), + ) + ); + + $this->assertSame( $expected, $this->get_created_post( $result )->post_name, 'The slug should be unique among the siblings.' ); + } + + /** + * Returns sent dates and the dates they are stored as, in America/New_York. + * + * @return array, local: string, gmt: string}> The cases. + */ + public static function data_dates(): array { + return array( + 'site without offset' => array( array( 'site' => '2016-12-12T14:00:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), + 'GMT without offset' => array( array( 'gmt' => '2016-12-12T19:00:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), + 'site with an offset' => array( array( 'site' => '2016-12-12T18:00:00-01:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), + 'GMT with an offset' => array( array( 'gmt' => '2016-12-12T18:00:00-01:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), + 'both, the site wins' => array( + array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => '2001-01-01T00:00:00', + ), + '2016-12-12 14:00:00', + '2016-12-12 19:00:00', + ), + ); + } + + /** + * A sent date sets both the local and the GMT date of a new draft. + * + * @dataProvider data_dates + * + * @since 7.3.0 + * + * @param array $date The sent date. + * @param string $local The stored local date. + * @param string $gmt The stored GMT date. + */ + public function test_date_is_stored_local_and_gmt( array $date, string $local, string $gmt ): void { + update_option( 'timezone_string', 'America/New_York' ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Dated', + 'date' => $date, + ), + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertSame( 'draft', $post->post_status, 'The post should be a draft.' ); + $this->assertSame( $local, $post->post_date, 'The local date should be stored.' ); + $this->assertSame( $gmt, $post->post_date_gmt, 'The GMT date should be stored.' ); + } + + /** + * A draft created without a date has a floating date. + * + * @since 7.3.0 + */ + public function test_draft_without_a_date_floats(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $post = $this->get_created_post( + $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Floating', + ), + ) + ) + ); + + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The draft should have no GMT date.' ); + } + + /** + * A date that is not a date-time fails input validation. + * + * Covers S5. + * + * @since 7.3.0 + */ + public function test_date_that_is_not_a_date_time_fails_input_validation(): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Invalid date', + 'date' => array( 'site' => '2010-60-01T02:00:00Z' ), + ), + ) + ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the date.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns markup and the values it is stored and rendered as for a user without + * `unfiltered_html`. + * + * @return array The cases. + */ + public static function data_filtered_markup(): array { + return array( + 'backslashes and non-ASCII text' => array( + '\o/ ¯\_(ツ)_/¯', + '\o/ ¯\_(ツ)_/¯', + '\o/ ¯\_(ツ)_/¯', + '\o/ ¯\_(ツ)_/¯', + '

\o/ ¯\_(ツ)_/¯

', + ), + 'entities' => array( + '\\\&\\\ & &invalid; < < &lt;', + '\\\&\\\ & &invalid; < < &lt;', + '\\\&\\\ & &invalid; < < &lt;', + '\\\&\\\ & &invalid; < < &lt;', + '

\\\&\\\ & &invalid; < < &lt;

', + ), + 'a script' => array( + '
div
strong ', + 'div strong ', + 'div strong', + '
div
strong ', + "
div
\n

strong

", + ), + 'a link with extra attributes' => array( + 'link', + 'link', + 'link', + 'link', + '

link

', + ), + ); + } + + /** + * Creates a post with the same markup in the title, content and excerpt, and returns the + * raw and rendered values. + * + * @since 7.3.0 + * + * @param string $markup The markup. + * @return array The ability result. + */ + private function create_with_markup( string $markup ): array { + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => $markup, + 'content' => $markup, + 'excerpt' => $markup, + ), + 'fields' => array( 'title', 'content.raw', 'content.rendered', 'excerpt.raw', 'excerpt.rendered' ), + ) + ); + + $this->assertIsArray( $result, 'The post with markup should be created.' ); + + return $result; + } + + /** + * The markup of an author, who lacks `unfiltered_html`, is filtered as on any other save. + * + * Covers S10. + * + * @dataProvider data_filtered_markup + * + * @since 7.3.0 + * + * @param string $input The sent markup. + * @param string $title_raw The stored title. + * @param string $title_rendered The rendered title, trimmed. + * @param string $text_raw The stored content and excerpt. + * @param string $text_rendered The rendered content and excerpt, trimmed. + */ + public function test_markup_of_an_author_is_filtered( string $input, string $title_raw, string $title_rendered, string $text_raw, string $text_rendered ): void { + $this->login_as( 'author' ); + + $this->assertFalse( current_user_can( 'unfiltered_html' ), 'Precondition: the author should not have unfiltered_html.' ); + + $result = $this->create_with_markup( $input ); + + $this->assertSame( $title_raw, $result['title']['raw'], 'The stored title should be filtered.' ); + $this->assertSame( $title_rendered, trim( $result['title']['rendered'] ), 'The rendered title should be filtered.' ); + $this->assertSame( $text_raw, $result['content']['raw'], 'The stored content should be filtered.' ); + $this->assertSame( $text_rendered, trim( $result['content']['rendered'] ), 'The rendered content should be filtered.' ); + $this->assertSame( $text_raw, $result['excerpt']['raw'], 'The stored excerpt should be filtered.' ); + $this->assertSame( $text_rendered, trim( $result['excerpt']['rendered'] ), 'The rendered excerpt should be filtered.' ); + } + + /** + * The markup of a contributor is filtered. + * + * Covers S10. + * + * @since 7.3.0 + */ + public function test_markup_of_a_contributor_is_filtered(): void { + $this->login_as( 'contributor' ); + + $result = $this->create_with_markup( '
div
strong ' ); + + $this->assertSame( 'div strong ', $result['title']['raw'], 'The stored title should be filtered.' ); + $this->assertSame( '
div
strong ', $result['content']['raw'], 'The stored content should be filtered.' ); + $this->assertSame( '
div
strong ', $result['excerpt']['raw'], 'The stored excerpt should be filtered.' ); + } + + /** + * On multisite, the markup of a site administrator, who lacks `unfiltered_html`, is filtered. + * + * Covers S10. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_markup_of_a_site_administrator_is_filtered_on_multisite(): void { + $this->login_as( 'administrator' ); + + $this->assertFalse( current_user_can( 'unfiltered_html' ), 'Precondition: the site administrator should not have unfiltered_html.' ); + + $result = $this->create_with_markup( '
div
strong ' ); + + $this->assertSame( 'div strong ', $result['title']['raw'], 'The stored title should be filtered.' ); + $this->assertSame( '
div
strong ', $result['content']['raw'], 'The stored content should be filtered.' ); + } + + /** + * Asserts that markup is stored unchanged. + * + * @since 7.3.0 + */ + private function assert_markup_is_stored_unchanged(): void { + $this->assertTrue( current_user_can( 'unfiltered_html' ), 'Precondition: the user should have unfiltered_html.' ); + + $markup = '
div
strong '; + $result = $this->create_with_markup( $markup ); + + $this->assertSame( $markup, $result['title']['raw'], 'The stored title should not be filtered.' ); + $this->assertSame( $markup, $result['content']['raw'], 'The stored content should not be filtered.' ); + $this->assertSame( $markup, $result['excerpt']['raw'], 'The stored excerpt should not be filtered.' ); + } + + /** + * The markup of an administrator with `unfiltered_html` is stored unchanged. + * + * Covers S10. + * + * @group ms-excluded + * + * @since 7.3.0 + */ + public function test_markup_of_an_administrator_is_not_filtered(): void { + $this->login_as( 'administrator' ); + + $this->assert_markup_is_stored_unchanged(); + } + + /** + * On multisite, the markup of a super admin is stored unchanged. + * + * Covers S10. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_markup_of_a_super_admin_is_not_filtered(): void { + $super_admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); + grant_super_admin( $super_admin ); + wp_set_current_user( $super_admin ); + + $this->assert_markup_is_stored_unchanged(); + } + + /** + * Quotes, backslashes and non-ASCII text survive a round trip unchanged. + * + * @since 7.3.0 + */ + public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { + $text = 'He said "it\'s done" \\ C:\\path\\to\\file \\\\server – café, naïve, 日本語, emoji ✓'; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => $text, + 'content' => $text, + 'excerpt' => $text, + ), + 'fields' => array( 'title.raw', 'content.raw', 'excerpt.raw' ), + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertSame( $text, $post->post_title, 'The stored title should be unchanged.' ); + $this->assertSame( $text, $post->post_content, 'The stored content should be unchanged.' ); + $this->assertSame( $text, $post->post_excerpt, 'The stored excerpt should be unchanged.' ); + $this->assertSame( $text, $result['title']['raw'], 'The returned title should be unchanged.' ); + $this->assertSame( $text, $result['content']['raw'], 'The returned content should be unchanged.' ); + $this->assertSame( $text, $result['excerpt']['raw'], 'The returned excerpt should be unchanged.' ); + } + + /** + * Raw fields are left out when the writer cannot edit the post after the write. + * + * The user may publish posts but not edit published posts. + * + * Covers S11. + * + * @since 7.3.0 + */ + public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new_post(): void { + $this->login_as( 'publisher' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Published once', + 'content' => 'Body.', + 'status' => 'publish', + ), + 'fields' => array( 'title', 'content.raw', 'content.rendered' ), + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertSame( 'publish', $post->post_status, 'The post should be published.' ); + $this->assertFalse( current_user_can( 'edit_post', $post->ID ), 'Precondition: the user should not be able to edit the published post.' ); + $this->assertSame( array( 'rendered' => 'Published once' ), $result['title'], 'The title should be returned without its raw value.' ); + $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); + } + + /** + * The current user is checked at each call, also on the same instance. + * + * Covers S4. + * + * @since 7.3.0 + */ + public function test_each_call_checks_the_current_user(): void { + $execute = $this->get_ability_callbacks()['execute_callback']; + $input = array( + 'data' => array( + 'type' => 'post', + 'title' => 'Twice', + ), + ); + + $this->login_as( 'author' ); + $first = $execute( $input ); + + $this->login_as( 'subscriber' ); + $before = $this->snapshot(); + $second = $execute( $input ); + + $this->assertSame( self::$user_ids['author'], (int) $this->get_created_post( $first )->post_author, 'The first call should create a post for the author.' ); + $this->assert_error( $second, 'content_cannot_create', 403, 'The second call as a subscriber' ); + $this->assertSame( $before, $this->snapshot(), 'The second call should write nothing.' ); + } + + /** + * Only the requested fields are returned, and the ID always is. + * + * Covers S11. + * + * @since 7.3.0 + */ + public function test_returns_the_requested_fields_and_the_id(): void { + $this->login_as( 'author' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Fields', + ), + 'fields' => array( 'author', 'title.rendered' ), + ) + ); + + $user = wp_get_current_user(); + $post = $this->get_created_post( $result ); + + $this->assertSame( + array( + 'id' => $post->ID, + 'title' => array( 'rendered' => 'Fields' ), + 'author' => array( + 'id' => $user->ID, + 'name' => $user->display_name, + 'slug' => $user->user_nicename, + ), + ), + $result, + 'The ID and the requested fields should be returned.' + ); + } + + /** + * Returns an integer in one of the forms that are read as that integer. + * + * @since 7.3.0 + * + * @param int $value The integer. + * @param string $form The form: `float`, `decimal`, `plus`, `leading space`, `trailing space`, + * `leading newline` or `exponent`. + * @return float|string The integer in that form. + */ + private static function whole_number_form( int $value, string $form ) { + $forms = array( + 'float' => (float) $value, + 'decimal' => "{$value}.0", + 'plus' => "+{$value}", + 'leading space' => " {$value}", + 'trailing space' => "{$value} ", + 'leading newline' => "\n{$value}", + 'exponent' => "{$value}e0", + ); + + return $forms[ $form ]; + } + + /** + * Returns the forms of a whole number that are read as the integer they equal. + * + * @return array The forms. + */ + public static function data_whole_number_forms(): array { + return array( + 'whole float' => array( 'float' ), + 'string with .0' => array( 'decimal' ), + 'string with plus' => array( 'plus' ), + 'string with leading space' => array( 'leading space' ), + 'string with trailing space' => array( 'trailing space' ), + 'string with leading line' => array( 'leading newline' ), + 'string with an exponent' => array( 'exponent' ), + ); + } + + /** + * Called directly, an author in another whole-number form is read as the integer it equals. + * + * Covers C3. + * + * @dataProvider data_whole_number_forms + * + * @since 7.3.0 + * + * @param string $form The form of the author. + */ + public function test_direct_call_reads_an_author_in_a_whole_number_form( string $form ): void { + $this->login_as( 'editor' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Whole number author', + 'author' => self::whole_number_form( self::$user_ids['author'], $form ), + ), + ) + ); + + $this->assertSame( self::$user_ids['author'], (int) $this->get_created_post( $result )->post_author, 'The user with that ID should be the author.' ); + } + + /** + * Called directly, a parent in another whole-number form is read as the integer it equals. + * + * Covers C3. + * + * @dataProvider data_whole_number_forms + * + * @since 7.3.0 + * + * @param string $form The form of the parent. + */ + public function test_direct_call_reads_a_parent_in_a_whole_number_form( string $form ): void { + $parent_id = self::$post_ids['published_page']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Whole number parent', + 'parent' => self::whole_number_form( $parent_id, $form ), + ), + ) + ); + + $this->assertSame( $parent_id, $this->get_created_post( $result )->post_parent, 'The page with that ID should be the parent.' ); + } + + /** + * Returns date values without a usable value for a direct call. + * + * @return array}> The dates. + */ + public static function data_dates_without_a_usable_value(): array { + return array( + 'empty date' => array( array() ), + 'GMT that is a number' => array( + array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => 123, + ), + ), + 'GMT that is a list' => array( + array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => array( '2016-12-12T19:00:00' ), + ), + ), + 'GMT that is null' => array( + array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => null, + ), + ), + ); + } + + /** + * Called directly, an empty date or a GMT value of the wrong type is an invalid attribute. + * + * Covers S5 (C2). + * + * @dataProvider data_dates_without_a_usable_value + * + * @since 7.3.0 + * + * @param array $date The date. + */ + public function test_direct_call_rejects_a_date_without_a_usable_value( array $date ): void { + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot(); + $result = $execute( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Unusable date', + 'date' => $date, + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'date', 'A direct call with a date without a usable value' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * A user who may create posts but not publish them cannot create a private post. + * + * Covers S5 and S7. + * + * @since 7.3.0 + */ + public function test_private_post_needs_publish_posts_from_a_user_who_may_create_posts(): void { + $user_id = self::factory()->user->create( array( 'role' => 'author' ) ); + // Denies the capability that the role grants. + get_userdata( $user_id )->add_cap( 'publish_posts', false ); + wp_set_current_user( $user_id ); + + $this->assertTrue( current_user_can( 'edit_posts' ), 'Precondition: the user should be able to create posts.' ); + $this->assertFalse( current_user_can( 'publish_posts' ), 'Precondition: the user should not be able to publish posts.' ); + + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Private without publish_posts', + 'status' => 'private', + ), + ) + ); + + $this->assert_error( $result, 'content_cannot_publish', 403, 'A private post without publish_posts' ); + $this->assertSame( 'Sorry, you are not allowed to create private posts in this post type.', $result->get_error_message(), 'The message should be the private message.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for the refused private post.' ); + } + + /** + * A pending post that a contributor creates without a date has a floating date. + * + * Covers S5. + * + * @since 7.3.0 + */ + public function test_pending_post_of_a_contributor_without_a_date_floats(): void { + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $post = $this->get_created_post( + $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Pending and floating', + 'status' => 'pending', + ), + ) + ) + ); + + $this->assertSame( 'pending', $post->post_status, 'The post should be pending.' ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The pending post should have no GMT date.' ); + } + + /** + * A type that gets `show_in_abilities` after the abilities were registered is not in the type + * enum, so a create of that type fails input validation. + * + * Covers S3 and S5. + * + * @since 7.3.0 + */ + public function test_type_exposed_after_registration_fails_input_validation(): void { + register_post_type( + 'cw_late', + array( + 'public' => true, + 'supports' => array( 'title' ), + ) + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + get_post_type_object( 'cw_late' )->show_in_abilities = true; + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'cw_late', + 'title' => 'Exposed too late', + ), + ) + ); + + $this->assertWPError( $result, 'A create of a type exposed after registration should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject a type exposed after registration.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for a type exposed after registration.' ); + } + + /** + * Registers an exposed post type whose create capability differs from its edit capability. + * + * @since 7.3.0 + */ + private function register_type_with_its_own_create_capability(): void { + register_post_type( + 'cw_book', + array( + 'public' => true, + 'show_in_abilities' => true, + 'supports' => array( 'title', 'editor' ), + 'capability_type' => 'cw_book', + 'map_meta_cap' => true, + 'capabilities' => array( 'create_posts' => 'create_cw_books' ), + ) + ); + + $caps = get_post_type_object( 'cw_book' )->cap; + + $this->assertSame( 'create_cw_books', $caps->create_posts, 'Precondition: the type should have its own create capability.' ); + $this->assertSame( 'edit_cw_books', $caps->edit_posts, 'Precondition: the type should have its own edit capability.' ); + } + + /** + * A user with only the create capability of a type may create a draft of it, but may not + * publish it. + * + * Covers S5 and S7. + * + * @since 7.3.0 + */ + public function test_user_with_only_the_create_capability_creates_a_draft_and_cannot_publish(): void { + $this->register_type_with_its_own_create_capability(); + + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + get_userdata( $user_id )->add_cap( 'create_cw_books' ); + wp_set_current_user( $user_id ); + + $this->assertFalse( current_user_can( 'edit_cw_books' ), 'Precondition: the user should not have the edit capability.' ); + + $this->register_abilities(); + + $draft = $this->run_ability( + array( + 'data' => array( + 'type' => 'cw_book', + 'title' => 'Draft book', + ), + ) + ); + + $post = $this->get_created_post( $draft ); + + $this->assertSame( 'cw_book', $post->post_type, 'The draft should be of the custom type.' ); + $this->assertSame( 'draft', $post->post_status, 'The new post should be a draft.' ); + $this->assertSame( $user_id, (int) $post->post_author, 'The user should be the author of the draft.' ); + + $before = $this->snapshot(); + $published = $this->run_ability( + array( + 'data' => array( + 'type' => 'cw_book', + 'title' => 'Published book', + 'status' => 'publish', + ), + ) + ); + + $this->assert_error( $published, 'content_cannot_publish', 403, 'Publishing with only the create capability' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for the refused publish.' ); + } + + /** + * Called directly, a user with only the edit capability of a type, and not its create + * capability, may not create a post of it. + * + * Covers S5. + * + * @since 7.3.0 + */ + public function test_direct_call_with_only_the_edit_capability_cannot_create(): void { + $this->register_type_with_its_own_create_capability(); + + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + get_userdata( $user_id )->add_cap( 'edit_cw_books' ); + wp_set_current_user( $user_id ); + + $this->assertFalse( current_user_can( 'create_cw_books' ), 'Precondition: the user should not have the create capability.' ); + + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot(); + $result = $execute( + array( + 'data' => array( + 'type' => 'cw_book', + 'title' => 'Book', + ), + ) + ); + + $this->assert_error( $result, 'content_cannot_create', 403, 'A direct call with only the edit capability' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * On multisite, after switching to a second site, a user who is an editor only on the first + * site may not create posts there. + * + * Covers S4 and S8. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_editor_of_the_first_site_only_is_refused_on_the_second_site(): void { + $blog_id = self::factory()->blog->create(); + + $this->assertFalse( is_user_member_of_blog( self::$user_ids['editor'], $blog_id ), 'Precondition: the editor should not be a member of the second site.' ); + + $this->login_as( 'editor' ); + $first_before = $this->snapshot(); + + switch_to_blog( $blog_id ); + $callbacks = $this->get_ability_callbacks(); + $input = array( + 'data' => array( + 'type' => 'post', + 'title' => 'Second site', + ), + ); + + $second_before = $this->snapshot(); + $regular = $this->run_ability( $input ); + $direct = $callbacks['execute_callback']( $input ); + $second_after = $this->snapshot(); + + restore_current_blog(); + + $this->assertWPError( $regular, 'The regular call on the second site should fail.' ); + $this->assertSame( 'ability_invalid_permissions', $regular->get_error_code(), 'The permission callback should use the capabilities of the second site.' ); + $this->assert_error( $direct, 'content_cannot_create', 403, 'A direct call on the second site' ); + $this->assertSame( $second_before, $second_after, 'Nothing should be written on the second site.' ); + $this->assertSame( $first_before, $this->snapshot(), 'Nothing should be written on the first site.' ); + } + + /** + * On multisite, after switching to a second site, an editor of that site who names a user who + * is a member only of the first site gets the invalid author error. + * + * Covers S5 and S8. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_member_of_the_first_site_only_is_an_invalid_author_on_the_second_site(): void { + $blog_id = self::factory()->blog->create(); + $editor_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + add_user_to_blog( $blog_id, $editor_id, 'editor' ); + + $this->assertFalse( is_user_member_of_blog( self::$user_ids['author'], $blog_id ), 'Precondition: the author should not be a member of the second site.' ); + + wp_set_current_user( $editor_id ); + switch_to_blog( $blog_id ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Second site', + 'author' => self::$user_ids['author'], + ), + ) + ); + $after = $this->snapshot(); + + restore_current_blog(); + + $this->assert_invalid_attribute( $result, 'author', 'An author who is a member of the first site only' ); + $this->assertSame( $before, $after, 'Nothing should be written on the second site.' ); + } + + /** + * A page in the Trash that the user may read is accepted as the parent. + * + * Covers S9 (C13). + * + * @since 7.3.0 + */ + public function test_trashed_page_the_user_may_read_is_accepted_as_the_parent(): void { + $parent_id = self::$post_ids['trashed_page']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $this->assertSame( 'trash', get_post_status( $parent_id ), 'Precondition: the parent should be in the Trash.' ); + $this->assertTrue( current_user_can( 'read_post', $parent_id ), 'Precondition: the editor should be able to read the trashed page.' ); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Child of a trashed page', + 'parent' => $parent_id, + ), + ) + ); + + $this->assertSame( $parent_id, $this->get_created_post( $result )->post_parent, 'The trashed page should be stored as the parent.' ); + } +} diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php new file mode 100644 index 0000000000000..7281e0786bd2f --- /dev/null +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -0,0 +1,1656 @@ + + */ + private const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); + + /** + * An ID that no post has. + * + * @since 7.3.0 + * + * @var int + */ + private const MISSING_ID = 987654321; + + /** + * The roles of the matrix tests. + * + * @since 7.3.0 + * + * @var list + */ + private const ROLES = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); + + /** + * The statuses of the matrix posts. `trash_` is a post trashed from ``. + * + * @since 7.3.0 + * + * @var list + */ + private const MATRIX_STATUSES = array( 'draft', 'pending', 'publish', 'future', 'private', 'trash_draft', 'trash_publish' ); + + /** + * Shared user IDs keyed by role or fixture name. + * + * @since 7.3.0 + * + * @var array + */ + private static $user_ids = array(); + + /** + * Shared post IDs keyed by fixture name. + * + * @since 7.3.0 + * + * @var array + */ + private static $post_ids = array(); + + /** + * Creates shared users and posts. + * + * @since 7.3.0 + * + * @param WP_UnitTest_Factory $factory The unit test factory. + */ + public static function wpSetUpBeforeClass( $factory ): void { + foreach ( self::ROLES as $role ) { + self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); + } + self::$user_ids['other'] = $factory->user->create( array( 'role' => 'editor' ) ); + + // One post per owner and status, for the matrix tests. + foreach ( array_merge( self::ROLES, array( 'other' ) ) as $owner ) { + foreach ( self::MATRIX_STATUSES as $status ) { + $stored_status = str_replace( 'trash_', '', $status ); + $post_id = $factory->post->create( + array( + 'post_author' => self::$user_ids[ $owner ], + 'post_status' => $stored_status, + 'post_date' => 'future' === $stored_status ? '2035-01-01 00:00:00' : '', + ) + ); + if ( $stored_status !== $status ) { + wp_trash_post( $post_id ); + } + self::$post_ids[ "{$owner}:{$status}" ] = $post_id; + } + } + + self::$post_ids['admin_post'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + ) + ); + + foreach ( array( 'admin_page', 'front_page', 'posts_page', 'privacy_page' ) as $page ) { + self::$post_ids[ $page ] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + } + + self::$post_ids['attachment'] = $factory->attachment->create_object( + 'image.jpg', + self::$post_ids['admin_post'], + array( + 'post_mime_type' => 'image/jpeg', + 'post_author' => self::$user_ids['administrator'], + ) + ); + + $revisioned_id = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_content' => 'First version.', + ) + ); + wp_update_post( + array( + 'ID' => $revisioned_id, + 'post_content' => 'Second version.', + ) + ); + self::$post_ids['revision'] = (int) array_key_first( wp_get_post_revisions( $revisioned_id ) ); + self::$post_ids['autosave'] = (int) _wp_put_post_revision( get_post( $revisioned_id ), true ); + + // Posts of core types that are not exposed. + foreach ( array( + 'reusable_block' => 'wp_block', + 'template' => 'wp_template', + 'menu_item' => 'nav_menu_item', + ) as $fixture => $post_type ) { + self::$post_ids[ $fixture ] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => $post_type, + 'post_status' => 'publish', + ) + ); + } + } + + /** + * Unregisters the content abilities and registers the content category. + * + * @since 7.3.0 + */ + public function set_up(): void { + parent::set_up(); + + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( ! wp_has_ability_category( 'content' ) ) { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + wp_register_ability_category( + 'content', + array( + 'label' => 'Content', + 'description' => 'Content.', + ) + ); + } + } + + /** + * Unregisters the content abilities and the category, and resets the post types. + * + * @since 7.3.0 + */ + public function tear_down(): void { + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( wp_has_ability_category( 'content' ) ) { + wp_unregister_ability_category( 'content' ); + } + + /* + * A test may turn off `show_in_abilities` on `post` and `page`, and the next class's + * wpSetUpBeforeClass() runs before the next set_up() resets the post types. + */ + $this->reset_post_types(); + + parent::tear_down(); + } + + /** + * Registers the content abilities inside a faked init action. + * + * @since 7.3.0 + */ + private function register_abilities(): void { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + ( new WP_Abilities_Content() )->register(); + } + + /** + * Registers the content abilities and returns the callbacks of the delete ability. + * + * The callbacks call private methods, so tests that skip input validation or the + * permission check capture them from the registration arguments. + * + * @since 7.3.0 + * + * @return array The permission and execute callbacks, keyed by argument name. + */ + private function get_ability_callbacks(): array { + $callbacks = array(); + + add_filter( + 'wp_register_ability_args', + static function ( array $args, string $name ) use ( &$callbacks ): array { + if ( self::ABILITY === $name ) { + $callbacks = array( + 'permission_callback' => $args['permission_callback'], + 'execute_callback' => $args['execute_callback'], + ); + } + + return $args; + }, + 10, + 2 + ); + + $this->register_abilities(); + + $this->assertArrayHasKey( 'execute_callback', $callbacks, 'The core/content-delete ability should be registered with callbacks.' ); + + return $callbacks; + } + + /** + * Runs the registered delete ability with the given input. + * + * @since 7.3.0 + * + * @param mixed $input The ability input. + * @return mixed The ability result. + */ + private function run_ability( $input ) { + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The core/content-delete ability should be registered.' ); + + return wp_get_ability( self::ABILITY )->execute( $input ); + } + + /** + * Builds the input that deletes a post. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @param bool|null $permanent The `permanent` value, or null to send none. + * @return array The input. + */ + private function delete_input( int $post_id, ?bool $permanent = null ): array { + $input = array( + 'where' => array( + 'type' => get_post_type( $post_id ), + 'id' => $post_id, + ), + ); + if ( null !== $permanent ) { + $input['permanent'] = $permanent; + } + + return $input; + } + + /** + * Logs in as the shared user with the given role or fixture name. + * + * @since 7.3.0 + * + * @param string $role The role or fixture name. + */ + private function login_as( string $role ): void { + wp_set_current_user( self::$user_ids[ $role ] ); + } + + /** + * Takes a snapshot of the stored content: the number of posts per type and status, + * revisions included, the number of meta rows, and the row and meta of one post. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @return array The snapshot. + */ + private function snapshot( int $post_id ): array { + global $wpdb; + + return array( + 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), + 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), + 'post' => $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id ), ARRAY_A ), + 'post_meta' => $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d ORDER BY meta_id", $post_id ), ARRAY_A ), + ); + } + + /** + * Asserts that a result is an error with the given code and status. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $code The expected error code. + * @param int $status The expected error status. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_error( $result, string $code, int $status, string $context ): void { + $this->assertWPError( $result, "{$context}: the call should fail." ); + $this->assertSame( $code, $result->get_error_code(), "{$context}: the error code should be {$code}." ); + + $data = $result->get_error_data(); + $this->assertIsArray( $data, "{$context}: the error should carry data." ); + $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); + } + + /** + * Asserts that a result is the not-found error. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_not_found( $result, string $context ): void { + $this->assert_error( $result, 'content_not_found', 404, $context ); + $this->assertSame( 'The requested content was not found.', $result->get_error_message(), "{$context}: the message should be the not-found message." ); + } + + /** + * Asserts that a result is the 403 delete error. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_cannot_delete( $result, string $context ): void { + $this->assert_error( $result, 'content_cannot_delete', 403, $context ); + $this->assertSame( 'Sorry, you are not allowed to delete this post.', $result->get_error_message(), "{$context}: the message should be the delete message." ); + } + + /** + * Asserts that a post was moved to the Trash and the result says so. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param int $post_id The post ID. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_trashed( $result, int $post_id, string $context ): void { + $this->assertSame( array( 'outcome' => 'trashed' ), $result, "{$context}: the outcome should be trashed." ); + + clean_post_cache( $post_id ); + + $this->assertSame( 'trash', get_post_status( $post_id ), "{$context}: the post should be in the Trash." ); + } + + /** + * Asserts that a post was deleted permanently and the result says so. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param int $post_id The post ID. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_deleted( $result, int $post_id, string $context ): void { + $this->assertSame( array( 'outcome' => 'deleted' ), $result, "{$context}: the outcome should be deleted." ); + + clean_post_cache( $post_id ); + + $this->assertNull( get_post( $post_id ), "{$context}: the post should no longer exist." ); + } + + /** + * Removes the `description` keywords of a schema at every depth. + * + * @since 7.3.0 + * + * @param array $schema The schema. + * @return array The schema without descriptions. + */ + private function strip_descriptions( array $schema ): array { + unset( $schema['description'] ); + foreach ( $schema as $key => $value ) { + if ( is_array( $value ) ) { + $schema[ $key ] = $this->strip_descriptions( $value ); + } + } + + return $schema; + } + + /** + * The ability is registered in the content category with its label and annotations. + * + * @since 7.3.0 + */ + public function test_registers_the_ability(): void { + $this->register_abilities(); + + $ability = wp_get_ability( self::ABILITY ); + + $this->assertInstanceOf( WP_Ability::class, $ability, 'The core/content-delete ability should be registered.' ); + $this->assertSame( 'Delete Content', $ability->get_label(), 'The ability should have its label.' ); + $this->assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); + $this->assertTrue( $ability->get_meta_item( 'public', false ), 'The ability should be marked public.' ); + + $annotations = $ability->get_meta_item( 'annotations', array() ); + $this->assertFalse( $annotations['readonly'], 'The ability should not be marked read-only.' ); + $this->assertTrue( $annotations['destructive'], 'The ability should be marked destructive.' ); + $this->assertTrue( $annotations['idempotent'], 'The ability should be marked idempotent.' ); + } + + /** + * The ability is not registered when no post type is exposed to abilities. + * + * @since 7.3.0 + */ + public function test_is_not_registered_without_exposed_post_types(): void { + foreach ( array( 'post', 'page' ) as $post_type ) { + get_post_type_object( $post_type )->show_in_abilities = false; + } + + $this->register_abilities(); + + $this->assertFalse( wp_has_ability( self::ABILITY ), 'The delete ability should not be registered without an exposed post type.' ); + } + + /** + * The input schema takes `where`, with the post type and ID, and `permanent`. + * + * @since 7.3.0 + */ + public function test_input_schema(): void { + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The delete ability should be registered.' ); + + $schema = wp_get_ability( self::ABILITY )->get_input_schema(); + + $this->assertSame( 'object', $schema['type'], 'The input should be an object.' ); + $this->assertFalse( $schema['additionalProperties'], 'The input should reject unknown keys.' ); + $this->assertSame( array( 'where' ), $schema['required'], 'The input should require where.' ); + $this->assertSame( array( 'where', 'permanent' ), array_keys( $schema['properties'] ), 'The input should take where and permanent.' ); + $this->assertSame( 'boolean', $schema['properties']['permanent']['type'], 'Permanent should be a boolean.' ); + + $update_where = wp_get_ability( 'core/content-update' )->get_input_schema()['properties']['where']; + + $this->assertSame( $this->strip_descriptions( $update_where ), $this->strip_descriptions( $schema['properties']['where'] ), 'The where should be the same as on update.' ); + $this->assertSame( array( 'type', 'id' ), $schema['properties']['where']['required'], 'The where should require the type and the ID.' ); + $this->assertSame( 1, $schema['properties']['where']['properties']['id']['minimum'], 'The ID should be at least 1.' ); + } + + /** + * The output holds the outcome only. + * + * @since 7.3.0 + */ + public function test_output_schema(): void { + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The delete ability should be registered.' ); + + $schema = wp_get_ability( self::ABILITY )->get_output_schema(); + + $this->assertSame( 'object', $schema['type'], 'The output should be an object.' ); + $this->assertFalse( $schema['additionalProperties'], 'The output should not take unknown keys.' ); + $this->assertSame( array( 'outcome' ), $schema['required'], 'The output should require the outcome.' ); + $this->assertSame( array( 'outcome' ), array_keys( $schema['properties'] ), 'The output should hold the outcome only.' ); + $this->assertSame( 'string', $schema['properties']['outcome']['type'], 'The outcome should be a string.' ); + } + + /** + * Example 1: trashing a post. + * + * Covers S12. + * + * @since 7.3.0 + */ + public function test_example_trashes_a_post(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $this->assert_trashed( + $this->run_ability( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + ) + ), + $post_id, + 'Trashing a post' + ); + } + + /** + * Example 2: deleting a page permanently. + * + * Covers S12. + * + * @since 7.3.0 + */ + public function test_example_deletes_a_page_permanently(): void { + $post_id = self::$post_ids['admin_page']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $this->assert_deleted( + $this->run_ability( + array( + 'where' => array( + 'type' => 'page', + 'id' => $post_id, + ), + 'permanent' => true, + ) + ), + $post_id, + 'Deleting a page permanently' + ); + } + + /** + * Example 3: trashing the post of example 1 again. + * + * Covers S5 and S12. + * + * @since 7.3.0 + */ + public function test_example_rejects_trashing_a_trashed_post(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $input = array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + ); + + $this->assert_trashed( $this->run_ability( $input ), $post_id, 'The first call' ); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, 'Trashing the post again should fail.' ); + $this->assertSame( + array( + 'code' => 'content_already_trashed', + 'message' => 'The post has already been trashed.', + 'data' => array( 'status' => 410 ), + ), + array( + 'code' => $result->get_error_code(), + 'message' => $result->get_error_message(), + 'data' => $result->get_error_data(), + ), + 'The error should be the already-trashed error of the example.' + ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A logged-out user is refused before the ability runs. + * + * Covers S4 and S5. + * + * @since 7.3.0 + */ + public function test_permission_callback_refuses_a_logged_out_user(): void { + $post_id = self::$post_ids['admin_post']; + + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $this->delete_input( $post_id, true ) ); + + $this->assertWPError( $result, 'The call should be refused.' ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a logged-out user.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns inputs for which the permission callback returns false, even for an administrator. + * + * @return array The inputs. + */ + public static function data_inputs_refused_by_the_permission_callback(): array { + return array( + 'no input' => array( null ), + 'no where' => array( array( 'permanent' => true ) ), + 'where that is a string' => array( array( 'where' => 'post' ) ), + 'type that is a number' => array( + array( + 'where' => array( + 'type' => 1, + 'id' => 1, + ), + ), + ), + 'unexposed type' => array( + array( + 'where' => array( + 'type' => 'revision', + 'id' => 1, + ), + ), + ), + ); + } + + /** + * The permission callback returns false for missing or malformed input. + * + * @dataProvider data_inputs_refused_by_the_permission_callback + * + * @since 7.3.0 + * + * @param mixed $input The input. + */ + public function test_permission_callback_returns_false_for_malformed_input( $input ): void { + $this->login_as( 'administrator' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertFalse( $permission( $input ), 'The permission callback should return false.' ); + } + + /** + * The permission callback is only a coarse gate: it lets a logged-in subscriber through, and + * the execute callback decides on the post. + * + * @since 7.3.0 + */ + public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type(): void { + $this->login_as( 'subscriber' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertTrue( $permission( $this->delete_input( self::$post_ids['admin_post'] ) ), 'The permission callback should return true.' ); + } + + /** + * Returns ID values that are not a whole number of at least 1. + * + * @return array The IDs. + */ + public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): array { + return array( + 'digits and letters' => array( '123abc' ), + 'fraction' => array( 1.5 ), + 'zero' => array( 0 ), + 'negative' => array( -1 ), + 'true' => array( true ), + 'null' => array( null ), + 'list' => array( array( 1 ) ), + ); + } + + /** + * An ID that is not a whole number of at least 1 fails input validation. + * + * Covers S2 and S5. + * + * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 + * + * @since 7.3.0 + * + * @param mixed $id The ID. + */ + public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( self::$post_ids['admin_post'] ); + $result = $this->run_ability( + array( + 'where' => array( + 'type' => 'post', + 'id' => $id, + ), + 'permanent' => true, + ) + ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the ID.' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); + } + + /** + * Returns ID values that a direct call does not read as a whole number of at least 1. + * + * @return array The IDs. + */ + public static function data_ids_that_are_not_whole_numbers_in_a_direct_call(): array { + return array_merge( + self::data_ids_that_are_not_whole_numbers_of_at_least_1(), + array( + 'hexadecimal string' => array( '0x1' ), + 'negative zero' => array( -0.0 ), + 'number above 2 ** 53' => array( 9007199254740993 ), + 'string above 2 ** 53' => array( '9007199254740993' ), + 'exponent above 2 ** 53' => array( '1e16' ), + ) + ); + } + + /** + * Called directly, an ID that is not a whole number of at least 1 is the not-found error and + * is never cast, even when it starts with the ID of an existing post. + * + * Covers S2, S5 and S13 (C3). + * + * @dataProvider data_ids_that_are_not_whole_numbers_in_a_direct_call + * + * @since 7.3.0 + * + * @param mixed $id The ID. + */ + public function test_direct_call_with_an_id_that_is_not_a_whole_number_is_not_found( $id ): void { + $post_id = self::$post_ids['admin_post']; + if ( '123abc' === $id ) { + $id = "{$post_id}abc"; + } elseif ( 1.5 === $id ) { + $id = $post_id + 0.5; + } elseif ( '0x1' === $id ) { + // The hexadecimal form of the ID of an existing post. + $id = '0x' . dechex( $post_id ); + } + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $id, + ), + 'permanent' => true, + ) + ); + + $this->assert_not_found( $result, 'A direct call with an ID that is not a whole number' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, an ID made of digits is read as that number. + * + * Covers S2. + * + * @since 7.3.0 + */ + public function test_direct_call_reads_an_id_made_of_digits(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => (string) $post_id, + ), + ) + ); + + $this->assert_trashed( $result, $post_id, 'A direct call with an ID made of digits' ); + } + + /** + * Returns posts that cannot be reached as a `post`. + * + * @return array The posts, as fixture names. + */ + public static function data_posts_that_are_not_posts(): array { + return array( + 'revision' => array( 'revision' ), + 'attachment' => array( 'attachment' ), + 'page' => array( 'admin_page' ), + 'missing' => array( 'missing' ), + 'reusable block' => array( 'reusable_block' ), + 'template' => array( 'template' ), + 'menu item' => array( 'menu_item' ), + 'autosave' => array( 'autosave' ), + ); + } + + /** + * A revision, an autosave, an attachment, a post of another exposed or non-exposed type and a + * missing post are not found as a `post`, even by an administrator and even for a permanent + * delete. + * + * Covers S3, S5 and S13. + * + * @dataProvider data_posts_that_are_not_posts + * + * @since 7.3.0 + * + * @param string $post The post, as a fixture name. + */ + public function test_post_of_another_type_is_not_found( string $post ): void { + $post_id = self::$post_ids[ $post ] ?? self::MISSING_ID; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'permanent' => true, + ) + ); + + $this->assert_not_found( $result, 'A post that is not a post' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, a post of a type that is not exposed is not found, whatever its ID. + * + * Covers S3 and S13. + * + * @since 7.3.0 + */ + public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): void { + register_post_type( 'cw_hidden', array( 'public' => true ) ); + $post_id = self::factory()->post->create( array( 'post_type' => 'cw_hidden' ) ); + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'cw_hidden', + 'id' => $post_id, + ), + 'permanent' => true, + ) + ); + + $this->assert_not_found( $result, 'A direct call for a type that is not exposed' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A post type whose `show_in_abilities` is turned off after registration cannot be deleted. + * + * Covers S3 and S5. + * + * @since 7.3.0 + */ + public function test_type_no_longer_exposed_cannot_be_deleted(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $callbacks = $this->get_ability_callbacks(); + + get_post_type_object( 'post' )->show_in_abilities = false; + + $input = $this->delete_input( $post_id, true ); + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, 'The regular call should fail.' ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a type that is no longer exposed.' ); + $this->assert_not_found( $callbacks['execute_callback']( $input ), 'A direct call for a type that is no longer exposed' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns direct-call inputs with a malformed `where`. + * + * @return array The inputs. + */ + public static function data_direct_inputs_with_a_malformed_where(): array { + return array( + 'no input' => array( null ), + 'no where' => array( array( 'permanent' => true ) ), + 'where that is a string' => array( array( 'where' => 'post' ) ), + 'no type' => array( array( 'where' => array( 'id' => 1 ) ) ), + 'no ID' => array( array( 'where' => array( 'type' => 'post' ) ) ), + ); + } + + /** + * Called directly, a malformed `where` is the not-found error. + * + * Covers S1 and S13. + * + * @dataProvider data_direct_inputs_with_a_malformed_where + * + * @since 7.3.0 + * + * @param mixed $input The input. + */ + public function test_direct_call_with_a_malformed_where_is_not_found( $input ): void { + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( self::$post_ids['admin_post'] ); + + $this->assert_not_found( $execute( $input ), 'A direct call with a malformed where' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be deleted for a malformed where.' ); + } + + /** + * The lookup comes before the capability: a user who may delete nothing gets the not-found + * error for a missing post, and the 403 error for an existing post. + * + * Covers O1, S4 and S13. + * + * @since 7.3.0 + */ + public function test_lookup_comes_before_the_capability(): void { + $this->login_as( 'subscriber' ); + $this->register_abilities(); + + $before = $this->snapshot( self::$post_ids['admin_post'] ); + $missing = $this->run_ability( + array( + 'where' => array( + 'type' => 'post', + 'id' => self::MISSING_ID, + ), + ) + ); + $existing = $this->run_ability( $this->delete_input( self::$post_ids['admin_post'] ) ); + + $this->assert_not_found( $missing, 'A missing post' ); + $this->assert_cannot_delete( $existing, 'An existing post the user may not delete' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'The refused post should still exist, unchanged.' ); + } + + /** + * Returns the outcome of trashing each matrix post per role. + * + * @return array The cases. + */ + public static function data_trash_matrix(): array { + return array( + 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', 'trashed' ), + 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', 'trashed' ), + 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', 'trashed' ), + 'administrator, own, future' => array( 'administrator', 'administrator', 'future', 'trashed' ), + 'administrator, own, private' => array( 'administrator', 'administrator', 'private', 'trashed' ), + 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', 'already_trashed' ), + 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', 'already_trashed' ), + 'administrator, other, draft' => array( 'administrator', 'other', 'draft', 'trashed' ), + 'administrator, other, pending' => array( 'administrator', 'other', 'pending', 'trashed' ), + 'administrator, other, publish' => array( 'administrator', 'other', 'publish', 'trashed' ), + 'administrator, other, future' => array( 'administrator', 'other', 'future', 'trashed' ), + 'administrator, other, private' => array( 'administrator', 'other', 'private', 'trashed' ), + 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', 'already_trashed' ), + 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', 'already_trashed' ), + 'editor, own, draft' => array( 'editor', 'editor', 'draft', 'trashed' ), + 'editor, own, pending' => array( 'editor', 'editor', 'pending', 'trashed' ), + 'editor, own, publish' => array( 'editor', 'editor', 'publish', 'trashed' ), + 'editor, own, future' => array( 'editor', 'editor', 'future', 'trashed' ), + 'editor, own, private' => array( 'editor', 'editor', 'private', 'trashed' ), + 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', 'already_trashed' ), + 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', 'already_trashed' ), + 'editor, other, draft' => array( 'editor', 'other', 'draft', 'trashed' ), + 'editor, other, pending' => array( 'editor', 'other', 'pending', 'trashed' ), + 'editor, other, publish' => array( 'editor', 'other', 'publish', 'trashed' ), + 'editor, other, future' => array( 'editor', 'other', 'future', 'trashed' ), + 'editor, other, private' => array( 'editor', 'other', 'private', 'trashed' ), + 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', 'already_trashed' ), + 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', 'already_trashed' ), + 'author, own, draft' => array( 'author', 'author', 'draft', 'trashed' ), + 'author, own, pending' => array( 'author', 'author', 'pending', 'trashed' ), + 'author, own, publish' => array( 'author', 'author', 'publish', 'trashed' ), + 'author, own, future' => array( 'author', 'author', 'future', 'trashed' ), + 'author, own, private' => array( 'author', 'author', 'private', 'trashed' ), + 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', 'already_trashed' ), + 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', 'already_trashed' ), + 'author, other, draft' => array( 'author', 'other', 'draft', 'forbidden' ), + 'author, other, pending' => array( 'author', 'other', 'pending', 'forbidden' ), + 'author, other, publish' => array( 'author', 'other', 'publish', 'forbidden' ), + 'author, other, future' => array( 'author', 'other', 'future', 'forbidden' ), + 'author, other, private' => array( 'author', 'other', 'private', 'forbidden' ), + 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', 'forbidden' ), + 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', 'forbidden' ), + 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', 'trashed' ), + 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', 'trashed' ), + 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', 'forbidden' ), + 'contributor, own, future' => array( 'contributor', 'contributor', 'future', 'forbidden' ), + 'contributor, own, private' => array( 'contributor', 'contributor', 'private', 'trashed' ), + 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', 'already_trashed' ), + 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', 'forbidden' ), + 'contributor, other, draft' => array( 'contributor', 'other', 'draft', 'forbidden' ), + 'contributor, other, pending' => array( 'contributor', 'other', 'pending', 'forbidden' ), + 'contributor, other, publish' => array( 'contributor', 'other', 'publish', 'forbidden' ), + 'contributor, other, future' => array( 'contributor', 'other', 'future', 'forbidden' ), + 'contributor, other, private' => array( 'contributor', 'other', 'private', 'forbidden' ), + 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', 'forbidden' ), + 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', 'forbidden' ), + 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', 'forbidden' ), + 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', 'forbidden' ), + 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', 'forbidden' ), + 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', 'forbidden' ), + 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', 'forbidden' ), + 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', 'forbidden' ), + 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', 'forbidden' ), + 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', 'forbidden' ), + 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', 'forbidden' ), + 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', 'forbidden' ), + 'subscriber, other, future' => array( 'subscriber', 'other', 'future', 'forbidden' ), + 'subscriber, other, private' => array( 'subscriber', 'other', 'private', 'forbidden' ), + 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', 'forbidden' ), + 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', 'forbidden' ), + ); + } + + /** + * Each role may trash the posts it may delete, gets the 410 error for those already in the + * Trash, and the 403 error for the others, whatever their status. + * + * Covers S4, S5, S12 and S13. + * + * @dataProvider data_trash_matrix + * + * @since 7.3.0 + * + * @param string $role The role to delete as. + * @param string $owner The role of the author of the post, or `other`. + * @param string $status The matrix status of the post. + * @param string $expected `trashed`, `already_trashed` or `forbidden`. + */ + public function test_trash_matrix( string $role, string $owner, string $status, string $expected ): void { + $post_id = self::$post_ids[ "{$owner}:{$status}" ]; + + $this->login_as( $role ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $this->delete_input( $post_id ) ); + + if ( 'trashed' === $expected ) { + $this->assert_trashed( $result, $post_id, 'Trashing a post the role may delete' ); + return; + } + + if ( 'already_trashed' === $expected ) { + $this->assert_error( $result, 'content_already_trashed', 410, 'Trashing a trashed post the role may delete' ); + } else { + $this->assert_cannot_delete( $result, 'Trashing a post the role may not delete' ); + } + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns the outcome of deleting each matrix post permanently per role. + * + * @return array The cases. + */ + public static function data_permanent_delete_matrix(): array { + return array( + 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', 'deleted' ), + 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', 'deleted' ), + 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', 'deleted' ), + 'administrator, own, future' => array( 'administrator', 'administrator', 'future', 'deleted' ), + 'administrator, own, private' => array( 'administrator', 'administrator', 'private', 'deleted' ), + 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', 'deleted' ), + 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', 'deleted' ), + 'administrator, other, draft' => array( 'administrator', 'other', 'draft', 'deleted' ), + 'administrator, other, pending' => array( 'administrator', 'other', 'pending', 'deleted' ), + 'administrator, other, publish' => array( 'administrator', 'other', 'publish', 'deleted' ), + 'administrator, other, future' => array( 'administrator', 'other', 'future', 'deleted' ), + 'administrator, other, private' => array( 'administrator', 'other', 'private', 'deleted' ), + 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', 'deleted' ), + 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', 'deleted' ), + 'editor, own, draft' => array( 'editor', 'editor', 'draft', 'deleted' ), + 'editor, own, pending' => array( 'editor', 'editor', 'pending', 'deleted' ), + 'editor, own, publish' => array( 'editor', 'editor', 'publish', 'deleted' ), + 'editor, own, future' => array( 'editor', 'editor', 'future', 'deleted' ), + 'editor, own, private' => array( 'editor', 'editor', 'private', 'deleted' ), + 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', 'deleted' ), + 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', 'deleted' ), + 'editor, other, draft' => array( 'editor', 'other', 'draft', 'deleted' ), + 'editor, other, pending' => array( 'editor', 'other', 'pending', 'deleted' ), + 'editor, other, publish' => array( 'editor', 'other', 'publish', 'deleted' ), + 'editor, other, future' => array( 'editor', 'other', 'future', 'deleted' ), + 'editor, other, private' => array( 'editor', 'other', 'private', 'deleted' ), + 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', 'deleted' ), + 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', 'deleted' ), + 'author, own, draft' => array( 'author', 'author', 'draft', 'deleted' ), + 'author, own, pending' => array( 'author', 'author', 'pending', 'deleted' ), + 'author, own, publish' => array( 'author', 'author', 'publish', 'deleted' ), + 'author, own, future' => array( 'author', 'author', 'future', 'deleted' ), + 'author, own, private' => array( 'author', 'author', 'private', 'deleted' ), + 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', 'deleted' ), + 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', 'deleted' ), + 'author, other, draft' => array( 'author', 'other', 'draft', 'forbidden' ), + 'author, other, pending' => array( 'author', 'other', 'pending', 'forbidden' ), + 'author, other, publish' => array( 'author', 'other', 'publish', 'forbidden' ), + 'author, other, future' => array( 'author', 'other', 'future', 'forbidden' ), + 'author, other, private' => array( 'author', 'other', 'private', 'forbidden' ), + 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', 'forbidden' ), + 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', 'forbidden' ), + 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', 'deleted' ), + 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', 'deleted' ), + 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', 'forbidden' ), + 'contributor, own, future' => array( 'contributor', 'contributor', 'future', 'forbidden' ), + 'contributor, own, private' => array( 'contributor', 'contributor', 'private', 'deleted' ), + 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', 'deleted' ), + 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', 'forbidden' ), + 'contributor, other, draft' => array( 'contributor', 'other', 'draft', 'forbidden' ), + 'contributor, other, pending' => array( 'contributor', 'other', 'pending', 'forbidden' ), + 'contributor, other, publish' => array( 'contributor', 'other', 'publish', 'forbidden' ), + 'contributor, other, future' => array( 'contributor', 'other', 'future', 'forbidden' ), + 'contributor, other, private' => array( 'contributor', 'other', 'private', 'forbidden' ), + 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', 'forbidden' ), + 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', 'forbidden' ), + 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', 'forbidden' ), + 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', 'forbidden' ), + 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', 'forbidden' ), + 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', 'forbidden' ), + 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', 'forbidden' ), + 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', 'forbidden' ), + 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', 'forbidden' ), + 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', 'forbidden' ), + 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', 'forbidden' ), + 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', 'forbidden' ), + 'subscriber, other, future' => array( 'subscriber', 'other', 'future', 'forbidden' ), + 'subscriber, other, private' => array( 'subscriber', 'other', 'private', 'forbidden' ), + 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', 'forbidden' ), + 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', 'forbidden' ), + ); + } + + /** + * Each role may delete permanently the posts it may delete, including posts in the Trash, and + * gets the 403 error for the others, whatever their status. + * + * Covers S4, S5, S12 and S13. + * + * @dataProvider data_permanent_delete_matrix + * + * @since 7.3.0 + * + * @param string $role The role to delete as. + * @param string $owner The role of the author of the post, or `other`. + * @param string $status The matrix status of the post. + * @param string $expected `deleted` or `forbidden`. + */ + public function test_permanent_delete_matrix( string $role, string $owner, string $status, string $expected ): void { + $post_id = self::$post_ids[ "{$owner}:{$status}" ]; + + $this->login_as( $role ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $this->delete_input( $post_id, true ) ); + + if ( 'deleted' === $expected ) { + $this->assert_deleted( $result, $post_id, 'Deleting a post the role may delete' ); + return; + } + + $this->assert_cannot_delete( $result, 'Deleting a post the role may not delete' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * `permanent` set to false moves the post to the Trash. + * + * Covers S12. + * + * @since 7.3.0 + */ + public function test_permanent_false_trashes_the_post(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $this->assert_trashed( $this->run_ability( $this->delete_input( $post_id, false ) ), $post_id, 'Deleting with permanent false' ); + } + + /** + * Returns `permanent` values that are not boolean true. + * + * @return array The values. + */ + public static function data_permanent_values_that_are_not_true(): array { + return array( + 'string true' => array( 'true' ), + 'one' => array( 1 ), + 'string one' => array( '1' ), + 'string yes' => array( 'yes' ), + 'false' => array( false ), + 'string false' => array( 'false' ), + 'list' => array( array( true ) ), + 'whole float' => array( 1.0 ), + 'upper case' => array( 'TRUE' ), + ); + } + + /** + * Called directly, `permanent` counts only when it is boolean true: anything else moves the + * post to the Trash. + * + * Covers S12. + * + * @dataProvider data_permanent_values_that_are_not_true + * + * @since 7.3.0 + * + * @param mixed $permanent The value. + */ + public function test_direct_call_deletes_permanently_only_for_boolean_true( $permanent ): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'permanent' => $permanent, + ) + ); + + $this->assert_trashed( $result, $post_id, 'A direct call with a permanent value that is not true' ); + } + + /** + * Called directly, `permanent` set to boolean true deletes the post permanently. + * + * Covers S12. + * + * @since 7.3.0 + */ + public function test_direct_call_deletes_permanently_for_boolean_true(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $this->assert_deleted( $execute( $this->delete_input( $post_id, true ) ), $post_id, 'A direct call with permanent true' ); + } + + /** + * Returns the special pages of the site. + * + * @return array The pages, as fixture names. + */ + public static function data_special_pages(): array { + return array( + 'front page' => array( 'front_page' ), + 'posts page' => array( 'posts_page' ), + 'privacy policy page' => array( 'privacy_page' ), + ); + } + + /** + * Sets the special pages of the site. + * + * @since 7.3.0 + */ + private function set_special_pages(): void { + update_option( 'show_on_front', 'page' ); + update_option( 'page_on_front', self::$post_ids['front_page'] ); + update_option( 'page_for_posts', self::$post_ids['posts_page'] ); + update_option( 'wp_page_for_privacy_policy', self::$post_ids['privacy_page'] ); + } + + /** + * An editor may delete neither the front page, the posts page nor the privacy policy page. + * + * Covers S4, S5 and S13. + * + * @dataProvider data_special_pages + * + * @since 7.3.0 + * + * @param string $page The page, as a fixture name. + */ + public function test_editor_cannot_delete_a_special_page( string $page ): void { + $this->set_special_pages(); + $post_id = self::$post_ids[ $page ]; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $trash = $this->run_ability( $this->delete_input( $post_id ) ); + $permanent = $this->run_ability( $this->delete_input( $post_id, true ) ); + + $this->assert_cannot_delete( $trash, 'An editor trashing a special page' ); + $this->assert_cannot_delete( $permanent, 'An editor deleting a special page' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * On a single site, an administrator may delete the special pages. + * + * @group ms-excluded + * + * @dataProvider data_special_pages + * + * @since 7.3.0 + * + * @param string $page The page, as a fixture name. + */ + public function test_administrator_can_delete_a_special_page( string $page ): void { + $this->set_special_pages(); + $post_id = self::$post_ids[ $page ]; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $this->assert_trashed( $this->run_ability( $this->delete_input( $post_id ) ), $post_id, 'An administrator trashing a special page' ); + $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'An administrator deleting a special page' ); + } + + /** + * Returns the special pages of the site and whether a site administrator may delete them on + * multisite. + * + * @return array The cases. + */ + public static function data_special_pages_for_a_site_administrator(): array { + return array( + 'front page' => array( 'front_page', true ), + 'posts page' => array( 'posts_page', true ), + 'privacy policy page' => array( 'privacy_page', false ), + ); + } + + /** + * On multisite, a site administrator may delete the front page and the posts page, but not + * the privacy policy page. + * + * Covers S4 and S5. + * + * @group ms-required + * + * @dataProvider data_special_pages_for_a_site_administrator + * + * @since 7.3.0 + * + * @param string $page The page, as a fixture name. + * @param bool $allowed Whether the site administrator may delete it. + */ + public function test_site_administrator_deletes_special_pages_on_multisite( string $page, bool $allowed ): void { + $this->set_special_pages(); + $post_id = self::$post_ids[ $page ]; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $this->delete_input( $post_id ) ); + + if ( $allowed ) { + $this->assert_trashed( $result, $post_id, 'A site administrator trashing a special page' ); + return; + } + + $this->assert_cannot_delete( $result, 'A site administrator trashing the privacy policy page' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * On multisite, a super admin may delete the special pages. + * + * @group ms-required + * + * @dataProvider data_special_pages + * + * @since 7.3.0 + * + * @param string $page The page, as a fixture name. + */ + public function test_super_admin_can_delete_a_special_page_on_multisite( string $page ): void { + $this->set_special_pages(); + $post_id = self::$post_ids[ $page ]; + + $super_admin = self::factory()->user->create( array( 'role' => 'administrator' ) ); + grant_super_admin( $super_admin ); + wp_set_current_user( $super_admin ); + + $this->register_abilities(); + + $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'A super admin deleting a special page' ); + } + + /** + * Returns values for the pre-trash and pre-delete filters that stop the operation. + * + * @return array The cases. + */ + public static function data_operations_that_are_stopped(): array { + return array( + 'trash, false' => array( false, 'pre_trash_post', false ), + 'trash, truthy without trashing' => array( false, 'pre_trash_post', true ), + 'delete, false' => array( true, 'pre_delete_post', false ), + 'delete, truthy without deleting' => array( true, 'pre_delete_post', true ), + ); + } + + /** + * When the trash or delete call fails or does not act, the result is the 500 error and the + * post is left as it was. + * + * Covers S5 and S12. + * + * @dataProvider data_operations_that_are_stopped + * + * @since 7.3.0 + * + * @param bool $permanent Whether the delete is permanent. + * @param string $filter The filter that stops the operation. + * @param mixed $value The value the filter returns. + */ + public function test_operation_that_does_not_act_is_an_error( bool $permanent, string $filter, $value ): void { + $post_id = self::$post_ids['admin_post']; + + add_filter( + $filter, + static function () use ( $value ) { + return $value; + } + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $this->delete_input( $post_id, $permanent ) ); + + $this->assert_error( $result, 'content_cannot_delete', 500, 'An operation that does not act' ); + $this->assertSame( 'The post cannot be deleted.', $result->get_error_message(), 'The message should be the failure message.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns keys that are not part of the input, and where they are sent. + * + * @return array The cases. + */ + public static function data_keys_outside_the_allow_list(): array { + return array( + 'force' => array( null, 'force', true ), + 'ID' => array( null, 'ID', 1 ), + 'where status' => array( 'where', 'status', 'trash' ), + 'where ID' => array( 'where', 'ID', 1 ), + ); + } + + /** + * A key outside the allow-list fails input validation. + * + * Covers S1 and S5. + * + * @dataProvider data_keys_outside_the_allow_list + * + * @since 7.3.0 + * + * @param string|null $location Where the key is sent, or null for the top level. + * @param string $key The key. + * @param mixed $value The value. + */ + public function test_key_outside_the_allow_list_fails_input_validation( ?string $location, string $key, $value ): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $input = $this->delete_input( $post_id ); + if ( null === $location ) { + $input[ $key ] = $value; + } else { + $input[ $location ][ $key ] = $value; + } + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the key.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, keys outside the allow-list are ignored: they neither make the delete + * permanent nor reach another post. + * + * Covers S6 and S12. + * + * @since 7.3.0 + */ + public function test_direct_call_ignores_keys_outside_the_allow_list(): void { + $post_id = self::$post_ids['admin_post']; + $other_id = self::$post_ids['administrator:publish']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $other_before = $this->snapshot( $other_id )['post']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + 'ID' => $other_id, + ), + 'force' => true, + 'ID' => $other_id, + ) + ); + + $this->assert_trashed( $result, $post_id, 'A direct call with unknown keys' ); + $this->assertSame( $other_before, $this->snapshot( $other_id )['post'], 'The post named by ID should not change.' ); + } + + /** + * The current user is checked at each call, also on the same instance. + * + * Covers S4. + * + * @since 7.3.0 + */ + public function test_each_call_checks_the_current_user(): void { + $own_id = self::$post_ids['author:draft']; + $other_id = self::$post_ids['author:pending']; + $execute = $this->get_ability_callbacks()['execute_callback']; + + $this->login_as( 'author' ); + $first = $execute( $this->delete_input( $own_id ) ); + + $this->login_as( 'contributor' ); + $before = $this->snapshot( $other_id ); + $second = $execute( $this->delete_input( $other_id ) ); + + $this->assert_trashed( $first, $own_id, 'The first call as the author' ); + $this->assert_cannot_delete( $second, 'The second call as a contributor' ); + $this->assertSame( $before, $this->snapshot( $other_id ), 'The second call should write nothing.' ); + } + + /** + * Returns the forms of a whole number that are read as the integer they equal. + * + * @return array The forms. + */ + public static function data_whole_number_forms(): array { + return array( + 'whole float' => array( 'float' ), + 'string with .0' => array( 'decimal' ), + 'string with plus' => array( 'plus' ), + 'string with leading space' => array( 'leading space' ), + 'string with trailing space' => array( 'trailing space' ), + 'string with leading line' => array( 'leading newline' ), + 'string with an exponent' => array( 'exponent' ), + ); + } + + /** + * Called directly, an ID in another whole-number form is read as the integer it equals. + * + * Covers S2 (C3). + * + * @dataProvider data_whole_number_forms + * + * @since 7.3.0 + * + * @param string $form The form of the ID. + */ + public function test_direct_call_reads_an_id_in_a_whole_number_form( string $form ): void { + $post_id = self::$post_ids['admin_post']; + $forms = array( + 'float' => (float) $post_id, + 'decimal' => "{$post_id}.0", + 'plus' => "+{$post_id}", + 'leading space' => " {$post_id}", + 'trailing space' => "{$post_id} ", + 'leading newline' => "\n{$post_id}", + 'exponent' => "{$post_id}e0", + ); + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $forms[ $form ], + ), + ) + ); + + $this->assert_trashed( $result, $post_id, 'A direct call with an ID in a whole-number form' ); + } +} diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php new file mode 100644 index 0000000000000..93a959a71a938 --- /dev/null +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -0,0 +1,3954 @@ + + */ + private const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); + + /** + * An ID that no post or user has. + * + * @since 7.3.0 + * + * @var int + */ + private const MISSING_ID = 987654321; + + /** + * The roles of the matrix tests. + * + * @since 7.3.0 + * + * @var list + */ + private const ROLES = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); + + /** + * The statuses of the matrix posts. `trash_` is a post trashed from ``. + * + * @since 7.3.0 + * + * @var list + */ + private const MATRIX_STATUSES = array( 'draft', 'pending', 'publish', 'future', 'private', 'trash_draft', 'trash_publish' ); + + /** + * Shared user IDs keyed by role or fixture name. + * + * @since 7.3.0 + * + * @var array + */ + private static $user_ids = array(); + + /** + * Shared post IDs keyed by fixture name. + * + * @since 7.3.0 + * + * @var array + */ + private static $post_ids = array(); + + /** + * Creates shared users and posts. + * + * @since 7.3.0 + * + * @param WP_UnitTest_Factory $factory The unit test factory. + */ + public static function wpSetUpBeforeClass( $factory ): void { + foreach ( self::ROLES as $role ) { + self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); + } + self::$user_ids['other'] = $factory->user->create( array( 'role' => 'editor' ) ); + self::$user_ids['page_writer'] = $factory->user->create( array( 'role' => 'subscriber' ) ); + self::$user_ids['publisher'] = $factory->user->create( array( 'role' => 'contributor' ) ); + + // A user who may edit their own draft pages but not read the drafts of other users. + get_userdata( self::$user_ids['page_writer'] )->add_cap( 'edit_pages' ); + + // A user who may publish posts but not edit published posts. + get_userdata( self::$user_ids['publisher'] )->add_cap( 'publish_posts' ); + + // One post per owner and status, for the matrix tests. + foreach ( array_merge( self::ROLES, array( 'other' ) ) as $owner ) { + foreach ( self::MATRIX_STATUSES as $status ) { + $stored_status = str_replace( 'trash_', '', $status ); + $post_id = $factory->post->create( + array( + 'post_author' => self::$user_ids[ $owner ], + 'post_status' => $stored_status, + 'post_title' => "Matrix {$owner} {$status}", + 'post_date' => 'future' === $stored_status ? '2035-01-01 00:00:00' : '', + ) + ); + if ( $stored_status !== $status ) { + wp_trash_post( $post_id ); + } + self::$post_ids[ "{$owner}:{$status}" ] = $post_id; + } + } + + self::$post_ids['admin_post'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_title' => 'Launch notes', + 'post_name' => 'shared-slug', + ) + ); + + self::$post_ids['admin_page'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + + self::$post_ids['editor_draft_page'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_type' => 'page', + 'post_status' => 'draft', + ) + ); + + self::$post_ids['writer_page'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['page_writer'], + 'post_type' => 'page', + 'post_status' => 'draft', + 'post_title' => 'Writer page', + ) + ); + + self::$post_ids['writer_child'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_parent' => self::$post_ids['writer_page'], + ) + ); + + self::$post_ids['writer_grandchild'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_parent' => self::$post_ids['writer_child'], + ) + ); + + // A page of the writer whose stored parent the writer may not read. + self::$post_ids['writer_page_with_hidden_parent'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['page_writer'], + 'post_type' => 'page', + 'post_status' => 'draft', + 'post_title' => 'Hidden parent', + 'post_parent' => self::$post_ids['editor_draft_page'], + ) + ); + + self::$post_ids['attachment'] = $factory->attachment->create_object( + 'image.jpg', + self::$post_ids['admin_post'], + array( + 'post_mime_type' => 'image/jpeg', + 'post_author' => self::$user_ids['administrator'], + ) + ); + + $revisioned_id = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_content' => 'First version.', + ) + ); + wp_update_post( + array( + 'ID' => $revisioned_id, + 'post_content' => 'Second version.', + ) + ); + $revisions = wp_get_post_revisions( $revisioned_id ); + self::$post_ids['revision'] = (int) array_key_first( $revisions ); + self::$post_ids['autosave'] = (int) _wp_put_post_revision( get_post( $revisioned_id ), true ); + + // Posts of core types that are not exposed. + foreach ( array( + 'reusable_block' => 'wp_block', + 'template' => 'wp_template', + 'menu_item' => 'nav_menu_item', + ) as $fixture => $post_type ) { + self::$post_ids[ $fixture ] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => $post_type, + 'post_status' => 'publish', + ) + ); + } + + // An author whose role grants publish_posts, denied it for this user. + self::$user_ids['restricted_author'] = $factory->user->create( array( 'role' => 'author' ) ); + get_userdata( self::$user_ids['restricted_author'] )->add_cap( 'publish_posts', false ); + + self::$post_ids['restricted_author:draft'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['restricted_author'], + 'post_status' => 'draft', + ) + ); + } + + /** + * Unregisters the content abilities and registers the content category. + * + * @since 7.3.0 + */ + public function set_up(): void { + parent::set_up(); + + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( ! wp_has_ability_category( 'content' ) ) { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + wp_register_ability_category( + 'content', + array( + 'label' => 'Content', + 'description' => 'Content.', + ) + ); + } + } + + /** + * Unregisters the content abilities and the category, and resets the post types. + * + * @since 7.3.0 + */ + public function tear_down(): void { + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( wp_has_ability_category( 'content' ) ) { + wp_unregister_ability_category( 'content' ); + } + + /* + * A test may turn off `show_in_abilities` on `post` and `page`, and the next class's + * wpSetUpBeforeClass() runs before the next set_up() resets the post types. + */ + $this->reset_post_types(); + + parent::tear_down(); + } + + /** + * Registers the content abilities inside a faked init action. + * + * @since 7.3.0 + */ + private function register_abilities(): void { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + ( new WP_Abilities_Content() )->register(); + } + + /** + * Registers the content abilities and returns the callbacks of the update ability. + * + * The callbacks call private methods, so tests that skip input validation or the + * permission check capture them from the registration arguments. + * + * @since 7.3.0 + * + * @return array The permission and execute callbacks, keyed by argument name. + */ + private function get_ability_callbacks(): array { + $callbacks = array(); + + add_filter( + 'wp_register_ability_args', + static function ( array $args, string $name ) use ( &$callbacks ): array { + if ( self::ABILITY === $name ) { + $callbacks = array( + 'permission_callback' => $args['permission_callback'], + 'execute_callback' => $args['execute_callback'], + ); + } + + return $args; + }, + 10, + 2 + ); + + $this->register_abilities(); + + $this->assertArrayHasKey( 'execute_callback', $callbacks, 'The core/content-update ability should be registered with callbacks.' ); + + return $callbacks; + } + + /** + * Runs the registered update ability with the given input. + * + * @since 7.3.0 + * + * @param mixed $input The ability input. + * @return mixed The ability result. + */ + private function run_ability( $input ) { + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The core/content-update ability should be registered.' ); + + return wp_get_ability( self::ABILITY )->execute( $input ); + } + + /** + * Runs the update ability on a post with the given data. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @param array $data The data. + * @param list|null $fields The fields, or null for the default. + * @return mixed The ability result. + */ + private function update_post( int $post_id, array $data, ?array $fields = null ) { + $input = array( + 'where' => array( + 'type' => get_post_type( $post_id ), + 'id' => $post_id, + ), + 'data' => $data, + ); + if ( null !== $fields ) { + $input['fields'] = $fields; + } + + return $this->run_ability( $input ); + } + + /** + * Logs in as the shared user with the given role or fixture name. + * + * @since 7.3.0 + * + * @param string $role The role or fixture name. + */ + private function login_as( string $role ): void { + wp_set_current_user( self::$user_ids[ $role ] ); + } + + /** + * Takes a snapshot of the stored content: the number of posts per type and status, + * revisions included, the number of meta rows, and the row and meta of one post. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @return array The snapshot. + */ + private function snapshot( int $post_id ): array { + global $wpdb; + + return array( + 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), + 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), + 'post' => $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id ), ARRAY_A ), + 'post_meta' => $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d ORDER BY meta_id", $post_id ), ARRAY_A ), + ); + } + + /** + * Asserts that a result is an error with the given code and status. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $code The expected error code. + * @param int $status The expected error status. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_error( $result, string $code, int $status, string $context ): void { + $this->assertWPError( $result, "{$context}: the call should fail." ); + $this->assertSame( $code, $result->get_error_code(), "{$context}: the error code should be {$code}." ); + + $data = $result->get_error_data(); + $this->assertIsArray( $data, "{$context}: the error should carry data." ); + $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); + } + + /** + * Asserts that a result is the invalid attribute error for the given attribute. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $attribute The attribute name. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_invalid_attribute( $result, string $attribute, string $context ): void { + $this->assert_error( $result, 'content_invalid_attribute', 400, $context ); + $this->assertSame( "Invalid attribute: {$attribute}.", $result->get_error_message(), "{$context}: the message should name the attribute." ); + $this->assertSame( array( "data[{$attribute}]" ), array_keys( $result->get_error_data()['params'] ?? array() ), "{$context}: the params should hold only the input path of the attribute." ); + } + + /** + * Asserts that a result is the not-found error. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $context What the call was, for the assertion messages. + */ + private function assert_not_found( $result, string $context ): void { + $this->assert_error( $result, 'content_not_found', 404, $context ); + $this->assertSame( 'The requested content was not found.', $result->get_error_message(), "{$context}: the message should be the not-found message." ); + } + + /** + * Asserts that a result is the post item of a successful update and returns the stored post. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param int $post_id The ID of the updated post. + * @return WP_Post The stored post. + */ + private function assert_updated( $result, int $post_id ): WP_Post { + $this->assertIsArray( $result, 'The update should succeed.' ); + $this->assertSame( $post_id, $result['id'] ?? null, 'The updated post should be returned with its ID.' ); + + clean_post_cache( $post_id ); + + return get_post( $post_id ); + } + + /** + * Removes the `description` keywords of a schema at every depth. + * + * @since 7.3.0 + * + * @param array $schema The schema. + * @return array The schema without descriptions. + */ + private function strip_descriptions( array $schema ): array { + unset( $schema['description'] ); + foreach ( $schema as $key => $value ) { + if ( is_array( $value ) ) { + $schema[ $key ] = $this->strip_descriptions( $value ); + } + } + + return $schema; + } + + /** + * Creates a draft with a floating date, stored in the site's timezone with no GMT date. + * + * @since 7.3.0 + * + * @param string $local The local date, as `Y-m-d H:i:s`. + * @return int The post ID. + */ + private function create_floating_draft( string $local ): int { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'draft', + 'post_title' => 'Floating', + 'post_date' => $local, + ) + ); + + $this->assertSame( '0000-00-00 00:00:00', get_post( $post_id )->post_date_gmt, 'Precondition: the draft should have a floating date.' ); + + return $post_id; + } + + /** + * Asserts that a stored GMT date is the current time, give or take a minute. + * + * @since 7.3.0 + * + * @param string $gmt The stored GMT date. + * @param string $context What the date is, for the assertion message. + */ + private function assert_is_now( string $gmt, string $context ): void { + $this->assertEqualsWithDelta( time(), strtotime( $gmt . ' UTC' ), 60, "{$context} should be the current time." ); + } + + /** + * The ability is registered in the content category with its label and annotations. + * + * @since 7.3.0 + */ + public function test_registers_the_ability(): void { + $this->register_abilities(); + + $ability = wp_get_ability( self::ABILITY ); + + $this->assertInstanceOf( WP_Ability::class, $ability, 'The core/content-update ability should be registered.' ); + $this->assertSame( 'Update Content', $ability->get_label(), 'The ability should have its label.' ); + $this->assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); + $this->assertTrue( $ability->get_meta_item( 'public', false ), 'The ability should be marked public.' ); + + $annotations = $ability->get_meta_item( 'annotations', array() ); + $this->assertFalse( $annotations['readonly'], 'The ability should not be marked read-only.' ); + $this->assertFalse( $annotations['destructive'], 'The ability should not be marked destructive.' ); + $this->assertTrue( $annotations['idempotent'], 'The ability should be marked idempotent.' ); + } + + /** + * The ability is not registered when no post type is exposed to abilities. + * + * @since 7.3.0 + */ + public function test_is_not_registered_without_exposed_post_types(): void { + foreach ( array( 'post', 'page' ) as $post_type ) { + get_post_type_object( $post_type )->show_in_abilities = false; + } + + $this->register_abilities(); + + $this->assertFalse( wp_has_ability( self::ABILITY ), 'The update ability should not be registered without an exposed post type.' ); + } + + /** + * The input schema takes `where`, with the post type and ID, `data`, with at least one + * attribute, and `fields`. + * + * @since 7.3.0 + */ + public function test_input_schema(): void { + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The update ability should be registered.' ); + + $schema = wp_get_ability( self::ABILITY )->get_input_schema(); + + $this->assertSame( 'object', $schema['type'], 'The input should be an object.' ); + $this->assertFalse( $schema['additionalProperties'], 'The input should reject unknown keys.' ); + $this->assertSame( array( 'where', 'data' ), $schema['required'], 'The input should require where and data.' ); + $this->assertSame( array( 'where', 'data', 'fields' ), array_keys( $schema['properties'] ), 'The input should take where, data and fields.' ); + + $where = $schema['properties']['where']; + + $this->assertSame( 'object', $where['type'], 'The where should be an object.' ); + $this->assertFalse( $where['additionalProperties'], 'The where should reject unknown keys.' ); + $this->assertSame( array( 'type', 'id' ), $where['required'], 'The where should require the type and the ID.' ); + $this->assertSame( array( 'type', 'id' ), array_keys( $where['properties'] ), 'The where should take the type and the ID.' ); + $this->assertSame( 'string', $where['properties']['type']['type'], 'The type should be a string.' ); + $this->assertSameSets( array( 'post', 'page' ), $where['properties']['type']['enum'], 'The type should list the exposed post types.' ); + $this->assertSame( 'integer', $where['properties']['id']['type'], 'The ID should be an integer.' ); + $this->assertSame( 1, $where['properties']['id']['minimum'], 'The ID should be at least 1.' ); + + $data = $schema['properties']['data']; + + $this->assertSame( 'object', $data['type'], 'The data should be an object.' ); + $this->assertFalse( $data['additionalProperties'], 'The data should reject unknown keys.' ); + $this->assertSame( 1, $data['minProperties'], 'The data should hold at least one attribute.' ); + $this->assertArrayNotHasKey( 'required', $data, 'The data should require no attribute.' ); + $this->assertSame( array( 'title', 'content', 'excerpt', 'status', 'slug', 'date', 'author', 'parent' ), array_keys( $data['properties'] ), 'The data should take the allowed attributes and no post type.' ); + $this->assertSame( array( 'publish', 'future', 'draft', 'pending', 'private' ), $data['properties']['status']['enum'], 'The status should list the statuses that are not internal.' ); + $this->assertSame( 1, $data['properties']['slug']['minLength'], 'The slug should not be empty.' ); + $this->assertSame( 1, $data['properties']['author']['minimum'], 'The author should be at least 1.' ); + $this->assertSame( 0, $data['properties']['parent']['minimum'], 'The parent should be at least 0.' ); + $this->assertSame( 1, $data['properties']['date']['minProperties'], 'The date should hold at least one value.' ); + $this->assertSame( array( 'site', 'gmt' ), array_keys( $data['properties']['date']['properties'] ), 'The date should take a site and a GMT value.' ); + + $create_schema = wp_get_ability( 'core/content-create' )->get_input_schema(); + $create_data = $create_schema['properties']['data']['properties']; + unset( $create_data['type'] ); + + $this->assertSame( $this->strip_descriptions( $create_data ), $this->strip_descriptions( $data['properties'] ), 'The attributes should have the same schemas as on create.' ); + + $query_schema = wp_get_ability( 'core/content-query' )->get_input_schema(); + + $this->assertSame( + $this->strip_descriptions( $query_schema['properties']['fields'] ), + $this->strip_descriptions( $schema['properties']['fields'] ), + 'The fields should be the same list as the fields of the query.' + ); + } + + /** + * The output is one post item, the same object as an item of the query. + * + * Covers S11. + * + * @since 7.3.0 + */ + public function test_output_schema_is_a_query_item(): void { + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The update ability should be registered.' ); + + $schema = wp_get_ability( self::ABILITY )->get_output_schema(); + $item = wp_get_ability( 'core/content-query' )->get_output_schema()['properties']['items']['items']; + + $this->assertSame( array( 'id' ), $schema['required'], 'The output should always hold the ID.' ); + $this->assertSame( $this->strip_descriptions( $item ), $this->strip_descriptions( $schema ), 'The output should be the same object as a query item.' ); + } + + /** + * Example 1: a new title and excerpt. + * + * @since 7.3.0 + */ + public function test_example_updates_the_title_and_excerpt(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'draft', + 'post_title' => 'Launch notes', + ) + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'title' => 'Launch notes, updated', + 'excerpt' => 'What shipped and what is next.', + ), + array( 'modified', 'title', 'excerpt' ) + ); + + $this->assert_updated( $result, $post_id ); + + $this->assertSame( array( 'id', 'modified', 'title', 'excerpt' ), array_keys( $result ), 'The ID and the chosen fields should be returned.' ); + $this->assertSame( array( 'site', 'gmt' ), array_keys( $result['modified'] ), 'The modified date should be given in site time and GMT.' ); + $this->assertNotFalse( rest_parse_date( $result['modified']['gmt'] ), 'The modified GMT date should be a date-time.' ); + $this->assertSame( + array( + 'raw' => 'Launch notes, updated', + 'rendered' => 'Launch notes, updated', + ), + $result['title'], + 'The new title should be returned raw and rendered.' + ); + $this->assertSame( + array( + 'raw' => 'What shipped and what is next.', + 'rendered' => "

What shipped and what is next.

\n", + 'protected' => false, + ), + $result['excerpt'], + 'The new excerpt should be returned raw, rendered and unprotected.' + ); + } + + /** + * Example 2: publishing the draft. + * + * @since 7.3.0 + */ + public function test_example_publishes_the_draft(): void { + $this->set_permalink_structure( '/%postname%/' ); + + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'draft', + 'post_title' => 'Launch notes, updated', + ) + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'status' => 'publish' ), array( 'status', 'slug', 'link' ) ); + + $this->assertSame( + array( + 'id' => $post_id, + 'status' => 'publish', + 'slug' => 'launch-notes-updated', + 'link' => home_url( '/launch-notes-updated/' ), + ), + $result, + 'The published post should be returned in the chosen fields.' + ); + } + + /** + * Example 3: a post the user cannot edit. + * + * Covers S4, S5 and S13. + * + * @since 7.3.0 + */ + public function test_example_rejects_a_post_the_user_cannot_edit(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'title' => 'Launch notes' ) ); + + $this->assertWPError( $result, 'Updating a post of another user should fail.' ); + $this->assertSame( + array( + 'code' => 'content_cannot_edit', + 'message' => 'Sorry, you are not allowed to edit this post.', + 'data' => array( 'status' => 403 ), + ), + array( + 'code' => $result->get_error_code(), + 'message' => $result->get_error_message(), + 'data' => $result->get_error_data(), + ), + 'The error should be the edit error of the example.' + ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A logged-out user is refused before the ability runs. + * + * Covers S4 and S5. + * + * @since 7.3.0 + */ + public function test_permission_callback_refuses_a_logged_out_user(): void { + $post_id = self::$post_ids['admin_post']; + + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'title' => 'Refused' ) ); + + $this->assertWPError( $result, 'The call should be refused.' ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a logged-out user.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns inputs for which the permission callback returns false, even for an administrator. + * + * @return array The inputs. + */ + public static function data_inputs_refused_by_the_permission_callback(): array { + return array( + 'no input' => array( null ), + 'no where' => array( array( 'data' => array( 'title' => 'x' ) ) ), + 'where that is a string' => array( + array( + 'where' => 'post', + 'data' => array( 'title' => 'x' ), + ), + ), + 'type that is a list' => array( + array( + 'where' => array( + 'type' => array( 'post' ), + 'id' => 1, + ), + 'data' => array( 'title' => 'x' ), + ), + ), + 'unexposed type' => array( + array( + 'where' => array( + 'type' => 'attachment', + 'id' => 1, + ), + 'data' => array( 'title' => 'x' ), + ), + ), + ); + } + + /** + * The permission callback returns false for missing or malformed input. + * + * @dataProvider data_inputs_refused_by_the_permission_callback + * + * @since 7.3.0 + * + * @param mixed $input The input. + */ + public function test_permission_callback_returns_false_for_malformed_input( $input ): void { + $this->login_as( 'administrator' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertFalse( $permission( $input ), 'The permission callback should return false.' ); + } + + /** + * The permission callback is only a coarse gate: it lets a logged-in subscriber through, and + * the execute callback decides on the post. + * + * @since 7.3.0 + */ + public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type(): void { + $this->login_as( 'subscriber' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertTrue( + $permission( + array( + 'where' => array( + 'type' => 'post', + 'id' => self::$post_ids['admin_post'], + ), + 'data' => array( 'title' => 'x' ), + ) + ), + 'The permission callback should return true.' + ); + } + + /** + * Returns ID values that are not a whole number of at least 1. + * + * @return array The IDs. + */ + public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): array { + return array( + 'digits and letters' => array( '123abc' ), + 'fraction' => array( 1.5 ), + 'zero' => array( 0 ), + 'negative' => array( -1 ), + 'true' => array( true ), + 'null' => array( null ), + 'list' => array( array( 1 ) ), + 'empty string' => array( '' ), + ); + } + + /** + * An ID that is not a whole number of at least 1 fails input validation. + * + * Covers S2 and S5. + * + * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 + * + * @since 7.3.0 + * + * @param mixed $id The ID. + */ + public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( self::$post_ids['admin_post'] ); + $result = $this->run_ability( + array( + 'where' => array( + 'type' => 'post', + 'id' => $id, + ), + 'data' => array( 'title' => 'Invalid ID' ), + ) + ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the ID.' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); + } + + /** + * Returns ID values that a direct call does not read as a whole number of at least 1. + * + * @return array The IDs. + */ + public static function data_ids_that_are_not_whole_numbers_in_a_direct_call(): array { + return array_merge( + self::data_ids_that_are_not_whole_numbers_of_at_least_1(), + array( + 'hexadecimal string' => array( '0x1' ), + 'negative zero' => array( -0.0 ), + 'number above 2 ** 53' => array( 9007199254740993 ), + 'string above 2 ** 53' => array( '9007199254740993' ), + 'exponent above 2 ** 53' => array( '1e16' ), + ) + ); + } + + /** + * Called directly, an ID that is not a whole number of at least 1 is the not-found error and + * is never cast, even when it starts with the ID of an existing post. + * + * Covers S2, S5 and S13 (C3). + * + * @dataProvider data_ids_that_are_not_whole_numbers_in_a_direct_call + * + * @since 7.3.0 + * + * @param mixed $id The ID. + */ + public function test_direct_call_with_an_id_that_is_not_a_whole_number_is_not_found( $id ): void { + $post_id = self::$post_ids['admin_post']; + if ( '123abc' === $id ) { + $id = "{$post_id}abc"; + } elseif ( 1.5 === $id ) { + $id = $post_id + 0.5; + } elseif ( '0x1' === $id ) { + // The hexadecimal form of the ID of an existing post. + $id = '0x' . dechex( $post_id ); + } + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $id, + ), + 'data' => array( 'title' => 'Invalid ID' ), + ) + ); + + $this->assert_not_found( $result, 'A direct call with an ID that is not a whole number' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, an ID made of digits is read as that number. + * + * Covers S2. + * + * @since 7.3.0 + */ + public function test_direct_call_reads_an_id_made_of_digits(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => (string) $post_id, + ), + 'data' => array( 'title' => 'Digits' ), + ) + ); + + $this->assertSame( 'Digits', $this->assert_updated( $result, $post_id )->post_title, 'The post should be updated.' ); + } + + /** + * Returns posts that cannot be reached as a `post`. + * + * @return array The posts, as fixture names. + */ + public static function data_posts_that_are_not_posts(): array { + return array( + 'revision' => array( 'revision' ), + 'attachment' => array( 'attachment' ), + 'page' => array( 'admin_page' ), + 'missing' => array( 'missing' ), + 'reusable block' => array( 'reusable_block' ), + 'template' => array( 'template' ), + 'menu item' => array( 'menu_item' ), + 'autosave' => array( 'autosave' ), + ); + } + + /** + * A revision, an autosave, an attachment, a post of another exposed or non-exposed type and a + * missing post are not found as a `post`, even by an administrator. + * + * Covers S3, S5 and S13. + * + * @dataProvider data_posts_that_are_not_posts + * + * @since 7.3.0 + * + * @param string $post The post, as a fixture name. + */ + public function test_post_of_another_type_is_not_found( string $post ): void { + $post_id = self::$post_ids[ $post ] ?? self::MISSING_ID; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Wrong type' ), + ) + ); + + $this->assert_not_found( $result, 'A post that is not a post' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, a post of a type that is not exposed is not found, whatever its ID. + * + * Covers S3 and S13. + * + * @since 7.3.0 + */ + public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): void { + register_post_type( 'cw_hidden', array( 'public' => true ) ); + $post_id = self::factory()->post->create( array( 'post_type' => 'cw_hidden' ) ); + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'cw_hidden', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Hidden' ), + ) + ); + + $this->assert_not_found( $result, 'A direct call for a type that is not exposed' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A post type whose `show_in_abilities` is turned off after registration cannot be updated. + * + * Covers S3 and S5. + * + * @since 7.3.0 + */ + public function test_type_no_longer_exposed_cannot_be_updated(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $callbacks = $this->get_ability_callbacks(); + + get_post_type_object( 'post' )->show_in_abilities = false; + + $input = array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Hidden' ), + ); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, 'The regular call should fail.' ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a type that is no longer exposed.' ); + $this->assert_not_found( $callbacks['execute_callback']( $input ), 'A direct call for a type that is no longer exposed' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns direct-call inputs with a malformed `where`. + * + * @return array The inputs. + */ + public static function data_direct_inputs_with_a_malformed_where(): array { + return array( + 'no input' => array( null ), + 'no where' => array( array( 'data' => array( 'title' => 'x' ) ) ), + 'where that is a string' => array( + array( + 'where' => 'post', + 'data' => array( 'title' => 'x' ), + ), + ), + 'no type' => array( + array( + 'where' => array( 'id' => 1 ), + 'data' => array( 'title' => 'x' ), + ), + ), + 'no ID' => array( + array( + 'where' => array( 'type' => 'post' ), + 'data' => array( 'title' => 'x' ), + ), + ), + ); + } + + /** + * Called directly, a malformed `where` is the not-found error. + * + * Covers S1 and S13. + * + * @dataProvider data_direct_inputs_with_a_malformed_where + * + * @since 7.3.0 + * + * @param mixed $input The input. + */ + public function test_direct_call_with_a_malformed_where_is_not_found( $input ): void { + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( self::$post_ids['administrator:draft'] ); + + $this->assert_not_found( $execute( $input ), 'A direct call with a malformed where' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['administrator:draft'] ), 'Nothing should be written for a malformed where.' ); + } + + /** + * The lookup comes before everything else: a missing post with invalid data is not found. + * + * Covers O1. + * + * @since 7.3.0 + */ + public function test_lookup_comes_before_the_attribute_rules(): void { + $this->login_as( 'subscriber' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( self::$post_ids['administrator:draft'] ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => self::MISSING_ID, + ), + 'data' => array( + 'status' => 'trash', + 'parent' => 1, + 'title' => array( 'x' ), + ), + ) + ); + + $this->assert_not_found( $result, 'A missing post with invalid data' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['administrator:draft'] ), 'Nothing should be written for a missing post.' ); + } + + /** + * Returns whether each role may edit each matrix post. + * + * @return array The cases. + */ + public static function data_update_matrix(): array { + return array( + 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', true ), + 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', true ), + 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', true ), + 'administrator, own, future' => array( 'administrator', 'administrator', 'future', true ), + 'administrator, own, private' => array( 'administrator', 'administrator', 'private', true ), + 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', true ), + 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', true ), + 'administrator, other, draft' => array( 'administrator', 'other', 'draft', true ), + 'administrator, other, pending' => array( 'administrator', 'other', 'pending', true ), + 'administrator, other, publish' => array( 'administrator', 'other', 'publish', true ), + 'administrator, other, future' => array( 'administrator', 'other', 'future', true ), + 'administrator, other, private' => array( 'administrator', 'other', 'private', true ), + 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', true ), + 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', true ), + 'editor, own, draft' => array( 'editor', 'editor', 'draft', true ), + 'editor, own, pending' => array( 'editor', 'editor', 'pending', true ), + 'editor, own, publish' => array( 'editor', 'editor', 'publish', true ), + 'editor, own, future' => array( 'editor', 'editor', 'future', true ), + 'editor, own, private' => array( 'editor', 'editor', 'private', true ), + 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', true ), + 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', true ), + 'editor, other, draft' => array( 'editor', 'other', 'draft', true ), + 'editor, other, pending' => array( 'editor', 'other', 'pending', true ), + 'editor, other, publish' => array( 'editor', 'other', 'publish', true ), + 'editor, other, future' => array( 'editor', 'other', 'future', true ), + 'editor, other, private' => array( 'editor', 'other', 'private', true ), + 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', true ), + 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', true ), + 'author, own, draft' => array( 'author', 'author', 'draft', true ), + 'author, own, pending' => array( 'author', 'author', 'pending', true ), + 'author, own, publish' => array( 'author', 'author', 'publish', true ), + 'author, own, future' => array( 'author', 'author', 'future', true ), + 'author, own, private' => array( 'author', 'author', 'private', true ), + 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', true ), + 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', true ), + 'author, other, draft' => array( 'author', 'other', 'draft', false ), + 'author, other, pending' => array( 'author', 'other', 'pending', false ), + 'author, other, publish' => array( 'author', 'other', 'publish', false ), + 'author, other, future' => array( 'author', 'other', 'future', false ), + 'author, other, private' => array( 'author', 'other', 'private', false ), + 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', false ), + 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', false ), + 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', true ), + 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', true ), + 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', false ), + 'contributor, own, future' => array( 'contributor', 'contributor', 'future', false ), + 'contributor, own, private' => array( 'contributor', 'contributor', 'private', true ), + 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', true ), + 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', false ), + 'contributor, other, draft' => array( 'contributor', 'other', 'draft', false ), + 'contributor, other, pending' => array( 'contributor', 'other', 'pending', false ), + 'contributor, other, publish' => array( 'contributor', 'other', 'publish', false ), + 'contributor, other, future' => array( 'contributor', 'other', 'future', false ), + 'contributor, other, private' => array( 'contributor', 'other', 'private', false ), + 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', false ), + 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', false ), + 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', false ), + 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', false ), + 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', false ), + 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', false ), + 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', false ), + 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', false ), + 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', false ), + 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', false ), + 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', false ), + 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', false ), + 'subscriber, other, future' => array( 'subscriber', 'other', 'future', false ), + 'subscriber, other, private' => array( 'subscriber', 'other', 'private', false ), + 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', false ), + 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', false ), + ); + } + + /** + * Each role may update the posts it may edit, in any status, and gets the 403 error for the + * others, whatever their status. + * + * Covers S4, S5 and S13. + * + * @dataProvider data_update_matrix + * + * @since 7.3.0 + * + * @param string $role The role to update as. + * @param string $owner The role of the author of the post, or `other`. + * @param string $status The matrix status of the post. + * @param bool $allowed Whether the update is allowed. + */ + public function test_update_matrix( string $role, string $owner, string $status, bool $allowed ): void { + $post_id = self::$post_ids[ "{$owner}:{$status}" ]; + $stored_status = get_post_status( $post_id ); + + $this->login_as( $role ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'title' => 'Matrix update' ), array( 'status', 'title.rendered' ) ); + + if ( ! $allowed ) { + $this->assert_error( $result, 'content_cannot_edit', 403, 'Updating a post the role may not edit' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + return; + } + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'Matrix update', $post->post_title, 'The title should be stored.' ); + $this->assertSame( $stored_status, $post->post_status, 'The status should not change.' ); + $this->assertSame( $stored_status, $result['status'], 'The stored status should be returned.' ); + $this->assertSame( array( 'rendered' => 'Matrix update' ), $result['title'], 'The new title should be returned.' ); + } + + /** + * Returns `data` values that are invalid in some way. + * + * @return array}> The data. + */ + public static function data_invalid_data(): array { + return array( + 'parent on a post' => array( array( 'parent' => 1 ) ), + 'trash' => array( array( 'status' => 'trash' ) ), + 'publish' => array( array( 'status' => 'publish' ) ), + 'empty slug' => array( array( 'slug' => '!!!' ) ), + 'missing author' => array( array( 'author' => self::MISSING_ID ) ), + 'title that is a list' => array( array( 'title' => array( 'x' ) ) ), + 'invalid date' => array( array( 'date' => array( 'site' => 'not a date' ) ) ), + 'empty data' => array( array() ), + ); + } + + /** + * Called directly by a user who may not edit the post, the call fails on the capability before + * anything about the attributes is checked. + * + * Covers S4, S5 and O2. + * + * @dataProvider data_invalid_data + * + * @since 7.3.0 + * + * @param array $data The data. + */ + public function test_direct_call_without_edit_post_fails_before_the_attributes( array $data ): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'contributor' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => $data, + ) + ); + + $this->assert_error( $result, 'content_cannot_edit', 403, 'A direct call without edit_post' ); + $this->assertSame( 'Sorry, you are not allowed to edit this post.', $result->get_error_message(), 'The message should be the edit message.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns keys that are not part of the input, and where they are sent. + * + * @return array The cases. + */ + public static function data_keys_outside_the_allow_list(): array { + return array( + 'data ID' => array( 'data', 'ID', 1 ), + 'data type' => array( 'data', 'type', 'page' ), + 'data post_type' => array( 'data', 'post_type', 'page' ), + 'data post_password' => array( 'data', 'post_password', 'secret' ), + 'data meta_input' => array( 'data', 'meta_input', array( 'cw_key' => 'value' ) ), + 'data tax_input' => array( 'data', 'tax_input', array( 'category' => array( 1 ) ) ), + 'data comment_status' => array( 'data', 'comment_status', 'closed' ), + 'data post_author' => array( 'data', 'post_author', 1 ), + 'data import_id' => array( 'data', 'import_id', 7654321 ), + 'data guid' => array( 'data', 'guid', 'https://example.org/forged' ), + 'data page_template' => array( 'data', 'page_template', 'cw-template.php' ), + 'data modified' => array( 'data', 'modified', '2001-01-01T00:00:00' ), + 'data post_modified' => array( 'data', 'post_modified', '2001-01-01 00:00:00' ), + 'data post_modified_gmt' => array( 'data', 'post_modified_gmt', '2001-01-01 00:00:00' ), + 'data sticky' => array( 'data', 'sticky', true ), + 'data tags_input' => array( 'data', 'tags_input', array( 'cw-tag' ) ), + 'data post_category' => array( 'data', 'post_category', array( 2 ) ), + 'data menu_order' => array( 'data', 'menu_order', 5 ), + 'data post_mime_type' => array( 'data', 'post_mime_type', 'image/jpeg' ), + 'where status' => array( 'where', 'status', 'draft' ), + 'where ID' => array( 'where', 'ID', 1 ), + ); + } + + /** + * A key outside the allow-list fails input validation. + * + * Covers S1, S5 and S6. + * + * @dataProvider data_keys_outside_the_allow_list + * + * @since 7.3.0 + * + * @param string $location Where the key is sent. + * @param string $key The key. + * @param mixed $value The value. + */ + public function test_key_outside_the_allow_list_fails_input_validation( string $location, string $key, $value ): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $input = array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Extra key' ), + ); + + $input[ $location ][ $key ] = $value; + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the key.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, keys outside the allow-list are ignored and never written. + * + * Covers S6. + * + * @since 7.3.0 + */ + public function test_direct_call_ignores_keys_outside_the_allow_list(): void { + $post_id = self::$post_ids['administrator:draft']; + $other_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $other_before = get_post( $other_id )->to_array(); + $stored = get_post( $post_id ); + $category_id = self::factory()->category->create(); + $categories = wp_get_post_categories( $post_id ); + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + 'ID' => $other_id, + ), + 'data' => array( + 'title' => 'Allow-list', + 'type' => 'page', + 'ID' => $other_id, + 'post_type' => 'page', + 'post_password' => 'secret', + 'meta_input' => array( 'cw_key' => 'value' ), + 'tax_input' => array( 'category' => array( 1 ) ), + 'comment_status' => 'closed', + 'post_author' => self::$user_ids['editor'], + 'import_id' => 7654321, + 'guid' => 'https://example.org/forged', + 'page_template' => 'cw-template.php', + 'modified' => '2001-01-01T00:00:00', + 'post_modified' => '2001-01-01 00:00:00', + 'post_modified_gmt' => '2001-01-01 00:00:00', + 'sticky' => true, + 'tags_input' => array( 'cw-tag' ), + 'post_category' => array( $category_id ), + 'menu_order' => 5, + 'post_mime_type' => 'image/jpeg', + ), + 'unknown' => 'ignored', + ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'Allow-list', $post->post_title, 'The title should be stored.' ); + $this->assertSame( 'post', $post->post_type, 'The post type should not change.' ); + $this->assertSame( '', $post->post_password, 'No password should be stored.' ); + $this->assertSame( array(), get_post_meta( $post_id, 'cw_key' ), 'No meta should be stored.' ); + $this->assertSame( $stored->comment_status, $post->comment_status, 'The comment status should not change.' ); + $this->assertSame( $stored->post_author, $post->post_author, 'The author should not change.' ); + $this->assertSame( $stored->guid, $post->guid, 'The GUID should not change.' ); + $this->assertSame( '', get_post_meta( $post_id, '_wp_page_template', true ), 'No page template should be stored.' ); + $this->assert_is_now( $post->post_modified_gmt, 'The stored modified date' ); + $this->assertFalse( is_sticky( $post_id ), 'The post should not be sticky.' ); + $this->assertSame( array(), wp_get_post_tags( $post_id, array( 'fields' => 'ids' ) ), 'No tag should be stored.' ); + $this->assertSame( array( (int) get_option( 'default_category' ) ), $categories, 'Precondition: the post should have only the default category.' ); + $this->assertSame( $categories, wp_get_post_categories( $post_id ), 'Only the default category should be kept.' ); + $this->assertSame( 0, $post->menu_order, 'The menu order should stay 0.' ); + $this->assertSame( '', $post->post_mime_type, 'No MIME type should be stored.' ); + $this->assertSame( $other_before, get_post( $other_id )->to_array(), 'The post named by ID should not change.' ); + } + + /** + * Returns statuses that can never be set. + * + * @return array The statuses. + */ + public static function data_statuses_that_can_never_be_set(): array { + return array( + 'trash' => array( 'trash' ), + 'auto-draft' => array( 'auto-draft' ), + 'inherit' => array( 'inherit' ), + 'unregistered' => array( 'cw_unregistered' ), + 'request-pending' => array( 'request-pending' ), + 'request-confirmed' => array( 'request-confirmed' ), + 'upper case' => array( 'PUBLISH' ), + 'trailing space' => array( 'publish ' ), + 'empty string' => array( '' ), + 'comma-joined list' => array( 'draft,publish' ), + ); + } + + /** + * An internal or unregistered status fails input validation. + * + * Covers S5 and S7. + * + * @dataProvider data_statuses_that_can_never_be_set + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_status_that_can_never_be_set_fails_input_validation( string $status ): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'status' => $status ) ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the status.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, an internal or unregistered status is an invalid status for a post that + * is not stored in it. + * + * Covers S1, S5 and S7. + * + * @dataProvider data_statuses_that_can_never_be_set + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_direct_call_rejects_a_status_that_can_never_be_set( string $status ): void { + $post_id = self::$post_ids['administrator:publish']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'status' => $status ), + ) + ); + + $this->assert_invalid_attribute( $result, 'status', 'A direct call with a status that can never be set' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns status changes that a contributor, and an author denied `publish_posts`, may not + * make on their own draft. + * + * @return array The cases. + */ + public static function data_status_changes_without_publish_posts(): array { + $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; + $private_message = 'Sorry, you are not allowed to create private posts in this post type.'; + + return array( + 'publish by a contributor' => array( 'contributor', 'publish', $publish_message ), + 'future by a contributor' => array( 'contributor', 'future', $publish_message ), + 'private by a contributor' => array( 'contributor', 'private', $private_message ), + 'publish by an author denied publish_posts' => array( 'restricted_author', 'publish', $publish_message ), + 'future by an author denied publish_posts' => array( 'restricted_author', 'future', $publish_message ), + 'private by an author denied publish_posts' => array( 'restricted_author', 'private', $private_message ), + ); + } + + /** + * A status that makes a post visible needs `publish_posts`. + * + * Covers S5 and S7. + * + * @dataProvider data_status_changes_without_publish_posts + * + * @since 7.3.0 + * + * @param string $role The role or fixture name of the user, who owns the draft. + * @param string $status The status. + * @param string $message The error message. + */ + public function test_status_that_makes_a_post_visible_needs_publish_posts( string $role, string $status, string $message ): void { + $post_id = self::$post_ids[ "{$role}:draft" ]; + + $this->login_as( $role ); + $this->register_abilities(); + + $this->assertFalse( current_user_can( 'publish_posts' ), 'Precondition: the user should not be able to publish posts.' ); + + $data = array( 'status' => $status ); + if ( 'future' === $status ) { + $data['date'] = array( 'gmt' => '2035-06-01T12:00:00' ); + } + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, $data ); + + $this->assert_error( $result, 'content_cannot_publish', 403, 'A status that makes a post visible without publish_posts' ); + $this->assertSame( $message, $result->get_error_message(), 'The message should name what the role may not do.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A status equal to the stored status changes nothing and passes with no check. + * + * A contributor may edit their own private post, but may not make a post private. + * + * @since 7.3.0 + */ + public function test_status_equal_to_the_stored_status_passes_with_no_check(): void { + $post_id = self::$post_ids['contributor:private']; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'status' => 'private', + 'title' => 'Still private', + ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'private', $post->post_status, 'The post should stay private.' ); + $this->assertSame( 'Still private', $post->post_title, 'The title should be stored.' ); + } + + /** + * A draft is scheduled with `future` and a date in the future. + * + * @since 7.3.0 + */ + public function test_schedules_a_draft(): void { + $post_id = self::$post_ids['author:draft']; + + $this->login_as( 'author' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'status' => 'future', + 'date' => array( 'gmt' => '2035-06-01T12:00:00' ), + ), + array( 'status', 'date' ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'future', $post->post_status, 'The post should be scheduled.' ); + $this->assertSame( '2035-06-01 12:00:00', $post->post_date_gmt, 'The post should be scheduled for the sent date.' ); + $this->assertSame( 'future', $result['status'], 'The scheduled status should be returned.' ); + } + + /** + * Returns statuses a published post can be moved back to. + * + * @return array The statuses. + */ + public static function data_unpublished_statuses(): array { + return array( + 'draft' => array( 'draft' ), + 'pending' => array( 'pending' ), + ); + } + + /** + * A published post can be unpublished. + * + * @dataProvider data_unpublished_statuses + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_unpublishes_a_post( string $status ): void { + $post_id = self::$post_ids['editor:publish']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'status' => $status ), array( 'status' ) ); + + $this->assertSame( $status, $this->assert_updated( $result, $post_id )->post_status, 'The post should be unpublished.' ); + $this->assertSame( $status, $result['status'], 'The new status should be returned.' ); + } + + /** + * Returns dates that change the stored status of a post. + * + * @return array The cases. + */ + public static function data_statuses_reported_as_stored(): array { + return array( + 'publish with a future date' => array( 'publish', '2035-06-01T12:00:00', 'future' ), + 'future with a past date' => array( 'future', '2016-12-12T19:00:00', 'publish' ), + ); + } + + /** + * The regular save rules decide the stored status, and the output reports it. + * + * @dataProvider data_statuses_reported_as_stored + * + * @since 7.3.0 + * + * @param string $status The requested status. + * @param string $gmt The requested GMT date. + * @param string $expected The stored status. + */ + public function test_output_reports_the_stored_status( string $status, string $gmt, string $expected ): void { + $post_id = self::$post_ids['editor:draft']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'status' => $status, + 'date' => array( 'gmt' => $gmt ), + ), + array( 'status' ) + ); + + $this->assertSame( $expected, $this->assert_updated( $result, $post_id )->post_status, 'The post should be stored in the expected status.' ); + $this->assertSame( $expected, $result['status'], 'The output should report the stored status.' ); + } + + /** + * A post in the Trash is updated like any other post and stays in the Trash. + * + * @since 7.3.0 + */ + public function test_updates_a_post_in_the_trash(): void { + $post_id = self::$post_ids['editor:trash_publish']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'title' => 'Trashed and edited' ), array( 'status' ) ); + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'Trashed and edited', $post->post_title, 'The title should be stored.' ); + $this->assertSame( 'trash', $post->post_status, 'The post should stay in the Trash.' ); + $this->assertSame( 'trash', $result['status'], 'The trash status should be returned.' ); + } + + /** + * Returns status changes that move a post out of the Trash. + * + * @return array The cases. + */ + public static function data_statuses_that_move_a_post_out_of_the_trash(): array { + return array( + 'draft by a contributor' => array( 'contributor', 'draft' ), + 'pending by an author' => array( 'author', 'pending' ), + 'publish by an author' => array( 'author', 'publish' ), + ); + } + + /** + * Sending a status moves a post out of the Trash through a plain update. + * + * @dataProvider data_statuses_that_move_a_post_out_of_the_trash + * + * @since 7.3.0 + * + * @param string $role The role, which owns the post. + * @param string $status The status. + */ + public function test_status_moves_a_post_out_of_the_trash( string $role, string $status ): void { + $post_id = self::$post_ids[ "{$role}:trash_draft" ]; + + $this->login_as( $role ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'status' => $status ), array( 'status' ) ); + + $this->assertSame( $status, $this->assert_updated( $result, $post_id )->post_status, 'The post should leave the Trash in the sent status.' ); + $this->assertSame( $status, $result['status'], 'The new status should be returned.' ); + } + + /** + * Moving a post out of the Trash passes the status gate like any status change. + * + * Covers S5 and S7. + * + * @since 7.3.0 + */ + public function test_publishing_a_post_from_the_trash_needs_publish_posts(): void { + $post_id = self::$post_ids['contributor:trash_draft']; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'status' => 'publish' ) ); + + $this->assert_error( $result, 'content_cannot_publish', 403, 'Publishing a post from the Trash without publish_posts' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns authors that a user without `edit_others_posts` may not name. + * + * @return array The authors, as fixture names. + */ + public static function data_authors_named_without_edit_others_posts(): array { + return array( + 'existing user' => array( 'editor' ), + 'missing user' => array( 'missing' ), + ); + } + + /** + * A user without `edit_others_posts` gets the same error for an existing and a missing + * author, so the error never tells whether a user exists. + * + * Covers S5, S8 and O3. + * + * @dataProvider data_authors_named_without_edit_others_posts + * + * @since 7.3.0 + * + * @param string $author The author, as a fixture name. + */ + public function test_naming_another_author_without_edit_others_posts_is_refused( string $author ): void { + $post_id = self::$post_ids['author:draft']; + + $this->login_as( 'author' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'author' => self::$user_ids[ $author ] ?? self::MISSING_ID ) ); + + $this->assertWPError( $result, 'Naming another author should fail.' ); + $this->assertSame( + array( + 'code' => 'content_cannot_edit_others', + 'message' => 'Sorry, you are not allowed to update posts as this user.', + 'data' => array( 'status' => 403 ), + ), + array( + 'code' => $result->get_error_code(), + 'message' => $result->get_error_message(), + 'data' => $result->get_error_data(), + ), + 'The error should be the same whether the user exists or not.' + ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A user with `edit_others_posts` may change the author of a post. + * + * @since 7.3.0 + */ + public function test_changes_the_author(): void { + $post_id = self::$post_ids['editor:draft']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'author' => self::$user_ids['author'] ), array( 'author.id' ) ); + + $this->assertSame( self::$user_ids['author'], (int) $this->assert_updated( $result, $post_id )->post_author, 'The new author should be stored.' ); + $this->assertSame( array( 'id' => self::$user_ids['author'] ), $result['author'], 'The new author should be returned.' ); + } + + /** + * An author equal to the stored author changes nothing and passes with no check, even when + * that user no longer exists. + * + * @since 7.3.0 + */ + public function test_author_equal_to_the_stored_author_passes_with_no_check(): void { + global $wpdb; + + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'draft', + ) + ); + $wpdb->update( $wpdb->posts, array( 'post_author' => self::MISSING_ID ), array( 'ID' => $post_id ) ); + clean_post_cache( $post_id ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'author' => self::MISSING_ID, + 'title' => 'Orphan', + ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'Orphan', $post->post_title, 'The title should be stored.' ); + $this->assertSame( self::MISSING_ID, (int) $post->post_author, 'The stored author should not change.' ); + } + + /** + * A user with `edit_others_posts` who names a missing user gets the invalid author error. + * + * Covers S5 and S8. + * + * @since 7.3.0 + */ + public function test_naming_a_missing_author_with_edit_others_posts_is_an_invalid_author(): void { + $post_id = self::$post_ids['editor:draft']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'author' => self::MISSING_ID ) ); + + $this->assert_invalid_attribute( $result, 'author', 'A missing author' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * On multisite, a user who is not a member of the site cannot be the author. + * + * Covers S5 and S8. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_invalid_author(): void { + $non_member = self::factory()->user->create( array( 'role' => 'author' ) ); + remove_user_from_blog( $non_member, get_current_blog_id() ); + + $post_id = self::$post_ids['editor:draft']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'author' => $non_member ) ); + + $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns parents that a page may not have. + * + * @return array The parents, as fixture names. + */ + public static function data_parents_that_cannot_be_used(): array { + return array( + 'missing post' => array( 'missing' ), + 'post of another type' => array( 'admin_post' ), + 'page the user may not read' => array( 'editor_draft_page' ), + 'the page itself' => array( 'writer_page' ), + 'a child' => array( 'writer_child' ), + 'a grandchild' => array( 'writer_grandchild' ), + 'revision' => array( 'revision' ), + ); + } + + /** + * A missing parent, a parent of another type, a parent the user may not read and a parent + * that would make a loop give one identical error. + * + * Covers S5 and S9. + * + * @dataProvider data_parents_that_cannot_be_used + * + * @since 7.3.0 + * + * @param string $parent_key The parent, as a fixture name. + */ + public function test_parent_that_cannot_be_used_gives_one_identical_error( string $parent_key ): void { + $post_id = self::$post_ids['writer_page']; + + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should be able to edit the page.' ); + $this->assertFalse( current_user_can( 'read_post', self::$post_ids['editor_draft_page'] ), 'Precondition: the user should not be able to read the draft page of the editor.' ); + + $before = $this->snapshot( $post_id ); + $reference = $this->update_post( $post_id, array( 'parent' => self::MISSING_ID ) ); + $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? self::MISSING_ID ) ); + + $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); + $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for a missing parent.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A parent equal to the stored parent changes nothing and passes with no check, even when the + * user may not read it. + * + * @since 7.3.0 + */ + public function test_parent_equal_to_the_stored_parent_passes_with_no_check(): void { + $post_id = self::$post_ids['writer_page_with_hidden_parent']; + + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'parent' => self::$post_ids['editor_draft_page'], + 'title' => 'Same parent', + ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'Same parent', $post->post_title, 'The title should be stored.' ); + $this->assertSame( self::$post_ids['editor_draft_page'], $post->post_parent, 'The stored parent should not change.' ); + } + + /** + * Returns parents a page may be moved to. + * + * @return array The parents, as fixture names, or null for 0. + */ + public static function data_parents_that_can_be_used(): array { + return array( + 'readable page' => array( 'admin_page' ), + 'top level' => array( null ), + ); + } + + /** + * A readable page of the same type can be the parent, and `0` makes the page a top-level page. + * + * @dataProvider data_parents_that_can_be_used + * + * @since 7.3.0 + * + * @param string|null $parent_key The parent, as a fixture name, or null for 0. + */ + public function test_moves_a_page_to_another_parent( ?string $parent_key ): void { + $post_id = self::$post_ids['writer_page_with_hidden_parent']; + $parent_id = null === $parent_key ? 0 : self::$post_ids[ $parent_key ]; + + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'parent' => $parent_id ), array( 'parent' ) ); + + $this->assertSame( $parent_id, $this->assert_updated( $result, $post_id )->post_parent, 'The new parent should be stored.' ); + $this->assertSame( $parent_id, $result['parent'], 'The new parent should be returned.' ); + } + + /** + * Returns attributes that a post type does not support. + * + * @return array The cases. + */ + public static function data_unsupported_attributes(): array { + return array( + 'excerpt of a page' => array( 'admin_page', 'excerpt', 'An excerpt' ), + 'parent of a post' => array( 'admin_post', 'parent', 1 ), + 'parent 0 of a post' => array( 'admin_post', 'parent', 0 ), + ); + } + + /** + * An attribute the post type does not support is rejected, even when it would change nothing. + * + * Covers S1, S5 and O4: the first failure is returned and nothing is written. + * + * @dataProvider data_unsupported_attributes + * + * @since 7.3.0 + * + * @param string $post The post, as a fixture name. + * @param string $attribute The attribute. + * @param mixed $value The value. + */ + public function test_unsupported_attribute_is_rejected( string $post, string $attribute, $value ): void { + $post_id = self::$post_ids[ $post ]; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( $attribute => $value ) ); + + $this->assert_invalid_attribute( $result, $attribute, 'An unsupported attribute' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns attribute values of the wrong type. + * + * @return array The cases. + */ + public static function data_values_of_the_wrong_type(): array { + return array( + 'title that is a number' => array( 'title', 123 ), + 'content that is null' => array( 'content', null ), + 'status that is a list' => array( 'status', array( 'draft' ) ), + 'date that is a string' => array( 'date', '2016-12-12T14:00:00' ), + 'date that cannot be parsed' => array( 'date', array( 'site' => 'not a date' ) ), + 'author that is a fraction' => array( 'author', 1.5 ), + 'author that is text' => array( 'author', '1abc' ), + ); + } + + /** + * Called directly, a value of the wrong type is an invalid attribute. + * + * Covers S1 and S5. + * + * @dataProvider data_values_of_the_wrong_type + * + * @since 7.3.0 + * + * @param string $attribute The attribute. + * @param mixed $value The value. + */ + public function test_direct_call_rejects_a_value_of_the_wrong_type( string $attribute, $value ): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( $attribute => $value ), + ) + ); + + $this->assert_invalid_attribute( $result, $attribute, 'A value of the wrong type' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Attributes that were not sent keep their stored values. + * + * @since 7.3.0 + */ + public function test_attributes_not_sent_keep_their_stored_values(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['author'], + 'post_status' => 'publish', + 'post_title' => 'Kept title', + 'post_content' => 'Kept content.', + 'post_excerpt' => 'Kept excerpt.', + 'post_name' => 'kept-slug', + 'post_date' => '2016-12-12 14:00:00', + ) + ); + $stored = get_post( $post_id ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'content' => 'New content.' ) ), $post_id ); + + $this->assertSame( 'New content.', $post->post_content, 'The content should be stored.' ); + $this->assertSame( $stored->post_title, $post->post_title, 'The title should be kept.' ); + $this->assertSame( $stored->post_excerpt, $post->post_excerpt, 'The excerpt should be kept.' ); + $this->assertSame( $stored->post_name, $post->post_name, 'The slug should be kept.' ); + $this->assertSame( $stored->post_status, $post->post_status, 'The status should be kept.' ); + $this->assertSame( $stored->post_author, $post->post_author, 'The author should be kept.' ); + $this->assertSame( $stored->post_date_gmt, $post->post_date_gmt, 'The date should be kept.' ); + } + + /** + * A slug that sanitizes to nothing is an invalid slug. + * + * Covers S1 and S5. + * + * @since 7.3.0 + */ + public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'slug' => '!!!' ) ); + + $this->assert_invalid_attribute( $result, 'slug', 'A slug that sanitizes to nothing' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns posts that take the slug of another post. + * + * @return array The cases. + */ + public static function data_posts_taking_a_used_slug(): array { + return array( + 'stored draft' => array( 'administrator:draft', null ), + 'stored pending' => array( 'administrator:pending', null ), + 'draft from publish' => array( 'administrator:publish', 'draft' ), + 'published post' => array( 'administrator:publish', null ), + ); + } + + /** + * A post never takes the slug of another post, also as a draft or a pending post. + * + * @dataProvider data_posts_taking_a_used_slug + * + * @since 7.3.0 + * + * @param string $post The post, as a fixture name. + * @param string|null $status The status sent next to the slug, or null to send none. + */ + public function test_slug_of_another_post_is_made_unique( string $post, ?string $status ): void { + $post_id = self::$post_ids[ $post ]; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $data = array( 'slug' => 'Shared Slug' ); + if ( null !== $status ) { + $data['status'] = $status; + } + + $result = $this->update_post( $post_id, $data, array( 'slug' ) ); + + $this->assertSame( 'shared-slug-2', $this->assert_updated( $result, $post_id )->post_name, 'The sanitized slug should be made unique.' ); + } + + /** + * Returns parents for a draft page that takes the slug of an existing child page. + * + * @return array The cases. + */ + public static function data_draft_page_slugs_by_parent(): array { + return array( + 'shared parent sent' => array( 'writer_page', 'writer-child-2' ), + 'different parent sent' => array( 'admin_page', 'writer-child' ), + ); + } + + /** + * A draft page is made unique among the pages that share the sent parent. + * + * @dataProvider data_draft_page_slugs_by_parent + * + * @since 7.3.0 + * + * @param string $parent_key The parent, as a fixture name. + * @param string $expected The stored slug. + */ + public function test_draft_page_slug_is_unique_among_its_siblings( string $parent_key, string $expected ): void { + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_parent' => self::$post_ids['writer_page'], + 'post_name' => 'writer-child', + ) + ); + $post_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'draft', + 'post_title' => 'Moving page', + ) + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'slug' => 'writer-child', + 'parent' => self::$post_ids[ $parent_key ], + ) + ); + + $this->assertSame( $expected, $this->assert_updated( $result, $post_id )->post_name, 'The slug should be unique among the siblings.' ); + } + + /** + * D1: an update without a date keeps a floating draft floating. + * + * @since 7.3.0 + */ + public function test_update_without_a_date_keeps_a_draft_floating(): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'title' => 'Still floating' ) ), $post_id ); + + $this->assertSame( 'draft', $post->post_status, 'The post should stay a draft.' ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The draft should keep floating.' ); + } + + /** + * Returns statuses that do not float, reached from a floating draft without a date. + * + * @return array The statuses. + */ + public static function data_statuses_that_stamp_the_current_time(): array { + return array( + 'publish' => array( 'publish' ), + 'future' => array( 'future' ), + ); + } + + /** + * D1: publishing a floating draft without a date stamps the current time, also when it is + * sent as scheduled, and with a past local date. + * + * @dataProvider data_statuses_that_stamp_the_current_time + * + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_publishing_a_floating_draft_without_a_date_stamps_the_current_time( string $status ): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = $this->create_floating_draft( '2020-01-01 10:00:00' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'status' => $status ) ), $post_id ); + + $this->assertSame( 'publish', $post->post_status, 'The post should be published.' ); + $this->assert_is_now( $post->post_date_gmt, 'The GMT date' ); + } + + /** + * D1: moving a floating draft to pending without a date keeps it floating. + * + * @since 7.3.0 + */ + public function test_pending_without_a_date_keeps_a_draft_floating(): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'status' => 'pending' ) ), $post_id ); + + $this->assertSame( 'pending', $post->post_status, 'The post should be pending.' ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The pending post should keep floating.' ); + } + + /** + * Returns dates that differ from the stored ones, in America/New_York. + * + * @return array}> The dates, each 2017-01-01 10:00 local. + */ + public static function data_new_dates(): array { + return array( + 'site' => array( array( 'site' => '2017-01-01T10:00:00' ) ), + 'gmt' => array( array( 'gmt' => '2017-01-01T15:00:00' ) ), + 'site with an offset' => array( array( 'site' => '2017-01-01T16:00:00+01:00' ) ), + 'both, the site wins' => array( + array( + 'site' => '2017-01-01T10:00:00', + 'gmt' => '2001-01-01T00:00:00', + ), + ), + ); + } + + /** + * D2: a date that differs from the stored one is stored as sent, local and GMT. + * + * @dataProvider data_new_dates + * + * @since 7.3.0 + * + * @param array $date The sent date. + */ + public function test_new_date_is_stored_local_and_gmt( array $date ): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_date' => '2016-12-12 14:00:00', + 'post_date_gmt' => '2016-12-12 19:00:00', + ) + ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'date' => $date ) ), $post_id ); + + $this->assertSame( '2017-01-01 10:00:00', $post->post_date, 'The local date should be stored.' ); + $this->assertSame( '2017-01-01 15:00:00', $post->post_date_gmt, 'The GMT date should be stored.' ); + } + + /** + * Returns dates for a floating draft that differ from the date it shows. + * + * @return array}> The dates, each 2017-01-01 10:00 local. + */ + public static function data_new_dates_for_a_floating_draft(): array { + return array( + 'site' => array( array( 'site' => '2017-01-01T10:00:00' ) ), + 'gmt' => array( array( 'gmt' => '2017-01-01T15:00:00' ) ), + ); + } + + /** + * D2: a new date pins a floating draft. + * + * @dataProvider data_new_dates_for_a_floating_draft + * + * @since 7.3.0 + * + * @param array $date The sent date. + */ + public function test_new_date_pins_a_floating_draft( array $date ): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'date' => $date ) ), $post_id ); + + $this->assertSame( 'draft', $post->post_status, 'The post should stay a draft.' ); + $this->assertSame( '2017-01-01 10:00:00', $post->post_date, 'The local date should be stored.' ); + $this->assertSame( '2017-01-01 15:00:00', $post->post_date_gmt, 'The draft should no longer float.' ); + } + + /** + * Returns dates equal to the date a floating draft shows, 2031-06-15 10:00 in America/New_York. + * + * @return array}> The dates. + */ + public static function data_dates_equal_to_the_floating_date(): array { + return array( + 'site' => array( array( 'site' => '2031-06-15T10:00:00' ) ), + 'gmt' => array( array( 'gmt' => '2031-06-15T14:00:00' ) ), + ); + } + + /** + * D3: sending back the date a floating draft shows counts as not sent and never pins it. + * + * @dataProvider data_dates_equal_to_the_floating_date + * + * @since 7.3.0 + * + * @param array $date The sent date. + */ + public function test_date_equal_to_the_floating_date_does_not_pin_the_draft( array $date ): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( + $this->update_post( + $post_id, + array( + 'date' => $date, + 'title' => 'Same date', + ) + ), + $post_id + ); + + $this->assertSame( 'draft', $post->post_status, 'The post should stay a draft.' ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The draft should keep floating.' ); + } + + /** + * Returns past dates equal to the date a floating draft shows, 2020-01-01 10:00 in + * America/New_York. + * + * @return array}> The dates. + */ + public static function data_past_dates_equal_to_the_floating_date(): array { + return array( + 'site' => array( array( 'site' => '2020-01-01T10:00:00' ) ), + 'gmt' => array( array( 'gmt' => '2020-01-01T15:00:00' ) ), + ); + } + + /** + * D3: publishing a floating draft with the past date it shows publishes it at the current + * time, as if no date was sent. + * + * @dataProvider data_past_dates_equal_to_the_floating_date + * + * @since 7.3.0 + * + * @param array $date The sent date. + */ + public function test_publishing_with_the_past_floating_date_stamps_the_current_time( array $date ): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = $this->create_floating_draft( '2020-01-01 10:00:00' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( + $this->update_post( + $post_id, + array( + 'date' => $date, + 'status' => 'publish', + ) + ), + $post_id + ); + + $this->assertSame( 'publish', $post->post_status, 'The post should be published.' ); + $this->assert_is_now( $post->post_date_gmt, 'The GMT date of the published post' ); + } + + /** + * D3: a date equal to the stored date of a post that does not float changes nothing. + * + * @dataProvider data_dates_equal_to_the_floating_date + * + * @since 7.3.0 + * + * @param array $date The sent date. + */ + public function test_date_equal_to_the_stored_date_changes_nothing( array $date ): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'future', + 'post_date' => '2031-06-15 10:00:00', + 'post_date_gmt' => '2031-06-15 14:00:00', + ) + ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'date' => $date ) ), $post_id ); + + $this->assertSame( 'future', $post->post_status, 'The post should stay scheduled.' ); + $this->assertSame( '2031-06-15 10:00:00', $post->post_date, 'The local date should not change.' ); + $this->assertSame( '2031-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should not change.' ); + } + + /** + * Returns statuses that do not float, for scheduling a floating draft for the date it shows. + * + * @return array}> The cases. + */ + public static function data_schedules_a_floating_draft_for_the_date_it_shows(): array { + return array( + 'future, site' => array( 'future', array( 'site' => '2031-06-15T10:00:00' ) ), + 'future, gmt' => array( 'future', array( 'gmt' => '2031-06-15T14:00:00' ) ), + 'publish, site' => array( 'publish', array( 'site' => '2031-06-15T10:00:00' ) ), + 'publish, gmt' => array( 'publish', array( 'gmt' => '2031-06-15T14:00:00' ) ), + ); + } + + /** + * D4: scheduling a floating draft for the future date it shows schedules it for that date. + * + * @dataProvider data_schedules_a_floating_draft_for_the_date_it_shows + * + * @since 7.3.0 + * + * @param string $status The status. + * @param array $date The sent date. + */ + public function test_scheduling_a_floating_draft_for_the_date_it_shows( string $status, array $date ): void { + update_option( 'timezone_string', 'America/New_York' ); + $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'date' => $date, + 'status' => $status, + ), + array( 'status' ) + ); + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'future', $post->post_status, 'The post should be scheduled.' ); + $this->assertSame( '2031-06-15 10:00:00', $post->post_date, 'The local date should be the sent date.' ); + $this->assertSame( '2031-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should be the sent date.' ); + $this->assertSame( 'future', $result['status'], 'The scheduled status should be returned.' ); + } + + /** + * A date that is not a date-time fails input validation. + * + * Covers S5. + * + * @since 7.3.0 + */ + public function test_date_that_is_not_a_date_time_fails_input_validation(): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'date' => array( 'gmt' => '2010-60-01T02:00:00Z' ) ) ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the date.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * The markup of an author is filtered on update. + * + * Covers S10. + * + * @since 7.3.0 + */ + public function test_markup_of_an_author_is_filtered(): void { + $post_id = self::$post_ids['author:draft']; + + $this->login_as( 'author' ); + $this->register_abilities(); + + $markup = 'link'; + $result = $this->update_post( + $post_id, + array( + 'title' => $markup, + 'content' => $markup, + ), + array( 'title.raw', 'content.raw' ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'link', $post->post_title, 'The stored title should be filtered.' ); + $this->assertSame( 'link', $post->post_content, 'The stored content should be filtered.' ); + $this->assertSame( 'link', $result['title']['raw'], 'The returned title should be filtered.' ); + } + + /** + * The markup of an administrator with `unfiltered_html` is stored unchanged on update. + * + * Covers S10. + * + * @group ms-excluded + * + * @since 7.3.0 + */ + public function test_markup_of_an_administrator_is_not_filtered(): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $markup = '
div
'; + $post = $this->assert_updated( $this->update_post( $post_id, array( 'content' => $markup ) ), $post_id ); + + $this->assertSame( $markup, $post->post_content, 'The stored content should not be filtered.' ); + } + + /** + * On multisite, the markup of a site administrator is filtered on update. + * + * Covers S10. + * + * @group ms-required + * + * @since 7.3.0 + */ + public function test_markup_of_a_site_administrator_is_filtered_on_multisite(): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'content' => '
div
' ) ), $post_id ); + + $this->assertSame( '
div
', $post->post_content, 'The stored content should be filtered.' ); + } + + /** + * Quotes, backslashes and non-ASCII text survive a round trip unchanged. + * + * @since 7.3.0 + */ + public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { + $text = 'He said "it\'s done" \\ C:\\path\\to\\file \\\\server – café, naïve, 日本語, emoji ✓'; + $post_id = self::$post_ids['editor:draft']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'title' => $text, + 'content' => $text, + 'excerpt' => $text, + ), + array( 'title.raw', 'content.raw', 'excerpt.raw' ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( $text, $post->post_title, 'The stored title should be unchanged.' ); + $this->assertSame( $text, $post->post_content, 'The stored content should be unchanged.' ); + $this->assertSame( $text, $post->post_excerpt, 'The stored excerpt should be unchanged.' ); + $this->assertSame( $text, $result['title']['raw'], 'The returned title should be unchanged.' ); + $this->assertSame( $text, $result['content']['raw'], 'The returned content should be unchanged.' ); + $this->assertSame( $text, $result['excerpt']['raw'], 'The returned excerpt should be unchanged.' ); + } + + /** + * Raw fields are left out when the writer can no longer edit the post after the write. + * + * The user may publish posts but not edit published posts. + * + * Covers S11. + * + * @since 7.3.0 + */ + public function test_raw_fields_are_left_out_when_the_writer_can_no_longer_edit_the_post(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['publisher'], + 'post_status' => 'draft', + 'post_title' => 'Publish once', + 'post_content' => 'Body.', + ) + ); + + $this->login_as( 'publisher' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'status' => 'publish' ), array( 'title', 'content.raw', 'content.rendered' ) ); + + $this->assertSame( 'publish', $this->assert_updated( $result, $post_id )->post_status, 'The post should be published.' ); + $this->assertFalse( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should not be able to edit the published post.' ); + $this->assertSame( array( 'rendered' => 'Publish once' ), $result['title'], 'The title should be returned without its raw value.' ); + $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); + } + + /** + * A password-protected post is returned with its protection flag. + * + * Covers S11. + * + * @since 7.3.0 + */ + public function test_password_protected_post_is_returned_with_its_protection_flag(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_password' => 'secret', + ) + ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'content' => 'Protected body.' ), array( 'content.raw', 'content.protected' ) ); + + $this->assert_updated( $result, $post_id ); + + $this->assertSame( + array( + 'raw' => 'Protected body.', + 'protected' => true, + ), + $result['content'], + 'The content should be returned raw to the editor and flagged as protected.' + ); + $this->assertSame( 'secret', get_post( $post_id )->post_password, 'The password should be kept.' ); + } + + /** + * The current user is checked at each call, also on the same instance. + * + * Covers S4. + * + * @since 7.3.0 + */ + public function test_each_call_checks_the_current_user(): void { + $post_id = self::$post_ids['author:draft']; + $execute = $this->get_ability_callbacks()['execute_callback']; + + $this->login_as( 'author' ); + $first = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'First' ), + ) + ); + + $this->login_as( 'contributor' ); + $before = $this->snapshot( $post_id ); + $second = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Second' ), + ) + ); + + $this->assertSame( 'First', $this->assert_updated( $first, $post_id )->post_title, 'The first call should update the post.' ); + $this->assert_error( $second, 'content_cannot_edit', 403, 'The second call as a contributor' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'The second call should write nothing.' ); + } + + /** + * The author visibility is checked again at each call. + * + * Covers S11. + * + * @since 7.3.0 + */ + public function test_each_call_checks_the_author_visibility_again(): void { + $writer = self::factory()->user->create( array( 'role' => 'author' ) ); + $post_id = self::factory()->post->create( + array( + 'post_author' => $writer, + 'post_status' => 'draft', + ) + ); + + $this->login_as( 'editor' ); + $this->assertFalse( current_user_can( 'list_users' ), 'Precondition: the editor should not be able to list users.' ); + + $execute = $this->get_ability_callbacks()['execute_callback']; + $input = array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Visibility' ), + 'fields' => array( 'author' ), + ); + + $hidden = $execute( $input ); + + self::factory()->post->create( + array( + 'post_author' => $writer, + 'post_status' => 'publish', + ) + ); + + $visible = $execute( $input ); + + $this->assertIsArray( $hidden, 'The first call should succeed.' ); + $this->assertIsArray( $visible, 'The second call should succeed.' ); + $this->assertSame( + array( + 'id' => $writer, + 'name' => null, + 'slug' => null, + ), + $hidden['author'], + 'An author without published posts should not be shown.' + ); + $this->assertSame( get_userdata( $writer )->display_name, $visible['author']['name'], 'The author should be shown once they have a published post.' ); + } + + /** + * A save error keeps its own code and message, with status 400 when the post is empty. + * + * Covers S5. + * + * @since 7.3.0 + */ + public function test_empty_post_is_the_save_error(): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( + $post_id, + array( + 'title' => '', + 'content' => '', + 'excerpt' => '', + ) + ); + + $this->assert_error( $result, 'empty_content', 400, 'An empty post' ); + $this->assertSame( 'Content, title, and excerpt are empty.', $result->get_error_message(), 'The message should be the message of the save error.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A database error during the save keeps its own code and message, with status 500. + * + * @since 7.3.0 + */ + public function test_database_error_is_the_save_error(): void { + global $wpdb; + + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + + $wpdb->suppress_errors = true; + add_filter( + 'query', + static function ( $query ) { + return 0 === strpos( $query, 'UPDATE' ) ? '],' : $query; + } + ); + + $result = $this->update_post( $post_id, array( 'title' => 'Database error' ) ); + + $wpdb->suppress_errors = false; + + $this->assert_error( $result, 'db_update_error', 500, 'A database error' ); + $this->assertSame( 'Could not update post in the database.', $result->get_error_message(), 'The message should be the message of the save error.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written after a database error.' ); + } + + /** + * A stored page template that no longer exists does not fail an unrelated update and is kept. + * + * @since 7.3.0 + */ + public function test_stale_page_template_does_not_fail_an_update(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + update_post_meta( $post_id, '_wp_page_template', 'cw-missing-template.php' ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, array( 'title' => 'Template kept' ) ), $post_id ); + + $this->assertSame( 'Template kept', $post->post_title, 'The title should be stored.' ); + $this->assertSame( 'cw-missing-template.php', get_post_meta( $post_id, '_wp_page_template', true ), 'The stored page template should be kept.' ); + } + + /** + * The after-insert hooks fire once for the updated post, as in any regular save. + * + * A save that stores a revision also fires the hooks for the revision, so only the fires + * for the updated post are counted, and a regular save of an identical post is the reference. + * + * @since 7.3.0 + */ + public function test_after_insert_hooks_fire_once(): void { + $post_id = self::$post_ids['administrator:draft']; + $reference_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'draft', + ) + ); + $fires = array(); + add_action( + 'wp_after_insert_post', + static function ( $id ) use ( &$fires ): void { + $fires[] = (int) $id; + } + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $this->assert_updated( $this->update_post( $post_id, array( 'title' => 'Hooks' ) ), $post_id ); + $ability_fires = $fires; + + $fires = array(); + wp_update_post( + array( + 'ID' => $reference_id, + 'post_title' => 'Hooks', + ) + ); + + $this->assertSame( 1, count( array_keys( $ability_fires, $post_id, true ) ), 'wp_after_insert_post should fire once for the updated post.' ); + $this->assertSame( 1, count( array_keys( $fires, $reference_id, true ) ), 'Precondition: a regular save should fire wp_after_insert_post once for the post.' ); + } + + /** + * Returns an integer in one of the forms that are read as that integer. + * + * @since 7.3.0 + * + * @param int $value The integer. + * @param string $form The form: `float`, `decimal`, `plus`, `leading space`, `trailing space`, + * `leading newline` or `exponent`. + * @return float|string The integer in that form. + */ + private static function whole_number_form( int $value, string $form ) { + $forms = array( + 'float' => (float) $value, + 'decimal' => "{$value}.0", + 'plus' => "+{$value}", + 'leading space' => " {$value}", + 'trailing space' => "{$value} ", + 'leading newline' => "\n{$value}", + 'exponent' => "{$value}e0", + ); + + return $forms[ $form ]; + } + + /** + * Returns the forms of a whole number that are read as the integer they equal. + * + * @return array The forms. + */ + public static function data_whole_number_forms(): array { + return array( + 'whole float' => array( 'float' ), + 'string with .0' => array( 'decimal' ), + 'string with plus' => array( 'plus' ), + 'string with leading space' => array( 'leading space' ), + 'string with trailing space' => array( 'trailing space' ), + 'string with leading line' => array( 'leading newline' ), + 'string with an exponent' => array( 'exponent' ), + ); + } + + /** + * Called directly, an ID in another whole-number form is read as the integer it equals. + * + * Covers S2 (C3). + * + * @dataProvider data_whole_number_forms + * + * @since 7.3.0 + * + * @param string $form The form of the ID. + */ + public function test_direct_call_reads_an_id_in_a_whole_number_form( string $form ): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => self::whole_number_form( $post_id, $form ), + ), + 'data' => array( 'title' => 'Whole number ID' ), + ) + ); + + $this->assertSame( 'Whole number ID', $this->assert_updated( $result, $post_id )->post_title, 'The post with that ID should be updated.' ); + } + + /** + * Called directly, an author in another whole-number form is read as the integer it equals. + * + * Covers C3. + * + * @dataProvider data_whole_number_forms + * + * @since 7.3.0 + * + * @param string $form The form of the author. + */ + public function test_direct_call_reads_an_author_in_a_whole_number_form( string $form ): void { + $post_id = self::$post_ids['editor:draft']; + + $this->login_as( 'editor' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'author' => self::whole_number_form( self::$user_ids['author'], $form ) ), + ) + ); + + $this->assertSame( self::$user_ids['author'], (int) $this->assert_updated( $result, $post_id )->post_author, 'The user with that ID should be the author.' ); + } + + /** + * Called directly, a parent in another whole-number form is read as the integer it equals. + * + * Covers C3. + * + * @dataProvider data_whole_number_forms + * + * @since 7.3.0 + * + * @param string $form The form of the parent. + */ + public function test_direct_call_reads_a_parent_in_a_whole_number_form( string $form ): void { + $post_id = self::$post_ids['writer_page_with_hidden_parent']; + $parent_id = self::$post_ids['admin_page']; + + $this->login_as( 'page_writer' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'page', + 'id' => $post_id, + ), + 'data' => array( 'parent' => self::whole_number_form( $parent_id, $form ) ), + ) + ); + + $this->assertSame( $parent_id, $this->assert_updated( $result, $post_id )->post_parent, 'The page with that ID should be the parent.' ); + } + + /** + * Creates a published post of the administrator and moves it to the Trash. + * + * @since 7.3.0 + * + * @return int The post ID. + */ + private function create_trashed_post(): int { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + ) + ); + wp_trash_post( $post_id ); + + $this->assertSame( 'trash', get_post_status( $post_id ), 'Precondition: the post should be in the Trash.' ); + + return $post_id; + } + + /** + * Returns internal statuses that a post can be stored in. + * + * @return array The statuses. + */ + public static function data_internal_stored_statuses(): array { + return array( + 'trash' => array( 'trash' ), + 'auto-draft' => array( 'auto-draft' ), + ); + } + + /** + * Called directly, an internal status is rejected also for a post that is already stored in + * it, and the post is found whatever its stored status. + * + * Covers S2, S5 and S7 (C1 and C12). + * + * @dataProvider data_internal_stored_statuses + * + * @since 7.3.0 + * + * @param string $status The stored status, which is also sent. + */ + public function test_direct_call_rejects_trash_for_a_post_in_the_trash( string $status ): void { + if ( 'trash' === $status ) { + $post_id = $this->create_trashed_post(); + } else { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => $status, + ) + ); + } + + $this->assertSame( $status, get_post_status( $post_id ), 'Precondition: the post should be stored in the status.' ); + + $this->login_as( 'administrator' ); + + $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should be able to edit the post.' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'status' => $status ), + ) + ); + + $this->assert_invalid_attribute( $result, 'status', 'A direct call with the internal status the post already has' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * `trash` sent for a post in the Trash fails input validation. + * + * Covers S5 (C1). + * + * @since 7.3.0 + */ + public function test_trash_for_a_post_in_the_trash_fails_input_validation(): void { + $post_id = $this->create_trashed_post(); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'status' => 'trash' ) ); + + $this->assertWPError( $result, 'Sending trash for a trashed post should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject trash for a trashed post.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'The trashed post should not change.' ); + } + + /** + * Returns date values without a usable value for a direct call. + * + * @return array}> The dates. + */ + public static function data_dates_without_a_usable_value(): array { + return array( + 'empty date' => array( array() ), + 'GMT that is a number' => array( + array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => 123, + ), + ), + 'GMT that is a list' => array( + array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => array( '2016-12-12T19:00:00' ), + ), + ), + 'GMT that is null' => array( + array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => null, + ), + ), + ); + } + + /** + * Called directly, an empty date or a GMT value of the wrong type is an invalid attribute. + * + * Covers S5 (C2). + * + * @dataProvider data_dates_without_a_usable_value + * + * @since 7.3.0 + * + * @param array $date The date. + */ + public function test_direct_call_rejects_a_date_without_a_usable_value( array $date ): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'date' => $date ), + ) + ); + + $this->assert_invalid_attribute( $result, 'date', 'A direct call with a date without a usable value' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns direct-call inputs whose `data` holds no supported attribute. + * + * @return array}> The inputs, without `where`. + */ + public static function data_inputs_without_usable_data(): array { + return array( + 'data missing' => array( array() ), + 'data that is a string' => array( array( 'data' => 'x' ) ), + 'empty data' => array( array( 'data' => array() ) ), + 'data with only keys outside the list' => array( + array( + 'data' => array( + 'ID' => 1, + 'post_password' => 'secret', + 'post_title' => 'Not an attribute', + ), + ), + ), + ); + } + + /** + * Called directly by a user who may edit the post, an update without a supported attribute is + * an invalid attribute and writes nothing. + * + * Covers S5 and O4 (C7). + * + * @dataProvider data_inputs_without_usable_data + * + * @since 7.3.0 + * + * @param array $input The input, without `where`. + */ + public function test_direct_call_without_usable_data_is_an_invalid_attribute( array $input ): void { + $post_id = self::$post_ids['administrator:draft']; + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $input['where'] = array( + 'type' => 'post', + 'id' => $post_id, + ); + + $before = $this->snapshot( $post_id ); + $result = $execute( $input ); + + $this->assert_error( $result, 'content_invalid_attribute', 400, 'A direct call without usable data' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * An empty excerpt and an empty title are stored on a post that keeps its content. + * + * Covers S5. + * + * @since 7.3.0 + */ + public function test_stores_an_empty_excerpt_and_an_empty_title(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'draft', + 'post_title' => 'Title to clear', + 'post_content' => 'Content to keep.', + 'post_excerpt' => 'Excerpt to clear.', + ) + ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $post = $this->assert_updated( + $this->update_post( + $post_id, + array( + 'excerpt' => '', + 'title' => '', + ) + ), + $post_id + ); + + $this->assertSame( '', $post->post_excerpt, 'The empty excerpt should be stored.' ); + $this->assertSame( '', $post->post_title, 'The empty title should be stored.' ); + $this->assertSame( 'Content to keep.', $post->post_content, 'The content should be kept.' ); + } + + /** + * An author denied `publish_posts` may still move their own draft to `pending`. + * + * Covers S7. + * + * @since 7.3.0 + */ + public function test_author_denied_publish_posts_may_set_pending(): void { + $post_id = self::$post_ids['restricted_author:draft']; + + $this->login_as( 'restricted_author' ); + $this->register_abilities(); + + $this->assertFalse( current_user_can( 'publish_posts' ), 'Precondition: the user should not be able to publish posts.' ); + + $result = $this->update_post( $post_id, array( 'status' => 'pending' ) ); + + $this->assertSame( 'pending', $this->assert_updated( $result, $post_id )->post_status, 'The draft should be pending.' ); + } + + /** + * Creates a published or scheduled post owned by the author denied `publish_posts`. + * + * @since 7.3.0 + * + * @param string $status The status, `publish` or `future`. + * @return int The post ID. + */ + private function create_post_of_the_restricted_author( string $status ): int { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['restricted_author'], + 'post_status' => $status, + 'post_date' => 'future' === $status ? '2035-01-01 00:00:00' : '2020-01-01 00:00:00', + ) + ); + + $this->assertSame( $status, get_post_status( $post_id ), "Precondition: the post should be stored as {$status}." ); + + return $post_id; + } + + /** + * Returns date changes on a published or scheduled post, with the status the save stores. + * + * @return array The cases. + */ + public static function data_date_changes_on_a_published_or_scheduled_post(): array { + $ahead = '2036-06-01T12:00:00'; + $past = '2021-06-01T12:00:00'; + + return array( + 'published post, date ahead' => array( 'publish', false, $ahead, 'future' ), + 'published post, date ahead, stored status' => array( 'publish', true, $ahead, 'future' ), + 'published post, date in the past' => array( 'publish', false, $past, 'publish' ), + 'scheduled post, date in the past' => array( 'future', false, $past, 'publish' ), + 'scheduled post, past date, stored status' => array( 'future', true, $past, 'publish' ), + 'scheduled post, another date ahead' => array( 'future', false, $ahead, 'future' ), + ); + } + + /** + * A date written to a published or scheduled post needs no `publish_posts`. + * + * The call sets no status. The save stores `future` for a date ahead and `publish` + * for any other date, so the date alone can schedule or publish the post. + * + * @dataProvider data_date_changes_on_a_published_or_scheduled_post + * + * @since 7.3.0 + * + * @param string $stored_status The status the post has. + * @param bool $send_status Whether the call also sends the stored status. + * @param string $date The sent GMT date. + * @param string $expected_status The status the save stores. + */ + public function test_date_on_a_published_or_scheduled_post_needs_no_publish_posts( string $stored_status, bool $send_status, string $date, string $expected_status ): void { + $post_id = $this->create_post_of_the_restricted_author( $stored_status ); + + $this->login_as( 'restricted_author' ); + $this->register_abilities(); + + $this->assertFalse( current_user_can( 'publish_posts' ), 'Precondition: the user should not be able to publish posts.' ); + + $data = array( 'date' => array( 'gmt' => $date ) ); + if ( $send_status ) { + $data['status'] = $stored_status; + } + + $result = $this->update_post( $post_id, $data, array( 'status' ) ); + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( str_replace( 'T', ' ', $date ), $post->post_date_gmt, 'The sent date should be stored.' ); + $this->assertSame( $expected_status, $post->post_status, 'The stored status should follow the date.' ); + $this->assertSame( $expected_status, $result['status'], 'The status that follows the date should be returned.' ); + } + + /** + * Returns changes between `publish` and `future` that name the new status. + * + * @return array The cases. + */ + public static function data_named_status_changes_on_a_published_or_scheduled_post(): array { + return array( + 'published post to future, date ahead' => array( 'publish', 'future', '2036-06-01T12:00:00' ), + 'scheduled post to publish, date in the past' => array( 'future', 'publish', '2021-06-01T12:00:00' ), + ); + } + + /** + * A call that names the other status, `future` or `publish`, needs `publish_posts`, + * also when the sent date alone would lead to that status. + * + * @dataProvider data_named_status_changes_on_a_published_or_scheduled_post + * + * @since 7.3.0 + * + * @param string $stored_status The status the post has. + * @param string $status The sent status. + * @param string $date The sent GMT date. + */ + public function test_naming_the_status_a_date_leads_to_needs_publish_posts( string $stored_status, string $status, string $date ): void { + $post_id = $this->create_post_of_the_restricted_author( $stored_status ); + + $this->login_as( 'restricted_author' ); + $this->register_abilities(); + + $this->assertFalse( current_user_can( 'publish_posts' ), 'Precondition: the user should not be able to publish posts.' ); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( + $post_id, + array( + 'status' => $status, + 'date' => array( 'gmt' => $date ), + ) + ); + + $this->assert_error( $result, 'content_cannot_publish', 403, 'A published or scheduled post moved to the other status without publish_posts' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Grants `edit_post` and `read_post` on one post to one user through `map_meta_cap`. + * + * @since 7.3.0 + * + * @param int $user_id The user ID. + * @param int $post_id The post ID. + * @param bool $only_while_draft Whether the grant holds only while the post is a draft. + */ + private function grant_edit_and_read_on_one_post( int $user_id, int $post_id, bool $only_while_draft ): void { + add_filter( + 'map_meta_cap', + static function ( $caps, $cap, $cap_user_id, $args ) use ( $user_id, $post_id, $only_while_draft ) { + if ( + in_array( $cap, array( 'edit_post', 'read_post' ), true ) + && $user_id === (int) $cap_user_id + && isset( $args[0] ) && $post_id === (int) $args[0] + && ( ! $only_while_draft || 'draft' === get_post_status( $post_id ) ) + ) { + return array( 'read' ); + } + + return $caps; + }, + 10, + 4 + ); + } + + /** + * A user with no edit capability for the type who is granted `edit_post` on exactly one post + * may update that post's title, and nothing more. + * + * Covers S4, S5 and S7. + * + * @since 7.3.0 + */ + public function test_edit_post_granted_on_one_post_allows_only_editing_that_post(): void { + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['other'], + 'post_status' => 'draft', + ) + ); + $other_id = self::$post_ids['other:draft']; + + $this->grant_edit_and_read_on_one_post( $user_id, $post_id, false ); + wp_set_current_user( $user_id ); + + $this->assertFalse( current_user_can( 'edit_posts' ), 'Precondition: the user should have no edit capability for the type.' ); + $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should be able to edit the granted post.' ); + $this->assertFalse( current_user_can( 'edit_post', $other_id ), 'Precondition: the user should not be able to edit another post.' ); + + $this->register_abilities(); + + $updated = $this->update_post( $post_id, array( 'title' => 'Granted title' ) ); + + $this->assertSame( 'Granted title', $this->assert_updated( $updated, $post_id )->post_title, 'The title of the granted post should be stored.' ); + + $other_before = $this->snapshot( $other_id ); + $other = $this->update_post( $other_id, array( 'title' => 'Not granted' ) ); + + $this->assert_error( $other, 'content_cannot_edit', 403, 'Updating a post that is not granted' ); + $this->assertSame( $other_before, $this->snapshot( $other_id ), 'Nothing should be written for the post that is not granted.' ); + + $before = $this->snapshot( $post_id ); + $publish = $this->update_post( $post_id, array( 'status' => 'publish' ) ); + + $this->assert_error( $publish, 'content_cannot_publish', 403, 'Publishing the granted post' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for the refused publish.' ); + + $this->assertTrue( wp_has_ability( 'core/content-delete' ), 'The core/content-delete ability should be registered.' ); + + $delete = wp_get_ability( 'core/content-delete' )->execute( + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + ) + ); + + $this->assert_error( $delete, 'content_cannot_delete', 403, 'Deleting the granted post' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for the refused delete.' ); + } + + /** + * Sets the front page, the posts page and the privacy policy page of the site, and returns + * their IDs. + * + * @since 7.3.0 + * + * @return array The page IDs, keyed by fixture name. + */ + private function set_special_pages(): array { + $pages = array(); + foreach ( array( 'front_page', 'posts_page', 'privacy_page' ) as $page ) { + $pages[ $page ] = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + } + + update_option( 'show_on_front', 'page' ); + update_option( 'page_on_front', $pages['front_page'] ); + update_option( 'page_for_posts', $pages['posts_page'] ); + update_option( 'wp_page_for_privacy_policy', $pages['privacy_page'] ); + + return $pages; + } + + /** + * An editor may not update the privacy policy page. + * + * Covers S4 and S5. + * + * @since 7.3.0 + */ + public function test_editor_cannot_update_the_privacy_policy_page(): void { + $post_id = $this->set_special_pages()['privacy_page']; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'title' => 'Privacy' ) ); + + $this->assert_error( $result, 'content_cannot_edit', 403, 'An editor updating the privacy policy page' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns the special pages that an editor may update. + * + * @return array The pages, as fixture names. + */ + public static function data_special_pages_an_editor_may_update(): array { + return array( + 'front page' => array( 'front_page' ), + 'posts page' => array( 'posts_page' ), + ); + } + + /** + * An editor may update the title of the front page and of the posts page. + * + * Covers S4. + * + * @dataProvider data_special_pages_an_editor_may_update + * + * @since 7.3.0 + * + * @param string $page The page, as a fixture name. + */ + public function test_editor_may_update_the_title_of_a_special_page( string $page ): void { + $post_id = $this->set_special_pages()[ $page ]; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'title' => 'Special page' ) ); + + $this->assertSame( 'Special page', $this->assert_updated( $result, $post_id )->post_title, 'The title of the special page should be stored.' ); + } + + /** + * Returns updates of a page whose stored parent no longer exists. + * + * @return array The cases. + */ + public static function data_updates_of_a_page_with_a_missing_parent(): array { + return array( + 'parent equal to the stored parent' => array( true ), + 'title only' => array( false ), + ); + } + + /** + * A page whose stored parent no longer exists can be updated, and keeps that parent. + * + * Covers S9. + * + * @dataProvider data_updates_of_a_page_with_a_missing_parent + * + * @since 7.3.0 + * + * @param bool $send_parent Whether the stored parent is sent. + */ + public function test_page_with_a_missing_parent_can_be_updated( bool $send_parent ): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['page_writer'], + 'post_type' => 'page', + 'post_status' => 'draft', + 'post_parent' => self::MISSING_ID, + ) + ); + + $this->assertSame( self::MISSING_ID, get_post( $post_id )->post_parent, 'Precondition: the page should keep the missing parent.' ); + $this->assertNull( get_post( self::MISSING_ID ), 'Precondition: the parent should not exist.' ); + + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $data = array( 'title' => 'Orphan' ); + if ( $send_parent ) { + $data['parent'] = self::MISSING_ID; + } + + $post = $this->assert_updated( $this->update_post( $post_id, $data ), $post_id ); + + $this->assertSame( 'Orphan', $post->post_title, 'The title should be stored.' ); + $this->assertSame( self::MISSING_ID, $post->post_parent, 'The stored parent should not change.' ); + } + + /** + * Registers a custom public, private and hidden status, before the abilities are registered. + * + * @since 7.3.0 + */ + private function register_custom_statuses(): void { + register_post_status( + 'cw_public', + array( + 'label' => 'Public', + 'public' => true, + ) + ); + register_post_status( + 'cw_private', + array( + 'label' => 'Private', + 'private' => true, + ) + ); + register_post_status( + 'cw_hidden', + array( + 'label' => 'Hidden', + 'public' => false, + 'protected' => true, + ) + ); + + $this->assertFalse( get_post_status_object( 'cw_hidden' )->internal, 'Precondition: the hidden status should not be internal.' ); + $this->assertFalse( is_post_status_viewable( 'cw_hidden' ), 'Precondition: the hidden status should not be publicly viewable.' ); + } + + /** + * A contributor may set a hidden custom status on their own draft without `publish_posts`. + * + * Covers S7 (C4). + * + * @since 7.3.0 + */ + public function test_contributor_may_set_a_hidden_custom_status(): void { + $this->register_custom_statuses(); + $post_id = self::$post_ids['contributor:draft']; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'status' => 'cw_hidden' ) ); + + $this->assertSame( 'cw_hidden', $this->assert_updated( $result, $post_id )->post_status, 'The hidden status should be stored.' ); + } + + /** + * Returns custom statuses that make a post visible, with the stored status of the post. + * + * @return array The cases. + */ + public static function data_custom_visible_statuses_by_stored_status(): array { + return array( + 'public from a draft' => array( 'cw_public', 'draft' ), + 'private from a draft' => array( 'cw_private', 'draft' ), + 'public from pending' => array( 'cw_public', 'pending' ), + 'private from pending' => array( 'cw_private', 'pending' ), + 'public from the Trash' => array( 'cw_public', 'trash_draft' ), + 'private from the Trash' => array( 'cw_private', 'trash_draft' ), + ); + } + + /** + * A contributor may not set a custom public or private status on their own post, and the + * refusal uses the general message. + * + * Covers S5 and S7 (C6). + * + * @dataProvider data_custom_visible_statuses_by_stored_status + * + * @since 7.3.0 + * + * @param string $status The status. + * @param string $stored The matrix status of the post. + */ + public function test_contributor_cannot_set_a_custom_visible_status( string $status, string $stored ): void { + $this->register_custom_statuses(); + $post_id = self::$post_ids[ "contributor:{$stored}" ]; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'status' => $status ) ); + + $this->assert_error( $result, 'content_cannot_publish', 403, 'A contributor setting a custom visible status' ); + $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $result->get_error_message(), 'A custom status, also a private one, should get the general message.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * When the update takes away the caller's read access, the update is stored and the output + * is the ID alone, whatever the fields ask for. + * + * Covers S11 (C14). + * + * @since 7.3.0 + */ + public function test_output_is_the_id_alone_when_the_update_takes_away_read_access(): void { + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['other'], + 'post_status' => 'draft', + ) + ); + + $this->grant_edit_and_read_on_one_post( $user_id, $post_id, true ); + wp_set_current_user( $user_id ); + + $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should be able to edit the draft.' ); + + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'status' => 'pending', + 'title' => 'Out of reach', + ), + array( 'title.rendered', 'status', 'content.rendered' ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'pending', $post->post_status, 'The new status should be stored.' ); + $this->assertSame( 'Out of reach', $post->post_title, 'The new title should be stored.' ); + $this->assertFalse( current_user_can( 'read_post', $post_id ), 'Precondition: the user should no longer be able to read the post.' ); + $this->assertSame( array( 'id' => $post_id ), $result, 'The output should be the ID alone.' ); + } + + /** + * When the caller may still read the post after the update, the output holds the requested + * fields. + * + * Covers S11 (C14). + * + * @since 7.3.0 + */ + public function test_output_is_complete_when_the_caller_may_still_read_the_post(): void { + $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['other'], + 'post_status' => 'draft', + ) + ); + + $this->grant_edit_and_read_on_one_post( $user_id, $post_id, true ); + wp_set_current_user( $user_id ); + $this->register_abilities(); + + $result = $this->update_post( $post_id, array( 'title' => 'Still a draft' ), array( 'title.rendered', 'status' ) ); + + $this->assert_updated( $result, $post_id ); + $this->assertTrue( current_user_can( 'read_post', $post_id ), 'Precondition: the user should still be able to read the post.' ); + + ksort( $result ); + + $this->assertSame( + array( + 'id' => $post_id, + 'status' => 'draft', + 'title' => array( 'rendered' => 'Still a draft' ), + ), + $result, + 'The output should hold the ID and the requested fields.' + ); + } +} diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index 0374d38eb37f4..416d92296713e 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -3,7 +3,7 @@ declare( strict_types=1 ); /** - * Tests dispatching the core/content-query ability through the Abilities REST run endpoint. + * Tests dispatching the content abilities through the Abilities REST run endpoint. * * @covers WP_Abilities_Content * @@ -408,4 +408,531 @@ public function test_input_without_where_returns_400(): void { $this->assertSame( 400, $response->get_status(), 'Input without where should be rejected.' ); $this->assertSame( 'ability_invalid_input', $response->get_data()['code'], 'The error should come from input validation.' ); } + + /** + * Builds a run request for a content write ability. + * + * A POST request carries the input in a JSON body, any other request in the query string. + * + * @since 7.3.0 + * + * @param string $method The HTTP method. + * @param string $ability The ability name. + * @param array $input The ability input. + * @return WP_REST_Request The request. + */ + private function write_request( string $method, string $ability, array $input ): WP_REST_Request { + $request = new WP_REST_Request( $method, "/wp-abilities/v1/abilities/{$ability}/run" ); + + if ( 'POST' === $method ) { + $request->set_header( 'Content-Type', 'application/json' ); + $request->set_body( wp_json_encode( array( 'input' => $input ) ) ); + } else { + $request->set_query_params( array( 'input' => $input ) ); + } + + return $request; + } + + /** + * Asserts that a response is an error with the given status and code. + * + * @since 7.3.0 + * + * @param WP_REST_Response $response The response. + * @param int $status The expected status. + * @param string $code The expected error code. + * @param string $context What the request was, for the assertion messages. + */ + private function assert_error_response( WP_REST_Response $response, int $status, string $code, string $context ): void { + $this->assertSame( $status, $response->get_status(), "{$context}: the status should be {$status}." ); + $this->assertSame( $code, $response->get_data()['code'] ?? null, "{$context}: the error code should be {$code}." ); + } + + /** + * Takes a snapshot of the stored content: the number of posts per type and status, the + * number of meta rows and, when given, the row and meta of one post. + * + * @since 7.3.0 + * + * @param int $post_id Optional. The post ID. Default 0. + * @return array The snapshot. + */ + private function snapshot( int $post_id = 0 ): array { + global $wpdb; + + return array( + 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), + 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), + 'post' => $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id ), ARRAY_A ), + 'post_meta' => $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d ORDER BY meta_id", $post_id ), ARRAY_A ), + ); + } + + /** + * Create runs with POST and the input in a JSON body. + * + * @since 7.3.0 + */ + public function test_create_runs_with_post_and_a_json_body(): void { + $response = $this->server->dispatch( + $this->write_request( + 'POST', + 'core/content-create', + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Over the run route', + ), + 'fields' => array( 'status', 'title.rendered' ), + ) + ) + ); + + $this->assertSame( 200, $response->get_status(), 'The create request should succeed.' ); + + $data = $response->get_data(); + + $this->assertSame( array( 'id', 'status', 'title' ), array_keys( $data ), 'The created post should be returned in the chosen fields.' ); + $this->assertSame( 'draft', $data['status'], 'The created post should be a draft.' ); + $this->assertSame( 'Over the run route', get_post( $data['id'] )->post_title, 'The created post should be stored.' ); + } + + /** + * Update runs with POST and the input in a JSON body. + * + * @since 7.3.0 + */ + public function test_update_runs_with_post_and_a_json_body(): void { + $post_id = self::factory()->post->create( array( 'post_status' => 'draft' ) ); + + $response = $this->server->dispatch( + $this->write_request( + 'POST', + 'core/content-update', + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Updated over the run route' ), + 'fields' => array( 'title.rendered' ), + ) + ) + ); + + $this->assertSame( 200, $response->get_status(), 'The update request should succeed.' ); + $this->assertSame( + array( + 'id' => $post_id, + 'title' => array( 'rendered' => 'Updated over the run route' ), + ), + $response->get_data(), + 'The updated post should be returned in the chosen fields.' + ); + } + + /** + * Delete runs with DELETE and the input in the query string, where the ID is a string. + * + * @since 7.3.0 + */ + public function test_delete_runs_with_delete_and_the_input_in_the_query_string(): void { + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + + $response = $this->server->dispatch( + $this->write_request( + 'DELETE', + 'core/content-delete', + array( + 'where' => array( + 'type' => 'post', + 'id' => (string) $post_id, + ), + ) + ) + ); + + $this->assertSame( 200, $response->get_status(), 'The delete request should succeed.' ); + $this->assertSame( array( 'outcome' => 'trashed' ), $response->get_data(), 'The post should be reported as trashed.' ); + $this->assertSame( 'trash', get_post_status( $post_id ), 'The post should be in the Trash.' ); + } + + /** + * Returns `permanent` values as they arrive in a query string, and the outcome. + * + * @return array The cases. + */ + public static function data_permanent_query_string_values(): array { + return array( + 'true' => array( 'true', 'deleted' ), + '1' => array( '1', 'deleted' ), + 'false' => array( 'false', 'trashed' ), + '0' => array( '0', 'trashed' ), + 'upper case' => array( 'TRUE', 'deleted' ), + ); + } + + /** + * `permanent` in the query string is read as a boolean. + * + * @dataProvider data_permanent_query_string_values + * + * @since 7.3.0 + * + * @param string $permanent The value in the query string. + * @param string $outcome The expected outcome. + */ + public function test_delete_reads_permanent_from_the_query_string( string $permanent, string $outcome ): void { + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + + $response = $this->server->dispatch( + $this->write_request( + 'DELETE', + 'core/content-delete', + array( + 'where' => array( + 'type' => 'post', + 'id' => (string) $post_id, + ), + 'permanent' => $permanent, + ) + ) + ); + + $this->assertSame( 200, $response->get_status(), 'The delete request should succeed.' ); + $this->assertSame( array( 'outcome' => $outcome ), $response->get_data(), 'The outcome should follow the permanent value.' ); + + clean_post_cache( $post_id ); + + if ( 'deleted' === $outcome ) { + $this->assertNull( get_post( $post_id ), 'The post should no longer exist.' ); + } else { + $this->assertSame( 'trash', get_post_status( $post_id ), 'The post should be in the Trash.' ); + } + } + + /** + * Returns `permanent` values in a query string that are not booleans. + * + * @return array The values. + */ + public static function data_permanent_query_string_values_that_are_not_booleans(): array { + return array( + 'yes' => array( 'yes' ), + '2' => array( '2' ), + 'on' => array( 'on' ), + 'empty string' => array( '' ), + ); + } + + /** + * A `permanent` value in the query string that is not a boolean fails input validation, and + * the post is unchanged. + * + * Covers S12 (C5). + * + * @dataProvider data_permanent_query_string_values_that_are_not_booleans + * + * @since 7.3.0 + * + * @param string $permanent The value in the query string. + */ + public function test_delete_with_a_permanent_value_that_is_not_a_boolean_returns_400( string $permanent ): void { + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + + $before = $this->snapshot( $post_id ); + $response = $this->server->dispatch( + $this->write_request( + 'DELETE', + 'core/content-delete', + array( + 'where' => array( + 'type' => 'post', + 'id' => (string) $post_id, + ), + 'permanent' => $permanent, + ) + ) + ); + + $this->assert_error_response( $response, 400, 'ability_invalid_input', 'A permanent value that is not a boolean' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'The post should not change.' ); + } + + /** + * A create sent with POST but with the input in the query string fails input validation. + * + * Covers C5. + * + * @since 7.3.0 + */ + public function test_create_with_the_input_in_the_query_string_returns_400(): void { + $request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/core/content-create/run' ); + $request->set_query_params( + array( + 'input' => array( + 'data' => array( + 'type' => 'post', + 'title' => 'Misplaced input', + ), + ), + ) + ); + + $before = $this->snapshot(); + $response = $this->server->dispatch( $request ); + + $this->assert_error_response( $response, 400, 'ability_invalid_input', 'A create with the input in the query string' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * A delete sent with DELETE but with the input in a JSON body fails input validation. + * + * Covers C5. + * + * @since 7.3.0 + */ + public function test_delete_with_the_input_in_a_json_body_returns_400(): void { + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + + $request = new WP_REST_Request( 'DELETE', '/wp-abilities/v1/abilities/core/content-delete/run' ); + $request->set_header( 'Content-Type', 'application/json' ); + $request->set_body( + wp_json_encode( + array( + 'input' => array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + ), + ) + ) + ); + + $before = $this->snapshot( $post_id ); + $response = $this->server->dispatch( $request ); + + $this->assert_error_response( $response, 400, 'ability_invalid_input', 'A delete with the input in a JSON body' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'The post should not change.' ); + } + + /** + * Returns write abilities with a method they do not take. + * + * @return array The cases. + */ + public static function data_wrong_methods_for_write_abilities(): array { + return array( + 'GET to create' => array( 'core/content-create', 'GET' ), + 'DELETE to create' => array( 'core/content-create', 'DELETE' ), + 'GET to update' => array( 'core/content-update', 'GET' ), + 'DELETE to update' => array( 'core/content-update', 'DELETE' ), + 'GET to delete' => array( 'core/content-delete', 'GET' ), + 'POST to delete' => array( 'core/content-delete', 'POST' ), + ); + } + + /** + * A write ability called with a method it does not take is rejected and writes nothing. + * + * @dataProvider data_wrong_methods_for_write_abilities + * + * @since 7.3.0 + * + * @param string $ability The ability name. + * @param string $method The HTTP method. + */ + public function test_write_ability_with_a_wrong_method_returns_405( string $ability, string $method ): void { + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + $inputs = array( + 'core/content-create' => array( 'data' => array( 'type' => 'post' ) ), + 'core/content-update' => array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Wrong method' ), + ), + 'core/content-delete' => array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + ), + ); + $before = get_post( $post_id )->to_array(); + + $response = $this->server->dispatch( $this->write_request( $method, $ability, $inputs[ $ability ] ) ); + + $this->assert_error_response( $response, 405, 'rest_ability_invalid_method', 'A wrong method' ); + $this->assertSame( $before, get_post( $post_id )->to_array(), 'The post should not change.' ); + } + + /** + * A logged-out user is refused as unauthenticated. + * + * @since 7.3.0 + */ + public function test_logged_out_create_receives_401(): void { + wp_set_current_user( 0 ); + + $before = $this->snapshot(); + $response = $this->server->dispatch( $this->write_request( 'POST', 'core/content-create', array( 'data' => array( 'type' => 'post' ) ) ) ); + + $this->assert_error_response( $response, 401, 'rest_ability_cannot_execute', 'A logged-out create' ); + $this->assertSame( $before, $this->snapshot(), 'A logged-out create should write nothing.' ); + } + + /** + * A user the permission callback refuses is forbidden. + * + * @since 7.3.0 + */ + public function test_subscriber_create_receives_403(): void { + wp_set_current_user( self::$subscriber_id ); + + $before = $this->snapshot(); + $response = $this->server->dispatch( $this->write_request( 'POST', 'core/content-create', array( 'data' => array( 'type' => 'post' ) ) ) ); + + $this->assert_error_response( $response, 403, 'rest_ability_cannot_execute', 'A subscriber create' ); + $this->assertSame( $before, $this->snapshot(), 'A subscriber create should write nothing.' ); + } + + /** + * Input that fails the schema is rejected. + * + * @since 7.3.0 + */ + public function test_create_with_input_that_fails_the_schema_returns_400(): void { + $before = $this->snapshot(); + $response = $this->server->dispatch( + $this->write_request( + 'POST', + 'core/content-create', + array( + 'data' => array( + 'type' => 'post', + 'post_password' => 'secret', + ), + ) + ) + ); + + $this->assert_error_response( $response, 400, 'ability_invalid_input', 'A create that fails the schema' ); + $this->assertSame( $before, $this->snapshot(), 'A create that fails the schema should write nothing.' ); + } + + /** + * The not-found error of an update reaches the response with its status. + * + * @since 7.3.0 + */ + public function test_update_of_a_missing_post_returns_404(): void { + $before = $this->snapshot(); + $response = $this->server->dispatch( + $this->write_request( + 'POST', + 'core/content-update', + array( + 'where' => array( + 'type' => 'post', + 'id' => 987654321, + ), + 'data' => array( 'title' => 'Missing' ), + ) + ) + ); + + $this->assert_error_response( $response, 404, 'content_not_found', 'An update of a missing post' ); + $this->assertSame( $before, $this->snapshot(), 'An update of a missing post should write nothing.' ); + } + + /** + * The edit error of an update reaches the response with its status. + * + * @since 7.3.0 + */ + public function test_update_of_a_post_the_user_cannot_edit_returns_403(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$admin_id, + 'post_status' => 'publish', + ) + ); + wp_set_current_user( self::factory()->user->create( array( 'role' => 'contributor' ) ) ); + + $before = $this->snapshot( $post_id ); + $response = $this->server->dispatch( + $this->write_request( + 'POST', + 'core/content-update', + array( + 'where' => array( + 'type' => 'post', + 'id' => $post_id, + ), + 'data' => array( 'title' => 'Not mine' ), + ) + ) + ); + + $this->assert_error_response( $response, 403, 'content_cannot_edit', 'An update of a post the user cannot edit' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'The post the user cannot edit should not change.' ); + } + + /** + * The invalid attribute error of a create reaches the response with its status. + * + * @since 7.3.0 + */ + public function test_create_with_an_unsupported_attribute_returns_400(): void { + $before = $this->snapshot(); + $response = $this->server->dispatch( + $this->write_request( + 'POST', + 'core/content-create', + array( + 'data' => array( + 'type' => 'post', + 'title' => 'With a parent', + 'parent' => 1, + ), + ) + ) + ); + + $this->assert_error_response( $response, 400, 'content_invalid_attribute', 'A create with an unsupported attribute' ); + $this->assertSame( array( 'data[parent]' => 'The post type is not hierarchical.' ), $response->get_data()['data']['params'] ?? null, 'The params should reach the response.' ); + $this->assertSame( $before, $this->snapshot(), 'A create with an unsupported attribute should write nothing.' ); + } + + /** + * The already-trashed error of a delete reaches the response with its status. + * + * @since 7.3.0 + */ + public function test_trashing_a_trashed_post_returns_410(): void { + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + wp_trash_post( $post_id ); + + $before = $this->snapshot( $post_id ); + $response = $this->server->dispatch( + $this->write_request( + 'DELETE', + 'core/content-delete', + array( + 'where' => array( + 'type' => 'post', + 'id' => (string) $post_id, + ), + ) + ) + ); + + $this->assert_error_response( $response, 410, 'content_already_trashed', 'Trashing a trashed post' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'The trashed post should stay as it was.' ); + } } From 9157feb857b65268c709a207ae58ec38ce61fff2 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 16 Nov 2026 15:22:52 +0000 Subject: [PATCH 02/24] Abilities API: Add the ticket number to the content write ability tests. --- .../wpRegisterCoreContentAbility.php | 7 ++ .../wpRegisterCoreContentCreateAbility.php | 65 ++++++++++++++ .../wpRegisterCoreContentDeleteAbility.php | 32 +++++++ .../wpRegisterCoreContentUpdateAbility.php | 87 +++++++++++++++++++ .../wpRestAbilitiesContentController.php | 15 ++++ 5 files changed, 206 insertions(+) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index 31ddeaa7d64f3..a3fea85a427eb 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -6876,6 +6876,7 @@ private function query_ids( array $where ): array { * Covers S14. * * @dataProvider data_roles_with_edit_access_to_other_users_posts + * @ticket 66706 * @since 7.3.0 * * @param string $role The role to query as. @@ -6903,6 +6904,7 @@ public function test_trash_requested_by_name_returns_trashed_posts( string $role * * Covers S14. * + * @ticket 66706 * @since 7.3.0 */ public function test_trash_can_be_requested_next_to_another_status(): void { @@ -6927,6 +6929,7 @@ public function test_trash_can_be_requested_next_to_another_status(): void { /** * A query without a status does not cover `trash`, also not when it lists slugs. * + * @ticket 66706 * @since 7.3.0 */ public function test_slugs_without_a_status_do_not_cover_trashed_posts(): void { @@ -6951,6 +6954,7 @@ public function test_slugs_without_a_status_do_not_cover_trashed_posts(): void { /** * A query without a status does not cover `trash`. * + * @ticket 66706 * @since 7.3.0 */ public function test_query_without_a_status_does_not_cover_trashed_posts(): void { @@ -6967,6 +6971,7 @@ public function test_query_without_a_status_does_not_cover_trashed_posts(): void * * Covers S14. * + * @ticket 66706 * @since 7.3.0 */ public function test_trash_needs_edit_posts(): void { @@ -7007,6 +7012,7 @@ public static function data_roles_that_edit_only_their_own_trashed_posts(): arra * Covers S14. * * @dataProvider data_roles_that_edit_only_their_own_trashed_posts + * @ticket 66706 * @since 7.3.0 * * @param string $role The role to query as. @@ -7040,6 +7046,7 @@ public function test_trash_returns_only_the_trashed_posts_the_user_may_read( str /** * Other internal statuses are still rejected. * + * @ticket 66706 * @since 7.3.0 */ public function test_other_internal_statuses_are_still_rejected(): void { diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index 6fc3e729d35c3..4ec3bada4e082 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -389,6 +389,7 @@ private function register_exposed_post_type( string $name, array $supports ): vo /** * The ability is registered in the content category with its label and annotations. * + * @ticket 66706 * @since 7.3.0 */ public function test_registers_the_ability(): void { @@ -410,6 +411,7 @@ public function test_registers_the_ability(): void { /** * The ability is not registered when no post type is exposed to abilities. * + * @ticket 66706 * @since 7.3.0 */ public function test_is_not_registered_without_exposed_post_types(): void { @@ -425,6 +427,7 @@ public function test_is_not_registered_without_exposed_post_types(): void { /** * The input schema takes `data`, with the post type and the allowed attributes, and `fields`. * + * @ticket 66706 * @since 7.3.0 */ public function test_input_schema(): void { @@ -489,6 +492,7 @@ public function test_input_schema(): void { * * Covers S11. * + * @ticket 66706 * @since 7.3.0 */ public function test_output_schema_is_a_query_item(): void { @@ -508,6 +512,7 @@ public function test_output_schema_is_a_query_item(): void { /** * The schema enums are built from the site when the ability is registered. * + * @ticket 66706 * @since 7.3.0 */ public function test_schema_enums_list_the_exposed_post_types_and_registered_statuses(): void { @@ -535,6 +540,7 @@ public function test_schema_enums_list_the_exposed_post_types_and_registered_sta /** * Example 1: a draft with the default fields. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_creates_a_draft_with_the_default_fields(): void { @@ -567,6 +573,7 @@ public function test_example_creates_a_draft_with_the_default_fields(): void { /** * Example 2: a published child page with chosen fields. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_creates_a_published_child_page_with_chosen_fields(): void { @@ -645,6 +652,7 @@ private function query_item( string $type, int $post_id, array $fields ): array * * Covers S1 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_rejects_a_parent_for_a_post_type_that_is_not_hierarchical(): void { @@ -694,6 +702,7 @@ public static function data_users_refused_by_the_permission_callback(): array { * * @dataProvider data_users_refused_by_the_permission_callback * + * @ticket 66706 * @since 7.3.0 * * @param string|null $role The role to log in as, or null to stay logged out. @@ -761,6 +770,7 @@ public static function data_inputs_refused_by_the_permission_callback(): array { * * @dataProvider data_inputs_refused_by_the_permission_callback * + * @ticket 66706 * @since 7.3.0 * * @param mixed $input The input. @@ -776,6 +786,7 @@ public function test_permission_callback_returns_false_for_malformed_input( $inp /** * The permission callback returns true for a user who may create posts of an exposed type. * + * @ticket 66706 * @since 7.3.0 */ public function test_permission_callback_returns_true_for_a_user_who_may_create_posts(): void { @@ -833,6 +844,7 @@ public static function data_create_by_role_and_status(): array { * * @dataProvider data_create_by_role_and_status * + * @ticket 66706 * @since 7.3.0 * * @param string $role The role to create as. @@ -883,6 +895,7 @@ public function test_create_by_role_and_status( string $role, ?string $status, s * * Covers S3 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_type_no_longer_exposed_cannot_be_created(): void { @@ -971,6 +984,7 @@ public static function data_direct_inputs_without_an_exposed_type(): array { * * @dataProvider data_direct_inputs_without_an_exposed_type * + * @ticket 66706 * @since 7.3.0 * * @param mixed $input The input. @@ -1052,6 +1066,7 @@ public static function data_invalid_data(): array { * * @dataProvider data_invalid_data * + * @ticket 66706 * @since 7.3.0 * * @param array $data The data. @@ -1103,6 +1118,7 @@ public static function data_keys_outside_the_allow_list(): array { * * @dataProvider data_keys_outside_the_allow_list * + * @ticket 66706 * @since 7.3.0 * * @param string $key The key. @@ -1133,6 +1149,7 @@ public function test_key_outside_the_allow_list_fails_input_validation( string $ * * Covers S6. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_ignores_keys_outside_the_allow_list(): void { @@ -1221,6 +1238,7 @@ public static function data_statuses_that_can_never_be_set(): array { * * @dataProvider data_statuses_that_can_never_be_set * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -1252,6 +1270,7 @@ public function test_status_that_can_never_be_set_fails_input_validation( string * * @dataProvider data_statuses_that_can_never_be_set * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -1280,6 +1299,7 @@ public function test_direct_call_rejects_a_status_that_can_never_be_set( string * * Covers S1, S5 and S7. * + * @ticket 66706 * @since 7.3.0 */ public function test_status_no_longer_registered_is_rejected(): void { @@ -1373,6 +1393,7 @@ public static function data_custom_statuses_that_make_a_post_visible(): array { * * @dataProvider data_custom_statuses_that_make_a_post_visible * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -1406,6 +1427,7 @@ public function test_custom_visible_status_needs_publish_posts( string $status ) * * @dataProvider data_custom_statuses_that_make_a_post_visible * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -1435,6 +1457,7 @@ public function test_custom_visible_status_is_set_with_publish_posts( string $st * * Covers S7. * + * @ticket 66706 * @since 7.3.0 */ public function test_custom_status_that_keeps_a_post_hidden_needs_no_publish_posts(): void { @@ -1473,6 +1496,7 @@ public static function data_statuses_reported_as_stored(): array { * * @dataProvider data_statuses_reported_as_stored * + * @ticket 66706 * @since 7.3.0 * * @param string $status The requested status. @@ -1519,6 +1543,7 @@ public static function data_authors_named_without_edit_others_posts(): array { * * @dataProvider data_authors_named_without_edit_others_posts * + * @ticket 66706 * @since 7.3.0 * * @param string $author The author, as a fixture name. @@ -1558,6 +1583,7 @@ public function test_naming_another_author_without_edit_others_posts_is_refused( /** * A user without `edit_others_posts` may name themselves as the author. * + * @ticket 66706 * @since 7.3.0 */ public function test_naming_oneself_as_the_author_needs_no_edit_others_posts(): void { @@ -1580,6 +1606,7 @@ public function test_naming_oneself_as_the_author_needs_no_edit_others_posts(): /** * A user with `edit_others_posts` may name another existing user as the author. * + * @ticket 66706 * @since 7.3.0 */ public function test_naming_another_author_with_edit_others_posts(): void { @@ -1604,6 +1631,7 @@ public function test_naming_another_author_with_edit_others_posts(): void { * * Covers S5 and S8. * + * @ticket 66706 * @since 7.3.0 */ public function test_naming_a_missing_author_with_edit_others_posts_is_an_invalid_author(): void { @@ -1632,6 +1660,7 @@ public function test_naming_a_missing_author_with_edit_others_posts_is_an_invali * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_invalid_author(): void { @@ -1666,6 +1695,7 @@ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_inva * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_naming_a_non_member_without_edit_others_posts_is_refused_like_a_missing_user(): void { @@ -1721,6 +1751,7 @@ public static function data_parents_that_cannot_be_used(): array { * * @dataProvider data_parents_that_cannot_be_used * + * @ticket 66706 * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -1756,6 +1787,7 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin /** * A readable page of the same type can be the parent, and `0` makes a top-level page. * + * @ticket 66706 * @since 7.3.0 */ public function test_parent_is_stored(): void { @@ -1813,6 +1845,7 @@ public static function data_unsupported_attributes(): array { * * @dataProvider data_unsupported_attributes * + * @ticket 66706 * @since 7.3.0 * * @param string $type The post type. @@ -1880,6 +1913,7 @@ public static function data_values_of_the_wrong_type(): array { * * @dataProvider data_values_of_the_wrong_type * + * @ticket 66706 * @since 7.3.0 * * @param string $type The post type. @@ -1906,6 +1940,7 @@ public function test_direct_call_rejects_a_value_of_the_wrong_type( string $type /** * An empty title is valid next to other content. * + * @ticket 66706 * @since 7.3.0 */ public function test_empty_title_is_valid(): void { @@ -1933,6 +1968,7 @@ public function test_empty_title_is_valid(): void { * * Covers S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_empty_post_is_the_save_error(): void { @@ -1959,6 +1995,7 @@ public function test_empty_post_is_the_save_error(): void { /** * A database error during the save keeps its own code and message, with status 500. * + * @ticket 66706 * @since 7.3.0 */ public function test_database_error_is_the_save_error(): void { @@ -1996,6 +2033,7 @@ static function ( $query ) { /** * The after-insert hooks fire once, as in any regular save. * + * @ticket 66706 * @since 7.3.0 */ public function test_after_insert_hooks_fire_once(): void { @@ -2036,6 +2074,7 @@ public static function data_slugs(): array { * * @dataProvider data_slugs * + * @ticket 66706 * @since 7.3.0 * * @param string $slug The sent slug. @@ -2065,6 +2104,7 @@ public function test_slug_is_sanitized_and_stored( string $slug, string $expecte * * Covers S1 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { @@ -2105,6 +2145,7 @@ public static function data_statuses_with_unique_slugs(): array { * * @dataProvider data_statuses_with_unique_slugs * + * @ticket 66706 * @since 7.3.0 * * @param string|null $status The status, or null to send none. @@ -2144,6 +2185,7 @@ public static function data_draft_page_slugs_by_parent(): array { * * @dataProvider data_draft_page_slugs_by_parent * + * @ticket 66706 * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -2194,6 +2236,7 @@ public static function data_dates(): array { * * @dataProvider data_dates * + * @ticket 66706 * @since 7.3.0 * * @param array $date The sent date. @@ -2226,6 +2269,7 @@ public function test_date_is_stored_local_and_gmt( array $date, string $local, s /** * A draft created without a date has a floating date. * + * @ticket 66706 * @since 7.3.0 */ public function test_draft_without_a_date_floats(): void { @@ -2251,6 +2295,7 @@ public function test_draft_without_a_date_floats(): void { * * Covers S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_date_that_is_not_a_date_time_fails_input_validation(): void { @@ -2348,6 +2393,7 @@ private function create_with_markup( string $markup ): array { * * @dataProvider data_filtered_markup * + * @ticket 66706 * @since 7.3.0 * * @param string $input The sent markup. @@ -2376,6 +2422,7 @@ public function test_markup_of_an_author_is_filtered( string $input, string $tit * * Covers S10. * + * @ticket 66706 * @since 7.3.0 */ public function test_markup_of_a_contributor_is_filtered(): void { @@ -2395,6 +2442,7 @@ public function test_markup_of_a_contributor_is_filtered(): void { * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_markup_of_a_site_administrator_is_filtered_on_multisite(): void { @@ -2431,6 +2479,7 @@ private function assert_markup_is_stored_unchanged(): void { * * @group ms-excluded * + * @ticket 66706 * @since 7.3.0 */ public function test_markup_of_an_administrator_is_not_filtered(): void { @@ -2446,6 +2495,7 @@ public function test_markup_of_an_administrator_is_not_filtered(): void { * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_markup_of_a_super_admin_is_not_filtered(): void { @@ -2459,6 +2509,7 @@ public function test_markup_of_a_super_admin_is_not_filtered(): void { /** * Quotes, backslashes and non-ASCII text survive a round trip unchanged. * + * @ticket 66706 * @since 7.3.0 */ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { @@ -2496,6 +2547,7 @@ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip( * * Covers S11. * + * @ticket 66706 * @since 7.3.0 */ public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new_post(): void { @@ -2527,6 +2579,7 @@ public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new * * Covers S4. * + * @ticket 66706 * @since 7.3.0 */ public function test_each_call_checks_the_current_user(): void { @@ -2555,6 +2608,7 @@ public function test_each_call_checks_the_current_user(): void { * * Covers S11. * + * @ticket 66706 * @since 7.3.0 */ public function test_returns_the_requested_fields_and_the_id(): void { @@ -2637,6 +2691,7 @@ public static function data_whole_number_forms(): array { * * @dataProvider data_whole_number_forms * + * @ticket 66706 * @since 7.3.0 * * @param string $form The form of the author. @@ -2665,6 +2720,7 @@ public function test_direct_call_reads_an_author_in_a_whole_number_form( string * * @dataProvider data_whole_number_forms * + * @ticket 66706 * @since 7.3.0 * * @param string $form The form of the parent. @@ -2724,6 +2780,7 @@ public static function data_dates_without_a_usable_value(): array { * * @dataProvider data_dates_without_a_usable_value * + * @ticket 66706 * @since 7.3.0 * * @param array $date The date. @@ -2752,6 +2809,7 @@ public function test_direct_call_rejects_a_date_without_a_usable_value( array $d * * Covers S5 and S7. * + * @ticket 66706 * @since 7.3.0 */ public function test_private_post_needs_publish_posts_from_a_user_who_may_create_posts(): void { @@ -2786,6 +2844,7 @@ public function test_private_post_needs_publish_posts_from_a_user_who_may_create * * Covers S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_pending_post_of_a_contributor_without_a_date_floats(): void { @@ -2814,6 +2873,7 @@ public function test_pending_post_of_a_contributor_without_a_date_floats(): void * * Covers S3 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_type_exposed_after_registration_fails_input_validation(): void { @@ -2875,6 +2935,7 @@ private function register_type_with_its_own_create_capability(): void { * * Covers S5 and S7. * + * @ticket 66706 * @since 7.3.0 */ public function test_user_with_only_the_create_capability_creates_a_draft_and_cannot_publish(): void { @@ -2924,6 +2985,7 @@ public function test_user_with_only_the_create_capability_creates_a_draft_and_ca * * Covers S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_with_only_the_edit_capability_cannot_create(): void { @@ -2959,6 +3021,7 @@ public function test_direct_call_with_only_the_edit_capability_cannot_create(): * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_editor_of_the_first_site_only_is_refused_on_the_second_site(): void { @@ -3000,6 +3063,7 @@ public function test_editor_of_the_first_site_only_is_refused_on_the_second_site * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_member_of_the_first_site_only_is_an_invalid_author_on_the_second_site(): void { @@ -3036,6 +3100,7 @@ public function test_member_of_the_first_site_only_is_an_invalid_author_on_the_s * * Covers S9 (C13). * + * @ticket 66706 * @since 7.3.0 */ public function test_trashed_page_the_user_may_read_is_accepted_as_the_parent(): void { diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index 7281e0786bd2f..a0799e07bf0e5 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -431,6 +431,7 @@ private function strip_descriptions( array $schema ): array { /** * The ability is registered in the content category with its label and annotations. * + * @ticket 66706 * @since 7.3.0 */ public function test_registers_the_ability(): void { @@ -452,6 +453,7 @@ public function test_registers_the_ability(): void { /** * The ability is not registered when no post type is exposed to abilities. * + * @ticket 66706 * @since 7.3.0 */ public function test_is_not_registered_without_exposed_post_types(): void { @@ -467,6 +469,7 @@ public function test_is_not_registered_without_exposed_post_types(): void { /** * The input schema takes `where`, with the post type and ID, and `permanent`. * + * @ticket 66706 * @since 7.3.0 */ public function test_input_schema(): void { @@ -492,6 +495,7 @@ public function test_input_schema(): void { /** * The output holds the outcome only. * + * @ticket 66706 * @since 7.3.0 */ public function test_output_schema(): void { @@ -513,6 +517,7 @@ public function test_output_schema(): void { * * Covers S12. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_trashes_a_post(): void { @@ -540,6 +545,7 @@ public function test_example_trashes_a_post(): void { * * Covers S12. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_deletes_a_page_permanently(): void { @@ -568,6 +574,7 @@ public function test_example_deletes_a_page_permanently(): void { * * Covers S5 and S12. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_rejects_trashing_a_trashed_post(): void { @@ -610,6 +617,7 @@ public function test_example_rejects_trashing_a_trashed_post(): void { * * Covers S4 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_permission_callback_refuses_a_logged_out_user(): void { @@ -659,6 +667,7 @@ public static function data_inputs_refused_by_the_permission_callback(): array { * * @dataProvider data_inputs_refused_by_the_permission_callback * + * @ticket 66706 * @since 7.3.0 * * @param mixed $input The input. @@ -675,6 +684,7 @@ public function test_permission_callback_returns_false_for_malformed_input( $inp * The permission callback is only a coarse gate: it lets a logged-in subscriber through, and * the execute callback decides on the post. * + * @ticket 66706 * @since 7.3.0 */ public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type(): void { @@ -709,6 +719,7 @@ public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): arra * * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 * + * @ticket 66706 * @since 7.3.0 * * @param mixed $id The ID. @@ -759,6 +770,7 @@ public static function data_ids_that_are_not_whole_numbers_in_a_direct_call(): a * * @dataProvider data_ids_that_are_not_whole_numbers_in_a_direct_call * + * @ticket 66706 * @since 7.3.0 * * @param mixed $id The ID. @@ -797,6 +809,7 @@ public function test_direct_call_with_an_id_that_is_not_a_whole_number_is_not_fo * * Covers S2. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_reads_an_id_made_of_digits(): void { @@ -844,6 +857,7 @@ public static function data_posts_that_are_not_posts(): array { * * @dataProvider data_posts_that_are_not_posts * + * @ticket 66706 * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -874,6 +888,7 @@ public function test_post_of_another_type_is_not_found( string $post ): void { * * Covers S3 and S13. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): void { @@ -903,6 +918,7 @@ public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): * * Covers S3 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_type_no_longer_exposed_cannot_be_deleted(): void { @@ -945,6 +961,7 @@ public static function data_direct_inputs_with_a_malformed_where(): array { * * @dataProvider data_direct_inputs_with_a_malformed_where * + * @ticket 66706 * @since 7.3.0 * * @param mixed $input The input. @@ -965,6 +982,7 @@ public function test_direct_call_with_a_malformed_where_is_not_found( $input ): * * Covers O1, S4 and S13. * + * @ticket 66706 * @since 7.3.0 */ public function test_lookup_comes_before_the_capability(): void { @@ -1075,6 +1093,7 @@ public static function data_trash_matrix(): array { * * @dataProvider data_trash_matrix * + * @ticket 66706 * @since 7.3.0 * * @param string $role The role to delete as. @@ -1192,6 +1211,7 @@ public static function data_permanent_delete_matrix(): array { * * @dataProvider data_permanent_delete_matrix * + * @ticket 66706 * @since 7.3.0 * * @param string $role The role to delete as. @@ -1222,6 +1242,7 @@ public function test_permanent_delete_matrix( string $role, string $owner, strin * * Covers S12. * + * @ticket 66706 * @since 7.3.0 */ public function test_permanent_false_trashes_the_post(): void { @@ -1260,6 +1281,7 @@ public static function data_permanent_values_that_are_not_true(): array { * * @dataProvider data_permanent_values_that_are_not_true * + * @ticket 66706 * @since 7.3.0 * * @param mixed $permanent The value. @@ -1288,6 +1310,7 @@ public function test_direct_call_deletes_permanently_only_for_boolean_true( $per * * Covers S12. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_deletes_permanently_for_boolean_true(): void { @@ -1331,6 +1354,7 @@ private function set_special_pages(): void { * * @dataProvider data_special_pages * + * @ticket 66706 * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1358,6 +1382,7 @@ public function test_editor_cannot_delete_a_special_page( string $page ): void { * * @dataProvider data_special_pages * + * @ticket 66706 * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1397,6 +1422,7 @@ public static function data_special_pages_for_a_site_administrator(): array { * * @dataProvider data_special_pages_for_a_site_administrator * + * @ticket 66706 * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1428,6 +1454,7 @@ public function test_site_administrator_deletes_special_pages_on_multisite( stri * * @dataProvider data_special_pages * + * @ticket 66706 * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1467,6 +1494,7 @@ public static function data_operations_that_are_stopped(): array { * * @dataProvider data_operations_that_are_stopped * + * @ticket 66706 * @since 7.3.0 * * @param bool $permanent Whether the delete is permanent. @@ -1515,6 +1543,7 @@ public static function data_keys_outside_the_allow_list(): array { * * @dataProvider data_keys_outside_the_allow_list * + * @ticket 66706 * @since 7.3.0 * * @param string|null $location Where the key is sent, or null for the top level. @@ -1548,6 +1577,7 @@ public function test_key_outside_the_allow_list_fails_input_validation( ?string * * Covers S6 and S12. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_ignores_keys_outside_the_allow_list(): void { @@ -1580,6 +1610,7 @@ public function test_direct_call_ignores_keys_outside_the_allow_list(): void { * * Covers S4. * + * @ticket 66706 * @since 7.3.0 */ public function test_each_call_checks_the_current_user(): void { @@ -1623,6 +1654,7 @@ public static function data_whole_number_forms(): array { * * @dataProvider data_whole_number_forms * + * @ticket 66706 * @since 7.3.0 * * @param string $form The form of the ID. diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index 93a959a71a938..b4efc3c16d4a2 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -521,6 +521,7 @@ private function assert_is_now( string $gmt, string $context ): void { /** * The ability is registered in the content category with its label and annotations. * + * @ticket 66706 * @since 7.3.0 */ public function test_registers_the_ability(): void { @@ -542,6 +543,7 @@ public function test_registers_the_ability(): void { /** * The ability is not registered when no post type is exposed to abilities. * + * @ticket 66706 * @since 7.3.0 */ public function test_is_not_registered_without_exposed_post_types(): void { @@ -558,6 +560,7 @@ public function test_is_not_registered_without_exposed_post_types(): void { * The input schema takes `where`, with the post type and ID, `data`, with at least one * attribute, and `fields`. * + * @ticket 66706 * @since 7.3.0 */ public function test_input_schema(): void { @@ -617,6 +620,7 @@ public function test_input_schema(): void { * * Covers S11. * + * @ticket 66706 * @since 7.3.0 */ public function test_output_schema_is_a_query_item(): void { @@ -634,6 +638,7 @@ public function test_output_schema_is_a_query_item(): void { /** * Example 1: a new title and excerpt. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_updates_the_title_and_excerpt(): void { @@ -684,6 +689,7 @@ public function test_example_updates_the_title_and_excerpt(): void { /** * Example 2: publishing the draft. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_publishes_the_draft(): void { @@ -719,6 +725,7 @@ public function test_example_publishes_the_draft(): void { * * Covers S4, S5 and S13. * + * @ticket 66706 * @since 7.3.0 */ public function test_example_rejects_a_post_the_user_cannot_edit(): void { @@ -752,6 +759,7 @@ public function test_example_rejects_a_post_the_user_cannot_edit(): void { * * Covers S4 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_permission_callback_refuses_a_logged_out_user(): void { @@ -808,6 +816,7 @@ public static function data_inputs_refused_by_the_permission_callback(): array { * * @dataProvider data_inputs_refused_by_the_permission_callback * + * @ticket 66706 * @since 7.3.0 * * @param mixed $input The input. @@ -824,6 +833,7 @@ public function test_permission_callback_returns_false_for_malformed_input( $inp * The permission callback is only a coarse gate: it lets a logged-in subscriber through, and * the execute callback decides on the post. * + * @ticket 66706 * @since 7.3.0 */ public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type(): void { @@ -870,6 +880,7 @@ public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): arra * * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 * + * @ticket 66706 * @since 7.3.0 * * @param mixed $id The ID. @@ -920,6 +931,7 @@ public static function data_ids_that_are_not_whole_numbers_in_a_direct_call(): a * * @dataProvider data_ids_that_are_not_whole_numbers_in_a_direct_call * + * @ticket 66706 * @since 7.3.0 * * @param mixed $id The ID. @@ -958,6 +970,7 @@ public function test_direct_call_with_an_id_that_is_not_a_whole_number_is_not_fo * * Covers S2. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_reads_an_id_made_of_digits(): void { @@ -1005,6 +1018,7 @@ public static function data_posts_that_are_not_posts(): array { * * @dataProvider data_posts_that_are_not_posts * + * @ticket 66706 * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -1035,6 +1049,7 @@ public function test_post_of_another_type_is_not_found( string $post ): void { * * Covers S3 and S13. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): void { @@ -1064,6 +1079,7 @@ public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): * * Covers S3 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_type_no_longer_exposed_cannot_be_updated(): void { @@ -1128,6 +1144,7 @@ public static function data_direct_inputs_with_a_malformed_where(): array { * * @dataProvider data_direct_inputs_with_a_malformed_where * + * @ticket 66706 * @since 7.3.0 * * @param mixed $input The input. @@ -1147,6 +1164,7 @@ public function test_direct_call_with_a_malformed_where_is_not_found( $input ): * * Covers O1. * + * @ticket 66706 * @since 7.3.0 */ public function test_lookup_comes_before_the_attribute_rules(): void { @@ -1260,6 +1278,7 @@ public static function data_update_matrix(): array { * * @dataProvider data_update_matrix * + * @ticket 66706 * @since 7.3.0 * * @param string $role The role to update as. @@ -1317,6 +1336,7 @@ public static function data_invalid_data(): array { * * @dataProvider data_invalid_data * + * @ticket 66706 * @since 7.3.0 * * @param array $data The data. @@ -1381,6 +1401,7 @@ public static function data_keys_outside_the_allow_list(): array { * * @dataProvider data_keys_outside_the_allow_list * + * @ticket 66706 * @since 7.3.0 * * @param string $location Where the key is sent. @@ -1416,6 +1437,7 @@ public function test_key_outside_the_allow_list_fails_input_validation( string $ * * Covers S6. * + * @ticket 66706 * @since 7.3.0 */ public function test_direct_call_ignores_keys_outside_the_allow_list(): void { @@ -1510,6 +1532,7 @@ public static function data_statuses_that_can_never_be_set(): array { * * @dataProvider data_statuses_that_can_never_be_set * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -1536,6 +1559,7 @@ public function test_status_that_can_never_be_set_fails_input_validation( string * * @dataProvider data_statuses_that_can_never_be_set * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -1588,6 +1612,7 @@ public static function data_status_changes_without_publish_posts(): array { * * @dataProvider data_status_changes_without_publish_posts * + * @ticket 66706 * @since 7.3.0 * * @param string $role The role or fixture name of the user, who owns the draft. @@ -1620,6 +1645,7 @@ public function test_status_that_makes_a_post_visible_needs_publish_posts( strin * * A contributor may edit their own private post, but may not make a post private. * + * @ticket 66706 * @since 7.3.0 */ public function test_status_equal_to_the_stored_status_passes_with_no_check(): void { @@ -1645,6 +1671,7 @@ public function test_status_equal_to_the_stored_status_passes_with_no_check(): v /** * A draft is scheduled with `future` and a date in the future. * + * @ticket 66706 * @since 7.3.0 */ public function test_schedules_a_draft(): void { @@ -1686,6 +1713,7 @@ public static function data_unpublished_statuses(): array { * * @dataProvider data_unpublished_statuses * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -1719,6 +1747,7 @@ public static function data_statuses_reported_as_stored(): array { * * @dataProvider data_statuses_reported_as_stored * + * @ticket 66706 * @since 7.3.0 * * @param string $status The requested status. @@ -1747,6 +1776,7 @@ public function test_output_reports_the_stored_status( string $status, string $g /** * A post in the Trash is updated like any other post and stays in the Trash. * + * @ticket 66706 * @since 7.3.0 */ public function test_updates_a_post_in_the_trash(): void { @@ -1781,6 +1811,7 @@ public static function data_statuses_that_move_a_post_out_of_the_trash(): array * * @dataProvider data_statuses_that_move_a_post_out_of_the_trash * + * @ticket 66706 * @since 7.3.0 * * @param string $role The role, which owns the post. @@ -1803,6 +1834,7 @@ public function test_status_moves_a_post_out_of_the_trash( string $role, string * * Covers S5 and S7. * + * @ticket 66706 * @since 7.3.0 */ public function test_publishing_a_post_from_the_trash_needs_publish_posts(): void { @@ -1838,6 +1870,7 @@ public static function data_authors_named_without_edit_others_posts(): array { * * @dataProvider data_authors_named_without_edit_others_posts * + * @ticket 66706 * @since 7.3.0 * * @param string $author The author, as a fixture name. @@ -1871,6 +1904,7 @@ public function test_naming_another_author_without_edit_others_posts_is_refused( /** * A user with `edit_others_posts` may change the author of a post. * + * @ticket 66706 * @since 7.3.0 */ public function test_changes_the_author(): void { @@ -1889,6 +1923,7 @@ public function test_changes_the_author(): void { * An author equal to the stored author changes nothing and passes with no check, even when * that user no longer exists. * + * @ticket 66706 * @since 7.3.0 */ public function test_author_equal_to_the_stored_author_passes_with_no_check(): void { @@ -1925,6 +1960,7 @@ public function test_author_equal_to_the_stored_author_passes_with_no_check(): v * * Covers S5 and S8. * + * @ticket 66706 * @since 7.3.0 */ public function test_naming_a_missing_author_with_edit_others_posts_is_an_invalid_author(): void { @@ -1947,6 +1983,7 @@ public function test_naming_a_missing_author_with_edit_others_posts_is_an_invali * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_invalid_author(): void { @@ -1990,6 +2027,7 @@ public static function data_parents_that_cannot_be_used(): array { * * @dataProvider data_parents_that_cannot_be_used * + * @ticket 66706 * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -2016,6 +2054,7 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin * A parent equal to the stored parent changes nothing and passes with no check, even when the * user may not read it. * + * @ticket 66706 * @since 7.3.0 */ public function test_parent_equal_to_the_stored_parent_passes_with_no_check(): void { @@ -2055,6 +2094,7 @@ public static function data_parents_that_can_be_used(): array { * * @dataProvider data_parents_that_can_be_used * + * @ticket 66706 * @since 7.3.0 * * @param string|null $parent_key The parent, as a fixture name, or null for 0. @@ -2092,6 +2132,7 @@ public static function data_unsupported_attributes(): array { * * @dataProvider data_unsupported_attributes * + * @ticket 66706 * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -2135,6 +2176,7 @@ public static function data_values_of_the_wrong_type(): array { * * @dataProvider data_values_of_the_wrong_type * + * @ticket 66706 * @since 7.3.0 * * @param string $attribute The attribute. @@ -2164,6 +2206,7 @@ public function test_direct_call_rejects_a_value_of_the_wrong_type( string $attr /** * Attributes that were not sent keep their stored values. * + * @ticket 66706 * @since 7.3.0 */ public function test_attributes_not_sent_keep_their_stored_values(): void { @@ -2199,6 +2242,7 @@ public function test_attributes_not_sent_keep_their_stored_values(): void { * * Covers S1 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { @@ -2233,6 +2277,7 @@ public static function data_posts_taking_a_used_slug(): array { * * @dataProvider data_posts_taking_a_used_slug * + * @ticket 66706 * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -2271,6 +2316,7 @@ public static function data_draft_page_slugs_by_parent(): array { * * @dataProvider data_draft_page_slugs_by_parent * + * @ticket 66706 * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -2310,6 +2356,7 @@ public function test_draft_page_slug_is_unique_among_its_siblings( string $paren /** * D1: an update without a date keeps a floating draft floating. * + * @ticket 66706 * @since 7.3.0 */ public function test_update_without_a_date_keeps_a_draft_floating(): void { @@ -2343,6 +2390,7 @@ public static function data_statuses_that_stamp_the_current_time(): array { * * @dataProvider data_statuses_that_stamp_the_current_time * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -2363,6 +2411,7 @@ public function test_publishing_a_floating_draft_without_a_date_stamps_the_curre /** * D1: moving a floating draft to pending without a date keeps it floating. * + * @ticket 66706 * @since 7.3.0 */ public function test_pending_without_a_date_keeps_a_draft_floating(): void { @@ -2402,6 +2451,7 @@ public static function data_new_dates(): array { * * @dataProvider data_new_dates * + * @ticket 66706 * @since 7.3.0 * * @param array $date The sent date. @@ -2443,6 +2493,7 @@ public static function data_new_dates_for_a_floating_draft(): array { * * @dataProvider data_new_dates_for_a_floating_draft * + * @ticket 66706 * @since 7.3.0 * * @param array $date The sent date. @@ -2478,6 +2529,7 @@ public static function data_dates_equal_to_the_floating_date(): array { * * @dataProvider data_dates_equal_to_the_floating_date * + * @ticket 66706 * @since 7.3.0 * * @param array $date The sent date. @@ -2523,6 +2575,7 @@ public static function data_past_dates_equal_to_the_floating_date(): array { * * @dataProvider data_past_dates_equal_to_the_floating_date * + * @ticket 66706 * @since 7.3.0 * * @param array $date The sent date. @@ -2554,6 +2607,7 @@ public function test_publishing_with_the_past_floating_date_stamps_the_current_t * * @dataProvider data_dates_equal_to_the_floating_date * + * @ticket 66706 * @since 7.3.0 * * @param array $date The sent date. @@ -2598,6 +2652,7 @@ public static function data_schedules_a_floating_draft_for_the_date_it_shows(): * * @dataProvider data_schedules_a_floating_draft_for_the_date_it_shows * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -2631,6 +2686,7 @@ public function test_scheduling_a_floating_draft_for_the_date_it_shows( string $ * * Covers S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_date_that_is_not_a_date_time_fails_input_validation(): void { @@ -2652,6 +2708,7 @@ public function test_date_that_is_not_a_date_time_fails_input_validation(): void * * Covers S10. * + * @ticket 66706 * @since 7.3.0 */ public function test_markup_of_an_author_is_filtered(): void { @@ -2684,6 +2741,7 @@ public function test_markup_of_an_author_is_filtered(): void { * * @group ms-excluded * + * @ticket 66706 * @since 7.3.0 */ public function test_markup_of_an_administrator_is_not_filtered(): void { @@ -2705,6 +2763,7 @@ public function test_markup_of_an_administrator_is_not_filtered(): void { * * @group ms-required * + * @ticket 66706 * @since 7.3.0 */ public function test_markup_of_a_site_administrator_is_filtered_on_multisite(): void { @@ -2721,6 +2780,7 @@ public function test_markup_of_a_site_administrator_is_filtered_on_multisite(): /** * Quotes, backslashes and non-ASCII text survive a round trip unchanged. * + * @ticket 66706 * @since 7.3.0 */ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { @@ -2757,6 +2817,7 @@ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip( * * Covers S11. * + * @ticket 66706 * @since 7.3.0 */ public function test_raw_fields_are_left_out_when_the_writer_can_no_longer_edit_the_post(): void { @@ -2785,6 +2846,7 @@ public function test_raw_fields_are_left_out_when_the_writer_can_no_longer_edit_ * * Covers S11. * + * @ticket 66706 * @since 7.3.0 */ public function test_password_protected_post_is_returned_with_its_protection_flag(): void { @@ -2819,6 +2881,7 @@ public function test_password_protected_post_is_returned_with_its_protection_fla * * Covers S4. * + * @ticket 66706 * @since 7.3.0 */ public function test_each_call_checks_the_current_user(): void { @@ -2858,6 +2921,7 @@ public function test_each_call_checks_the_current_user(): void { * * Covers S11. * + * @ticket 66706 * @since 7.3.0 */ public function test_each_call_checks_the_author_visibility_again(): void { @@ -2912,6 +2976,7 @@ public function test_each_call_checks_the_author_visibility_again(): void { * * Covers S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_empty_post_is_the_save_error(): void { @@ -2938,6 +3003,7 @@ public function test_empty_post_is_the_save_error(): void { /** * A database error during the save keeps its own code and message, with status 500. * + * @ticket 66706 * @since 7.3.0 */ public function test_database_error_is_the_save_error(): void { @@ -2970,6 +3036,7 @@ static function ( $query ) { /** * A stored page template that no longer exists does not fail an unrelated update and is kept. * + * @ticket 66706 * @since 7.3.0 */ public function test_stale_page_template_does_not_fail_an_update(): void { @@ -2997,6 +3064,7 @@ public function test_stale_page_template_does_not_fail_an_update(): void { * A save that stores a revision also fires the hooks for the revision, so only the fires * for the updated post are counted, and a regular save of an identical post is the reference. * + * @ticket 66706 * @since 7.3.0 */ public function test_after_insert_hooks_fire_once(): void { @@ -3081,6 +3149,7 @@ public static function data_whole_number_forms(): array { * * @dataProvider data_whole_number_forms * + * @ticket 66706 * @since 7.3.0 * * @param string $form The form of the ID. @@ -3111,6 +3180,7 @@ public function test_direct_call_reads_an_id_in_a_whole_number_form( string $for * * @dataProvider data_whole_number_forms * + * @ticket 66706 * @since 7.3.0 * * @param string $form The form of the author. @@ -3141,6 +3211,7 @@ public function test_direct_call_reads_an_author_in_a_whole_number_form( string * * @dataProvider data_whole_number_forms * + * @ticket 66706 * @since 7.3.0 * * @param string $form The form of the parent. @@ -3206,6 +3277,7 @@ public static function data_internal_stored_statuses(): array { * * @dataProvider data_internal_stored_statuses * + * @ticket 66706 * @since 7.3.0 * * @param string $status The stored status, which is also sent. @@ -3249,6 +3321,7 @@ public function test_direct_call_rejects_trash_for_a_post_in_the_trash( string $ * * Covers S5 (C1). * + * @ticket 66706 * @since 7.3.0 */ public function test_trash_for_a_post_in_the_trash_fails_input_validation(): void { @@ -3301,6 +3374,7 @@ public static function data_dates_without_a_usable_value(): array { * * @dataProvider data_dates_without_a_usable_value * + * @ticket 66706 * @since 7.3.0 * * @param array $date The date. @@ -3356,6 +3430,7 @@ public static function data_inputs_without_usable_data(): array { * * @dataProvider data_inputs_without_usable_data * + * @ticket 66706 * @since 7.3.0 * * @param array $input The input, without `where`. @@ -3383,6 +3458,7 @@ public function test_direct_call_without_usable_data_is_an_invalid_attribute( ar * * Covers S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_stores_an_empty_excerpt_and_an_empty_title(): void { @@ -3420,6 +3496,7 @@ public function test_stores_an_empty_excerpt_and_an_empty_title(): void { * * Covers S7. * + * @ticket 66706 * @since 7.3.0 */ public function test_author_denied_publish_posts_may_set_pending(): void { @@ -3484,6 +3561,7 @@ public static function data_date_changes_on_a_published_or_scheduled_post(): arr * * @dataProvider data_date_changes_on_a_published_or_scheduled_post * + * @ticket 66706 * @since 7.3.0 * * @param string $stored_status The status the post has. @@ -3530,6 +3608,7 @@ public static function data_named_status_changes_on_a_published_or_scheduled_pos * * @dataProvider data_named_status_changes_on_a_published_or_scheduled_post * + * @ticket 66706 * @since 7.3.0 * * @param string $stored_status The status the post has. @@ -3592,6 +3671,7 @@ static function ( $caps, $cap, $cap_user_id, $args ) use ( $user_id, $post_id, $ * * Covers S4, S5 and S7. * + * @ticket 66706 * @since 7.3.0 */ public function test_edit_post_granted_on_one_post_allows_only_editing_that_post(): void { @@ -3677,6 +3757,7 @@ private function set_special_pages(): array { * * Covers S4 and S5. * + * @ticket 66706 * @since 7.3.0 */ public function test_editor_cannot_update_the_privacy_policy_page(): void { @@ -3711,6 +3792,7 @@ public static function data_special_pages_an_editor_may_update(): array { * * @dataProvider data_special_pages_an_editor_may_update * + * @ticket 66706 * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -3745,6 +3827,7 @@ public static function data_updates_of_a_page_with_a_missing_parent(): array { * * @dataProvider data_updates_of_a_page_with_a_missing_parent * + * @ticket 66706 * @since 7.3.0 * * @param bool $send_parent Whether the stored parent is sent. @@ -3814,6 +3897,7 @@ private function register_custom_statuses(): void { * * Covers S7 (C4). * + * @ticket 66706 * @since 7.3.0 */ public function test_contributor_may_set_a_hidden_custom_status(): void { @@ -3852,6 +3936,7 @@ public static function data_custom_visible_statuses_by_stored_status(): array { * * @dataProvider data_custom_visible_statuses_by_stored_status * + * @ticket 66706 * @since 7.3.0 * * @param string $status The status. @@ -3878,6 +3963,7 @@ public function test_contributor_cannot_set_a_custom_visible_status( string $sta * * Covers S11 (C14). * + * @ticket 66706 * @since 7.3.0 */ public function test_output_is_the_id_alone_when_the_update_takes_away_read_access(): void { @@ -3919,6 +4005,7 @@ public function test_output_is_the_id_alone_when_the_update_takes_away_read_acce * * Covers S11 (C14). * + * @ticket 66706 * @since 7.3.0 */ public function test_output_is_complete_when_the_caller_may_still_read_the_post(): void { diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index 416d92296713e..39e79517d241a 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -472,6 +472,7 @@ private function snapshot( int $post_id = 0 ): array { /** * Create runs with POST and the input in a JSON body. * + * @ticket 66706 * @since 7.3.0 */ public function test_create_runs_with_post_and_a_json_body(): void { @@ -501,6 +502,7 @@ public function test_create_runs_with_post_and_a_json_body(): void { /** * Update runs with POST and the input in a JSON body. * + * @ticket 66706 * @since 7.3.0 */ public function test_update_runs_with_post_and_a_json_body(): void { @@ -535,6 +537,7 @@ public function test_update_runs_with_post_and_a_json_body(): void { /** * Delete runs with DELETE and the input in the query string, where the ID is a string. * + * @ticket 66706 * @since 7.3.0 */ public function test_delete_runs_with_delete_and_the_input_in_the_query_string(): void { @@ -578,6 +581,7 @@ public static function data_permanent_query_string_values(): array { * * @dataProvider data_permanent_query_string_values * + * @ticket 66706 * @since 7.3.0 * * @param string $permanent The value in the query string. @@ -634,6 +638,7 @@ public static function data_permanent_query_string_values_that_are_not_booleans( * * @dataProvider data_permanent_query_string_values_that_are_not_booleans * + * @ticket 66706 * @since 7.3.0 * * @param string $permanent The value in the query string. @@ -665,6 +670,7 @@ public function test_delete_with_a_permanent_value_that_is_not_a_boolean_returns * * Covers C5. * + * @ticket 66706 * @since 7.3.0 */ public function test_create_with_the_input_in_the_query_string_returns_400(): void { @@ -692,6 +698,7 @@ public function test_create_with_the_input_in_the_query_string_returns_400(): vo * * Covers C5. * + * @ticket 66706 * @since 7.3.0 */ public function test_delete_with_the_input_in_a_json_body_returns_400(): void { @@ -740,6 +747,7 @@ public static function data_wrong_methods_for_write_abilities(): array { * * @dataProvider data_wrong_methods_for_write_abilities * + * @ticket 66706 * @since 7.3.0 * * @param string $ability The ability name. @@ -774,6 +782,7 @@ public function test_write_ability_with_a_wrong_method_returns_405( string $abil /** * A logged-out user is refused as unauthenticated. * + * @ticket 66706 * @since 7.3.0 */ public function test_logged_out_create_receives_401(): void { @@ -789,6 +798,7 @@ public function test_logged_out_create_receives_401(): void { /** * A user the permission callback refuses is forbidden. * + * @ticket 66706 * @since 7.3.0 */ public function test_subscriber_create_receives_403(): void { @@ -804,6 +814,7 @@ public function test_subscriber_create_receives_403(): void { /** * Input that fails the schema is rejected. * + * @ticket 66706 * @since 7.3.0 */ public function test_create_with_input_that_fails_the_schema_returns_400(): void { @@ -828,6 +839,7 @@ public function test_create_with_input_that_fails_the_schema_returns_400(): void /** * The not-found error of an update reaches the response with its status. * + * @ticket 66706 * @since 7.3.0 */ public function test_update_of_a_missing_post_returns_404(): void { @@ -853,6 +865,7 @@ public function test_update_of_a_missing_post_returns_404(): void { /** * The edit error of an update reaches the response with its status. * + * @ticket 66706 * @since 7.3.0 */ public function test_update_of_a_post_the_user_cannot_edit_returns_403(): void { @@ -886,6 +899,7 @@ public function test_update_of_a_post_the_user_cannot_edit_returns_403(): void { /** * The invalid attribute error of a create reaches the response with its status. * + * @ticket 66706 * @since 7.3.0 */ public function test_create_with_an_unsupported_attribute_returns_400(): void { @@ -912,6 +926,7 @@ public function test_create_with_an_unsupported_attribute_returns_400(): void { /** * The already-trashed error of a delete reaches the response with its status. * + * @ticket 66706 * @since 7.3.0 */ public function test_trashing_a_trashed_post_returns_410(): void { From acd6df58eea37b94b63101690758f7ed6a4263d6 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 16 Nov 2026 20:24:19 +0000 Subject: [PATCH 03/24] Abilities API: Simplify the content write abilities and their tests. The execute callbacks always receive input that `WP_Ability::execute()` validated and converted to the schema types, so the type checks they repeated are removed. The permission callbacks keep reading the input defensively, since `WP_Ability::check_permissions()` does not prepare it. Other changes: - A `slug` sent for a pending post by a user who cannot publish it is now an error instead of being dropped by the save. - The invalid attribute error uses the reason as its message. - A delete that did not happen returns `content_delete_failed`, so `content_cannot_delete` is only the permission error. - A date at the Unix epoch is returned instead of null. In the tests, the helpers and data providers shared by the three write abilities move to a base test case, and the tests for input that validation already rejects are removed. New tests cover a post type without Trash support, the slug of a pending post, a date at the Unix epoch, and trashing and permanently deleting the special pages separately. --- src/wp-includes/abilities.php | 2 +- .../abilities/class-wp-abilities-content.php | 203 ++-- .../testcase-content-write-ability.php | 372 ++++++ .../wpRegisterCoreContentAbility.php | 66 +- .../wpRegisterCoreContentCreateAbility.php | 784 ++---------- .../wpRegisterCoreContentDeleteAbility.php | 899 +++----------- .../wpRegisterCoreContentUpdateAbility.php | 1069 +++-------------- .../wpRestAbilitiesContentController.php | 23 +- 8 files changed, 956 insertions(+), 2462 deletions(-) create mode 100644 tests/phpunit/tests/abilities-api/testcase-content-write-ability.php diff --git a/src/wp-includes/abilities.php b/src/wp-includes/abilities.php index c5ee2a5ca8c17..b67387be617bd 100644 --- a/src/wp-includes/abilities.php +++ b/src/wp-includes/abilities.php @@ -378,6 +378,6 @@ function wp_register_core_abilities(): void { // Register the user abilities (currently the read-only `core/users-query`). ( new WP_Abilities_Users() )->register(); - // Register the content abilities: `core/content-query`, `core/content-create`, `core/content-update`, and `core/content-delete`. + // Register the content abilities (query, create, update, and delete). ( new WP_Abilities_Content() )->register(); } diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 614db42083706..5dfd8452d91f8 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -132,8 +132,8 @@ public function register(): void { return; } - // The statuses that can be set. The query accepts `trash` as well. - $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); + // The statuses that can be set: the ones the query accepts, other than `trash`. + $statuses = array_values( array_diff( $this->get_queryable_statuses(), array( 'trash' ) ) ); $this->register_content_query( $post_types ); $this->register_content_create( $post_types, $statuses ); @@ -170,7 +170,7 @@ private function register_content_query( array $post_types ): void { return $this->execute_content_query( $input ); }, 'permission_callback' => function ( $input = array() ): bool { - return $this->check_permission( $input ); + return $this->check_query_permission( $input ); }, 'meta' => array( 'annotations' => array( @@ -301,7 +301,7 @@ private function register_content_delete( array $post_types ): void { * @param mixed $input Optional. The ability input. Default empty array. * @return bool True if the request may proceed, false otherwise. */ - private function check_permission( $input = array() ): bool { + private function check_query_permission( $input = array() ): bool { $input = rest_sanitize_object( $input ); if ( ! is_user_logged_in() ) { @@ -343,7 +343,8 @@ private function check_create_permission( $input = array() ): bool { return false; } - $data = $this->get_required_input_object( rest_sanitize_object( $input ), 'data' ) ?? array(); + // The input is read defensively: WP_Ability::check_permissions() does not prepare it. + $data = $this->get_input_object( rest_sanitize_object( $input ), 'data' ) ?? array(); $post_type_object = $this->get_exposed_post_type( $data['type'] ?? null ); return null !== $post_type_object && current_user_can( $post_type_object->cap->create_posts ); @@ -365,7 +366,8 @@ private function check_post_permission( $input = array() ): bool { return false; } - $where = $this->get_required_input_object( rest_sanitize_object( $input ), 'where' ) ?? array(); + // The input is read defensively: WP_Ability::check_permissions() does not prepare it. + $where = $this->get_input_object( rest_sanitize_object( $input ), 'where' ) ?? array(); return null !== $this->get_exposed_post_type( $where['type'] ?? null ); } @@ -375,8 +377,8 @@ private function check_post_permission( $input = array() ): bool { * * Accepts the whole numbers that the integer schema accepts: native integers, floats such * as the 2.0 that JSON encoders produce, and integer strings such as "2", "2.0", or "+2". - * Only the REST run controller converts input to the schema types, so other callers, such - * as the MCP adapter or a direct WP_Ability::execute() call, pass them on unconverted. + * WP_Ability::execute() converts input to the schema types, but + * WP_Ability::check_permissions() does not, so a permission callback can receive any of them. * Anything else, such as a fraction, is rejected rather than coerced: a malformed ID would * otherwise resolve to another post, and a malformed `parent` filter would be read as 0, * which asks for top-level posts. @@ -477,7 +479,7 @@ private function can_query_status( string $status, WP_Post_Type $post_type_objec * Checks if a post can be read by the current user. * * Mirrors the REST posts controller's read permission, while keeping this ability - * authenticated-only via {@see self::check_permission()}. + * authenticated-only via {@see self::check_query_permission()}. * * @since 7.2.0 * @@ -557,7 +559,7 @@ private function check_read_permission( WP_Post $post, array $checked_post_ids = /** * Executes the `core/content-query` ability. * - * {@see WP_Ability::execute()} always runs {@see self::check_permission()} first, which + * {@see WP_Ability::execute()} always runs {@see self::check_query_permission()} first, which * gates the request as a whole. The query still filters every row by read permission, * because the gate cannot know the rows before the query runs. * @@ -587,8 +589,8 @@ private function execute_content_query( $input = array() ) { $page = $this->parse_filter_int( $pagination['page'] ?? 1, 1 ) ?? 1; /* - * Unreachable through gated transports, which run check_permission() first and deny a - * request without a post type exposed to abilities. A direct call to the execute + * Unreachable through gated transports, which run check_query_permission() first and deny + * a request without a post type exposed to abilities. A direct call to the execute * callback still fails closed here, but it bypasses the checks of the request as a * whole that the permission callback performed, such as the status capabilities, so * only invoke the callback through WP_Ability::execute(). @@ -869,16 +871,14 @@ private function execute_content_query( $input = array() ) { * * @since 7.3.0 * - * @param mixed $input Optional. The ability input. Default empty array. + * @param array $input The ability input, as prepared by WP_Ability::execute(). * @return array|WP_Error The created post, or a WP_Error on failure. */ - private function execute_content_create( $input = array() ) { - $input = rest_sanitize_object( $input ); - + private function execute_content_create( array $input ) { // The author visibility memo is per call; see get_visible_author(). $this->visible_authors = array(); - $data = $this->get_required_input_object( $input, 'data' ) ?? array(); + $data = $input['data'] ?? array(); $post_type_object = $this->get_exposed_post_type( $data['type'] ?? null ); if ( ! $post_type_object ) { return $this->not_found_error(); @@ -897,7 +897,7 @@ private function execute_content_create( $input = array() ) { return $postarr; } - return $this->save_post( $postarr, $this->normalize_fields( $input ), false ); + return $this->save_post( $postarr, $this->normalize_fields( $input ) ); } /** @@ -908,12 +908,10 @@ private function execute_content_create( $input = array() ) { * * @since 7.3.0 * - * @param mixed $input Optional. The ability input. Default empty array. + * @param array $input The ability input, as prepared by WP_Ability::execute(). * @return array|WP_Error The updated post, or a WP_Error on failure. */ - private function execute_content_update( $input = array() ) { - $input = rest_sanitize_object( $input ); - + private function execute_content_update( array $input ) { // The author visibility memo is per call; see get_visible_author(). $this->visible_authors = array(); @@ -931,11 +929,11 @@ private function execute_content_update( $input = array() ) { } /* - * A `data` that is not an object, or that sends no attribute that can be written, - * has nothing to apply, so it is rejected instead of saving the post unchanged. + * A `data` that sends no attribute that can be written has nothing to apply, so it is + * rejected instead of saving the post unchanged. */ - $data = $this->get_input_object( $input, 'data' ); - if ( empty( $data ) || empty( array_intersect_key( $data, array_flip( self::WRITE_ATTRIBUTES ) ) ) ) { + $data = $input['data'] ?? array(); + if ( empty( array_intersect_key( $data, array_flip( self::WRITE_ATTRIBUTES ) ) ) ) { return $this->invalid_attribute_error( 'data', __( 'The data must be an object with at least one attribute to write.' ) ); } @@ -949,24 +947,22 @@ private function execute_content_update( $input = array() ) { return $postarr; } - return $this->save_post( $postarr, $this->normalize_fields( $input ), true ); + return $this->save_post( $postarr, $this->normalize_fields( $input ) ); } /** * Executes the `core/content-delete` ability. * * The post is looked up first, then the capability to delete it is checked. The post is - * deleted permanently only when `permanent` is boolean true; otherwise it is moved to the - * Trash. The outcome is confirmed against the stored post afterwards. + * deleted permanently when `permanent` is true; otherwise it is moved to the Trash. The + * outcome is confirmed against the stored post afterwards. * * @since 7.3.0 * - * @param mixed $input Optional. The ability input. Default empty array. + * @param array $input The ability input, as prepared by WP_Ability::execute(). * @return array|WP_Error The outcome, or a WP_Error on failure. */ - private function execute_content_delete( $input = array() ) { - $input = rest_sanitize_object( $input ); - + private function execute_content_delete( array $input ) { $post = $this->get_requested_post( $input ); if ( ! $post ) { return $this->not_found_error(); @@ -980,11 +976,7 @@ private function execute_content_delete( $input = array() ) { ); } - /* - * Intentionally strict: only boolean true deletes permanently. Any other value, such - * as "true" or 1, trashes. - */ - if ( true === ( $input['permanent'] ?? null ) ) { + if ( true === ( $input['permanent'] ?? false ) ) { $deleted = wp_delete_post( $post->ID, true ); return $deleted && null === get_post( $post->ID ) ? array( 'outcome' => 'deleted' ) : $this->delete_failed_error(); @@ -1019,8 +1011,8 @@ private function execute_content_delete( $input = array() ) { * Returns the post that `where` names, when it exists and has the named exposed type. * * The ID is read once, with {@see self::parse_filter_int()}, and that same integer is used - * for the lookup, the capability checks, and the write. A value that is not a whole number - * of at least 1 names no post and is never cast. + * for the lookup, the capability checks, and the write. An ID that is not a whole number + * of at least 1 names no post. * * @since 7.3.0 * @@ -1028,11 +1020,7 @@ private function execute_content_delete( $input = array() ) { * @return WP_Post|null The post, or null when `where` names no post of an exposed type. */ private function get_requested_post( array $input ): ?WP_Post { - $where = $this->get_required_input_object( $input, 'where' ); - if ( null === $where ) { - return null; - } - + $where = $input['where'] ?? array(); $post_type_object = $this->get_exposed_post_type( $where['type'] ?? null ); $post_id = $this->parse_filter_int( $where['id'] ?? null, 1 ); if ( ! $post_type_object || null === $post_id ) { @@ -1075,13 +1063,9 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, ); foreach ( $text_fields as $attribute => $field ) { - if ( ! array_key_exists( $attribute, $data ) ) { - continue; + if ( array_key_exists( $attribute, $data ) ) { + $postarr[ $field ] = $data[ $attribute ]; } - if ( ! is_string( $data[ $attribute ] ) ) { - return $this->invalid_attribute_error( $attribute, __( 'The value must be a string.' ) ); - } - $postarr[ $field ] = $data[ $attribute ]; } $status = $this->prepare_status( $data, $post_type_object, $post ); @@ -1114,15 +1098,10 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, } $postarr = array_merge( $postarr, $dates ); - if ( $post ) { - $final_status = $postarr['post_status'] ?? $post->post_status; - $final_parent = $postarr['post_parent'] ?? (int) $post->post_parent; - } else { - $final_status = $postarr['post_status'] ?? 'draft'; - $final_parent = $postarr['post_parent'] ?? 0; - } + $final_status = $postarr['post_status'] ?? $post->post_status ?? 'draft'; + $final_parent = $postarr['post_parent'] ?? (int) ( $post->post_parent ?? 0 ); - $slug = $this->prepare_slug( $data, $post_type, $post, $final_status, $final_parent ); + $slug = $this->prepare_slug( $data, $post_type_object, $post, $final_status, $final_parent ); if ( is_wp_error( $slug ) ) { return $slug; } @@ -1167,14 +1146,14 @@ private function check_supported_attributes( array $data, string $post_type ): ? if ( array_key_exists( $attribute, $data ) && ! post_type_supports( $post_type, $feature ) ) { return $this->invalid_attribute_error( $attribute, - /* translators: %s: Post type feature, such as "editor". */ - sprintf( __( 'The post type does not support the %s feature.' ), $feature ) + /* translators: 1: Attribute name, such as "content", 2: Post type feature, such as "editor". */ + sprintf( __( 'The %1$s attribute is only supported for post types that support the %2$s feature.' ), $attribute, $feature ) ); } } if ( array_key_exists( 'parent', $data ) && ! is_post_type_hierarchical( $post_type ) ) { - return $this->invalid_attribute_error( 'parent', __( 'The post type is not hierarchical.' ) ); + return $this->invalid_attribute_error( 'parent', __( 'The parent attribute is only supported for hierarchical post types.' ) ); } return null; @@ -1196,14 +1175,14 @@ private function prepare_status( array $data, WP_Post_Type $post_type_object, ?W } $status = $data['status']; - $status_object = is_string( $status ) ? get_post_status_object( $status ) : null; + $status_object = get_post_status_object( $status ); /* * Intentionally strict: a status that is not registered is an error and is never * replaced by another status. Internal statuses, such as `trash`, can never be set, * not even when the post already has them. */ - if ( ! is_string( $status ) || ! $status_object instanceof stdClass || ! empty( $status_object->internal ) ) { + if ( ! $status_object instanceof stdClass || ! empty( $status_object->internal ) ) { return $this->invalid_attribute_error( 'status', __( 'The status must be a registered post status that is not internal.' ) ); } @@ -1347,18 +1326,7 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { return array(); } - $reason = __( 'The date must be an object with a valid `site` or `gmt` date.' ); - $date = $this->get_input_object( $data, 'date' ); - if ( empty( $date ) ) { - return $this->invalid_attribute_error( 'date', $reason ); - } - - foreach ( array( 'site', 'gmt' ) as $key ) { - if ( array_key_exists( $key, $date ) && ! is_string( $date[ $key ] ) ) { - return $this->invalid_attribute_error( 'date', $reason ); - } - } - + $date = $data['date']; if ( isset( $date['site'] ) ) { $dates = rest_get_date_with_gmt( $date['site'] ); } elseif ( isset( $date['gmt'] ) ) { @@ -1368,7 +1336,7 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { } if ( null === $dates ) { - return $this->invalid_attribute_error( 'date', $reason ); + return $this->invalid_attribute_error( 'date', __( 'The date must be an object with a valid `site` or `gmt` date.' ) ); } $fields = array( @@ -1413,19 +1381,32 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { * * @since 7.3.0 * - * @param array $data The `data` input. - * @param string $post_type The post type name. - * @param WP_Post|null $post The stored post on update, or null on create. - * @param string $status The status the post will have. - * @param int $parent_id The parent ID the post will have. + * @param array $data The `data` input. + * @param WP_Post_Type $post_type_object The post type of the post being written. + * @param WP_Post|null $post The stored post on update, or null on create. + * @param string $status The status the post will have. + * @param int $parent_id The parent ID the post will have. * @return string|WP_Error|null The slug, null when none is sent, or a WP_Error. */ - private function prepare_slug( array $data, string $post_type, ?WP_Post $post, string $status, int $parent_id ) { + private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_Post $post, string $status, int $parent_id ) { if ( ! array_key_exists( 'slug', $data ) ) { return null; } - $slug = is_string( $data['slug'] ) ? sanitize_title( $data['slug'] ) : ''; + /* + * Intentionally strict: the save drops the slug of a pending post when the user cannot + * publish it, so the slug is an error here instead of being dropped without notice. + */ + if ( 'pending' === $status ) { + $can_publish = $post + ? current_user_can( 'publish_post', $post->ID ) + : current_user_can( $post_type_object->cap->publish_posts ); + if ( ! $can_publish ) { + return $this->invalid_attribute_error( 'slug', __( 'The slug of a pending post can only be set by a user who can publish the post.' ) ); + } + } + + $slug = sanitize_title( $data['slug'] ); // Intentionally strict: a slug that sanitizes to nothing is an error. if ( '' === $slug ) { @@ -1438,7 +1419,7 @@ private function prepare_slug( array $data, string $post_type, ?WP_Post $post, s * made unique here as if the post were published. */ if ( in_array( $status, array( 'draft', 'pending' ), true ) ) { - $slug = wp_unique_post_slug( $slug, $post ? $post->ID : 0, 'publish', $post_type, $parent_id ); + $slug = wp_unique_post_slug( $slug, $post ? $post->ID : 0, 'publish', $post_type_object->name, $parent_id ); } return $slug; @@ -1453,15 +1434,15 @@ private function prepare_slug( array $data, string $post_type, ?WP_Post $post, s * * @since 7.3.0 * - * @param array $postarr The unslashed post array from prepare_post_data(). + * @param array $postarr The unslashed post array from prepare_post_data(). It + * holds an `ID` when the post is updated. * @param list $fields The requested field paths. - * @param bool $update Whether the post is updated rather than created. * @return array|WP_Error The saved post, only its ID when the current user * may not read it, or a WP_Error on failure. */ - private function save_post( array $postarr, array $fields, bool $update ) { + private function save_post( array $postarr, array $fields ) { $postarr = wp_slash( $postarr ); - $post_id = $update ? wp_update_post( $postarr, true ) : wp_insert_post( $postarr, true ); + $post_id = isset( $postarr['ID'] ) ? wp_update_post( $postarr, true ) : wp_insert_post( $postarr, true ); if ( is_wp_error( $post_id ) ) { $is_database_error = in_array( $post_id->get_error_code(), array( 'db_insert_error', 'db_update_error' ), true ); @@ -1497,20 +1478,15 @@ private function save_post( array $postarr, array $fields, bool $update ) { * @return bool Whether the post can be trashed. */ private function can_trash( WP_Post $post ): bool { - $can_trash = (int) constant( 'EMPTY_TRASH_DAYS' ) > 0; - if ( 'attachment' === $post->post_type ) { - $can_trash = $can_trash && (bool) constant( 'MEDIA_TRASH' ); - } - - return $can_trash; + return EMPTY_TRASH_DAYS > 0 && ( 'attachment' !== $post->post_type || MEDIA_TRASH ); } /** * Reads an object member of the input, such as `where` or `pagination`. * * Accepts what schema validation reads as an object: an array, a `stdClass`, or an - * empty string. Only the REST run controller converts input to the schema types, so - * callers that bypass it, such as a direct WP_Ability::execute() call, can pass any of them. + * empty string. WP_Ability::execute() converts input to the schema types, but + * WP_Ability::check_permissions() does not, so a permission callback can receive any of them. * * @since 7.2.0 * @@ -1527,19 +1503,6 @@ private function get_input_object( array $input, string $key ): ?array { return rest_is_object( $input[ $key ] ) ? rest_sanitize_object( $input[ $key ] ) : null; } - /** - * Reads an object member of the input that must be sent, such as `where` or `data`. - * - * @since 7.3.0 - * - * @param array $input The ability input. - * @param string $key The member name. - * @return array|null The member as an array, or null when it is omitted or not an object. - */ - private function get_required_input_object( array $input, string $key ): ?array { - return array_key_exists( $key, $input ) ? $this->get_input_object( $input, $key ) : null; - } - /** * Normalizes the requested per-page value to the supported bounds. * @@ -2811,7 +2774,7 @@ private function format_date( WP_Post $post, string $field, bool $gmt ): ?string */ $date = ( $gmt ? $datetime->setTimezone( new DateTimeZone( 'UTC' ) ) : $datetime )->format( 'c' ); - return rest_parse_date( $date ) ? $date : null; + return false !== rest_parse_date( $date ) ? $date : null; } /** @@ -2897,25 +2860,25 @@ private function not_found_error(): WP_Error { /** * Builds the error for a `data` attribute that cannot be applied. * - * The error data maps the attribute's input path, such as `data[title]`, to the reason - * under `params`, so callers can tell which attribute failed without parsing the message. + * The error data maps the attribute's input path, such as `data[title]`, to the message + * under `params`, so callers can tell which attribute failed without parsing the + * translated message. * * @since 7.3.0 * * @param string $attribute The attribute name, such as `title`, or `data` for the whole object. - * @param string $reason Why the attribute cannot be applied. + * @param string $message The error message. * @return WP_Error The invalid attribute error. */ - private function invalid_attribute_error( string $attribute, string $reason ): WP_Error { + private function invalid_attribute_error( string $attribute, string $message ): WP_Error { $param = 'data' === $attribute ? 'data' : "data[{$attribute}]"; return new WP_Error( 'content_invalid_attribute', - /* translators: %s: Attribute name. */ - sprintf( __( 'Invalid attribute: %s.' ), $attribute ), + $message, array( 'status' => 400, - 'params' => array( $param => $reason ), + 'params' => array( $param => $message ), ) ); } @@ -2929,8 +2892,8 @@ private function invalid_attribute_error( string $attribute, string $reason ): W */ private function delete_failed_error(): WP_Error { return new WP_Error( - 'content_cannot_delete', - __( 'The post cannot be deleted.' ), + 'content_delete_failed', + __( 'The post could not be deleted.' ), array( 'status' => 500 ) ); } diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php new file mode 100644 index 0000000000000..7373a3552ac98 --- /dev/null +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -0,0 +1,372 @@ + + */ + protected const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); + + /** + * An ID that no post or user has. + * + * @since 7.3.0 + * + * @var int + */ + protected const MISSING_ID = 987654321; + + /** + * The roles of the matrix tests. + * + * @since 7.3.0 + * + * @var list + */ + protected const ROLES = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); + + /** + * The statuses of the matrix posts. `trash_` is a post trashed from ``. + * + * @since 7.3.0 + * + * @var list + */ + protected const MATRIX_STATUSES = array( 'draft', 'pending', 'publish', 'future', 'private', 'trash_draft', 'trash_publish' ); + + /** + * Shared user IDs keyed by role or fixture name. + * + * @since 7.3.0 + * + * @var array + */ + protected static $user_ids = array(); + + /** + * Shared post IDs keyed by fixture name. + * + * @since 7.3.0 + * + * @var array + */ + protected static $post_ids = array(); + + /** + * Forgets the shared fixtures, so a test class never reads the ones of another. + * + * @since 7.3.0 + */ + public static function wpTearDownAfterClass(): void { + self::$user_ids = array(); + self::$post_ids = array(); + } + + /** + * Unregisters the content abilities and registers the content category. + * + * @since 7.3.0 + */ + public function set_up(): void { + parent::set_up(); + + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( ! wp_has_ability_category( 'content' ) ) { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + wp_register_ability_category( + 'content', + array( + 'label' => 'Content', + 'description' => 'Content.', + ) + ); + } + } + + /** + * Unregisters the content abilities and the category, and resets the post types. + * + * @since 7.3.0 + */ + public function tear_down(): void { + foreach ( self::CONTENT_ABILITIES as $ability_name ) { + if ( wp_has_ability( $ability_name ) ) { + wp_unregister_ability( $ability_name ); + } + } + + if ( wp_has_ability_category( 'content' ) ) { + wp_unregister_ability_category( 'content' ); + } + + /* + * A test may turn off `show_in_abilities` on `post` and `page`, and the next class's + * wpSetUpBeforeClass() runs before the next set_up() resets the post types. + */ + $this->reset_post_types(); + + parent::tear_down(); + } + + /** + * Registers the content abilities inside a faked init action. + * + * @since 7.3.0 + */ + protected function register_abilities(): void { + global $wp_current_filter; + $wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. + ( new WP_Abilities_Content() )->register(); + } + + /** + * Registers the content abilities and returns the callbacks of the ability under test. + * + * The callbacks call private methods, so tests that skip input validation or the + * permission check capture them from the registration arguments. + * + * @since 7.3.0 + * + * @return array The permission and execute callbacks, keyed by argument name. + */ + protected function get_ability_callbacks(): array { + $ability = static::ABILITY; + $callbacks = array(); + + add_filter( + 'wp_register_ability_args', + static function ( array $args, string $name ) use ( &$callbacks, $ability ): array { + if ( $ability === $name ) { + $callbacks = array( + 'permission_callback' => $args['permission_callback'], + 'execute_callback' => $args['execute_callback'], + ); + } + + return $args; + }, + 10, + 2 + ); + + $this->register_abilities(); + + $this->assertArrayHasKey( 'execute_callback', $callbacks, "The {$ability} ability should be registered with callbacks." ); + + return $callbacks; + } + + /** + * Runs the registered ability under test with the given input. + * + * @since 7.3.0 + * + * @param mixed $input The ability input. + * @return mixed The ability result. + */ + protected function run_ability( $input ) { + $ability = static::ABILITY; + + $this->assertTrue( wp_has_ability( $ability ), "The {$ability} ability should be registered." ); + + return wp_get_ability( $ability )->execute( $input ); + } + + /** + * Logs in as the shared user with the given role or fixture name. + * + * @since 7.3.0 + * + * @param string $role The role or fixture name. + */ + protected function login_as( string $role ): void { + wp_set_current_user( self::$user_ids[ $role ] ); + } + + /** + * Takes a snapshot of the stored content: the number of posts per type and status, + * revisions included, the number of meta rows and term relationships, and, when a post + * is given, the row and meta of that post. + * + * @since 7.3.0 + * + * @param int|null $post_id Optional. The post ID. Default null. + * @return array The snapshot. + */ + protected function snapshot( ?int $post_id = null ): array { + global $wpdb; + + $snapshot = array( + 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), + 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), + 'terms' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->term_relationships}" ), + ); + + if ( null !== $post_id ) { + $snapshot['post'] = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id ), ARRAY_A ); + $snapshot['post_meta'] = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d ORDER BY meta_id", $post_id ), ARRAY_A ); + } + + return $snapshot; + } + + /** + * Asserts that a result is an error with the given code and status. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $code The expected error code. + * @param int $status The expected error status. + * @param string $context What the call was, for the assertion messages. + */ + protected function assert_error( $result, string $code, int $status, string $context ): void { + $this->assertWPError( $result, "{$context}: the call should fail." ); + $this->assertSame( $code, $result->get_error_code(), "{$context}: the error code should be {$code}." ); + + $data = $result->get_error_data(); + $this->assertIsArray( $data, "{$context}: the error should carry data." ); + $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); + } + + /** + * Asserts that a result is the invalid attribute error for the given attribute. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $attribute The attribute name. + * @param string $context What the call was, for the assertion messages. + */ + protected function assert_invalid_attribute( $result, string $attribute, string $context ): void { + $this->assert_error( $result, 'content_invalid_attribute', 400, $context ); + + $message = $result->get_error_message(); + $this->assertNotSame( '', $message, "{$context}: the message should give the reason." ); + $this->assertSame( array( "data[{$attribute}]" => $message ), $result->get_error_data()['params'] ?? array(), "{$context}: the params should hold only the input path of the attribute, with the reason." ); + } + + /** + * Asserts that a result is the not-found error. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $context What the call was, for the assertion messages. + */ + protected function assert_not_found( $result, string $context ): void { + $this->assert_error( $result, 'content_not_found', 404, $context ); + $this->assertSame( 'The requested content was not found.', $result->get_error_message(), "{$context}: the message should be the not-found message." ); + } + + /** + * Removes the `description` keywords of a schema at every depth. + * + * @since 7.3.0 + * + * @param array $schema The schema. + * @return array The schema without descriptions. + */ + protected function strip_descriptions( array $schema ): array { + unset( $schema['description'] ); + foreach ( $schema as $key => $value ) { + if ( is_array( $value ) ) { + $schema[ $key ] = $this->strip_descriptions( $value ); + } + } + + return $schema; + } + + /** + * Returns posts that cannot be reached as a `post`. + * + * @return array The posts, as fixture names. + */ + public static function data_posts_that_are_not_posts(): array { + return array( + 'revision' => array( 'revision' ), + 'attachment' => array( 'attachment' ), + 'page' => array( 'admin_page' ), + 'missing' => array( 'missing' ), + 'reusable block' => array( 'reusable_block' ), + 'template' => array( 'template' ), + 'menu item' => array( 'menu_item' ), + 'autosave' => array( 'autosave' ), + ); + } + + /** + * Returns statuses that can never be set. + * + * @return array The statuses. + */ + public static function data_statuses_that_can_never_be_set(): array { + return array( + 'trash' => array( 'trash' ), + 'auto-draft' => array( 'auto-draft' ), + 'inherit' => array( 'inherit' ), + 'unregistered' => array( 'cw_unregistered' ), + 'request-pending' => array( 'request-pending' ), + 'request-confirmed' => array( 'request-confirmed' ), + 'upper case' => array( 'PUBLISH' ), + 'trailing space' => array( 'publish ' ), + 'empty string' => array( '' ), + 'comma-joined list' => array( 'draft,publish' ), + ); + } + + /** + * Returns authors that a user without `edit_others_posts` may not name. + * + * @return array The authors, as fixture names. + */ + public static function data_authors_named_without_edit_others_posts(): array { + return array( + 'existing user' => array( 'editor' ), + 'missing user' => array( 'missing' ), + ); + } + + /** + * Returns date values without a usable value for a direct call. + * + * @return array}> The dates. + */ + public static function data_dates_without_a_usable_value(): array { + return array( + 'empty date' => array( array() ), + 'site date that cannot be parsed' => array( array( 'site' => 'not a date' ) ), + 'GMT date that cannot be parsed' => array( array( 'gmt' => 'not a date' ) ), + ); + } +} diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index a3fea85a427eb..b78b542dbebf5 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -11,6 +11,15 @@ */ class Tests_Abilities_API_WpRegisterCoreContentAbility extends WP_UnitTestCase { + /** + * The content abilities, which every test unregisters before and after it runs. + * + * @since 7.3.0 + * + * @var list + */ + private const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); + /** * Globals that rendering a post changes: the global post and the globals that * setup_postdata() populates. @@ -142,7 +151,7 @@ public static function wpSetUpBeforeClass( $factory ): void { public function set_up(): void { parent::set_up(); - foreach ( array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ) as $ability_name ) { + foreach ( self::CONTENT_ABILITIES as $ability_name ) { if ( wp_has_ability( $ability_name ) ) { wp_unregister_ability( $ability_name ); } @@ -162,12 +171,12 @@ public function set_up(): void { } /** - * Removes the ability and its category, and resets the post types, after each test. + * Removes the content abilities and their category, and resets the post types, after each test. * * @since 7.2.0 */ public function tear_down(): void { - foreach ( array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ) as $ability_name ) { + foreach ( self::CONTENT_ABILITIES as $ability_name ) { if ( wp_has_ability( $ability_name ) ) { wp_unregister_ability( $ability_name ); } @@ -190,7 +199,7 @@ public function tear_down(): void { } /** - * Registers the core/content-query ability inside a faked init action. + * Registers the content abilities inside a faked init action. * * @since 7.2.0 */ @@ -5693,6 +5702,41 @@ public function test_malformed_date_is_null( string $field, string $column ): vo $this->assertNull( $result['items'][0][ $object ][ $key ], 'A malformed date should be null.' ); } + /** + * A stored date at the Unix epoch is returned: a timestamp of zero is a date, not a + * date that could not be parsed. + * + * @ticket 66706 + * @dataProvider data_date_fields + * @since 7.3.0 + * + * @param string $field The field path to request, such as `date.site`. + * @param string $column The cached post column to set to the Unix epoch. + */ + public function test_date_at_the_unix_epoch_is_returned( string $field, string $column ): void { + $this->login_as( 'administrator' ); + $this->register_ability(); + + $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); + + $this->replace_cached_post_date_columns( $post_id, array( $column => '1970-01-01 00:00:00' ) ); + + list( $object, $key ) = explode( '.', $field ); + + $result = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => array( + 'type' => 'post', + 'ids' => array( $post_id ), + ), + 'fields' => array( 'id', $field ), + ) + ); + + $this->assertIsArray( $result, 'A date at the Unix epoch should pass output validation.' ); + $this->assertSame( '1970-01-01T00:00:00+00:00', $result['items'][0][ $object ][ $key ] ?? null, 'A date at the Unix epoch should be returned.' ); + } + /** * Returns `where.author` values that are not an object holding an ID or a slug: values * that are not objects, empty values, and objects with an unknown property. @@ -6850,7 +6894,7 @@ private function create_trashed_post( string $author ): int { } /** - * Queries trashed posts and returns the IDs of the items. + * Runs a query and returns the IDs of the items. * * @since 7.3.0 * @@ -6873,10 +6917,8 @@ private function query_ids( array $where ): array { /** * `trash` requested by name returns the trashed posts to roles that may edit them. * - * Covers S14. - * - * @dataProvider data_roles_with_edit_access_to_other_users_posts * @ticket 66706 + * @dataProvider data_roles_with_edit_access_to_other_users_posts * @since 7.3.0 * * @param string $role The role to query as. @@ -6902,8 +6944,6 @@ public function test_trash_requested_by_name_returns_trashed_posts( string $role /** * `trash` can be requested next to another status. * - * Covers S14. - * * @ticket 66706 * @since 7.3.0 */ @@ -6969,8 +7009,6 @@ public function test_query_without_a_status_does_not_cover_trashed_posts(): void /** * Querying `trash` needs `edit_posts`. * - * Covers S14. - * * @ticket 66706 * @since 7.3.0 */ @@ -7009,10 +7047,8 @@ public static function data_roles_that_edit_only_their_own_trashed_posts(): arra * Trashed rows pass the per-post read check: a role that edits only its own posts gets its * own trashed post and not the trashed post of another user. * - * Covers S14. - * - * @dataProvider data_roles_that_edit_only_their_own_trashed_posts * @ticket 66706 + * @dataProvider data_roles_that_edit_only_their_own_trashed_posts * @since 7.3.0 * * @param string $role The role to query as. diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index 4ec3bada4e082..2aa37a2828180 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -2,6 +2,8 @@ declare( strict_types=1 ); +require_once __DIR__ . '/testcase-content-write-ability.php'; + /** * Tests for the core/content-create ability shipped with the Abilities API. * @@ -9,7 +11,7 @@ * * @group abilities-api */ -class Tests_Abilities_API_WpRegisterCoreContentCreateAbility extends WP_UnitTestCase { +class Tests_Abilities_API_WpRegisterCoreContentCreateAbility extends WP_Abilities_Content_Write_UnitTestCase { /** * The name of the ability under test. @@ -18,25 +20,7 @@ class Tests_Abilities_API_WpRegisterCoreContentCreateAbility extends WP_UnitTest * * @var string */ - private const ABILITY = 'core/content-create'; - - /** - * The content abilities, which every test unregisters before and after it runs. - * - * @since 7.3.0 - * - * @var list - */ - private const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); - - /** - * A user ID that no user has. - * - * @since 7.3.0 - * - * @var int - */ - private const MISSING_ID = 987654321; + protected const ABILITY = 'core/content-create'; /** * A date that stays in the future. @@ -45,25 +29,7 @@ class Tests_Abilities_API_WpRegisterCoreContentCreateAbility extends WP_UnitTest * * @var string */ - private const FUTURE_GMT = '2035-06-01T12:00:00'; - - /** - * Shared user IDs keyed by role or fixture name. - * - * @since 7.3.0 - * - * @var array - */ - private static $user_ids = array(); - - /** - * Shared post IDs keyed by fixture name. - * - * @since 7.3.0 - * - * @var array - */ - private static $post_ids = array(); + private const FUTURE_GMT = '2099-06-01T12:00:00'; /** * Creates shared users and posts. @@ -158,188 +124,12 @@ public static function wpSetUpBeforeClass( $factory ): void { } /** - * Unregisters the content abilities and registers the content category. - * - * @since 7.3.0 - */ - public function set_up(): void { - parent::set_up(); - - foreach ( self::CONTENT_ABILITIES as $ability_name ) { - if ( wp_has_ability( $ability_name ) ) { - wp_unregister_ability( $ability_name ); - } - } - - if ( ! wp_has_ability_category( 'content' ) ) { - global $wp_current_filter; - $wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. - wp_register_ability_category( - 'content', - array( - 'label' => 'Content', - 'description' => 'Content.', - ) - ); - } - } - - /** - * Unregisters the content abilities and the category, and resets the post types. - * - * @since 7.3.0 - */ - public function tear_down(): void { - foreach ( self::CONTENT_ABILITIES as $ability_name ) { - if ( wp_has_ability( $ability_name ) ) { - wp_unregister_ability( $ability_name ); - } - } - - if ( wp_has_ability_category( 'content' ) ) { - wp_unregister_ability_category( 'content' ); - } - - /* - * A test may turn off `show_in_abilities` on `post` and `page`, and the next class's - * wpSetUpBeforeClass() runs before the next set_up() resets the post types. - */ - $this->reset_post_types(); - - parent::tear_down(); - } - - /** - * Registers the content abilities inside a faked init action. - * - * @since 7.3.0 - */ - private function register_abilities(): void { - global $wp_current_filter; - $wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. - ( new WP_Abilities_Content() )->register(); - } - - /** - * Registers the content abilities and returns the callbacks of the create ability. - * - * The callbacks call private methods, so tests that skip input validation or the - * permission check capture them from the registration arguments. - * - * @since 7.3.0 - * - * @return array The permission and execute callbacks, keyed by argument name. - */ - private function get_ability_callbacks(): array { - $callbacks = array(); - - add_filter( - 'wp_register_ability_args', - static function ( array $args, string $name ) use ( &$callbacks ): array { - if ( self::ABILITY === $name ) { - $callbacks = array( - 'permission_callback' => $args['permission_callback'], - 'execute_callback' => $args['execute_callback'], - ); - } - - return $args; - }, - 10, - 2 - ); - - $this->register_abilities(); - - $this->assertArrayHasKey( 'execute_callback', $callbacks, 'The core/content-create ability should be registered with callbacks.' ); - - return $callbacks; - } - - /** - * Runs the registered create ability with the given input. - * - * @since 7.3.0 - * - * @param mixed $input The ability input. - * @return mixed The ability result. - */ - private function run_ability( $input ) { - $this->assertTrue( wp_has_ability( self::ABILITY ), 'The core/content-create ability should be registered.' ); - - return wp_get_ability( self::ABILITY )->execute( $input ); - } - - /** - * Logs in as the shared user with the given role or fixture name. - * - * @since 7.3.0 - * - * @param string $role The role or fixture name. - */ - private function login_as( string $role ): void { - wp_set_current_user( self::$user_ids[ $role ] ); - } - - /** - * Takes a snapshot of the stored content: the number of posts per type and status, - * revisions included, and the number of meta rows and term relationships. - * - * @since 7.3.0 - * - * @return array The snapshot. - */ - private function snapshot(): array { - global $wpdb; - - return array( - 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), - 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), - 'terms' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->term_relationships}" ), - ); - } - - /** - * Asserts that a result is an error with the given code and status. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $code The expected error code. - * @param int $status The expected error status. - * @param string $context What the call was, for the assertion messages. - */ - private function assert_error( $result, string $code, int $status, string $context ): void { - $this->assertWPError( $result, "{$context}: the call should fail." ); - $this->assertSame( $code, $result->get_error_code(), "{$context}: the error code should be {$code}." ); - - $data = $result->get_error_data(); - $this->assertIsArray( $data, "{$context}: the error should carry data." ); - $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); - } - - /** - * Asserts that a result is the invalid attribute error for the given attribute. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $attribute The attribute name. - * @param string $context What the call was, for the assertion messages. - */ - private function assert_invalid_attribute( $result, string $attribute, string $context ): void { - $this->assert_error( $result, 'content_invalid_attribute', 400, $context ); - $this->assertSame( "Invalid attribute: {$attribute}.", $result->get_error_message(), "{$context}: the message should name the attribute." ); - $this->assertSame( array( "data[{$attribute}]" ), array_keys( $result->get_error_data()['params'] ?? array() ), "{$context}: the params should hold only the input path of the attribute." ); - } - - /** - * Returns the newest post created by a call, or null when the call created none. + * Returns the post a create call returned. * * @since 7.3.0 * * @param mixed $result The ability result. - * @return WP_Post|null The created post. + * @return WP_Post|null The post with the returned ID, or null when there is none. */ private function get_created_post( $result ): ?WP_Post { $this->assertIsArray( $result, 'The create call should succeed.' ); @@ -348,25 +138,6 @@ private function get_created_post( $result ): ?WP_Post { return get_post( $result['id'] ); } - /** - * Removes the `description` keywords of a schema at every depth. - * - * @since 7.3.0 - * - * @param array $schema The schema. - * @return array The schema without descriptions. - */ - private function strip_descriptions( array $schema ): array { - unset( $schema['description'] ); - foreach ( $schema as $key => $value ) { - if ( is_array( $value ) ) { - $schema[ $key ] = $this->strip_descriptions( $value ); - } - } - - return $schema; - } - /** * Registers a post type exposed to abilities with the given supported features. * @@ -490,8 +261,6 @@ public function test_input_schema(): void { /** * The output is one post item, the same object as an item of the query. * - * Covers S11. - * * @ticket 66706 * @since 7.3.0 */ @@ -538,12 +307,12 @@ public function test_schema_enums_list_the_exposed_post_types_and_registered_sta } /** - * Example 1: a draft with the default fields. + * A post created with only a type and a title is a draft, returned in the default fields. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_creates_a_draft_with_the_default_fields(): void { + public function test_creates_a_draft_with_the_default_fields(): void { $this->login_as( 'administrator' ); $this->register_abilities(); @@ -558,11 +327,11 @@ public function test_example_creates_a_draft_with_the_default_fields(): void { $post = $this->get_created_post( $result ); - $this->assertSame( array( 'id', 'type', 'status', 'date', 'slug', 'title' ), array_keys( $result ), 'The default fields should be returned.' ); + $this->assertSameSets( array( 'id', 'type', 'status', 'date', 'slug', 'title' ), array_keys( $result ), 'The default fields should be returned.' ); $this->assertSame( $post->ID, $result['id'], 'The ID should be the ID of the new post.' ); $this->assertSame( 'post', $result['type'], 'The type should be post.' ); $this->assertSame( 'draft', $result['status'], 'The post should be a draft.' ); - $this->assertSame( array( 'site', 'gmt' ), array_keys( $result['date'] ), 'The date should be given in site time and GMT.' ); + $this->assertSameSets( array( 'site', 'gmt' ), array_keys( $result['date'] ), 'The date should be given in site time and GMT.' ); $this->assertNotFalse( rest_parse_date( $result['date']['site'] ), 'The site date should be a date-time.' ); $this->assertNotFalse( rest_parse_date( $result['date']['gmt'] ), 'The GMT date should be a date-time.' ); $this->assertSame( '', $result['slug'], 'A draft without a slug should have an empty slug.' ); @@ -571,12 +340,12 @@ public function test_example_creates_a_draft_with_the_default_fields(): void { } /** - * Example 2: a published child page with chosen fields. + * A published child page is returned in the chosen fields. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_creates_a_published_child_page_with_chosen_fields(): void { + public function test_creates_a_published_child_page_with_chosen_fields(): void { $this->set_permalink_structure( '/%postname%/' ); $parent_id = self::factory()->post->create( @@ -606,7 +375,7 @@ public function test_example_creates_a_published_child_page_with_chosen_fields() $post = $this->get_created_post( $result ); - $this->assertSame( array( 'id', 'status', 'link', 'title', 'content', 'parent' ), array_keys( $result ), 'The chosen fields should be returned.' ); + $this->assertSameSets( array( 'id', 'status', 'link', 'title', 'content', 'parent' ), array_keys( $result ), 'The chosen fields should be returned.' ); $this->assertSame( $post->ID, $result['id'], 'The ID should be the ID of the new page.' ); $this->assertSame( 'publish', $result['status'], 'The page should be published.' ); $this->assertSame( home_url( '/products/pricing/' ), $result['link'], 'The link should include the parent.' ); @@ -648,14 +417,12 @@ private function query_item( string $type, int $post_id, array $fields ): array } /** - * Example 3: an attribute the post type does not support. - * - * Covers S1 and S5. + * An attribute the post type does not support is an invalid attribute. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_rejects_a_parent_for_a_post_type_that_is_not_hierarchical(): void { + public function test_rejects_a_parent_for_a_post_type_that_is_not_hierarchical(): void { $this->login_as( 'administrator' ); $this->register_abilities(); @@ -673,9 +440,9 @@ public function test_example_rejects_a_parent_for_a_post_type_that_is_not_hierar $this->assertWPError( $result, 'A parent for a post should be rejected.' ); $this->assertSame( 'content_invalid_attribute', $result->get_error_code(), 'The error should be the invalid attribute error.' ); - $this->assertSame( 'Invalid attribute: parent.', $result->get_error_message(), 'The message should name the parent.' ); + $this->assertSame( 'The parent attribute is only supported for hierarchical post types.', $result->get_error_message(), 'The message should give the reason.' ); $this->assertSame( 400, $result->get_error_data()['status'], 'The status should be 400.' ); - $this->assertSame( array( 'data[parent]' => 'The post type is not hierarchical.' ), $result->get_error_data()['params'], 'The params should give the fixed reason for the parent.' ); + $this->assertSame( array( 'data[parent]' => 'The parent attribute is only supported for hierarchical post types.' ), $result->get_error_data()['params'], 'The params should give the reason for the parent.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -698,11 +465,8 @@ public static function data_users_refused_by_the_permission_callback(): array { /** * A logged-out user and a user without `create_posts` are refused before the ability runs. * - * Covers S4 and S5. - * - * @dataProvider data_users_refused_by_the_permission_callback - * * @ticket 66706 + * @dataProvider data_users_refused_by_the_permission_callback * @since 7.3.0 * * @param string|null $role The role to log in as, or null to stay logged out. @@ -766,11 +530,8 @@ public static function data_inputs_refused_by_the_permission_callback(): array { /** * The permission callback returns false for missing or malformed input. * - * Covers S1 and S3. - * - * @dataProvider data_inputs_refused_by_the_permission_callback - * * @ticket 66706 + * @dataProvider data_inputs_refused_by_the_permission_callback * @since 7.3.0 * * @param mixed $input The input. @@ -840,11 +601,8 @@ public static function data_create_by_role_and_status(): array { * `future`, `publish` and `private` need `publish_posts`. A scheduled post is sent with a * date in the future. * - * Covers S4, S5 and S7. - * - * @dataProvider data_create_by_role_and_status - * * @ticket 66706 + * @dataProvider data_create_by_role_and_status * @since 7.3.0 * * @param string $role The role to create as. @@ -893,8 +651,6 @@ public function test_create_by_role_and_status( string $role, ?string $status, s /** * A type whose `show_in_abilities` is turned off after registration cannot be created. * - * Covers S3 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -926,31 +682,17 @@ public function test_type_no_longer_exposed_cannot_be_created(): void { /** * Returns direct-call inputs without an exposed post type. * - * @return array The inputs. + * @return array}> The inputs. */ public static function data_direct_inputs_without_an_exposed_type(): array { return array( - 'no input' => array( - 'input' => null, - ), - 'no data' => array( + 'no data' => array( 'input' => array(), ), - 'data that is a string' => array( - 'input' => array( 'data' => 'post' ), - ), - 'no type' => array( + 'no type' => array( 'input' => array( 'data' => array( 'title' => 'x' ) ), ), - 'type that is a list' => array( - 'input' => array( - 'data' => array( - 'type' => array( 'post' ), - 'title' => 'x', - ), - ), - ), - 'attachment' => array( + 'attachment' => array( 'input' => array( 'data' => array( 'type' => 'attachment', @@ -958,7 +700,7 @@ public static function data_direct_inputs_without_an_exposed_type(): array { ), ), ), - 'revision' => array( + 'revision' => array( 'input' => array( 'data' => array( 'type' => 'revision', @@ -966,7 +708,7 @@ public static function data_direct_inputs_without_an_exposed_type(): array { ), ), ), - 'unexposed type' => array( + 'unexposed type' => array( 'input' => array( 'data' => array( 'type' => 'cw_hidden', @@ -980,16 +722,13 @@ public static function data_direct_inputs_without_an_exposed_type(): array { /** * Called directly, input without an exposed post type is the not-found error. * - * Covers S1, S3, S5 and S13. - * - * @dataProvider data_direct_inputs_without_an_exposed_type - * * @ticket 66706 + * @dataProvider data_direct_inputs_without_an_exposed_type * @since 7.3.0 * - * @param mixed $input The input. + * @param array $input The input. */ - public function test_direct_call_without_an_exposed_type_is_not_found( $input ): void { + public function test_direct_call_without_an_exposed_type_is_not_found( array $input ): void { register_post_type( 'cw_hidden', array( 'public' => true ) ); $this->login_as( 'administrator' ); @@ -1062,11 +801,8 @@ public static function data_invalid_data(): array { * Called directly by a user without `create_posts`, the call fails on the capability before * anything about the attributes is checked. * - * Covers S4, S5 and O2. - * - * @dataProvider data_invalid_data - * * @ticket 66706 + * @dataProvider data_invalid_data * @since 7.3.0 * * @param array $data The data. @@ -1114,11 +850,8 @@ public static function data_keys_outside_the_allow_list(): array { /** * A key outside the allow-list fails input validation. * - * Covers S1, S5 and S6. - * - * @dataProvider data_keys_outside_the_allow_list - * * @ticket 66706 + * @dataProvider data_keys_outside_the_allow_list * @since 7.3.0 * * @param string $key The key. @@ -1144,101 +877,11 @@ public function test_key_outside_the_allow_list_fails_input_validation( string $ $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Called directly, keys outside the allow-list are ignored and never written. - * - * Covers S6. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_ignores_keys_outside_the_allow_list(): void { - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $existing_before = get_post( self::$post_ids['published_post'] )->to_array(); - $category_id = self::factory()->category->create(); - - $result = $execute( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Allow-list', - 'ID' => self::$post_ids['published_post'], - 'post_type' => 'page', - 'post_password' => 'secret', - 'meta_input' => array( 'cw_key' => 'value' ), - 'tax_input' => array( 'category' => array( 1 ) ), - 'comment_status' => 'closed', - 'post_author' => self::$user_ids['editor'], - 'import_id' => 7654321, - 'guid' => 'https://example.org/forged', - 'page_template' => 'cw-template.php', - 'modified' => '2001-01-01T00:00:00', - 'post_modified' => '2001-01-01 00:00:00', - 'post_modified_gmt' => '2001-01-01 00:00:00', - 'sticky' => true, - 'tags_input' => array( 'cw-tag' ), - 'post_category' => array( $category_id ), - 'menu_order' => 5, - 'post_mime_type' => 'image/jpeg', - ), - 'unknown' => 'ignored', - ) - ); - - $post = $this->get_created_post( $result ); - - $this->assertNotSame( self::$post_ids['published_post'], $post->ID, 'A new post should be created.' ); - $this->assertNotSame( 7654321, $post->ID, 'The import ID should not be used.' ); - $this->assertSame( 'post', $post->post_type, 'The post type should come from data.type.' ); - $this->assertSame( 'Allow-list', $post->post_title, 'The title should be stored.' ); - $this->assertSame( '', $post->post_password, 'No password should be stored.' ); - $this->assertSame( array(), get_post_meta( $post->ID, 'cw_key' ), 'No meta should be stored.' ); - $this->assertSame( 'open', $post->comment_status, 'The comment status should be the default.' ); - $this->assertSame( self::$user_ids['administrator'], (int) $post->post_author, 'The author should be the current user.' ); - $this->assertStringNotContainsString( 'forged', $post->guid, 'The GUID should not be the sent one.' ); - $this->assertSame( '', get_post_meta( $post->ID, '_wp_page_template', true ), 'No page template should be stored.' ); - // A new draft without a date floats, so only the site-time modified date holds the save time. - $this->assertNotSame( '2001-01-01 00:00:00', $post->post_modified, 'The sent site-time modified date should not be stored.' ); - $this->assertNotSame( '2001-01-01 00:00:00', $post->post_modified_gmt, 'The sent GMT modified date should not be stored.' ); - $this->assertEqualsWithDelta( time(), strtotime( get_gmt_from_date( $post->post_modified ) . ' UTC' ), 60, 'The stored site-time modified date should be the time of the save.' ); - $this->assertFalse( is_sticky( $post->ID ), 'The post should not be sticky.' ); - $this->assertSame( array(), wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ), 'No tag should be stored.' ); - $this->assertSame( array( (int) get_option( 'default_category' ) ), wp_get_post_categories( $post->ID ), 'Only the default category should be stored.' ); - $this->assertSame( 0, $post->menu_order, 'The menu order should be 0.' ); - $this->assertSame( '', $post->post_mime_type, 'No MIME type should be stored.' ); - $this->assertSame( $existing_before, get_post( self::$post_ids['published_post'] )->to_array(), 'The post named by ID should not change.' ); - } - - /** - * Returns statuses that can never be set. - * - * @return array The statuses. - */ - public static function data_statuses_that_can_never_be_set(): array { - return array( - 'trash' => array( 'trash' ), - 'auto-draft' => array( 'auto-draft' ), - 'inherit' => array( 'inherit' ), - 'unregistered' => array( 'cw_unregistered' ), - 'request-pending' => array( 'request-pending' ), - 'request-confirmed' => array( 'request-confirmed' ), - 'upper case' => array( 'PUBLISH' ), - 'trailing space' => array( 'publish ' ), - 'empty string' => array( '' ), - 'comma-joined list' => array( 'draft,publish' ), - ); - } - /** * An internal or unregistered status fails input validation. * - * Covers S5 and S7. - * - * @dataProvider data_statuses_that_can_never_be_set - * * @ticket 66706 + * @dataProvider data_statuses_that_can_never_be_set * @since 7.3.0 * * @param string $status The status. @@ -1266,11 +909,8 @@ public function test_status_that_can_never_be_set_fails_input_validation( string /** * Called directly, an internal or unregistered status is an invalid status. * - * Covers S1, S5 and S7. - * - * @dataProvider data_statuses_that_can_never_be_set - * * @ticket 66706 + * @dataProvider data_statuses_that_can_never_be_set * @since 7.3.0 * * @param string $status The status. @@ -1297,8 +937,6 @@ public function test_direct_call_rejects_a_status_that_can_never_be_set( string /** * A status that is no longer registered when the call runs is an invalid status. * - * Covers S1, S5 and S7. - * * @ticket 66706 * @since 7.3.0 */ @@ -1387,13 +1025,10 @@ public static function data_custom_statuses_that_make_a_post_visible(): array { /** * A custom status that makes a post visible needs `publish_posts`, and the refusal uses the - * general message, also for a custom private status (C6). - * - * Covers S5 and S7. - * - * @dataProvider data_custom_statuses_that_make_a_post_visible + * general message, also for a custom private status. * * @ticket 66706 + * @dataProvider data_custom_statuses_that_make_a_post_visible * @since 7.3.0 * * @param string $status The status. @@ -1423,11 +1058,8 @@ public function test_custom_visible_status_needs_publish_posts( string $status ) /** * A user with `publish_posts` may set a custom status that makes a post visible. * - * Covers S7. - * - * @dataProvider data_custom_statuses_that_make_a_post_visible - * * @ticket 66706 + * @dataProvider data_custom_statuses_that_make_a_post_visible * @since 7.3.0 * * @param string $status The status. @@ -1455,8 +1087,6 @@ public function test_custom_visible_status_is_set_with_publish_posts( string $st * A custom status that is neither public, private nor publicly viewable needs nothing beyond * `create_posts`. * - * Covers S7. - * * @ticket 66706 * @since 7.3.0 */ @@ -1494,9 +1124,8 @@ public static function data_statuses_reported_as_stored(): array { /** * The regular save rules decide the stored status, and the output reports it. * - * @dataProvider data_statuses_reported_as_stored - * * @ticket 66706 + * @dataProvider data_statuses_reported_as_stored * @since 7.3.0 * * @param string $status The requested status. @@ -1523,27 +1152,12 @@ public function test_output_reports_the_stored_status( string $status, string $g $this->assertSame( $expected, $result['status'], 'The output should report the stored status.' ); } - /** - * Returns authors that a user without `edit_others_posts` may not name. - * - * @return array The authors, as fixture names. - */ - public static function data_authors_named_without_edit_others_posts(): array { - return array( - 'existing user' => array( 'editor' ), - 'missing user' => array( 'missing' ), - ); - } - /** * A user without `edit_others_posts` gets the same error for an existing and a missing * author, so the error never tells whether a user exists. * - * Covers S5, S8 and O3. - * - * @dataProvider data_authors_named_without_edit_others_posts - * * @ticket 66706 + * @dataProvider data_authors_named_without_edit_others_posts * @since 7.3.0 * * @param string $author The author, as a fixture name. @@ -1629,8 +1243,6 @@ public function test_naming_another_author_with_edit_others_posts(): void { /** * A user with `edit_others_posts` who names a missing user gets the invalid author error. * - * Covers S5 and S8. - * * @ticket 66706 * @since 7.3.0 */ @@ -1656,8 +1268,6 @@ public function test_naming_a_missing_author_with_edit_others_posts_is_an_invali /** * On multisite, a user who is not a member of the site cannot be the author. * - * Covers S5 and S8. - * * @group ms-required * * @ticket 66706 @@ -1691,8 +1301,6 @@ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_inva * On multisite, a user without `edit_others_posts` who names a user who is not a member of the * site gets the same error as for a missing user. * - * Covers S8 and O3. - * * @group ms-required * * @ticket 66706 @@ -1747,11 +1355,8 @@ public static function data_parents_that_cannot_be_used(): array { * A missing parent, a parent of another type and a parent the user may not read give one * identical error, so the error never tells whether a post exists. * - * Covers S5 and S9. - * - * @dataProvider data_parents_that_cannot_be_used - * * @ticket 66706 + * @dataProvider data_parents_that_cannot_be_used * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -1841,11 +1446,8 @@ public static function data_unsupported_attributes(): array { /** * An attribute the post type does not support is rejected, even when it would change nothing. * - * Covers S1, S5 and O4: the first failure is returned and nothing is written. - * - * @dataProvider data_unsupported_attributes - * * @ticket 66706 + * @dataProvider data_unsupported_attributes * @since 7.3.0 * * @param string $type The post type. @@ -1907,22 +1509,19 @@ public static function data_values_of_the_wrong_type(): array { } /** - * Called directly, a value of the wrong type is an invalid attribute. - * - * Covers S1 and S5. - * - * @dataProvider data_values_of_the_wrong_type + * A value of the wrong type fails input validation. * * @ticket 66706 + * @dataProvider data_values_of_the_wrong_type * @since 7.3.0 * * @param string $type The post type. * @param string $attribute The attribute. * @param mixed $value The value. */ - public function test_direct_call_rejects_a_value_of_the_wrong_type( string $type, string $attribute, $value ): void { + public function test_value_of_the_wrong_type_fails_input_validation( string $type, string $attribute, $value ): void { $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; + $this->register_abilities(); $data = array( 'type' => $type ); if ( 'title' !== $attribute ) { @@ -1931,9 +1530,10 @@ public function test_direct_call_rejects_a_value_of_the_wrong_type( string $type $data[ $attribute ] = $value; $before = $this->snapshot(); - $result = $execute( array( 'data' => $data ) ); + $result = $this->run_ability( array( 'data' => $data ) ); - $this->assert_invalid_attribute( $result, $attribute, 'A value of the wrong type' ); + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the value.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1966,8 +1566,6 @@ public function test_empty_title_is_valid(): void { /** * A save error keeps its own code and message, with status 400 when the post is empty. * - * Covers S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -2006,11 +1604,11 @@ public function test_database_error_is_the_save_error(): void { $before = $this->snapshot(); - $wpdb->suppress_errors = true; + $suppress = $wpdb->suppress_errors(); add_filter( 'query', static function ( $query ) { - return 0 === strpos( $query, 'INSERT' ) ? '],' : $query; + return str_starts_with( $query, 'INSERT' ) ? '],' : $query; } ); @@ -2023,7 +1621,7 @@ static function ( $query ) { ) ); - $wpdb->suppress_errors = false; + $wpdb->suppress_errors( $suppress ); $this->assert_error( $result, 'db_insert_error', 500, 'A database error' ); $this->assertSame( 'Could not insert post into the database.', $result->get_error_message(), 'The message should be the message of the save error.' ); @@ -2072,9 +1670,8 @@ public static function data_slugs(): array { /** * A slug is sanitized and stored. * - * @dataProvider data_slugs - * * @ticket 66706 + * @dataProvider data_slugs * @since 7.3.0 * * @param string $slug The sent slug. @@ -2102,8 +1699,6 @@ public function test_slug_is_sanitized_and_stored( string $slug, string $expecte /** * A slug that sanitizes to nothing is an invalid slug. * - * Covers S1 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -2126,6 +1721,65 @@ public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } + /** + * A user who may not publish posts cannot set the slug of a pending post. + * + * Intentionally strict: the save would drop the slug, so it is an error instead of being + * dropped without notice. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_slug_of_a_pending_post_is_rejected_without_publish_posts(): void { + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Pending slug', + 'status' => 'pending', + 'slug' => 'pending-slug', + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'slug', 'A slug on a pending post without publish_posts' ); + $this->assertSame( 'The slug of a pending post can only be set by a user who can publish the post.', $result->get_error_message(), 'The message should say who may set the slug.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for a slug that cannot be stored.' ); + } + + /** + * A user who may publish posts sets the slug of a pending post. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_slug_of_a_pending_post_is_stored_with_publish_posts(): void { + $this->login_as( 'publisher' ); + $this->register_abilities(); + + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Pending slug', + 'status' => 'pending', + 'slug' => 'pending-slug', + ), + 'fields' => array( 'status', 'slug' ), + ) + ); + + $post = $this->get_created_post( $result ); + + $this->assertSame( 'pending', $post->post_status, 'The post should be pending.' ); + $this->assertSame( 'pending-slug', $post->post_name, 'The slug of the pending post should be stored.' ); + $this->assertSame( 'pending-slug', $result['slug'], 'The slug of the pending post should be returned.' ); + } + /** * Returns statuses for which the slug of a published post is made unique. * @@ -2143,9 +1797,8 @@ public static function data_statuses_with_unique_slugs(): array { /** * A new post never takes the slug of another post, also as a draft or a pending post. * - * @dataProvider data_statuses_with_unique_slugs - * * @ticket 66706 + * @dataProvider data_statuses_with_unique_slugs * @since 7.3.0 * * @param string|null $status The status, or null to send none. @@ -2183,9 +1836,8 @@ public static function data_draft_page_slugs_by_parent(): array { /** * A draft page is made unique among the pages that share its parent only. * - * @dataProvider data_draft_page_slugs_by_parent - * * @ticket 66706 + * @dataProvider data_draft_page_slugs_by_parent * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -2234,9 +1886,8 @@ public static function data_dates(): array { /** * A sent date sets both the local and the GMT date of a new draft. * - * @dataProvider data_dates - * * @ticket 66706 + * @dataProvider data_dates * @since 7.3.0 * * @param array $date The sent date. @@ -2293,8 +1944,6 @@ public function test_draft_without_a_date_floats(): void { /** * A date that is not a date-time fails input validation. * - * Covers S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -2389,11 +2038,8 @@ private function create_with_markup( string $markup ): array { /** * The markup of an author, who lacks `unfiltered_html`, is filtered as on any other save. * - * Covers S10. - * - * @dataProvider data_filtered_markup - * * @ticket 66706 + * @dataProvider data_filtered_markup * @since 7.3.0 * * @param string $input The sent markup. @@ -2420,8 +2066,6 @@ public function test_markup_of_an_author_is_filtered( string $input, string $tit /** * The markup of a contributor is filtered. * - * Covers S10. - * * @ticket 66706 * @since 7.3.0 */ @@ -2438,8 +2082,6 @@ public function test_markup_of_a_contributor_is_filtered(): void { /** * On multisite, the markup of a site administrator, who lacks `unfiltered_html`, is filtered. * - * Covers S10. - * * @group ms-required * * @ticket 66706 @@ -2475,8 +2117,6 @@ private function assert_markup_is_stored_unchanged(): void { /** * The markup of an administrator with `unfiltered_html` is stored unchanged. * - * Covers S10. - * * @group ms-excluded * * @ticket 66706 @@ -2491,8 +2131,6 @@ public function test_markup_of_an_administrator_is_not_filtered(): void { /** * On multisite, the markup of a super admin is stored unchanged. * - * Covers S10. - * * @group ms-required * * @ticket 66706 @@ -2545,8 +2183,6 @@ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip( * * The user may publish posts but not edit published posts. * - * Covers S11. - * * @ticket 66706 * @since 7.3.0 */ @@ -2577,8 +2213,6 @@ public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new /** * The current user is checked at each call, also on the same instance. * - * Covers S4. - * * @ticket 66706 * @since 7.3.0 */ @@ -2606,8 +2240,6 @@ public function test_each_call_checks_the_current_user(): void { /** * Only the requested fields are returned, and the ID always is. * - * Covers S11. - * * @ticket 66706 * @since 7.3.0 */ @@ -2644,146 +2276,13 @@ public function test_returns_the_requested_fields_and_the_id(): void { } /** - * Returns an integer in one of the forms that are read as that integer. - * - * @since 7.3.0 - * - * @param int $value The integer. - * @param string $form The form: `float`, `decimal`, `plus`, `leading space`, `trailing space`, - * `leading newline` or `exponent`. - * @return float|string The integer in that form. - */ - private static function whole_number_form( int $value, string $form ) { - $forms = array( - 'float' => (float) $value, - 'decimal' => "{$value}.0", - 'plus' => "+{$value}", - 'leading space' => " {$value}", - 'trailing space' => "{$value} ", - 'leading newline' => "\n{$value}", - 'exponent' => "{$value}e0", - ); - - return $forms[ $form ]; - } - - /** - * Returns the forms of a whole number that are read as the integer they equal. - * - * @return array The forms. - */ - public static function data_whole_number_forms(): array { - return array( - 'whole float' => array( 'float' ), - 'string with .0' => array( 'decimal' ), - 'string with plus' => array( 'plus' ), - 'string with leading space' => array( 'leading space' ), - 'string with trailing space' => array( 'trailing space' ), - 'string with leading line' => array( 'leading newline' ), - 'string with an exponent' => array( 'exponent' ), - ); - } - - /** - * Called directly, an author in another whole-number form is read as the integer it equals. - * - * Covers C3. - * - * @dataProvider data_whole_number_forms + * Called directly, an empty date or a date that cannot be parsed is an invalid attribute. * * @ticket 66706 - * @since 7.3.0 - * - * @param string $form The form of the author. - */ - public function test_direct_call_reads_an_author_in_a_whole_number_form( string $form ): void { - $this->login_as( 'editor' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Whole number author', - 'author' => self::whole_number_form( self::$user_ids['author'], $form ), - ), - ) - ); - - $this->assertSame( self::$user_ids['author'], (int) $this->get_created_post( $result )->post_author, 'The user with that ID should be the author.' ); - } - - /** - * Called directly, a parent in another whole-number form is read as the integer it equals. - * - * Covers C3. - * - * @dataProvider data_whole_number_forms - * - * @ticket 66706 - * @since 7.3.0 - * - * @param string $form The form of the parent. - */ - public function test_direct_call_reads_a_parent_in_a_whole_number_form( string $form ): void { - $parent_id = self::$post_ids['published_page']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'data' => array( - 'type' => 'page', - 'title' => 'Whole number parent', - 'parent' => self::whole_number_form( $parent_id, $form ), - ), - ) - ); - - $this->assertSame( $parent_id, $this->get_created_post( $result )->post_parent, 'The page with that ID should be the parent.' ); - } - - /** - * Returns date values without a usable value for a direct call. - * - * @return array}> The dates. - */ - public static function data_dates_without_a_usable_value(): array { - return array( - 'empty date' => array( array() ), - 'GMT that is a number' => array( - array( - 'site' => '2016-12-12T14:00:00', - 'gmt' => 123, - ), - ), - 'GMT that is a list' => array( - array( - 'site' => '2016-12-12T14:00:00', - 'gmt' => array( '2016-12-12T19:00:00' ), - ), - ), - 'GMT that is null' => array( - array( - 'site' => '2016-12-12T14:00:00', - 'gmt' => null, - ), - ), - ); - } - - /** - * Called directly, an empty date or a GMT value of the wrong type is an invalid attribute. - * - * Covers S5 (C2). - * * @dataProvider data_dates_without_a_usable_value - * - * @ticket 66706 * @since 7.3.0 * - * @param array $date The date. + * @param array $date The date. */ public function test_direct_call_rejects_a_date_without_a_usable_value( array $date ): void { $this->login_as( 'administrator' ); @@ -2804,46 +2303,9 @@ public function test_direct_call_rejects_a_date_without_a_usable_value( array $d $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * A user who may create posts but not publish them cannot create a private post. - * - * Covers S5 and S7. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_private_post_needs_publish_posts_from_a_user_who_may_create_posts(): void { - $user_id = self::factory()->user->create( array( 'role' => 'author' ) ); - // Denies the capability that the role grants. - get_userdata( $user_id )->add_cap( 'publish_posts', false ); - wp_set_current_user( $user_id ); - - $this->assertTrue( current_user_can( 'edit_posts' ), 'Precondition: the user should be able to create posts.' ); - $this->assertFalse( current_user_can( 'publish_posts' ), 'Precondition: the user should not be able to publish posts.' ); - - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Private without publish_posts', - 'status' => 'private', - ), - ) - ); - - $this->assert_error( $result, 'content_cannot_publish', 403, 'A private post without publish_posts' ); - $this->assertSame( 'Sorry, you are not allowed to create private posts in this post type.', $result->get_error_message(), 'The message should be the private message.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for the refused private post.' ); - } - /** * A pending post that a contributor creates without a date has a floating date. * - * Covers S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -2871,8 +2333,6 @@ public function test_pending_post_of_a_contributor_without_a_date_floats(): void * A type that gets `show_in_abilities` after the abilities were registered is not in the type * enum, so a create of that type fails input validation. * - * Covers S3 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -2933,8 +2393,6 @@ private function register_type_with_its_own_create_capability(): void { * A user with only the create capability of a type may create a draft of it, but may not * publish it. * - * Covers S5 and S7. - * * @ticket 66706 * @since 7.3.0 */ @@ -2983,8 +2441,6 @@ public function test_user_with_only_the_create_capability_creates_a_draft_and_ca * Called directly, a user with only the edit capability of a type, and not its create * capability, may not create a post of it. * - * Covers S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -3017,8 +2473,6 @@ public function test_direct_call_with_only_the_edit_capability_cannot_create(): * On multisite, after switching to a second site, a user who is an editor only on the first * site may not create posts there. * - * Covers S4 and S8. - * * @group ms-required * * @ticket 66706 @@ -3059,8 +2513,6 @@ public function test_editor_of_the_first_site_only_is_refused_on_the_second_site * On multisite, after switching to a second site, an editor of that site who names a user who * is a member only of the first site gets the invalid author error. * - * Covers S5 and S8. - * * @group ms-required * * @ticket 66706 @@ -3098,8 +2550,6 @@ public function test_member_of_the_first_site_only_is_an_invalid_author_on_the_s /** * A page in the Trash that the user may read is accepted as the parent. * - * Covers S9 (C13). - * * @ticket 66706 * @since 7.3.0 */ diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index a0799e07bf0e5..b7471dda26827 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -2,6 +2,8 @@ declare( strict_types=1 ); +require_once __DIR__ . '/testcase-content-write-ability.php'; + /** * Tests for the core/content-delete ability shipped with the Abilities API. * @@ -9,7 +11,7 @@ * * @group abilities-api */ -class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_UnitTestCase { +class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_Abilities_Content_Write_UnitTestCase { /** * The name of the ability under test. @@ -18,61 +20,7 @@ class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_UnitTest * * @var string */ - private const ABILITY = 'core/content-delete'; - - /** - * The content abilities, which every test unregisters before and after it runs. - * - * @since 7.3.0 - * - * @var list - */ - private const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); - - /** - * An ID that no post has. - * - * @since 7.3.0 - * - * @var int - */ - private const MISSING_ID = 987654321; - - /** - * The roles of the matrix tests. - * - * @since 7.3.0 - * - * @var list - */ - private const ROLES = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); - - /** - * The statuses of the matrix posts. `trash_` is a post trashed from ``. - * - * @since 7.3.0 - * - * @var list - */ - private const MATRIX_STATUSES = array( 'draft', 'pending', 'publish', 'future', 'private', 'trash_draft', 'trash_publish' ); - - /** - * Shared user IDs keyed by role or fixture name. - * - * @since 7.3.0 - * - * @var array - */ - private static $user_ids = array(); - - /** - * Shared post IDs keyed by fixture name. - * - * @since 7.3.0 - * - * @var array - */ - private static $post_ids = array(); + protected const ABILITY = 'core/content-delete'; /** * Creates shared users and posts. @@ -95,7 +43,7 @@ public static function wpSetUpBeforeClass( $factory ): void { array( 'post_author' => self::$user_ids[ $owner ], 'post_status' => $stored_status, - 'post_date' => 'future' === $stored_status ? '2035-01-01 00:00:00' : '', + 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', ) ); if ( $stored_status !== $status ) { @@ -163,119 +111,6 @@ public static function wpSetUpBeforeClass( $factory ): void { } } - /** - * Unregisters the content abilities and registers the content category. - * - * @since 7.3.0 - */ - public function set_up(): void { - parent::set_up(); - - foreach ( self::CONTENT_ABILITIES as $ability_name ) { - if ( wp_has_ability( $ability_name ) ) { - wp_unregister_ability( $ability_name ); - } - } - - if ( ! wp_has_ability_category( 'content' ) ) { - global $wp_current_filter; - $wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. - wp_register_ability_category( - 'content', - array( - 'label' => 'Content', - 'description' => 'Content.', - ) - ); - } - } - - /** - * Unregisters the content abilities and the category, and resets the post types. - * - * @since 7.3.0 - */ - public function tear_down(): void { - foreach ( self::CONTENT_ABILITIES as $ability_name ) { - if ( wp_has_ability( $ability_name ) ) { - wp_unregister_ability( $ability_name ); - } - } - - if ( wp_has_ability_category( 'content' ) ) { - wp_unregister_ability_category( 'content' ); - } - - /* - * A test may turn off `show_in_abilities` on `post` and `page`, and the next class's - * wpSetUpBeforeClass() runs before the next set_up() resets the post types. - */ - $this->reset_post_types(); - - parent::tear_down(); - } - - /** - * Registers the content abilities inside a faked init action. - * - * @since 7.3.0 - */ - private function register_abilities(): void { - global $wp_current_filter; - $wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. - ( new WP_Abilities_Content() )->register(); - } - - /** - * Registers the content abilities and returns the callbacks of the delete ability. - * - * The callbacks call private methods, so tests that skip input validation or the - * permission check capture them from the registration arguments. - * - * @since 7.3.0 - * - * @return array The permission and execute callbacks, keyed by argument name. - */ - private function get_ability_callbacks(): array { - $callbacks = array(); - - add_filter( - 'wp_register_ability_args', - static function ( array $args, string $name ) use ( &$callbacks ): array { - if ( self::ABILITY === $name ) { - $callbacks = array( - 'permission_callback' => $args['permission_callback'], - 'execute_callback' => $args['execute_callback'], - ); - } - - return $args; - }, - 10, - 2 - ); - - $this->register_abilities(); - - $this->assertArrayHasKey( 'execute_callback', $callbacks, 'The core/content-delete ability should be registered with callbacks.' ); - - return $callbacks; - } - - /** - * Runs the registered delete ability with the given input. - * - * @since 7.3.0 - * - * @param mixed $input The ability input. - * @return mixed The ability result. - */ - private function run_ability( $input ) { - $this->assertTrue( wp_has_ability( self::ABILITY ), 'The core/content-delete ability should be registered.' ); - - return wp_get_ability( self::ABILITY )->execute( $input ); - } - /** * Builds the input that deletes a post. * @@ -299,69 +134,6 @@ private function delete_input( int $post_id, ?bool $permanent = null ): array { return $input; } - /** - * Logs in as the shared user with the given role or fixture name. - * - * @since 7.3.0 - * - * @param string $role The role or fixture name. - */ - private function login_as( string $role ): void { - wp_set_current_user( self::$user_ids[ $role ] ); - } - - /** - * Takes a snapshot of the stored content: the number of posts per type and status, - * revisions included, the number of meta rows, and the row and meta of one post. - * - * @since 7.3.0 - * - * @param int $post_id The post ID. - * @return array The snapshot. - */ - private function snapshot( int $post_id ): array { - global $wpdb; - - return array( - 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), - 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), - 'post' => $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id ), ARRAY_A ), - 'post_meta' => $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d ORDER BY meta_id", $post_id ), ARRAY_A ), - ); - } - - /** - * Asserts that a result is an error with the given code and status. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $code The expected error code. - * @param int $status The expected error status. - * @param string $context What the call was, for the assertion messages. - */ - private function assert_error( $result, string $code, int $status, string $context ): void { - $this->assertWPError( $result, "{$context}: the call should fail." ); - $this->assertSame( $code, $result->get_error_code(), "{$context}: the error code should be {$code}." ); - - $data = $result->get_error_data(); - $this->assertIsArray( $data, "{$context}: the error should carry data." ); - $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); - } - - /** - * Asserts that a result is the not-found error. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $context What the call was, for the assertion messages. - */ - private function assert_not_found( $result, string $context ): void { - $this->assert_error( $result, 'content_not_found', 404, $context ); - $this->assertSame( 'The requested content was not found.', $result->get_error_message(), "{$context}: the message should be the not-found message." ); - } - /** * Asserts that a result is the 403 delete error. * @@ -409,25 +181,6 @@ private function assert_deleted( $result, int $post_id, string $context ): void $this->assertNull( get_post( $post_id ), "{$context}: the post should no longer exist." ); } - /** - * Removes the `description` keywords of a schema at every depth. - * - * @since 7.3.0 - * - * @param array $schema The schema. - * @return array The schema without descriptions. - */ - private function strip_descriptions( array $schema ): array { - unset( $schema['description'] ); - foreach ( $schema as $key => $value ) { - if ( is_array( $value ) ) { - $schema[ $key ] = $this->strip_descriptions( $value ); - } - } - - return $schema; - } - /** * The ability is registered in the content category with its label and annotations. * @@ -513,14 +266,12 @@ public function test_output_schema(): void { } /** - * Example 1: trashing a post. - * - * Covers S12. + * A post is moved to the Trash by default. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_trashes_a_post(): void { + public function test_trashes_a_post(): void { $post_id = self::$post_ids['admin_post']; $this->login_as( 'administrator' ); @@ -541,14 +292,12 @@ public function test_example_trashes_a_post(): void { } /** - * Example 2: deleting a page permanently. - * - * Covers S12. + * A page is deleted permanently with `permanent`. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_deletes_a_page_permanently(): void { + public function test_deletes_a_page_permanently(): void { $post_id = self::$post_ids['admin_page']; $this->login_as( 'administrator' ); @@ -570,14 +319,12 @@ public function test_example_deletes_a_page_permanently(): void { } /** - * Example 3: trashing the post of example 1 again. - * - * Covers S5 and S12. + * Trashing a post that is already in the Trash is the already-trashed error. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_rejects_trashing_a_trashed_post(): void { + public function test_rejects_trashing_a_trashed_post(): void { $post_id = self::$post_ids['admin_post']; $this->login_as( 'administrator' ); @@ -607,7 +354,7 @@ public function test_example_rejects_trashing_a_trashed_post(): void { 'message' => $result->get_error_message(), 'data' => $result->get_error_data(), ), - 'The error should be the already-trashed error of the example.' + 'The error should be the already-trashed error.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -615,8 +362,6 @@ public function test_example_rejects_trashing_a_trashed_post(): void { /** * A logged-out user is refused before the ability runs. * - * Covers S4 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -665,9 +410,8 @@ public static function data_inputs_refused_by_the_permission_callback(): array { /** * The permission callback returns false for missing or malformed input. * - * @dataProvider data_inputs_refused_by_the_permission_callback - * * @ticket 66706 + * @dataProvider data_inputs_refused_by_the_permission_callback * @since 7.3.0 * * @param mixed $input The input. @@ -715,11 +459,8 @@ public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): arra /** * An ID that is not a whole number of at least 1 fails input validation. * - * Covers S2 and S5. - * - * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 - * * @ticket 66706 + * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 * @since 7.3.0 * * @param mixed $id The ID. @@ -744,120 +485,13 @@ public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ) $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); } - /** - * Returns ID values that a direct call does not read as a whole number of at least 1. - * - * @return array The IDs. - */ - public static function data_ids_that_are_not_whole_numbers_in_a_direct_call(): array { - return array_merge( - self::data_ids_that_are_not_whole_numbers_of_at_least_1(), - array( - 'hexadecimal string' => array( '0x1' ), - 'negative zero' => array( -0.0 ), - 'number above 2 ** 53' => array( 9007199254740993 ), - 'string above 2 ** 53' => array( '9007199254740993' ), - 'exponent above 2 ** 53' => array( '1e16' ), - ) - ); - } - - /** - * Called directly, an ID that is not a whole number of at least 1 is the not-found error and - * is never cast, even when it starts with the ID of an existing post. - * - * Covers S2, S5 and S13 (C3). - * - * @dataProvider data_ids_that_are_not_whole_numbers_in_a_direct_call - * - * @ticket 66706 - * @since 7.3.0 - * - * @param mixed $id The ID. - */ - public function test_direct_call_with_an_id_that_is_not_a_whole_number_is_not_found( $id ): void { - $post_id = self::$post_ids['admin_post']; - if ( '123abc' === $id ) { - $id = "{$post_id}abc"; - } elseif ( 1.5 === $id ) { - $id = $post_id + 0.5; - } elseif ( '0x1' === $id ) { - // The hexadecimal form of the ID of an existing post. - $id = '0x' . dechex( $post_id ); - } - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $id, - ), - 'permanent' => true, - ) - ); - - $this->assert_not_found( $result, 'A direct call with an ID that is not a whole number' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Called directly, an ID made of digits is read as that number. - * - * Covers S2. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_reads_an_id_made_of_digits(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => (string) $post_id, - ), - ) - ); - - $this->assert_trashed( $result, $post_id, 'A direct call with an ID made of digits' ); - } - - /** - * Returns posts that cannot be reached as a `post`. - * - * @return array The posts, as fixture names. - */ - public static function data_posts_that_are_not_posts(): array { - return array( - 'revision' => array( 'revision' ), - 'attachment' => array( 'attachment' ), - 'page' => array( 'admin_page' ), - 'missing' => array( 'missing' ), - 'reusable block' => array( 'reusable_block' ), - 'template' => array( 'template' ), - 'menu item' => array( 'menu_item' ), - 'autosave' => array( 'autosave' ), - ); - } - /** * A revision, an autosave, an attachment, a post of another exposed or non-exposed type and a * missing post are not found as a `post`, even by an administrator and even for a permanent * delete. * - * Covers S3, S5 and S13. - * - * @dataProvider data_posts_that_are_not_posts - * * @ticket 66706 + * @dataProvider data_posts_that_are_not_posts * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -886,8 +520,6 @@ public function test_post_of_another_type_is_not_found( string $post ): void { /** * Called directly, a post of a type that is not exposed is not found, whatever its ID. * - * Covers S3 and S13. - * * @ticket 66706 * @since 7.3.0 */ @@ -916,8 +548,6 @@ public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): /** * A post type whose `show_in_abilities` is turned off after registration cannot be deleted. * - * Covers S3 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -942,31 +572,26 @@ public function test_type_no_longer_exposed_cannot_be_deleted(): void { /** * Returns direct-call inputs with a malformed `where`. * - * @return array The inputs. + * @return array}> The inputs. */ public static function data_direct_inputs_with_a_malformed_where(): array { return array( - 'no input' => array( null ), - 'no where' => array( array( 'permanent' => true ) ), - 'where that is a string' => array( array( 'where' => 'post' ) ), - 'no type' => array( array( 'where' => array( 'id' => 1 ) ) ), - 'no ID' => array( array( 'where' => array( 'type' => 'post' ) ) ), + 'no where' => array( array( 'permanent' => true ) ), + 'no type' => array( array( 'where' => array( 'id' => 1 ) ) ), + 'no ID' => array( array( 'where' => array( 'type' => 'post' ) ) ), ); } /** * Called directly, a malformed `where` is the not-found error. * - * Covers S1 and S13. - * - * @dataProvider data_direct_inputs_with_a_malformed_where - * * @ticket 66706 + * @dataProvider data_direct_inputs_with_a_malformed_where * @since 7.3.0 * - * @param mixed $input The input. + * @param array $input The input. */ - public function test_direct_call_with_a_malformed_where_is_not_found( $input ): void { + public function test_direct_call_with_a_malformed_where_is_not_found( array $input ): void { $this->login_as( 'administrator' ); $execute = $this->get_ability_callbacks()['execute_callback']; @@ -980,8 +605,6 @@ public function test_direct_call_with_a_malformed_where_is_not_found( $input ): * The lookup comes before the capability: a user who may delete nothing gets the not-found * error for a missing post, and the 403 error for an existing post. * - * Covers O1, S4 and S13. - * * @ticket 66706 * @since 7.3.0 */ @@ -1006,82 +629,82 @@ public function test_lookup_comes_before_the_capability(): void { } /** - * Returns the outcome of trashing each matrix post per role. + * Returns whether each role may delete each matrix post. * - * @return array The cases. + * @return array The cases. */ - public static function data_trash_matrix(): array { + public static function data_delete_matrix(): array { return array( - 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', 'trashed' ), - 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', 'trashed' ), - 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', 'trashed' ), - 'administrator, own, future' => array( 'administrator', 'administrator', 'future', 'trashed' ), - 'administrator, own, private' => array( 'administrator', 'administrator', 'private', 'trashed' ), - 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', 'already_trashed' ), - 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', 'already_trashed' ), - 'administrator, other, draft' => array( 'administrator', 'other', 'draft', 'trashed' ), - 'administrator, other, pending' => array( 'administrator', 'other', 'pending', 'trashed' ), - 'administrator, other, publish' => array( 'administrator', 'other', 'publish', 'trashed' ), - 'administrator, other, future' => array( 'administrator', 'other', 'future', 'trashed' ), - 'administrator, other, private' => array( 'administrator', 'other', 'private', 'trashed' ), - 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', 'already_trashed' ), - 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', 'already_trashed' ), - 'editor, own, draft' => array( 'editor', 'editor', 'draft', 'trashed' ), - 'editor, own, pending' => array( 'editor', 'editor', 'pending', 'trashed' ), - 'editor, own, publish' => array( 'editor', 'editor', 'publish', 'trashed' ), - 'editor, own, future' => array( 'editor', 'editor', 'future', 'trashed' ), - 'editor, own, private' => array( 'editor', 'editor', 'private', 'trashed' ), - 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', 'already_trashed' ), - 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', 'already_trashed' ), - 'editor, other, draft' => array( 'editor', 'other', 'draft', 'trashed' ), - 'editor, other, pending' => array( 'editor', 'other', 'pending', 'trashed' ), - 'editor, other, publish' => array( 'editor', 'other', 'publish', 'trashed' ), - 'editor, other, future' => array( 'editor', 'other', 'future', 'trashed' ), - 'editor, other, private' => array( 'editor', 'other', 'private', 'trashed' ), - 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', 'already_trashed' ), - 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', 'already_trashed' ), - 'author, own, draft' => array( 'author', 'author', 'draft', 'trashed' ), - 'author, own, pending' => array( 'author', 'author', 'pending', 'trashed' ), - 'author, own, publish' => array( 'author', 'author', 'publish', 'trashed' ), - 'author, own, future' => array( 'author', 'author', 'future', 'trashed' ), - 'author, own, private' => array( 'author', 'author', 'private', 'trashed' ), - 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', 'already_trashed' ), - 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', 'already_trashed' ), - 'author, other, draft' => array( 'author', 'other', 'draft', 'forbidden' ), - 'author, other, pending' => array( 'author', 'other', 'pending', 'forbidden' ), - 'author, other, publish' => array( 'author', 'other', 'publish', 'forbidden' ), - 'author, other, future' => array( 'author', 'other', 'future', 'forbidden' ), - 'author, other, private' => array( 'author', 'other', 'private', 'forbidden' ), - 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', 'forbidden' ), - 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', 'forbidden' ), - 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', 'trashed' ), - 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', 'trashed' ), - 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', 'forbidden' ), - 'contributor, own, future' => array( 'contributor', 'contributor', 'future', 'forbidden' ), - 'contributor, own, private' => array( 'contributor', 'contributor', 'private', 'trashed' ), - 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', 'already_trashed' ), - 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', 'forbidden' ), - 'contributor, other, draft' => array( 'contributor', 'other', 'draft', 'forbidden' ), - 'contributor, other, pending' => array( 'contributor', 'other', 'pending', 'forbidden' ), - 'contributor, other, publish' => array( 'contributor', 'other', 'publish', 'forbidden' ), - 'contributor, other, future' => array( 'contributor', 'other', 'future', 'forbidden' ), - 'contributor, other, private' => array( 'contributor', 'other', 'private', 'forbidden' ), - 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', 'forbidden' ), - 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', 'forbidden' ), - 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', 'forbidden' ), - 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', 'forbidden' ), - 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', 'forbidden' ), - 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', 'forbidden' ), - 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', 'forbidden' ), - 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', 'forbidden' ), - 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', 'forbidden' ), - 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', 'forbidden' ), - 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', 'forbidden' ), - 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', 'forbidden' ), - 'subscriber, other, future' => array( 'subscriber', 'other', 'future', 'forbidden' ), - 'subscriber, other, private' => array( 'subscriber', 'other', 'private', 'forbidden' ), - 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', 'forbidden' ), - 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', 'forbidden' ), + 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', true ), + 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', true ), + 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', true ), + 'administrator, own, future' => array( 'administrator', 'administrator', 'future', true ), + 'administrator, own, private' => array( 'administrator', 'administrator', 'private', true ), + 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', true ), + 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', true ), + 'administrator, other, draft' => array( 'administrator', 'other', 'draft', true ), + 'administrator, other, pending' => array( 'administrator', 'other', 'pending', true ), + 'administrator, other, publish' => array( 'administrator', 'other', 'publish', true ), + 'administrator, other, future' => array( 'administrator', 'other', 'future', true ), + 'administrator, other, private' => array( 'administrator', 'other', 'private', true ), + 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', true ), + 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', true ), + 'editor, own, draft' => array( 'editor', 'editor', 'draft', true ), + 'editor, own, pending' => array( 'editor', 'editor', 'pending', true ), + 'editor, own, publish' => array( 'editor', 'editor', 'publish', true ), + 'editor, own, future' => array( 'editor', 'editor', 'future', true ), + 'editor, own, private' => array( 'editor', 'editor', 'private', true ), + 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', true ), + 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', true ), + 'editor, other, draft' => array( 'editor', 'other', 'draft', true ), + 'editor, other, pending' => array( 'editor', 'other', 'pending', true ), + 'editor, other, publish' => array( 'editor', 'other', 'publish', true ), + 'editor, other, future' => array( 'editor', 'other', 'future', true ), + 'editor, other, private' => array( 'editor', 'other', 'private', true ), + 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', true ), + 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', true ), + 'author, own, draft' => array( 'author', 'author', 'draft', true ), + 'author, own, pending' => array( 'author', 'author', 'pending', true ), + 'author, own, publish' => array( 'author', 'author', 'publish', true ), + 'author, own, future' => array( 'author', 'author', 'future', true ), + 'author, own, private' => array( 'author', 'author', 'private', true ), + 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', true ), + 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', true ), + 'author, other, draft' => array( 'author', 'other', 'draft', false ), + 'author, other, pending' => array( 'author', 'other', 'pending', false ), + 'author, other, publish' => array( 'author', 'other', 'publish', false ), + 'author, other, future' => array( 'author', 'other', 'future', false ), + 'author, other, private' => array( 'author', 'other', 'private', false ), + 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', false ), + 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', false ), + 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', true ), + 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', true ), + 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', false ), + 'contributor, own, future' => array( 'contributor', 'contributor', 'future', false ), + 'contributor, own, private' => array( 'contributor', 'contributor', 'private', true ), + 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', true ), + 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', false ), + 'contributor, other, draft' => array( 'contributor', 'other', 'draft', false ), + 'contributor, other, pending' => array( 'contributor', 'other', 'pending', false ), + 'contributor, other, publish' => array( 'contributor', 'other', 'publish', false ), + 'contributor, other, future' => array( 'contributor', 'other', 'future', false ), + 'contributor, other, private' => array( 'contributor', 'other', 'private', false ), + 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', false ), + 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', false ), + 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', false ), + 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', false ), + 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', false ), + 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', false ), + 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', false ), + 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', false ), + 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', false ), + 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', false ), + 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', false ), + 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', false ), + 'subscriber, other, future' => array( 'subscriber', 'other', 'future', false ), + 'subscriber, other, private' => array( 'subscriber', 'other', 'private', false ), + 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', false ), + 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', false ), ); } @@ -1089,19 +712,16 @@ public static function data_trash_matrix(): array { * Each role may trash the posts it may delete, gets the 410 error for those already in the * Trash, and the 403 error for the others, whatever their status. * - * Covers S4, S5, S12 and S13. - * - * @dataProvider data_trash_matrix - * * @ticket 66706 + * @dataProvider data_delete_matrix * @since 7.3.0 * - * @param string $role The role to delete as. - * @param string $owner The role of the author of the post, or `other`. - * @param string $status The matrix status of the post. - * @param string $expected `trashed`, `already_trashed` or `forbidden`. + * @param string $role The role to delete as. + * @param string $owner The role of the author of the post, or `other`. + * @param string $status The matrix status of the post. + * @param bool $may_delete Whether the role may delete the post. */ - public function test_trash_matrix( string $role, string $owner, string $status, string $expected ): void { + public function test_trash_matrix( string $role, string $owner, string $status, bool $may_delete ): void { $post_id = self::$post_ids[ "{$owner}:{$status}" ]; $this->login_as( $role ); @@ -1110,12 +730,12 @@ public function test_trash_matrix( string $role, string $owner, string $status, $before = $this->snapshot( $post_id ); $result = $this->run_ability( $this->delete_input( $post_id ) ); - if ( 'trashed' === $expected ) { + if ( $may_delete && ! str_starts_with( $status, 'trash_' ) ) { $this->assert_trashed( $result, $post_id, 'Trashing a post the role may delete' ); return; } - if ( 'already_trashed' === $expected ) { + if ( $may_delete ) { $this->assert_error( $result, 'content_already_trashed', 410, 'Trashing a trashed post the role may delete' ); } else { $this->assert_cannot_delete( $result, 'Trashing a post the role may not delete' ); @@ -1123,103 +743,20 @@ public function test_trash_matrix( string $role, string $owner, string $status, $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - /** - * Returns the outcome of deleting each matrix post permanently per role. - * - * @return array The cases. - */ - public static function data_permanent_delete_matrix(): array { - return array( - 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', 'deleted' ), - 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', 'deleted' ), - 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', 'deleted' ), - 'administrator, own, future' => array( 'administrator', 'administrator', 'future', 'deleted' ), - 'administrator, own, private' => array( 'administrator', 'administrator', 'private', 'deleted' ), - 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', 'deleted' ), - 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', 'deleted' ), - 'administrator, other, draft' => array( 'administrator', 'other', 'draft', 'deleted' ), - 'administrator, other, pending' => array( 'administrator', 'other', 'pending', 'deleted' ), - 'administrator, other, publish' => array( 'administrator', 'other', 'publish', 'deleted' ), - 'administrator, other, future' => array( 'administrator', 'other', 'future', 'deleted' ), - 'administrator, other, private' => array( 'administrator', 'other', 'private', 'deleted' ), - 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', 'deleted' ), - 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', 'deleted' ), - 'editor, own, draft' => array( 'editor', 'editor', 'draft', 'deleted' ), - 'editor, own, pending' => array( 'editor', 'editor', 'pending', 'deleted' ), - 'editor, own, publish' => array( 'editor', 'editor', 'publish', 'deleted' ), - 'editor, own, future' => array( 'editor', 'editor', 'future', 'deleted' ), - 'editor, own, private' => array( 'editor', 'editor', 'private', 'deleted' ), - 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', 'deleted' ), - 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', 'deleted' ), - 'editor, other, draft' => array( 'editor', 'other', 'draft', 'deleted' ), - 'editor, other, pending' => array( 'editor', 'other', 'pending', 'deleted' ), - 'editor, other, publish' => array( 'editor', 'other', 'publish', 'deleted' ), - 'editor, other, future' => array( 'editor', 'other', 'future', 'deleted' ), - 'editor, other, private' => array( 'editor', 'other', 'private', 'deleted' ), - 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', 'deleted' ), - 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', 'deleted' ), - 'author, own, draft' => array( 'author', 'author', 'draft', 'deleted' ), - 'author, own, pending' => array( 'author', 'author', 'pending', 'deleted' ), - 'author, own, publish' => array( 'author', 'author', 'publish', 'deleted' ), - 'author, own, future' => array( 'author', 'author', 'future', 'deleted' ), - 'author, own, private' => array( 'author', 'author', 'private', 'deleted' ), - 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', 'deleted' ), - 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', 'deleted' ), - 'author, other, draft' => array( 'author', 'other', 'draft', 'forbidden' ), - 'author, other, pending' => array( 'author', 'other', 'pending', 'forbidden' ), - 'author, other, publish' => array( 'author', 'other', 'publish', 'forbidden' ), - 'author, other, future' => array( 'author', 'other', 'future', 'forbidden' ), - 'author, other, private' => array( 'author', 'other', 'private', 'forbidden' ), - 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', 'forbidden' ), - 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', 'forbidden' ), - 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', 'deleted' ), - 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', 'deleted' ), - 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', 'forbidden' ), - 'contributor, own, future' => array( 'contributor', 'contributor', 'future', 'forbidden' ), - 'contributor, own, private' => array( 'contributor', 'contributor', 'private', 'deleted' ), - 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', 'deleted' ), - 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', 'forbidden' ), - 'contributor, other, draft' => array( 'contributor', 'other', 'draft', 'forbidden' ), - 'contributor, other, pending' => array( 'contributor', 'other', 'pending', 'forbidden' ), - 'contributor, other, publish' => array( 'contributor', 'other', 'publish', 'forbidden' ), - 'contributor, other, future' => array( 'contributor', 'other', 'future', 'forbidden' ), - 'contributor, other, private' => array( 'contributor', 'other', 'private', 'forbidden' ), - 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', 'forbidden' ), - 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', 'forbidden' ), - 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', 'forbidden' ), - 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', 'forbidden' ), - 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', 'forbidden' ), - 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', 'forbidden' ), - 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', 'forbidden' ), - 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', 'forbidden' ), - 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', 'forbidden' ), - 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', 'forbidden' ), - 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', 'forbidden' ), - 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', 'forbidden' ), - 'subscriber, other, future' => array( 'subscriber', 'other', 'future', 'forbidden' ), - 'subscriber, other, private' => array( 'subscriber', 'other', 'private', 'forbidden' ), - 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', 'forbidden' ), - 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', 'forbidden' ), - ); - } - /** * Each role may delete permanently the posts it may delete, including posts in the Trash, and * gets the 403 error for the others, whatever their status. * - * Covers S4, S5, S12 and S13. - * - * @dataProvider data_permanent_delete_matrix - * * @ticket 66706 + * @dataProvider data_delete_matrix * @since 7.3.0 * - * @param string $role The role to delete as. - * @param string $owner The role of the author of the post, or `other`. - * @param string $status The matrix status of the post. - * @param string $expected `deleted` or `forbidden`. + * @param string $role The role to delete as. + * @param string $owner The role of the author of the post, or `other`. + * @param string $status The matrix status of the post. + * @param bool $may_delete Whether the role may delete the post. */ - public function test_permanent_delete_matrix( string $role, string $owner, string $status, string $expected ): void { + public function test_permanent_delete_matrix( string $role, string $owner, string $status, bool $may_delete ): void { $post_id = self::$post_ids[ "{$owner}:{$status}" ]; $this->login_as( $role ); @@ -1228,7 +765,7 @@ public function test_permanent_delete_matrix( string $role, string $owner, strin $before = $this->snapshot( $post_id ); $result = $this->run_ability( $this->delete_input( $post_id, true ) ); - if ( 'deleted' === $expected ) { + if ( $may_delete ) { $this->assert_deleted( $result, $post_id, 'Deleting a post the role may delete' ); return; } @@ -1240,8 +777,6 @@ public function test_permanent_delete_matrix( string $role, string $owner, strin /** * `permanent` set to false moves the post to the Trash. * - * Covers S12. - * * @ticket 66706 * @since 7.3.0 */ @@ -1255,71 +790,32 @@ public function test_permanent_false_trashes_the_post(): void { } /** - * Returns `permanent` values that are not boolean true. + * A post that cannot be moved to the Trash is not deleted in its place: the call fails + * and asks for `permanent`, which then deletes the post. * - * @return array The values. - */ - public static function data_permanent_values_that_are_not_true(): array { - return array( - 'string true' => array( 'true' ), - 'one' => array( 1 ), - 'string one' => array( '1' ), - 'string yes' => array( 'yes' ), - 'false' => array( false ), - 'string false' => array( 'false' ), - 'list' => array( array( true ) ), - 'whole float' => array( 1.0 ), - 'upper case' => array( 'TRUE' ), - ); - } - - /** - * Called directly, `permanent` counts only when it is boolean true: anything else moves the - * post to the Trash. - * - * Covers S12. - * - * @dataProvider data_permanent_values_that_are_not_true + * Attachments only support the Trash when `MEDIA_TRASH` is on, so the test exposes them. * * @ticket 66706 * @since 7.3.0 - * - * @param mixed $permanent The value. */ - public function test_direct_call_deletes_permanently_only_for_boolean_true( $permanent ): void { - $post_id = self::$post_ids['admin_post']; + public function test_post_without_trash_support_needs_permanent(): void { + $post_id = self::$post_ids['attachment']; - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; + get_post_type_object( 'attachment' )->show_in_abilities = true; - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'permanent' => $permanent, - ) - ); + $this->login_as( 'administrator' ); + $this->register_abilities(); - $this->assert_trashed( $result, $post_id, 'A direct call with a permanent value that is not true' ); - } + $this->assertFalse( MEDIA_TRASH, 'Precondition: attachments should not support the Trash.' ); - /** - * Called directly, `permanent` set to boolean true deletes the post permanently. - * - * Covers S12. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_deletes_permanently_for_boolean_true(): void { - $post_id = self::$post_ids['admin_post']; + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $this->delete_input( $post_id ) ); - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; + $this->assert_error( $result, 'content_trash_not_supported', 501, 'Trashing a post without Trash support' ); + $this->assertSame( 'The post does not support trashing. Set "permanent" to delete it.', $result->get_error_message(), 'The message should ask for a permanent delete.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'A post without Trash support should be left as it was.' ); - $this->assert_deleted( $execute( $this->delete_input( $post_id, true ) ), $post_id, 'A direct call with permanent true' ); + $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'Deleting a post without Trash support permanently' ); } /** @@ -1350,11 +846,8 @@ private function set_special_pages(): void { /** * An editor may delete neither the front page, the posts page nor the privacy policy page. * - * Covers S4, S5 and S13. - * - * @dataProvider data_special_pages - * * @ticket 66706 + * @dataProvider data_special_pages * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1376,13 +869,36 @@ public function test_editor_cannot_delete_a_special_page( string $page ): void { } /** - * On a single site, an administrator may delete the special pages. + * On a single site, an administrator may trash the special pages. * * @group ms-excluded * + * @ticket 66706 * @dataProvider data_special_pages + * @since 7.3.0 + * + * @param string $page The page, as a fixture name. + */ + public function test_administrator_can_trash_a_special_page( string $page ): void { + $this->set_special_pages(); + $post_id = self::$post_ids[ $page ]; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $this->assert_trashed( $this->run_ability( $this->delete_input( $post_id ) ), $post_id, 'An administrator trashing a special page' ); + } + + /** + * On a single site, an administrator may delete the special pages permanently. + * + * The page is not trashed first: trashing the front page or the posts page clears the + * option that makes it special. + * + * @group ms-excluded * * @ticket 66706 + * @dataProvider data_special_pages * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1394,7 +910,6 @@ public function test_administrator_can_delete_a_special_page( string $page ): vo $this->login_as( 'administrator' ); $this->register_abilities(); - $this->assert_trashed( $this->run_ability( $this->delete_input( $post_id ) ), $post_id, 'An administrator trashing a special page' ); $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'An administrator deleting a special page' ); } @@ -1416,13 +931,10 @@ public static function data_special_pages_for_a_site_administrator(): array { * On multisite, a site administrator may delete the front page and the posts page, but not * the privacy policy page. * - * Covers S4 and S5. - * * @group ms-required * - * @dataProvider data_special_pages_for_a_site_administrator - * * @ticket 66706 + * @dataProvider data_special_pages_for_a_site_administrator * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1452,9 +964,8 @@ public function test_site_administrator_deletes_special_pages_on_multisite( stri * * @group ms-required * - * @dataProvider data_special_pages - * * @ticket 66706 + * @dataProvider data_special_pages * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -1490,11 +1001,8 @@ public static function data_operations_that_are_stopped(): array { * When the trash or delete call fails or does not act, the result is the 500 error and the * post is left as it was. * - * Covers S5 and S12. - * - * @dataProvider data_operations_that_are_stopped - * * @ticket 66706 + * @dataProvider data_operations_that_are_stopped * @since 7.3.0 * * @param bool $permanent Whether the delete is permanent. @@ -1517,8 +1025,8 @@ static function () use ( $value ) { $before = $this->snapshot( $post_id ); $result = $this->run_ability( $this->delete_input( $post_id, $permanent ) ); - $this->assert_error( $result, 'content_cannot_delete', 500, 'An operation that does not act' ); - $this->assertSame( 'The post cannot be deleted.', $result->get_error_message(), 'The message should be the failure message.' ); + $this->assert_error( $result, 'content_delete_failed', 500, 'An operation that does not act' ); + $this->assertSame( 'The post could not be deleted.', $result->get_error_message(), 'The message should be the failure message.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -1539,11 +1047,8 @@ public static function data_keys_outside_the_allow_list(): array { /** * A key outside the allow-list fails input validation. * - * Covers S1 and S5. - * - * @dataProvider data_keys_outside_the_allow_list - * * @ticket 66706 + * @dataProvider data_keys_outside_the_allow_list * @since 7.3.0 * * @param string|null $location Where the key is sent, or null for the top level. @@ -1571,45 +1076,9 @@ public function test_key_outside_the_allow_list_fails_input_validation( ?string $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - /** - * Called directly, keys outside the allow-list are ignored: they neither make the delete - * permanent nor reach another post. - * - * Covers S6 and S12. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_ignores_keys_outside_the_allow_list(): void { - $post_id = self::$post_ids['admin_post']; - $other_id = self::$post_ids['administrator:publish']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $other_before = $this->snapshot( $other_id )['post']; - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - 'ID' => $other_id, - ), - 'force' => true, - 'ID' => $other_id, - ) - ); - - $this->assert_trashed( $result, $post_id, 'A direct call with unknown keys' ); - $this->assertSame( $other_before, $this->snapshot( $other_id )['post'], 'The post named by ID should not change.' ); - } - /** * The current user is checked at each call, also on the same instance. * - * Covers S4. - * * @ticket 66706 * @since 7.3.0 */ @@ -1629,60 +1098,4 @@ public function test_each_call_checks_the_current_user(): void { $this->assert_cannot_delete( $second, 'The second call as a contributor' ); $this->assertSame( $before, $this->snapshot( $other_id ), 'The second call should write nothing.' ); } - - /** - * Returns the forms of a whole number that are read as the integer they equal. - * - * @return array The forms. - */ - public static function data_whole_number_forms(): array { - return array( - 'whole float' => array( 'float' ), - 'string with .0' => array( 'decimal' ), - 'string with plus' => array( 'plus' ), - 'string with leading space' => array( 'leading space' ), - 'string with trailing space' => array( 'trailing space' ), - 'string with leading line' => array( 'leading newline' ), - 'string with an exponent' => array( 'exponent' ), - ); - } - - /** - * Called directly, an ID in another whole-number form is read as the integer it equals. - * - * Covers S2 (C3). - * - * @dataProvider data_whole_number_forms - * - * @ticket 66706 - * @since 7.3.0 - * - * @param string $form The form of the ID. - */ - public function test_direct_call_reads_an_id_in_a_whole_number_form( string $form ): void { - $post_id = self::$post_ids['admin_post']; - $forms = array( - 'float' => (float) $post_id, - 'decimal' => "{$post_id}.0", - 'plus' => "+{$post_id}", - 'leading space' => " {$post_id}", - 'trailing space' => "{$post_id} ", - 'leading newline' => "\n{$post_id}", - 'exponent' => "{$post_id}e0", - ); - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $forms[ $form ], - ), - ) - ); - - $this->assert_trashed( $result, $post_id, 'A direct call with an ID in a whole-number form' ); - } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index b4efc3c16d4a2..f9a58e20e8fc3 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -2,6 +2,8 @@ declare( strict_types=1 ); +require_once __DIR__ . '/testcase-content-write-ability.php'; + /** * Tests for the core/content-update ability shipped with the Abilities API. * @@ -9,7 +11,7 @@ * * @group abilities-api */ -class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_UnitTestCase { +class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_Abilities_Content_Write_UnitTestCase { /** * The name of the ability under test. @@ -18,61 +20,7 @@ class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_UnitTest * * @var string */ - private const ABILITY = 'core/content-update'; - - /** - * The content abilities, which every test unregisters before and after it runs. - * - * @since 7.3.0 - * - * @var list - */ - private const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); - - /** - * An ID that no post or user has. - * - * @since 7.3.0 - * - * @var int - */ - private const MISSING_ID = 987654321; - - /** - * The roles of the matrix tests. - * - * @since 7.3.0 - * - * @var list - */ - private const ROLES = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); - - /** - * The statuses of the matrix posts. `trash_` is a post trashed from ``. - * - * @since 7.3.0 - * - * @var list - */ - private const MATRIX_STATUSES = array( 'draft', 'pending', 'publish', 'future', 'private', 'trash_draft', 'trash_publish' ); - - /** - * Shared user IDs keyed by role or fixture name. - * - * @since 7.3.0 - * - * @var array - */ - private static $user_ids = array(); - - /** - * Shared post IDs keyed by fixture name. - * - * @since 7.3.0 - * - * @var array - */ - private static $post_ids = array(); + protected const ABILITY = 'core/content-update'; /** * Creates shared users and posts. @@ -104,7 +52,7 @@ public static function wpSetUpBeforeClass( $factory ): void { 'post_author' => self::$user_ids[ $owner ], 'post_status' => $stored_status, 'post_title' => "Matrix {$owner} {$status}", - 'post_date' => 'future' === $stored_status ? '2035-01-01 00:00:00' : '', + 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', ) ); if ( $stored_status !== $status ) { @@ -230,119 +178,6 @@ public static function wpSetUpBeforeClass( $factory ): void { ); } - /** - * Unregisters the content abilities and registers the content category. - * - * @since 7.3.0 - */ - public function set_up(): void { - parent::set_up(); - - foreach ( self::CONTENT_ABILITIES as $ability_name ) { - if ( wp_has_ability( $ability_name ) ) { - wp_unregister_ability( $ability_name ); - } - } - - if ( ! wp_has_ability_category( 'content' ) ) { - global $wp_current_filter; - $wp_current_filter[] = 'wp_abilities_api_categories_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. - wp_register_ability_category( - 'content', - array( - 'label' => 'Content', - 'description' => 'Content.', - ) - ); - } - } - - /** - * Unregisters the content abilities and the category, and resets the post types. - * - * @since 7.3.0 - */ - public function tear_down(): void { - foreach ( self::CONTENT_ABILITIES as $ability_name ) { - if ( wp_has_ability( $ability_name ) ) { - wp_unregister_ability( $ability_name ); - } - } - - if ( wp_has_ability_category( 'content' ) ) { - wp_unregister_ability_category( 'content' ); - } - - /* - * A test may turn off `show_in_abilities` on `post` and `page`, and the next class's - * wpSetUpBeforeClass() runs before the next set_up() resets the post types. - */ - $this->reset_post_types(); - - parent::tear_down(); - } - - /** - * Registers the content abilities inside a faked init action. - * - * @since 7.3.0 - */ - private function register_abilities(): void { - global $wp_current_filter; - $wp_current_filter[] = 'wp_abilities_api_init'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Faking the action context to register within it. - ( new WP_Abilities_Content() )->register(); - } - - /** - * Registers the content abilities and returns the callbacks of the update ability. - * - * The callbacks call private methods, so tests that skip input validation or the - * permission check capture them from the registration arguments. - * - * @since 7.3.0 - * - * @return array The permission and execute callbacks, keyed by argument name. - */ - private function get_ability_callbacks(): array { - $callbacks = array(); - - add_filter( - 'wp_register_ability_args', - static function ( array $args, string $name ) use ( &$callbacks ): array { - if ( self::ABILITY === $name ) { - $callbacks = array( - 'permission_callback' => $args['permission_callback'], - 'execute_callback' => $args['execute_callback'], - ); - } - - return $args; - }, - 10, - 2 - ); - - $this->register_abilities(); - - $this->assertArrayHasKey( 'execute_callback', $callbacks, 'The core/content-update ability should be registered with callbacks.' ); - - return $callbacks; - } - - /** - * Runs the registered update ability with the given input. - * - * @since 7.3.0 - * - * @param mixed $input The ability input. - * @return mixed The ability result. - */ - private function run_ability( $input ) { - $this->assertTrue( wp_has_ability( self::ABILITY ), 'The core/content-update ability should be registered.' ); - - return wp_get_ability( self::ABILITY )->execute( $input ); - } - /** * Runs the update ability on a post with the given data. * @@ -368,84 +203,6 @@ private function update_post( int $post_id, array $data, ?array $fields = null ) return $this->run_ability( $input ); } - /** - * Logs in as the shared user with the given role or fixture name. - * - * @since 7.3.0 - * - * @param string $role The role or fixture name. - */ - private function login_as( string $role ): void { - wp_set_current_user( self::$user_ids[ $role ] ); - } - - /** - * Takes a snapshot of the stored content: the number of posts per type and status, - * revisions included, the number of meta rows, and the row and meta of one post. - * - * @since 7.3.0 - * - * @param int $post_id The post ID. - * @return array The snapshot. - */ - private function snapshot( int $post_id ): array { - global $wpdb; - - return array( - 'posts' => $wpdb->get_results( "SELECT post_type, post_status, COUNT(*) AS total FROM {$wpdb->posts} GROUP BY post_type, post_status ORDER BY post_type, post_status", ARRAY_A ), - 'meta' => (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->postmeta}" ), - 'post' => $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id ), ARRAY_A ), - 'post_meta' => $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d ORDER BY meta_id", $post_id ), ARRAY_A ), - ); - } - - /** - * Asserts that a result is an error with the given code and status. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $code The expected error code. - * @param int $status The expected error status. - * @param string $context What the call was, for the assertion messages. - */ - private function assert_error( $result, string $code, int $status, string $context ): void { - $this->assertWPError( $result, "{$context}: the call should fail." ); - $this->assertSame( $code, $result->get_error_code(), "{$context}: the error code should be {$code}." ); - - $data = $result->get_error_data(); - $this->assertIsArray( $data, "{$context}: the error should carry data." ); - $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); - } - - /** - * Asserts that a result is the invalid attribute error for the given attribute. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $attribute The attribute name. - * @param string $context What the call was, for the assertion messages. - */ - private function assert_invalid_attribute( $result, string $attribute, string $context ): void { - $this->assert_error( $result, 'content_invalid_attribute', 400, $context ); - $this->assertSame( "Invalid attribute: {$attribute}.", $result->get_error_message(), "{$context}: the message should name the attribute." ); - $this->assertSame( array( "data[{$attribute}]" ), array_keys( $result->get_error_data()['params'] ?? array() ), "{$context}: the params should hold only the input path of the attribute." ); - } - - /** - * Asserts that a result is the not-found error. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $context What the call was, for the assertion messages. - */ - private function assert_not_found( $result, string $context ): void { - $this->assert_error( $result, 'content_not_found', 404, $context ); - $this->assertSame( 'The requested content was not found.', $result->get_error_message(), "{$context}: the message should be the not-found message." ); - } - /** * Asserts that a result is the post item of a successful update and returns the stored post. * @@ -464,25 +221,6 @@ private function assert_updated( $result, int $post_id ): WP_Post { return get_post( $post_id ); } - /** - * Removes the `description` keywords of a schema at every depth. - * - * @since 7.3.0 - * - * @param array $schema The schema. - * @return array The schema without descriptions. - */ - private function strip_descriptions( array $schema ): array { - unset( $schema['description'] ); - foreach ( $schema as $key => $value ) { - if ( is_array( $value ) ) { - $schema[ $key ] = $this->strip_descriptions( $value ); - } - } - - return $schema; - } - /** * Creates a draft with a floating date, stored in the site's timezone with no GMT date. * @@ -618,8 +356,6 @@ public function test_input_schema(): void { /** * The output is one post item, the same object as an item of the query. * - * Covers S11. - * * @ticket 66706 * @since 7.3.0 */ @@ -636,12 +372,12 @@ public function test_output_schema_is_a_query_item(): void { } /** - * Example 1: a new title and excerpt. + * A new title and excerpt are stored, and the post is returned in the chosen fields. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_updates_the_title_and_excerpt(): void { + public function test_updates_the_title_and_excerpt(): void { $post_id = self::factory()->post->create( array( 'post_author' => self::$user_ids['administrator'], @@ -664,8 +400,8 @@ public function test_example_updates_the_title_and_excerpt(): void { $this->assert_updated( $result, $post_id ); - $this->assertSame( array( 'id', 'modified', 'title', 'excerpt' ), array_keys( $result ), 'The ID and the chosen fields should be returned.' ); - $this->assertSame( array( 'site', 'gmt' ), array_keys( $result['modified'] ), 'The modified date should be given in site time and GMT.' ); + $this->assertSameSets( array( 'id', 'modified', 'title', 'excerpt' ), array_keys( $result ), 'The ID and the chosen fields should be returned.' ); + $this->assertSameSets( array( 'site', 'gmt' ), array_keys( $result['modified'] ), 'The modified date should be given in site time and GMT.' ); $this->assertNotFalse( rest_parse_date( $result['modified']['gmt'] ), 'The modified GMT date should be a date-time.' ); $this->assertSame( array( @@ -687,12 +423,12 @@ public function test_example_updates_the_title_and_excerpt(): void { } /** - * Example 2: publishing the draft. + * A draft is published by setting its status. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_publishes_the_draft(): void { + public function test_publishes_the_draft(): void { $this->set_permalink_structure( '/%postname%/' ); $post_id = self::factory()->post->create( @@ -721,14 +457,12 @@ public function test_example_publishes_the_draft(): void { } /** - * Example 3: a post the user cannot edit. - * - * Covers S4, S5 and S13. + * A post the user cannot edit is the edit error. * * @ticket 66706 * @since 7.3.0 */ - public function test_example_rejects_a_post_the_user_cannot_edit(): void { + public function test_rejects_a_post_the_user_cannot_edit(): void { $post_id = self::$post_ids['admin_post']; $this->login_as( 'contributor' ); @@ -749,7 +483,7 @@ public function test_example_rejects_a_post_the_user_cannot_edit(): void { 'message' => $result->get_error_message(), 'data' => $result->get_error_data(), ), - 'The error should be the edit error of the example.' + 'The error should be the edit error.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -757,8 +491,6 @@ public function test_example_rejects_a_post_the_user_cannot_edit(): void { /** * A logged-out user is refused before the ability runs. * - * Covers S4 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -814,9 +546,8 @@ public static function data_inputs_refused_by_the_permission_callback(): array { /** * The permission callback returns false for missing or malformed input. * - * @dataProvider data_inputs_refused_by_the_permission_callback - * * @ticket 66706 + * @dataProvider data_inputs_refused_by_the_permission_callback * @since 7.3.0 * * @param mixed $input The input. @@ -876,11 +607,8 @@ public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): arra /** * An ID that is not a whole number of at least 1 fails input validation. * - * Covers S2 and S5. - * - * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 - * * @ticket 66706 + * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 * @since 7.3.0 * * @param mixed $id The ID. @@ -905,120 +633,12 @@ public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ) $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); } - /** - * Returns ID values that a direct call does not read as a whole number of at least 1. - * - * @return array The IDs. - */ - public static function data_ids_that_are_not_whole_numbers_in_a_direct_call(): array { - return array_merge( - self::data_ids_that_are_not_whole_numbers_of_at_least_1(), - array( - 'hexadecimal string' => array( '0x1' ), - 'negative zero' => array( -0.0 ), - 'number above 2 ** 53' => array( 9007199254740993 ), - 'string above 2 ** 53' => array( '9007199254740993' ), - 'exponent above 2 ** 53' => array( '1e16' ), - ) - ); - } - - /** - * Called directly, an ID that is not a whole number of at least 1 is the not-found error and - * is never cast, even when it starts with the ID of an existing post. - * - * Covers S2, S5 and S13 (C3). - * - * @dataProvider data_ids_that_are_not_whole_numbers_in_a_direct_call - * - * @ticket 66706 - * @since 7.3.0 - * - * @param mixed $id The ID. - */ - public function test_direct_call_with_an_id_that_is_not_a_whole_number_is_not_found( $id ): void { - $post_id = self::$post_ids['admin_post']; - if ( '123abc' === $id ) { - $id = "{$post_id}abc"; - } elseif ( 1.5 === $id ) { - $id = $post_id + 0.5; - } elseif ( '0x1' === $id ) { - // The hexadecimal form of the ID of an existing post. - $id = '0x' . dechex( $post_id ); - } - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $id, - ), - 'data' => array( 'title' => 'Invalid ID' ), - ) - ); - - $this->assert_not_found( $result, 'A direct call with an ID that is not a whole number' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Called directly, an ID made of digits is read as that number. - * - * Covers S2. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_reads_an_id_made_of_digits(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => (string) $post_id, - ), - 'data' => array( 'title' => 'Digits' ), - ) - ); - - $this->assertSame( 'Digits', $this->assert_updated( $result, $post_id )->post_title, 'The post should be updated.' ); - } - - /** - * Returns posts that cannot be reached as a `post`. - * - * @return array The posts, as fixture names. - */ - public static function data_posts_that_are_not_posts(): array { - return array( - 'revision' => array( 'revision' ), - 'attachment' => array( 'attachment' ), - 'page' => array( 'admin_page' ), - 'missing' => array( 'missing' ), - 'reusable block' => array( 'reusable_block' ), - 'template' => array( 'template' ), - 'menu item' => array( 'menu_item' ), - 'autosave' => array( 'autosave' ), - ); - } - /** * A revision, an autosave, an attachment, a post of another exposed or non-exposed type and a * missing post are not found as a `post`, even by an administrator. * - * Covers S3, S5 and S13. - * - * @dataProvider data_posts_that_are_not_posts - * * @ticket 66706 + * @dataProvider data_posts_that_are_not_posts * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -1047,8 +667,6 @@ public function test_post_of_another_type_is_not_found( string $post ): void { /** * Called directly, a post of a type that is not exposed is not found, whatever its ID. * - * Covers S3 and S13. - * * @ticket 66706 * @since 7.3.0 */ @@ -1077,8 +695,6 @@ public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): /** * A post type whose `show_in_abilities` is turned off after registration cannot be updated. * - * Covers S3 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -1110,25 +726,18 @@ public function test_type_no_longer_exposed_cannot_be_updated(): void { /** * Returns direct-call inputs with a malformed `where`. * - * @return array The inputs. + * @return array}> The inputs. */ public static function data_direct_inputs_with_a_malformed_where(): array { return array( - 'no input' => array( null ), - 'no where' => array( array( 'data' => array( 'title' => 'x' ) ) ), - 'where that is a string' => array( - array( - 'where' => 'post', - 'data' => array( 'title' => 'x' ), - ), - ), - 'no type' => array( + 'no where' => array( array( 'data' => array( 'title' => 'x' ) ) ), + 'no type' => array( array( 'where' => array( 'id' => 1 ), 'data' => array( 'title' => 'x' ), ), ), - 'no ID' => array( + 'no ID' => array( array( 'where' => array( 'type' => 'post' ), 'data' => array( 'title' => 'x' ), @@ -1140,16 +749,13 @@ public static function data_direct_inputs_with_a_malformed_where(): array { /** * Called directly, a malformed `where` is the not-found error. * - * Covers S1 and S13. - * - * @dataProvider data_direct_inputs_with_a_malformed_where - * * @ticket 66706 + * @dataProvider data_direct_inputs_with_a_malformed_where * @since 7.3.0 * - * @param mixed $input The input. + * @param array $input The input. */ - public function test_direct_call_with_a_malformed_where_is_not_found( $input ): void { + public function test_direct_call_with_a_malformed_where_is_not_found( array $input ): void { $this->login_as( 'administrator' ); $execute = $this->get_ability_callbacks()['execute_callback']; @@ -1162,8 +768,6 @@ public function test_direct_call_with_a_malformed_where_is_not_found( $input ): /** * The lookup comes before everything else: a missing post with invalid data is not found. * - * Covers O1. - * * @ticket 66706 * @since 7.3.0 */ @@ -1274,11 +878,8 @@ public static function data_update_matrix(): array { * Each role may update the posts it may edit, in any status, and gets the 403 error for the * others, whatever their status. * - * Covers S4, S5 and S13. - * - * @dataProvider data_update_matrix - * * @ticket 66706 + * @dataProvider data_update_matrix * @since 7.3.0 * * @param string $role The role to update as. @@ -1332,11 +933,8 @@ public static function data_invalid_data(): array { * Called directly by a user who may not edit the post, the call fails on the capability before * anything about the attributes is checked. * - * Covers S4, S5 and O2. - * - * @dataProvider data_invalid_data - * * @ticket 66706 + * @dataProvider data_invalid_data * @since 7.3.0 * * @param array $data The data. @@ -1397,11 +995,8 @@ public static function data_keys_outside_the_allow_list(): array { /** * A key outside the allow-list fails input validation. * - * Covers S1, S5 and S6. - * - * @dataProvider data_keys_outside_the_allow_list - * * @ticket 66706 + * @dataProvider data_keys_outside_the_allow_list * @since 7.3.0 * * @param string $location Where the key is sent. @@ -1432,107 +1027,11 @@ public function test_key_outside_the_allow_list_fails_input_validation( string $ $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - /** - * Called directly, keys outside the allow-list are ignored and never written. - * - * Covers S6. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_ignores_keys_outside_the_allow_list(): void { - $post_id = self::$post_ids['administrator:draft']; - $other_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $other_before = get_post( $other_id )->to_array(); - $stored = get_post( $post_id ); - $category_id = self::factory()->category->create(); - $categories = wp_get_post_categories( $post_id ); - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - 'ID' => $other_id, - ), - 'data' => array( - 'title' => 'Allow-list', - 'type' => 'page', - 'ID' => $other_id, - 'post_type' => 'page', - 'post_password' => 'secret', - 'meta_input' => array( 'cw_key' => 'value' ), - 'tax_input' => array( 'category' => array( 1 ) ), - 'comment_status' => 'closed', - 'post_author' => self::$user_ids['editor'], - 'import_id' => 7654321, - 'guid' => 'https://example.org/forged', - 'page_template' => 'cw-template.php', - 'modified' => '2001-01-01T00:00:00', - 'post_modified' => '2001-01-01 00:00:00', - 'post_modified_gmt' => '2001-01-01 00:00:00', - 'sticky' => true, - 'tags_input' => array( 'cw-tag' ), - 'post_category' => array( $category_id ), - 'menu_order' => 5, - 'post_mime_type' => 'image/jpeg', - ), - 'unknown' => 'ignored', - ) - ); - - $post = $this->assert_updated( $result, $post_id ); - - $this->assertSame( 'Allow-list', $post->post_title, 'The title should be stored.' ); - $this->assertSame( 'post', $post->post_type, 'The post type should not change.' ); - $this->assertSame( '', $post->post_password, 'No password should be stored.' ); - $this->assertSame( array(), get_post_meta( $post_id, 'cw_key' ), 'No meta should be stored.' ); - $this->assertSame( $stored->comment_status, $post->comment_status, 'The comment status should not change.' ); - $this->assertSame( $stored->post_author, $post->post_author, 'The author should not change.' ); - $this->assertSame( $stored->guid, $post->guid, 'The GUID should not change.' ); - $this->assertSame( '', get_post_meta( $post_id, '_wp_page_template', true ), 'No page template should be stored.' ); - $this->assert_is_now( $post->post_modified_gmt, 'The stored modified date' ); - $this->assertFalse( is_sticky( $post_id ), 'The post should not be sticky.' ); - $this->assertSame( array(), wp_get_post_tags( $post_id, array( 'fields' => 'ids' ) ), 'No tag should be stored.' ); - $this->assertSame( array( (int) get_option( 'default_category' ) ), $categories, 'Precondition: the post should have only the default category.' ); - $this->assertSame( $categories, wp_get_post_categories( $post_id ), 'Only the default category should be kept.' ); - $this->assertSame( 0, $post->menu_order, 'The menu order should stay 0.' ); - $this->assertSame( '', $post->post_mime_type, 'No MIME type should be stored.' ); - $this->assertSame( $other_before, get_post( $other_id )->to_array(), 'The post named by ID should not change.' ); - } - - /** - * Returns statuses that can never be set. - * - * @return array The statuses. - */ - public static function data_statuses_that_can_never_be_set(): array { - return array( - 'trash' => array( 'trash' ), - 'auto-draft' => array( 'auto-draft' ), - 'inherit' => array( 'inherit' ), - 'unregistered' => array( 'cw_unregistered' ), - 'request-pending' => array( 'request-pending' ), - 'request-confirmed' => array( 'request-confirmed' ), - 'upper case' => array( 'PUBLISH' ), - 'trailing space' => array( 'publish ' ), - 'empty string' => array( '' ), - 'comma-joined list' => array( 'draft,publish' ), - ); - } - /** * An internal or unregistered status fails input validation. * - * Covers S5 and S7. - * - * @dataProvider data_statuses_that_can_never_be_set - * * @ticket 66706 + * @dataProvider data_statuses_that_can_never_be_set * @since 7.3.0 * * @param string $status The status. @@ -1555,11 +1054,8 @@ public function test_status_that_can_never_be_set_fails_input_validation( string * Called directly, an internal or unregistered status is an invalid status for a post that * is not stored in it. * - * Covers S1, S5 and S7. - * - * @dataProvider data_statuses_that_can_never_be_set - * * @ticket 66706 + * @dataProvider data_statuses_that_can_never_be_set * @since 7.3.0 * * @param string $status The status. @@ -1608,11 +1104,8 @@ public static function data_status_changes_without_publish_posts(): array { /** * A status that makes a post visible needs `publish_posts`. * - * Covers S5 and S7. - * - * @dataProvider data_status_changes_without_publish_posts - * * @ticket 66706 + * @dataProvider data_status_changes_without_publish_posts * @since 7.3.0 * * @param string $role The role or fixture name of the user, who owns the draft. @@ -1629,7 +1122,7 @@ public function test_status_that_makes_a_post_visible_needs_publish_posts( strin $data = array( 'status' => $status ); if ( 'future' === $status ) { - $data['date'] = array( 'gmt' => '2035-06-01T12:00:00' ); + $data['date'] = array( 'gmt' => '2099-06-01T12:00:00' ); } $before = $this->snapshot( $post_id ); @@ -1684,7 +1177,7 @@ public function test_schedules_a_draft(): void { $post_id, array( 'status' => 'future', - 'date' => array( 'gmt' => '2035-06-01T12:00:00' ), + 'date' => array( 'gmt' => '2099-06-01T12:00:00' ), ), array( 'status', 'date' ) ); @@ -1692,7 +1185,7 @@ public function test_schedules_a_draft(): void { $post = $this->assert_updated( $result, $post_id ); $this->assertSame( 'future', $post->post_status, 'The post should be scheduled.' ); - $this->assertSame( '2035-06-01 12:00:00', $post->post_date_gmt, 'The post should be scheduled for the sent date.' ); + $this->assertSame( '2099-06-01 12:00:00', $post->post_date_gmt, 'The post should be scheduled for the sent date.' ); $this->assertSame( 'future', $result['status'], 'The scheduled status should be returned.' ); } @@ -1711,9 +1204,8 @@ public static function data_unpublished_statuses(): array { /** * A published post can be unpublished. * - * @dataProvider data_unpublished_statuses - * * @ticket 66706 + * @dataProvider data_unpublished_statuses * @since 7.3.0 * * @param string $status The status. @@ -1737,7 +1229,7 @@ public function test_unpublishes_a_post( string $status ): void { */ public static function data_statuses_reported_as_stored(): array { return array( - 'publish with a future date' => array( 'publish', '2035-06-01T12:00:00', 'future' ), + 'publish with a future date' => array( 'publish', '2099-06-01T12:00:00', 'future' ), 'future with a past date' => array( 'future', '2016-12-12T19:00:00', 'publish' ), ); } @@ -1745,9 +1237,8 @@ public static function data_statuses_reported_as_stored(): array { /** * The regular save rules decide the stored status, and the output reports it. * - * @dataProvider data_statuses_reported_as_stored - * * @ticket 66706 + * @dataProvider data_statuses_reported_as_stored * @since 7.3.0 * * @param string $status The requested status. @@ -1809,9 +1300,8 @@ public static function data_statuses_that_move_a_post_out_of_the_trash(): array /** * Sending a status moves a post out of the Trash through a plain update. * - * @dataProvider data_statuses_that_move_a_post_out_of_the_trash - * * @ticket 66706 + * @dataProvider data_statuses_that_move_a_post_out_of_the_trash * @since 7.3.0 * * @param string $role The role, which owns the post. @@ -1832,8 +1322,6 @@ public function test_status_moves_a_post_out_of_the_trash( string $role, string /** * Moving a post out of the Trash passes the status gate like any status change. * - * Covers S5 and S7. - * * @ticket 66706 * @since 7.3.0 */ @@ -1850,27 +1338,12 @@ public function test_publishing_a_post_from_the_trash_needs_publish_posts(): voi $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - /** - * Returns authors that a user without `edit_others_posts` may not name. - * - * @return array The authors, as fixture names. - */ - public static function data_authors_named_without_edit_others_posts(): array { - return array( - 'existing user' => array( 'editor' ), - 'missing user' => array( 'missing' ), - ); - } - /** * A user without `edit_others_posts` gets the same error for an existing and a missing * author, so the error never tells whether a user exists. * - * Covers S5, S8 and O3. - * - * @dataProvider data_authors_named_without_edit_others_posts - * * @ticket 66706 + * @dataProvider data_authors_named_without_edit_others_posts * @since 7.3.0 * * @param string $author The author, as a fixture name. @@ -1958,8 +1431,6 @@ public function test_author_equal_to_the_stored_author_passes_with_no_check(): v /** * A user with `edit_others_posts` who names a missing user gets the invalid author error. * - * Covers S5 and S8. - * * @ticket 66706 * @since 7.3.0 */ @@ -1979,8 +1450,6 @@ public function test_naming_a_missing_author_with_edit_others_posts_is_an_invali /** * On multisite, a user who is not a member of the site cannot be the author. * - * Covers S5 and S8. - * * @group ms-required * * @ticket 66706 @@ -2023,11 +1492,8 @@ public static function data_parents_that_cannot_be_used(): array { * A missing parent, a parent of another type, a parent the user may not read and a parent * that would make a loop give one identical error. * - * Covers S5 and S9. - * - * @dataProvider data_parents_that_cannot_be_used - * * @ticket 66706 + * @dataProvider data_parents_that_cannot_be_used * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -2090,11 +1556,10 @@ public static function data_parents_that_can_be_used(): array { } /** - * A readable page of the same type can be the parent, and `0` makes the page a top-level page. - * - * @dataProvider data_parents_that_can_be_used + * A readable page of the same type can be the parent, and `0` makes the page a top-level page. * * @ticket 66706 + * @dataProvider data_parents_that_can_be_used * @since 7.3.0 * * @param string|null $parent_key The parent, as a fixture name, or null for 0. @@ -2128,11 +1593,8 @@ public static function data_unsupported_attributes(): array { /** * An attribute the post type does not support is rejected, even when it would change nothing. * - * Covers S1, S5 and O4: the first failure is returned and nothing is written. - * - * @dataProvider data_unsupported_attributes - * * @ticket 66706 + * @dataProvider data_unsupported_attributes * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -2170,36 +1632,26 @@ public static function data_values_of_the_wrong_type(): array { } /** - * Called directly, a value of the wrong type is an invalid attribute. - * - * Covers S1 and S5. - * - * @dataProvider data_values_of_the_wrong_type + * A value of the wrong type fails input validation. * * @ticket 66706 + * @dataProvider data_values_of_the_wrong_type * @since 7.3.0 * * @param string $attribute The attribute. * @param mixed $value The value. */ - public function test_direct_call_rejects_a_value_of_the_wrong_type( string $attribute, $value ): void { + public function test_value_of_the_wrong_type_fails_input_validation( string $attribute, $value ): void { $post_id = self::$post_ids['administrator:draft']; $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; + $this->register_abilities(); $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( $attribute => $value ), - ) - ); + $result = $this->update_post( $post_id, array( $attribute => $value ) ); - $this->assert_invalid_attribute( $result, $attribute, 'A value of the wrong type' ); + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the value.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -2240,8 +1692,6 @@ public function test_attributes_not_sent_keep_their_stored_values(): void { /** * A slug that sanitizes to nothing is an invalid slug. * - * Covers S1 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -2258,6 +1708,73 @@ public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } + /** + * Returns updates that send a slug for a post that is, or becomes, pending. + * + * @return array}> The posts, as fixture names, and the data. + */ + public static function data_slugs_of_pending_posts(): array { + return array( + 'draft moved to pending' => array( + 'contributor:draft', + array( + 'status' => 'pending', + 'slug' => 'pending-slug', + ), + ), + 'pending post' => array( 'contributor:pending', array( 'slug' => 'pending-slug' ) ), + ); + } + + /** + * A user who may not publish the post cannot set the slug of a pending post. + * + * Intentionally strict: the save would drop the slug, so it is an error instead of being + * dropped without notice. + * + * @ticket 66706 + * @dataProvider data_slugs_of_pending_posts + * @since 7.3.0 + * + * @param string $post The post, as a fixture name. + * @param array $data The `data` input. + */ + public function test_slug_of_a_pending_post_is_rejected_without_publish_posts( string $post, array $data ): void { + $post_id = self::$post_ids[ $post ]; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, $data ); + + $this->assert_invalid_attribute( $result, 'slug', 'A slug on a pending post without publish_posts' ); + $this->assertSame( 'The slug of a pending post can only be set by a user who can publish the post.', $result->get_error_message(), 'The message should say who may set the slug.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for a slug that cannot be stored.' ); + } + + /** + * A user who may publish the post sets the slug of a pending post. + * + * @ticket 66706 + * @dataProvider data_slugs_of_pending_posts + * @since 7.3.0 + * + * @param string $post The post, as a fixture name. + * @param array $data The `data` input. + */ + public function test_slug_of_a_pending_post_is_stored_with_publish_posts( string $post, array $data ): void { + $post_id = self::$post_ids[ $post ]; + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $post = $this->assert_updated( $this->update_post( $post_id, $data ), $post_id ); + + $this->assertSame( 'pending', $post->post_status, 'The post should be pending.' ); + $this->assertSame( 'pending-slug', $post->post_name, 'The slug of the pending post should be stored.' ); + } + /** * Returns posts that take the slug of another post. * @@ -2275,9 +1792,8 @@ public static function data_posts_taking_a_used_slug(): array { /** * A post never takes the slug of another post, also as a draft or a pending post. * - * @dataProvider data_posts_taking_a_used_slug - * * @ticket 66706 + * @dataProvider data_posts_taking_a_used_slug * @since 7.3.0 * * @param string $post The post, as a fixture name. @@ -2314,9 +1830,8 @@ public static function data_draft_page_slugs_by_parent(): array { /** * A draft page is made unique among the pages that share the sent parent. * - * @dataProvider data_draft_page_slugs_by_parent - * * @ticket 66706 + * @dataProvider data_draft_page_slugs_by_parent * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. @@ -2354,14 +1869,14 @@ public function test_draft_page_slug_is_unique_among_its_siblings( string $paren } /** - * D1: an update without a date keeps a floating draft floating. + * An update without a date keeps a floating draft floating. * * @ticket 66706 * @since 7.3.0 */ public function test_update_without_a_date_keeps_a_draft_floating(): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2385,12 +1900,11 @@ public static function data_statuses_that_stamp_the_current_time(): array { } /** - * D1: publishing a floating draft without a date stamps the current time, also when it is + * Publishing a floating draft without a date stamps the current time, also when it is * sent as scheduled, and with a past local date. * - * @dataProvider data_statuses_that_stamp_the_current_time - * * @ticket 66706 + * @dataProvider data_statuses_that_stamp_the_current_time * @since 7.3.0 * * @param string $status The status. @@ -2409,14 +1923,14 @@ public function test_publishing_a_floating_draft_without_a_date_stamps_the_curre } /** - * D1: moving a floating draft to pending without a date keeps it floating. + * Moving a floating draft to pending without a date keeps it floating. * * @ticket 66706 * @since 7.3.0 */ public function test_pending_without_a_date_keeps_a_draft_floating(): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2447,11 +1961,10 @@ public static function data_new_dates(): array { } /** - * D2: a date that differs from the stored one is stored as sent, local and GMT. - * - * @dataProvider data_new_dates + * A date that differs from the stored one is stored as sent, local and GMT. * * @ticket 66706 + * @dataProvider data_new_dates * @since 7.3.0 * * @param array $date The sent date. @@ -2489,18 +2002,17 @@ public static function data_new_dates_for_a_floating_draft(): array { } /** - * D2: a new date pins a floating draft. - * - * @dataProvider data_new_dates_for_a_floating_draft + * A new date pins a floating draft. * * @ticket 66706 + * @dataProvider data_new_dates_for_a_floating_draft * @since 7.3.0 * * @param array $date The sent date. */ public function test_new_date_pins_a_floating_draft( array $date ): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2513,30 +2025,29 @@ public function test_new_date_pins_a_floating_draft( array $date ): void { } /** - * Returns dates equal to the date a floating draft shows, 2031-06-15 10:00 in America/New_York. + * Returns dates equal to the date a floating draft shows, 2099-06-15 10:00 in America/New_York. * * @return array}> The dates. */ public static function data_dates_equal_to_the_floating_date(): array { return array( - 'site' => array( array( 'site' => '2031-06-15T10:00:00' ) ), - 'gmt' => array( array( 'gmt' => '2031-06-15T14:00:00' ) ), + 'site' => array( array( 'site' => '2099-06-15T10:00:00' ) ), + 'gmt' => array( array( 'gmt' => '2099-06-15T14:00:00' ) ), ); } /** - * D3: sending back the date a floating draft shows counts as not sent and never pins it. - * - * @dataProvider data_dates_equal_to_the_floating_date + * Sending back the date a floating draft shows counts as not sent and never pins it. * * @ticket 66706 + * @dataProvider data_dates_equal_to_the_floating_date * @since 7.3.0 * * @param array $date The sent date. */ public function test_date_equal_to_the_floating_date_does_not_pin_the_draft( array $date ): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2570,12 +2081,11 @@ public static function data_past_dates_equal_to_the_floating_date(): array { } /** - * D3: publishing a floating draft with the past date it shows publishes it at the current + * Publishing a floating draft with the past date it shows publishes it at the current * time, as if no date was sent. * - * @dataProvider data_past_dates_equal_to_the_floating_date - * * @ticket 66706 + * @dataProvider data_past_dates_equal_to_the_floating_date * @since 7.3.0 * * @param array $date The sent date. @@ -2603,11 +2113,10 @@ public function test_publishing_with_the_past_floating_date_stamps_the_current_t } /** - * D3: a date equal to the stored date of a post that does not float changes nothing. - * - * @dataProvider data_dates_equal_to_the_floating_date + * A date equal to the stored date of a post that does not float changes nothing. * * @ticket 66706 + * @dataProvider data_dates_equal_to_the_floating_date * @since 7.3.0 * * @param array $date The sent date. @@ -2618,8 +2127,8 @@ public function test_date_equal_to_the_stored_date_changes_nothing( array $date array( 'post_author' => self::$user_ids['editor'], 'post_status' => 'future', - 'post_date' => '2031-06-15 10:00:00', - 'post_date_gmt' => '2031-06-15 14:00:00', + 'post_date' => '2099-06-15 10:00:00', + 'post_date_gmt' => '2099-06-15 14:00:00', ) ); @@ -2629,8 +2138,8 @@ public function test_date_equal_to_the_stored_date_changes_nothing( array $date $post = $this->assert_updated( $this->update_post( $post_id, array( 'date' => $date ) ), $post_id ); $this->assertSame( 'future', $post->post_status, 'The post should stay scheduled.' ); - $this->assertSame( '2031-06-15 10:00:00', $post->post_date, 'The local date should not change.' ); - $this->assertSame( '2031-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should not change.' ); + $this->assertSame( '2099-06-15 10:00:00', $post->post_date, 'The local date should not change.' ); + $this->assertSame( '2099-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should not change.' ); } /** @@ -2640,19 +2149,18 @@ public function test_date_equal_to_the_stored_date_changes_nothing( array $date */ public static function data_schedules_a_floating_draft_for_the_date_it_shows(): array { return array( - 'future, site' => array( 'future', array( 'site' => '2031-06-15T10:00:00' ) ), - 'future, gmt' => array( 'future', array( 'gmt' => '2031-06-15T14:00:00' ) ), - 'publish, site' => array( 'publish', array( 'site' => '2031-06-15T10:00:00' ) ), - 'publish, gmt' => array( 'publish', array( 'gmt' => '2031-06-15T14:00:00' ) ), + 'future, site' => array( 'future', array( 'site' => '2099-06-15T10:00:00' ) ), + 'future, gmt' => array( 'future', array( 'gmt' => '2099-06-15T14:00:00' ) ), + 'publish, site' => array( 'publish', array( 'site' => '2099-06-15T10:00:00' ) ), + 'publish, gmt' => array( 'publish', array( 'gmt' => '2099-06-15T14:00:00' ) ), ); } /** - * D4: scheduling a floating draft for the future date it shows schedules it for that date. - * - * @dataProvider data_schedules_a_floating_draft_for_the_date_it_shows + * Scheduling a floating draft for the future date it shows schedules it for that date. * * @ticket 66706 + * @dataProvider data_schedules_a_floating_draft_for_the_date_it_shows * @since 7.3.0 * * @param string $status The status. @@ -2660,7 +2168,7 @@ public static function data_schedules_a_floating_draft_for_the_date_it_shows(): */ public function test_scheduling_a_floating_draft_for_the_date_it_shows( string $status, array $date ): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2031-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2676,16 +2184,14 @@ public function test_scheduling_a_floating_draft_for_the_date_it_shows( string $ $post = $this->assert_updated( $result, $post_id ); $this->assertSame( 'future', $post->post_status, 'The post should be scheduled.' ); - $this->assertSame( '2031-06-15 10:00:00', $post->post_date, 'The local date should be the sent date.' ); - $this->assertSame( '2031-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should be the sent date.' ); + $this->assertSame( '2099-06-15 10:00:00', $post->post_date, 'The local date should be the sent date.' ); + $this->assertSame( '2099-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should be the sent date.' ); $this->assertSame( 'future', $result['status'], 'The scheduled status should be returned.' ); } /** * A date that is not a date-time fails input validation. * - * Covers S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -2706,8 +2212,6 @@ public function test_date_that_is_not_a_date_time_fails_input_validation(): void /** * The markup of an author is filtered on update. * - * Covers S10. - * * @ticket 66706 * @since 7.3.0 */ @@ -2737,8 +2241,6 @@ public function test_markup_of_an_author_is_filtered(): void { /** * The markup of an administrator with `unfiltered_html` is stored unchanged on update. * - * Covers S10. - * * @group ms-excluded * * @ticket 66706 @@ -2759,8 +2261,6 @@ public function test_markup_of_an_administrator_is_not_filtered(): void { /** * On multisite, the markup of a site administrator is filtered on update. * - * Covers S10. - * * @group ms-required * * @ticket 66706 @@ -2815,8 +2315,6 @@ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip( * * The user may publish posts but not edit published posts. * - * Covers S11. - * * @ticket 66706 * @since 7.3.0 */ @@ -2844,8 +2342,6 @@ public function test_raw_fields_are_left_out_when_the_writer_can_no_longer_edit_ /** * A password-protected post is returned with its protection flag. * - * Covers S11. - * * @ticket 66706 * @since 7.3.0 */ @@ -2879,8 +2375,6 @@ public function test_password_protected_post_is_returned_with_its_protection_fla /** * The current user is checked at each call, also on the same instance. * - * Covers S4. - * * @ticket 66706 * @since 7.3.0 */ @@ -2919,8 +2413,6 @@ public function test_each_call_checks_the_current_user(): void { /** * The author visibility is checked again at each call. * - * Covers S11. - * * @ticket 66706 * @since 7.3.0 */ @@ -2974,8 +2466,6 @@ public function test_each_call_checks_the_author_visibility_again(): void { /** * A save error keeps its own code and message, with status 400 when the post is empty. * - * Covers S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -3016,17 +2506,17 @@ public function test_database_error_is_the_save_error(): void { $before = $this->snapshot( $post_id ); - $wpdb->suppress_errors = true; + $suppress = $wpdb->suppress_errors(); add_filter( 'query', static function ( $query ) { - return 0 === strpos( $query, 'UPDATE' ) ? '],' : $query; + return str_starts_with( $query, 'UPDATE' ) ? '],' : $query; } ); $result = $this->update_post( $post_id, array( 'title' => 'Database error' ) ); - $wpdb->suppress_errors = false; + $wpdb->suppress_errors( $suppress ); $this->assert_error( $result, 'db_update_error', 500, 'A database error' ); $this->assertSame( 'Could not update post in the database.', $result->get_error_message(), 'The message should be the message of the save error.' ); @@ -3086,10 +2576,6 @@ static function ( $id ) use ( &$fires ): void { $this->login_as( 'administrator' ); $this->register_abilities(); - $this->assert_updated( $this->update_post( $post_id, array( 'title' => 'Hooks' ) ), $post_id ); - $ability_fires = $fires; - - $fires = array(); wp_update_post( array( 'ID' => $reference_id, @@ -3097,143 +2583,12 @@ static function ( $id ) use ( &$fires ): void { ) ); - $this->assertSame( 1, count( array_keys( $ability_fires, $post_id, true ) ), 'wp_after_insert_post should fire once for the updated post.' ); - $this->assertSame( 1, count( array_keys( $fires, $reference_id, true ) ), 'Precondition: a regular save should fire wp_after_insert_post once for the post.' ); - } - - /** - * Returns an integer in one of the forms that are read as that integer. - * - * @since 7.3.0 - * - * @param int $value The integer. - * @param string $form The form: `float`, `decimal`, `plus`, `leading space`, `trailing space`, - * `leading newline` or `exponent`. - * @return float|string The integer in that form. - */ - private static function whole_number_form( int $value, string $form ) { - $forms = array( - 'float' => (float) $value, - 'decimal' => "{$value}.0", - 'plus' => "+{$value}", - 'leading space' => " {$value}", - 'trailing space' => "{$value} ", - 'leading newline' => "\n{$value}", - 'exponent' => "{$value}e0", - ); - - return $forms[ $form ]; - } - - /** - * Returns the forms of a whole number that are read as the integer they equal. - * - * @return array The forms. - */ - public static function data_whole_number_forms(): array { - return array( - 'whole float' => array( 'float' ), - 'string with .0' => array( 'decimal' ), - 'string with plus' => array( 'plus' ), - 'string with leading space' => array( 'leading space' ), - 'string with trailing space' => array( 'trailing space' ), - 'string with leading line' => array( 'leading newline' ), - 'string with an exponent' => array( 'exponent' ), - ); - } - - /** - * Called directly, an ID in another whole-number form is read as the integer it equals. - * - * Covers S2 (C3). - * - * @dataProvider data_whole_number_forms - * - * @ticket 66706 - * @since 7.3.0 - * - * @param string $form The form of the ID. - */ - public function test_direct_call_reads_an_id_in_a_whole_number_form( string $form ): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => self::whole_number_form( $post_id, $form ), - ), - 'data' => array( 'title' => 'Whole number ID' ), - ) - ); - - $this->assertSame( 'Whole number ID', $this->assert_updated( $result, $post_id )->post_title, 'The post with that ID should be updated.' ); - } - - /** - * Called directly, an author in another whole-number form is read as the integer it equals. - * - * Covers C3. - * - * @dataProvider data_whole_number_forms - * - * @ticket 66706 - * @since 7.3.0 - * - * @param string $form The form of the author. - */ - public function test_direct_call_reads_an_author_in_a_whole_number_form( string $form ): void { - $post_id = self::$post_ids['editor:draft']; - - $this->login_as( 'editor' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'author' => self::whole_number_form( self::$user_ids['author'], $form ) ), - ) - ); - - $this->assertSame( self::$user_ids['author'], (int) $this->assert_updated( $result, $post_id )->post_author, 'The user with that ID should be the author.' ); - } - - /** - * Called directly, a parent in another whole-number form is read as the integer it equals. - * - * Covers C3. - * - * @dataProvider data_whole_number_forms - * - * @ticket 66706 - * @since 7.3.0 - * - * @param string $form The form of the parent. - */ - public function test_direct_call_reads_a_parent_in_a_whole_number_form( string $form ): void { - $post_id = self::$post_ids['writer_page_with_hidden_parent']; - $parent_id = self::$post_ids['admin_page']; - - $this->login_as( 'page_writer' ); - $execute = $this->get_ability_callbacks()['execute_callback']; + $this->assertCount( 1, array_keys( $fires, $reference_id, true ), 'Precondition: a regular save should fire wp_after_insert_post once for the post.' ); - $result = $execute( - array( - 'where' => array( - 'type' => 'page', - 'id' => $post_id, - ), - 'data' => array( 'parent' => self::whole_number_form( $parent_id, $form ) ), - ) - ); + $fires = array(); + $this->assert_updated( $this->update_post( $post_id, array( 'title' => 'Hooks' ) ), $post_id ); - $this->assertSame( $parent_id, $this->assert_updated( $result, $post_id )->post_parent, 'The page with that ID should be the parent.' ); + $this->assertCount( 1, array_keys( $fires, $post_id, true ), 'wp_after_insert_post should fire once for the updated post.' ); } /** @@ -3273,11 +2628,8 @@ public static function data_internal_stored_statuses(): array { * Called directly, an internal status is rejected also for a post that is already stored in * it, and the post is found whatever its stored status. * - * Covers S2, S5 and S7 (C1 and C12). - * - * @dataProvider data_internal_stored_statuses - * * @ticket 66706 + * @dataProvider data_internal_stored_statuses * @since 7.3.0 * * @param string $status The stored status, which is also sent. @@ -3317,67 +2669,13 @@ public function test_direct_call_rejects_trash_for_a_post_in_the_trash( string $ } /** - * `trash` sent for a post in the Trash fails input validation. - * - * Covers S5 (C1). + * Called directly, an empty date or a date that cannot be parsed is an invalid attribute. * * @ticket 66706 - * @since 7.3.0 - */ - public function test_trash_for_a_post_in_the_trash_fails_input_validation(): void { - $post_id = $this->create_trashed_post(); - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'status' => 'trash' ) ); - - $this->assertWPError( $result, 'Sending trash for a trashed post should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject trash for a trashed post.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'The trashed post should not change.' ); - } - - /** - * Returns date values without a usable value for a direct call. - * - * @return array}> The dates. - */ - public static function data_dates_without_a_usable_value(): array { - return array( - 'empty date' => array( array() ), - 'GMT that is a number' => array( - array( - 'site' => '2016-12-12T14:00:00', - 'gmt' => 123, - ), - ), - 'GMT that is a list' => array( - array( - 'site' => '2016-12-12T14:00:00', - 'gmt' => array( '2016-12-12T19:00:00' ), - ), - ), - 'GMT that is null' => array( - array( - 'site' => '2016-12-12T14:00:00', - 'gmt' => null, - ), - ), - ); - } - - /** - * Called directly, an empty date or a GMT value of the wrong type is an invalid attribute. - * - * Covers S5 (C2). - * * @dataProvider data_dates_without_a_usable_value - * - * @ticket 66706 * @since 7.3.0 * - * @param array $date The date. + * @param array $date The date. */ public function test_direct_call_rejects_a_date_without_a_usable_value( array $date ): void { $post_id = self::$post_ids['administrator:draft']; @@ -3408,7 +2706,6 @@ public function test_direct_call_rejects_a_date_without_a_usable_value( array $d public static function data_inputs_without_usable_data(): array { return array( 'data missing' => array( array() ), - 'data that is a string' => array( array( 'data' => 'x' ) ), 'empty data' => array( array( 'data' => array() ) ), 'data with only keys outside the list' => array( array( @@ -3426,11 +2723,8 @@ public static function data_inputs_without_usable_data(): array { * Called directly by a user who may edit the post, an update without a supported attribute is * an invalid attribute and writes nothing. * - * Covers S5 and O4 (C7). - * - * @dataProvider data_inputs_without_usable_data - * * @ticket 66706 + * @dataProvider data_inputs_without_usable_data * @since 7.3.0 * * @param array $input The input, without `where`. @@ -3456,8 +2750,6 @@ public function test_direct_call_without_usable_data_is_an_invalid_attribute( ar /** * An empty excerpt and an empty title are stored on a post that keeps its content. * - * Covers S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -3494,8 +2786,6 @@ public function test_stores_an_empty_excerpt_and_an_empty_title(): void { /** * An author denied `publish_posts` may still move their own draft to `pending`. * - * Covers S7. - * * @ticket 66706 * @since 7.3.0 */ @@ -3525,7 +2815,7 @@ private function create_post_of_the_restricted_author( string $status ): int { array( 'post_author' => self::$user_ids['restricted_author'], 'post_status' => $status, - 'post_date' => 'future' === $status ? '2035-01-01 00:00:00' : '2020-01-01 00:00:00', + 'post_date' => 'future' === $status ? '2099-01-01 00:00:00' : '2020-01-01 00:00:00', ) ); @@ -3559,9 +2849,8 @@ public static function data_date_changes_on_a_published_or_scheduled_post(): arr * The call sets no status. The save stores `future` for a date ahead and `publish` * for any other date, so the date alone can schedule or publish the post. * - * @dataProvider data_date_changes_on_a_published_or_scheduled_post - * * @ticket 66706 + * @dataProvider data_date_changes_on_a_published_or_scheduled_post * @since 7.3.0 * * @param string $stored_status The status the post has. @@ -3606,9 +2895,8 @@ public static function data_named_status_changes_on_a_published_or_scheduled_pos * A call that names the other status, `future` or `publish`, needs `publish_posts`, * also when the sent date alone would lead to that status. * - * @dataProvider data_named_status_changes_on_a_published_or_scheduled_post - * * @ticket 66706 + * @dataProvider data_named_status_changes_on_a_published_or_scheduled_post * @since 7.3.0 * * @param string $stored_status The status the post has. @@ -3669,8 +2957,6 @@ static function ( $caps, $cap, $cap_user_id, $args ) use ( $user_id, $post_id, $ * A user with no edit capability for the type who is granted `edit_post` on exactly one post * may update that post's title, and nothing more. * - * Covers S4, S5 and S7. - * * @ticket 66706 * @since 7.3.0 */ @@ -3755,8 +3041,6 @@ private function set_special_pages(): array { /** * An editor may not update the privacy policy page. * - * Covers S4 and S5. - * * @ticket 66706 * @since 7.3.0 */ @@ -3788,11 +3072,8 @@ public static function data_special_pages_an_editor_may_update(): array { /** * An editor may update the title of the front page and of the posts page. * - * Covers S4. - * - * @dataProvider data_special_pages_an_editor_may_update - * * @ticket 66706 + * @dataProvider data_special_pages_an_editor_may_update * @since 7.3.0 * * @param string $page The page, as a fixture name. @@ -3823,11 +3104,8 @@ public static function data_updates_of_a_page_with_a_missing_parent(): array { /** * A page whose stored parent no longer exists can be updated, and keeps that parent. * - * Covers S9. - * - * @dataProvider data_updates_of_a_page_with_a_missing_parent - * * @ticket 66706 + * @dataProvider data_updates_of_a_page_with_a_missing_parent * @since 7.3.0 * * @param bool $send_parent Whether the stored parent is sent. @@ -3895,8 +3173,6 @@ private function register_custom_statuses(): void { /** * A contributor may set a hidden custom status on their own draft without `publish_posts`. * - * Covers S7 (C4). - * * @ticket 66706 * @since 7.3.0 */ @@ -3932,11 +3208,8 @@ public static function data_custom_visible_statuses_by_stored_status(): array { * A contributor may not set a custom public or private status on their own post, and the * refusal uses the general message. * - * Covers S5 and S7 (C6). - * - * @dataProvider data_custom_visible_statuses_by_stored_status - * * @ticket 66706 + * @dataProvider data_custom_visible_statuses_by_stored_status * @since 7.3.0 * * @param string $status The status. @@ -3961,8 +3234,6 @@ public function test_contributor_cannot_set_a_custom_visible_status( string $sta * When the update takes away the caller's read access, the update is stored and the output * is the ID alone, whatever the fields ask for. * - * Covers S11 (C14). - * * @ticket 66706 * @since 7.3.0 */ @@ -4003,8 +3274,6 @@ public function test_output_is_the_id_alone_when_the_update_takes_away_read_acce * When the caller may still read the post after the update, the output holds the requested * fields. * - * Covers S11 (C14). - * * @ticket 66706 * @since 7.3.0 */ diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index 39e79517d241a..d90366fbaff9e 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -494,7 +494,7 @@ public function test_create_runs_with_post_and_a_json_body(): void { $data = $response->get_data(); - $this->assertSame( array( 'id', 'status', 'title' ), array_keys( $data ), 'The created post should be returned in the chosen fields.' ); + $this->assertSameSets( array( 'id', 'status', 'title' ), array_keys( $data ), 'The created post should be returned in the chosen fields.' ); $this->assertSame( 'draft', $data['status'], 'The created post should be a draft.' ); $this->assertSame( 'Over the run route', get_post( $data['id'] )->post_title, 'The created post should be stored.' ); } @@ -579,9 +579,8 @@ public static function data_permanent_query_string_values(): array { /** * `permanent` in the query string is read as a boolean. * - * @dataProvider data_permanent_query_string_values - * * @ticket 66706 + * @dataProvider data_permanent_query_string_values * @since 7.3.0 * * @param string $permanent The value in the query string. @@ -634,11 +633,8 @@ public static function data_permanent_query_string_values_that_are_not_booleans( * A `permanent` value in the query string that is not a boolean fails input validation, and * the post is unchanged. * - * Covers S12 (C5). - * - * @dataProvider data_permanent_query_string_values_that_are_not_booleans - * * @ticket 66706 + * @dataProvider data_permanent_query_string_values_that_are_not_booleans * @since 7.3.0 * * @param string $permanent The value in the query string. @@ -668,8 +664,6 @@ public function test_delete_with_a_permanent_value_that_is_not_a_boolean_returns /** * A create sent with POST but with the input in the query string fails input validation. * - * Covers C5. - * * @ticket 66706 * @since 7.3.0 */ @@ -696,8 +690,6 @@ public function test_create_with_the_input_in_the_query_string_returns_400(): vo /** * A delete sent with DELETE but with the input in a JSON body fails input validation. * - * Covers C5. - * * @ticket 66706 * @since 7.3.0 */ @@ -745,9 +737,8 @@ public static function data_wrong_methods_for_write_abilities(): array { /** * A write ability called with a method it does not take is rejected and writes nothing. * - * @dataProvider data_wrong_methods_for_write_abilities - * * @ticket 66706 + * @dataProvider data_wrong_methods_for_write_abilities * @since 7.3.0 * * @param string $ability The ability name. @@ -771,12 +762,12 @@ public function test_write_ability_with_a_wrong_method_returns_405( string $abil ), ), ); - $before = get_post( $post_id )->to_array(); + $before = $this->snapshot( $post_id ); $response = $this->server->dispatch( $this->write_request( $method, $ability, $inputs[ $ability ] ) ); $this->assert_error_response( $response, 405, 'rest_ability_invalid_method', 'A wrong method' ); - $this->assertSame( $before, get_post( $post_id )->to_array(), 'The post should not change.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } /** @@ -919,7 +910,7 @@ public function test_create_with_an_unsupported_attribute_returns_400(): void { ); $this->assert_error_response( $response, 400, 'content_invalid_attribute', 'A create with an unsupported attribute' ); - $this->assertSame( array( 'data[parent]' => 'The post type is not hierarchical.' ), $response->get_data()['data']['params'] ?? null, 'The params should reach the response.' ); + $this->assertSame( array( 'data[parent]' => 'The parent attribute is only supported for hierarchical post types.' ), $response->get_data()['data']['params'] ?? null, 'The params should reach the response.' ); $this->assertSame( $before, $this->snapshot(), 'A create with an unsupported attribute should write nothing.' ); } From 799eb36e40bb188f05ca7f536d7332a962dc2f8c Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 16 Nov 2026 20:53:18 +0000 Subject: [PATCH 04/24] Abilities API: Use a winter date in the content update ability tests. The floating draft tests used a date in June 2099 in a time zone with daylight saving time. PHP versions before 8.1 apply no daylight saving time after 2037, so the expected GMT date was one hour off on PHP 7.4 and 8.0. --- .../wpRegisterCoreContentUpdateAbility.php | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index f9a58e20e8fc3..a652fbfa8a2a1 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -1876,7 +1876,7 @@ public function test_draft_page_slug_is_unique_among_its_siblings( string $paren */ public function test_update_without_a_date_keeps_a_draft_floating(): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -1930,7 +1930,7 @@ public function test_publishing_a_floating_draft_without_a_date_stamps_the_curre */ public function test_pending_without_a_date_keeps_a_draft_floating(): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2012,7 +2012,7 @@ public static function data_new_dates_for_a_floating_draft(): array { */ public function test_new_date_pins_a_floating_draft( array $date ): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2025,14 +2025,17 @@ public function test_new_date_pins_a_floating_draft( array $date ): void { } /** - * Returns dates equal to the date a floating draft shows, 2099-06-15 10:00 in America/New_York. + * Returns dates equal to the date a floating draft shows, 2099-01-15 10:00 in America/New_York. + * + * The date is in winter because PHP versions before 8.1 apply no daylight saving time + * after 2037. * * @return array}> The dates. */ public static function data_dates_equal_to_the_floating_date(): array { return array( - 'site' => array( array( 'site' => '2099-06-15T10:00:00' ) ), - 'gmt' => array( array( 'gmt' => '2099-06-15T14:00:00' ) ), + 'site' => array( array( 'site' => '2099-01-15T10:00:00' ) ), + 'gmt' => array( array( 'gmt' => '2099-01-15T15:00:00' ) ), ); } @@ -2047,7 +2050,7 @@ public static function data_dates_equal_to_the_floating_date(): array { */ public function test_date_equal_to_the_floating_date_does_not_pin_the_draft( array $date ): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2127,8 +2130,8 @@ public function test_date_equal_to_the_stored_date_changes_nothing( array $date array( 'post_author' => self::$user_ids['editor'], 'post_status' => 'future', - 'post_date' => '2099-06-15 10:00:00', - 'post_date_gmt' => '2099-06-15 14:00:00', + 'post_date' => '2099-01-15 10:00:00', + 'post_date_gmt' => '2099-01-15 15:00:00', ) ); @@ -2138,8 +2141,8 @@ public function test_date_equal_to_the_stored_date_changes_nothing( array $date $post = $this->assert_updated( $this->update_post( $post_id, array( 'date' => $date ) ), $post_id ); $this->assertSame( 'future', $post->post_status, 'The post should stay scheduled.' ); - $this->assertSame( '2099-06-15 10:00:00', $post->post_date, 'The local date should not change.' ); - $this->assertSame( '2099-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should not change.' ); + $this->assertSame( '2099-01-15 10:00:00', $post->post_date, 'The local date should not change.' ); + $this->assertSame( '2099-01-15 15:00:00', $post->post_date_gmt, 'The GMT date should not change.' ); } /** @@ -2149,10 +2152,10 @@ public function test_date_equal_to_the_stored_date_changes_nothing( array $date */ public static function data_schedules_a_floating_draft_for_the_date_it_shows(): array { return array( - 'future, site' => array( 'future', array( 'site' => '2099-06-15T10:00:00' ) ), - 'future, gmt' => array( 'future', array( 'gmt' => '2099-06-15T14:00:00' ) ), - 'publish, site' => array( 'publish', array( 'site' => '2099-06-15T10:00:00' ) ), - 'publish, gmt' => array( 'publish', array( 'gmt' => '2099-06-15T14:00:00' ) ), + 'future, site' => array( 'future', array( 'site' => '2099-01-15T10:00:00' ) ), + 'future, gmt' => array( 'future', array( 'gmt' => '2099-01-15T15:00:00' ) ), + 'publish, site' => array( 'publish', array( 'site' => '2099-01-15T10:00:00' ) ), + 'publish, gmt' => array( 'publish', array( 'gmt' => '2099-01-15T15:00:00' ) ), ); } @@ -2168,7 +2171,7 @@ public static function data_schedules_a_floating_draft_for_the_date_it_shows(): */ public function test_scheduling_a_floating_draft_for_the_date_it_shows( string $status, array $date ): void { update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2099-06-15 10:00:00' ); + $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); @@ -2184,8 +2187,8 @@ public function test_scheduling_a_floating_draft_for_the_date_it_shows( string $ $post = $this->assert_updated( $result, $post_id ); $this->assertSame( 'future', $post->post_status, 'The post should be scheduled.' ); - $this->assertSame( '2099-06-15 10:00:00', $post->post_date, 'The local date should be the sent date.' ); - $this->assertSame( '2099-06-15 14:00:00', $post->post_date_gmt, 'The GMT date should be the sent date.' ); + $this->assertSame( '2099-01-15 10:00:00', $post->post_date, 'The local date should be the sent date.' ); + $this->assertSame( '2099-01-15 15:00:00', $post->post_date_gmt, 'The GMT date should be the sent date.' ); $this->assertSame( 'future', $result['status'], 'The scheduled status should be returned.' ); } From 6386a17f512653154bacfa7e9cdee77717697e8d Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 16 Nov 2026 21:14:31 +0000 Subject: [PATCH 05/24] Abilities API: Check the post capability in the content update and delete permission callbacks. The permission callbacks of `core/content-update` and `core/content-delete` now require `edit_post` or `delete_post` on the post that `where` names, the way the `core/content-create` callback already requires `create_posts`. Before, they only required a logged-in user and an exposed post type. A post that does not exist is let through, so that the call still ends as not found. The execute callbacks keep their own capability check. It applies when the `wp_ability_permission_result` filter grants the permission and when an execute callback is called directly. In the tests, a post the user cannot edit or delete is now refused by the permission callback. New tests cover the callback for input that was not prepared, for a post the user can edit or delete and for a missing post, and the capability check that remains after a filter grants the permission. --- .../abilities/class-wp-abilities-content.php | 24 ++-- .../testcase-content-write-ability.php | 50 ++++++++ .../wpRegisterCoreContentDeleteAbility.php | 93 ++++++++++++--- .../wpRegisterCoreContentUpdateAbility.php | 110 +++++++++++++----- .../wpRestAbilitiesContentController.php | 4 +- 5 files changed, 233 insertions(+), 48 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 5dfd8452d91f8..1d9381678243a 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -240,7 +240,7 @@ private function register_content_update( array $post_types, array $statuses ): return $this->execute_content_update( $input ); }, 'permission_callback' => function ( $input = array() ): bool { - return $this->check_post_permission( $input ); + return $this->check_post_permission( $input, 'edit_post' ); }, 'meta' => array( 'annotations' => array( @@ -274,7 +274,7 @@ private function register_content_delete( array $post_types ): void { return $this->execute_content_delete( $input ); }, 'permission_callback' => function ( $input = array() ): bool { - return $this->check_post_permission( $input ); + return $this->check_post_permission( $input, 'delete_post' ); }, 'meta' => array( 'annotations' => array( @@ -353,23 +353,31 @@ private function check_create_permission( $input = array() ): bool { /** * Permission callback for the `core/content-update` and `core/content-delete` abilities. * - * A coarse gate: a logged-in user and a `where.type` that is exposed to abilities. The - * post and the capabilities on it are checked by the execute callbacks. + * Requires a logged-in user, a `where.type` that is exposed to abilities, and the given + * capability on the post that `where` names. A post that does not exist is let through, + * so that the execute callback can report it as not found. The execute callbacks check + * the capability again and make every other decision. * * @since 7.3.0 * - * @param mixed $input Optional. The ability input. Default empty array. + * @param mixed $input The ability input. + * @param string $capability The capability required on the post: `edit_post` or `delete_post`. * @return bool True if the request may proceed, false otherwise. */ - private function check_post_permission( $input = array() ): bool { + private function check_post_permission( $input, string $capability ): bool { if ( ! is_user_logged_in() ) { return false; } // The input is read defensively: WP_Ability::check_permissions() does not prepare it. $where = $this->get_input_object( rest_sanitize_object( $input ), 'where' ) ?? array(); + if ( ! $this->get_exposed_post_type( $where['type'] ?? null ) ) { + return false; + } + + $post = $this->get_requested_post( array( 'where' => $where ) ); - return null !== $this->get_exposed_post_type( $where['type'] ?? null ); + return ! $post || current_user_can( $capability, $post->ID ); } /** @@ -920,6 +928,7 @@ private function execute_content_update( array $input ) { return $this->not_found_error(); } + // Checked again after the permission callback, whose result a filter can change. if ( ! current_user_can( 'edit_post', $post->ID ) ) { return new WP_Error( 'content_cannot_edit', @@ -968,6 +977,7 @@ private function execute_content_delete( array $input ) { return $this->not_found_error(); } + // Checked again after the permission callback, whose result a filter can change. if ( ! current_user_can( 'delete_post', $post->ID ) ) { return new WP_Error( 'content_cannot_delete', diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php index 7373a3552ac98..bab079369a663 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -258,6 +258,19 @@ protected function assert_error( $result, string $code, int $status, string $con $this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." ); } + /** + * Asserts that a result is the error of a call that the permission callback refused. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $context What the call was, for the assertion messages. + */ + protected function assert_refused( $result, string $context ): void { + $this->assertWPError( $result, "{$context}: the call should fail." ); + $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), "{$context}: the permission callback should refuse the call." ); + } + /** * Asserts that a result is the invalid attribute error for the given attribute. * @@ -307,6 +320,43 @@ protected function strip_descriptions( array $schema ): array { return $schema; } + /** + * Returns the forms a `where` can take in input that was not prepared. + * + * @return array The type of the ID, and + * whether `where` is an object. + */ + public static function data_unprepared_where_forms(): array { + return array( + 'integer ID' => array( 'integer', false ), + 'integer string ID' => array( 'string', false ), + 'float ID' => array( 'float', false ), + 'where that is an object' => array( 'integer', true ), + ); + } + + /** + * Builds a `where` for a post in one of the forms of unprepared input. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @param string $id_type The type to send the ID as. + * @param bool $as_object Whether to send `where` as an object. + * @return array|stdClass The `where`. + */ + protected function build_unprepared_where( int $post_id, string $id_type, bool $as_object ) { + $id = $post_id; + settype( $id, $id_type ); + + $where = array( + 'type' => get_post_type( $post_id ), + 'id' => $id, + ); + + return $as_object ? (object) $where : $where; + } + /** * Returns posts that cannot be reached as a `post`. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index b7471dda26827..47d8340c150f3 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -425,18 +425,85 @@ public function test_permission_callback_returns_false_for_malformed_input( $inp } /** - * The permission callback is only a coarse gate: it lets a logged-in subscriber through, and - * the execute callback decides on the post. + * The permission callback returns false for a post the user cannot delete, in every form the + * post can be named in input that was not prepared. + * + * @ticket 66706 + * @dataProvider data_unprepared_where_forms + * @since 7.3.0 + * + * @param string $id_type The type to send the ID as. + * @param bool $as_object Whether to send `where` as an object. + */ + public function test_permission_callback_returns_false_for_a_post_the_user_cannot_delete( string $id_type, bool $as_object ): void { + $this->login_as( 'contributor' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + $where = $this->build_unprepared_where( self::$post_ids['admin_post'], $id_type, $as_object ); + + $this->assertFalse( $permission( array( 'where' => $where ) ), 'The permission callback should return false.' ); + } + + /** + * The permission callback returns true for a post the user can delete. * * @ticket 66706 * @since 7.3.0 */ - public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type(): void { + public function test_permission_callback_returns_true_for_a_post_the_user_can_delete(): void { + $this->login_as( 'author' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertTrue( $permission( $this->delete_input( self::$post_ids['author:draft'] ) ), 'The permission callback should return true.' ); + } + + /** + * The permission callback lets a post that does not exist through, also for a user who may + * delete nothing. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_permission_callback_lets_a_missing_post_through(): void { $this->login_as( 'subscriber' ); $permission = $this->get_ability_callbacks()['permission_callback']; - $this->assertTrue( $permission( $this->delete_input( self::$post_ids['admin_post'] ) ), 'The permission callback should return true.' ); + $this->assertTrue( + $permission( + array( + 'where' => array( + 'type' => 'post', + 'id' => self::MISSING_ID, + ), + ) + ), + 'The permission callback should return true.' + ); + } + + /** + * A permission result that a filter turned into a grant does not skip the capability check + * on the post. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_granted_permission_result_does_not_skip_the_capability_check(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + add_filter( 'wp_ability_permission_result', '__return_true' ); + + $before = $this->snapshot( $post_id ); + $trash = $this->run_ability( $this->delete_input( $post_id ) ); + $permanent = $this->run_ability( $this->delete_input( $post_id, true ) ); + + $this->assert_cannot_delete( $trash, 'A trash granted by a filter' ); + $this->assert_cannot_delete( $permanent, 'A permanent delete granted by a filter' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } /** @@ -603,7 +670,7 @@ public function test_direct_call_with_a_malformed_where_is_not_found( array $inp /** * The lookup comes before the capability: a user who may delete nothing gets the not-found - * error for a missing post, and the 403 error for an existing post. + * error for a missing post, and is refused for an existing post. * * @ticket 66706 * @since 7.3.0 @@ -624,7 +691,7 @@ public function test_lookup_comes_before_the_capability(): void { $existing = $this->run_ability( $this->delete_input( self::$post_ids['admin_post'] ) ); $this->assert_not_found( $missing, 'A missing post' ); - $this->assert_cannot_delete( $existing, 'An existing post the user may not delete' ); + $this->assert_refused( $existing, 'An existing post the user may not delete' ); $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'The refused post should still exist, unchanged.' ); } @@ -710,7 +777,7 @@ public static function data_delete_matrix(): array { /** * Each role may trash the posts it may delete, gets the 410 error for those already in the - * Trash, and the 403 error for the others, whatever their status. + * Trash, and is refused for the others, whatever their status. * * @ticket 66706 * @dataProvider data_delete_matrix @@ -738,14 +805,14 @@ public function test_trash_matrix( string $role, string $owner, string $status, if ( $may_delete ) { $this->assert_error( $result, 'content_already_trashed', 410, 'Trashing a trashed post the role may delete' ); } else { - $this->assert_cannot_delete( $result, 'Trashing a post the role may not delete' ); + $this->assert_refused( $result, 'Trashing a post the role may not delete' ); } $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } /** * Each role may delete permanently the posts it may delete, including posts in the Trash, and - * gets the 403 error for the others, whatever their status. + * is refused for the others, whatever their status. * * @ticket 66706 * @dataProvider data_delete_matrix @@ -770,7 +837,7 @@ public function test_permanent_delete_matrix( string $role, string $owner, strin return; } - $this->assert_cannot_delete( $result, 'Deleting a post the role may not delete' ); + $this->assert_refused( $result, 'Deleting a post the role may not delete' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -863,8 +930,8 @@ public function test_editor_cannot_delete_a_special_page( string $page ): void { $trash = $this->run_ability( $this->delete_input( $post_id ) ); $permanent = $this->run_ability( $this->delete_input( $post_id, true ) ); - $this->assert_cannot_delete( $trash, 'An editor trashing a special page' ); - $this->assert_cannot_delete( $permanent, 'An editor deleting a special page' ); + $this->assert_refused( $trash, 'An editor trashing a special page' ); + $this->assert_refused( $permanent, 'An editor deleting a special page' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -955,7 +1022,7 @@ public function test_site_administrator_deletes_special_pages_on_multisite( stri return; } - $this->assert_cannot_delete( $result, 'A site administrator trashing the privacy policy page' ); + $this->assert_refused( $result, 'A site administrator trashing the privacy policy page' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index a652fbfa8a2a1..a9e161d3174d1 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -457,7 +457,7 @@ public function test_publishes_the_draft(): void { } /** - * A post the user cannot edit is the edit error. + * A post the user cannot edit is refused by the permission callback. * * @ticket 66706 * @since 7.3.0 @@ -471,20 +471,29 @@ public function test_rejects_a_post_the_user_cannot_edit(): void { $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'title' => 'Launch notes' ) ); - $this->assertWPError( $result, 'Updating a post of another user should fail.' ); - $this->assertSame( - array( - 'code' => 'content_cannot_edit', - 'message' => 'Sorry, you are not allowed to edit this post.', - 'data' => array( 'status' => 403 ), - ), - array( - 'code' => $result->get_error_code(), - 'message' => $result->get_error_message(), - 'data' => $result->get_error_data(), - ), - 'The error should be the edit error.' - ); + $this->assert_refused( $result, 'Updating a post of another user' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A permission result that a filter turned into a grant does not skip the capability check + * on the post. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_granted_permission_result_does_not_skip_the_capability_check(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + add_filter( 'wp_ability_permission_result', '__return_true' ); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'title' => 'Granted by a filter' ) ); + + $this->assert_error( $result, 'content_cannot_edit', 403, 'An update granted by a filter' ); + $this->assertSame( 'Sorry, you are not allowed to edit this post.', $result->get_error_message(), 'The message should be the edit message.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -561,14 +570,40 @@ public function test_permission_callback_returns_false_for_malformed_input( $inp } /** - * The permission callback is only a coarse gate: it lets a logged-in subscriber through, and - * the execute callback decides on the post. + * The permission callback returns false for a post the user cannot edit, in every form the + * post can be named in input that was not prepared. * * @ticket 66706 + * @dataProvider data_unprepared_where_forms * @since 7.3.0 + * + * @param string $id_type The type to send the ID as. + * @param bool $as_object Whether to send `where` as an object. */ - public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type(): void { - $this->login_as( 'subscriber' ); + public function test_permission_callback_returns_false_for_a_post_the_user_cannot_edit( string $id_type, bool $as_object ): void { + $this->login_as( 'contributor' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertFalse( + $permission( + array( + 'where' => $this->build_unprepared_where( self::$post_ids['admin_post'], $id_type, $as_object ), + 'data' => array( 'title' => 'x' ), + ) + ), + 'The permission callback should return false.' + ); + } + + /** + * The permission callback returns true for a post the user can edit. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_permission_callback_returns_true_for_a_post_the_user_can_edit(): void { + $this->login_as( 'author' ); $permission = $this->get_ability_callbacks()['permission_callback']; @@ -577,7 +612,7 @@ public function test_permission_callback_returns_true_for_a_logged_in_user_and_a array( 'where' => array( 'type' => 'post', - 'id' => self::$post_ids['admin_post'], + 'id' => self::$post_ids['author:draft'], ), 'data' => array( 'title' => 'x' ), ) @@ -586,6 +621,29 @@ public function test_permission_callback_returns_true_for_a_logged_in_user_and_a ); } + /** + * The permission callback lets a post that does not exist through, so that the call returns + * the not-found error, also for a user who may edit nothing. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_permission_callback_lets_a_missing_post_through(): void { + $this->login_as( 'subscriber' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + $input = array( + 'where' => array( + 'type' => 'post', + 'id' => self::MISSING_ID, + ), + 'data' => array( 'title' => 'x' ), + ); + + $this->assertTrue( $permission( $input ), 'The permission callback should return true.' ); + $this->assert_not_found( $this->run_ability( $input ), 'A missing post' ); + } + /** * Returns ID values that are not a whole number of at least 1. * @@ -875,8 +933,8 @@ public static function data_update_matrix(): array { } /** - * Each role may update the posts it may edit, in any status, and gets the 403 error for the - * others, whatever their status. + * Each role may update the posts it may edit, in any status, and is refused for the others, + * whatever their status. * * @ticket 66706 * @dataProvider data_update_matrix @@ -898,7 +956,7 @@ public function test_update_matrix( string $role, string $owner, string $status, $result = $this->update_post( $post_id, array( 'title' => 'Matrix update' ), array( 'status', 'title.rendered' ) ); if ( ! $allowed ) { - $this->assert_error( $result, 'content_cannot_edit', 403, 'Updating a post the role may not edit' ); + $this->assert_refused( $result, 'Updating a post the role may not edit' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); return; } @@ -2989,7 +3047,7 @@ public function test_edit_post_granted_on_one_post_allows_only_editing_that_post $other_before = $this->snapshot( $other_id ); $other = $this->update_post( $other_id, array( 'title' => 'Not granted' ) ); - $this->assert_error( $other, 'content_cannot_edit', 403, 'Updating a post that is not granted' ); + $this->assert_refused( $other, 'Updating a post that is not granted' ); $this->assertSame( $other_before, $this->snapshot( $other_id ), 'Nothing should be written for the post that is not granted.' ); $before = $this->snapshot( $post_id ); @@ -3009,7 +3067,7 @@ public function test_edit_post_granted_on_one_post_allows_only_editing_that_post ) ); - $this->assert_error( $delete, 'content_cannot_delete', 403, 'Deleting the granted post' ); + $this->assert_refused( $delete, 'Deleting the granted post' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for the refused delete.' ); } @@ -3056,7 +3114,7 @@ public function test_editor_cannot_update_the_privacy_policy_page(): void { $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'title' => 'Privacy' ) ); - $this->assert_error( $result, 'content_cannot_edit', 403, 'An editor updating the privacy policy page' ); + $this->assert_refused( $result, 'An editor updating the privacy policy page' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index d90366fbaff9e..857f506062871 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -854,7 +854,7 @@ public function test_update_of_a_missing_post_returns_404(): void { } /** - * The edit error of an update reaches the response with its status. + * An update of a post the user cannot edit is refused before the ability runs. * * @ticket 66706 * @since 7.3.0 @@ -883,7 +883,7 @@ public function test_update_of_a_post_the_user_cannot_edit_returns_403(): void { ) ); - $this->assert_error_response( $response, 403, 'content_cannot_edit', 'An update of a post the user cannot edit' ); + $this->assert_error_response( $response, 403, 'rest_ability_cannot_execute', 'An update of a post the user cannot edit' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'The post the user cannot edit should not change.' ); } From c2efdbd16c047c68c5197fc4a7c9a2852e2d4734 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 16 Nov 2026 21:38:09 +0000 Subject: [PATCH 06/24] Abilities API: Polish the content write abilities. - Add `enum` to the `outcome` of the `core/content-delete` output schema. - Note in the changelog that the `where.status` filter of `core/content-query` accepts `trash`. - Say to set `permanent` to true in the message for posts that cannot be trashed. - Pass the full input path to `invalid_attribute_error()`, as `invalid_filter_error()` takes it. - Rename `save_post()` to `persist_post()`, so it does not read as the `save_post` hook. - Read the statuses that can be set directly, instead of removing `trash` from the queryable ones. - Remove the unused input defaults from the write ability callbacks. - Align comments and docblocks. See #66706. --- .../abilities/class-wp-abilities-content.php | 76 +++++++++---------- .../wpRegisterCoreContentDeleteAbility.php | 3 +- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 1d9381678243a..ad7f309102e02 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -98,7 +98,8 @@ final class WP_Abilities_Content { /** * The `data` attributes the create and update abilities can write. * - * Only these reach the save, each mapped to one post field. Other keys are never written. + * An update must send at least one of them. {@see self::prepare_post_data()} maps each + * one that is sent to a post field. * * @since 7.3.0 * @var list @@ -119,7 +120,7 @@ final class WP_Abilities_Content { * Must run on the `wp_abilities_api_init` hook. * * @since 7.2.0 - * @since 7.3.0 Registers the `core/content-create`, `core/content-update`, and `core/content-delete` abilities. + * @since 7.3.0 Added the `core/content-create`, `core/content-update`, and `core/content-delete` abilities. */ public function register(): void { /* @@ -132,8 +133,8 @@ public function register(): void { return; } - // The statuses that can be set: the ones the query accepts, other than `trash`. - $statuses = array_values( array_diff( $this->get_queryable_statuses(), array( 'trash' ) ) ); + // The statuses that can be set: the ones that are not internal. + $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); $this->register_content_query( $post_types ); $this->register_content_create( $post_types, $statuses ); @@ -145,7 +146,7 @@ public function register(): void { * Registers the read-only `core/content-query` ability. * * @since 7.2.0 - * @since 7.3.0 Added the `$post_types` parameter. + * @since 7.3.0 Added the `$post_types` parameter and support for `trash` in the `where.status` filter. * * @param list $post_types Exposed post type names. */ @@ -201,10 +202,10 @@ private function register_content_create( array $post_types, array $statuses ): 'category' => self::CATEGORY, 'input_schema' => $this->get_content_create_input_schema( $post_types, $statuses ), 'output_schema' => $this->get_content_output_schema(), - 'execute_callback' => function ( $input = array() ) { + 'execute_callback' => function ( $input ) { return $this->execute_content_create( $input ); }, - 'permission_callback' => function ( $input = array() ): bool { + 'permission_callback' => function ( $input ): bool { return $this->check_create_permission( $input ); }, 'meta' => array( @@ -236,10 +237,10 @@ private function register_content_update( array $post_types, array $statuses ): 'category' => self::CATEGORY, 'input_schema' => $this->get_content_update_input_schema( $post_types, $statuses ), 'output_schema' => $this->get_content_output_schema(), - 'execute_callback' => function ( $input = array() ) { + 'execute_callback' => function ( $input ) { return $this->execute_content_update( $input ); }, - 'permission_callback' => function ( $input = array() ): bool { + 'permission_callback' => function ( $input ): bool { return $this->check_post_permission( $input, 'edit_post' ); }, 'meta' => array( @@ -270,10 +271,10 @@ private function register_content_delete( array $post_types ): void { 'category' => self::CATEGORY, 'input_schema' => $this->get_content_delete_input_schema( $post_types ), 'output_schema' => $this->get_content_delete_output_schema(), - 'execute_callback' => function ( $input = array() ) { + 'execute_callback' => function ( $input ) { return $this->execute_content_delete( $input ); }, - 'permission_callback' => function ( $input = array() ): bool { + 'permission_callback' => function ( $input ): bool { return $this->check_post_permission( $input, 'delete_post' ); }, 'meta' => array( @@ -335,15 +336,14 @@ private function check_query_permission( $input = array() ): bool { * * @since 7.3.0 * - * @param mixed $input Optional. The ability input. Default empty array. + * @param mixed $input The unprepared ability input. * @return bool True if the request may proceed, false otherwise. */ - private function check_create_permission( $input = array() ): bool { + private function check_create_permission( $input ): bool { if ( ! is_user_logged_in() ) { return false; } - // The input is read defensively: WP_Ability::check_permissions() does not prepare it. $data = $this->get_input_object( rest_sanitize_object( $input ), 'data' ) ?? array(); $post_type_object = $this->get_exposed_post_type( $data['type'] ?? null ); @@ -360,7 +360,7 @@ private function check_create_permission( $input = array() ): bool { * * @since 7.3.0 * - * @param mixed $input The ability input. + * @param mixed $input The unprepared ability input. * @param string $capability The capability required on the post: `edit_post` or `delete_post`. * @return bool True if the request may proceed, false otherwise. */ @@ -369,7 +369,6 @@ private function check_post_permission( $input, string $capability ): bool { return false; } - // The input is read defensively: WP_Ability::check_permissions() does not prepare it. $where = $this->get_input_object( rest_sanitize_object( $input ), 'where' ) ?? array(); if ( ! $this->get_exposed_post_type( $where['type'] ?? null ) ) { return false; @@ -892,6 +891,7 @@ private function execute_content_create( array $input ) { return $this->not_found_error(); } + // Checked again after the permission callback, whose result a filter can change. if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { return new WP_Error( 'content_cannot_create', @@ -905,7 +905,7 @@ private function execute_content_create( array $input ) { return $postarr; } - return $this->save_post( $postarr, $this->normalize_fields( $input ) ); + return $this->persist_post( $postarr, $this->normalize_fields( $input ) ); } /** @@ -956,7 +956,7 @@ private function execute_content_update( array $input ) { return $postarr; } - return $this->save_post( $postarr, $this->normalize_fields( $input ) ); + return $this->persist_post( $postarr, $this->normalize_fields( $input ) ); } /** @@ -999,7 +999,7 @@ private function execute_content_delete( array $input ) { if ( ! $this->can_trash( $post ) ) { return new WP_Error( 'content_trash_not_supported', - __( 'The post does not support trashing. Set "permanent" to delete it.' ), + __( 'The post does not support trashing. Set `permanent` to true to delete it.' ), array( 'status' => 501 ) ); } @@ -1155,7 +1155,7 @@ private function check_supported_attributes( array $data, string $post_type ): ? foreach ( $features as $attribute => $feature ) { if ( array_key_exists( $attribute, $data ) && ! post_type_supports( $post_type, $feature ) ) { return $this->invalid_attribute_error( - $attribute, + "data[{$attribute}]", /* translators: 1: Attribute name, such as "content", 2: Post type feature, such as "editor". */ sprintf( __( 'The %1$s attribute is only supported for post types that support the %2$s feature.' ), $attribute, $feature ) ); @@ -1163,7 +1163,7 @@ private function check_supported_attributes( array $data, string $post_type ): ? } if ( array_key_exists( 'parent', $data ) && ! is_post_type_hierarchical( $post_type ) ) { - return $this->invalid_attribute_error( 'parent', __( 'The parent attribute is only supported for hierarchical post types.' ) ); + return $this->invalid_attribute_error( 'data[parent]', __( 'The parent attribute is only supported for hierarchical post types.' ) ); } return null; @@ -1193,7 +1193,7 @@ private function prepare_status( array $data, WP_Post_Type $post_type_object, ?W * not even when the post already has them. */ if ( ! $status_object instanceof stdClass || ! empty( $status_object->internal ) ) { - return $this->invalid_attribute_error( 'status', __( 'The status must be a registered post status that is not internal.' ) ); + return $this->invalid_attribute_error( 'data[status]', __( 'The status must be a registered post status that is not internal.' ) ); } // Keeping the stored status changes nothing. @@ -1239,7 +1239,7 @@ private function prepare_author( array $data, WP_Post_Type $post_type_object, ?W $author_id = $this->parse_filter_int( $data['author'], 1 ); if ( null === $author_id ) { - return $this->invalid_attribute_error( 'author', __( 'The author must be a user ID.' ) ); + return $this->invalid_attribute_error( 'data[author]', __( 'The author must be a user ID.' ) ); } // Keeping the stored author changes nothing. @@ -1263,7 +1263,7 @@ private function prepare_author( array $data, WP_Post_Type $post_type_object, ?W // Intentionally strict: the author must be an existing user who belongs to this site. if ( ! get_userdata( $author_id ) || ( is_multisite() && ! is_user_member_of_blog( $author_id ) ) ) { - return $this->invalid_attribute_error( 'author', __( 'The author must be an existing user of this site.' ) ); + return $this->invalid_attribute_error( 'data[author]', __( 'The author must be an existing user of this site.' ) ); } return $author_id; @@ -1289,7 +1289,7 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post $reason = __( 'The parent must be 0, or a readable post of the same post type that is neither the post itself nor one of its descendants.' ); $parent_id = $this->parse_filter_int( $data['parent'], 0 ); if ( null === $parent_id ) { - return $this->invalid_attribute_error( 'parent', $reason ); + return $this->invalid_attribute_error( 'data[parent]', $reason ); } // Keeping the stored parent changes nothing. @@ -1307,11 +1307,11 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post */ $parent = get_post( $parent_id ); if ( ! $parent instanceof WP_Post || $post_type !== $parent->post_type || ! $this->check_read_permission( $parent ) ) { - return $this->invalid_attribute_error( 'parent', $reason ); + return $this->invalid_attribute_error( 'data[parent]', $reason ); } if ( $post && ( $post->ID === $parent->ID || in_array( $post->ID, get_post_ancestors( $parent ), true ) ) ) { - return $this->invalid_attribute_error( 'parent', $reason ); + return $this->invalid_attribute_error( 'data[parent]', $reason ); } return $parent_id; @@ -1346,7 +1346,7 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { } if ( null === $dates ) { - return $this->invalid_attribute_error( 'date', __( 'The date must be an object with a valid `site` or `gmt` date.' ) ); + return $this->invalid_attribute_error( 'data[date]', __( 'The date must be an object with a valid `site` or `gmt` date.' ) ); } $fields = array( @@ -1412,7 +1412,7 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_ ? current_user_can( 'publish_post', $post->ID ) : current_user_can( $post_type_object->cap->publish_posts ); if ( ! $can_publish ) { - return $this->invalid_attribute_error( 'slug', __( 'The slug of a pending post can only be set by a user who can publish the post.' ) ); + return $this->invalid_attribute_error( 'data[slug]', __( 'The slug of a pending post can only be set by a user who can publish the post.' ) ); } } @@ -1420,7 +1420,7 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_ // Intentionally strict: a slug that sanitizes to nothing is an error. if ( '' === $slug ) { - return $this->invalid_attribute_error( 'slug', __( 'The slug must contain characters that are valid in a slug.' ) ); + return $this->invalid_attribute_error( 'data[slug]', __( 'The slug must contain characters that are valid in a slug.' ) ); } /* @@ -1450,7 +1450,7 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_ * @return array|WP_Error The saved post, only its ID when the current user * may not read it, or a WP_Error on failure. */ - private function save_post( array $postarr, array $fields ) { + private function persist_post( array $postarr, array $fields ) { $postarr = wp_slash( $postarr ); $post_id = isset( $postarr['ID'] ) ? wp_update_post( $postarr, true ) : wp_insert_post( $postarr, true ); @@ -2280,6 +2280,7 @@ private function get_content_delete_output_schema(): array { 'properties' => array( 'outcome' => array( 'type' => 'string', + 'enum' => array( 'trashed', 'deleted' ), 'description' => __( 'What happened to the post: `trashed` when it was moved to the Trash, `deleted` when it was deleted permanently.' ), ), ), @@ -2870,19 +2871,16 @@ private function not_found_error(): WP_Error { /** * Builds the error for a `data` attribute that cannot be applied. * - * The error data maps the attribute's input path, such as `data[title]`, to the message - * under `params`, so callers can tell which attribute failed without parsing the - * translated message. + * The error data maps the attribute's input path to the message under `params`, so + * callers can tell which attribute failed without parsing the translated message. * * @since 7.3.0 * - * @param string $attribute The attribute name, such as `title`, or `data` for the whole object. - * @param string $message The error message. + * @param string $param The attribute's input path, such as `data[title]`. + * @param string $message The error message. * @return WP_Error The invalid attribute error. */ - private function invalid_attribute_error( string $attribute, string $message ): WP_Error { - $param = 'data' === $attribute ? 'data' : "data[{$attribute}]"; - + private function invalid_attribute_error( string $param, string $message ): WP_Error { return new WP_Error( 'content_invalid_attribute', $message, diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index 47d8340c150f3..fb1aa807051ff 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -263,6 +263,7 @@ public function test_output_schema(): void { $this->assertSame( array( 'outcome' ), $schema['required'], 'The output should require the outcome.' ); $this->assertSame( array( 'outcome' ), array_keys( $schema['properties'] ), 'The output should hold the outcome only.' ); $this->assertSame( 'string', $schema['properties']['outcome']['type'], 'The outcome should be a string.' ); + $this->assertSame( array( 'trashed', 'deleted' ), $schema['properties']['outcome']['enum'], 'The outcome should be one of trashed and deleted.' ); } /** @@ -879,7 +880,7 @@ public function test_post_without_trash_support_needs_permanent(): void { $result = $this->run_ability( $this->delete_input( $post_id ) ); $this->assert_error( $result, 'content_trash_not_supported', 501, 'Trashing a post without Trash support' ); - $this->assertSame( 'The post does not support trashing. Set "permanent" to delete it.', $result->get_error_message(), 'The message should ask for a permanent delete.' ); + $this->assertSame( 'The post does not support trashing. Set `permanent` to true to delete it.', $result->get_error_message(), 'The message should ask for a permanent delete.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'A post without Trash support should be left as it was.' ); $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'Deleting a post without Trash support permanently' ); From 33f5577fb2f1e11020156b2ba50bd353194842cb Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 16:51:46 +0000 Subject: [PATCH 07/24] Abilities API: Simplify the content write abilities and merge duplicate tests. Removes an unreachable post type check, a redundant guard and cast, and the separate list of write attributes and queryable statuses, and explains why the write abilities share one class with the query ability. Moves the fixtures, tests and providers shared by the create, update and delete tests to the base test case, turns duplicate tests into data provider rows, and fixes misleading test names, docblocks and assertions. See #66706. --- .../abilities/class-wp-abilities-content.php | 86 +-- .../testcase-content-write-ability.php | 215 ++++++- .../wpRegisterCoreContentAbility.php | 143 ++--- .../wpRegisterCoreContentCreateAbility.php | 509 ++++++---------- .../wpRegisterCoreContentDeleteAbility.php | 302 +++------- .../wpRegisterCoreContentUpdateAbility.php | 553 +++++------------- .../wpRestAbilitiesContentController.php | 39 +- 7 files changed, 695 insertions(+), 1152 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index ad7f309102e02..747811dd97820 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -27,6 +27,12 @@ * post type and the filters through the same code, and the input schema, output schema, * and field projection are built from the same field definitions. * + * The write abilities are deliberately kept in the same class as `core/content-query`, so + * that all four share these helpers, such as the read check, the field projection, and the + * input parsing, as private methods. Splitting them into separate classes would require + * making the shared helpers public or moving them to another class, and they are not meant + * to be part of the public API. + * * Only register() is public. The ability callbacks are closures that call private * methods, so callers go through the Abilities API, such as * `wp_get_ability( 'core/content-query' )->execute()`, which validates the input and @@ -95,17 +101,6 @@ final class WP_Abilities_Content { */ private const LOOP_GLOBALS = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); - /** - * The `data` attributes the create and update abilities can write. - * - * An update must send at least one of them. {@see self::prepare_post_data()} maps each - * one that is sent to a post field. - * - * @since 7.3.0 - * @var list - */ - private const WRITE_ATTRIBUTES = array( 'title', 'content', 'excerpt', 'status', 'slug', 'date', 'author', 'parent' ); - /** * Whether each author seen in the current call has posts the current user may read. * @@ -136,7 +131,7 @@ public function register(): void { // The statuses that can be set: the ones that are not internal. $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); - $this->register_content_query( $post_types ); + $this->register_content_query( $post_types, $statuses ); $this->register_content_create( $post_types, $statuses ); $this->register_content_update( $post_types, $statuses ); $this->register_content_delete( $post_types ); @@ -146,18 +141,21 @@ public function register(): void { * Registers the read-only `core/content-query` ability. * * @since 7.2.0 - * @since 7.3.0 Added the `$post_types` parameter and support for `trash` in the `where.status` filter. + * @since 7.3.0 Added the `$post_types` and `$statuses` parameters, and support for `trash` + * in the `where.status` filter. * * @param list $post_types Exposed post type names. + * @param list $statuses Post status slugs that are not internal. */ - private function register_content_query( array $post_types ): void { + private function register_content_query( array $post_types, array $statuses ): void { /* - * Internal statuses (e.g. `inherit`) are excluded here, other than `trash`, and from - * the statuses a query covers by default, so the posts of post types that rely on them - * (attachments) cannot be queried, not even by ID. Revisit if such a post type is ever - * exposed via `show_in_abilities`. + * Of the internal statuses, only `trash` can be requested, and only by name: it is never + * one of the statuses a query covers by default. The others, such as `inherit`, can never + * be queried, so the posts of post types that rely on them (attachments) cannot be + * queried, not even by ID. Revisit if such a post type is ever exposed via + * `show_in_abilities`. */ - $statuses = $this->get_queryable_statuses(); + $statuses[] = 'trash'; wp_register_ability( 'core/content-query', @@ -548,10 +546,10 @@ private function check_read_permission( WP_Post $post, array $checked_post_ids = /* * Mirror the REST posts controller's inherited-parent behavior, but keep the * ability fail-closed for missing parents or parent loops. No input can request - * `inherit`, since internal statuses other than `trash` are left out of the input - * schema, and all of them of the statuses a query covers by default, but a query - * filter such as a `pre_get_posts` callback can still add it, so such posts keep - * their parent's read check. + * `inherit`: the input schema leaves out every internal status other than `trash`, + * and the statuses a query covers by default hold no internal status. A query + * filter such as a `pre_get_posts` callback can still add it, though, so such posts + * keep their parent's read check. */ if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) { $parent = get_post( $post->post_parent ); @@ -752,7 +750,7 @@ private function execute_content_query( $input = array() ) { * when none is left, which would cover every status, including internal ones such as * `trash`. Of the internal statuses, only `trash` can be requested, by name. */ - if ( ! empty( array_diff( $statuses, $this->get_queryable_statuses() ) ) ) { + if ( ! empty( array_diff( $statuses, get_post_stati( array( 'internal' => false ) ), array( 'trash' ) ) ) ) { return $this->invalid_filter_error( 'where[status]', /* translators: %s: Parameter. */ @@ -942,16 +940,12 @@ private function execute_content_update( array $input ) { * rejected instead of saving the post unchanged. */ $data = $input['data'] ?? array(); - if ( empty( array_intersect_key( $data, array_flip( self::WRITE_ATTRIBUTES ) ) ) ) { + if ( empty( array_intersect_key( $data, $this->get_content_write_properties( array() ) ) ) ) { return $this->invalid_attribute_error( 'data', __( 'The data must be an object with at least one attribute to write.' ) ); } - $post_type_object = $this->get_exposed_post_type( $post->post_type ); - if ( ! $post_type_object ) { - return $this->not_found_error(); - } - - $postarr = $this->prepare_post_data( $data, $post_type_object, $post ); + // get_requested_post() only returns posts of an exposed post type. + $postarr = $this->prepare_post_data( $data, get_post_type_object( $post->post_type ), $post ); if ( is_wp_error( $postarr ) ) { return $postarr; } @@ -1046,9 +1040,9 @@ private function get_requested_post( array $input ): ?WP_Post { * Checks the `data` attributes and builds the array passed to the save. * * Shared by the create and update abilities. Only the attributes of - * {@see self::WRITE_ATTRIBUTES} that were sent reach the array, each mapped to one post - * field, next to the values the server sets: the post type on create, the post ID on - * update. The first rule that fails is returned and nothing is written. + * {@see self::get_content_write_properties()} that were sent reach the array, each mapped + * to one post field, next to the values the server sets: the post type on create, the + * post ID on update. The first rule that fails is returned and nothing is written. * * @since 7.3.0 * @@ -1109,7 +1103,7 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, $postarr = array_merge( $postarr, $dates ); $final_status = $postarr['post_status'] ?? $post->post_status ?? 'draft'; - $final_parent = $postarr['post_parent'] ?? (int) ( $post->post_parent ?? 0 ); + $final_parent = $postarr['post_parent'] ?? $post->post_parent ?? 0; $slug = $this->prepare_slug( $data, $post_type_object, $post, $final_status, $final_parent ); if ( is_wp_error( $slug ) ) { @@ -1461,8 +1455,7 @@ private function persist_post( array $postarr, array $fields ) { return $post_id; } - // get_post() would return the global post for an ID of 0. - $post = $post_id > 0 ? get_post( $post_id ) : null; + $post = get_post( $post_id ); if ( ! $post instanceof WP_Post ) { return $this->not_found_error(); } @@ -1635,25 +1628,6 @@ private function get_requested_statuses( array $where ): array { return array_map( 'sanitize_key', $this->parse_list_input( $where, 'status' ) ); } - /** - * Returns the statuses the `status` filter accepts. - * - * These are the statuses that are not internal, followed by `trash`, so trashed posts can - * be queried by name. `trash` is never one of the statuses a query covers by default, see - * {@see self::normalize_statuses()}, and like every status that is not publicly viewable - * it requires edit access, see {@see self::can_query_status()}. - * - * @since 7.3.0 - * - * @return list Post status slugs. - */ - private function get_queryable_statuses(): array { - $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); - $statuses[] = 'trash'; - - return $statuses; - } - /** * Normalizes the statuses a query covers. * diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php index bab079369a663..7b176b1ba1489 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -85,6 +85,98 @@ public static function wpTearDownAfterClass(): void { self::$post_ids = array(); } + /** + * Creates the users and posts shared by the tests that write an existing post: a user per + * role and an `other` editor, one post per owner and matrix status, and the posts that + * data_posts_that_are_not_posts() names. + * + * @since 7.3.0 + * + * @param WP_UnitTest_Factory $factory The unit test factory. + */ + protected static function create_shared_fixtures( WP_UnitTest_Factory $factory ): void { + foreach ( self::ROLES as $role ) { + self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); + } + self::$user_ids['other'] = $factory->user->create( array( 'role' => 'editor' ) ); + + // One post per owner and status, for the matrix tests. + foreach ( array_merge( self::ROLES, array( 'other' ) ) as $owner ) { + foreach ( self::MATRIX_STATUSES as $status ) { + $stored_status = str_replace( 'trash_', '', $status ); + $post_id = $factory->post->create( + array( + 'post_author' => self::$user_ids[ $owner ], + 'post_status' => $stored_status, + 'post_title' => "Matrix {$owner} {$status}", + 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', + ) + ); + if ( $stored_status !== $status ) { + wp_trash_post( $post_id ); + } + self::$post_ids[ "{$owner}:{$status}" ] = $post_id; + } + } + + self::$post_ids['admin_post'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_title' => 'Launch notes', + 'post_name' => 'shared-slug', + ) + ); + + self::$post_ids['admin_page'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + + self::$post_ids['attachment'] = $factory->attachment->create_object( + 'image.jpg', + self::$post_ids['admin_post'], + array( + 'post_mime_type' => 'image/jpeg', + 'post_author' => self::$user_ids['administrator'], + ) + ); + + $revisioned_id = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_content' => 'First version.', + ) + ); + wp_update_post( + array( + 'ID' => $revisioned_id, + 'post_content' => 'Second version.', + ) + ); + self::$post_ids['revision'] = (int) array_key_first( wp_get_post_revisions( $revisioned_id ) ); + self::$post_ids['autosave'] = (int) _wp_put_post_revision( get_post( $revisioned_id ), true ); + + // Posts of core types that are not exposed. + foreach ( array( + 'reusable_block' => 'wp_block', + 'template' => 'wp_template', + 'menu_item' => 'nav_menu_item', + ) as $fixture => $post_type ) { + self::$post_ids[ $fixture ] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => $post_type, + 'post_status' => 'publish', + ) + ); + } + } + /** * Unregisters the content abilities and registers the content category. * @@ -212,6 +304,57 @@ protected function login_as( string $role ): void { wp_set_current_user( self::$user_ids[ $role ] ); } + /** + * Registers custom statuses of every visibility, before the abilities are registered. + * + * @since 7.3.0 + */ + protected function register_custom_statuses(): void { + register_post_status( + 'cw_public', + array( + 'label' => 'Public', + 'public' => true, + ) + ); + register_post_status( + 'cw_private', + array( + 'label' => 'Private', + 'private' => true, + ) + ); + register_post_status( + 'cw_viewable', + array( + 'label' => 'Viewable', + 'internal' => false, + 'public' => false, + 'publicly_queryable' => true, + ) + ); + register_post_status( + 'cw_protected', + array( + 'label' => 'Protected', + 'protected' => true, + ) + ); + register_post_status( + 'cw_hidden', + array( + 'label' => 'Hidden', + 'public' => false, + 'protected' => true, + ) + ); + + $this->assertTrue( is_post_status_viewable( 'cw_viewable' ), 'Precondition: the viewable status should be publicly viewable.' ); + $this->assertFalse( get_post_status_object( 'cw_viewable' )->internal, 'Precondition: the viewable status should not be internal.' ); + $this->assertFalse( get_post_status_object( 'cw_hidden' )->internal, 'Precondition: the hidden status should not be internal.' ); + $this->assertFalse( is_post_status_viewable( 'cw_hidden' ), 'Precondition: the hidden status should not be publicly viewable.' ); + } + /** * Takes a snapshot of the stored content: the number of posts per type and status, * revisions included, the number of meta rows and term relationships, and, when a post @@ -323,8 +466,8 @@ protected function strip_descriptions( array $schema ): array { /** * Returns the forms a `where` can take in input that was not prepared. * - * @return array The type of the ID, and - * whether `where` is an object. + * @return array The type of the ID, and whether `where` is + * an object. */ public static function data_unprepared_where_forms(): array { return array( @@ -360,7 +503,7 @@ protected function build_unprepared_where( int $post_id, string $id_type, bool $ /** * Returns posts that cannot be reached as a `post`. * - * @return array The posts, as fixture names. + * @return array The posts, as fixture names. */ public static function data_posts_that_are_not_posts(): array { return array( @@ -378,7 +521,7 @@ public static function data_posts_that_are_not_posts(): array { /** * Returns statuses that can never be set. * - * @return array The statuses. + * @return array The statuses. */ public static function data_statuses_that_can_never_be_set(): array { return array( @@ -398,7 +541,7 @@ public static function data_statuses_that_can_never_be_set(): array { /** * Returns authors that a user without `edit_others_posts` may not name. * - * @return array The authors, as fixture names. + * @return array The authors, as fixture names. */ public static function data_authors_named_without_edit_others_posts(): array { return array( @@ -410,7 +553,7 @@ public static function data_authors_named_without_edit_others_posts(): array { /** * Returns date values without a usable value for a direct call. * - * @return array}> The dates. + * @return array}> The dates. */ public static function data_dates_without_a_usable_value(): array { return array( @@ -419,4 +562,64 @@ public static function data_dates_without_a_usable_value(): array { 'GMT date that cannot be parsed' => array( array( 'gmt' => 'not a date' ) ), ); } + + /** + * Returns ID values that are not a whole number of at least 1. + * + * @return array The IDs. + */ + public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): array { + return array( + 'digits and letters' => array( '123abc' ), + 'fraction' => array( 1.5 ), + 'zero' => array( 0 ), + 'negative' => array( -1 ), + 'true' => array( true ), + 'null' => array( null ), + 'list' => array( array( 1 ) ), + 'empty string' => array( '' ), + ); + } + + /** + * The ability is not registered when no post type is exposed to abilities. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_is_not_registered_without_exposed_post_types(): void { + foreach ( array( 'post', 'page' ) as $post_type ) { + get_post_type_object( $post_type )->show_in_abilities = false; + } + + $this->register_abilities(); + + $ability = static::ABILITY; + + $this->assertFalse( wp_has_ability( $ability ), "The {$ability} ability should not be registered without an exposed post type." ); + } + + /** + * Returns inputs for which the permission callback returns false, even for an administrator. + * + * @return array The inputs. + */ + abstract public static function data_inputs_refused_by_the_permission_callback(): array; + + /** + * The permission callback returns false for missing or malformed input. + * + * @ticket 66706 + * @dataProvider data_inputs_refused_by_the_permission_callback + * @since 7.3.0 + * + * @param mixed $input The input. + */ + public function test_permission_callback_returns_false_for_malformed_input( $input ): void { + $this->login_as( 'administrator' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertFalse( $permission( $input ), 'The permission callback should return false.' ); + } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index b78b542dbebf5..611748a728bb1 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -3577,46 +3577,76 @@ public static function data_roles_with_edit_access_to_other_users_posts(): array } /** - * A query by ID without a status does not cover the internal `trash` status, even for a - * role that can edit the trashed post. + * Returns the filters that name a trashed post in a query without a status, and roles that + * can edit that post. * - * The default statuses with IDs are the statuses that are not internal, so the query - * neither returns nor counts the trashed post. The post is named by ID, since trashing a - * post renames its slug. + * @return array Query test cases. + */ + public static function data_queries_without_a_status(): array { + return array( + 'IDs, administrator' => array( + 'filter' => 'ids', + 'role' => 'administrator', + ), + 'IDs, editor' => array( + 'filter' => 'ids', + 'role' => 'editor', + ), + 'slugs, administrator' => array( + 'filter' => 'slugs', + 'role' => 'administrator', + ), + 'no filter, administrator' => array( + 'filter' => null, + 'role' => 'administrator', + ), + ); + } + + /** + * A query without a status does not cover the internal `trash` status, even for a role that + * can edit the trashed post, also not when it lists IDs or slugs. + * + * The default statuses with IDs or slugs are the statuses that are not internal, so the query + * neither returns nor counts the trashed post. * * @ticket 66268 - * @dataProvider data_roles_with_edit_access_to_other_users_posts + * @ticket 66706 + * @dataProvider data_queries_without_a_status * @since 7.2.0 + * @since 7.3.0 Covers queries by slug and queries without a filter. * - * @param string $role The role to query as. + * @param string|null $filter The filter that names the trashed post, `ids` or `slugs`, or null for none. + * @param string $role The role to query as. */ - public function test_ids_without_a_status_do_not_cover_trashed_posts( string $role ): void { - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$user_ids['author'], - 'post_status' => 'publish', - ) - ); - wp_trash_post( $post_id ); - - $this->assertSame( 'trash', get_post_status( $post_id ), 'Precondition: the post should be in the trash.' ); + public function test_query_without_a_status_does_not_cover_trashed_posts( ?string $filter, string $role ): void { + $post_id = $this->create_trashed_post( 'author' ); $this->login_as( $role ); $this->register_ability(); $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the role should be able to edit the trashed post.' ); + $where = array( 'type' => 'post' ); + if ( 'ids' === $filter ) { + $where['ids'] = array( $post_id ); + } elseif ( 'slugs' === $filter ) { + $where['slugs'] = array( get_post( $post_id )->post_name ); + } + + if ( null === $filter ) { + $this->assertNotContains( $post_id, $this->query_ids( $where ), 'A query without a status should not return a trashed post.' ); + return; + } + $result = wp_get_ability( 'core/content-query' )->execute( array( - 'where' => array( - 'type' => 'post', - 'ids' => array( $post_id ), - ), + 'where' => $where, 'fields' => array( 'id' ), ) ); - $this->assertSame( self::EMPTY_RESULT, $result, 'A query by ID without a status should neither return nor count a trashed post.' ); + $this->assertSame( self::EMPTY_RESULT, $result, "A query by {$filter} without a status should neither return nor count a trashed post." ); } /** @@ -6966,46 +6996,6 @@ public function test_trash_can_be_requested_next_to_another_status(): void { ); } - /** - * A query without a status does not cover `trash`, also not when it lists slugs. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_slugs_without_a_status_do_not_cover_trashed_posts(): void { - $post_id = $this->create_trashed_post( 'author' ); - - $this->login_as( 'administrator' ); - $this->register_ability(); - - $result = wp_get_ability( 'core/content-query' )->execute( - array( - 'where' => array( - 'type' => 'post', - 'slugs' => array( get_post( $post_id )->post_name ), - ), - 'fields' => array( 'id' ), - ) - ); - - $this->assertSame( self::EMPTY_RESULT, $result, 'A query by slug without a status should neither return nor count a trashed post.' ); - } - - /** - * A query without a status does not cover `trash`. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_query_without_a_status_does_not_cover_trashed_posts(): void { - $post_id = $this->create_trashed_post( 'administrator' ); - - $this->login_as( 'administrator' ); - $this->register_ability(); - - $this->assertNotContains( $post_id, $this->query_ids( array( 'type' => 'post' ) ), 'A query without a status should not return a trashed post.' ); - } - /** * Querying `trash` needs `edit_posts`. * @@ -7013,8 +7003,6 @@ public function test_query_without_a_status_does_not_cover_trashed_posts(): void * @since 7.3.0 */ public function test_trash_needs_edit_posts(): void { - $this->create_trashed_post( 'author' ); - $this->login_as( 'subscriber' ); $this->register_ability(); @@ -7038,8 +7026,8 @@ public function test_trash_needs_edit_posts(): void { */ public static function data_roles_that_edit_only_their_own_trashed_posts(): array { return array( - 'author' => array( 'author_secondary' ), - 'contributor' => array( 'contributor' ), + 'author' => array( 'role' => 'author_secondary' ), + 'contributor' => array( 'role' => 'contributor' ), ); } @@ -7078,27 +7066,4 @@ public function test_trash_returns_only_the_trashed_posts_the_user_may_read( str 'Only the own trashed post should be returned.' ); } - - /** - * Other internal statuses are still rejected. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_other_internal_statuses_are_still_rejected(): void { - $this->login_as( 'administrator' ); - $this->register_ability(); - - $result = wp_get_ability( 'core/content-query' )->execute( - array( - 'where' => array( - 'type' => 'post', - 'status' => array( 'auto-draft' ), - ), - ) - ); - - $this->assertWPError( $result, 'An auto-draft query should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject auto-draft.' ); - } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index 2aa37a2828180..4a60087c7114f 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -86,6 +86,7 @@ public static function wpSetUpBeforeClass( $factory ): void { ), ); + // Read through its `child` slug by data_draft_page_slugs_by_parent(), not by its key. self::$post_ids['child_page'] = $factory->post->create( array( 'post_author' => self::$user_ids['administrator'], @@ -128,12 +129,13 @@ public static function wpSetUpBeforeClass( $factory ): void { * * @since 7.3.0 * - * @param mixed $result The ability result. + * @param mixed $result The ability result. + * @param string $context Optional. What the call was, for the assertion messages. Default 'The create call'. * @return WP_Post|null The post with the returned ID, or null when there is none. */ - private function get_created_post( $result ): ?WP_Post { - $this->assertIsArray( $result, 'The create call should succeed.' ); - $this->assertArrayHasKey( 'id', $result, 'The created post should be returned with its ID.' ); + private function get_created_post( $result, string $context = 'The create call' ): ?WP_Post { + $this->assertIsArray( $result, "{$context} should succeed." ); + $this->assertArrayHasKey( 'id', $result, "{$context} should return the created post with its ID." ); return get_post( $result['id'] ); } @@ -179,22 +181,6 @@ public function test_registers_the_ability(): void { $this->assertFalse( $annotations['idempotent'], 'The ability should not be marked idempotent.' ); } - /** - * The ability is not registered when no post type is exposed to abilities. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_is_not_registered_without_exposed_post_types(): void { - foreach ( array( 'post', 'page' ) as $post_type ) { - get_post_type_object( $post_type )->show_in_abilities = false; - } - - $this->register_abilities(); - - $this->assertFalse( wp_has_ability( self::ABILITY ), 'The create ability should not be registered without an exposed post type.' ); - } - /** * The input schema takes `data`, with the post type and the allowed attributes, and `fields`. * @@ -416,49 +402,15 @@ private function query_item( string $type, int $post_id, array $fields ): array return $result['items'][0]; } - /** - * An attribute the post type does not support is an invalid attribute. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_rejects_a_parent_for_a_post_type_that_is_not_hierarchical(): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot(); - - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Launch notes', - 'parent' => self::$post_ids['published_post'], - ), - ) - ); - - $this->assertWPError( $result, 'A parent for a post should be rejected.' ); - $this->assertSame( 'content_invalid_attribute', $result->get_error_code(), 'The error should be the invalid attribute error.' ); - $this->assertSame( 'The parent attribute is only supported for hierarchical post types.', $result->get_error_message(), 'The message should give the reason.' ); - $this->assertSame( 400, $result->get_error_data()['status'], 'The status should be 400.' ); - $this->assertSame( array( 'data[parent]' => 'The parent attribute is only supported for hierarchical post types.' ), $result->get_error_data()['params'], 'The params should give the reason for the parent.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * Returns users that the permission callback refuses. * - * @return array The users. + * @return array The role, or null to stay logged out. */ public static function data_users_refused_by_the_permission_callback(): array { return array( - 'logged out' => array( - 'role' => null, - ), - 'subscriber' => array( - 'role' => 'subscriber', - ), + 'logged out' => array( null ), + 'subscriber' => array( 'subscriber' ), ); } @@ -488,62 +440,28 @@ public function test_permission_callback_refuses_users_who_cannot_create_posts( ) ); - $this->assertWPError( $result, 'The call should be refused.' ); - $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse the call.' ); + $this->assert_refused( $result, 'A user who cannot create posts' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } /** * Returns inputs for which the permission callback returns false, even for an administrator. * - * @return array The inputs. + * @return array The inputs. */ public static function data_inputs_refused_by_the_permission_callback(): array { return array( - 'no input' => array( - 'input' => null, - ), - 'no data' => array( - 'input' => array(), - ), - 'data that is a string' => array( - 'input' => array( 'data' => 'post' ), - ), - 'no type' => array( - 'input' => array( 'data' => array( 'title' => 'x' ) ), - ), - 'type that is a list' => array( - 'input' => array( 'data' => array( 'type' => array( 'post' ) ) ), - ), - 'type that is a number' => array( - 'input' => array( 'data' => array( 'type' => 1 ) ), - ), - 'unexposed type' => array( - 'input' => array( 'data' => array( 'type' => 'attachment' ) ), - ), - 'unregistered type' => array( - 'input' => array( 'data' => array( 'type' => 'cw_missing' ) ), - ), + 'no input' => array( null ), + 'no data' => array( array() ), + 'data that is a string' => array( array( 'data' => 'post' ) ), + 'no type' => array( array( 'data' => array( 'title' => 'x' ) ) ), + 'type that is a list' => array( array( 'data' => array( 'type' => array( 'post' ) ) ) ), + 'type that is a number' => array( array( 'data' => array( 'type' => 1 ) ) ), + 'unexposed type' => array( array( 'data' => array( 'type' => 'attachment' ) ) ), + 'unregistered type' => array( array( 'data' => array( 'type' => 'cw_missing' ) ) ), ); } - /** - * The permission callback returns false for missing or malformed input. - * - * @ticket 66706 - * @dataProvider data_inputs_refused_by_the_permission_callback - * @since 7.3.0 - * - * @param mixed $input The input. - */ - public function test_permission_callback_returns_false_for_malformed_input( $input ): void { - $this->login_as( 'administrator' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - - $this->assertFalse( $permission( $input ), 'The permission callback should return false.' ); - } - /** * The permission callback returns true for a user who may create posts of an exposed type. * @@ -559,9 +477,11 @@ public function test_permission_callback_returns_true_for_a_user_who_may_create_ } /** - * Returns the outcome of creating a post per role and requested status. + * Returns the outcome of creating a post per role and requested status: the role, the + * requested status or null to send none, the stored status or the error code, and the error + * message or null on success. * - * @return array The cases. + * @return array The cases. */ public static function data_create_by_role_and_status(): array { $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; @@ -670,30 +590,25 @@ public function test_type_no_longer_exposed_cannot_be_created(): void { $result = $this->run_ability( $input ); - $this->assertWPError( $result, 'The regular call should fail.' ); - $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a type that is no longer exposed.' ); + $this->assert_refused( $result, 'The regular call for a type that is no longer exposed' ); $direct = $callbacks['execute_callback']( $input ); - $this->assert_error( $direct, 'content_not_found', 404, 'A direct call for a type that is no longer exposed' ); + $this->assert_not_found( $direct, 'A direct call for a type that is no longer exposed' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } /** * Returns direct-call inputs without an exposed post type. * - * @return array}> The inputs. + * @return array}> The inputs. */ public static function data_direct_inputs_without_an_exposed_type(): array { return array( - 'no data' => array( - 'input' => array(), - ), - 'no type' => array( - 'input' => array( 'data' => array( 'title' => 'x' ) ), - ), + 'no data' => array( array() ), + 'no type' => array( array( 'data' => array( 'title' => 'x' ) ) ), 'attachment' => array( - 'input' => array( + array( 'data' => array( 'type' => 'attachment', 'title' => 'x', @@ -701,7 +616,7 @@ public static function data_direct_inputs_without_an_exposed_type(): array { ), ), 'revision' => array( - 'input' => array( + array( 'data' => array( 'type' => 'revision', 'title' => 'x', @@ -709,7 +624,7 @@ public static function data_direct_inputs_without_an_exposed_type(): array { ), ), 'unexposed type' => array( - 'input' => array( + array( 'data' => array( 'type' => 'cw_hidden', 'title' => 'x', @@ -737,58 +652,57 @@ public function test_direct_call_without_an_exposed_type_is_not_found( array $in $before = $this->snapshot(); $result = $execute( $input ); - $this->assert_error( $result, 'content_not_found', 404, 'A direct call without an exposed type' ); - $this->assertSame( 'The requested content was not found.', $result->get_error_message(), 'The message should be the not-found message.' ); + $this->assert_not_found( $result, 'A direct call without an exposed type' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } /** * Returns `data` values that are invalid in some way. * - * @return array}> The data. + * @return array}> The data. */ public static function data_invalid_data(): array { return array( 'parent on a post' => array( - 'data' => array( + array( 'type' => 'post', 'parent' => 1, ), ), 'unregistered status' => array( - 'data' => array( + array( 'type' => 'post', 'status' => 'cw_unregistered', ), ), 'publish' => array( - 'data' => array( + array( 'type' => 'post', 'status' => 'publish', ), ), 'empty slug' => array( - 'data' => array( + array( 'type' => 'post', 'title' => 'x', 'slug' => '!!!', ), ), 'missing author' => array( - 'data' => array( + array( 'type' => 'post', 'title' => 'x', 'author' => self::MISSING_ID, ), ), 'title that is a list' => array( - 'data' => array( + array( 'type' => 'post', 'title' => array( 'x' ), ), ), 'invalid date' => array( - 'data' => array( + array( 'type' => 'post', 'title' => 'x', 'date' => array( 'site' => 'not a date' ), @@ -820,9 +734,9 @@ public function test_direct_call_without_create_posts_fails_before_the_attribute } /** - * Returns keys that are not part of the input. + * Returns keys that are not part of the input, each with a value. * - * @return array The keys and values. + * @return array The keys and values. */ public static function data_keys_outside_the_allow_list(): array { return array( @@ -969,51 +883,10 @@ public function test_status_no_longer_registered_is_rejected(): void { $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Registers custom statuses of every visibility, before the abilities are registered. - * - * @since 7.3.0 - */ - private function register_custom_statuses(): void { - register_post_status( - 'cw_public', - array( - 'label' => 'Public', - 'public' => true, - ) - ); - register_post_status( - 'cw_private', - array( - 'label' => 'Private', - 'private' => true, - ) - ); - register_post_status( - 'cw_viewable', - array( - 'label' => 'Viewable', - 'internal' => false, - 'public' => false, - 'publicly_queryable' => true, - ) - ); - - $this->assertTrue( is_post_status_viewable( 'cw_viewable' ), 'Precondition: the viewable status should be publicly viewable.' ); - $this->assertFalse( get_post_status_object( 'cw_viewable' )->internal, 'Precondition: the viewable status should not be internal.' ); - register_post_status( - 'cw_protected', - array( - 'label' => 'Protected', - 'protected' => true, - ) - ); - } - /** * Returns custom statuses that make a post visible. * - * @return array The statuses. + * @return array The statuses. */ public static function data_custom_statuses_that_make_a_post_visible(): array { return array( @@ -1056,63 +929,55 @@ public function test_custom_visible_status_needs_publish_posts( string $status ) } /** - * A user with `publish_posts` may set a custom status that makes a post visible. - * - * @ticket 66706 - * @dataProvider data_custom_statuses_that_make_a_post_visible - * @since 7.3.0 + * Returns custom statuses and a role that may set them: the role and the status. * - * @param string $status The status. + * @return array The cases. */ - public function test_custom_visible_status_is_set_with_publish_posts( string $status ): void { - $this->register_custom_statuses(); - - $this->login_as( 'author' ); - $this->register_abilities(); - - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Custom status', - 'status' => $status, - ), - ) + public static function data_custom_statuses_a_role_may_set(): array { + return array( + 'public with publish_posts' => array( 'author', 'cw_public' ), + 'private with publish_posts' => array( 'author', 'cw_private' ), + 'publicly viewable with publish_posts' => array( 'author', 'cw_viewable' ), + 'hidden without publish_posts' => array( 'contributor', 'cw_protected' ), ); - - $this->assertSame( $status, $this->get_created_post( $result )->post_status, 'The custom status should be stored.' ); } /** - * A custom status that is neither public, private nor publicly viewable needs nothing beyond + * A user with `publish_posts` may set a custom status that makes a post visible, and a custom + * status that is neither public, private nor publicly viewable needs nothing beyond * `create_posts`. * * @ticket 66706 + * @dataProvider data_custom_statuses_a_role_may_set * @since 7.3.0 + * + * @param string $role The role to create as. + * @param string $status The status. */ - public function test_custom_status_that_keeps_a_post_hidden_needs_no_publish_posts(): void { + public function test_custom_status_is_set_by_a_role_that_may_set_it( string $role, string $status ): void { $this->register_custom_statuses(); - $this->login_as( 'contributor' ); + $this->login_as( $role ); $this->register_abilities(); $result = $this->run_ability( array( 'data' => array( 'type' => 'post', - 'title' => 'Protected status', - 'status' => 'cw_protected', + 'title' => 'Custom status', + 'status' => $status, ), ) ); - $this->assertSame( 'cw_protected', $this->get_created_post( $result )->post_status, 'The custom status should be stored.' ); + $this->assertSame( $status, $this->get_created_post( $result )->post_status, 'The custom status should be stored.' ); } /** - * Returns dates that change the stored status of a new post. + * Returns dates that change the stored status of a new post: the requested status, the + * requested GMT date and the stored status. * - * @return array The cases. + * @return array The cases. */ public static function data_statuses_reported_as_stored(): array { return array( @@ -1195,49 +1060,43 @@ public function test_naming_another_author_without_edit_others_posts_is_refused( } /** - * A user without `edit_others_posts` may name themselves as the author. + * Returns authors that a role may name: the role and the author, as fixture names. * - * @ticket 66706 - * @since 7.3.0 + * @return array The cases. */ - public function test_naming_oneself_as_the_author_needs_no_edit_others_posts(): void { - $this->login_as( 'contributor' ); - $this->register_abilities(); - - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Own post', - 'author' => self::$user_ids['contributor'], - ), - ) + public static function data_authors_a_role_may_name(): array { + return array( + 'oneself without edit_others_posts' => array( 'contributor', 'contributor' ), + 'another user with edit_others_posts' => array( 'editor', 'author' ), ); - - $this->assertSame( self::$user_ids['contributor'], (int) $this->get_created_post( $result )->post_author, 'The current user should be the author.' ); } /** - * A user with `edit_others_posts` may name another existing user as the author. + * A user without `edit_others_posts` may name themselves as the author, and a user with + * `edit_others_posts` may name another existing user. * * @ticket 66706 + * @dataProvider data_authors_a_role_may_name * @since 7.3.0 + * + * @param string $role The role to create as. + * @param string $author The author, as a fixture name. */ - public function test_naming_another_author_with_edit_others_posts(): void { - $this->login_as( 'editor' ); + public function test_naming_an_author_the_role_may_name( string $role, string $author ): void { + $this->login_as( $role ); $this->register_abilities(); $result = $this->run_ability( array( 'data' => array( 'type' => 'post', - 'title' => 'For the author', - 'author' => self::$user_ids['author'], + 'title' => 'Named author', + 'author' => self::$user_ids[ $author ], ), ) ); - $this->assertSame( self::$user_ids['author'], (int) $this->get_created_post( $result )->post_author, 'The named user should be the author.' ); + $this->assertSame( self::$user_ids[ $author ], (int) $this->get_created_post( $result )->post_author, 'The named user should be the author.' ); } /** @@ -1340,7 +1199,7 @@ public function test_naming_a_non_member_without_edit_others_posts_is_refused_li /** * Returns parents that a page may not have. * - * @return array The parents, as fixture names. + * @return array The parents, as fixture names. */ public static function data_parents_that_cannot_be_used(): array { return array( @@ -1352,8 +1211,8 @@ public static function data_parents_that_cannot_be_used(): array { } /** - * A missing parent, a parent of another type and a parent the user may not read give one - * identical error, so the error never tells whether a post exists. + * A missing parent, a parent of another type, a parent the user may not read and a revision + * give one identical error, so the error never tells whether a post exists. * * @ticket 66706 * @dataProvider data_parents_that_cannot_be_used @@ -1380,12 +1239,13 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin ); }; + // The reference is another missing ID, so the `missing post` row is compared with a different input too. $before = $this->snapshot(); - $reference = $create( self::MISSING_ID ); + $reference = $create( self::MISSING_ID + 1 ); $result = $create( self::$post_ids[ $parent_key ] ?? self::MISSING_ID ); $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); - $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for a missing parent.' ); + $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for another missing parent.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1420,25 +1280,28 @@ public function test_parent_is_stored(): void { ) ); - $this->assertSame( self::$post_ids['published_page'], $this->get_created_post( $child )->post_parent, 'The parent should be stored.' ); + $this->assertSame( self::$post_ids['published_page'], $this->get_created_post( $child, 'The create call of the child page' )->post_parent, 'The parent should be stored.' ); $this->assertSame( self::$post_ids['published_page'], $child['parent'], 'The parent should be returned.' ); - $this->assertSame( 0, $this->get_created_post( $top )->post_parent, 'A parent of 0 should make a top-level page.' ); + $this->assertSame( 0, $this->get_created_post( $top, 'The create call of the top-level page' )->post_parent, 'A parent of 0 should make a top-level page.' ); } /** - * Returns attributes that a post type does not support. + * Returns attributes that a post type does not support: the post type, the attribute, the + * value and, optionally, the expected error message. An author value is a user and a string + * parent value is a post, as fixture names. * - * @return array The cases. + * @return array The cases. */ public static function data_unsupported_attributes(): array { return array( 'excerpt of a page' => array( 'page', 'excerpt', 'An excerpt' ), 'parent of a post' => array( 'post', 'parent', 1 ), + 'existing parent of a post' => array( 'post', 'parent', 'published_post', 'The parent attribute is only supported for hierarchical post types.' ), 'parent 0 of a post' => array( 'post', 'parent', 0 ), 'title without title support' => array( 'cw_no_title', 'title', 'A title' ), 'content without editor support' => array( 'cw_title_only', 'content', 'Some content' ), 'excerpt without excerpt support' => array( 'cw_title_only', 'excerpt', 'An excerpt' ), - 'author without author support' => array( 'cw_title_only', 'author', 1 ), + 'author without author support' => array( 'cw_title_only', 'author', 'administrator' ), 'empty title without title support' => array( 'cw_no_title', 'title', '' ), ); } @@ -1450,17 +1313,25 @@ public static function data_unsupported_attributes(): array { * @dataProvider data_unsupported_attributes * @since 7.3.0 * - * @param string $type The post type. - * @param string $attribute The attribute. - * @param mixed $value The value. + * @param string $type The post type. + * @param string $attribute The attribute. + * @param mixed $value The value. + * @param string|null $message Optional. The expected error message. Default null. */ - public function test_unsupported_attribute_is_rejected( string $type, string $attribute, $value ): void { + public function test_unsupported_attribute_is_rejected( string $type, string $attribute, $value, ?string $message = null ): void { $this->register_exposed_post_type( 'cw_no_title', array( 'editor' ) ); $this->register_exposed_post_type( 'cw_title_only', array( 'title' ) ); $this->login_as( 'administrator' ); $this->register_abilities(); + // The fixtures do not exist yet when the provider runs. + if ( 'author' === $attribute ) { + $value = self::$user_ids[ $value ]; + } elseif ( 'parent' === $attribute && is_string( $value ) ) { + $value = self::$post_ids[ $value ]; + } + $data = array( 'type' => $type, $attribute => $value, @@ -1471,21 +1342,21 @@ public function test_unsupported_attribute_is_rejected( string $type, string $at if ( 'cw_no_title' === $type ) { $data['content'] = 'Unsupported'; } - if ( 'author' === $attribute ) { - $data['author'] = self::$user_ids['administrator']; - } $before = $this->snapshot(); $result = $this->run_ability( array( 'data' => $data ) ); $this->assert_invalid_attribute( $result, $attribute, 'An unsupported attribute' ); + if ( null !== $message ) { + $this->assertSame( $message, $result->get_error_message(), 'The message should give the reason.' ); + } $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } /** - * Returns attribute values of the wrong type. + * Returns attribute values of the wrong type: the post type, the attribute and the value. * - * @return array The cases. + * @return array The cases. */ public static function data_values_of_the_wrong_type(): array { return array( @@ -1500,6 +1371,7 @@ public static function data_values_of_the_wrong_type(): array { 'date with a number' => array( 'post', 'date', array( 'site' => 123 ) ), 'date that cannot be parsed' => array( 'post', 'date', array( 'site' => 'not a date' ) ), 'GMT date that cannot be parsed' => array( 'post', 'date', array( 'gmt' => 'not a date' ) ), + 'date that is not a date-time' => array( 'post', 'date', array( 'site' => '2010-60-01T02:00:00Z' ) ), 'author that is text' => array( 'post', 'author', 'abc' ), 'author 0' => array( 'post', 'author', 0 ), 'author that is a fraction' => array( 'post', 'author', 1.5 ), @@ -1657,7 +1529,7 @@ public function test_after_insert_hooks_fire_once(): void { /** * Returns slugs and the slug they are stored as. * - * @return array The cases. + * @return array The sent and the stored slugs. */ public static function data_slugs(): array { return array( @@ -1783,7 +1655,7 @@ public function test_slug_of_a_pending_post_is_stored_with_publish_posts(): void /** * Returns statuses for which the slug of a published post is made unique. * - * @return array The statuses, or null for the default. + * @return array The statuses, or null for the default. */ public static function data_statuses_with_unique_slugs(): array { return array( @@ -1822,9 +1694,10 @@ public function test_slug_of_another_post_is_made_unique( ?string $status ): voi } /** - * Returns parents for a draft page with the slug of an existing child page. + * Returns parents for a draft page with the slug of an existing child page, and the slug the + * page is stored with. * - * @return array The cases. + * @return array The parents, as fixture names, and the slugs. */ public static function data_draft_page_slugs_by_parent(): array { return array( @@ -1862,9 +1735,9 @@ public function test_draft_page_slug_is_unique_among_its_siblings( string $paren } /** - * Returns sent dates and the dates they are stored as, in America/New_York. + * Returns sent dates and the local and GMT dates they are stored as, in America/New_York. * - * @return array, local: string, gmt: string}> The cases. + * @return array, 1: string, 2: string}> The cases. */ public static function data_dates(): array { return array( @@ -1918,64 +1791,66 @@ public function test_date_is_stored_local_and_gmt( array $date, string $local, s } /** - * A draft created without a date has a floating date. + * Returns posts created without a date: the role, the requested status or null to send none, + * and the stored status. * - * @ticket 66706 - * @since 7.3.0 + * @return array The cases. */ - public function test_draft_without_a_date_floats(): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $post = $this->get_created_post( - $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Floating', - ), - ) - ) + public static function data_posts_without_a_date(): array { + return array( + 'draft of an administrator' => array( 'administrator', null, 'draft' ), + 'pending post of a contributor' => array( 'contributor', 'pending', 'pending' ), ); - - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The draft should have no GMT date.' ); } /** - * A date that is not a date-time fails input validation. + * A draft or a pending post created without a date has a floating date. * * @ticket 66706 + * @dataProvider data_posts_without_a_date * @since 7.3.0 + * + * @param string $role The role to create as. + * @param string|null $status The requested status, or null to send none. + * @param string $expected The stored status. */ - public function test_date_that_is_not_a_date_time_fails_input_validation(): void { - $this->login_as( 'administrator' ); + public function test_post_without_a_date_floats( string $role, ?string $status, string $expected ): void { + $this->login_as( $role ); $this->register_abilities(); - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Invalid date', - 'date' => array( 'site' => '2010-60-01T02:00:00Z' ), - ), - ) + $data = array( + 'type' => 'post', + 'title' => 'Floating', ); + if ( null !== $status ) { + $data['status'] = $status; + } - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the date.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + $post = $this->get_created_post( $this->run_ability( array( 'data' => $data ) ) ); + + $this->assertSame( $expected, $post->post_status, 'The post should be stored in the expected status.' ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The post should have no GMT date.' ); } /** * Returns markup and the values it is stored and rendered as for a user without - * `unfiltered_html`. + * `unfiltered_html`: the role, the sent markup, the stored and the rendered title, and the + * stored and the rendered content and excerpt. * - * @return array The cases. + * @return array The cases. */ public static function data_filtered_markup(): array { + $script = array( + '
div
strong ', + 'div strong ', + 'div strong', + '
div
strong ', + "
div
\n

strong

", + ); + return array( 'backslashes and non-ASCII text' => array( + 'author', '\o/ ¯\_(ツ)_/¯', '\o/ ¯\_(ツ)_/¯', '\o/ ¯\_(ツ)_/¯', @@ -1983,20 +1858,17 @@ public static function data_filtered_markup(): array { '

\o/ ¯\_(ツ)_/¯

', ), 'entities' => array( + 'author', '\\\&\\\ & &invalid; < < &lt;', '\\\&\\\ & &invalid; < < &lt;', '\\\&\\\ & &invalid; < < &lt;', '\\\&\\\ & &invalid; < < &lt;', '

\\\&\\\ & &invalid; < < &lt;

', ), - 'a script' => array( - '
div
strong ', - 'div strong ', - 'div strong', - '
div
strong ', - "
div
\n

strong

", - ), + 'a script' => array_merge( array( 'author' ), $script ), + 'a script of a contributor' => array_merge( array( 'contributor' ), $script ), 'a link with extra attributes' => array( + 'author', 'link', 'link', 'link', @@ -2036,22 +1908,24 @@ private function create_with_markup( string $markup ): array { } /** - * The markup of an author, who lacks `unfiltered_html`, is filtered as on any other save. + * The markup of an author or a contributor, who lack `unfiltered_html`, is filtered as on any + * other save. * * @ticket 66706 * @dataProvider data_filtered_markup * @since 7.3.0 * + * @param string $role The role to create as. * @param string $input The sent markup. * @param string $title_raw The stored title. * @param string $title_rendered The rendered title, trimmed. * @param string $text_raw The stored content and excerpt. * @param string $text_rendered The rendered content and excerpt, trimmed. */ - public function test_markup_of_an_author_is_filtered( string $input, string $title_raw, string $title_rendered, string $text_raw, string $text_rendered ): void { - $this->login_as( 'author' ); + public function test_markup_of_a_user_without_unfiltered_html_is_filtered( string $role, string $input, string $title_raw, string $title_rendered, string $text_raw, string $text_rendered ): void { + $this->login_as( $role ); - $this->assertFalse( current_user_can( 'unfiltered_html' ), 'Precondition: the author should not have unfiltered_html.' ); + $this->assertFalse( current_user_can( 'unfiltered_html' ), 'Precondition: the user should not have unfiltered_html.' ); $result = $this->create_with_markup( $input ); @@ -2063,22 +1937,6 @@ public function test_markup_of_an_author_is_filtered( string $input, string $tit $this->assertSame( $text_rendered, trim( $result['excerpt']['rendered'] ), 'The rendered excerpt should be filtered.' ); } - /** - * The markup of a contributor is filtered. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_markup_of_a_contributor_is_filtered(): void { - $this->login_as( 'contributor' ); - - $result = $this->create_with_markup( '
div
strong ' ); - - $this->assertSame( 'div strong ', $result['title']['raw'], 'The stored title should be filtered.' ); - $this->assertSame( '
div
strong ', $result['content']['raw'], 'The stored content should be filtered.' ); - $this->assertSame( '
div
strong ', $result['excerpt']['raw'], 'The stored excerpt should be filtered.' ); - } - /** * On multisite, the markup of a site administrator, who lacks `unfiltered_html`, is filtered. * @@ -2205,7 +2063,7 @@ public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new $post = $this->get_created_post( $result ); $this->assertSame( 'publish', $post->post_status, 'The post should be published.' ); - $this->assertFalse( current_user_can( 'edit_post', $post->ID ), 'Precondition: the user should not be able to edit the published post.' ); + $this->assertFalse( current_user_can( 'edit_post', $post->ID ), 'The user should not be able to edit the published post.' ); $this->assertSame( array( 'rendered' => 'Published once' ), $result['title'], 'The title should be returned without its raw value.' ); $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); } @@ -2303,32 +2161,6 @@ public function test_direct_call_rejects_a_date_without_a_usable_value( array $d $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * A pending post that a contributor creates without a date has a floating date. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_pending_post_of_a_contributor_without_a_date_floats(): void { - $this->login_as( 'contributor' ); - $this->register_abilities(); - - $post = $this->get_created_post( - $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Pending and floating', - 'status' => 'pending', - ), - ) - ) - ); - - $this->assertSame( 'pending', $post->post_status, 'The post should be pending.' ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The pending post should have no GMT date.' ); - } - /** * A type that gets `show_in_abilities` after the abilities were registered is not in the type * enum, so a create of that type fails input validation. @@ -2502,8 +2334,7 @@ public function test_editor_of_the_first_site_only_is_refused_on_the_second_site restore_current_blog(); - $this->assertWPError( $regular, 'The regular call on the second site should fail.' ); - $this->assertSame( 'ability_invalid_permissions', $regular->get_error_code(), 'The permission callback should use the capabilities of the second site.' ); + $this->assert_refused( $regular, 'The regular call on the second site' ); $this->assert_error( $direct, 'content_cannot_create', 403, 'A direct call on the second site' ); $this->assertSame( $second_before, $second_after, 'Nothing should be written on the second site.' ); $this->assertSame( $first_before, $this->snapshot(), 'Nothing should be written on the first site.' ); diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index fb1aa807051ff..81779af7b85af 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -30,37 +30,9 @@ class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_Abilitie * @param WP_UnitTest_Factory $factory The unit test factory. */ public static function wpSetUpBeforeClass( $factory ): void { - foreach ( self::ROLES as $role ) { - self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); - } - self::$user_ids['other'] = $factory->user->create( array( 'role' => 'editor' ) ); - - // One post per owner and status, for the matrix tests. - foreach ( array_merge( self::ROLES, array( 'other' ) ) as $owner ) { - foreach ( self::MATRIX_STATUSES as $status ) { - $stored_status = str_replace( 'trash_', '', $status ); - $post_id = $factory->post->create( - array( - 'post_author' => self::$user_ids[ $owner ], - 'post_status' => $stored_status, - 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', - ) - ); - if ( $stored_status !== $status ) { - wp_trash_post( $post_id ); - } - self::$post_ids[ "{$owner}:{$status}" ] = $post_id; - } - } + self::create_shared_fixtures( $factory ); - self::$post_ids['admin_post'] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - ) - ); - - foreach ( array( 'admin_page', 'front_page', 'posts_page', 'privacy_page' ) as $page ) { + foreach ( array( 'front_page', 'posts_page', 'privacy_page' ) as $page ) { self::$post_ids[ $page ] = $factory->post->create( array( 'post_author' => self::$user_ids['administrator'], @@ -69,46 +41,6 @@ public static function wpSetUpBeforeClass( $factory ): void { ) ); } - - self::$post_ids['attachment'] = $factory->attachment->create_object( - 'image.jpg', - self::$post_ids['admin_post'], - array( - 'post_mime_type' => 'image/jpeg', - 'post_author' => self::$user_ids['administrator'], - ) - ); - - $revisioned_id = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - 'post_content' => 'First version.', - ) - ); - wp_update_post( - array( - 'ID' => $revisioned_id, - 'post_content' => 'Second version.', - ) - ); - self::$post_ids['revision'] = (int) array_key_first( wp_get_post_revisions( $revisioned_id ) ); - self::$post_ids['autosave'] = (int) _wp_put_post_revision( get_post( $revisioned_id ), true ); - - // Posts of core types that are not exposed. - foreach ( array( - 'reusable_block' => 'wp_block', - 'template' => 'wp_template', - 'menu_item' => 'nav_menu_item', - ) as $fixture => $post_type ) { - self::$post_ids[ $fixture ] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => $post_type, - 'post_status' => 'publish', - ) - ); - } } /** @@ -203,22 +135,6 @@ public function test_registers_the_ability(): void { $this->assertTrue( $annotations['idempotent'], 'The ability should be marked idempotent.' ); } - /** - * The ability is not registered when no post type is exposed to abilities. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_is_not_registered_without_exposed_post_types(): void { - foreach ( array( 'post', 'page' ) as $post_type ) { - get_post_type_object( $post_type )->show_in_abilities = false; - } - - $this->register_abilities(); - - $this->assertFalse( wp_has_ability( self::ABILITY ), 'The delete ability should not be registered without an exposed post type.' ); - } - /** * The input schema takes `where`, with the post type and ID, and `permanent`. * @@ -266,32 +182,6 @@ public function test_output_schema(): void { $this->assertSame( array( 'trashed', 'deleted' ), $schema['properties']['outcome']['enum'], 'The outcome should be one of trashed and deleted.' ); } - /** - * A post is moved to the Trash by default. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_trashes_a_post(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $this->assert_trashed( - $this->run_ability( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - ) - ), - $post_id, - 'Trashing a post' - ); - } - /** * A page is deleted permanently with `permanent`. * @@ -304,19 +194,7 @@ public function test_deletes_a_page_permanently(): void { $this->login_as( 'administrator' ); $this->register_abilities(); - $this->assert_deleted( - $this->run_ability( - array( - 'where' => array( - 'type' => 'page', - 'id' => $post_id, - ), - 'permanent' => true, - ) - ), - $post_id, - 'Deleting a page permanently' - ); + $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'Deleting a page permanently' ); } /** @@ -331,12 +209,7 @@ public function test_rejects_trashing_a_trashed_post(): void { $this->login_as( 'administrator' ); $this->register_abilities(); - $input = array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - ); + $input = $this->delete_input( $post_id ); $this->assert_trashed( $this->run_ability( $input ), $post_id, 'The first call' ); @@ -374,15 +247,14 @@ public function test_permission_callback_refuses_a_logged_out_user(): void { $before = $this->snapshot( $post_id ); $result = $this->run_ability( $this->delete_input( $post_id, true ) ); - $this->assertWPError( $result, 'The call should be refused.' ); - $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a logged-out user.' ); + $this->assert_refused( $result, 'A logged-out user' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } /** * Returns inputs for which the permission callback returns false, even for an administrator. * - * @return array The inputs. + * @return array The inputs. */ public static function data_inputs_refused_by_the_permission_callback(): array { return array( @@ -408,23 +280,6 @@ public static function data_inputs_refused_by_the_permission_callback(): array { ); } - /** - * The permission callback returns false for missing or malformed input. - * - * @ticket 66706 - * @dataProvider data_inputs_refused_by_the_permission_callback - * @since 7.3.0 - * - * @param mixed $input The input. - */ - public function test_permission_callback_returns_false_for_malformed_input( $input ): void { - $this->login_as( 'administrator' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - - $this->assertFalse( $permission( $input ), 'The permission callback should return false.' ); - } - /** * The permission callback returns false for a post the user cannot delete, in every form the * post can be named in input that was not prepared. @@ -507,23 +362,6 @@ public function test_granted_permission_result_does_not_skip_the_capability_chec $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - /** - * Returns ID values that are not a whole number of at least 1. - * - * @return array The IDs. - */ - public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): array { - return array( - 'digits and letters' => array( '123abc' ), - 'fraction' => array( 1.5 ), - 'zero' => array( 0 ), - 'negative' => array( -1 ), - 'true' => array( true ), - 'null' => array( null ), - 'list' => array( array( 1 ) ), - ); - } - /** * An ID that is not a whole number of at least 1 fails input validation. * @@ -599,15 +437,7 @@ public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): $execute = $this->get_ability_callbacks()['execute_callback']; $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'cw_hidden', - 'id' => $post_id, - ), - 'permanent' => true, - ) - ); + $result = $execute( $this->delete_input( $post_id, true ) ); $this->assert_not_found( $result, 'A direct call for a type that is not exposed' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); @@ -631,8 +461,7 @@ public function test_type_no_longer_exposed_cannot_be_deleted(): void { $before = $this->snapshot( $post_id ); $result = $this->run_ability( $input ); - $this->assertWPError( $result, 'The regular call should fail.' ); - $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a type that is no longer exposed.' ); + $this->assert_refused( $result, 'A regular call for a type that is no longer exposed' ); $this->assert_not_found( $callbacks['execute_callback']( $input ), 'A direct call for a type that is no longer exposed' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -640,7 +469,7 @@ public function test_type_no_longer_exposed_cannot_be_deleted(): void { /** * Returns direct-call inputs with a malformed `where`. * - * @return array}> The inputs. + * @return array}> The inputs. */ public static function data_direct_inputs_with_a_malformed_where(): array { return array( @@ -699,7 +528,7 @@ public function test_lookup_comes_before_the_capability(): void { /** * Returns whether each role may delete each matrix post. * - * @return array The cases. + * @return array The cases. */ public static function data_delete_matrix(): array { return array( @@ -776,27 +605,43 @@ public static function data_delete_matrix(): array { ); } + /** + * Returns the cases of the delete matrix, plus one that sends `permanent` set to false. + * + * @return array The cases. + */ + public static function data_trash_matrix(): array { + return array_merge( + self::data_delete_matrix(), + array( + 'administrator, own, publish, permanent false' => array( 'administrator', 'administrator', 'publish', true, false ), + ) + ); + } + /** * Each role may trash the posts it may delete, gets the 410 error for those already in the - * Trash, and is refused for the others, whatever their status. + * Trash, and is refused for the others, whatever their status. `permanent` set to false also + * moves the post to the Trash. * * @ticket 66706 - * @dataProvider data_delete_matrix + * @dataProvider data_trash_matrix * @since 7.3.0 * - * @param string $role The role to delete as. - * @param string $owner The role of the author of the post, or `other`. - * @param string $status The matrix status of the post. - * @param bool $may_delete Whether the role may delete the post. + * @param string $role The role to delete as. + * @param string $owner The role of the author of the post, or `other`. + * @param string $status The matrix status of the post. + * @param bool $may_delete Whether the role may delete the post. + * @param bool|null $permanent Optional. The `permanent` value, or null to send none. Default null. */ - public function test_trash_matrix( string $role, string $owner, string $status, bool $may_delete ): void { + public function test_trash_matrix( string $role, string $owner, string $status, bool $may_delete, ?bool $permanent = null ): void { $post_id = self::$post_ids[ "{$owner}:{$status}" ]; $this->login_as( $role ); $this->register_abilities(); $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $this->delete_input( $post_id ) ); + $result = $this->run_ability( $this->delete_input( $post_id, $permanent ) ); if ( $may_delete && ! str_starts_with( $status, 'trash_' ) ) { $this->assert_trashed( $result, $post_id, 'Trashing a post the role may delete' ); @@ -842,21 +687,6 @@ public function test_permanent_delete_matrix( string $role, string $owner, strin $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - /** - * `permanent` set to false moves the post to the Trash. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_permanent_false_trashes_the_post(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $this->assert_trashed( $this->run_ability( $this->delete_input( $post_id, false ) ), $post_id, 'Deleting with permanent false' ); - } - /** * A post that cannot be moved to the Trash is not deleted in its place: the call fails * and asks for `permanent`, which then deletes the post. @@ -889,7 +719,7 @@ public function test_post_without_trash_support_needs_permanent(): void { /** * Returns the special pages of the site. * - * @return array The pages, as fixture names. + * @return array The pages, as fixture names. */ public static function data_special_pages(): array { return array( @@ -937,55 +767,57 @@ public function test_editor_cannot_delete_a_special_page( string $page ): void { } /** - * On a single site, an administrator may trash the special pages. + * Returns the special pages of the site, each with a trash and a permanent delete. * - * @group ms-excluded - * - * @ticket 66706 - * @dataProvider data_special_pages - * @since 7.3.0 - * - * @param string $page The page, as a fixture name. + * @return array The cases. */ - public function test_administrator_can_trash_a_special_page( string $page ): void { - $this->set_special_pages(); - $post_id = self::$post_ids[ $page ]; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $this->assert_trashed( $this->run_ability( $this->delete_input( $post_id ) ), $post_id, 'An administrator trashing a special page' ); + public static function data_special_page_deletes(): array { + return array( + 'front page, trash' => array( 'front_page', false ), + 'front page, permanent' => array( 'front_page', true ), + 'posts page, trash' => array( 'posts_page', false ), + 'posts page, permanent' => array( 'posts_page', true ), + 'privacy policy page, trash' => array( 'privacy_page', false ), + 'privacy policy page, permanent' => array( 'privacy_page', true ), + ); } /** - * On a single site, an administrator may delete the special pages permanently. + * On a single site, an administrator may trash the special pages and delete them permanently. * - * The page is not trashed first: trashing the front page or the posts page clears the - * option that makes it special. + * The page is not trashed before the permanent delete: trashing the front page or the posts + * page clears the option that makes it special. * * @group ms-excluded * * @ticket 66706 - * @dataProvider data_special_pages + * @dataProvider data_special_page_deletes * @since 7.3.0 * - * @param string $page The page, as a fixture name. + * @param string $page The page, as a fixture name. + * @param bool $permanent Whether the delete is permanent. */ - public function test_administrator_can_delete_a_special_page( string $page ): void { + public function test_administrator_can_delete_a_special_page( string $page, bool $permanent ): void { $this->set_special_pages(); $post_id = self::$post_ids[ $page ]; $this->login_as( 'administrator' ); $this->register_abilities(); - $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'An administrator deleting a special page' ); + $result = $this->run_ability( $this->delete_input( $post_id, $permanent ) ); + + if ( $permanent ) { + $this->assert_deleted( $result, $post_id, 'An administrator deleting a special page' ); + } else { + $this->assert_trashed( $result, $post_id, 'An administrator trashing a special page' ); + } } /** - * Returns the special pages of the site and whether a site administrator may delete them on + * Returns the special pages of the site and whether a site administrator may trash them on * multisite. * - * @return array The cases. + * @return array The cases. */ public static function data_special_pages_for_a_site_administrator(): array { return array( @@ -996,7 +828,7 @@ public static function data_special_pages_for_a_site_administrator(): array { } /** - * On multisite, a site administrator may delete the front page and the posts page, but not + * On multisite, a site administrator may trash the front page and the posts page, but not * the privacy policy page. * * @group ms-required @@ -1006,9 +838,9 @@ public static function data_special_pages_for_a_site_administrator(): array { * @since 7.3.0 * * @param string $page The page, as a fixture name. - * @param bool $allowed Whether the site administrator may delete it. + * @param bool $allowed Whether the site administrator may trash it. */ - public function test_site_administrator_deletes_special_pages_on_multisite( string $page, bool $allowed ): void { + public function test_site_administrator_trashes_special_pages_on_multisite( string $page, bool $allowed ): void { $this->set_special_pages(); $post_id = self::$post_ids[ $page ]; @@ -1054,7 +886,7 @@ public function test_super_admin_can_delete_a_special_page_on_multisite( string /** * Returns values for the pre-trash and pre-delete filters that stop the operation. * - * @return array The cases. + * @return array The cases. */ public static function data_operations_that_are_stopped(): array { return array( @@ -1101,7 +933,7 @@ static function () use ( $value ) { /** * Returns keys that are not part of the input, and where they are sent. * - * @return array The cases. + * @return array The cases. */ public static function data_keys_outside_the_allow_list(): array { return array( diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index a9e161d3174d1..a8b44262bf347 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -30,10 +30,8 @@ class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_Abilitie * @param WP_UnitTest_Factory $factory The unit test factory. */ public static function wpSetUpBeforeClass( $factory ): void { - foreach ( self::ROLES as $role ) { - self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); - } - self::$user_ids['other'] = $factory->user->create( array( 'role' => 'editor' ) ); + self::create_shared_fixtures( $factory ); + self::$user_ids['page_writer'] = $factory->user->create( array( 'role' => 'subscriber' ) ); self::$user_ids['publisher'] = $factory->user->create( array( 'role' => 'contributor' ) ); @@ -43,42 +41,6 @@ public static function wpSetUpBeforeClass( $factory ): void { // A user who may publish posts but not edit published posts. get_userdata( self::$user_ids['publisher'] )->add_cap( 'publish_posts' ); - // One post per owner and status, for the matrix tests. - foreach ( array_merge( self::ROLES, array( 'other' ) ) as $owner ) { - foreach ( self::MATRIX_STATUSES as $status ) { - $stored_status = str_replace( 'trash_', '', $status ); - $post_id = $factory->post->create( - array( - 'post_author' => self::$user_ids[ $owner ], - 'post_status' => $stored_status, - 'post_title' => "Matrix {$owner} {$status}", - 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', - ) - ); - if ( $stored_status !== $status ) { - wp_trash_post( $post_id ); - } - self::$post_ids[ "{$owner}:{$status}" ] = $post_id; - } - } - - self::$post_ids['admin_post'] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - 'post_title' => 'Launch notes', - 'post_name' => 'shared-slug', - ) - ); - - self::$post_ids['admin_page'] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => 'page', - 'post_status' => 'publish', - ) - ); - self::$post_ids['editor_draft_page'] = $factory->post->create( array( 'post_author' => self::$user_ids['editor'], @@ -125,47 +87,6 @@ public static function wpSetUpBeforeClass( $factory ): void { ) ); - self::$post_ids['attachment'] = $factory->attachment->create_object( - 'image.jpg', - self::$post_ids['admin_post'], - array( - 'post_mime_type' => 'image/jpeg', - 'post_author' => self::$user_ids['administrator'], - ) - ); - - $revisioned_id = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - 'post_content' => 'First version.', - ) - ); - wp_update_post( - array( - 'ID' => $revisioned_id, - 'post_content' => 'Second version.', - ) - ); - $revisions = wp_get_post_revisions( $revisioned_id ); - self::$post_ids['revision'] = (int) array_key_first( $revisions ); - self::$post_ids['autosave'] = (int) _wp_put_post_revision( get_post( $revisioned_id ), true ); - - // Posts of core types that are not exposed. - foreach ( array( - 'reusable_block' => 'wp_block', - 'template' => 'wp_template', - 'menu_item' => 'nav_menu_item', - ) as $fixture => $post_type ) { - self::$post_ids[ $fixture ] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => $post_type, - 'post_status' => 'publish', - ) - ); - } - // An author whose role grants publish_posts, denied it for this user. self::$user_ids['restricted_author'] = $factory->user->create( array( 'role' => 'author' ) ); get_userdata( self::$user_ids['restricted_author'] )->add_cap( 'publish_posts', false ); @@ -278,22 +199,6 @@ public function test_registers_the_ability(): void { $this->assertTrue( $annotations['idempotent'], 'The ability should be marked idempotent.' ); } - /** - * The ability is not registered when no post type is exposed to abilities. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_is_not_registered_without_exposed_post_types(): void { - foreach ( array( 'post', 'page' ) as $post_type ) { - get_post_type_object( $post_type )->show_in_abilities = false; - } - - $this->register_abilities(); - - $this->assertFalse( wp_has_ability( self::ABILITY ), 'The update ability should not be registered without an exposed post type.' ); - } - /** * The input schema takes `where`, with the post type and ID, `data`, with at least one * attribute, and `fields`. @@ -456,25 +361,6 @@ public function test_publishes_the_draft(): void { ); } - /** - * A post the user cannot edit is refused by the permission callback. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_rejects_a_post_the_user_cannot_edit(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'contributor' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'title' => 'Launch notes' ) ); - - $this->assert_refused( $result, 'Updating a post of another user' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * A permission result that a filter turned into a grant does not skip the capability check * on the post. @@ -511,15 +397,14 @@ public function test_permission_callback_refuses_a_logged_out_user(): void { $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'title' => 'Refused' ) ); - $this->assertWPError( $result, 'The call should be refused.' ); - $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a logged-out user.' ); + $this->assert_refused( $result, 'A logged-out user' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } /** * Returns inputs for which the permission callback returns false, even for an administrator. * - * @return array The inputs. + * @return array The inputs. */ public static function data_inputs_refused_by_the_permission_callback(): array { return array( @@ -552,23 +437,6 @@ public static function data_inputs_refused_by_the_permission_callback(): array { ); } - /** - * The permission callback returns false for missing or malformed input. - * - * @ticket 66706 - * @dataProvider data_inputs_refused_by_the_permission_callback - * @since 7.3.0 - * - * @param mixed $input The input. - */ - public function test_permission_callback_returns_false_for_malformed_input( $input ): void { - $this->login_as( 'administrator' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - - $this->assertFalse( $permission( $input ), 'The permission callback should return false.' ); - } - /** * The permission callback returns false for a post the user cannot edit, in every form the * post can be named in input that was not prepared. @@ -644,24 +512,6 @@ public function test_permission_callback_lets_a_missing_post_through(): void { $this->assert_not_found( $this->run_ability( $input ), 'A missing post' ); } - /** - * Returns ID values that are not a whole number of at least 1. - * - * @return array The IDs. - */ - public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): array { - return array( - 'digits and letters' => array( '123abc' ), - 'fraction' => array( 1.5 ), - 'zero' => array( 0 ), - 'negative' => array( -1 ), - 'true' => array( true ), - 'null' => array( null ), - 'list' => array( array( 1 ) ), - 'empty string' => array( '' ), - ); - } - /** * An ID that is not a whole number of at least 1 fails input validation. * @@ -775,8 +625,7 @@ public function test_type_no_longer_exposed_cannot_be_updated(): void { $before = $this->snapshot( $post_id ); $result = $this->run_ability( $input ); - $this->assertWPError( $result, 'The regular call should fail.' ); - $this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), 'The permission callback should refuse a type that is no longer exposed.' ); + $this->assert_refused( $result, 'A regular call for a type that is no longer exposed' ); $this->assert_not_found( $callbacks['execute_callback']( $input ), 'A direct call for a type that is no longer exposed' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -784,7 +633,7 @@ public function test_type_no_longer_exposed_cannot_be_updated(): void { /** * Returns direct-call inputs with a malformed `where`. * - * @return array}> The inputs. + * @return array}> The inputs. */ public static function data_direct_inputs_with_a_malformed_where(): array { return array( @@ -855,7 +704,7 @@ public function test_lookup_comes_before_the_attribute_rules(): void { /** * Returns whether each role may edit each matrix post. * - * @return array The cases. + * @return array The cases. */ public static function data_update_matrix(): array { return array( @@ -972,7 +821,7 @@ public function test_update_matrix( string $role, string $owner, string $status, /** * Returns `data` values that are invalid in some way. * - * @return array}> The data. + * @return array}> The data. */ public static function data_invalid_data(): array { return array( @@ -1022,7 +871,7 @@ public function test_direct_call_without_edit_post_fails_before_the_attributes( /** * Returns keys that are not part of the input, and where they are sent. * - * @return array The cases. + * @return array The cases. */ public static function data_keys_outside_the_allow_list(): array { return array( @@ -1143,7 +992,7 @@ public function test_direct_call_rejects_a_status_that_can_never_be_set( string * Returns status changes that a contributor, and an author denied `publish_posts`, may not * make on their own draft. * - * @return array The cases. + * @return array The cases. */ public static function data_status_changes_without_publish_posts(): array { $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; @@ -1248,42 +1097,50 @@ public function test_schedules_a_draft(): void { } /** - * Returns statuses a published post can be moved back to. + * Returns status changes that unpublish a post or move a post out of the Trash. * - * @return array The statuses. + * @return array The role, which owns the post, + * the matrix status of the post, + * and the sent status. */ - public static function data_unpublished_statuses(): array { + public static function data_status_changes(): array { return array( - 'draft' => array( 'draft' ), - 'pending' => array( 'pending' ), + 'draft from publish by an editor' => array( 'editor', 'publish', 'draft' ), + 'pending from publish by an editor' => array( 'editor', 'publish', 'pending' ), + 'draft from the Trash by a contributor' => array( 'contributor', 'trash_draft', 'draft' ), + 'pending from the Trash by an author' => array( 'author', 'trash_draft', 'pending' ), + 'publish from the Trash by an author' => array( 'author', 'trash_draft', 'publish' ), ); } /** - * A published post can be unpublished. + * A published post can be unpublished, and sending a status moves a post out of the Trash + * through a plain update. * * @ticket 66706 - * @dataProvider data_unpublished_statuses + * @dataProvider data_status_changes * @since 7.3.0 * - * @param string $status The status. + * @param string $role The role, which owns the post. + * @param string $stored The matrix status of the post. + * @param string $status The sent status. */ - public function test_unpublishes_a_post( string $status ): void { - $post_id = self::$post_ids['editor:publish']; + public function test_status_change_is_stored( string $role, string $stored, string $status ): void { + $post_id = self::$post_ids[ "{$role}:{$stored}" ]; - $this->login_as( 'editor' ); + $this->login_as( $role ); $this->register_abilities(); $result = $this->update_post( $post_id, array( 'status' => $status ), array( 'status' ) ); - $this->assertSame( $status, $this->assert_updated( $result, $post_id )->post_status, 'The post should be unpublished.' ); + $this->assertSame( $status, $this->assert_updated( $result, $post_id )->post_status, 'The post should be stored in the sent status.' ); $this->assertSame( $status, $result['status'], 'The new status should be returned.' ); } /** * Returns dates that change the stored status of a post. * - * @return array The cases. + * @return array The cases. */ public static function data_statuses_reported_as_stored(): array { return array( @@ -1342,41 +1199,6 @@ public function test_updates_a_post_in_the_trash(): void { $this->assertSame( 'trash', $result['status'], 'The trash status should be returned.' ); } - /** - * Returns status changes that move a post out of the Trash. - * - * @return array The cases. - */ - public static function data_statuses_that_move_a_post_out_of_the_trash(): array { - return array( - 'draft by a contributor' => array( 'contributor', 'draft' ), - 'pending by an author' => array( 'author', 'pending' ), - 'publish by an author' => array( 'author', 'publish' ), - ); - } - - /** - * Sending a status moves a post out of the Trash through a plain update. - * - * @ticket 66706 - * @dataProvider data_statuses_that_move_a_post_out_of_the_trash - * @since 7.3.0 - * - * @param string $role The role, which owns the post. - * @param string $status The status. - */ - public function test_status_moves_a_post_out_of_the_trash( string $role, string $status ): void { - $post_id = self::$post_ids[ "{$role}:trash_draft" ]; - - $this->login_as( $role ); - $this->register_abilities(); - - $result = $this->update_post( $post_id, array( 'status' => $status ), array( 'status' ) ); - - $this->assertSame( $status, $this->assert_updated( $result, $post_id )->post_status, 'The post should leave the Trash in the sent status.' ); - $this->assertSame( $status, $result['status'], 'The new status should be returned.' ); - } - /** * Moving a post out of the Trash passes the status gate like any status change. * @@ -1532,7 +1354,7 @@ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_inva /** * Returns parents that a page may not have. * - * @return array The parents, as fixture names. + * @return array The parents, as fixture names. */ public static function data_parents_that_cannot_be_used(): array { return array( @@ -1565,8 +1387,9 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should be able to edit the page.' ); $this->assertFalse( current_user_can( 'read_post', self::$post_ids['editor_draft_page'] ), 'Precondition: the user should not be able to read the draft page of the editor.' ); + // The reference names another missing ID than the missing post row, so no row compares a call with itself. $before = $this->snapshot( $post_id ); - $reference = $this->update_post( $post_id, array( 'parent' => self::MISSING_ID ) ); + $reference = $this->update_post( $post_id, array( 'parent' => self::MISSING_ID + 1 ) ); $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? self::MISSING_ID ) ); $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); @@ -1604,7 +1427,7 @@ public function test_parent_equal_to_the_stored_parent_passes_with_no_check(): v /** * Returns parents a page may be moved to. * - * @return array The parents, as fixture names, or null for 0. + * @return array The parents, as fixture names, or null for 0. */ public static function data_parents_that_can_be_used(): array { return array( @@ -1638,7 +1461,7 @@ public function test_moves_a_page_to_another_parent( ?string $parent_key ): void /** * Returns attributes that a post type does not support. * - * @return array The cases. + * @return array The cases. */ public static function data_unsupported_attributes(): array { return array( @@ -1675,17 +1498,18 @@ public function test_unsupported_attribute_is_rejected( string $post, string $at /** * Returns attribute values of the wrong type. * - * @return array The cases. + * @return array The cases. */ public static function data_values_of_the_wrong_type(): array { return array( - 'title that is a number' => array( 'title', 123 ), - 'content that is null' => array( 'content', null ), - 'status that is a list' => array( 'status', array( 'draft' ) ), - 'date that is a string' => array( 'date', '2016-12-12T14:00:00' ), - 'date that cannot be parsed' => array( 'date', array( 'site' => 'not a date' ) ), - 'author that is a fraction' => array( 'author', 1.5 ), - 'author that is text' => array( 'author', '1abc' ), + 'title that is a number' => array( 'title', 123 ), + 'content that is null' => array( 'content', null ), + 'status that is a list' => array( 'status', array( 'draft' ) ), + 'date that is a string' => array( 'date', '2016-12-12T14:00:00' ), + 'date that cannot be parsed' => array( 'date', array( 'site' => 'not a date' ) ), + 'date that is not a date-time' => array( 'date', array( 'gmt' => '2010-60-01T02:00:00Z' ) ), + 'author that is a fraction' => array( 'author', 1.5 ), + 'author that is text' => array( 'author', '1abc' ), ); } @@ -1769,7 +1593,7 @@ public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { /** * Returns updates that send a slug for a post that is, or becomes, pending. * - * @return array}> The posts, as fixture names, and the data. + * @return array}> The posts, as fixture names, and the data. */ public static function data_slugs_of_pending_posts(): array { return array( @@ -1827,16 +1651,16 @@ public function test_slug_of_a_pending_post_is_stored_with_publish_posts( string $this->login_as( 'editor' ); $this->register_abilities(); - $post = $this->assert_updated( $this->update_post( $post_id, $data ), $post_id ); + $stored = $this->assert_updated( $this->update_post( $post_id, $data ), $post_id ); - $this->assertSame( 'pending', $post->post_status, 'The post should be pending.' ); - $this->assertSame( 'pending-slug', $post->post_name, 'The slug of the pending post should be stored.' ); + $this->assertSame( 'pending', $stored->post_status, 'The post should be pending.' ); + $this->assertSame( 'pending-slug', $stored->post_name, 'The slug of the pending post should be stored.' ); } /** * Returns posts that take the slug of another post. * - * @return array The cases. + * @return array The cases. */ public static function data_posts_taking_a_used_slug(): array { return array( @@ -1876,7 +1700,7 @@ public function test_slug_of_another_post_is_made_unique( string $post, ?string /** * Returns parents for a draft page that takes the slug of an existing child page. * - * @return array The cases. + * @return array The cases. */ public static function data_draft_page_slugs_by_parent(): array { return array( @@ -1927,28 +1751,63 @@ public function test_draft_page_slug_is_unique_among_its_siblings( string $paren } /** - * An update without a date keeps a floating draft floating. + * Returns updates without a new date of a floating draft, 2099-01-15 10:00 in + * America/New_York, with the status they store. + * + * The date is in winter because PHP versions before 8.1 apply no daylight saving time + * after 2037. + * + * @return array, 1: string}> The data, and the stored status. + */ + public static function data_updates_without_a_new_date_of_a_floating_draft(): array { + return array( + 'title' => array( array( 'title' => 'Still floating' ), 'draft' ), + 'pending' => array( array( 'status' => 'pending' ), 'pending' ), + 'title with the site date it shows' => array( + array( + 'date' => array( 'site' => '2099-01-15T10:00:00' ), + 'title' => 'Same date', + ), + 'draft', + ), + 'title with the GMT date it shows' => array( + array( + 'date' => array( 'gmt' => '2099-01-15T15:00:00' ), + 'title' => 'Same date', + ), + 'draft', + ), + ); + } + + /** + * An update without a date keeps a floating draft floating, also when it moves the draft + * to pending. Sending back the date the draft shows counts as not sent and never pins it. * * @ticket 66706 + * @dataProvider data_updates_without_a_new_date_of_a_floating_draft * @since 7.3.0 + * + * @param array $data The data. + * @param string $status The stored status. */ - public function test_update_without_a_date_keeps_a_draft_floating(): void { + public function test_update_without_a_new_date_keeps_a_draft_floating( array $data, string $status ): void { update_option( 'timezone_string', 'America/New_York' ); $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); $this->login_as( 'editor' ); $this->register_abilities(); - $post = $this->assert_updated( $this->update_post( $post_id, array( 'title' => 'Still floating' ) ), $post_id ); + $post = $this->assert_updated( $this->update_post( $post_id, $data ), $post_id ); - $this->assertSame( 'draft', $post->post_status, 'The post should stay a draft.' ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The draft should keep floating.' ); + $this->assertSame( $status, $post->post_status, "The post should be stored as {$status}." ); + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The post should keep floating.' ); } /** * Returns statuses that do not float, reached from a floating draft without a date. * - * @return array The statuses. + * @return array The statuses. */ public static function data_statuses_that_stamp_the_current_time(): array { return array( @@ -1980,29 +1839,10 @@ public function test_publishing_a_floating_draft_without_a_date_stamps_the_curre $this->assert_is_now( $post->post_date_gmt, 'The GMT date' ); } - /** - * Moving a floating draft to pending without a date keeps it floating. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_pending_without_a_date_keeps_a_draft_floating(): void { - update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $post = $this->assert_updated( $this->update_post( $post_id, array( 'status' => 'pending' ) ), $post_id ); - - $this->assertSame( 'pending', $post->post_status, 'The post should be pending.' ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The pending post should keep floating.' ); - } - /** * Returns dates that differ from the stored ones, in America/New_York. * - * @return array}> The dates, each 2017-01-01 10:00 local. + * @return array}> The dates, each 2017-01-01 10:00 local. */ public static function data_new_dates(): array { return array( @@ -2047,23 +1887,11 @@ public function test_new_date_is_stored_local_and_gmt( array $date ): void { $this->assertSame( '2017-01-01 15:00:00', $post->post_date_gmt, 'The GMT date should be stored.' ); } - /** - * Returns dates for a floating draft that differ from the date it shows. - * - * @return array}> The dates, each 2017-01-01 10:00 local. - */ - public static function data_new_dates_for_a_floating_draft(): array { - return array( - 'site' => array( array( 'site' => '2017-01-01T10:00:00' ) ), - 'gmt' => array( array( 'gmt' => '2017-01-01T15:00:00' ) ), - ); - } - /** * A new date pins a floating draft. * * @ticket 66706 - * @dataProvider data_new_dates_for_a_floating_draft + * @dataProvider data_new_dates * @since 7.3.0 * * @param array $date The sent date. @@ -2082,57 +1910,11 @@ public function test_new_date_pins_a_floating_draft( array $date ): void { $this->assertSame( '2017-01-01 15:00:00', $post->post_date_gmt, 'The draft should no longer float.' ); } - /** - * Returns dates equal to the date a floating draft shows, 2099-01-15 10:00 in America/New_York. - * - * The date is in winter because PHP versions before 8.1 apply no daylight saving time - * after 2037. - * - * @return array}> The dates. - */ - public static function data_dates_equal_to_the_floating_date(): array { - return array( - 'site' => array( array( 'site' => '2099-01-15T10:00:00' ) ), - 'gmt' => array( array( 'gmt' => '2099-01-15T15:00:00' ) ), - ); - } - - /** - * Sending back the date a floating draft shows counts as not sent and never pins it. - * - * @ticket 66706 - * @dataProvider data_dates_equal_to_the_floating_date - * @since 7.3.0 - * - * @param array $date The sent date. - */ - public function test_date_equal_to_the_floating_date_does_not_pin_the_draft( array $date ): void { - update_option( 'timezone_string', 'America/New_York' ); - $post_id = $this->create_floating_draft( '2099-01-15 10:00:00' ); - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $post = $this->assert_updated( - $this->update_post( - $post_id, - array( - 'date' => $date, - 'title' => 'Same date', - ) - ), - $post_id - ); - - $this->assertSame( 'draft', $post->post_status, 'The post should stay a draft.' ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The draft should keep floating.' ); - } - /** * Returns past dates equal to the date a floating draft shows, 2020-01-01 10:00 in * America/New_York. * - * @return array}> The dates. + * @return array}> The dates. */ public static function data_past_dates_equal_to_the_floating_date(): array { return array( @@ -2173,11 +1955,27 @@ public function test_publishing_with_the_past_floating_date_stamps_the_current_t $this->assert_is_now( $post->post_date_gmt, 'The GMT date of the published post' ); } + /** + * Returns dates equal to the stored date of a scheduled post, 2099-01-15 10:00 in + * America/New_York. + * + * The date is in winter because PHP versions before 8.1 apply no daylight saving time + * after 2037. + * + * @return array}> The dates. + */ + public static function data_dates_equal_to_the_stored_date(): array { + return array( + 'site' => array( array( 'site' => '2099-01-15T10:00:00' ) ), + 'gmt' => array( array( 'gmt' => '2099-01-15T15:00:00' ) ), + ); + } + /** * A date equal to the stored date of a post that does not float changes nothing. * * @ticket 66706 - * @dataProvider data_dates_equal_to_the_floating_date + * @dataProvider data_dates_equal_to_the_stored_date * @since 7.3.0 * * @param array $date The sent date. @@ -2206,7 +2004,7 @@ public function test_date_equal_to_the_stored_date_changes_nothing( array $date /** * Returns statuses that do not float, for scheduling a floating draft for the date it shows. * - * @return array}> The cases. + * @return array}> The cases. */ public static function data_schedules_a_floating_draft_for_the_date_it_shows(): array { return array( @@ -2250,26 +2048,6 @@ public function test_scheduling_a_floating_draft_for_the_date_it_shows( string $ $this->assertSame( 'future', $result['status'], 'The scheduled status should be returned.' ); } - /** - * A date that is not a date-time fails input validation. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_date_that_is_not_a_date_time_fails_input_validation(): void { - $post_id = self::$post_ids['administrator:draft']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'date' => array( 'gmt' => '2010-60-01T02:00:00Z' ) ) ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the date.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * The markup of an author is filtered on update. * @@ -2395,7 +2173,7 @@ public function test_raw_fields_are_left_out_when_the_writer_can_no_longer_edit_ $result = $this->update_post( $post_id, array( 'status' => 'publish' ), array( 'title', 'content.raw', 'content.rendered' ) ); $this->assertSame( 'publish', $this->assert_updated( $result, $post_id )->post_status, 'The post should be published.' ); - $this->assertFalse( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should not be able to edit the published post.' ); + $this->assertFalse( current_user_can( 'edit_post', $post_id ), 'The user should not be able to edit the published post.' ); $this->assertSame( array( 'rendered' => 'Publish once' ), $result['title'], 'The title should be returned without its raw value.' ); $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); } @@ -2676,7 +2454,7 @@ private function create_trashed_post(): int { /** * Returns internal statuses that a post can be stored in. * - * @return array The statuses. + * @return array The statuses. */ public static function data_internal_stored_statuses(): array { return array( @@ -2695,7 +2473,7 @@ public static function data_internal_stored_statuses(): array { * * @param string $status The stored status, which is also sent. */ - public function test_direct_call_rejects_trash_for_a_post_in_the_trash( string $status ): void { + public function test_direct_call_rejects_an_internal_status_the_post_already_has( string $status ): void { if ( 'trash' === $status ) { $post_id = $this->create_trashed_post(); } else { @@ -2762,7 +2540,7 @@ public function test_direct_call_rejects_a_date_without_a_usable_value( array $d /** * Returns direct-call inputs whose `data` holds no supported attribute. * - * @return array}> The inputs, without `where`. + * @return array}> The inputs, without `where`. */ public static function data_inputs_without_usable_data(): array { return array( @@ -2888,10 +2666,10 @@ private function create_post_of_the_restricted_author( string $status ): int { /** * Returns date changes on a published or scheduled post, with the status the save stores. * - * @return array The cases. + * @return array The cases. */ public static function data_date_changes_on_a_published_or_scheduled_post(): array { - $ahead = '2036-06-01T12:00:00'; + $ahead = '2099-01-15T12:00:00'; $past = '2021-06-01T12:00:00'; return array( @@ -2943,11 +2721,11 @@ public function test_date_on_a_published_or_scheduled_post_needs_no_publish_post /** * Returns changes between `publish` and `future` that name the new status. * - * @return array The cases. + * @return array The cases. */ public static function data_named_status_changes_on_a_published_or_scheduled_post(): array { return array( - 'published post to future, date ahead' => array( 'publish', 'future', '2036-06-01T12:00:00' ), + 'published post to future, date ahead' => array( 'publish', 'future', '2099-01-15T12:00:00' ), 'scheduled post to publish, date in the past' => array( 'future', 'publish', '2021-06-01T12:00:00' ), ); } @@ -3100,60 +2878,52 @@ private function set_special_pages(): array { } /** - * An editor may not update the privacy policy page. + * Returns the special pages, and whether an editor may update them. * - * @ticket 66706 - * @since 7.3.0 + * @return array The pages, as fixture names, and whether + * the update is allowed. */ - public function test_editor_cannot_update_the_privacy_policy_page(): void { - $post_id = $this->set_special_pages()['privacy_page']; - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'title' => 'Privacy' ) ); - - $this->assert_refused( $result, 'An editor updating the privacy policy page' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Returns the special pages that an editor may update. - * - * @return array The pages, as fixture names. - */ - public static function data_special_pages_an_editor_may_update(): array { + public static function data_special_pages(): array { return array( - 'front page' => array( 'front_page' ), - 'posts page' => array( 'posts_page' ), + 'front page' => array( 'front_page', true ), + 'posts page' => array( 'posts_page', true ), + 'privacy policy page' => array( 'privacy_page', false ), ); } /** - * An editor may update the title of the front page and of the posts page. + * An editor may update the title of the front page and of the posts page, but not the + * privacy policy page. * * @ticket 66706 - * @dataProvider data_special_pages_an_editor_may_update + * @dataProvider data_special_pages * @since 7.3.0 * - * @param string $page The page, as a fixture name. + * @param string $page The page, as a fixture name. + * @param bool $allowed Whether the update is allowed. */ - public function test_editor_may_update_the_title_of_a_special_page( string $page ): void { + public function test_editor_may_update_a_special_page_other_than_the_privacy_policy_page( string $page, bool $allowed ): void { $post_id = $this->set_special_pages()[ $page ]; $this->login_as( 'editor' ); $this->register_abilities(); + $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'title' => 'Special page' ) ); + if ( ! $allowed ) { + $this->assert_refused( $result, 'An editor updating the privacy policy page' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + return; + } + $this->assertSame( 'Special page', $this->assert_updated( $result, $post_id )->post_title, 'The title of the special page should be stored.' ); } /** * Returns updates of a page whose stored parent no longer exists. * - * @return array The cases. + * @return array The cases. */ public static function data_updates_of_a_page_with_a_missing_parent(): array { return array( @@ -3198,39 +2968,6 @@ public function test_page_with_a_missing_parent_can_be_updated( bool $send_paren $this->assertSame( self::MISSING_ID, $post->post_parent, 'The stored parent should not change.' ); } - /** - * Registers a custom public, private and hidden status, before the abilities are registered. - * - * @since 7.3.0 - */ - private function register_custom_statuses(): void { - register_post_status( - 'cw_public', - array( - 'label' => 'Public', - 'public' => true, - ) - ); - register_post_status( - 'cw_private', - array( - 'label' => 'Private', - 'private' => true, - ) - ); - register_post_status( - 'cw_hidden', - array( - 'label' => 'Hidden', - 'public' => false, - 'protected' => true, - ) - ); - - $this->assertFalse( get_post_status_object( 'cw_hidden' )->internal, 'Precondition: the hidden status should not be internal.' ); - $this->assertFalse( is_post_status_viewable( 'cw_hidden' ), 'Precondition: the hidden status should not be publicly viewable.' ); - } - /** * A contributor may set a hidden custom status on their own draft without `publish_posts`. * @@ -3252,7 +2989,7 @@ public function test_contributor_may_set_a_hidden_custom_status(): void { /** * Returns custom statuses that make a post visible, with the stored status of the post. * - * @return array The cases. + * @return array The cases. */ public static function data_custom_visible_statuses_by_stored_status(): array { return array( @@ -3327,7 +3064,7 @@ public function test_output_is_the_id_alone_when_the_update_takes_away_read_acce $this->assertSame( 'pending', $post->post_status, 'The new status should be stored.' ); $this->assertSame( 'Out of reach', $post->post_title, 'The new title should be stored.' ); - $this->assertFalse( current_user_can( 'read_post', $post_id ), 'Precondition: the user should no longer be able to read the post.' ); + $this->assertFalse( current_user_can( 'read_post', $post_id ), 'The user should no longer be able to read the post.' ); $this->assertSame( array( 'id' => $post_id ), $result, 'The output should be the ID alone.' ); } @@ -3354,7 +3091,7 @@ public function test_output_is_complete_when_the_caller_may_still_read_the_post( $result = $this->update_post( $post_id, array( 'title' => 'Still a draft' ), array( 'title.rendered', 'status' ) ); $this->assert_updated( $result, $post_id ); - $this->assertTrue( current_user_can( 'read_post', $post_id ), 'Precondition: the user should still be able to read the post.' ); + $this->assertTrue( current_user_can( 'read_post', $post_id ), 'The user should still be able to read the post.' ); ksort( $result ); diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index 857f506062871..0141942a0ae95 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -564,7 +564,7 @@ public function test_delete_runs_with_delete_and_the_input_in_the_query_string() /** * Returns `permanent` values as they arrive in a query string, and the outcome. * - * @return array The cases. + * @return array The cases. */ public static function data_permanent_query_string_values(): array { return array( @@ -618,7 +618,7 @@ public function test_delete_reads_permanent_from_the_query_string( string $perma /** * Returns `permanent` values in a query string that are not booleans. * - * @return array The values. + * @return array The values. */ public static function data_permanent_query_string_values_that_are_not_booleans(): array { return array( @@ -721,7 +721,7 @@ public function test_delete_with_the_input_in_a_json_body_returns_400(): void { /** * Returns write abilities with a method they do not take. * - * @return array The cases. + * @return array The cases. */ public static function data_wrong_methods_for_write_abilities(): array { return array( @@ -771,35 +771,36 @@ public function test_write_ability_with_a_wrong_method_returns_405( string $abil } /** - * A logged-out user is refused as unauthenticated. + * Returns users that may not create content, and the status of the response they receive. * - * @ticket 66706 - * @since 7.3.0 + * @return array The cases. */ - public function test_logged_out_create_receives_401(): void { - wp_set_current_user( 0 ); - - $before = $this->snapshot(); - $response = $this->server->dispatch( $this->write_request( 'POST', 'core/content-create', array( 'data' => array( 'type' => 'post' ) ) ) ); - - $this->assert_error_response( $response, 401, 'rest_ability_cannot_execute', 'A logged-out create' ); - $this->assertSame( $before, $this->snapshot(), 'A logged-out create should write nothing.' ); + public static function data_users_refused_a_create(): array { + return array( + 'logged out' => array( 'logged-out', 401 ), + 'subscriber' => array( 'subscriber', 403 ), + ); } /** - * A user the permission callback refuses is forbidden. + * A logged-out user is refused as unauthenticated, and a user the permission callback refuses + * is forbidden. * * @ticket 66706 + * @dataProvider data_users_refused_a_create * @since 7.3.0 + * + * @param string $user The user, `logged-out` or `subscriber`. + * @param int $status The expected status. */ - public function test_subscriber_create_receives_403(): void { - wp_set_current_user( self::$subscriber_id ); + public function test_refused_create_receives_401_or_403( string $user, int $status ): void { + wp_set_current_user( 'subscriber' === $user ? self::$subscriber_id : 0 ); $before = $this->snapshot(); $response = $this->server->dispatch( $this->write_request( 'POST', 'core/content-create', array( 'data' => array( 'type' => 'post' ) ) ) ); - $this->assert_error_response( $response, 403, 'rest_ability_cannot_execute', 'A subscriber create' ); - $this->assertSame( $before, $this->snapshot(), 'A subscriber create should write nothing.' ); + $this->assert_error_response( $response, $status, 'rest_ability_cannot_execute', "A {$user} create" ); + $this->assertSame( $before, $this->snapshot(), "A {$user} create should write nothing." ); } /** From 04bd5c47cd9741d9f118bad575d2bc97878e024d Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 17:11:43 +0000 Subject: [PATCH 08/24] Tests: Give the pages in the posts list table search test a stable order. `Tests_Admin_wpPostsListTable::test_search_hierarchical_pages_first_page` expects the child pages of the first top-level page. The child pages of different parents share a title, and the test query orders by `menu_order` and title only, so which of them comes first is up to the database. On MariaDB and MySQL 5.7 that order depends on the page IDs, which in turn depend on how many posts the earlier tests created. With the tests added on this branch, the test fails on those databases. The test query now also orders by ID. See #66706. --- tests/phpunit/tests/admin/wpPostsListTable.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/admin/wpPostsListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php index 50fd032c86265..8bb103dd3cdc5 100644 --- a/tests/phpunit/tests/admin/wpPostsListTable.php +++ b/tests/phpunit/tests/admin/wpPostsListTable.php @@ -270,9 +270,13 @@ protected function _test_list_hierarchical_page( array $args, array $expected_id $args ); - // Mimic the behavior of `wp_edit_posts_query()`: + /* + * Mimic the behavior of `wp_edit_posts_query()`. The ID is added to the order because + * pages of different parents share a title, and the order of pages with the same + * title is not the same on every database. + */ if ( ! isset( $args['orderby'] ) ) { - $args['orderby'] = 'menu_order title'; + $args['orderby'] = 'menu_order title ID'; $args['order'] = 'asc'; $args['posts_per_page'] = -1; $args['posts_per_archive_page'] = -1; From 6a627d4cadc431f3374414a5530e9d53d32edf37 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 17:20:07 +0000 Subject: [PATCH 09/24] Abilities API: Clarify comments in the content write abilities. Documents that a status sent for a post in the Trash moves it out of the Trash through a regular save, as an update through the REST API posts endpoint does, and corrects which requests the update and delete permission callback lets through. See #66706. --- .../abilities/class-wp-abilities-content.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 747811dd97820..0b10c03c694ad 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -352,7 +352,8 @@ private function check_create_permission( $input ): bool { * Permission callback for the `core/content-update` and `core/content-delete` abilities. * * Requires a logged-in user, a `where.type` that is exposed to abilities, and the given - * capability on the post that `where` names. A post that does not exist is let through, + * capability on the post that `where` names. When `where` names no post of that type, + * such as a missing ID or the ID of a post of another type, the request is let through * so that the execute callback can report it as not found. The execute callbacks check * the capability again and make every other decision. * @@ -944,6 +945,15 @@ private function execute_content_update( array $input ) { return $this->invalid_attribute_error( 'data', __( 'The data must be an object with at least one attribute to write.' ) ); } + /* + * Known issue: a `data.status` sent for a post in the Trash moves it out of the Trash + * through a regular save, not through wp_untrash_post(). The `_wp_trash_meta_status` + * and `_wp_trash_meta_time` meta and the comments trashed with the post are left + * behind. The REST API posts endpoint has the same issue when an update sends a + * status for a trashed post. The cause is in the save path, so the fix belongs there, + * where it covers every caller of wp_update_post(). + */ + // get_requested_post() only returns posts of an exposed post type. $postarr = $this->prepare_post_data( $data, get_post_type_object( $post->post_type ), $post ); if ( is_wp_error( $postarr ) ) { From 297370a15077efa37b04e03ac015f4dcfaaa0290 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 17:55:50 +0000 Subject: [PATCH 10/24] Revert "Tests: Give the pages in the posts list table search test a stable order." This reverts commit 04bd5c47cd9741d9f118bad575d2bc97878e024d. --- tests/phpunit/tests/admin/wpPostsListTable.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/admin/wpPostsListTable.php b/tests/phpunit/tests/admin/wpPostsListTable.php index 8bb103dd3cdc5..50fd032c86265 100644 --- a/tests/phpunit/tests/admin/wpPostsListTable.php +++ b/tests/phpunit/tests/admin/wpPostsListTable.php @@ -270,13 +270,9 @@ protected function _test_list_hierarchical_page( array $args, array $expected_id $args ); - /* - * Mimic the behavior of `wp_edit_posts_query()`. The ID is added to the order because - * pages of different parents share a title, and the order of pages with the same - * title is not the same on every database. - */ + // Mimic the behavior of `wp_edit_posts_query()`: if ( ! isset( $args['orderby'] ) ) { - $args['orderby'] = 'menu_order title ID'; + $args['orderby'] = 'menu_order title'; $args['order'] = 'asc'; $args['posts_per_page'] = -1; $args['posts_per_archive_page'] = -1; From 9036d3c077b28ae476f79a2d6be095fa80fdf62e Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 17:59:41 +0000 Subject: [PATCH 11/24] Tests: Run the content query test that creates a high post ID last. `test_execute_callback_rejects_ids_with_no_valid_ids` creates a post with the ID 4194304, and every post created after it gets a higher ID. With the content write tests running after it, the pages of `Tests_Admin_wpPostsListTable::test_search_hierarchical_pages_first_page` got IDs for which MariaDB and MySQL 5.7 return pages with the same title in another order, and that test failed. The test is now the last one of its class, so fewer posts are created after it. The posts list table test is back to its trunk version. See #66706. --- .../wpRegisterCoreContentAbility.php | 143 +++++++++--------- 1 file changed, 73 insertions(+), 70 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index 611748a728bb1..b7394d9474a9b 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -6260,76 +6260,6 @@ public function test_execute_callback_rejects_non_integer_parent_filter(): void ); } - /** - * Returns `where.ids` values that hold no valid ID: no item is a whole number of at least - * 1 within the integer range. - * - * @return array The `where.ids` values. - */ - public static function data_ids_with_no_valid_ids(): array { - return array( - 'zero' => array( - 'ids' => array( 0 ), - ), - 'fraction' => array( - 'ids' => array( 2.5 ), - ), - 'negative number' => array( - 'ids' => array( -5 ), - ), - 'non-numeric string' => array( - 'ids' => 'abc', - ), - 'beyond the integer range' => array( - 'ids' => array( 2 ** 64 + self::ALIASED_POST_ID ), - ), - ); - } - - /** - * An ids filter that parses to no valid IDs is rejected rather than returning an - * unrestricted result set. - * - * WP_Query ignores an empty `post__in`, so an ids list with no valid IDs would otherwise - * return every post of the type — the opposite of the caller's intent. The filter must - * fail closed instead, and an ID beyond the integer range must not wrap around onto the - * published post whose ID it aliases. - * - * @ticket 66268 - * @dataProvider data_ids_with_no_valid_ids - * @since 7.2.0 - * - * @param mixed $ids The `where.ids` value to send. - */ - public function test_execute_callback_rejects_ids_with_no_valid_ids( $ids ): void { - $this->login_as( 'administrator' ); - - self::factory()->post->create( - array( - 'import_id' => self::ALIASED_POST_ID, - 'post_status' => 'publish', - ) - ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'ids' => $ids, - ), - ) - ); - - $this->assertWPError( $result, 'An ids filter with no valid IDs must not fall through to an unrestricted query.' ); - $this->assertSame( 'content_invalid_filter', $result->get_error_code(), 'An empty-after-parsing ids filter should fail closed as an invalid filter.' ); - $this->assertSame( - array( 'where[ids]' => $result->get_error_message() ), - $result->get_error_data()['params'], - 'The error data should map the ids filter to the error message.' - ); - } - /** * Called directly, an item of `where.ids` that is not a valid ID is dropped, while the * other IDs still apply. @@ -7066,4 +6996,77 @@ public function test_trash_returns_only_the_trashed_posts_the_user_may_read( str 'Only the own trashed post should be returned.' ); } + + /** + * Returns `where.ids` values that hold no valid ID: no item is a whole number of at least + * 1 within the integer range. + * + * @return array The `where.ids` values. + */ + public static function data_ids_with_no_valid_ids(): array { + return array( + 'zero' => array( + 'ids' => array( 0 ), + ), + 'fraction' => array( + 'ids' => array( 2.5 ), + ), + 'negative number' => array( + 'ids' => array( -5 ), + ), + 'non-numeric string' => array( + 'ids' => 'abc', + ), + 'beyond the integer range' => array( + 'ids' => array( 2 ** 64 + self::ALIASED_POST_ID ), + ), + ); + } + + /** + * An ids filter that parses to no valid IDs is rejected rather than returning an + * unrestricted result set. + * + * WP_Query ignores an empty `post__in`, so an ids list with no valid IDs would otherwise + * return every post of the type — the opposite of the caller's intent. The filter must + * fail closed instead, and an ID beyond the integer range must not wrap around onto the + * published post whose ID it aliases. + * + * This is the last test of the class: every post created after it gets an ID above + * ALIASED_POST_ID. + * + * @ticket 66268 + * @dataProvider data_ids_with_no_valid_ids + * @since 7.2.0 + * + * @param mixed $ids The `where.ids` value to send. + */ + public function test_execute_callback_rejects_ids_with_no_valid_ids( $ids ): void { + $this->login_as( 'administrator' ); + + self::factory()->post->create( + array( + 'import_id' => self::ALIASED_POST_ID, + 'post_status' => 'publish', + ) + ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $result = $execute( + array( + 'where' => array( + 'type' => 'post', + 'ids' => $ids, + ), + ) + ); + + $this->assertWPError( $result, 'An ids filter with no valid IDs must not fall through to an unrestricted query.' ); + $this->assertSame( 'content_invalid_filter', $result->get_error_code(), 'An empty-after-parsing ids filter should fail closed as an invalid filter.' ); + $this->assertSame( + array( 'where[ids]' => $result->get_error_message() ), + $result->get_error_data()['params'], + 'The error data should map the ids filter to the error message.' + ); + } } From 74a5c3be6c5feb0febe0cc13a1c9283b32db12c5 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 18:15:51 +0000 Subject: [PATCH 12/24] Abilities API: Simplify the content write abilities. Registers the create, update and delete abilities in one loop that adds the shared category, public flag and `readonly` annotation, passes `where` to the post lookup, leaves the default `draft` status of a create to the save, and returns no status from the status preparation when the sent status is unchanged. See #66706. --- .../abilities/class-wp-abilities-content.php | 131 ++++++------------ 1 file changed, 46 insertions(+), 85 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 0b10c03c694ad..5d5073a999845 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -22,17 +22,12 @@ * written post in the same shape as a `core/content-query` item. * * Unlike the core abilities registered directly in wp_register_core_abilities() as - * self-contained closures, the content abilities live in a dedicated class because - * their callbacks and schemas share helpers: the permission and execute callbacks read the - * post type and the filters through the same code, and the input schema, output schema, + * self-contained closures, the four content abilities live in one dedicated class because + * their callbacks and schemas share helpers as private methods, such as the read check, + * the field projection, and the input parsing: the permission and execute callbacks read + * the post type and the filters through the same code, and the input schema, output schema, * and field projection are built from the same field definitions. * - * The write abilities are deliberately kept in the same class as `core/content-query`, so - * that all four share these helpers, such as the read check, the field projection, and the - * input parsing, as private methods. Splitting them into separate classes would require - * making the shared helpers public or moving them to another class, and they are not meant - * to be part of the public API. - * * Only register() is public. The ability callbacks are closures that call private * methods, so callers go through the Abilities API, such as * `wp_get_ability( 'core/content-query' )->execute()`, which validates the input and @@ -132,9 +127,7 @@ public function register(): void { $statuses = array_values( get_post_stati( array( 'internal' => false ) ) ); $this->register_content_query( $post_types, $statuses ); - $this->register_content_create( $post_types, $statuses ); - $this->register_content_update( $post_types, $statuses ); - $this->register_content_delete( $post_types ); + $this->register_content_write_abilities( $post_types, $statuses ); } /** @@ -184,20 +177,18 @@ private function register_content_query( array $post_types, array $statuses ): v } /** - * Registers the `core/content-create` ability. + * Registers the `core/content-create`, `core/content-update`, and `core/content-delete` abilities. * * @since 7.3.0 * * @param list $post_types Exposed post type names. * @param list $statuses Post status slugs that can be set. */ - private function register_content_create( array $post_types, array $statuses ): void { - wp_register_ability( - 'core/content-create', - array( + private function register_content_write_abilities( array $post_types, array $statuses ): void { + $abilities = array( + 'core/content-create' => array( 'label' => __( 'Create Content' ), 'description' => __( 'Creates a post of a post type exposed to abilities and returns it. Requires an authenticated user who can create posts of that type. The post is saved as a draft unless another status is given.' ), - 'category' => self::CATEGORY, 'input_schema' => $this->get_content_create_input_schema( $post_types, $statuses ), 'output_schema' => $this->get_content_output_schema(), 'execute_callback' => function ( $input ) { @@ -206,33 +197,14 @@ private function register_content_create( array $post_types, array $statuses ): 'permission_callback' => function ( $input ): bool { return $this->check_create_permission( $input ); }, - 'meta' => array( - 'annotations' => array( - 'readonly' => false, - 'destructive' => false, - 'idempotent' => false, - ), - 'public' => true, + 'annotations' => array( + 'destructive' => false, + 'idempotent' => false, ), - ) - ); - } - - /** - * Registers the `core/content-update` ability. - * - * @since 7.3.0 - * - * @param list $post_types Exposed post type names. - * @param list $statuses Post status slugs that can be set. - */ - private function register_content_update( array $post_types, array $statuses ): void { - wp_register_ability( - 'core/content-update', - array( + ), + 'core/content-update' => array( 'label' => __( 'Update Content' ), 'description' => __( 'Updates the given attributes of a post of a post type exposed to abilities and returns it. Attributes that are left out keep their stored values. Requires an authenticated user who can edit the post.' ), - 'category' => self::CATEGORY, 'input_schema' => $this->get_content_update_input_schema( $post_types, $statuses ), 'output_schema' => $this->get_content_output_schema(), 'execute_callback' => function ( $input ) { @@ -241,32 +213,14 @@ private function register_content_update( array $post_types, array $statuses ): 'permission_callback' => function ( $input ): bool { return $this->check_post_permission( $input, 'edit_post' ); }, - 'meta' => array( - 'annotations' => array( - 'readonly' => false, - 'destructive' => false, - 'idempotent' => true, - ), - 'public' => true, + 'annotations' => array( + 'destructive' => false, + 'idempotent' => true, ), - ) - ); - } - - /** - * Registers the `core/content-delete` ability. - * - * @since 7.3.0 - * - * @param list $post_types Exposed post type names. - */ - private function register_content_delete( array $post_types ): void { - wp_register_ability( - 'core/content-delete', - array( + ), + 'core/content-delete' => array( 'label' => __( 'Delete Content' ), 'description' => __( 'Moves a post of a post type exposed to abilities to the Trash, or deletes it permanently when `permanent` is true. Requires an authenticated user who can delete the post.' ), - 'category' => self::CATEGORY, 'input_schema' => $this->get_content_delete_input_schema( $post_types ), 'output_schema' => $this->get_content_delete_output_schema(), 'execute_callback' => function ( $input ) { @@ -275,16 +229,24 @@ private function register_content_delete( array $post_types ): void { 'permission_callback' => function ( $input ): bool { return $this->check_post_permission( $input, 'delete_post' ); }, - 'meta' => array( - 'annotations' => array( - 'readonly' => false, - 'destructive' => true, - 'idempotent' => true, - ), - 'public' => true, + 'annotations' => array( + 'destructive' => true, + 'idempotent' => true, ), - ) + ), ); + + // Every write ability shares the category, the public flag, and the `readonly` annotation. + foreach ( $abilities as $name => $args ) { + $args['category'] = self::CATEGORY; + $args['meta'] = array( + 'annotations' => array_merge( array( 'readonly' => false ), $args['annotations'] ), + 'public' => true, + ); + unset( $args['annotations'] ); + + wp_register_ability( $name, $args ); + } } /** @@ -373,7 +335,7 @@ private function check_post_permission( $input, string $capability ): bool { return false; } - $post = $this->get_requested_post( array( 'where' => $where ) ); + $post = $this->get_requested_post( $where ); return ! $post || current_user_can( $capability, $post->ID ); } @@ -922,7 +884,7 @@ private function execute_content_update( array $input ) { // The author visibility memo is per call; see get_visible_author(). $this->visible_authors = array(); - $post = $this->get_requested_post( $input ); + $post = $this->get_requested_post( $input['where'] ?? array() ); if ( ! $post ) { return $this->not_found_error(); } @@ -976,7 +938,7 @@ private function execute_content_update( array $input ) { * @return array|WP_Error The outcome, or a WP_Error on failure. */ private function execute_content_delete( array $input ) { - $post = $this->get_requested_post( $input ); + $post = $this->get_requested_post( $input['where'] ?? array() ); if ( ! $post ) { return $this->not_found_error(); } @@ -990,7 +952,7 @@ private function execute_content_delete( array $input ) { ); } - if ( true === ( $input['permanent'] ?? false ) ) { + if ( ! empty( $input['permanent'] ) ) { $deleted = wp_delete_post( $post->ID, true ); return $deleted && null === get_post( $post->ID ) ? array( 'outcome' => 'deleted' ) : $this->delete_failed_error(); @@ -1030,11 +992,10 @@ private function execute_content_delete( array $input ) { * * @since 7.3.0 * - * @param array $input The ability input. + * @param array $where The `where` input. * @return WP_Post|null The post, or null when `where` names no post of an exposed type. */ - private function get_requested_post( array $input ): ?WP_Post { - $where = $input['where'] ?? array(); + private function get_requested_post( array $where ): ?WP_Post { $post_type_object = $this->get_exposed_post_type( $where['type'] ?? null ); $post_id = $this->parse_filter_int( $where['id'] ?? null, 1 ); if ( ! $post_type_object || null === $post_id ) { @@ -1129,8 +1090,8 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, // A stored page template that no longer exists must not fail an unrelated update. $postarr['page_template'] = null; } else { - $postarr['post_type'] = $post_type; - $postarr['post_status'] = $final_status; + // Without a sent status, the save defaults to `draft`. + $postarr['post_type'] = $post_type; } return $postarr; @@ -1181,7 +1142,7 @@ private function check_supported_attributes( array $data, string $post_type ): ? * @param array $data The `data` input. * @param WP_Post_Type $post_type_object The post type of the post being written. * @param WP_Post|null $post The stored post on update, or null on create. - * @return string|WP_Error|null The status, null when none is sent, or a WP_Error. + * @return string|WP_Error|null The status, null when none is sent or it is unchanged, or a WP_Error. */ private function prepare_status( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ) { if ( ! array_key_exists( 'status', $data ) ) { @@ -1202,7 +1163,7 @@ private function prepare_status( array $data, WP_Post_Type $post_type_object, ?W // Keeping the stored status changes nothing. if ( $post && $post->post_status === $status ) { - return $status; + return null; } /* @@ -1332,7 +1293,7 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post * * @param array $data The `data` input. * @param WP_Post|null $post The stored post on update, or null on create. - * @param string|null $status The sent status, or null when none is sent. + * @param string|null $status The status to set, or null when none is sent or it is unchanged. * @return array|WP_Error The date fields to save, empty for none, or a WP_Error. */ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { From 9cb761586b6bae6574f2c0bc1a4bf0d2e23ec183 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 18:15:51 +0000 Subject: [PATCH 13/24] Tests: Share the duplicated content write ability tests. Moves the tests that the update and delete abilities share, such as the `where` lookup, the permission callback and the role matrix, to a base test case for the abilities that write an existing post, and the registration test to the base of the write abilities. Merges tests that differed only in their input into data providers, removes tests that other tests already cover, and checks the error data in a few more tests. See #66706. --- ...testcase-content-existing-post-ability.php | 393 ++++++++++ .../testcase-content-write-ability.php | 74 +- .../wpRegisterCoreContentAbility.php | 19 +- .../wpRegisterCoreContentCreateAbility.php | 486 ++++--------- .../wpRegisterCoreContentDeleteAbility.php | 576 ++------------- .../wpRegisterCoreContentUpdateAbility.php | 678 ++---------------- .../wpRestAbilitiesContentController.php | 11 +- 7 files changed, 751 insertions(+), 1486 deletions(-) create mode 100644 tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php diff --git a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php new file mode 100644 index 0000000000000..0712540bd1c85 --- /dev/null +++ b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php @@ -0,0 +1,393 @@ + + */ + protected const OTHER_INPUT = array(); + + /** + * Asserts that a result is the 403 error of the capability check in the execute callback. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $context What the call was, for the assertion messages. + */ + abstract protected function assert_cannot_write( $result, string $context ): void; + + /** + * Builds the input that names a post with the given `where`. + * + * @since 7.3.0 + * + * @param mixed $where The `where`. + * @return array The input. + */ + protected function build_input( $where ): array { + return array_merge( array( 'where' => $where ), static::OTHER_INPUT ); + } + + /** + * Builds the input that names a post by its type and ID. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @param string|null $type Optional. The post type, or null for the type of the post. Default null. + * @return array The input. + */ + protected function build_post_input( int $post_id, ?string $type = null ): array { + return $this->build_input( + array( + 'type' => $type ?? get_post_type( $post_id ), + 'id' => $post_id, + ) + ); + } + + /** + * Returns whether each role may edit and delete each matrix post. + * + * @return array The cases. + */ + public static function data_capability_matrix(): array { + $denied = array( + 'author:other' => true, + 'contributor:other' => true, + 'subscriber:own' => true, + 'subscriber:other' => true, + ); + + // A contributor may only write their own posts that were never published. + $denied_to_own_contributor = array( 'publish', 'future', 'trash_publish' ); + + $cases = array(); + foreach ( self::ROLES as $role ) { + foreach ( array( 'own', 'other' ) as $owner ) { + foreach ( self::MATRIX_STATUSES as $status ) { + $allowed = ! isset( $denied[ "{$role}:{$owner}" ] ) + && ! ( 'contributor' === $role && 'own' === $owner && in_array( $status, $denied_to_own_contributor, true ) ); + + $cases[ "{$role}, {$owner}, {$status}" ] = array( $role, 'own' === $owner ? $role : 'other', $status, $allowed ); + } + } + } + + return $cases; + } + + /** + * Returns inputs for which the permission callback returns false, even for an administrator. + * + * Each input but the missing one holds the OTHER_INPUT of the test class. + * + * @return array The inputs. + */ + public static function data_inputs_refused_by_the_permission_callback(): array { + $cases = array( + 'no input' => array( null ), + 'no where' => array( static::OTHER_INPUT ), + ); + + $wheres = array( + 'where that is a string' => 'post', + 'type that is a list' => array( + 'type' => array( 'post' ), + 'id' => 1, + ), + 'unexposed type' => array( + 'type' => 'attachment', + 'id' => 1, + ), + ); + foreach ( $wheres as $label => $where ) { + $cases[ $label ] = array( array_merge( array( 'where' => $where ), static::OTHER_INPUT ) ); + } + + return $cases; + } + + /** + * Returns the special pages of the site. + * + * @return array The pages, as fixture names. + */ + public static function data_special_pages(): array { + return array( + 'front page' => array( 'front_page' ), + 'posts page' => array( 'posts_page' ), + 'privacy policy page' => array( 'privacy_page' ), + ); + } + + /** + * Makes the shared pages the front page, the posts page and the privacy policy page of the site. + * + * @since 7.3.0 + */ + protected function set_special_pages(): void { + update_option( 'show_on_front', 'page' ); + update_option( 'page_on_front', self::$post_ids['front_page'] ); + update_option( 'page_for_posts', self::$post_ids['posts_page'] ); + update_option( 'wp_page_for_privacy_policy', self::$post_ids['privacy_page'] ); + } + + /** + * A logged-out user is refused before the ability runs. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_permission_callback_refuses_a_logged_out_user(): void { + $post_id = self::$post_ids['admin_post']; + + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + + $this->assert_refused( $this->run_ability( $this->build_post_input( $post_id ) ), 'A logged-out user' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * The permission callback returns false for a post the user cannot write, in every form the + * post can be named in input that was not prepared. + * + * @ticket 66706 + * @dataProvider data_unprepared_where_forms + * @since 7.3.0 + * + * @param string $id_type The type to send the ID as. + * @param bool $as_object Whether to send `where` as an object. + */ + public function test_permission_callback_returns_false_for_a_post_the_user_cannot_write( string $id_type, bool $as_object ): void { + $this->login_as( 'contributor' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + $where = $this->build_unprepared_where( self::$post_ids['admin_post'], $id_type, $as_object ); + + $this->assertFalse( $permission( $this->build_input( $where ) ), 'The permission callback should return false.' ); + } + + /** + * The permission callback returns true for a post the user can write. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_permission_callback_returns_true_for_a_post_the_user_can_write(): void { + $this->login_as( 'author' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertTrue( $permission( $this->build_post_input( self::$post_ids['author:draft'] ) ), 'The permission callback should return true.' ); + } + + /** + * The permission callback lets a post that does not exist through, so that the call returns + * the not-found error, also for a user who may write nothing. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_permission_callback_lets_a_missing_post_through(): void { + $this->login_as( 'subscriber' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + $input = $this->build_post_input( self::MISSING_ID, 'post' ); + + $this->assertTrue( $permission( $input ), 'The permission callback should return true.' ); + $this->assert_not_found( $this->run_ability( $input ), 'A missing post' ); + } + + /** + * A permission result that a filter turned into a grant does not skip the capability check + * on the post. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_granted_permission_result_does_not_skip_the_capability_check(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'contributor' ); + $this->register_abilities(); + add_filter( 'wp_ability_permission_result', '__return_true' ); + + $before = $this->snapshot( $post_id ); + + $this->assert_cannot_write( $this->run_ability( $this->build_post_input( $post_id ) ), 'A call granted by a filter' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * An ID that is not a whole number of at least 1 fails input validation. + * + * @ticket 66706 + * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 + * @since 7.3.0 + * + * @param mixed $id The ID. + */ + public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ): void { + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( self::$post_ids['admin_post'] ); + $result = $this->run_ability( + $this->build_input( + array( + 'type' => 'post', + 'id' => $id, + ) + ) + ); + + $this->assertWPError( $result, 'The call should fail.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the ID.' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); + } + + /** + * A revision, an autosave, an attachment, a post of another exposed or non-exposed type and a + * missing post are not found as a `post`, even by an administrator. + * + * @ticket 66706 + * @dataProvider data_posts_that_are_not_posts + * @since 7.3.0 + * + * @param string $post The post, as a fixture name. + */ + public function test_post_of_another_type_is_not_found( string $post ): void { + $post_id = self::$post_ids[ $post ] ?? self::MISSING_ID; + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + + $this->assert_not_found( $this->run_ability( $this->build_post_input( $post_id, 'post' ) ), 'A post that is not a post' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Called directly, a post of a type that is not exposed is not found. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): void { + register_post_type( 'cw_unexposed', array( 'public' => true ) ); + $post_id = self::factory()->post->create( array( 'post_type' => 'cw_unexposed' ) ); + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + + $this->assert_not_found( $execute( $this->build_post_input( $post_id ) ), 'A direct call for a type that is not exposed' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * A post type whose `show_in_abilities` is turned off after registration cannot be written. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_type_no_longer_exposed_cannot_be_written(): void { + $post_id = self::$post_ids['admin_post']; + + $this->login_as( 'administrator' ); + $callbacks = $this->get_ability_callbacks(); + + get_post_type_object( 'post' )->show_in_abilities = false; + + $input = $this->build_post_input( $post_id ); + $before = $this->snapshot( $post_id ); + + $this->assert_refused( $this->run_ability( $input ), 'A regular call for a type that is no longer exposed' ); + $this->assert_not_found( $callbacks['execute_callback']( $input ), 'A direct call for a type that is no longer exposed' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns malformed `where` values for a direct call, or null to send none. + * + * @return array|null}> The values. + */ + public static function data_malformed_where_values(): array { + return array( + 'no where' => array( null ), + 'no type' => array( array( 'id' => 1 ) ), + 'no ID' => array( array( 'type' => 'post' ) ), + ); + } + + /** + * Called directly, a malformed `where` is the not-found error. + * + * @ticket 66706 + * @dataProvider data_malformed_where_values + * @since 7.3.0 + * + * @param array|null $where The `where`, or null to send none. + */ + public function test_direct_call_with_a_malformed_where_is_not_found( ?array $where ): void { + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $input = $this->build_input( $where ); + if ( null === $where ) { + unset( $input['where'] ); + } + + $before = $this->snapshot( self::$post_ids['admin_post'] ); + + $this->assert_not_found( $execute( $input ), 'A direct call with a malformed where' ); + $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); + } + + /** + * The current user is checked at each call, also on the same instance. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_each_call_checks_the_current_user(): void { + $own_id = self::$post_ids['author:draft']; + $other_id = self::$post_ids['author:pending']; + $execute = $this->get_ability_callbacks()['execute_callback']; + + $this->login_as( 'author' ); + $own_before = $this->snapshot( $own_id ); + $first = $execute( $this->build_post_input( $own_id ) ); + + $this->login_as( 'contributor' ); + $other_before = $this->snapshot( $other_id ); + $second = $execute( $this->build_post_input( $other_id ) ); + + $this->assertIsArray( $first, 'The first call as the author should succeed.' ); + $this->assertNotSame( $own_before, $this->snapshot( $own_id ), 'The first call should write the post.' ); + $this->assert_cannot_write( $second, 'The second call as a contributor' ); + $this->assertSame( $other_before, $this->snapshot( $other_id ), 'The second call should write nothing.' ); + } +} diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php index 7b176b1ba1489..cbc88f6ceeca3 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -39,6 +39,33 @@ abstract class WP_Abilities_Content_Write_UnitTestCase extends WP_UnitTestCase { */ protected const MISSING_ID = 987654321; + /** + * A GMT date that stays in the future. + * + * @since 7.3.0 + * + * @var string + */ + protected const FUTURE_GMT = '2099-06-01T12:00:00'; + + /** + * The label of the ability under test, set by each test class. + * + * @since 7.3.0 + * + * @var string + */ + protected const LABEL = ''; + + /** + * The annotations of the ability under test, set by each test class. + * + * @since 7.3.0 + * + * @var array + */ + protected const ANNOTATIONS = array(); + /** * The roles of the matrix tests. * @@ -87,8 +114,8 @@ public static function wpTearDownAfterClass(): void { /** * Creates the users and posts shared by the tests that write an existing post: a user per - * role and an `other` editor, one post per owner and matrix status, and the posts that - * data_posts_that_are_not_posts() names. + * role and an `other` editor, one post per owner and matrix status, the posts that + * data_posts_that_are_not_posts() names, and the pages that data_special_pages() names. * * @since 7.3.0 * @@ -128,13 +155,16 @@ protected static function create_shared_fixtures( WP_UnitTest_Factory $factory ) ) ); - self::$post_ids['admin_page'] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => 'page', - 'post_status' => 'publish', - ) - ); + // A plain page, and the pages that set_special_pages() makes special. + foreach ( array( 'admin_page', 'front_page', 'posts_page', 'privacy_page' ) as $page ) { + self::$post_ids[ $page ] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + } self::$post_ids['attachment'] = $factory->attachment->create_object( 'image.jpg', @@ -333,18 +363,10 @@ protected function register_custom_statuses(): void { 'publicly_queryable' => true, ) ); - register_post_status( - 'cw_protected', - array( - 'label' => 'Protected', - 'protected' => true, - ) - ); register_post_status( 'cw_hidden', array( 'label' => 'Hidden', - 'public' => false, 'protected' => true, ) ); @@ -581,6 +603,24 @@ public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): arra ); } + /** + * The ability is registered in the content category with its label and annotations. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_registers_the_ability(): void { + $this->register_abilities(); + + $ability = wp_get_ability( static::ABILITY ); + + $this->assertInstanceOf( WP_Ability::class, $ability, 'The ability should be registered.' ); + $this->assertSame( static::LABEL, $ability->get_label(), 'The ability should have its label.' ); + $this->assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); + $this->assertTrue( $ability->get_meta_item( 'public', false ), 'The ability should be marked public.' ); + $this->assertSameSetsWithIndex( static::ANNOTATIONS, $ability->get_meta_item( 'annotations', array() ), 'The ability should have its annotations.' ); + } + /** * The ability is not registered when no post type is exposed to abilities. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index b7394d9474a9b..2af073e8733bc 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -3577,8 +3577,8 @@ public static function data_roles_with_edit_access_to_other_users_posts(): array } /** - * Returns the filters that name a trashed post in a query without a status, and roles that - * can edit that post. + * Returns how a query without a status names a trashed post, by ID, by slug or not at all, + * and a role that can edit that post. * * @return array Query test cases. */ @@ -3620,7 +3620,7 @@ public static function data_queries_without_a_status(): array { * @param string $role The role to query as. */ public function test_query_without_a_status_does_not_cover_trashed_posts( ?string $filter, string $role ): void { - $post_id = $this->create_trashed_post( 'author' ); + $post_id = $this->create_trashed_post(); $this->login_as( $role ); $this->register_ability(); @@ -6832,17 +6832,16 @@ public function test_author_filter_names_authors_who_are_not_site_members(): voi } /** - * Creates a post in the Trash. + * Creates a post of the shared author in the Trash. * * @since 7.3.0 * - * @param string $author The author, as a role or fixture name. * @return int The post ID. */ - private function create_trashed_post( string $author ): int { + private function create_trashed_post(): int { $post_id = self::factory()->post->create( array( - 'post_author' => self::$user_ids[ $author ], + 'post_author' => self::$user_ids['author'], 'post_status' => 'publish', ) ); @@ -6884,7 +6883,7 @@ private function query_ids( array $where ): array { * @param string $role The role to query as. */ public function test_trash_requested_by_name_returns_trashed_posts( string $role ): void { - $post_id = $this->create_trashed_post( 'author' ); + $post_id = $this->create_trashed_post(); $this->login_as( $role ); $this->register_ability(); @@ -6908,7 +6907,7 @@ public function test_trash_requested_by_name_returns_trashed_posts( string $role * @since 7.3.0 */ public function test_trash_can_be_requested_next_to_another_status(): void { - $post_id = $this->create_trashed_post( 'author' ); + $post_id = $this->create_trashed_post(); $this->login_as( 'editor' ); $this->register_ability(); @@ -6978,7 +6977,7 @@ public function test_trash_returns_only_the_trashed_posts_the_user_may_read( str 'post_status' => 'draft', ) ); - $other_id = $this->create_trashed_post( 'author' ); + $other_id = $this->create_trashed_post(); wp_trash_post( $own_id ); $this->login_as( $role ); diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index 4a60087c7114f..b5eb83093ef54 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -23,13 +23,26 @@ class Tests_Abilities_API_WpRegisterCoreContentCreateAbility extends WP_Abilitie protected const ABILITY = 'core/content-create'; /** - * A date that stays in the future. + * The label of the ability under test. * * @since 7.3.0 * * @var string */ - private const FUTURE_GMT = '2099-06-01T12:00:00'; + protected const LABEL = 'Create Content'; + + /** + * The annotations of the ability under test. + * + * @since 7.3.0 + * + * @var array + */ + protected const ANNOTATIONS = array( + 'readonly' => false, + 'destructive' => false, + 'idempotent' => false, + ); /** * Creates shared users and posts. @@ -39,73 +52,55 @@ class Tests_Abilities_API_WpRegisterCoreContentCreateAbility extends WP_Abilitie * @param WP_UnitTest_Factory $factory The unit test factory. */ public static function wpSetUpBeforeClass( $factory ): void { - self::$user_ids = array( - 'administrator' => $factory->user->create( array( 'role' => 'administrator' ) ), - 'editor' => $factory->user->create( array( 'role' => 'editor' ) ), - 'author' => $factory->user->create( array( 'role' => 'author' ) ), - 'contributor' => $factory->user->create( array( 'role' => 'contributor' ) ), - 'subscriber' => $factory->user->create( array( 'role' => 'subscriber' ) ), - 'page_writer' => $factory->user->create( array( 'role' => 'subscriber' ) ), - 'publisher' => $factory->user->create( array( 'role' => 'contributor' ) ), - ); + foreach ( self::ROLES as $role ) { + self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); + } // A user who may create pages but not read the drafts of other users. + self::$user_ids['page_writer'] = $factory->user->create( array( 'role' => 'subscriber' ) ); get_userdata( self::$user_ids['page_writer'] )->add_cap( 'edit_pages' ); // A user who may publish posts but not edit published posts. + self::$user_ids['publisher'] = $factory->user->create( array( 'role' => 'contributor' ) ); get_userdata( self::$user_ids['publisher'] )->add_cap( 'publish_posts' ); - self::$post_ids = array( - 'published_post' => $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - 'post_name' => 'shared-slug', - ) - ), - 'published_page' => $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => 'page', - 'post_status' => 'publish', - ) - ), - 'other_parent_page' => $factory->post->create( + self::$post_ids['published_post'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_name' => 'shared-slug', + ) + ); + + foreach ( array( 'published_page', 'other_parent_page', 'trashed_page' ) as $key ) { + self::$post_ids[ $key ] = $factory->post->create( array( 'post_author' => self::$user_ids['administrator'], 'post_type' => 'page', 'post_status' => 'publish', ) - ), - 'editor_draft_page' => $factory->post->create( - array( - 'post_author' => self::$user_ids['editor'], - 'post_type' => 'page', - 'post_status' => 'draft', - ) - ), - ); + ); + } + wp_trash_post( self::$post_ids['trashed_page'] ); - // Read through its `child` slug by data_draft_page_slugs_by_parent(), not by its key. - self::$post_ids['child_page'] = $factory->post->create( + self::$post_ids['editor_draft_page'] = $factory->post->create( array( - 'post_author' => self::$user_ids['administrator'], + 'post_author' => self::$user_ids['editor'], 'post_type' => 'page', - 'post_status' => 'publish', - 'post_parent' => self::$post_ids['published_page'], - 'post_name' => 'child', + 'post_status' => 'draft', ) ); - // A page of the administrator in the Trash. - self::$post_ids['trashed_page'] = $factory->post->create( + // A child page whose `child` slug data_draft_page_slugs_by_parent() reads. + $factory->post->create( array( 'post_author' => self::$user_ids['administrator'], 'post_type' => 'page', 'post_status' => 'publish', + 'post_parent' => self::$post_ids['published_page'], + 'post_name' => 'child', ) ); - wp_trash_post( self::$post_ids['trashed_page'] ); $revisioned_id = $factory->post->create( array( @@ -131,13 +126,16 @@ public static function wpSetUpBeforeClass( $factory ): void { * * @param mixed $result The ability result. * @param string $context Optional. What the call was, for the assertion messages. Default 'The create call'. - * @return WP_Post|null The post with the returned ID, or null when there is none. + * @return WP_Post The post with the returned ID. */ - private function get_created_post( $result, string $context = 'The create call' ): ?WP_Post { + private function get_created_post( $result, string $context = 'The create call' ): WP_Post { $this->assertIsArray( $result, "{$context} should succeed." ); $this->assertArrayHasKey( 'id', $result, "{$context} should return the created post with its ID." ); - return get_post( $result['id'] ); + $post = get_post( $result['id'] ); + $this->assertInstanceOf( WP_Post::class, $post, "{$context} should return the ID of a stored post." ); + + return $post; } /** @@ -159,28 +157,6 @@ private function register_exposed_post_type( string $name, array $supports ): vo ); } - /** - * The ability is registered in the content category with its label and annotations. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_registers_the_ability(): void { - $this->register_abilities(); - - $ability = wp_get_ability( self::ABILITY ); - - $this->assertInstanceOf( WP_Ability::class, $ability, 'The core/content-create ability should be registered.' ); - $this->assertSame( 'Create Content', $ability->get_label(), 'The ability should have its label.' ); - $this->assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); - $this->assertTrue( $ability->get_meta_item( 'public', false ), 'The ability should be marked public.' ); - - $annotations = $ability->get_meta_item( 'annotations', array() ); - $this->assertFalse( $annotations['readonly'], 'The ability should not be marked read-only.' ); - $this->assertFalse( $annotations['destructive'], 'The ability should not be marked destructive.' ); - $this->assertFalse( $annotations['idempotent'], 'The ability should not be marked idempotent.' ); - } - /** * The input schema takes `data`, with the post type and the allowed attributes, and `fields`. * @@ -272,7 +248,7 @@ public function test_output_schema_is_a_query_item(): void { */ public function test_schema_enums_list_the_exposed_post_types_and_registered_statuses(): void { $this->register_exposed_post_type( 'cw_book', array( 'title' ) ); - register_post_type( 'cw_hidden', array( 'public' => true ) ); + register_post_type( 'cw_unexposed', array( 'public' => true ) ); register_post_status( 'cw_archived', array( @@ -512,14 +488,24 @@ public static function data_create_by_role_and_status(): array { 'contributor, publish' => array( 'contributor', 'publish', 'content_cannot_publish', $publish_message ), 'contributor, future' => array( 'contributor', 'future', 'content_cannot_publish', $publish_message ), 'contributor, private' => array( 'contributor', 'private', 'content_cannot_publish', $private_message ), + // A custom status that makes a post visible needs `publish_posts`, and the refusal uses the general message. + 'author, cw_public' => array( 'author', 'cw_public', 'cw_public', null ), + 'author, cw_private' => array( 'author', 'cw_private', 'cw_private', null ), + 'author, cw_viewable' => array( 'author', 'cw_viewable', 'cw_viewable', null ), + 'contributor, cw_public' => array( 'contributor', 'cw_public', 'content_cannot_publish', $publish_message ), + 'contributor, cw_private' => array( 'contributor', 'cw_private', 'content_cannot_publish', $publish_message ), + 'contributor, cw_viewable' => array( 'contributor', 'cw_viewable', 'content_cannot_publish', $publish_message ), + // A custom status that is neither public, private nor publicly viewable needs nothing beyond `create_posts`. + 'contributor, cw_hidden' => array( 'contributor', 'cw_hidden', 'cw_hidden', null ), ); } /** * Each role may create posts in the statuses it may set, as their own author. * - * `future`, `publish` and `private` need `publish_posts`. A scheduled post is sent with a - * date in the future. + * `future`, `publish`, `private` and custom statuses that make a post visible need + * `publish_posts`. A scheduled post is sent with a date in the future, and a draft or a + * pending post sent without a date has a floating date. * * @ticket 66706 * @dataProvider data_create_by_role_and_status @@ -531,6 +517,8 @@ public static function data_create_by_role_and_status(): array { * @param string|null $message The error message, or null on success. */ public function test_create_by_role_and_status( string $role, ?string $status, string $expected, ?string $message ): void { + $this->register_custom_statuses(); + $this->login_as( $role ); $this->register_abilities(); @@ -566,6 +554,9 @@ public function test_create_by_role_and_status( string $role, ?string $status, s $this->assertSame( $expected, $post->post_status, 'The post should be stored in the expected status.' ); $this->assertSame( self::$user_ids[ $role ], (int) $post->post_author, 'The current user should be the author.' ); $this->assertSame( 'post', $post->post_type, 'The post should have the requested type.' ); + if ( in_array( $expected, array( 'draft', 'pending' ), true ) ) { + $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'A draft or a pending post sent without a date should have no GMT date.' ); + } } /** @@ -626,7 +617,7 @@ public static function data_direct_inputs_without_an_exposed_type(): array { 'unexposed type' => array( array( 'data' => array( - 'type' => 'cw_hidden', + 'type' => 'cw_unexposed', 'title' => 'x', ), ), @@ -644,7 +635,7 @@ public static function data_direct_inputs_without_an_exposed_type(): array { * @param array $input The input. */ public function test_direct_call_without_an_exposed_type_is_not_found( array $input ): void { - register_post_type( 'cw_hidden', array( 'public' => true ) ); + register_post_type( 'cw_unexposed', array( 'public' => true ) ); $this->login_as( 'administrator' ); $execute = $this->get_ability_callbacks()['execute_callback']; @@ -663,45 +654,45 @@ public function test_direct_call_without_an_exposed_type_is_not_found( array $in */ public static function data_invalid_data(): array { return array( - 'parent on a post' => array( + 'parent on a post' => array( array( 'type' => 'post', 'parent' => 1, ), ), - 'unregistered status' => array( + 'unregistered status' => array( array( 'type' => 'post', 'status' => 'cw_unregistered', ), ), - 'publish' => array( + 'publish' => array( array( 'type' => 'post', 'status' => 'publish', ), ), - 'empty slug' => array( + 'slug that sanitizes to nothing' => array( array( 'type' => 'post', 'title' => 'x', 'slug' => '!!!', ), ), - 'missing author' => array( + 'missing author' => array( array( 'type' => 'post', 'title' => 'x', 'author' => self::MISSING_ID, ), ), - 'title that is a list' => array( + 'title that is a list' => array( array( 'type' => 'post', 'title' => array( 'x' ), ), ), - 'invalid date' => array( + 'invalid date' => array( array( 'type' => 'post', 'title' => 'x', @@ -734,89 +725,103 @@ public function test_direct_call_without_create_posts_fails_before_the_attribute } /** - * Returns keys that are not part of the input, each with a value. + * Returns `data` values that the input schema rejects: keys outside the allow-list, statuses + * that can never be set and values of the wrong type. * - * @return array The keys and values. - */ - public static function data_keys_outside_the_allow_list(): array { - return array( - 'ID' => array( 'ID', 1 ), - 'post_type' => array( 'post_type', 'page' ), - 'post_password' => array( 'post_password', 'secret' ), - 'meta_input' => array( 'meta_input', array( 'cw_key' => 'value' ) ), - 'tax_input' => array( 'tax_input', array( 'category' => array( 1 ) ) ), - 'comment_status' => array( 'comment_status', 'closed' ), - 'post_author' => array( 'post_author', 1 ), - 'import_id' => array( 'import_id', 7654321 ), - 'guid' => array( 'guid', 'https://example.org/forged' ), - 'page_template' => array( 'page_template', 'cw-template.php' ), - 'modified' => array( 'modified', '2001-01-01T00:00:00' ), - 'post_modified' => array( 'post_modified', '2001-01-01 00:00:00' ), - 'post_modified_gmt' => array( 'post_modified_gmt', '2001-01-01 00:00:00' ), - 'sticky' => array( 'sticky', true ), - 'tags_input' => array( 'tags_input', array( 'cw-tag' ) ), - 'post_category' => array( 'post_category', array( 2 ) ), - 'menu_order' => array( 'menu_order', 5 ), - 'post_mime_type' => array( 'post_mime_type', 'image/jpeg' ), - ); - } - - /** - * A key outside the allow-list fails input validation. - * - * @ticket 66706 - * @dataProvider data_keys_outside_the_allow_list - * @since 7.3.0 - * - * @param string $key The key. - * @param mixed $value The value. + * @return array}> The data. */ - public function test_key_outside_the_allow_list_fails_input_validation( string $key, $value ): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); + public static function data_data_that_fails_input_validation(): array { + $cases = array(); - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( + foreach ( array( + 'ID' => 1, + 'post_type' => 'page', + 'post_password' => 'secret', + 'meta_input' => array( 'cw_key' => 'value' ), + 'tax_input' => array( 'category' => array( 1 ) ), + 'comment_status' => 'closed', + 'post_author' => 1, + 'import_id' => 7654321, + 'guid' => 'https://example.org/forged', + 'page_template' => 'cw-template.php', + 'modified' => '2001-01-01T00:00:00', + 'post_modified' => '2001-01-01 00:00:00', + 'post_modified_gmt' => '2001-01-01 00:00:00', + 'sticky' => true, + 'tags_input' => array( 'cw-tag' ), + 'post_category' => array( 2 ), + 'menu_order' => 5, + 'post_mime_type' => 'image/jpeg', + ) as $key => $value ) { + $cases[ "key outside the allow-list: {$key}" ] = array( + array( 'type' => 'post', - 'title' => 'Extra key', + 'title' => 'Invalid', $key => $value, ), - ) - ); + ); + } - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the key.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + foreach ( self::data_statuses_that_can_never_be_set() as $label => list( $status ) ) { + $cases[ "status that can never be set: {$label}" ] = array( + array( + 'type' => 'post', + 'title' => 'Invalid', + 'status' => $status, + ), + ); + } + + foreach ( array( + 'title that is a number' => array( 'post', 'title', 123 ), + 'title that is a list' => array( 'post', 'title', array( 'x' ) ), + 'title that is null' => array( 'post', 'title', null ), + 'content that is a boolean' => array( 'post', 'content', true ), + 'excerpt that is a number' => array( 'post', 'excerpt', 5 ), + 'status that is a number' => array( 'post', 'status', 1 ), + 'slug that is a list' => array( 'post', 'slug', array( 'slug' ) ), + 'date that is a string' => array( 'post', 'date', '2016-12-12T14:00:00' ), + 'date with a number' => array( 'post', 'date', array( 'site' => 123 ) ), + 'date that cannot be parsed' => array( 'post', 'date', array( 'site' => 'not a date' ) ), + 'GMT date that cannot be parsed' => array( 'post', 'date', array( 'gmt' => 'not a date' ) ), + 'date that is not a date-time' => array( 'post', 'date', array( 'site' => '2010-60-01T02:00:00Z' ) ), + 'author that is text' => array( 'post', 'author', 'abc' ), + 'author 0' => array( 'post', 'author', 0 ), + 'author that is a fraction' => array( 'post', 'author', 1.5 ), + 'parent that is negative' => array( 'page', 'parent', -1 ), + 'parent that is text' => array( 'page', 'parent', 'abc' ), + ) as $label => list( $type, $attribute, $value ) ) { + $data = array( 'type' => $type ); + if ( 'title' !== $attribute ) { + $data['title'] = 'Invalid'; + } + $data[ $attribute ] = $value; + + $cases[ "value of the wrong type: {$label}" ] = array( $data ); + } + + return $cases; } /** - * An internal or unregistered status fails input validation. + * A key outside the allow-list, a status that can never be set and a value of the wrong type + * fail input validation. * * @ticket 66706 - * @dataProvider data_statuses_that_can_never_be_set + * @dataProvider data_data_that_fails_input_validation * @since 7.3.0 * - * @param string $status The status. + * @param array $data The data. */ - public function test_status_that_can_never_be_set_fails_input_validation( string $status ): void { + public function test_data_fails_input_validation( array $data ): void { $this->login_as( 'administrator' ); $this->register_abilities(); $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Status', - 'status' => $status, - ), - ) - ); + $result = $this->run_ability( array( 'data' => $data ) ); $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the status.' ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the data.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -883,96 +888,6 @@ public function test_status_no_longer_registered_is_rejected(): void { $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Returns custom statuses that make a post visible. - * - * @return array The statuses. - */ - public static function data_custom_statuses_that_make_a_post_visible(): array { - return array( - 'public' => array( 'cw_public' ), - 'private' => array( 'cw_private' ), - 'publicly viewable' => array( 'cw_viewable' ), - ); - } - - /** - * A custom status that makes a post visible needs `publish_posts`, and the refusal uses the - * general message, also for a custom private status. - * - * @ticket 66706 - * @dataProvider data_custom_statuses_that_make_a_post_visible - * @since 7.3.0 - * - * @param string $status The status. - */ - public function test_custom_visible_status_needs_publish_posts( string $status ): void { - $this->register_custom_statuses(); - - $this->login_as( 'contributor' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Custom status', - 'status' => $status, - ), - ) - ); - - $this->assert_error( $result, 'content_cannot_publish', 403, 'A custom visible status without publish_posts' ); - $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $result->get_error_message(), 'A custom status, also a private one, should get the general message.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - - /** - * Returns custom statuses and a role that may set them: the role and the status. - * - * @return array The cases. - */ - public static function data_custom_statuses_a_role_may_set(): array { - return array( - 'public with publish_posts' => array( 'author', 'cw_public' ), - 'private with publish_posts' => array( 'author', 'cw_private' ), - 'publicly viewable with publish_posts' => array( 'author', 'cw_viewable' ), - 'hidden without publish_posts' => array( 'contributor', 'cw_protected' ), - ); - } - - /** - * A user with `publish_posts` may set a custom status that makes a post visible, and a custom - * status that is neither public, private nor publicly viewable needs nothing beyond - * `create_posts`. - * - * @ticket 66706 - * @dataProvider data_custom_statuses_a_role_may_set - * @since 7.3.0 - * - * @param string $role The role to create as. - * @param string $status The status. - */ - public function test_custom_status_is_set_by_a_role_that_may_set_it( string $role, string $status ): void { - $this->register_custom_statuses(); - - $this->login_as( $role ); - $this->register_abilities(); - - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Custom status', - 'status' => $status, - ), - ) - ); - - $this->assertSame( $status, $this->get_created_post( $result )->post_status, 'The custom status should be stored.' ); - } - /** * Returns dates that change the stored status of a new post: the requested status, the * requested GMT date and the stored status. @@ -1245,6 +1160,7 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin $result = $create( self::$post_ids[ $parent_key ] ?? self::MISSING_ID ); $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); + $this->assertWPError( $reference, 'Another missing parent should fail.' ); $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for another missing parent.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1353,62 +1269,6 @@ public function test_unsupported_attribute_is_rejected( string $type, string $at $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Returns attribute values of the wrong type: the post type, the attribute and the value. - * - * @return array The cases. - */ - public static function data_values_of_the_wrong_type(): array { - return array( - 'title that is a number' => array( 'post', 'title', 123 ), - 'title that is a list' => array( 'post', 'title', array( 'x' ) ), - 'title that is null' => array( 'post', 'title', null ), - 'content that is a boolean' => array( 'post', 'content', true ), - 'excerpt that is a number' => array( 'post', 'excerpt', 5 ), - 'status that is a number' => array( 'post', 'status', 1 ), - 'slug that is a list' => array( 'post', 'slug', array( 'slug' ) ), - 'date that is a string' => array( 'post', 'date', '2016-12-12T14:00:00' ), - 'date with a number' => array( 'post', 'date', array( 'site' => 123 ) ), - 'date that cannot be parsed' => array( 'post', 'date', array( 'site' => 'not a date' ) ), - 'GMT date that cannot be parsed' => array( 'post', 'date', array( 'gmt' => 'not a date' ) ), - 'date that is not a date-time' => array( 'post', 'date', array( 'site' => '2010-60-01T02:00:00Z' ) ), - 'author that is text' => array( 'post', 'author', 'abc' ), - 'author 0' => array( 'post', 'author', 0 ), - 'author that is a fraction' => array( 'post', 'author', 1.5 ), - 'parent that is negative' => array( 'page', 'parent', -1 ), - 'parent that is text' => array( 'page', 'parent', 'abc' ), - ); - } - - /** - * A value of the wrong type fails input validation. - * - * @ticket 66706 - * @dataProvider data_values_of_the_wrong_type - * @since 7.3.0 - * - * @param string $type The post type. - * @param string $attribute The attribute. - * @param mixed $value The value. - */ - public function test_value_of_the_wrong_type_fails_input_validation( string $type, string $attribute, $value ): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $data = array( 'type' => $type ); - if ( 'title' !== $attribute ) { - $data['title'] = 'Wrong type'; - } - $data[ $attribute ] = $value; - - $before = $this->snapshot(); - $result = $this->run_ability( array( 'data' => $data ) ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the value.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * An empty title is valid next to other content. * @@ -1713,8 +1573,8 @@ public static function data_draft_page_slugs_by_parent(): array { * @dataProvider data_draft_page_slugs_by_parent * @since 7.3.0 * - * @param string $parent_key The parent, as a fixture name. - * @param string $expected The stored slug. + * @param string $parent_key The parent, as a fixture name. + * @param string $expected The stored slug. */ public function test_draft_page_slug_is_unique_among_its_siblings( string $parent_key, string $expected ): void { $this->login_as( 'administrator' ); @@ -1790,48 +1650,6 @@ public function test_date_is_stored_local_and_gmt( array $date, string $local, s $this->assertSame( $gmt, $post->post_date_gmt, 'The GMT date should be stored.' ); } - /** - * Returns posts created without a date: the role, the requested status or null to send none, - * and the stored status. - * - * @return array The cases. - */ - public static function data_posts_without_a_date(): array { - return array( - 'draft of an administrator' => array( 'administrator', null, 'draft' ), - 'pending post of a contributor' => array( 'contributor', 'pending', 'pending' ), - ); - } - - /** - * A draft or a pending post created without a date has a floating date. - * - * @ticket 66706 - * @dataProvider data_posts_without_a_date - * @since 7.3.0 - * - * @param string $role The role to create as. - * @param string|null $status The requested status, or null to send none. - * @param string $expected The stored status. - */ - public function test_post_without_a_date_floats( string $role, ?string $status, string $expected ): void { - $this->login_as( $role ); - $this->register_abilities(); - - $data = array( - 'type' => 'post', - 'title' => 'Floating', - ); - if ( null !== $status ) { - $data['status'] = $status; - } - - $post = $this->get_created_post( $this->run_ability( array( 'data' => $data ) ) ); - - $this->assertSame( $expected, $post->post_status, 'The post should be stored in the expected status.' ); - $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt, 'The post should have no GMT date.' ); - } - /** * Returns markup and the values it is stored and rendered as for a user without * `unfiltered_html`: the role, the sent markup, the stored and the rendered title, and the diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index 81779af7b85af..a8011bf4aacf5 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -2,7 +2,7 @@ declare( strict_types=1 ); -require_once __DIR__ . '/testcase-content-write-ability.php'; +require_once __DIR__ . '/testcase-content-existing-post-ability.php'; /** * Tests for the core/content-delete ability shipped with the Abilities API. @@ -11,7 +11,7 @@ * * @group abilities-api */ -class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_Abilities_Content_Write_UnitTestCase { +class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_Abilities_Content_Existing_Post_UnitTestCase { /** * The name of the ability under test. @@ -22,6 +22,37 @@ class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_Abilitie */ protected const ABILITY = 'core/content-delete'; + /** + * The label of the ability under test. + * + * @since 7.3.0 + * + * @var string + */ + protected const LABEL = 'Delete Content'; + + /** + * The annotations of the ability under test. + * + * @since 7.3.0 + * + * @var array + */ + protected const ANNOTATIONS = array( + 'readonly' => false, + 'destructive' => true, + 'idempotent' => true, + ); + + /** + * The input that the shared tests send next to `where`. + * + * @since 7.3.0 + * + * @var array + */ + protected const OTHER_INPUT = array( 'permanent' => true ); + /** * Creates shared users and posts. * @@ -31,16 +62,6 @@ class Tests_Abilities_API_WpRegisterCoreContentDeleteAbility extends WP_Abilitie */ public static function wpSetUpBeforeClass( $factory ): void { self::create_shared_fixtures( $factory ); - - foreach ( array( 'front_page', 'posts_page', 'privacy_page' ) as $page ) { - self::$post_ids[ $page ] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => 'page', - 'post_status' => 'publish', - ) - ); - } } /** @@ -74,7 +95,7 @@ private function delete_input( int $post_id, ?bool $permanent = null ): array { * @param mixed $result The ability result. * @param string $context What the call was, for the assertion messages. */ - private function assert_cannot_delete( $result, string $context ): void { + protected function assert_cannot_write( $result, string $context ): void { $this->assert_error( $result, 'content_cannot_delete', 403, $context ); $this->assertSame( 'Sorry, you are not allowed to delete this post.', $result->get_error_message(), "{$context}: the message should be the delete message." ); } @@ -113,28 +134,6 @@ private function assert_deleted( $result, int $post_id, string $context ): void $this->assertNull( get_post( $post_id ), "{$context}: the post should no longer exist." ); } - /** - * The ability is registered in the content category with its label and annotations. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_registers_the_ability(): void { - $this->register_abilities(); - - $ability = wp_get_ability( self::ABILITY ); - - $this->assertInstanceOf( WP_Ability::class, $ability, 'The core/content-delete ability should be registered.' ); - $this->assertSame( 'Delete Content', $ability->get_label(), 'The ability should have its label.' ); - $this->assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); - $this->assertTrue( $ability->get_meta_item( 'public', false ), 'The ability should be marked public.' ); - - $annotations = $ability->get_meta_item( 'annotations', array() ); - $this->assertFalse( $annotations['readonly'], 'The ability should not be marked read-only.' ); - $this->assertTrue( $annotations['destructive'], 'The ability should be marked destructive.' ); - $this->assertTrue( $annotations['idempotent'], 'The ability should be marked idempotent.' ); - } - /** * The input schema takes `where`, with the post type and ID, and `permanent`. * @@ -157,8 +156,6 @@ public function test_input_schema(): void { $update_where = wp_get_ability( 'core/content-update' )->get_input_schema()['properties']['where']; $this->assertSame( $this->strip_descriptions( $update_where ), $this->strip_descriptions( $schema['properties']['where'] ), 'The where should be the same as on update.' ); - $this->assertSame( array( 'type', 'id' ), $schema['properties']['where']['required'], 'The where should require the type and the ID.' ); - $this->assertSame( 1, $schema['properties']['where']['properties']['id']['minimum'], 'The ID should be at least 1.' ); } /** @@ -198,421 +195,13 @@ public function test_deletes_a_page_permanently(): void { } /** - * Trashing a post that is already in the Trash is the already-trashed error. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_rejects_trashing_a_trashed_post(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $input = $this->delete_input( $post_id ); - - $this->assert_trashed( $this->run_ability( $input ), $post_id, 'The first call' ); - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $input ); - - $this->assertWPError( $result, 'Trashing the post again should fail.' ); - $this->assertSame( - array( - 'code' => 'content_already_trashed', - 'message' => 'The post has already been trashed.', - 'data' => array( 'status' => 410 ), - ), - array( - 'code' => $result->get_error_code(), - 'message' => $result->get_error_message(), - 'data' => $result->get_error_data(), - ), - 'The error should be the already-trashed error.' - ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * A logged-out user is refused before the ability runs. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_permission_callback_refuses_a_logged_out_user(): void { - $post_id = self::$post_ids['admin_post']; - - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $this->delete_input( $post_id, true ) ); - - $this->assert_refused( $result, 'A logged-out user' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Returns inputs for which the permission callback returns false, even for an administrator. - * - * @return array The inputs. - */ - public static function data_inputs_refused_by_the_permission_callback(): array { - return array( - 'no input' => array( null ), - 'no where' => array( array( 'permanent' => true ) ), - 'where that is a string' => array( array( 'where' => 'post' ) ), - 'type that is a number' => array( - array( - 'where' => array( - 'type' => 1, - 'id' => 1, - ), - ), - ), - 'unexposed type' => array( - array( - 'where' => array( - 'type' => 'revision', - 'id' => 1, - ), - ), - ), - ); - } - - /** - * The permission callback returns false for a post the user cannot delete, in every form the - * post can be named in input that was not prepared. - * - * @ticket 66706 - * @dataProvider data_unprepared_where_forms - * @since 7.3.0 - * - * @param string $id_type The type to send the ID as. - * @param bool $as_object Whether to send `where` as an object. - */ - public function test_permission_callback_returns_false_for_a_post_the_user_cannot_delete( string $id_type, bool $as_object ): void { - $this->login_as( 'contributor' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - $where = $this->build_unprepared_where( self::$post_ids['admin_post'], $id_type, $as_object ); - - $this->assertFalse( $permission( array( 'where' => $where ) ), 'The permission callback should return false.' ); - } - - /** - * The permission callback returns true for a post the user can delete. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_permission_callback_returns_true_for_a_post_the_user_can_delete(): void { - $this->login_as( 'author' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - - $this->assertTrue( $permission( $this->delete_input( self::$post_ids['author:draft'] ) ), 'The permission callback should return true.' ); - } - - /** - * The permission callback lets a post that does not exist through, also for a user who may - * delete nothing. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_permission_callback_lets_a_missing_post_through(): void { - $this->login_as( 'subscriber' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - - $this->assertTrue( - $permission( - array( - 'where' => array( - 'type' => 'post', - 'id' => self::MISSING_ID, - ), - ) - ), - 'The permission callback should return true.' - ); - } - - /** - * A permission result that a filter turned into a grant does not skip the capability check - * on the post. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_granted_permission_result_does_not_skip_the_capability_check(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'contributor' ); - $this->register_abilities(); - add_filter( 'wp_ability_permission_result', '__return_true' ); - - $before = $this->snapshot( $post_id ); - $trash = $this->run_ability( $this->delete_input( $post_id ) ); - $permanent = $this->run_ability( $this->delete_input( $post_id, true ) ); - - $this->assert_cannot_delete( $trash, 'A trash granted by a filter' ); - $this->assert_cannot_delete( $permanent, 'A permanent delete granted by a filter' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * An ID that is not a whole number of at least 1 fails input validation. - * - * @ticket 66706 - * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 - * @since 7.3.0 - * - * @param mixed $id The ID. - */ - public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( self::$post_ids['admin_post'] ); - $result = $this->run_ability( - array( - 'where' => array( - 'type' => 'post', - 'id' => $id, - ), - 'permanent' => true, - ) - ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the ID.' ); - $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); - } - - /** - * A revision, an autosave, an attachment, a post of another exposed or non-exposed type and a - * missing post are not found as a `post`, even by an administrator and even for a permanent - * delete. - * - * @ticket 66706 - * @dataProvider data_posts_that_are_not_posts - * @since 7.3.0 - * - * @param string $post The post, as a fixture name. - */ - public function test_post_of_another_type_is_not_found( string $post ): void { - $post_id = self::$post_ids[ $post ] ?? self::MISSING_ID; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'permanent' => true, - ) - ); - - $this->assert_not_found( $result, 'A post that is not a post' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Called directly, a post of a type that is not exposed is not found, whatever its ID. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): void { - register_post_type( 'cw_hidden', array( 'public' => true ) ); - $post_id = self::factory()->post->create( array( 'post_type' => 'cw_hidden' ) ); - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( $post_id ); - $result = $execute( $this->delete_input( $post_id, true ) ); - - $this->assert_not_found( $result, 'A direct call for a type that is not exposed' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * A post type whose `show_in_abilities` is turned off after registration cannot be deleted. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_type_no_longer_exposed_cannot_be_deleted(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $callbacks = $this->get_ability_callbacks(); - - get_post_type_object( 'post' )->show_in_abilities = false; - - $input = $this->delete_input( $post_id, true ); - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $input ); - - $this->assert_refused( $result, 'A regular call for a type that is no longer exposed' ); - $this->assert_not_found( $callbacks['execute_callback']( $input ), 'A direct call for a type that is no longer exposed' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Returns direct-call inputs with a malformed `where`. - * - * @return array}> The inputs. - */ - public static function data_direct_inputs_with_a_malformed_where(): array { - return array( - 'no where' => array( array( 'permanent' => true ) ), - 'no type' => array( array( 'where' => array( 'id' => 1 ) ) ), - 'no ID' => array( array( 'where' => array( 'type' => 'post' ) ) ), - ); - } - - /** - * Called directly, a malformed `where` is the not-found error. - * - * @ticket 66706 - * @dataProvider data_direct_inputs_with_a_malformed_where - * @since 7.3.0 - * - * @param array $input The input. - */ - public function test_direct_call_with_a_malformed_where_is_not_found( array $input ): void { - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( self::$post_ids['admin_post'] ); - - $this->assert_not_found( $execute( $input ), 'A direct call with a malformed where' ); - $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be deleted for a malformed where.' ); - } - - /** - * The lookup comes before the capability: a user who may delete nothing gets the not-found - * error for a missing post, and is refused for an existing post. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_lookup_comes_before_the_capability(): void { - $this->login_as( 'subscriber' ); - $this->register_abilities(); - - $before = $this->snapshot( self::$post_ids['admin_post'] ); - $missing = $this->run_ability( - array( - 'where' => array( - 'type' => 'post', - 'id' => self::MISSING_ID, - ), - ) - ); - $existing = $this->run_ability( $this->delete_input( self::$post_ids['admin_post'] ) ); - - $this->assert_not_found( $missing, 'A missing post' ); - $this->assert_refused( $existing, 'An existing post the user may not delete' ); - $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'The refused post should still exist, unchanged.' ); - } - - /** - * Returns whether each role may delete each matrix post. - * - * @return array The cases. - */ - public static function data_delete_matrix(): array { - return array( - 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', true ), - 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', true ), - 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', true ), - 'administrator, own, future' => array( 'administrator', 'administrator', 'future', true ), - 'administrator, own, private' => array( 'administrator', 'administrator', 'private', true ), - 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', true ), - 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', true ), - 'administrator, other, draft' => array( 'administrator', 'other', 'draft', true ), - 'administrator, other, pending' => array( 'administrator', 'other', 'pending', true ), - 'administrator, other, publish' => array( 'administrator', 'other', 'publish', true ), - 'administrator, other, future' => array( 'administrator', 'other', 'future', true ), - 'administrator, other, private' => array( 'administrator', 'other', 'private', true ), - 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', true ), - 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', true ), - 'editor, own, draft' => array( 'editor', 'editor', 'draft', true ), - 'editor, own, pending' => array( 'editor', 'editor', 'pending', true ), - 'editor, own, publish' => array( 'editor', 'editor', 'publish', true ), - 'editor, own, future' => array( 'editor', 'editor', 'future', true ), - 'editor, own, private' => array( 'editor', 'editor', 'private', true ), - 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', true ), - 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', true ), - 'editor, other, draft' => array( 'editor', 'other', 'draft', true ), - 'editor, other, pending' => array( 'editor', 'other', 'pending', true ), - 'editor, other, publish' => array( 'editor', 'other', 'publish', true ), - 'editor, other, future' => array( 'editor', 'other', 'future', true ), - 'editor, other, private' => array( 'editor', 'other', 'private', true ), - 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', true ), - 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', true ), - 'author, own, draft' => array( 'author', 'author', 'draft', true ), - 'author, own, pending' => array( 'author', 'author', 'pending', true ), - 'author, own, publish' => array( 'author', 'author', 'publish', true ), - 'author, own, future' => array( 'author', 'author', 'future', true ), - 'author, own, private' => array( 'author', 'author', 'private', true ), - 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', true ), - 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', true ), - 'author, other, draft' => array( 'author', 'other', 'draft', false ), - 'author, other, pending' => array( 'author', 'other', 'pending', false ), - 'author, other, publish' => array( 'author', 'other', 'publish', false ), - 'author, other, future' => array( 'author', 'other', 'future', false ), - 'author, other, private' => array( 'author', 'other', 'private', false ), - 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', false ), - 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', false ), - 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', true ), - 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', true ), - 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', false ), - 'contributor, own, future' => array( 'contributor', 'contributor', 'future', false ), - 'contributor, own, private' => array( 'contributor', 'contributor', 'private', true ), - 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', true ), - 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', false ), - 'contributor, other, draft' => array( 'contributor', 'other', 'draft', false ), - 'contributor, other, pending' => array( 'contributor', 'other', 'pending', false ), - 'contributor, other, publish' => array( 'contributor', 'other', 'publish', false ), - 'contributor, other, future' => array( 'contributor', 'other', 'future', false ), - 'contributor, other, private' => array( 'contributor', 'other', 'private', false ), - 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', false ), - 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', false ), - 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', false ), - 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', false ), - 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', false ), - 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', false ), - 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', false ), - 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', false ), - 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', false ), - 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', false ), - 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', false ), - 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', false ), - 'subscriber, other, future' => array( 'subscriber', 'other', 'future', false ), - 'subscriber, other, private' => array( 'subscriber', 'other', 'private', false ), - 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', false ), - 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', false ), - ); - } - - /** - * Returns the cases of the delete matrix, plus one that sends `permanent` set to false. + * Returns the cases of the capability matrix, plus one that sends `permanent` set to false. * * @return array The cases. */ public static function data_trash_matrix(): array { return array_merge( - self::data_delete_matrix(), + self::data_capability_matrix(), array( 'administrator, own, publish, permanent false' => array( 'administrator', 'administrator', 'publish', true, false ), ) @@ -650,6 +239,7 @@ public function test_trash_matrix( string $role, string $owner, string $status, if ( $may_delete ) { $this->assert_error( $result, 'content_already_trashed', 410, 'Trashing a trashed post the role may delete' ); + $this->assertSame( 'The post has already been trashed.', $result->get_error_message(), 'The message should be the already-trashed message.' ); } else { $this->assert_refused( $result, 'Trashing a post the role may not delete' ); } @@ -661,7 +251,7 @@ public function test_trash_matrix( string $role, string $owner, string $status, * is refused for the others, whatever their status. * * @ticket 66706 - * @dataProvider data_delete_matrix + * @dataProvider data_capability_matrix * @since 7.3.0 * * @param string $role The role to delete as. @@ -716,31 +306,6 @@ public function test_post_without_trash_support_needs_permanent(): void { $this->assert_deleted( $this->run_ability( $this->delete_input( $post_id, true ) ), $post_id, 'Deleting a post without Trash support permanently' ); } - /** - * Returns the special pages of the site. - * - * @return array The pages, as fixture names. - */ - public static function data_special_pages(): array { - return array( - 'front page' => array( 'front_page' ), - 'posts page' => array( 'posts_page' ), - 'privacy policy page' => array( 'privacy_page' ), - ); - } - - /** - * Sets the special pages of the site. - * - * @since 7.3.0 - */ - private function set_special_pages(): void { - update_option( 'show_on_front', 'page' ); - update_option( 'page_on_front', self::$post_ids['front_page'] ); - update_option( 'page_for_posts', self::$post_ids['posts_page'] ); - update_option( 'wp_page_for_privacy_policy', self::$post_ids['privacy_page'] ); - } - /** * An editor may delete neither the front page, the posts page nor the privacy policy page. * @@ -813,20 +378,6 @@ public function test_administrator_can_delete_a_special_page( string $page, bool } } - /** - * Returns the special pages of the site and whether a site administrator may trash them on - * multisite. - * - * @return array The cases. - */ - public static function data_special_pages_for_a_site_administrator(): array { - return array( - 'front page' => array( 'front_page', true ), - 'posts page' => array( 'posts_page', true ), - 'privacy policy page' => array( 'privacy_page', false ), - ); - } - /** * On multisite, a site administrator may trash the front page and the posts page, but not * the privacy policy page. @@ -834,13 +385,12 @@ public static function data_special_pages_for_a_site_administrator(): array { * @group ms-required * * @ticket 66706 - * @dataProvider data_special_pages_for_a_site_administrator + * @dataProvider data_special_pages * @since 7.3.0 * - * @param string $page The page, as a fixture name. - * @param bool $allowed Whether the site administrator may trash it. + * @param string $page The page, as a fixture name. */ - public function test_site_administrator_trashes_special_pages_on_multisite( string $page, bool $allowed ): void { + public function test_site_administrator_trashes_special_pages_on_multisite( string $page ): void { $this->set_special_pages(); $post_id = self::$post_ids[ $page ]; @@ -850,7 +400,7 @@ public function test_site_administrator_trashes_special_pages_on_multisite( stri $before = $this->snapshot( $post_id ); $result = $this->run_ability( $this->delete_input( $post_id ) ); - if ( $allowed ) { + if ( 'privacy_page' !== $page ) { $this->assert_trashed( $result, $post_id, 'A site administrator trashing a special page' ); return; } @@ -886,14 +436,14 @@ public function test_super_admin_can_delete_a_special_page_on_multisite( string /** * Returns values for the pre-trash and pre-delete filters that stop the operation. * - * @return array The cases. + * @return array The cases. */ public static function data_operations_that_are_stopped(): array { return array( - 'trash, false' => array( false, 'pre_trash_post', false ), - 'trash, truthy without trashing' => array( false, 'pre_trash_post', true ), - 'delete, false' => array( true, 'pre_delete_post', false ), - 'delete, truthy without deleting' => array( true, 'pre_delete_post', true ), + 'trash, false' => array( false, false ), + 'trash, truthy without trashing' => array( false, true ), + 'delete, false' => array( true, false ), + 'delete, truthy without deleting' => array( true, true ), ); } @@ -905,15 +455,14 @@ public static function data_operations_that_are_stopped(): array { * @dataProvider data_operations_that_are_stopped * @since 7.3.0 * - * @param bool $permanent Whether the delete is permanent. - * @param string $filter The filter that stops the operation. - * @param mixed $value The value the filter returns. + * @param bool $permanent Whether the delete is permanent. + * @param mixed $value The value that the pre-trash or pre-delete filter returns. */ - public function test_operation_that_does_not_act_is_an_error( bool $permanent, string $filter, $value ): void { + public function test_operation_that_does_not_act_is_an_error( bool $permanent, $value ): void { $post_id = self::$post_ids['admin_post']; add_filter( - $filter, + $permanent ? 'pre_delete_post' : 'pre_trash_post', static function () use ( $value ) { return $value; } @@ -975,27 +524,4 @@ public function test_key_outside_the_allow_list_fails_input_validation( ?string $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the key.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - - /** - * The current user is checked at each call, also on the same instance. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_each_call_checks_the_current_user(): void { - $own_id = self::$post_ids['author:draft']; - $other_id = self::$post_ids['author:pending']; - $execute = $this->get_ability_callbacks()['execute_callback']; - - $this->login_as( 'author' ); - $first = $execute( $this->delete_input( $own_id ) ); - - $this->login_as( 'contributor' ); - $before = $this->snapshot( $other_id ); - $second = $execute( $this->delete_input( $other_id ) ); - - $this->assert_trashed( $first, $own_id, 'The first call as the author' ); - $this->assert_cannot_delete( $second, 'The second call as a contributor' ); - $this->assertSame( $before, $this->snapshot( $other_id ), 'The second call should write nothing.' ); - } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index a8b44262bf347..a1cd9f40387f0 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -2,7 +2,7 @@ declare( strict_types=1 ); -require_once __DIR__ . '/testcase-content-write-ability.php'; +require_once __DIR__ . '/testcase-content-existing-post-ability.php'; /** * Tests for the core/content-update ability shipped with the Abilities API. @@ -11,7 +11,7 @@ * * @group abilities-api */ -class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_Abilities_Content_Write_UnitTestCase { +class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_Abilities_Content_Existing_Post_UnitTestCase { /** * The name of the ability under test. @@ -22,6 +22,37 @@ class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_Abilitie */ protected const ABILITY = 'core/content-update'; + /** + * The label of the ability under test. + * + * @since 7.3.0 + * + * @var string + */ + protected const LABEL = 'Update Content'; + + /** + * The annotations of the ability under test. + * + * @since 7.3.0 + * + * @var array + */ + protected const ANNOTATIONS = array( + 'readonly' => false, + 'destructive' => false, + 'idempotent' => true, + ); + + /** + * The input that the shared tests send next to `where`. + * + * @since 7.3.0 + * + * @var array + */ + protected const OTHER_INPUT = array( 'data' => array( 'title' => 'x' ) ); + /** * Creates shared users and posts. * @@ -142,6 +173,19 @@ private function assert_updated( $result, int $post_id ): WP_Post { return get_post( $post_id ); } + /** + * Asserts that a result is the 403 edit error. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param string $context What the call was, for the assertion messages. + */ + protected function assert_cannot_write( $result, string $context ): void { + $this->assert_error( $result, 'content_cannot_edit', 403, $context ); + $this->assertSame( 'Sorry, you are not allowed to edit this post.', $result->get_error_message(), "{$context}: the message should be the edit message." ); + } + /** * Creates a draft with a floating date, stored in the site's timezone with no GMT date. * @@ -177,28 +221,6 @@ private function assert_is_now( string $gmt, string $context ): void { $this->assertEqualsWithDelta( time(), strtotime( $gmt . ' UTC' ), 60, "{$context} should be the current time." ); } - /** - * The ability is registered in the content category with its label and annotations. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_registers_the_ability(): void { - $this->register_abilities(); - - $ability = wp_get_ability( self::ABILITY ); - - $this->assertInstanceOf( WP_Ability::class, $ability, 'The core/content-update ability should be registered.' ); - $this->assertSame( 'Update Content', $ability->get_label(), 'The ability should have its label.' ); - $this->assertSame( 'content', $ability->get_category(), 'The ability should use the content category.' ); - $this->assertTrue( $ability->get_meta_item( 'public', false ), 'The ability should be marked public.' ); - - $annotations = $ability->get_meta_item( 'annotations', array() ); - $this->assertFalse( $annotations['readonly'], 'The ability should not be marked read-only.' ); - $this->assertFalse( $annotations['destructive'], 'The ability should not be marked destructive.' ); - $this->assertTrue( $annotations['idempotent'], 'The ability should be marked idempotent.' ); - } - /** * The input schema takes `where`, with the post type and ID, `data`, with at least one * attribute, and `fields`. @@ -361,317 +383,6 @@ public function test_publishes_the_draft(): void { ); } - /** - * A permission result that a filter turned into a grant does not skip the capability check - * on the post. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_granted_permission_result_does_not_skip_the_capability_check(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'contributor' ); - $this->register_abilities(); - add_filter( 'wp_ability_permission_result', '__return_true' ); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'title' => 'Granted by a filter' ) ); - - $this->assert_error( $result, 'content_cannot_edit', 403, 'An update granted by a filter' ); - $this->assertSame( 'Sorry, you are not allowed to edit this post.', $result->get_error_message(), 'The message should be the edit message.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * A logged-out user is refused before the ability runs. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_permission_callback_refuses_a_logged_out_user(): void { - $post_id = self::$post_ids['admin_post']; - - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'title' => 'Refused' ) ); - - $this->assert_refused( $result, 'A logged-out user' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Returns inputs for which the permission callback returns false, even for an administrator. - * - * @return array The inputs. - */ - public static function data_inputs_refused_by_the_permission_callback(): array { - return array( - 'no input' => array( null ), - 'no where' => array( array( 'data' => array( 'title' => 'x' ) ) ), - 'where that is a string' => array( - array( - 'where' => 'post', - 'data' => array( 'title' => 'x' ), - ), - ), - 'type that is a list' => array( - array( - 'where' => array( - 'type' => array( 'post' ), - 'id' => 1, - ), - 'data' => array( 'title' => 'x' ), - ), - ), - 'unexposed type' => array( - array( - 'where' => array( - 'type' => 'attachment', - 'id' => 1, - ), - 'data' => array( 'title' => 'x' ), - ), - ), - ); - } - - /** - * The permission callback returns false for a post the user cannot edit, in every form the - * post can be named in input that was not prepared. - * - * @ticket 66706 - * @dataProvider data_unprepared_where_forms - * @since 7.3.0 - * - * @param string $id_type The type to send the ID as. - * @param bool $as_object Whether to send `where` as an object. - */ - public function test_permission_callback_returns_false_for_a_post_the_user_cannot_edit( string $id_type, bool $as_object ): void { - $this->login_as( 'contributor' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - - $this->assertFalse( - $permission( - array( - 'where' => $this->build_unprepared_where( self::$post_ids['admin_post'], $id_type, $as_object ), - 'data' => array( 'title' => 'x' ), - ) - ), - 'The permission callback should return false.' - ); - } - - /** - * The permission callback returns true for a post the user can edit. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_permission_callback_returns_true_for_a_post_the_user_can_edit(): void { - $this->login_as( 'author' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - - $this->assertTrue( - $permission( - array( - 'where' => array( - 'type' => 'post', - 'id' => self::$post_ids['author:draft'], - ), - 'data' => array( 'title' => 'x' ), - ) - ), - 'The permission callback should return true.' - ); - } - - /** - * The permission callback lets a post that does not exist through, so that the call returns - * the not-found error, also for a user who may edit nothing. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_permission_callback_lets_a_missing_post_through(): void { - $this->login_as( 'subscriber' ); - - $permission = $this->get_ability_callbacks()['permission_callback']; - $input = array( - 'where' => array( - 'type' => 'post', - 'id' => self::MISSING_ID, - ), - 'data' => array( 'title' => 'x' ), - ); - - $this->assertTrue( $permission( $input ), 'The permission callback should return true.' ); - $this->assert_not_found( $this->run_ability( $input ), 'A missing post' ); - } - - /** - * An ID that is not a whole number of at least 1 fails input validation. - * - * @ticket 66706 - * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 - * @since 7.3.0 - * - * @param mixed $id The ID. - */ - public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( self::$post_ids['admin_post'] ); - $result = $this->run_ability( - array( - 'where' => array( - 'type' => 'post', - 'id' => $id, - ), - 'data' => array( 'title' => 'Invalid ID' ), - ) - ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the ID.' ); - $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); - } - - /** - * A revision, an autosave, an attachment, a post of another exposed or non-exposed type and a - * missing post are not found as a `post`, even by an administrator. - * - * @ticket 66706 - * @dataProvider data_posts_that_are_not_posts - * @since 7.3.0 - * - * @param string $post The post, as a fixture name. - */ - public function test_post_of_another_type_is_not_found( string $post ): void { - $post_id = self::$post_ids[ $post ] ?? self::MISSING_ID; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'Wrong type' ), - ) - ); - - $this->assert_not_found( $result, 'A post that is not a post' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Called directly, a post of a type that is not exposed is not found, whatever its ID. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_direct_call_for_a_type_that_is_not_exposed_is_not_found(): void { - register_post_type( 'cw_hidden', array( 'public' => true ) ); - $post_id = self::factory()->post->create( array( 'post_type' => 'cw_hidden' ) ); - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'cw_hidden', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'Hidden' ), - ) - ); - - $this->assert_not_found( $result, 'A direct call for a type that is not exposed' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * A post type whose `show_in_abilities` is turned off after registration cannot be updated. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_type_no_longer_exposed_cannot_be_updated(): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $callbacks = $this->get_ability_callbacks(); - - get_post_type_object( 'post' )->show_in_abilities = false; - - $input = array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'Hidden' ), - ); - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $input ); - - $this->assert_refused( $result, 'A regular call for a type that is no longer exposed' ); - $this->assert_not_found( $callbacks['execute_callback']( $input ), 'A direct call for a type that is no longer exposed' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Returns direct-call inputs with a malformed `where`. - * - * @return array}> The inputs. - */ - public static function data_direct_inputs_with_a_malformed_where(): array { - return array( - 'no where' => array( array( 'data' => array( 'title' => 'x' ) ) ), - 'no type' => array( - array( - 'where' => array( 'id' => 1 ), - 'data' => array( 'title' => 'x' ), - ), - ), - 'no ID' => array( - array( - 'where' => array( 'type' => 'post' ), - 'data' => array( 'title' => 'x' ), - ), - ), - ); - } - - /** - * Called directly, a malformed `where` is the not-found error. - * - * @ticket 66706 - * @dataProvider data_direct_inputs_with_a_malformed_where - * @since 7.3.0 - * - * @param array $input The input. - */ - public function test_direct_call_with_a_malformed_where_is_not_found( array $input ): void { - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( self::$post_ids['administrator:draft'] ); - - $this->assert_not_found( $execute( $input ), 'A direct call with a malformed where' ); - $this->assertSame( $before, $this->snapshot( self::$post_ids['administrator:draft'] ), 'Nothing should be written for a malformed where.' ); - } - /** * The lookup comes before everything else: a missing post with invalid data is not found. * @@ -701,92 +412,12 @@ public function test_lookup_comes_before_the_attribute_rules(): void { $this->assertSame( $before, $this->snapshot( self::$post_ids['administrator:draft'] ), 'Nothing should be written for a missing post.' ); } - /** - * Returns whether each role may edit each matrix post. - * - * @return array The cases. - */ - public static function data_update_matrix(): array { - return array( - 'administrator, own, draft' => array( 'administrator', 'administrator', 'draft', true ), - 'administrator, own, pending' => array( 'administrator', 'administrator', 'pending', true ), - 'administrator, own, publish' => array( 'administrator', 'administrator', 'publish', true ), - 'administrator, own, future' => array( 'administrator', 'administrator', 'future', true ), - 'administrator, own, private' => array( 'administrator', 'administrator', 'private', true ), - 'administrator, own, trash_draft' => array( 'administrator', 'administrator', 'trash_draft', true ), - 'administrator, own, trash_publish' => array( 'administrator', 'administrator', 'trash_publish', true ), - 'administrator, other, draft' => array( 'administrator', 'other', 'draft', true ), - 'administrator, other, pending' => array( 'administrator', 'other', 'pending', true ), - 'administrator, other, publish' => array( 'administrator', 'other', 'publish', true ), - 'administrator, other, future' => array( 'administrator', 'other', 'future', true ), - 'administrator, other, private' => array( 'administrator', 'other', 'private', true ), - 'administrator, other, trash_draft' => array( 'administrator', 'other', 'trash_draft', true ), - 'administrator, other, trash_publish' => array( 'administrator', 'other', 'trash_publish', true ), - 'editor, own, draft' => array( 'editor', 'editor', 'draft', true ), - 'editor, own, pending' => array( 'editor', 'editor', 'pending', true ), - 'editor, own, publish' => array( 'editor', 'editor', 'publish', true ), - 'editor, own, future' => array( 'editor', 'editor', 'future', true ), - 'editor, own, private' => array( 'editor', 'editor', 'private', true ), - 'editor, own, trash_draft' => array( 'editor', 'editor', 'trash_draft', true ), - 'editor, own, trash_publish' => array( 'editor', 'editor', 'trash_publish', true ), - 'editor, other, draft' => array( 'editor', 'other', 'draft', true ), - 'editor, other, pending' => array( 'editor', 'other', 'pending', true ), - 'editor, other, publish' => array( 'editor', 'other', 'publish', true ), - 'editor, other, future' => array( 'editor', 'other', 'future', true ), - 'editor, other, private' => array( 'editor', 'other', 'private', true ), - 'editor, other, trash_draft' => array( 'editor', 'other', 'trash_draft', true ), - 'editor, other, trash_publish' => array( 'editor', 'other', 'trash_publish', true ), - 'author, own, draft' => array( 'author', 'author', 'draft', true ), - 'author, own, pending' => array( 'author', 'author', 'pending', true ), - 'author, own, publish' => array( 'author', 'author', 'publish', true ), - 'author, own, future' => array( 'author', 'author', 'future', true ), - 'author, own, private' => array( 'author', 'author', 'private', true ), - 'author, own, trash_draft' => array( 'author', 'author', 'trash_draft', true ), - 'author, own, trash_publish' => array( 'author', 'author', 'trash_publish', true ), - 'author, other, draft' => array( 'author', 'other', 'draft', false ), - 'author, other, pending' => array( 'author', 'other', 'pending', false ), - 'author, other, publish' => array( 'author', 'other', 'publish', false ), - 'author, other, future' => array( 'author', 'other', 'future', false ), - 'author, other, private' => array( 'author', 'other', 'private', false ), - 'author, other, trash_draft' => array( 'author', 'other', 'trash_draft', false ), - 'author, other, trash_publish' => array( 'author', 'other', 'trash_publish', false ), - 'contributor, own, draft' => array( 'contributor', 'contributor', 'draft', true ), - 'contributor, own, pending' => array( 'contributor', 'contributor', 'pending', true ), - 'contributor, own, publish' => array( 'contributor', 'contributor', 'publish', false ), - 'contributor, own, future' => array( 'contributor', 'contributor', 'future', false ), - 'contributor, own, private' => array( 'contributor', 'contributor', 'private', true ), - 'contributor, own, trash_draft' => array( 'contributor', 'contributor', 'trash_draft', true ), - 'contributor, own, trash_publish' => array( 'contributor', 'contributor', 'trash_publish', false ), - 'contributor, other, draft' => array( 'contributor', 'other', 'draft', false ), - 'contributor, other, pending' => array( 'contributor', 'other', 'pending', false ), - 'contributor, other, publish' => array( 'contributor', 'other', 'publish', false ), - 'contributor, other, future' => array( 'contributor', 'other', 'future', false ), - 'contributor, other, private' => array( 'contributor', 'other', 'private', false ), - 'contributor, other, trash_draft' => array( 'contributor', 'other', 'trash_draft', false ), - 'contributor, other, trash_publish' => array( 'contributor', 'other', 'trash_publish', false ), - 'subscriber, own, draft' => array( 'subscriber', 'subscriber', 'draft', false ), - 'subscriber, own, pending' => array( 'subscriber', 'subscriber', 'pending', false ), - 'subscriber, own, publish' => array( 'subscriber', 'subscriber', 'publish', false ), - 'subscriber, own, future' => array( 'subscriber', 'subscriber', 'future', false ), - 'subscriber, own, private' => array( 'subscriber', 'subscriber', 'private', false ), - 'subscriber, own, trash_draft' => array( 'subscriber', 'subscriber', 'trash_draft', false ), - 'subscriber, own, trash_publish' => array( 'subscriber', 'subscriber', 'trash_publish', false ), - 'subscriber, other, draft' => array( 'subscriber', 'other', 'draft', false ), - 'subscriber, other, pending' => array( 'subscriber', 'other', 'pending', false ), - 'subscriber, other, publish' => array( 'subscriber', 'other', 'publish', false ), - 'subscriber, other, future' => array( 'subscriber', 'other', 'future', false ), - 'subscriber, other, private' => array( 'subscriber', 'other', 'private', false ), - 'subscriber, other, trash_draft' => array( 'subscriber', 'other', 'trash_draft', false ), - 'subscriber, other, trash_publish' => array( 'subscriber', 'other', 'trash_publish', false ), - ); - } - /** * Each role may update the posts it may edit, in any status, and is refused for the others, * whatever their status. * * @ticket 66706 - * @dataProvider data_update_matrix + * @dataProvider data_capability_matrix * @since 7.3.0 * * @param string $role The role to update as. @@ -825,14 +456,14 @@ public function test_update_matrix( string $role, string $owner, string $status, */ public static function data_invalid_data(): array { return array( - 'parent on a post' => array( array( 'parent' => 1 ) ), - 'trash' => array( array( 'status' => 'trash' ) ), - 'publish' => array( array( 'status' => 'publish' ) ), - 'empty slug' => array( array( 'slug' => '!!!' ) ), - 'missing author' => array( array( 'author' => self::MISSING_ID ) ), - 'title that is a list' => array( array( 'title' => array( 'x' ) ) ), - 'invalid date' => array( array( 'date' => array( 'site' => 'not a date' ) ) ), - 'empty data' => array( array() ), + 'parent on a post' => array( array( 'parent' => 1 ) ), + 'trash' => array( array( 'status' => 'trash' ) ), + 'private without publish_posts' => array( array( 'status' => 'private' ) ), + 'slug that sanitizes to nothing' => array( array( 'slug' => '!!!' ) ), + 'missing author' => array( array( 'author' => self::MISSING_ID ) ), + 'title that is a list' => array( array( 'title' => array( 'x' ) ) ), + 'invalid date' => array( array( 'date' => array( 'site' => 'not a date' ) ) ), + 'empty data' => array( array() ), ); } @@ -992,7 +623,7 @@ public function test_direct_call_rejects_a_status_that_can_never_be_set( string * Returns status changes that a contributor, and an author denied `publish_posts`, may not * make on their own draft. * - * @return array The cases. + * @return array The cases. */ public static function data_status_changes_without_publish_posts(): array { $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; @@ -1005,22 +636,25 @@ public static function data_status_changes_without_publish_posts(): array { 'publish by an author denied publish_posts' => array( 'restricted_author', 'publish', $publish_message ), 'future by an author denied publish_posts' => array( 'restricted_author', 'future', $publish_message ), 'private by an author denied publish_posts' => array( 'restricted_author', 'private', $private_message ), + 'publish from the Trash by a contributor' => array( 'contributor', 'publish', $publish_message, 'trash_draft' ), ); } /** - * A status that makes a post visible needs `publish_posts`. + * A status that makes a post visible needs `publish_posts`, also to move a post out of the + * Trash. * * @ticket 66706 * @dataProvider data_status_changes_without_publish_posts * @since 7.3.0 * - * @param string $role The role or fixture name of the user, who owns the draft. + * @param string $role The role or fixture name of the user, who owns the post. * @param string $status The status. * @param string $message The error message. + * @param string $stored Optional. The matrix status of the post. Default 'draft'. */ - public function test_status_that_makes_a_post_visible_needs_publish_posts( string $role, string $status, string $message ): void { - $post_id = self::$post_ids[ "{$role}:draft" ]; + public function test_status_that_makes_a_post_visible_needs_publish_posts( string $role, string $status, string $message, string $stored = 'draft' ): void { + $post_id = self::$post_ids[ "{$role}:{$stored}" ]; $this->login_as( $role ); $this->register_abilities(); @@ -1029,7 +663,7 @@ public function test_status_that_makes_a_post_visible_needs_publish_posts( strin $data = array( 'status' => $status ); if ( 'future' === $status ) { - $data['date'] = array( 'gmt' => '2099-06-01T12:00:00' ); + $data['date'] = array( 'gmt' => self::FUTURE_GMT ); } $before = $this->snapshot( $post_id ); @@ -1084,7 +718,7 @@ public function test_schedules_a_draft(): void { $post_id, array( 'status' => 'future', - 'date' => array( 'gmt' => '2099-06-01T12:00:00' ), + 'date' => array( 'gmt' => self::FUTURE_GMT ), ), array( 'status', 'date' ) ); @@ -1144,7 +778,7 @@ public function test_status_change_is_stored( string $role, string $stored, stri */ public static function data_statuses_reported_as_stored(): array { return array( - 'publish with a future date' => array( 'publish', '2099-06-01T12:00:00', 'future' ), + 'publish with a future date' => array( 'publish', self::FUTURE_GMT, 'future' ), 'future with a past date' => array( 'future', '2016-12-12T19:00:00', 'publish' ), ); } @@ -1179,45 +813,6 @@ public function test_output_reports_the_stored_status( string $status, string $g $this->assertSame( $expected, $result['status'], 'The output should report the stored status.' ); } - /** - * A post in the Trash is updated like any other post and stays in the Trash. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_updates_a_post_in_the_trash(): void { - $post_id = self::$post_ids['editor:trash_publish']; - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $result = $this->update_post( $post_id, array( 'title' => 'Trashed and edited' ), array( 'status' ) ); - $post = $this->assert_updated( $result, $post_id ); - - $this->assertSame( 'Trashed and edited', $post->post_title, 'The title should be stored.' ); - $this->assertSame( 'trash', $post->post_status, 'The post should stay in the Trash.' ); - $this->assertSame( 'trash', $result['status'], 'The trash status should be returned.' ); - } - - /** - * Moving a post out of the Trash passes the status gate like any status change. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_publishing_a_post_from_the_trash_needs_publish_posts(): void { - $post_id = self::$post_ids['contributor:trash_draft']; - - $this->login_as( 'contributor' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'status' => 'publish' ) ); - - $this->assert_error( $result, 'content_cannot_publish', 403, 'Publishing a post from the Trash without publish_posts' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * A user without `edit_others_posts` gets the same error for an existing and a missing * author, so the error never tells whether a user exists. @@ -1392,6 +987,7 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin $reference = $this->update_post( $post_id, array( 'parent' => self::MISSING_ID + 1 ) ); $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? self::MISSING_ID ) ); + $this->assertWPError( $reference, 'The reference call with a missing parent should fail.' ); $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for a missing parent.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); @@ -2211,44 +1807,6 @@ public function test_password_protected_post_is_returned_with_its_protection_fla $this->assertSame( 'secret', get_post( $post_id )->post_password, 'The password should be kept.' ); } - /** - * The current user is checked at each call, also on the same instance. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_each_call_checks_the_current_user(): void { - $post_id = self::$post_ids['author:draft']; - $execute = $this->get_ability_callbacks()['execute_callback']; - - $this->login_as( 'author' ); - $first = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'First' ), - ) - ); - - $this->login_as( 'contributor' ); - $before = $this->snapshot( $post_id ); - $second = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'Second' ), - ) - ); - - $this->assertSame( 'First', $this->assert_updated( $first, $post_id )->post_title, 'The first call should update the post.' ); - $this->assert_error( $second, 'content_cannot_edit', 403, 'The second call as a contributor' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'The second call should write nothing.' ); - } - /** * The author visibility is checked again at each call. * @@ -2430,27 +1988,6 @@ static function ( $id ) use ( &$fires ): void { $this->assertCount( 1, array_keys( $fires, $post_id, true ), 'wp_after_insert_post should fire once for the updated post.' ); } - /** - * Creates a published post of the administrator and moves it to the Trash. - * - * @since 7.3.0 - * - * @return int The post ID. - */ - private function create_trashed_post(): int { - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - ) - ); - wp_trash_post( $post_id ); - - $this->assertSame( 'trash', get_post_status( $post_id ), 'Precondition: the post should be in the Trash.' ); - - return $post_id; - } - /** * Returns internal statuses that a post can be stored in. * @@ -2475,7 +2012,7 @@ public static function data_internal_stored_statuses(): array { */ public function test_direct_call_rejects_an_internal_status_the_post_already_has( string $status ): void { if ( 'trash' === $status ) { - $post_id = $this->create_trashed_post(); + $post_id = self::$post_ids['administrator:trash_publish']; } else { $post_id = self::factory()->post->create( array( @@ -2583,6 +2120,7 @@ public function test_direct_call_without_usable_data_is_an_invalid_attribute( ar $result = $execute( $input ); $this->assert_error( $result, 'content_invalid_attribute', 400, 'A direct call without usable data' ); + $this->assertSame( array( 'data' => 'The data must be an object with at least one attribute to write.' ), $result->get_error_data()['params'] ?? array(), 'The params should hold only the data path, with the reason.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -2675,8 +2213,8 @@ public static function data_date_changes_on_a_published_or_scheduled_post(): arr return array( 'published post, date ahead' => array( 'publish', false, $ahead, 'future' ), 'published post, date ahead, stored status' => array( 'publish', true, $ahead, 'future' ), - 'published post, date in the past' => array( 'publish', false, $past, 'publish' ), - 'scheduled post, date in the past' => array( 'future', false, $past, 'publish' ), + 'published post, past date' => array( 'publish', false, $past, 'publish' ), + 'scheduled post, past date' => array( 'future', false, $past, 'publish' ), 'scheduled post, past date, stored status' => array( 'future', true, $past, 'publish' ), 'scheduled post, another date ahead' => array( 'future', false, $ahead, 'future' ), ); @@ -2725,8 +2263,8 @@ public function test_date_on_a_published_or_scheduled_post_needs_no_publish_post */ public static function data_named_status_changes_on_a_published_or_scheduled_post(): array { return array( - 'published post to future, date ahead' => array( 'publish', 'future', '2099-01-15T12:00:00' ), - 'scheduled post to publish, date in the past' => array( 'future', 'publish', '2021-06-01T12:00:00' ), + 'published post to future, date ahead' => array( 'publish', 'future', '2099-01-15T12:00:00' ), + 'scheduled post to publish, past date' => array( 'future', 'publish', '2021-06-01T12:00:00' ), ); } @@ -2833,62 +2371,6 @@ public function test_edit_post_granted_on_one_post_allows_only_editing_that_post $this->assert_error( $publish, 'content_cannot_publish', 403, 'Publishing the granted post' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for the refused publish.' ); - - $this->assertTrue( wp_has_ability( 'core/content-delete' ), 'The core/content-delete ability should be registered.' ); - - $delete = wp_get_ability( 'core/content-delete' )->execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - ) - ); - - $this->assert_refused( $delete, 'Deleting the granted post' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for the refused delete.' ); - } - - /** - * Sets the front page, the posts page and the privacy policy page of the site, and returns - * their IDs. - * - * @since 7.3.0 - * - * @return array The page IDs, keyed by fixture name. - */ - private function set_special_pages(): array { - $pages = array(); - foreach ( array( 'front_page', 'posts_page', 'privacy_page' ) as $page ) { - $pages[ $page ] = self::factory()->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => 'page', - 'post_status' => 'publish', - ) - ); - } - - update_option( 'show_on_front', 'page' ); - update_option( 'page_on_front', $pages['front_page'] ); - update_option( 'page_for_posts', $pages['posts_page'] ); - update_option( 'wp_page_for_privacy_policy', $pages['privacy_page'] ); - - return $pages; - } - - /** - * Returns the special pages, and whether an editor may update them. - * - * @return array The pages, as fixture names, and whether - * the update is allowed. - */ - public static function data_special_pages(): array { - return array( - 'front page' => array( 'front_page', true ), - 'posts page' => array( 'posts_page', true ), - 'privacy policy page' => array( 'privacy_page', false ), - ); } /** @@ -2899,11 +2381,11 @@ public static function data_special_pages(): array { * @dataProvider data_special_pages * @since 7.3.0 * - * @param string $page The page, as a fixture name. - * @param bool $allowed Whether the update is allowed. + * @param string $page The page, as a fixture name. */ - public function test_editor_may_update_a_special_page_other_than_the_privacy_policy_page( string $page, bool $allowed ): void { - $post_id = $this->set_special_pages()[ $page ]; + public function test_editor_may_update_a_special_page_other_than_the_privacy_policy_page( string $page ): void { + $this->set_special_pages(); + $post_id = self::$post_ids[ $page ]; $this->login_as( 'editor' ); $this->register_abilities(); @@ -2911,7 +2393,7 @@ public function test_editor_may_update_a_special_page_other_than_the_privacy_pol $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'title' => 'Special page' ) ); - if ( ! $allowed ) { + if ( 'privacy_page' === $page ) { $this->assert_refused( $result, 'An editor updating the privacy policy page' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); return; @@ -3093,9 +2575,7 @@ public function test_output_is_complete_when_the_caller_may_still_read_the_post( $this->assert_updated( $result, $post_id ); $this->assertTrue( current_user_can( 'read_post', $post_id ), 'The user should still be able to read the post.' ); - ksort( $result ); - - $this->assertSame( + $this->assertSameSetsWithIndex( array( 'id' => $post_id, 'status' => 'draft', diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index 0141942a0ae95..4ff0c9ad9381d 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -40,6 +40,15 @@ class Tests_REST_API_WpRestAbilitiesContentController extends WP_UnitTestCase { */ const RUN_ROUTE = '/wp-abilities/v1/abilities/core/content-query/run'; + /** + * An ID that no post has. + * + * @since 7.3.0 + * + * @var int + */ + private const MISSING_ID = 987654321; + /** * Sets up users and registers the core abilities. * @@ -843,7 +852,7 @@ public function test_update_of_a_missing_post_returns_404(): void { array( 'where' => array( 'type' => 'post', - 'id' => 987654321, + 'id' => self::MISSING_ID, ), 'data' => array( 'title' => 'Missing' ), ) From e7465ce47406bd632608cffd9d70c8c4c15a8fd0 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 19:46:57 +0000 Subject: [PATCH 14/24] Abilities API: Simplify the content write abilities and their tests. Registers each write ability with an explicit category and meta, uses the prepared input directly for the author, parent and date, and checks for data to write with empty(). Links the known Trash issue to its ticket. Merges duplicated tests into data providers, removes tests that could not fail, and moves the members used only by the update and delete tests to the existing post test case. See #66706, #66809. --- .../abilities/class-wp-abilities-content.php | 94 +++--- ...testcase-content-existing-post-ability.php | 266 ++++++++++++++- .../testcase-content-write-ability.php | 177 ---------- .../wpRegisterCoreContentAbility.php | 43 +-- .../wpRegisterCoreContentCreateAbility.php | 259 +++++++-------- .../wpRegisterCoreContentDeleteAbility.php | 32 -- .../wpRegisterCoreContentUpdateAbility.php | 209 +++--------- .../wpRestAbilitiesContentController.php | 309 +++++++----------- 8 files changed, 594 insertions(+), 795 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 5d5073a999845..0ea9269660e43 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -185,10 +185,12 @@ private function register_content_query( array $post_types, array $statuses ): v * @param list $statuses Post status slugs that can be set. */ private function register_content_write_abilities( array $post_types, array $statuses ): void { - $abilities = array( - 'core/content-create' => array( + wp_register_ability( + 'core/content-create', + array( 'label' => __( 'Create Content' ), 'description' => __( 'Creates a post of a post type exposed to abilities and returns it. Requires an authenticated user who can create posts of that type. The post is saved as a draft unless another status is given.' ), + 'category' => self::CATEGORY, 'input_schema' => $this->get_content_create_input_schema( $post_types, $statuses ), 'output_schema' => $this->get_content_output_schema(), 'execute_callback' => function ( $input ) { @@ -197,14 +199,23 @@ private function register_content_write_abilities( array $post_types, array $sta 'permission_callback' => function ( $input ): bool { return $this->check_create_permission( $input ); }, - 'annotations' => array( - 'destructive' => false, - 'idempotent' => false, + 'meta' => array( + 'annotations' => array( + 'readonly' => false, + 'destructive' => false, + 'idempotent' => false, + ), + 'public' => true, ), - ), - 'core/content-update' => array( + ) + ); + + wp_register_ability( + 'core/content-update', + array( 'label' => __( 'Update Content' ), 'description' => __( 'Updates the given attributes of a post of a post type exposed to abilities and returns it. Attributes that are left out keep their stored values. Requires an authenticated user who can edit the post.' ), + 'category' => self::CATEGORY, 'input_schema' => $this->get_content_update_input_schema( $post_types, $statuses ), 'output_schema' => $this->get_content_output_schema(), 'execute_callback' => function ( $input ) { @@ -213,14 +224,23 @@ private function register_content_write_abilities( array $post_types, array $sta 'permission_callback' => function ( $input ): bool { return $this->check_post_permission( $input, 'edit_post' ); }, - 'annotations' => array( - 'destructive' => false, - 'idempotent' => true, + 'meta' => array( + 'annotations' => array( + 'readonly' => false, + 'destructive' => false, + 'idempotent' => true, + ), + 'public' => true, ), - ), - 'core/content-delete' => array( + ) + ); + + wp_register_ability( + 'core/content-delete', + array( 'label' => __( 'Delete Content' ), 'description' => __( 'Moves a post of a post type exposed to abilities to the Trash, or deletes it permanently when `permanent` is true. Requires an authenticated user who can delete the post.' ), + 'category' => self::CATEGORY, 'input_schema' => $this->get_content_delete_input_schema( $post_types ), 'output_schema' => $this->get_content_delete_output_schema(), 'execute_callback' => function ( $input ) { @@ -229,24 +249,16 @@ private function register_content_write_abilities( array $post_types, array $sta 'permission_callback' => function ( $input ): bool { return $this->check_post_permission( $input, 'delete_post' ); }, - 'annotations' => array( - 'destructive' => true, - 'idempotent' => true, + 'meta' => array( + 'annotations' => array( + 'readonly' => false, + 'destructive' => true, + 'idempotent' => true, + ), + 'public' => true, ), - ), + ) ); - - // Every write ability shares the category, the public flag, and the `readonly` annotation. - foreach ( $abilities as $name => $args ) { - $args['category'] = self::CATEGORY; - $args['meta'] = array( - 'annotations' => array_merge( array( 'readonly' => false ), $args['annotations'] ), - 'public' => true, - ); - unset( $args['annotations'] ); - - wp_register_ability( $name, $args ); - } } /** @@ -903,7 +915,7 @@ private function execute_content_update( array $input ) { * rejected instead of saving the post unchanged. */ $data = $input['data'] ?? array(); - if ( empty( array_intersect_key( $data, $this->get_content_write_properties( array() ) ) ) ) { + if ( empty( $data ) ) { return $this->invalid_attribute_error( 'data', __( 'The data must be an object with at least one attribute to write.' ) ); } @@ -914,6 +926,7 @@ private function execute_content_update( array $input ) { * behind. The REST API posts endpoint has the same issue when an update sends a * status for a trashed post. The cause is in the save path, so the fix belongs there, * where it covers every caller of wp_update_post(). + * See https://core.trac.wordpress.org/ticket/66809. */ // get_requested_post() only returns posts of an exposed post type. @@ -1073,6 +1086,7 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, } $postarr = array_merge( $postarr, $dates ); + // Without a sent status, the save defaults to `draft`. $final_status = $postarr['post_status'] ?? $post->post_status ?? 'draft'; $final_parent = $postarr['post_parent'] ?? $post->post_parent ?? 0; @@ -1090,7 +1104,6 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, // A stored page template that no longer exists must not fail an unrelated update. $postarr['page_template'] = null; } else { - // Without a sent status, the save defaults to `draft`. $postarr['post_type'] = $post_type; } @@ -1202,10 +1215,7 @@ private function prepare_author( array $data, WP_Post_Type $post_type_object, ?W return null; } - $author_id = $this->parse_filter_int( $data['author'], 1 ); - if ( null === $author_id ) { - return $this->invalid_attribute_error( 'data[author]', __( 'The author must be a user ID.' ) ); - } + $author_id = $data['author']; // Keeping the stored author changes nothing. if ( $post && (int) $post->post_author === $author_id ) { @@ -1252,10 +1262,7 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post } $reason = __( 'The parent must be 0, or a readable post of the same post type that is neither the post itself nor one of its descendants.' ); - $parent_id = $this->parse_filter_int( $data['parent'], 0 ); - if ( null === $parent_id ) { - return $this->invalid_attribute_error( 'data[parent]', $reason ); - } + $parent_id = $data['parent']; // Keeping the stored parent changes nothing. if ( $post && (int) $post->post_parent === $parent_id ) { @@ -1301,15 +1308,8 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { return array(); } - $date = $data['date']; - if ( isset( $date['site'] ) ) { - $dates = rest_get_date_with_gmt( $date['site'] ); - } elseif ( isset( $date['gmt'] ) ) { - $dates = rest_get_date_with_gmt( $date['gmt'], true ); - } else { - $dates = null; - } - + $date = $data['date']; + $dates = isset( $date['site'] ) ? rest_get_date_with_gmt( $date['site'] ) : rest_get_date_with_gmt( $date['gmt'] ?? '', true ); if ( null === $dates ) { return $this->invalid_attribute_error( 'data[date]', __( 'The date must be an object with a valid `site` or `gmt` date.' ) ); } diff --git a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php index 0712540bd1c85..edbba30337e43 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php @@ -23,6 +23,110 @@ abstract class WP_Abilities_Content_Existing_Post_UnitTestCase extends WP_Abilit */ protected const OTHER_INPUT = array(); + /** + * The statuses of the matrix posts. `trash_` is a post trashed from ``. + * + * @since 7.3.0 + * + * @var list + */ + protected const MATRIX_STATUSES = array( 'draft', 'pending', 'publish', 'future', 'private', 'trash_draft', 'trash_publish' ); + + /** + * Creates the users and posts shared by the tests that write an existing post: a user per + * role and an `other` editor, one post per owner and matrix status, the posts that + * data_posts_that_are_not_posts() names, and the pages that data_special_pages() names. + * + * @since 7.3.0 + * + * @param WP_UnitTest_Factory $factory The unit test factory. + */ + protected static function create_shared_fixtures( WP_UnitTest_Factory $factory ): void { + foreach ( self::ROLES as $role ) { + self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); + } + self::$user_ids['other'] = $factory->user->create( array( 'role' => 'editor' ) ); + + // One post per owner and status, for the matrix tests. + foreach ( array_merge( self::ROLES, array( 'other' ) ) as $owner ) { + foreach ( self::MATRIX_STATUSES as $status ) { + $stored_status = str_replace( 'trash_', '', $status ); + $post_id = $factory->post->create( + array( + 'post_author' => self::$user_ids[ $owner ], + 'post_status' => $stored_status, + 'post_title' => "Matrix {$owner} {$status}", + 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', + ) + ); + if ( $stored_status !== $status ) { + wp_trash_post( $post_id ); + } + self::$post_ids[ "{$owner}:{$status}" ] = $post_id; + } + } + + self::$post_ids['admin_post'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_title' => 'Launch notes', + 'post_name' => 'shared-slug', + ) + ); + + // A plain page, and the pages that set_special_pages() makes special. + foreach ( array( 'admin_page', 'front_page', 'posts_page', 'privacy_page' ) as $page ) { + self::$post_ids[ $page ] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => 'page', + 'post_status' => 'publish', + ) + ); + } + + self::$post_ids['attachment'] = $factory->attachment->create_object( + 'image.jpg', + self::$post_ids['admin_post'], + array( + 'post_mime_type' => 'image/jpeg', + 'post_author' => self::$user_ids['administrator'], + ) + ); + + $revisioned_id = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_status' => 'publish', + 'post_content' => 'First version.', + ) + ); + wp_update_post( + array( + 'ID' => $revisioned_id, + 'post_content' => 'Second version.', + ) + ); + self::$post_ids['revision'] = (int) array_key_first( wp_get_post_revisions( $revisioned_id ) ); + self::$post_ids['autosave'] = (int) _wp_put_post_revision( get_post( $revisioned_id ), true ); + + // Posts of core types that are not exposed. + foreach ( array( + 'reusable_block' => 'wp_block', + 'template' => 'wp_template', + 'menu_item' => 'nav_menu_item', + ) as $fixture => $post_type ) { + self::$post_ids[ $fixture ] = $factory->post->create( + array( + 'post_author' => self::$user_ids['administrator'], + 'post_type' => $post_type, + 'post_status' => 'publish', + ) + ); + } + } + /** * Asserts that a result is the 403 error of the capability check in the execute callback. * @@ -63,6 +167,32 @@ protected function build_post_input( int $post_id, ?string $type = null ): array ); } + /** + * Asserts that input validation rejects an input of an administrator and that nothing is + * written to the given post. + * + * Registers the content abilities unless the test already did. + * + * @since 7.3.0 + * + * @param mixed $input The ability input. + * @param int $post_id The ID of the post that should be left as it was. + * @param string $context What the call was, for the assertion messages. + */ + protected function assert_invalid_input( $input, int $post_id, string $context ): void { + $this->login_as( 'administrator' ); + if ( ! wp_has_ability( static::ABILITY ) ) { + $this->register_abilities(); + } + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, "{$context}: the call should fail." ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), "{$context}: the schema should reject the input." ); + $this->assertSame( $before, $this->snapshot( $post_id ), "{$context}: nothing should be written." ); + } + /** * Returns whether each role may edit and delete each matrix post. * @@ -150,6 +280,79 @@ protected function set_special_pages(): void { update_option( 'wp_page_for_privacy_policy', self::$post_ids['privacy_page'] ); } + /** + * Returns the forms a `where` can take in input that was not prepared. + * + * @return array The type of the ID, and whether `where` is + * an object. + */ + public static function data_unprepared_where_forms(): array { + return array( + 'integer ID' => array( 'integer', false ), + 'integer string ID' => array( 'string', false ), + 'float ID' => array( 'float', false ), + 'where that is an object' => array( 'integer', true ), + ); + } + + /** + * Builds a `where` for a post in one of the forms of unprepared input. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @param string $id_type The type to send the ID as. + * @param bool $as_object Whether to send `where` as an object. + * @return array|stdClass The `where`. + */ + protected function build_unprepared_where( int $post_id, string $id_type, bool $as_object ) { + $id = $post_id; + settype( $id, $id_type ); + + $where = array( + 'type' => get_post_type( $post_id ), + 'id' => $id, + ); + + return $as_object ? (object) $where : $where; + } + + /** + * Returns posts that cannot be reached as a `post`. + * + * @return array The posts, as fixture names. + */ + public static function data_posts_that_are_not_posts(): array { + return array( + 'revision' => array( 'revision' ), + 'attachment' => array( 'attachment' ), + 'page' => array( 'admin_page' ), + 'missing' => array( 'missing' ), + 'reusable block' => array( 'reusable_block' ), + 'template' => array( 'template' ), + 'menu item' => array( 'menu_item' ), + 'autosave' => array( 'autosave' ), + ); + } + + /** + * Returns ID values that are not a whole number of at least 1. + * + * @return array The IDs. + */ + public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): array { + return array( + 'digits and letters' => array( '123abc' ), + 'fraction' => array( 1.5 ), + 'zero' => array( 0 ), + 'negative' => array( -1 ), + 'true' => array( true ), + 'null' => array( null ), + 'list' => array( array( 1 ) ), + 'empty string' => array( '' ), + ); + } + /** * A logged-out user is refused before the ability runs. * @@ -239,7 +442,8 @@ public function test_granted_permission_result_does_not_skip_the_capability_chec } /** - * An ID that is not a whole number of at least 1 fails input validation. + * An ID that is not a whole number of at least 1 fails input validation, and called directly, + * it is the not-found error. * * @ticket 66706 * @dataProvider data_ids_that_are_not_whole_numbers_of_at_least_1 @@ -248,22 +452,56 @@ public function test_granted_permission_result_does_not_skip_the_capability_chec * @param mixed $id The ID. */ public function test_id_that_is_not_a_whole_number_fails_input_validation( $id ): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( self::$post_ids['admin_post'] ); - $result = $this->run_ability( - $this->build_input( - array( - 'type' => 'post', - 'id' => $id, - ) + $post_id = self::$post_ids['admin_post']; + $input = $this->build_input( + array( + 'type' => 'post', + 'id' => $id, ) ); - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the ID.' ); - $this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'Nothing should be written.' ); + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + + $this->assert_not_found( $execute( $input ), 'A direct call with an ID that is not a whole number' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'The direct call should write nothing.' ); + + $this->assert_invalid_input( $input, $post_id, 'An ID that is not a whole number' ); + } + + /** + * Returns keys that are not part of the input, and where they are sent. + * + * @return array Where the key is sent, or + * null for the top level, the + * key, and the value. + */ + abstract public static function data_keys_outside_the_allow_list(): array; + + /** + * A key outside the allow-list fails input validation. + * + * @ticket 66706 + * @dataProvider data_keys_outside_the_allow_list + * @since 7.3.0 + * + * @param string|null $location Where the key is sent, or null for the top level. + * @param string $key The key. + * @param mixed $value The value. + */ + public function test_key_outside_the_allow_list_fails_input_validation( ?string $location, string $key, $value ): void { + $post_id = self::$post_ids['admin_post']; + + $input = $this->build_post_input( $post_id ); + if ( null === $location ) { + $input[ $key ] = $value; + } else { + $input[ $location ][ $key ] = $value; + } + + $this->assert_invalid_input( $input, $post_id, 'A key outside the allow-list' ); } /** diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php index cbc88f6ceeca3..8129a7f375c92 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -75,15 +75,6 @@ abstract class WP_Abilities_Content_Write_UnitTestCase extends WP_UnitTestCase { */ protected const ROLES = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ); - /** - * The statuses of the matrix posts. `trash_` is a post trashed from ``. - * - * @since 7.3.0 - * - * @var list - */ - protected const MATRIX_STATUSES = array( 'draft', 'pending', 'publish', 'future', 'private', 'trash_draft', 'trash_publish' ); - /** * Shared user IDs keyed by role or fixture name. * @@ -112,101 +103,6 @@ public static function wpTearDownAfterClass(): void { self::$post_ids = array(); } - /** - * Creates the users and posts shared by the tests that write an existing post: a user per - * role and an `other` editor, one post per owner and matrix status, the posts that - * data_posts_that_are_not_posts() names, and the pages that data_special_pages() names. - * - * @since 7.3.0 - * - * @param WP_UnitTest_Factory $factory The unit test factory. - */ - protected static function create_shared_fixtures( WP_UnitTest_Factory $factory ): void { - foreach ( self::ROLES as $role ) { - self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); - } - self::$user_ids['other'] = $factory->user->create( array( 'role' => 'editor' ) ); - - // One post per owner and status, for the matrix tests. - foreach ( array_merge( self::ROLES, array( 'other' ) ) as $owner ) { - foreach ( self::MATRIX_STATUSES as $status ) { - $stored_status = str_replace( 'trash_', '', $status ); - $post_id = $factory->post->create( - array( - 'post_author' => self::$user_ids[ $owner ], - 'post_status' => $stored_status, - 'post_title' => "Matrix {$owner} {$status}", - 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', - ) - ); - if ( $stored_status !== $status ) { - wp_trash_post( $post_id ); - } - self::$post_ids[ "{$owner}:{$status}" ] = $post_id; - } - } - - self::$post_ids['admin_post'] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - 'post_title' => 'Launch notes', - 'post_name' => 'shared-slug', - ) - ); - - // A plain page, and the pages that set_special_pages() makes special. - foreach ( array( 'admin_page', 'front_page', 'posts_page', 'privacy_page' ) as $page ) { - self::$post_ids[ $page ] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => 'page', - 'post_status' => 'publish', - ) - ); - } - - self::$post_ids['attachment'] = $factory->attachment->create_object( - 'image.jpg', - self::$post_ids['admin_post'], - array( - 'post_mime_type' => 'image/jpeg', - 'post_author' => self::$user_ids['administrator'], - ) - ); - - $revisioned_id = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_status' => 'publish', - 'post_content' => 'First version.', - ) - ); - wp_update_post( - array( - 'ID' => $revisioned_id, - 'post_content' => 'Second version.', - ) - ); - self::$post_ids['revision'] = (int) array_key_first( wp_get_post_revisions( $revisioned_id ) ); - self::$post_ids['autosave'] = (int) _wp_put_post_revision( get_post( $revisioned_id ), true ); - - // Posts of core types that are not exposed. - foreach ( array( - 'reusable_block' => 'wp_block', - 'template' => 'wp_template', - 'menu_item' => 'nav_menu_item', - ) as $fixture => $post_type ) { - self::$post_ids[ $fixture ] = $factory->post->create( - array( - 'post_author' => self::$user_ids['administrator'], - 'post_type' => $post_type, - 'post_status' => 'publish', - ) - ); - } - } - /** * Unregisters the content abilities and registers the content category. * @@ -485,61 +381,6 @@ protected function strip_descriptions( array $schema ): array { return $schema; } - /** - * Returns the forms a `where` can take in input that was not prepared. - * - * @return array The type of the ID, and whether `where` is - * an object. - */ - public static function data_unprepared_where_forms(): array { - return array( - 'integer ID' => array( 'integer', false ), - 'integer string ID' => array( 'string', false ), - 'float ID' => array( 'float', false ), - 'where that is an object' => array( 'integer', true ), - ); - } - - /** - * Builds a `where` for a post in one of the forms of unprepared input. - * - * @since 7.3.0 - * - * @param int $post_id The post ID. - * @param string $id_type The type to send the ID as. - * @param bool $as_object Whether to send `where` as an object. - * @return array|stdClass The `where`. - */ - protected function build_unprepared_where( int $post_id, string $id_type, bool $as_object ) { - $id = $post_id; - settype( $id, $id_type ); - - $where = array( - 'type' => get_post_type( $post_id ), - 'id' => $id, - ); - - return $as_object ? (object) $where : $where; - } - - /** - * Returns posts that cannot be reached as a `post`. - * - * @return array The posts, as fixture names. - */ - public static function data_posts_that_are_not_posts(): array { - return array( - 'revision' => array( 'revision' ), - 'attachment' => array( 'attachment' ), - 'page' => array( 'admin_page' ), - 'missing' => array( 'missing' ), - 'reusable block' => array( 'reusable_block' ), - 'template' => array( 'template' ), - 'menu item' => array( 'menu_item' ), - 'autosave' => array( 'autosave' ), - ); - } - /** * Returns statuses that can never be set. * @@ -585,24 +426,6 @@ public static function data_dates_without_a_usable_value(): array { ); } - /** - * Returns ID values that are not a whole number of at least 1. - * - * @return array The IDs. - */ - public static function data_ids_that_are_not_whole_numbers_of_at_least_1(): array { - return array( - 'digits and letters' => array( '123abc' ), - 'fraction' => array( 1.5 ), - 'zero' => array( 0 ), - 'negative' => array( -1 ), - 'true' => array( true ), - 'null' => array( null ), - 'list' => array( array( 1 ) ), - 'empty string' => array( '' ), - ); - } - /** * The ability is registered in the content category with its label and annotations. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index 2af073e8733bc..13332448422c7 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -210,7 +210,8 @@ private function register_ability(): void { } /** - * Registers the core/content-query ability and returns the callbacks it was registered with. + * Registers the content abilities and returns the callbacks core/content-query was + * registered with. * * The callbacks call private methods, so tests that skip input validation or the * permission check capture them from the registration arguments. @@ -3577,35 +3578,31 @@ public static function data_roles_with_edit_access_to_other_users_posts(): array } /** - * Returns how a query without a status names a trashed post, by ID, by slug or not at all, - * and a role that can edit that post. + * Returns how a query without a status names a trashed post, by ID or by slug, and a role + * that can edit that post. * - * @return array Query test cases. + * @return array Query test cases. */ public static function data_queries_without_a_status(): array { return array( - 'IDs, administrator' => array( + 'IDs, administrator' => array( 'filter' => 'ids', 'role' => 'administrator', ), - 'IDs, editor' => array( + 'IDs, editor' => array( 'filter' => 'ids', 'role' => 'editor', ), - 'slugs, administrator' => array( + 'slugs, administrator' => array( 'filter' => 'slugs', 'role' => 'administrator', ), - 'no filter, administrator' => array( - 'filter' => null, - 'role' => 'administrator', - ), ); } /** - * A query without a status does not cover the internal `trash` status, even for a role that - * can edit the trashed post, also not when it lists IDs or slugs. + * A query without a status that lists IDs or slugs does not cover the internal `trash` + * status, even for a role that can edit the trashed post. * * The default statuses with IDs or slugs are the statuses that are not internal, so the query * neither returns nor counts the trashed post. @@ -3614,12 +3611,12 @@ public static function data_queries_without_a_status(): array { * @ticket 66706 * @dataProvider data_queries_without_a_status * @since 7.2.0 - * @since 7.3.0 Covers queries by slug and queries without a filter. + * @since 7.3.0 Covers queries by slug. * - * @param string|null $filter The filter that names the trashed post, `ids` or `slugs`, or null for none. - * @param string $role The role to query as. + * @param string $filter The filter that names the trashed post, `ids` or `slugs`. + * @param string $role The role to query as. */ - public function test_query_without_a_status_does_not_cover_trashed_posts( ?string $filter, string $role ): void { + public function test_query_without_a_status_does_not_cover_trashed_posts( string $filter, string $role ): void { $post_id = $this->create_trashed_post(); $this->login_as( $role ); @@ -3630,15 +3627,10 @@ public function test_query_without_a_status_does_not_cover_trashed_posts( ?strin $where = array( 'type' => 'post' ); if ( 'ids' === $filter ) { $where['ids'] = array( $post_id ); - } elseif ( 'slugs' === $filter ) { + } else { $where['slugs'] = array( get_post( $post_id )->post_name ); } - if ( null === $filter ) { - $this->assertNotContains( $post_id, $this->query_ids( $where ), 'A query without a status should not return a trashed post.' ); - return; - } - $result = wp_get_ability( 'core/content-query' )->execute( array( 'where' => $where, @@ -7031,8 +7023,9 @@ public static function data_ids_with_no_valid_ids(): array { * fail closed instead, and an ID beyond the integer range must not wrap around onto the * published post whose ID it aliases. * - * This is the last test of the class: every post created after it gets an ID above - * ALIASED_POST_ID. + * Keep this test last in the class: the post it creates sets the next post ID above + * ALIASED_POST_ID, so the fewer tests run after it, the fewer post IDs later test classes + * see shifted. * * @ticket 66268 * @dataProvider data_ids_with_no_valid_ids diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index b5eb83093ef54..8b8b7708945b1 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -264,7 +264,6 @@ public function test_schema_enums_list_the_exposed_post_types_and_registered_sta $properties = wp_get_ability( self::ABILITY )->get_input_schema()['properties']['data']['properties']; $this->assertSameSets( array( 'post', 'page', 'cw_book' ), $properties['type']['enum'], 'The post type should list every exposed post type and no other.' ); - $this->assertSameSets( array_values( get_post_stati( array( 'internal' => false ) ) ), $properties['status']['enum'], 'The status should list every registered status that is not internal.' ); $this->assertContains( 'cw_archived', $properties['status']['enum'], 'The status should list a custom status.' ); } @@ -463,31 +462,20 @@ public static function data_create_by_role_and_status(): array { $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; $private_message = 'Sorry, you are not allowed to create private posts in this post type.'; - return array( - 'administrator, no status' => array( 'administrator', null, 'draft', null ), - 'administrator, draft' => array( 'administrator', 'draft', 'draft', null ), - 'administrator, pending' => array( 'administrator', 'pending', 'pending', null ), - 'administrator, publish' => array( 'administrator', 'publish', 'publish', null ), - 'administrator, future' => array( 'administrator', 'future', 'future', null ), - 'administrator, private' => array( 'administrator', 'private', 'private', null ), - 'editor, no status' => array( 'editor', null, 'draft', null ), - 'editor, draft' => array( 'editor', 'draft', 'draft', null ), - 'editor, pending' => array( 'editor', 'pending', 'pending', null ), - 'editor, publish' => array( 'editor', 'publish', 'publish', null ), - 'editor, future' => array( 'editor', 'future', 'future', null ), - 'editor, private' => array( 'editor', 'private', 'private', null ), - 'author, no status' => array( 'author', null, 'draft', null ), - 'author, draft' => array( 'author', 'draft', 'draft', null ), - 'author, pending' => array( 'author', 'pending', 'pending', null ), - 'author, publish' => array( 'author', 'publish', 'publish', null ), - 'author, future' => array( 'author', 'future', 'future', null ), - 'author, private' => array( 'author', 'private', 'private', null ), - 'contributor, no status' => array( 'contributor', null, 'draft', null ), - 'contributor, draft' => array( 'contributor', 'draft', 'draft', null ), - 'contributor, pending' => array( 'contributor', 'pending', 'pending', null ), - 'contributor, publish' => array( 'contributor', 'publish', 'content_cannot_publish', $publish_message ), - 'contributor, future' => array( 'contributor', 'future', 'content_cannot_publish', $publish_message ), - 'contributor, private' => array( 'contributor', 'private', 'content_cannot_publish', $private_message ), + $cases = array(); + foreach ( array( 'administrator', 'editor', 'author', 'contributor' ) as $role ) { + $cases[ "{$role}, no status" ] = array( $role, null, 'draft', null ); + foreach ( array( 'draft', 'pending', 'publish', 'future', 'private' ) as $status ) { + // A contributor may only create drafts and pending posts. + if ( 'contributor' === $role && ! in_array( $status, array( 'draft', 'pending' ), true ) ) { + $cases[ "{$role}, {$status}" ] = array( $role, $status, 'content_cannot_publish', 'private' === $status ? $private_message : $publish_message ); + } else { + $cases[ "{$role}, {$status}" ] = array( $role, $status, $status, null ); + } + } + } + + return $cases + array( // A custom status that makes a post visible needs `publish_posts`, and the refusal uses the general message. 'author, cw_public' => array( 'author', 'cw_public', 'cw_public', null ), 'author, cw_private' => array( 'author', 'cw_private', 'cw_private', null ), @@ -826,15 +814,34 @@ public function test_data_fails_input_validation( array $data ): void { } /** - * Called directly, an internal or unregistered status is an invalid status. + * Returns attribute values that a direct call cannot use: the attribute and the value. + * + * @return array The cases. + */ + public static function data_values_a_direct_call_cannot_use(): array { + $cases = array(); + foreach ( self::data_statuses_that_can_never_be_set() as $label => list( $status ) ) { + $cases[ "status: {$label}" ] = array( 'status', $status ); + } + foreach ( self::data_dates_without_a_usable_value() as $label => list( $date ) ) { + $cases[ "date: {$label}" ] = array( 'date', $date ); + } + + return $cases; + } + + /** + * Called directly, an internal or unregistered status, an empty date and a date that cannot be + * parsed are invalid attributes. * * @ticket 66706 - * @dataProvider data_statuses_that_can_never_be_set + * @dataProvider data_values_a_direct_call_cannot_use * @since 7.3.0 * - * @param string $status The status. + * @param string $attribute The attribute. + * @param mixed $value The value. */ - public function test_direct_call_rejects_a_status_that_can_never_be_set( string $status ): void { + public function test_direct_call_rejects_a_value_it_cannot_use( string $attribute, $value ): void { $this->login_as( 'administrator' ); $execute = $this->get_ability_callbacks()['execute_callback']; @@ -842,14 +849,14 @@ public function test_direct_call_rejects_a_status_that_can_never_be_set( string $result = $execute( array( 'data' => array( - 'type' => 'post', - 'title' => 'Status', - 'status' => $status, + 'type' => 'post', + 'title' => 'Unusable value', + $attribute => $value, ), ) ); - $this->assert_invalid_attribute( $result, 'status', 'A direct call with a status that can never be set' ); + $this->assert_invalid_attribute( $result, $attribute, "A direct call with a {$attribute} it cannot use" ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1015,27 +1022,72 @@ public function test_naming_an_author_the_role_may_name( string $role, string $a } /** - * A user with `edit_others_posts` who names a missing user gets the invalid author error. + * Returns attributes that are invalid for the user or after sanitization: the role, the data, + * the invalid attribute and the expected error message or null. * - * @ticket 66706 - * @since 7.3.0 + * @return array, 2: string, 3: string|null}> The cases. */ - public function test_naming_a_missing_author_with_edit_others_posts_is_an_invalid_author(): void { - $this->login_as( 'editor' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( + public static function data_invalid_attributes(): array { + return array( + 'missing author named with edit_others_posts' => array( + 'editor', + array( 'type' => 'post', 'title' => 'Nobody', 'author' => self::MISSING_ID, ), - ) + 'author', + null, + ), + 'slug that sanitizes to nothing' => array( + 'administrator', + array( + 'type' => 'post', + 'title' => 'Slug', + 'slug' => '!!!', + ), + 'slug', + null, + ), + // Intentionally strict: the save would drop the slug, so it is an error instead of being dropped without notice. + 'slug of a pending post without publish_posts' => array( + 'contributor', + array( + 'type' => 'post', + 'title' => 'Pending slug', + 'status' => 'pending', + 'slug' => 'pending-slug', + ), + 'slug', + 'The slug of a pending post can only be set by a user who can publish the post.', + ), ); + } + + /** + * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing + * and the slug of a pending post set by a user who may not publish posts are invalid attributes. + * + * @ticket 66706 + * @dataProvider data_invalid_attributes + * @since 7.3.0 + * + * @param string $role The role to create as. + * @param array $data The data. + * @param string $attribute The invalid attribute. + * @param string|null $message The expected error message, or null to check only that there is one. + */ + public function test_invalid_attribute_is_rejected( string $role, array $data, string $attribute, ?string $message ): void { + $this->login_as( $role ); + $this->register_abilities(); - $this->assert_invalid_attribute( $result, 'author', 'A missing author' ); + $before = $this->snapshot(); + $result = $this->run_ability( array( 'data' => $data ) ); + + $this->assert_invalid_attribute( $result, $attribute, "An invalid {$attribute}" ); + if ( null !== $message ) { + $this->assertSame( $message, $result->get_error_message(), 'The message should give the reason.' ); + } $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1166,7 +1218,10 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin } /** - * A readable page of the same type can be the parent, and `0` makes a top-level page. + * A readable page of the same type can be the parent, and a parent of `0` is accepted. + * + * A new page is top level without a parent too, so for `0` only the acceptance and the + * returned value are checked. Moving a page to the top level is an update. * * @ticket 66706 * @since 7.3.0 @@ -1198,7 +1253,8 @@ public function test_parent_is_stored(): void { $this->assertSame( self::$post_ids['published_page'], $this->get_created_post( $child, 'The create call of the child page' )->post_parent, 'The parent should be stored.' ); $this->assertSame( self::$post_ids['published_page'], $child['parent'], 'The parent should be returned.' ); - $this->assertSame( 0, $this->get_created_post( $top, 'The create call of the top-level page' )->post_parent, 'A parent of 0 should make a top-level page.' ); + $this->get_created_post( $top, 'The create call with a parent of 0' ); + $this->assertSame( 0, $top['parent'], 'A parent of 0 should be accepted and returned as 0.' ); } /** @@ -1344,16 +1400,18 @@ static function ( $query ) { } ); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Database error', - ), - ) - ); - - $wpdb->suppress_errors( $suppress ); + try { + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Database error', + ), + ) + ); + } finally { + $wpdb->suppress_errors( $suppress ); + } $this->assert_error( $result, 'db_insert_error', 500, 'A database error' ); $this->assertSame( 'Could not insert post into the database.', $result->get_error_message(), 'The message should be the message of the save error.' ); @@ -1428,61 +1486,6 @@ public function test_slug_is_sanitized_and_stored( string $slug, string $expecte $this->assertSame( $expected, $result['slug'], 'The stored slug should be returned.' ); } - /** - * A slug that sanitizes to nothing is an invalid slug. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Slug', - 'slug' => '!!!', - ), - ) - ); - - $this->assert_invalid_attribute( $result, 'slug', 'A slug that sanitizes to nothing' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - - /** - * A user who may not publish posts cannot set the slug of a pending post. - * - * Intentionally strict: the save would drop the slug, so it is an error instead of being - * dropped without notice. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_slug_of_a_pending_post_is_rejected_without_publish_posts(): void { - $this->login_as( 'contributor' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Pending slug', - 'status' => 'pending', - 'slug' => 'pending-slug', - ), - ) - ); - - $this->assert_invalid_attribute( $result, 'slug', 'A slug on a pending post without publish_posts' ); - $this->assertSame( 'The slug of a pending post can only be set by a user who can publish the post.', $result->get_error_message(), 'The message should say who may set the slug.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for a slug that cannot be stored.' ); - } - /** * A user who may publish posts sets the slug of a pending post. * @@ -1951,34 +1954,6 @@ public function test_returns_the_requested_fields_and_the_id(): void { ); } - /** - * Called directly, an empty date or a date that cannot be parsed is an invalid attribute. - * - * @ticket 66706 - * @dataProvider data_dates_without_a_usable_value - * @since 7.3.0 - * - * @param array $date The date. - */ - public function test_direct_call_rejects_a_date_without_a_usable_value( array $date ): void { - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot(); - $result = $execute( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Unusable date', - 'date' => $date, - ), - ) - ); - - $this->assert_invalid_attribute( $result, 'date', 'A direct call with a date without a usable value' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * A type that gets `show_in_abilities` after the abilities were registered is not in the type * enum, so a create of that type fails input validation. diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index a8011bf4aacf5..265d58a4f1db9 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -492,36 +492,4 @@ public static function data_keys_outside_the_allow_list(): array { 'where ID' => array( 'where', 'ID', 1 ), ); } - - /** - * A key outside the allow-list fails input validation. - * - * @ticket 66706 - * @dataProvider data_keys_outside_the_allow_list - * @since 7.3.0 - * - * @param string|null $location Where the key is sent, or null for the top level. - * @param string $key The key. - * @param mixed $value The value. - */ - public function test_key_outside_the_allow_list_fails_input_validation( ?string $location, string $key, $value ): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $input = $this->delete_input( $post_id ); - if ( null === $location ) { - $input[ $key ] = $value; - } else { - $input[ $location ][ $key ] = $value; - } - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $input ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the key.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index a1cd9f40387f0..08bcede58e3b2 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -131,16 +131,16 @@ public static function wpSetUpBeforeClass( $factory ): void { } /** - * Runs the update ability on a post with the given data. + * Builds the input that updates a post with the given data. * * @since 7.3.0 * * @param int $post_id The post ID. * @param array $data The data. - * @param list|null $fields The fields, or null for the default. - * @return mixed The ability result. + * @param list|null $fields Optional. The fields, or null for the default. Default null. + * @return array The input. */ - private function update_post( int $post_id, array $data, ?array $fields = null ) { + private function update_input( int $post_id, array $data, ?array $fields = null ): array { $input = array( 'where' => array( 'type' => get_post_type( $post_id ), @@ -152,7 +152,21 @@ private function update_post( int $post_id, array $data, ?array $fields = null ) $input['fields'] = $fields; } - return $this->run_ability( $input ); + return $input; + } + + /** + * Runs the update ability on a post with the given data. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @param array $data The data. + * @param list|null $fields The fields, or null for the default. + * @return mixed The ability result. + */ + private function update_post( int $post_id, array $data, ?array $fields = null ) { + return $this->run_ability( $this->update_input( $post_id, $data, $fields ) ); } /** @@ -393,21 +407,16 @@ public function test_lookup_comes_before_the_attribute_rules(): void { $this->login_as( 'subscriber' ); $execute = $this->get_ability_callbacks()['execute_callback']; - $before = $this->snapshot( self::$post_ids['administrator:draft'] ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => self::MISSING_ID, - ), - 'data' => array( - 'status' => 'trash', - 'parent' => 1, - 'title' => array( 'x' ), - ), - ) + $input = $this->build_post_input( self::MISSING_ID, 'post' ); + $input['data'] = array( + 'status' => 'trash', + 'parent' => 1, + 'title' => array( 'x' ), ); + $before = $this->snapshot( self::$post_ids['administrator:draft'] ); + $result = $execute( $input ); + $this->assert_not_found( $result, 'A missing post with invalid data' ); $this->assertSame( $before, $this->snapshot( self::$post_ids['administrator:draft'] ), 'Nothing should be written for a missing post.' ); } @@ -484,18 +493,8 @@ public function test_direct_call_without_edit_post_fails_before_the_attributes( $execute = $this->get_ability_callbacks()['execute_callback']; $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => $data, - ) - ); - $this->assert_error( $result, 'content_cannot_edit', 403, 'A direct call without edit_post' ); - $this->assertSame( 'Sorry, you are not allowed to edit this post.', $result->get_error_message(), 'The message should be the edit message.' ); + $this->assert_cannot_write( $execute( $this->update_input( $post_id, $data ) ), 'A direct call without edit_post' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -530,41 +529,6 @@ public static function data_keys_outside_the_allow_list(): array { ); } - /** - * A key outside the allow-list fails input validation. - * - * @ticket 66706 - * @dataProvider data_keys_outside_the_allow_list - * @since 7.3.0 - * - * @param string $location Where the key is sent. - * @param string $key The key. - * @param mixed $value The value. - */ - public function test_key_outside_the_allow_list_fails_input_validation( string $location, string $key, $value ): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $input = array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'Extra key' ), - ); - - $input[ $location ][ $key ] = $value; - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $input ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the key.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * An internal or unregistered status fails input validation. * @@ -577,15 +541,7 @@ public function test_key_outside_the_allow_list_fails_input_validation( string $ public function test_status_that_can_never_be_set_fails_input_validation( string $status ): void { $post_id = self::$post_ids['administrator:draft']; - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'status' => $status ) ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the status.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + $this->assert_invalid_input( $this->update_input( $post_id, array( 'status' => $status ) ), $post_id, 'A status that can never be set' ); } /** @@ -605,15 +561,7 @@ public function test_direct_call_rejects_a_status_that_can_never_be_set( string $execute = $this->get_ability_callbacks()['execute_callback']; $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'status' => $status ), - ) - ); + $result = $execute( $this->update_input( $post_id, array( 'status' => $status ) ) ); $this->assert_invalid_attribute( $result, 'status', 'A direct call with a status that can never be set' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); @@ -1122,15 +1070,7 @@ public static function data_values_of_the_wrong_type(): array { public function test_value_of_the_wrong_type_fails_input_validation( string $attribute, $value ): void { $post_id = self::$post_ids['administrator:draft']; - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( $attribute => $value ) ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the value.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + $this->assert_invalid_input( $this->update_input( $post_id, array( $attribute => $value ) ), $post_id, 'A value of the wrong type' ); } /** @@ -1551,52 +1491,6 @@ public function test_publishing_with_the_past_floating_date_stamps_the_current_t $this->assert_is_now( $post->post_date_gmt, 'The GMT date of the published post' ); } - /** - * Returns dates equal to the stored date of a scheduled post, 2099-01-15 10:00 in - * America/New_York. - * - * The date is in winter because PHP versions before 8.1 apply no daylight saving time - * after 2037. - * - * @return array}> The dates. - */ - public static function data_dates_equal_to_the_stored_date(): array { - return array( - 'site' => array( array( 'site' => '2099-01-15T10:00:00' ) ), - 'gmt' => array( array( 'gmt' => '2099-01-15T15:00:00' ) ), - ); - } - - /** - * A date equal to the stored date of a post that does not float changes nothing. - * - * @ticket 66706 - * @dataProvider data_dates_equal_to_the_stored_date - * @since 7.3.0 - * - * @param array $date The sent date. - */ - public function test_date_equal_to_the_stored_date_changes_nothing( array $date ): void { - update_option( 'timezone_string', 'America/New_York' ); - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$user_ids['editor'], - 'post_status' => 'future', - 'post_date' => '2099-01-15 10:00:00', - 'post_date_gmt' => '2099-01-15 15:00:00', - ) - ); - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $post = $this->assert_updated( $this->update_post( $post_id, array( 'date' => $date ) ), $post_id ); - - $this->assertSame( 'future', $post->post_status, 'The post should stay scheduled.' ); - $this->assertSame( '2099-01-15 10:00:00', $post->post_date, 'The local date should not change.' ); - $this->assertSame( '2099-01-15 15:00:00', $post->post_date_gmt, 'The GMT date should not change.' ); - } - /** * Returns statuses that do not float, for scheduling a floating draft for the date it shows. * @@ -2030,15 +1924,7 @@ public function test_direct_call_rejects_an_internal_status_the_post_already_has $execute = $this->get_ability_callbacks()['execute_callback']; $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'status' => $status ), - ) - ); + $result = $execute( $this->update_input( $post_id, array( 'status' => $status ) ) ); $this->assert_invalid_attribute( $result, 'status', 'A direct call with the internal status the post already has' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); @@ -2060,44 +1946,27 @@ public function test_direct_call_rejects_a_date_without_a_usable_value( array $d $execute = $this->get_ability_callbacks()['execute_callback']; $before = $this->snapshot( $post_id ); - $result = $execute( - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'date' => $date ), - ) - ); + $result = $execute( $this->update_input( $post_id, array( 'date' => $date ) ) ); $this->assert_invalid_attribute( $result, 'date', 'A direct call with a date without a usable value' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } /** - * Returns direct-call inputs whose `data` holds no supported attribute. + * Returns direct-call inputs without `data` to write. * * @return array}> The inputs, without `where`. */ public static function data_inputs_without_usable_data(): array { return array( - 'data missing' => array( array() ), - 'empty data' => array( array( 'data' => array() ) ), - 'data with only keys outside the list' => array( - array( - 'data' => array( - 'ID' => 1, - 'post_password' => 'secret', - 'post_title' => 'Not an attribute', - ), - ), - ), + 'data missing' => array( array() ), + 'empty data' => array( array( 'data' => array() ) ), ); } /** - * Called directly by a user who may edit the post, an update without a supported attribute is - * an invalid attribute and writes nothing. + * Called directly by a user who may edit the post, an update without `data` to write is an + * invalid attribute and writes nothing. * * @ticket 66706 * @dataProvider data_inputs_without_usable_data @@ -2298,6 +2167,7 @@ public function test_naming_the_status_a_date_leads_to_needs_publish_posts( stri ); $this->assert_error( $result, 'content_cannot_publish', 403, 'A published or scheduled post moved to the other status without publish_posts' ); + $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $result->get_error_message(), 'The message should be the publish message.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -2370,6 +2240,7 @@ public function test_edit_post_granted_on_one_post_allows_only_editing_that_post $publish = $this->update_post( $post_id, array( 'status' => 'publish' ) ); $this->assert_error( $publish, 'content_cannot_publish', 403, 'Publishing the granted post' ); + $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $publish->get_error_message(), 'The message should be the publish message.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for the refused publish.' ); } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index 4ff0c9ad9381d..c730273dbe457 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -34,20 +34,20 @@ class Tests_REST_API_WpRestAbilitiesContentController extends WP_UnitTestCase { protected static $subscriber_id; /** - * The run route for the core/content-query ability. + * Contributor user ID. * - * @var string + * @since 7.3.0 + * + * @var int */ - const RUN_ROUTE = '/wp-abilities/v1/abilities/core/content-query/run'; + protected static $contributor_id; /** - * An ID that no post has. - * - * @since 7.3.0 + * The run route for the core/content-query ability. * - * @var int + * @var string */ - private const MISSING_ID = 987654321; + const RUN_ROUTE = '/wp-abilities/v1/abilities/core/content-query/run'; /** * Sets up users and registers the core abilities. @@ -57,8 +57,9 @@ class Tests_REST_API_WpRestAbilitiesContentController extends WP_UnitTestCase { * @param WP_UnitTest_Factory $factory The unit test factory. */ public static function wpSetUpBeforeClass( $factory ): void { - self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); - self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + self::$contributor_id = $factory->user->create( array( 'role' => 'contributor' ) ); remove_action( 'wp_abilities_api_categories_init', '_unhook_core_ability_categories_registration', 1 ); remove_action( 'wp_abilities_api_init', '_unhook_core_abilities_registration', 1 ); @@ -577,11 +578,8 @@ public function test_delete_runs_with_delete_and_the_input_in_the_query_string() */ public static function data_permanent_query_string_values(): array { return array( - 'true' => array( 'true', 'deleted' ), - '1' => array( '1', 'deleted' ), - 'false' => array( 'false', 'trashed' ), - '0' => array( '0', 'trashed' ), - 'upper case' => array( 'TRUE', 'deleted' ), + 'true' => array( 'true', 'deleted' ), + 'false' => array( 'false', 'trashed' ), ); } @@ -631,10 +629,7 @@ public function test_delete_reads_permanent_from_the_query_string( string $perma */ public static function data_permanent_query_string_values_that_are_not_booleans(): array { return array( - 'yes' => array( 'yes' ), - '2' => array( '2' ), - 'on' => array( 'on' ), - 'empty string' => array( '' ), + 'yes' => array( 'yes' ), ); } @@ -670,63 +665,6 @@ public function test_delete_with_a_permanent_value_that_is_not_a_boolean_returns $this->assertSame( $before, $this->snapshot( $post_id ), 'The post should not change.' ); } - /** - * A create sent with POST but with the input in the query string fails input validation. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_create_with_the_input_in_the_query_string_returns_400(): void { - $request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/core/content-create/run' ); - $request->set_query_params( - array( - 'input' => array( - 'data' => array( - 'type' => 'post', - 'title' => 'Misplaced input', - ), - ), - ) - ); - - $before = $this->snapshot(); - $response = $this->server->dispatch( $request ); - - $this->assert_error_response( $response, 400, 'ability_invalid_input', 'A create with the input in the query string' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - - /** - * A delete sent with DELETE but with the input in a JSON body fails input validation. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_delete_with_the_input_in_a_json_body_returns_400(): void { - $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); - - $request = new WP_REST_Request( 'DELETE', '/wp-abilities/v1/abilities/core/content-delete/run' ); - $request->set_header( 'Content-Type', 'application/json' ); - $request->set_body( - wp_json_encode( - array( - 'input' => array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - ), - ) - ) - ); - - $before = $this->snapshot( $post_id ); - $response = $this->server->dispatch( $request ); - - $this->assert_error_response( $response, 400, 'ability_invalid_input', 'A delete with the input in a JSON body' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'The post should not change.' ); - } - /** * Returns write abilities with a method they do not take. * @@ -813,142 +751,135 @@ public function test_refused_create_receives_401_or_403( string $user, int $stat } /** - * Input that fails the schema is rejected. + * Returns write requests that fail, and the error response each receives. * - * @ticket 66706 - * @since 7.3.0 + * `post` names the post the request targets: `missing` for an ID that no post has, `published` + * for a published post by the administrator, `trashed` for a post in the Trash, or null for a + * create. `params` is the expected error `params`, or null to skip that check. + * + * @return array, post: string|null, user: string, status: int, code: string, params?: array}> The cases. */ - public function test_create_with_input_that_fails_the_schema_returns_400(): void { - $before = $this->snapshot(); - $response = $this->server->dispatch( - $this->write_request( - 'POST', - 'core/content-create', - array( + public static function data_failing_write_requests(): array { + return array( + 'create that fails the schema' => array( + 'ability' => 'core/content-create', + 'method' => 'POST', + 'input' => array( 'data' => array( 'type' => 'post', 'post_password' => 'secret', ), - ) - ) - ); - - $this->assert_error_response( $response, 400, 'ability_invalid_input', 'A create that fails the schema' ); - $this->assertSame( $before, $this->snapshot(), 'A create that fails the schema should write nothing.' ); - } - - /** - * The not-found error of an update reaches the response with its status. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_update_of_a_missing_post_returns_404(): void { - $before = $this->snapshot(); - $response = $this->server->dispatch( - $this->write_request( - 'POST', - 'core/content-update', - array( - 'where' => array( - 'type' => 'post', - 'id' => self::MISSING_ID, - ), - 'data' => array( 'title' => 'Missing' ), - ) - ) - ); - - $this->assert_error_response( $response, 404, 'content_not_found', 'An update of a missing post' ); - $this->assertSame( $before, $this->snapshot(), 'An update of a missing post should write nothing.' ); - } - - /** - * An update of a post the user cannot edit is refused before the ability runs. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_update_of_a_post_the_user_cannot_edit_returns_403(): void { - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$admin_id, - 'post_status' => 'publish', - ) - ); - wp_set_current_user( self::factory()->user->create( array( 'role' => 'contributor' ) ) ); - - $before = $this->snapshot( $post_id ); - $response = $this->server->dispatch( - $this->write_request( - 'POST', - 'core/content-update', - array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'Not mine' ), - ) - ) - ); - - $this->assert_error_response( $response, 403, 'rest_ability_cannot_execute', 'An update of a post the user cannot edit' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'The post the user cannot edit should not change.' ); - } - - /** - * The invalid attribute error of a create reaches the response with its status. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_create_with_an_unsupported_attribute_returns_400(): void { - $before = $this->snapshot(); - $response = $this->server->dispatch( - $this->write_request( - 'POST', - 'core/content-create', - array( + ), + 'post' => null, + 'user' => 'administrator', + 'status' => 400, + 'code' => 'ability_invalid_input', + ), + 'update of a missing post' => array( + 'ability' => 'core/content-update', + 'method' => 'POST', + 'input' => array( 'data' => array( 'title' => 'Missing' ) ), + 'post' => 'missing', + 'user' => 'administrator', + 'status' => 404, + 'code' => 'content_not_found', + ), + 'update of a post the user cannot edit' => array( + 'ability' => 'core/content-update', + 'method' => 'POST', + 'input' => array( 'data' => array( 'title' => 'Not mine' ) ), + 'post' => 'published', + 'user' => 'contributor', + 'status' => 403, + 'code' => 'rest_ability_cannot_execute', + ), + 'create with an unsupported attribute' => array( + 'ability' => 'core/content-create', + 'method' => 'POST', + 'input' => array( 'data' => array( 'type' => 'post', 'title' => 'With a parent', 'parent' => 1, ), - ) - ) + ), + 'post' => null, + 'user' => 'administrator', + 'status' => 400, + 'code' => 'content_invalid_attribute', + 'params' => array( 'data[parent]' => 'The parent attribute is only supported for hierarchical post types.' ), + ), + 'trashing a trashed post' => array( + 'ability' => 'core/content-delete', + 'method' => 'DELETE', + 'input' => array(), + 'post' => 'trashed', + 'user' => 'administrator', + 'status' => 410, + 'code' => 'content_already_trashed', + ), ); - - $this->assert_error_response( $response, 400, 'content_invalid_attribute', 'A create with an unsupported attribute' ); - $this->assertSame( array( 'data[parent]' => 'The parent attribute is only supported for hierarchical post types.' ), $response->get_data()['data']['params'] ?? null, 'The params should reach the response.' ); - $this->assertSame( $before, $this->snapshot(), 'A create with an unsupported attribute should write nothing.' ); } /** - * The already-trashed error of a delete reaches the response with its status. + * A failing write request receives the error with its status and writes nothing. + * + * Input that fails the schema is rejected, an update of a post the user cannot edit is + * refused before the ability runs, and the errors of the abilities reach the response with + * their status and `params`. * * @ticket 66706 + * @dataProvider data_failing_write_requests * @since 7.3.0 - */ - public function test_trashing_a_trashed_post_returns_410(): void { - $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); - wp_trash_post( $post_id ); + * + * @param string $ability The ability name. + * @param string $method The HTTP method. + * @param array $input The ability input, without `where`. + * @param string|null $post The post the request targets, or null for none. + * @param string $user The user, `administrator` or `contributor`. + * @param int $status The expected status. + * @param string $code The expected error code. + * @param array|null $params The expected error params, or null to skip that check. + */ + public function test_failing_write_request_returns_the_error_and_writes_nothing( string $ability, string $method, array $input, ?string $post, string $user, int $status, string $code, ?array $params = null ): void { + $post_id = 0; + if ( 'missing' === $post ) { + $post_id = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; + } elseif ( null !== $post ) { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$admin_id, + 'post_status' => 'publish', + ) + ); + if ( 'trashed' === $post ) { + wp_trash_post( $post_id ); + } + } - $before = $this->snapshot( $post_id ); - $response = $this->server->dispatch( - $this->write_request( - 'DELETE', - 'core/content-delete', + if ( null !== $post ) { + // A query string carries the ID as a string. + $input = array_merge( array( 'where' => array( 'type' => 'post', - 'id' => (string) $post_id, + 'id' => 'POST' === $method ? $post_id : (string) $post_id, ), - ) - ) - ); + ), + $input + ); + } - $this->assert_error_response( $response, 410, 'content_already_trashed', 'Trashing a trashed post' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'The trashed post should stay as it was.' ); + wp_set_current_user( 'contributor' === $user ? self::$contributor_id : self::$admin_id ); + + $context = $this->dataName(); + $before = $this->snapshot( $post_id ); + $response = $this->server->dispatch( $this->write_request( $method, $ability, $input ) ); + + $this->assert_error_response( $response, $status, $code, $context ); + if ( null !== $params ) { + $this->assertSame( $params, $response->get_data()['data']['params'] ?? null, "{$context}: the params should reach the response." ); + } + $this->assertSame( $before, $this->snapshot( $post_id ), "{$context}: nothing should be written." ); } } From cc7e2ef655cd580fa2de7a5ac1b2fce6af6e5465 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 20:06:10 +0000 Subject: [PATCH 15/24] Abilities API: Leave the Unix epoch date fix to its own ticket. The content query returns null for a date at the Unix epoch on trunk too, so the fix and its test move to a separate change. --- .../abilities/class-wp-abilities-content.php | 2 +- .../wpRegisterCoreContentAbility.php | 35 ------------------- 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 0ea9269660e43..23628193301fd 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -2730,7 +2730,7 @@ private function format_date( WP_Post $post, string $field, bool $gmt ): ?string */ $date = ( $gmt ? $datetime->setTimezone( new DateTimeZone( 'UTC' ) ) : $datetime )->format( 'c' ); - return false !== rest_parse_date( $date ) ? $date : null; + return rest_parse_date( $date ) ? $date : null; } /** diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index 13332448422c7..9b37bb5220469 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -5724,41 +5724,6 @@ public function test_malformed_date_is_null( string $field, string $column ): vo $this->assertNull( $result['items'][0][ $object ][ $key ], 'A malformed date should be null.' ); } - /** - * A stored date at the Unix epoch is returned: a timestamp of zero is a date, not a - * date that could not be parsed. - * - * @ticket 66706 - * @dataProvider data_date_fields - * @since 7.3.0 - * - * @param string $field The field path to request, such as `date.site`. - * @param string $column The cached post column to set to the Unix epoch. - */ - public function test_date_at_the_unix_epoch_is_returned( string $field, string $column ): void { - $this->login_as( 'administrator' ); - $this->register_ability(); - - $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); - - $this->replace_cached_post_date_columns( $post_id, array( $column => '1970-01-01 00:00:00' ) ); - - list( $object, $key ) = explode( '.', $field ); - - $result = wp_get_ability( 'core/content-query' )->execute( - array( - 'where' => array( - 'type' => 'post', - 'ids' => array( $post_id ), - ), - 'fields' => array( 'id', $field ), - ) - ); - - $this->assertIsArray( $result, 'A date at the Unix epoch should pass output validation.' ); - $this->assertSame( '1970-01-01T00:00:00+00:00', $result['items'][0][ $object ][ $key ] ?? null, 'A date at the Unix epoch should be returned.' ); - } - /** * Returns `where.author` values that are not an object holding an ID or a slug: values * that are not objects, empty values, and objects with an unknown property. From b3532acec251f3d972f0e68e7cbcfc2b9480f8e2 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 20:35:49 +0000 Subject: [PATCH 16/24] Tests: Use the shared constants in the content write ability tests. Replaces the `MISSING_ID` constant with `REST_TESTS_IMPOSSIBLY_HIGH_NUMBER` and dates the future fixture posts with `FUTURE_GMT`. See #66706. --- ...testcase-content-existing-post-ability.php | 12 ++++---- .../testcase-content-write-ability.php | 9 ------ .../wpRegisterCoreContentCreateAbility.php | 12 ++++---- .../wpRegisterCoreContentUpdateAbility.php | 28 +++++++++---------- 4 files changed, 26 insertions(+), 35 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php index edbba30337e43..35735e5ab65fe 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php @@ -53,10 +53,10 @@ protected static function create_shared_fixtures( WP_UnitTest_Factory $factory ) $stored_status = str_replace( 'trash_', '', $status ); $post_id = $factory->post->create( array( - 'post_author' => self::$user_ids[ $owner ], - 'post_status' => $stored_status, - 'post_title' => "Matrix {$owner} {$status}", - 'post_date' => 'future' === $stored_status ? '2099-01-01 00:00:00' : '', + 'post_author' => self::$user_ids[ $owner ], + 'post_status' => $stored_status, + 'post_title' => "Matrix {$owner} {$status}", + 'post_date_gmt' => 'future' === $stored_status ? str_replace( 'T', ' ', self::FUTURE_GMT ) : '', ) ); if ( $stored_status !== $status ) { @@ -415,7 +415,7 @@ public function test_permission_callback_lets_a_missing_post_through(): void { $this->login_as( 'subscriber' ); $permission = $this->get_ability_callbacks()['permission_callback']; - $input = $this->build_post_input( self::MISSING_ID, 'post' ); + $input = $this->build_post_input( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, 'post' ); $this->assertTrue( $permission( $input ), 'The permission callback should return true.' ); $this->assert_not_found( $this->run_ability( $input ), 'A missing post' ); @@ -515,7 +515,7 @@ public function test_key_outside_the_allow_list_fails_input_validation( ?string * @param string $post The post, as a fixture name. */ public function test_post_of_another_type_is_not_found( string $post ): void { - $post_id = self::$post_ids[ $post ] ?? self::MISSING_ID; + $post_id = self::$post_ids[ $post ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; $this->login_as( 'administrator' ); $this->register_abilities(); diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php index 8129a7f375c92..6f24432566eed 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -30,15 +30,6 @@ abstract class WP_Abilities_Content_Write_UnitTestCase extends WP_UnitTestCase { */ protected const CONTENT_ABILITIES = array( 'core/content-query', 'core/content-create', 'core/content-update', 'core/content-delete' ); - /** - * An ID that no post or user has. - * - * @since 7.3.0 - * - * @var int - */ - protected const MISSING_ID = 987654321; - /** * A GMT date that stays in the future. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index 8b8b7708945b1..b6af5921d0fb3 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -671,7 +671,7 @@ public static function data_invalid_data(): array { array( 'type' => 'post', 'title' => 'x', - 'author' => self::MISSING_ID, + 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ), ), 'title that is a list' => array( @@ -959,7 +959,7 @@ public function test_naming_another_author_without_edit_others_posts_is_refused( 'data' => array( 'type' => 'post', 'title' => 'Ghost written', - 'author' => self::$user_ids[ $author ] ?? self::MISSING_ID, + 'author' => self::$user_ids[ $author ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ), ) ); @@ -1034,7 +1034,7 @@ public static function data_invalid_attributes(): array { array( 'type' => 'post', 'title' => 'Nobody', - 'author' => self::MISSING_ID, + 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ), 'author', null, @@ -1143,7 +1143,7 @@ public function test_naming_a_non_member_without_edit_others_posts_is_refused_li $errors = array(); foreach ( array( 'non-member' => $non_member, - 'missing' => self::MISSING_ID, + 'missing' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ) as $key => $author ) { $result = $this->run_ability( array( @@ -1208,8 +1208,8 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin // The reference is another missing ID, so the `missing post` row is compared with a different input too. $before = $this->snapshot(); - $reference = $create( self::MISSING_ID + 1 ); - $result = $create( self::$post_ids[ $parent_key ] ?? self::MISSING_ID ); + $reference = $create( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER + 1 ); + $result = $create( self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); $this->assertWPError( $reference, 'Another missing parent should fail.' ); diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index 08bcede58e3b2..7621632dbd1cb 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -407,7 +407,7 @@ public function test_lookup_comes_before_the_attribute_rules(): void { $this->login_as( 'subscriber' ); $execute = $this->get_ability_callbacks()['execute_callback']; - $input = $this->build_post_input( self::MISSING_ID, 'post' ); + $input = $this->build_post_input( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, 'post' ); $input['data'] = array( 'status' => 'trash', 'parent' => 1, @@ -469,7 +469,7 @@ public static function data_invalid_data(): array { 'trash' => array( array( 'status' => 'trash' ) ), 'private without publish_posts' => array( array( 'status' => 'private' ) ), 'slug that sanitizes to nothing' => array( array( 'slug' => '!!!' ) ), - 'missing author' => array( array( 'author' => self::MISSING_ID ) ), + 'missing author' => array( array( 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ), 'title that is a list' => array( array( 'title' => array( 'x' ) ) ), 'invalid date' => array( array( 'date' => array( 'site' => 'not a date' ) ) ), 'empty data' => array( array() ), @@ -778,7 +778,7 @@ public function test_naming_another_author_without_edit_others_posts_is_refused( $this->register_abilities(); $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'author' => self::$user_ids[ $author ] ?? self::MISSING_ID ) ); + $result = $this->update_post( $post_id, array( 'author' => self::$user_ids[ $author ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $this->assertWPError( $result, 'Naming another author should fail.' ); $this->assertSame( @@ -831,7 +831,7 @@ public function test_author_equal_to_the_stored_author_passes_with_no_check(): v 'post_status' => 'draft', ) ); - $wpdb->update( $wpdb->posts, array( 'post_author' => self::MISSING_ID ), array( 'ID' => $post_id ) ); + $wpdb->update( $wpdb->posts, array( 'post_author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ), array( 'ID' => $post_id ) ); clean_post_cache( $post_id ); $this->login_as( 'editor' ); @@ -840,7 +840,7 @@ public function test_author_equal_to_the_stored_author_passes_with_no_check(): v $result = $this->update_post( $post_id, array( - 'author' => self::MISSING_ID, + 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, 'title' => 'Orphan', ) ); @@ -848,7 +848,7 @@ public function test_author_equal_to_the_stored_author_passes_with_no_check(): v $post = $this->assert_updated( $result, $post_id ); $this->assertSame( 'Orphan', $post->post_title, 'The title should be stored.' ); - $this->assertSame( self::MISSING_ID, (int) $post->post_author, 'The stored author should not change.' ); + $this->assertSame( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, (int) $post->post_author, 'The stored author should not change.' ); } /** @@ -864,7 +864,7 @@ public function test_naming_a_missing_author_with_edit_others_posts_is_an_invali $this->register_abilities(); $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'author' => self::MISSING_ID ) ); + $result = $this->update_post( $post_id, array( 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $this->assert_invalid_attribute( $result, 'author', 'A missing author' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); @@ -932,8 +932,8 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin // The reference names another missing ID than the missing post row, so no row compares a call with itself. $before = $this->snapshot( $post_id ); - $reference = $this->update_post( $post_id, array( 'parent' => self::MISSING_ID + 1 ) ); - $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? self::MISSING_ID ) ); + $reference = $this->update_post( $post_id, array( 'parent' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER + 1 ) ); + $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); $this->assertWPError( $reference, 'The reference call with a missing parent should fail.' ); $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); @@ -2300,25 +2300,25 @@ public function test_page_with_a_missing_parent_can_be_updated( bool $send_paren 'post_author' => self::$user_ids['page_writer'], 'post_type' => 'page', 'post_status' => 'draft', - 'post_parent' => self::MISSING_ID, + 'post_parent' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ) ); - $this->assertSame( self::MISSING_ID, get_post( $post_id )->post_parent, 'Precondition: the page should keep the missing parent.' ); - $this->assertNull( get_post( self::MISSING_ID ), 'Precondition: the parent should not exist.' ); + $this->assertSame( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, get_post( $post_id )->post_parent, 'Precondition: the page should keep the missing parent.' ); + $this->assertNull( get_post( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ), 'Precondition: the parent should not exist.' ); $this->login_as( 'page_writer' ); $this->register_abilities(); $data = array( 'title' => 'Orphan' ); if ( $send_parent ) { - $data['parent'] = self::MISSING_ID; + $data['parent'] = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; } $post = $this->assert_updated( $this->update_post( $post_id, $data ), $post_id ); $this->assertSame( 'Orphan', $post->post_title, 'The title should be stored.' ); - $this->assertSame( self::MISSING_ID, $post->post_parent, 'The stored parent should not change.' ); + $this->assertSame( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, $post->post_parent, 'The stored parent should not change.' ); } /** From 69f9d1956f03a5bc435d7bba495ba12824e8f631 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 24 Nov 2026 21:19:00 +0000 Subject: [PATCH 17/24] Abilities API: Reject content write dates that name different times. When both `site` and `gmt` dates are sent to the content write abilities, they now have to name the same time, instead of the site date winning. Also removes guards for an empty `data` and an unusable date that input validation already covers, and resets the author visibility memo where the post is saved. Tests: check the exact invalid attribute messages, share the tests that core/content-create and core/content-update have in common through a trait, and fold or remove duplicated tests and providers. See #66706. --- .../abilities/class-wp-abilities-content.php | 51 ++- ...testcase-content-existing-post-ability.php | 23 +- .../testcase-content-write-ability.php | 29 +- .../trait-content-data-write-tests.php | 186 ++++++++++ .../wpRegisterCoreContentAbility.php | 8 +- .../wpRegisterCoreContentCreateAbility.php | 259 +++++-------- .../wpRegisterCoreContentDeleteAbility.php | 7 +- .../wpRegisterCoreContentUpdateAbility.php | 344 ++++++------------ .../wpRestAbilitiesContentController.php | 86 ++--- 9 files changed, 459 insertions(+), 534 deletions(-) create mode 100644 tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 23628193301fd..193e633a56932 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -308,7 +308,8 @@ private function check_query_permission( $input = array() ): bool { * * @since 7.3.0 * - * @param mixed $input The unprepared ability input. + * @param mixed $input The ability input, which is not prepared when + * WP_Ability::check_permissions() is called directly. * @return bool True if the request may proceed, false otherwise. */ private function check_create_permission( $input ): bool { @@ -333,7 +334,8 @@ private function check_create_permission( $input ): bool { * * @since 7.3.0 * - * @param mixed $input The unprepared ability input. + * @param mixed $input The ability input, which is not prepared when + * WP_Ability::check_permissions() is called directly. * @param string $capability The capability required on the post: `edit_post` or `delete_post`. * @return bool True if the request may proceed, false otherwise. */ @@ -855,9 +857,6 @@ private function execute_content_query( $input = array() ) { * @return array|WP_Error The created post, or a WP_Error on failure. */ private function execute_content_create( array $input ) { - // The author visibility memo is per call; see get_visible_author(). - $this->visible_authors = array(); - $data = $input['data'] ?? array(); $post_type_object = $this->get_exposed_post_type( $data['type'] ?? null ); if ( ! $post_type_object ) { @@ -893,9 +892,6 @@ private function execute_content_create( array $input ) { * @return array|WP_Error The updated post, or a WP_Error on failure. */ private function execute_content_update( array $input ) { - // The author visibility memo is per call; see get_visible_author(). - $this->visible_authors = array(); - $post = $this->get_requested_post( $input['where'] ?? array() ); if ( ! $post ) { return $this->not_found_error(); @@ -910,15 +906,6 @@ private function execute_content_update( array $input ) { ); } - /* - * A `data` that sends no attribute that can be written has nothing to apply, so it is - * rejected instead of saving the post unchanged. - */ - $data = $input['data'] ?? array(); - if ( empty( $data ) ) { - return $this->invalid_attribute_error( 'data', __( 'The data must be an object with at least one attribute to write.' ) ); - } - /* * Known issue: a `data.status` sent for a post in the Trash moves it out of the Trash * through a regular save, not through wp_untrash_post(). The `_wp_trash_meta_status` @@ -930,7 +917,7 @@ private function execute_content_update( array $input ) { */ // get_requested_post() only returns posts of an exposed post type. - $postarr = $this->prepare_post_data( $data, get_post_type_object( $post->post_type ), $post ); + $postarr = $this->prepare_post_data( $input['data'], get_post_type_object( $post->post_type ), $post ); if ( is_wp_error( $postarr ) ) { return $postarr; } @@ -1261,7 +1248,6 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post return null; } - $reason = __( 'The parent must be 0, or a readable post of the same post type that is neither the post itself nor one of its descendants.' ); $parent_id = $data['parent']; // Keeping the stored parent changes nothing. @@ -1273,6 +1259,8 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post return 0; } + $reason = __( 'The parent must be 0, or a readable post of the same post type that is neither the post itself nor one of its descendants.' ); + /* * Intentionally strict: a parent must be readable, of the same type and free of * loops; it is never silently reset. @@ -1292,9 +1280,10 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post /** * Checks the sent `date` and returns the date fields to save. * - * `site` wins when both `site` and `gmt` are sent. On update, a sent date that equals the - * stored one counts as not sent, so a draft with a floating date keeps floating, unless - * the call moves it to a status whose dates do not float with a date that is still ahead. + * When both `site` and `gmt` are sent, they must name the same time. On update, a sent date + * that equals the stored one counts as not sent, so a draft with a floating date keeps + * floating, unless the call moves it to a status whose dates do not float with a date that + * is still ahead. * * @since 7.3.0 * @@ -1308,12 +1297,17 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { return array(); } - $date = $data['date']; - $dates = isset( $date['site'] ) ? rest_get_date_with_gmt( $date['site'] ) : rest_get_date_with_gmt( $date['gmt'] ?? '', true ); - if ( null === $dates ) { - return $this->invalid_attribute_error( 'data[date]', __( 'The date must be an object with a valid `site` or `gmt` date.' ) ); + $date = $data['date']; + $site = isset( $date['site'] ) ? rest_get_date_with_gmt( $date['site'] ) : null; + $gmt = isset( $date['gmt'] ) ? rest_get_date_with_gmt( $date['gmt'], true ) : null; + + // Intentionally strict: `site` and `gmt` that name different times are an error. + if ( $site && $gmt && $site[1] !== $gmt[1] ) { + return $this->invalid_attribute_error( 'data[date]', __( 'The `site` and `gmt` dates must name the same time.' ) ); } + $dates = $site ?? $gmt; + $fields = array( 'post_date' => $dates[0], 'post_date_gmt' => $dates[1], @@ -1416,6 +1410,9 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_ * may not read it, or a WP_Error on failure. */ private function persist_post( array $postarr, array $fields ) { + // The author visibility memo is per call; see get_visible_author(). + $this->visible_authors = array(); + $postarr = wp_slash( $postarr ); $post_id = isset( $postarr['ID'] ) ? wp_update_post( $postarr, true ) : wp_insert_post( $postarr, true ); @@ -2078,7 +2075,7 @@ private function get_content_write_properties( array $statuses ): array { 'description' => __( 'The publication date, in ISO 8601 format. Read as GMT unless it has a UTC offset.' ), ), ), - 'description' => __( 'The publication date. When both `site` and `gmt` are given, `site` is used.' ), + 'description' => __( 'The publication date. When both `site` and `gmt` are given, they must name the same time.' ), ), 'author' => array( 'type' => 'integer', diff --git a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php index 35735e5ab65fe..659493d0add43 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php @@ -149,6 +149,22 @@ protected function build_input( $where ): array { return array_merge( array( 'where' => $where ), static::OTHER_INPUT ); } + /** + * Builds the `where` that names a post by its type and ID. + * + * @since 7.3.0 + * + * @param int $post_id The post ID. + * @param string|null $type Optional. The post type, or null for the type of the post. Default null. + * @return array{type: string|false, id: int} The `where`. + */ + protected function build_where( int $post_id, ?string $type = null ): array { + return array( + 'type' => $type ?? get_post_type( $post_id ), + 'id' => $post_id, + ); + } + /** * Builds the input that names a post by its type and ID. * @@ -159,12 +175,7 @@ protected function build_input( $where ): array { * @return array The input. */ protected function build_post_input( int $post_id, ?string $type = null ): array { - return $this->build_input( - array( - 'type' => $type ?? get_post_type( $post_id ), - 'id' => $post_id, - ) - ); + return $this->build_input( $this->build_where( $post_id, $type ) ); } /** diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php index 6f24432566eed..750ffaf0645ae 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -328,15 +328,21 @@ protected function assert_refused( $result, string $context ): void { * * @since 7.3.0 * - * @param mixed $result The ability result. - * @param string $attribute The attribute name. - * @param string $context What the call was, for the assertion messages. + * @param mixed $result The ability result. + * @param string $attribute The attribute name. + * @param string $context What the call was, for the assertion messages. + * @param string|null $expected Optional. The expected message. Default null, which only + * requires a message. */ - protected function assert_invalid_attribute( $result, string $attribute, string $context ): void { + protected function assert_invalid_attribute( $result, string $attribute, string $context, ?string $expected = null ): void { $this->assert_error( $result, 'content_invalid_attribute', 400, $context ); $message = $result->get_error_message(); - $this->assertNotSame( '', $message, "{$context}: the message should give the reason." ); + if ( null === $expected ) { + $this->assertNotSame( '', $message, "{$context}: the message should give the reason." ); + } else { + $this->assertSame( $expected, $message, "{$context}: the message should give the reason." ); + } $this->assertSame( array( "data[{$attribute}]" => $message ), $result->get_error_data()['params'] ?? array(), "{$context}: the params should hold only the input path of the attribute, with the reason." ); } @@ -404,19 +410,6 @@ public static function data_authors_named_without_edit_others_posts(): array { ); } - /** - * Returns date values without a usable value for a direct call. - * - * @return array}> The dates. - */ - public static function data_dates_without_a_usable_value(): array { - return array( - 'empty date' => array( array() ), - 'site date that cannot be parsed' => array( array( 'site' => 'not a date' ) ), - 'GMT date that cannot be parsed' => array( array( 'gmt' => 'not a date' ) ), - ); - } - /** * The ability is registered in the content category with its label and annotations. * diff --git a/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php b/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php new file mode 100644 index 0000000000000..42d0fc6ffa8c6 --- /dev/null +++ b/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php @@ -0,0 +1,186 @@ + $data The data, without the type. + * @param list|null $fields Optional. The fields, or null for the default. Default null. + * @return array The input. + */ + abstract protected function data_write_input( ?int $post_id, array $data, ?array $fields = null ): array; + + /** + * Asserts that a result is the post item of a successful write and returns the stored post. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param int|null $post_id The post ID, or null when the call created a post. + * @return WP_Post The stored post. + */ + abstract protected function assert_data_written( $result, ?int $post_id ): WP_Post; + + /** + * Called directly, an internal or unregistered status is an invalid attribute. + * + * @ticket 66706 + * @dataProvider data_statuses_that_can_never_be_set + * @since 7.3.0 + * + * @param string $status The status. + */ + public function test_direct_call_rejects_a_status_that_can_never_be_set( string $status ): void { + $post_id = $this->data_write_post_id( 'administrator' ); + + $this->login_as( 'administrator' ); + $execute = $this->get_ability_callbacks()['execute_callback']; + + $before = $this->snapshot( $post_id ); + $result = $execute( + $this->data_write_input( + $post_id, + array( + 'title' => 'Unusable status', + 'status' => $status, + ) + ) + ); + + $this->assert_invalid_attribute( $result, 'status', 'A direct call with a status that can never be set', 'The status must be a registered post status that is not internal.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns dates that change the stored status: the requested status, the requested GMT date + * and the stored status. + * + * @return array The cases. + */ + public static function data_statuses_reported_as_stored(): array { + return array( + 'publish with a future date' => array( 'publish', self::FUTURE_GMT, 'future' ), + 'future with a past date' => array( 'future', '2016-12-12T19:00:00', 'publish' ), + ); + } + + /** + * The regular save rules decide the stored status, and the output reports it. + * + * @ticket 66706 + * @dataProvider data_statuses_reported_as_stored + * @since 7.3.0 + * + * @param string $status The requested status. + * @param string $gmt The requested GMT date. + * @param string $expected The stored status. + */ + public function test_output_reports_the_stored_status( string $status, string $gmt, string $expected ): void { + $post_id = $this->data_write_post_id( 'editor' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $input = $this->data_write_input( + $post_id, + array( + 'title' => 'Scheduled', + 'status' => $status, + 'date' => array( 'gmt' => $gmt ), + ), + array( 'status' ) + ); + $result = $this->run_ability( $input ); + + $this->assertSame( $expected, $this->assert_data_written( $result, $post_id )->post_status, 'The post should be stored in the expected status.' ); + $this->assertSame( $expected, $result['status'], 'The output should report the stored status.' ); + } + + /** + * A save error keeps its own code and message, with status 400 when the post is empty. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_empty_post_is_the_save_error(): void { + $post_id = $this->data_write_post_id( 'administrator' ); + + $this->login_as( 'administrator' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( + $this->data_write_input( + $post_id, + array( + 'title' => '', + 'content' => '', + 'excerpt' => '', + ) + ) + ); + + $this->assert_error( $result, 'empty_content', 400, 'An empty post' ); + $this->assertSame( 'Content, title, and excerpt are empty.', $result->get_error_message(), 'The message should be the message of the save error.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Quotes, backslashes and non-ASCII text survive a round trip unchanged. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { + $text = 'He said "it\'s done" \\ C:\\path\\to\\file \\\\server – café, naïve, 日本語, emoji ✓'; + $post_id = $this->data_write_post_id( 'editor' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $input = $this->data_write_input( + $post_id, + array( + 'title' => $text, + 'content' => $text, + 'excerpt' => $text, + ), + array( 'title.raw', 'content.raw', 'excerpt.raw' ) + ); + $result = $this->run_ability( $input ); + + $post = $this->assert_data_written( $result, $post_id ); + + $this->assertSame( $text, $post->post_title, 'The stored title should be unchanged.' ); + $this->assertSame( $text, $post->post_content, 'The stored content should be unchanged.' ); + $this->assertSame( $text, $post->post_excerpt, 'The stored excerpt should be unchanged.' ); + $this->assertSame( $text, $result['title']['raw'], 'The returned title should be unchanged.' ); + $this->assertSame( $text, $result['content']['raw'], 'The returned content should be unchanged.' ); + $this->assertSame( $text, $result['excerpt']['raw'], 'The returned excerpt should be unchanged.' ); + } +} diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php index 9b37bb5220469..71546b2c7424a 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentAbility.php @@ -3597,6 +3597,10 @@ public static function data_queries_without_a_status(): array { 'filter' => 'slugs', 'role' => 'administrator', ), + 'slugs, editor' => array( + 'filter' => 'slugs', + 'role' => 'editor', + ), ); } @@ -6912,8 +6916,8 @@ public function test_trash_needs_edit_posts(): void { */ public static function data_roles_that_edit_only_their_own_trashed_posts(): array { return array( - 'author' => array( 'role' => 'author_secondary' ), - 'contributor' => array( 'role' => 'contributor' ), + 'secondary author' => array( 'role' => 'author_secondary' ), + 'contributor' => array( 'role' => 'contributor' ), ); } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index b6af5921d0fb3..6301bec5a1efa 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -3,6 +3,7 @@ declare( strict_types=1 ); require_once __DIR__ . '/testcase-content-write-ability.php'; +require_once __DIR__ . '/trait-content-data-write-tests.php'; /** * Tests for the core/content-create ability shipped with the Abilities API. @@ -13,6 +14,8 @@ */ class Tests_Abilities_API_WpRegisterCoreContentCreateAbility extends WP_Abilities_Content_Write_UnitTestCase { + use WP_Abilities_Content_Data_Write_Tests; + /** * The name of the ability under test. * @@ -131,6 +134,7 @@ public static function wpSetUpBeforeClass( $factory ): void { private function get_created_post( $result, string $context = 'The create call' ): WP_Post { $this->assertIsArray( $result, "{$context} should succeed." ); $this->assertArrayHasKey( 'id', $result, "{$context} should return the created post with its ID." ); + $this->assertIsInt( $result['id'], "{$context} should return the ID as an integer." ); $post = get_post( $result['id'] ); $this->assertInstanceOf( WP_Post::class, $post, "{$context} should return the ID of a stored post." ); @@ -138,6 +142,50 @@ private function get_created_post( $result, string $context = 'The create call' return $post; } + /** + * Returns null: the shared tests create a post. + * + * @since 7.3.0 + * + * @param string $role The role, as a fixture name. + * @return null No post ID. + */ + protected function data_write_post_id( string $role ): ?int { + return null; + } + + /** + * Builds the input that creates a post of type `post` with the given data. + * + * @since 7.3.0 + * + * @param int|null $post_id Unused: the call creates a post. + * @param array $data The data, without the type. + * @param list|null $fields Optional. The fields, or null for the default. Default null. + * @return array The input. + */ + protected function data_write_input( ?int $post_id, array $data, ?array $fields = null ): array { + $input = array( 'data' => array_merge( array( 'type' => 'post' ), $data ) ); + if ( null !== $fields ) { + $input['fields'] = $fields; + } + + return $input; + } + + /** + * Returns the post a create call returned. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param int|null $post_id Unused: the call created a post. + * @return WP_Post The created post. + */ + protected function assert_data_written( $result, ?int $post_id ): WP_Post { + return $this->get_created_post( $result ); + } + /** * Registers a post type exposed to abilities with the given supported features. * @@ -289,7 +337,6 @@ public function test_creates_a_draft_with_the_default_fields(): void { $post = $this->get_created_post( $result ); $this->assertSameSets( array( 'id', 'type', 'status', 'date', 'slug', 'title' ), array_keys( $result ), 'The default fields should be returned.' ); - $this->assertSame( $post->ID, $result['id'], 'The ID should be the ID of the new post.' ); $this->assertSame( 'post', $result['type'], 'The type should be post.' ); $this->assertSame( 'draft', $result['status'], 'The post should be a draft.' ); $this->assertSameSets( array( 'site', 'gmt' ), array_keys( $result['date'] ), 'The date should be given in site time and GMT.' ); @@ -337,7 +384,6 @@ public function test_creates_a_published_child_page_with_chosen_fields(): void { $post = $this->get_created_post( $result ); $this->assertSameSets( array( 'id', 'status', 'link', 'title', 'content', 'parent' ), array_keys( $result ), 'The chosen fields should be returned.' ); - $this->assertSame( $post->ID, $result['id'], 'The ID should be the ID of the new page.' ); $this->assertSame( 'publish', $result['status'], 'The page should be published.' ); $this->assertSame( home_url( '/products/pricing/' ), $result['link'], 'The link should include the parent.' ); $this->assertSame( array( 'rendered' => 'Pricing' ), $result['title'], 'The title should be returned rendered.' ); @@ -813,53 +859,6 @@ public function test_data_fails_input_validation( array $data ): void { $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Returns attribute values that a direct call cannot use: the attribute and the value. - * - * @return array The cases. - */ - public static function data_values_a_direct_call_cannot_use(): array { - $cases = array(); - foreach ( self::data_statuses_that_can_never_be_set() as $label => list( $status ) ) { - $cases[ "status: {$label}" ] = array( 'status', $status ); - } - foreach ( self::data_dates_without_a_usable_value() as $label => list( $date ) ) { - $cases[ "date: {$label}" ] = array( 'date', $date ); - } - - return $cases; - } - - /** - * Called directly, an internal or unregistered status, an empty date and a date that cannot be - * parsed are invalid attributes. - * - * @ticket 66706 - * @dataProvider data_values_a_direct_call_cannot_use - * @since 7.3.0 - * - * @param string $attribute The attribute. - * @param mixed $value The value. - */ - public function test_direct_call_rejects_a_value_it_cannot_use( string $attribute, $value ): void { - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot(); - $result = $execute( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Unusable value', - $attribute => $value, - ), - ) - ); - - $this->assert_invalid_attribute( $result, $attribute, "A direct call with a {$attribute} it cannot use" ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * A status that is no longer registered when the call runs is an invalid status. * @@ -891,54 +890,10 @@ public function test_status_no_longer_registered_is_rejected(): void { ) ); - $this->assert_invalid_attribute( $result, 'status', 'A status that is no longer registered' ); + $this->assert_invalid_attribute( $result, 'status', 'A status that is no longer registered', 'The status must be a registered post status that is not internal.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Returns dates that change the stored status of a new post: the requested status, the - * requested GMT date and the stored status. - * - * @return array The cases. - */ - public static function data_statuses_reported_as_stored(): array { - return array( - 'publish with a future date' => array( 'publish', self::FUTURE_GMT, 'future' ), - 'future with a past date' => array( 'future', '2016-12-12T19:00:00', 'publish' ), - ); - } - - /** - * The regular save rules decide the stored status, and the output reports it. - * - * @ticket 66706 - * @dataProvider data_statuses_reported_as_stored - * @since 7.3.0 - * - * @param string $status The requested status. - * @param string $gmt The requested GMT date. - * @param string $expected The stored status. - */ - public function test_output_reports_the_stored_status( string $status, string $gmt, string $expected ): void { - $this->login_as( 'editor' ); - $this->register_abilities(); - - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Scheduled', - 'status' => $status, - 'date' => array( 'gmt' => $gmt ), - ), - 'fields' => array( 'status' ), - ) - ); - - $this->assertSame( $expected, $this->get_created_post( $result )->post_status, 'The post should be stored in the expected status.' ); - $this->assertSame( $expected, $result['status'], 'The output should report the stored status.' ); - } - /** * A user without `edit_others_posts` gets the same error for an existing and a missing * author, so the error never tells whether a user exists. @@ -1023,9 +978,9 @@ public function test_naming_an_author_the_role_may_name( string $role, string $a /** * Returns attributes that are invalid for the user or after sanitization: the role, the data, - * the invalid attribute and the expected error message or null. + * the invalid attribute and the expected error message. * - * @return array, 2: string, 3: string|null}> The cases. + * @return array, 2: string, 3: string}> The cases. */ public static function data_invalid_attributes(): array { return array( @@ -1037,7 +992,7 @@ public static function data_invalid_attributes(): array { 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, ), 'author', - null, + 'The author must be an existing user of this site.', ), 'slug that sanitizes to nothing' => array( 'administrator', @@ -1047,7 +1002,21 @@ public static function data_invalid_attributes(): array { 'slug' => '!!!', ), 'slug', - null, + 'The slug must contain characters that are valid in a slug.', + ), + // Intentionally strict: dates that name different times are an error instead of one of them winning. + 'site and GMT dates that name different times' => array( + 'administrator', + array( + 'type' => 'post', + 'title' => 'Two dates', + 'date' => array( + 'site' => '2016-12-12T14:00:00', + 'gmt' => '2001-01-01T00:00:00', + ), + ), + 'date', + 'The `site` and `gmt` dates must name the same time.', ), // Intentionally strict: the save would drop the slug, so it is an error instead of being dropped without notice. 'slug of a pending post without publish_posts' => array( @@ -1065,8 +1034,9 @@ public static function data_invalid_attributes(): array { } /** - * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing - * and the slug of a pending post set by a user who may not publish posts are invalid attributes. + * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing, + * site and GMT dates that name different times and the slug of a pending post set by a user + * who may not publish posts are invalid attributes. * * @ticket 66706 * @dataProvider data_invalid_attributes @@ -1075,19 +1045,16 @@ public static function data_invalid_attributes(): array { * @param string $role The role to create as. * @param array $data The data. * @param string $attribute The invalid attribute. - * @param string|null $message The expected error message, or null to check only that there is one. + * @param string $message The expected error message. */ - public function test_invalid_attribute_is_rejected( string $role, array $data, string $attribute, ?string $message ): void { + public function test_invalid_attribute_is_rejected( string $role, array $data, string $attribute, string $message ): void { $this->login_as( $role ); $this->register_abilities(); $before = $this->snapshot(); $result = $this->run_ability( array( 'data' => $data ) ); - $this->assert_invalid_attribute( $result, $attribute, "An invalid {$attribute}" ); - if ( null !== $message ) { - $this->assertSame( $message, $result->get_error_message(), 'The message should give the reason.' ); - } + $this->assert_invalid_attribute( $result, $attribute, "An invalid {$attribute}", $message ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1119,7 +1086,7 @@ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_inva ) ); - $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site' ); + $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site', 'The author must be an existing user of this site.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1318,10 +1285,7 @@ public function test_unsupported_attribute_is_rejected( string $type, string $at $before = $this->snapshot(); $result = $this->run_ability( array( 'data' => $data ) ); - $this->assert_invalid_attribute( $result, $attribute, 'An unsupported attribute' ); - if ( null !== $message ) { - $this->assertSame( $message, $result->get_error_message(), 'The message should give the reason.' ); - } + $this->assert_invalid_attribute( $result, $attribute, 'An unsupported attribute', $message ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1351,33 +1315,6 @@ public function test_empty_title_is_valid(): void { $this->assertSame( 'Only content.', $post->post_content, 'The content should be stored.' ); } - /** - * A save error keeps its own code and message, with status 400 when the post is empty. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_empty_post_is_the_save_error(): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => '', - 'content' => '', - 'excerpt' => '', - ), - ) - ); - - $this->assert_error( $result, 'empty_content', 400, 'An empty post' ); - $this->assertSame( 'Content, title, and excerpt are empty.', $result->get_error_message(), 'The message should be the message of the save error.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * A database error during the save keeps its own code and message, with status 500. * @@ -1608,10 +1545,18 @@ public static function data_dates(): array { 'GMT without offset' => array( array( 'gmt' => '2016-12-12T19:00:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), 'site with an offset' => array( array( 'site' => '2016-12-12T18:00:00-01:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), 'GMT with an offset' => array( array( 'gmt' => '2016-12-12T18:00:00-01:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), - 'both, the site wins' => array( + 'both, the same time' => array( array( 'site' => '2016-12-12T14:00:00', - 'gmt' => '2001-01-01T00:00:00', + 'gmt' => '2016-12-12T19:00:00', + ), + '2016-12-12 14:00:00', + '2016-12-12 19:00:00', + ), + 'both, with offsets' => array( + array( + 'site' => '2016-12-12T18:00:00-01:00', + 'gmt' => '2016-12-12T20:00:00+01:00', ), '2016-12-12 14:00:00', '2016-12-12 19:00:00', @@ -1823,40 +1768,6 @@ public function test_markup_of_a_super_admin_is_not_filtered(): void { $this->assert_markup_is_stored_unchanged(); } - /** - * Quotes, backslashes and non-ASCII text survive a round trip unchanged. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { - $text = 'He said "it\'s done" \\ C:\\path\\to\\file \\\\server – café, naïve, 日本語, emoji ✓'; - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => $text, - 'content' => $text, - 'excerpt' => $text, - ), - 'fields' => array( 'title.raw', 'content.raw', 'excerpt.raw' ), - ) - ); - - $post = $this->get_created_post( $result ); - - $this->assertSame( $text, $post->post_title, 'The stored title should be unchanged.' ); - $this->assertSame( $text, $post->post_content, 'The stored content should be unchanged.' ); - $this->assertSame( $text, $post->post_excerpt, 'The stored excerpt should be unchanged.' ); - $this->assertSame( $text, $result['title']['raw'], 'The returned title should be unchanged.' ); - $this->assertSame( $text, $result['content']['raw'], 'The returned content should be unchanged.' ); - $this->assertSame( $text, $result['excerpt']['raw'], 'The returned excerpt should be unchanged.' ); - } - /** * Raw fields are left out when the writer cannot edit the post after the write. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index 265d58a4f1db9..4c29195c5f201 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -74,12 +74,7 @@ public static function wpSetUpBeforeClass( $factory ): void { * @return array The input. */ private function delete_input( int $post_id, ?bool $permanent = null ): array { - $input = array( - 'where' => array( - 'type' => get_post_type( $post_id ), - 'id' => $post_id, - ), - ); + $input = array( 'where' => $this->build_where( $post_id ) ); if ( null !== $permanent ) { $input['permanent'] = $permanent; } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index 7621632dbd1cb..45751bcddbe81 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -3,6 +3,7 @@ declare( strict_types=1 ); require_once __DIR__ . '/testcase-content-existing-post-ability.php'; +require_once __DIR__ . '/trait-content-data-write-tests.php'; /** * Tests for the core/content-update ability shipped with the Abilities API. @@ -13,6 +14,8 @@ */ class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_Abilities_Content_Existing_Post_UnitTestCase { + use WP_Abilities_Content_Data_Write_Tests; + /** * The name of the ability under test. * @@ -142,10 +145,7 @@ public static function wpSetUpBeforeClass( $factory ): void { */ private function update_input( int $post_id, array $data, ?array $fields = null ): array { $input = array( - 'where' => array( - 'type' => get_post_type( $post_id ), - 'id' => $post_id, - ), + 'where' => $this->build_where( $post_id ), 'data' => $data, ); if ( null !== $fields ) { @@ -162,7 +162,7 @@ private function update_input( int $post_id, array $data, ?array $fields = null * * @param int $post_id The post ID. * @param array $data The data. - * @param list|null $fields The fields, or null for the default. + * @param list|null $fields Optional. The fields, or null for the default. Default null. * @return mixed The ability result. */ private function update_post( int $post_id, array $data, ?array $fields = null ) { @@ -187,6 +187,45 @@ private function assert_updated( $result, int $post_id ): WP_Post { return get_post( $post_id ); } + /** + * Returns the draft of the role, which the shared tests update. + * + * @since 7.3.0 + * + * @param string $role The role, as a fixture name. + * @return int The post ID. + */ + protected function data_write_post_id( string $role ): ?int { + return self::$post_ids[ "{$role}:draft" ]; + } + + /** + * Builds the input that updates a post with the given data. + * + * @since 7.3.0 + * + * @param int|null $post_id The post ID. + * @param array $data The data. + * @param list|null $fields Optional. The fields, or null for the default. Default null. + * @return array The input. + */ + protected function data_write_input( ?int $post_id, array $data, ?array $fields = null ): array { + return $this->update_input( (int) $post_id, $data, $fields ); + } + + /** + * Asserts that a result is the post item of a successful update and returns the stored post. + * + * @since 7.3.0 + * + * @param mixed $result The ability result. + * @param int|null $post_id The ID of the updated post. + * @return WP_Post The stored post. + */ + protected function assert_data_written( $result, ?int $post_id ): WP_Post { + return $this->assert_updated( $result, (int) $post_id ); + } + /** * Asserts that a result is the 403 edit error. * @@ -544,29 +583,6 @@ public function test_status_that_can_never_be_set_fails_input_validation( string $this->assert_invalid_input( $this->update_input( $post_id, array( 'status' => $status ) ), $post_id, 'A status that can never be set' ); } - /** - * Called directly, an internal or unregistered status is an invalid status for a post that - * is not stored in it. - * - * @ticket 66706 - * @dataProvider data_statuses_that_can_never_be_set - * @since 7.3.0 - * - * @param string $status The status. - */ - public function test_direct_call_rejects_a_status_that_can_never_be_set( string $status ): void { - $post_id = self::$post_ids['administrator:publish']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( $post_id ); - $result = $execute( $this->update_input( $post_id, array( 'status' => $status ) ) ); - - $this->assert_invalid_attribute( $result, 'status', 'A direct call with a status that can never be set' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * Returns status changes that a contributor, and an author denied `publish_posts`, may not * make on their own draft. @@ -674,7 +690,7 @@ public function test_schedules_a_draft(): void { $post = $this->assert_updated( $result, $post_id ); $this->assertSame( 'future', $post->post_status, 'The post should be scheduled.' ); - $this->assertSame( '2099-06-01 12:00:00', $post->post_date_gmt, 'The post should be scheduled for the sent date.' ); + $this->assertSame( str_replace( 'T', ' ', self::FUTURE_GMT ), $post->post_date_gmt, 'The post should be scheduled for the sent date.' ); $this->assertSame( 'future', $result['status'], 'The scheduled status should be returned.' ); } @@ -719,48 +735,6 @@ public function test_status_change_is_stored( string $role, string $stored, stri $this->assertSame( $status, $result['status'], 'The new status should be returned.' ); } - /** - * Returns dates that change the stored status of a post. - * - * @return array The cases. - */ - public static function data_statuses_reported_as_stored(): array { - return array( - 'publish with a future date' => array( 'publish', self::FUTURE_GMT, 'future' ), - 'future with a past date' => array( 'future', '2016-12-12T19:00:00', 'publish' ), - ); - } - - /** - * The regular save rules decide the stored status, and the output reports it. - * - * @ticket 66706 - * @dataProvider data_statuses_reported_as_stored - * @since 7.3.0 - * - * @param string $status The requested status. - * @param string $gmt The requested GMT date. - * @param string $expected The stored status. - */ - public function test_output_reports_the_stored_status( string $status, string $gmt, string $expected ): void { - $post_id = self::$post_ids['editor:draft']; - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $result = $this->update_post( - $post_id, - array( - 'status' => $status, - 'date' => array( 'gmt' => $gmt ), - ), - array( 'status' ) - ); - - $this->assertSame( $expected, $this->assert_updated( $result, $post_id )->post_status, 'The post should be stored in the expected status.' ); - $this->assertSame( $expected, $result['status'], 'The output should report the stored status.' ); - } - /** * A user without `edit_others_posts` gets the same error for an existing and a missing * author, so the error never tells whether a user exists. @@ -852,21 +826,63 @@ public function test_author_equal_to_the_stored_author_passes_with_no_check(): v } /** - * A user with `edit_others_posts` who names a missing user gets the invalid author error. + * Returns attributes that are invalid for the user or after sanitization: the role, the data, + * the invalid attribute and the expected error message. The post is the draft of the role. + * + * @return array, 2: string, 3: string}> The cases. + */ + public static function data_invalid_attributes(): array { + return array( + 'missing author named with edit_others_posts' => array( + 'editor', + array( 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ), + 'author', + 'The author must be an existing user of this site.', + ), + 'slug that sanitizes to nothing' => array( + 'administrator', + array( 'slug' => '!!!' ), + 'slug', + 'The slug must contain characters that are valid in a slug.', + ), + // Intentionally strict: dates that name different times are an error instead of one of them winning. + 'site and GMT dates that name different times' => array( + 'editor', + array( + 'date' => array( + 'site' => '2017-01-01T10:00:00', + 'gmt' => '2001-01-01T00:00:00', + ), + ), + 'date', + 'The `site` and `gmt` dates must name the same time.', + ), + ); + } + + /** + * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing + * and site and GMT dates that name different times are invalid attributes. * * @ticket 66706 + * @dataProvider data_invalid_attributes * @since 7.3.0 + * + * @param string $role The role to update as. + * @param array $data The `data` input. + * @param string $attribute The invalid attribute. + * @param string $message The expected error message. */ - public function test_naming_a_missing_author_with_edit_others_posts_is_an_invalid_author(): void { - $post_id = self::$post_ids['editor:draft']; + public function test_invalid_attribute_is_rejected( string $role, array $data, string $attribute, string $message ): void { + $post_id = self::$post_ids[ "{$role}:draft" ]; - $this->login_as( 'editor' ); + $this->login_as( $role ); $this->register_abilities(); $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); + $result = $this->update_post( $post_id, $data ); - $this->assert_invalid_attribute( $result, 'author', 'A missing author' ); + $this->assert_invalid_attribute( $result, $attribute, "An invalid {$attribute}", $message ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -890,7 +906,7 @@ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_inva $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'author' => $non_member ) ); - $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site' ); + $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site', 'The author must be an existing user of this site.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -912,8 +928,8 @@ public static function data_parents_that_cannot_be_used(): array { } /** - * A missing parent, a parent of another type, a parent the user may not read and a parent - * that would make a loop give one identical error. + * A missing parent, a parent of another type, a parent the user may not read, a revision and a + * parent that would make a loop give one identical error. * * @ticket 66706 * @dataProvider data_parents_that_cannot_be_used @@ -1107,25 +1123,6 @@ public function test_attributes_not_sent_keep_their_stored_values(): void { $this->assertSame( $stored->post_date_gmt, $post->post_date_gmt, 'The date should be kept.' ); } - /** - * A slug that sanitizes to nothing is an invalid slug. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_slug_that_sanitizes_to_nothing_is_rejected(): void { - $post_id = self::$post_ids['administrator:draft']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'slug' => '!!!' ) ); - - $this->assert_invalid_attribute( $result, 'slug', 'A slug that sanitizes to nothing' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * Returns updates that send a slug for a post that is, or becomes, pending. * @@ -1166,8 +1163,7 @@ public function test_slug_of_a_pending_post_is_rejected_without_publish_posts( s $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, $data ); - $this->assert_invalid_attribute( $result, 'slug', 'A slug on a pending post without publish_posts' ); - $this->assertSame( 'The slug of a pending post can only be set by a user who can publish the post.', $result->get_error_message(), 'The message should say who may set the slug.' ); + $this->assert_invalid_attribute( $result, 'slug', 'A slug on a pending post without publish_posts', 'The slug of a pending post can only be set by a user who can publish the post.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for a slug that cannot be stored.' ); } @@ -1385,10 +1381,10 @@ public static function data_new_dates(): array { 'site' => array( array( 'site' => '2017-01-01T10:00:00' ) ), 'gmt' => array( array( 'gmt' => '2017-01-01T15:00:00' ) ), 'site with an offset' => array( array( 'site' => '2017-01-01T16:00:00+01:00' ) ), - 'both, the site wins' => array( + 'both, the same time' => array( array( 'site' => '2017-01-01T10:00:00', - 'gmt' => '2001-01-01T00:00:00', + 'gmt' => '2017-01-01T15:00:00', ), ), ); @@ -1606,39 +1602,6 @@ public function test_markup_of_a_site_administrator_is_filtered_on_multisite(): $this->assertSame( '
div
', $post->post_content, 'The stored content should be filtered.' ); } - /** - * Quotes, backslashes and non-ASCII text survive a round trip unchanged. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { - $text = 'He said "it\'s done" \\ C:\\path\\to\\file \\\\server – café, naïve, 日本語, emoji ✓'; - $post_id = self::$post_ids['editor:draft']; - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $result = $this->update_post( - $post_id, - array( - 'title' => $text, - 'content' => $text, - 'excerpt' => $text, - ), - array( 'title.raw', 'content.raw', 'excerpt.raw' ) - ); - - $post = $this->assert_updated( $result, $post_id ); - - $this->assertSame( $text, $post->post_title, 'The stored title should be unchanged.' ); - $this->assertSame( $text, $post->post_content, 'The stored content should be unchanged.' ); - $this->assertSame( $text, $post->post_excerpt, 'The stored excerpt should be unchanged.' ); - $this->assertSame( $text, $result['title']['raw'], 'The returned title should be unchanged.' ); - $this->assertSame( $text, $result['content']['raw'], 'The returned content should be unchanged.' ); - $this->assertSame( $text, $result['excerpt']['raw'], 'The returned excerpt should be unchanged.' ); - } - /** * Raw fields are left out when the writer can no longer edit the post after the write. * @@ -1720,14 +1683,7 @@ public function test_each_call_checks_the_author_visibility_again(): void { $this->assertFalse( current_user_can( 'list_users' ), 'Precondition: the editor should not be able to list users.' ); $execute = $this->get_ability_callbacks()['execute_callback']; - $input = array( - 'where' => array( - 'type' => 'post', - 'id' => $post_id, - ), - 'data' => array( 'title' => 'Visibility' ), - 'fields' => array( 'author' ), - ); + $input = $this->update_input( $post_id, array( 'title' => 'Visibility' ), array( 'author' ) ); $hidden = $execute( $input ); @@ -1754,33 +1710,6 @@ public function test_each_call_checks_the_author_visibility_again(): void { $this->assertSame( get_userdata( $writer )->display_name, $visible['author']['name'], 'The author should be shown once they have a published post.' ); } - /** - * A save error keeps its own code and message, with status 400 when the post is empty. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_empty_post_is_the_save_error(): void { - $post_id = self::$post_ids['administrator:draft']; - - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( - $post_id, - array( - 'title' => '', - 'content' => '', - 'excerpt' => '', - ) - ); - - $this->assert_error( $result, 'empty_content', 400, 'An empty post' ); - $this->assertSame( 'Content, title, and excerpt are empty.', $result->get_error_message(), 'The message should be the message of the save error.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * A database error during the save keeps its own code and message, with status 500. * @@ -1805,9 +1734,11 @@ static function ( $query ) { } ); - $result = $this->update_post( $post_id, array( 'title' => 'Database error' ) ); - - $wpdb->suppress_errors( $suppress ); + try { + $result = $this->update_post( $post_id, array( 'title' => 'Database error' ) ); + } finally { + $wpdb->suppress_errors( $suppress ); + } $this->assert_error( $result, 'db_update_error', 500, 'A database error' ); $this->assertSame( 'Could not update post in the database.', $result->get_error_message(), 'The message should be the message of the save error.' ); @@ -1926,70 +1857,7 @@ public function test_direct_call_rejects_an_internal_status_the_post_already_has $before = $this->snapshot( $post_id ); $result = $execute( $this->update_input( $post_id, array( 'status' => $status ) ) ); - $this->assert_invalid_attribute( $result, 'status', 'A direct call with the internal status the post already has' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Called directly, an empty date or a date that cannot be parsed is an invalid attribute. - * - * @ticket 66706 - * @dataProvider data_dates_without_a_usable_value - * @since 7.3.0 - * - * @param array $date The date. - */ - public function test_direct_call_rejects_a_date_without_a_usable_value( array $date ): void { - $post_id = self::$post_ids['administrator:draft']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( $post_id ); - $result = $execute( $this->update_input( $post_id, array( 'date' => $date ) ) ); - - $this->assert_invalid_attribute( $result, 'date', 'A direct call with a date without a usable value' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - - /** - * Returns direct-call inputs without `data` to write. - * - * @return array}> The inputs, without `where`. - */ - public static function data_inputs_without_usable_data(): array { - return array( - 'data missing' => array( array() ), - 'empty data' => array( array( 'data' => array() ) ), - ); - } - - /** - * Called directly by a user who may edit the post, an update without `data` to write is an - * invalid attribute and writes nothing. - * - * @ticket 66706 - * @dataProvider data_inputs_without_usable_data - * @since 7.3.0 - * - * @param array $input The input, without `where`. - */ - public function test_direct_call_without_usable_data_is_an_invalid_attribute( array $input ): void { - $post_id = self::$post_ids['administrator:draft']; - - $this->login_as( 'administrator' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $input['where'] = array( - 'type' => 'post', - 'id' => $post_id, - ); - - $before = $this->snapshot( $post_id ); - $result = $execute( $input ); - - $this->assert_error( $result, 'content_invalid_attribute', 400, 'A direct call without usable data' ); - $this->assertSame( array( 'data' => 'The data must be an object with at least one attribute to write.' ), $result->get_error_data()['params'] ?? array(), 'The params should hold only the data path, with the reason.' ); + $this->assert_invalid_attribute( $result, 'status', 'A direct call with the internal status the post already has', 'The status must be a registered post status that is not internal.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php index c730273dbe457..831a1a532e699 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesContentController.php @@ -545,70 +545,44 @@ public function test_update_runs_with_post_and_a_json_body(): void { } /** - * Delete runs with DELETE and the input in the query string, where the ID is a string. + * Returns `permanent` values as they arrive in a query string, or null when it is not sent, + * and the outcome. * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_delete_runs_with_delete_and_the_input_in_the_query_string(): void { - $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); - - $response = $this->server->dispatch( - $this->write_request( - 'DELETE', - 'core/content-delete', - array( - 'where' => array( - 'type' => 'post', - 'id' => (string) $post_id, - ), - ) - ) - ); - - $this->assertSame( 200, $response->get_status(), 'The delete request should succeed.' ); - $this->assertSame( array( 'outcome' => 'trashed' ), $response->get_data(), 'The post should be reported as trashed.' ); - $this->assertSame( 'trash', get_post_status( $post_id ), 'The post should be in the Trash.' ); - } - - /** - * Returns `permanent` values as they arrive in a query string, and the outcome. - * - * @return array The cases. + * @return array The cases. */ public static function data_permanent_query_string_values(): array { return array( - 'true' => array( 'true', 'deleted' ), - 'false' => array( 'false', 'trashed' ), + 'not sent' => array( null, 'trashed' ), + 'true' => array( 'true', 'deleted' ), + 'false' => array( 'false', 'trashed' ), ); } /** - * `permanent` in the query string is read as a boolean. + * Delete runs with DELETE and the input in the query string, where the ID is a string and + * `permanent` is read as a boolean. * * @ticket 66706 * @dataProvider data_permanent_query_string_values * @since 7.3.0 * - * @param string $permanent The value in the query string. - * @param string $outcome The expected outcome. + * @param string|null $permanent The value in the query string, or null when it is not sent. + * @param string $outcome The expected outcome. */ - public function test_delete_reads_permanent_from_the_query_string( string $permanent, string $outcome ): void { + public function test_delete_reads_permanent_from_the_query_string( ?string $permanent, string $outcome ): void { $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); - $response = $this->server->dispatch( - $this->write_request( - 'DELETE', - 'core/content-delete', - array( - 'where' => array( - 'type' => 'post', - 'id' => (string) $post_id, - ), - 'permanent' => $permanent, - ) - ) + $input = array( + 'where' => array( + 'type' => 'post', + 'id' => (string) $post_id, + ), ); + if ( null !== $permanent ) { + $input['permanent'] = $permanent; + } + + $response = $this->server->dispatch( $this->write_request( 'DELETE', 'core/content-delete', $input ) ); $this->assertSame( 200, $response->get_status(), 'The delete request should succeed.' ); $this->assertSame( array( 'outcome' => $outcome ), $response->get_data(), 'The outcome should follow the permanent value.' ); @@ -622,28 +596,14 @@ public function test_delete_reads_permanent_from_the_query_string( string $perma } } - /** - * Returns `permanent` values in a query string that are not booleans. - * - * @return array The values. - */ - public static function data_permanent_query_string_values_that_are_not_booleans(): array { - return array( - 'yes' => array( 'yes' ), - ); - } - /** * A `permanent` value in the query string that is not a boolean fails input validation, and * the post is unchanged. * * @ticket 66706 - * @dataProvider data_permanent_query_string_values_that_are_not_booleans * @since 7.3.0 - * - * @param string $permanent The value in the query string. */ - public function test_delete_with_a_permanent_value_that_is_not_a_boolean_returns_400( string $permanent ): void { + public function test_delete_with_a_permanent_value_that_is_not_a_boolean_returns_400(): void { $post_id = self::factory()->post->create( array( 'post_status' => 'publish' ) ); $before = $this->snapshot( $post_id ); @@ -656,7 +616,7 @@ public function test_delete_with_a_permanent_value_that_is_not_a_boolean_returns 'type' => 'post', 'id' => (string) $post_id, ), - 'permanent' => $permanent, + 'permanent' => 'yes', ) ) ); From ca76e015e7dc29a2228ae33251af8ffa87cc273f Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 26 Nov 2026 17:09:56 +0000 Subject: [PATCH 18/24] Abilities API: Check the content write capabilities in the permission callbacks. The permission callbacks of `core/content-create` and `core/content-update` now also check the capabilities the sent status and author need, so the execute callbacks no longer check capabilities again. When both the site and the GMT date are sent, the site date is used again, so the dates that `core/content-query` returns can be sent back, also after the site's timezone changed. Sending back the stored slug, also the empty slug of a draft, leaves it unchanged. --- .../abilities/class-wp-abilities-content.php | 232 +++++++------- ...testcase-content-existing-post-ability.php | 32 +- .../wpRegisterCoreContentCreateAbility.php | 233 ++++---------- .../wpRegisterCoreContentDeleteAbility.php | 13 - .../wpRegisterCoreContentUpdateAbility.php | 298 +++++++++++------- 5 files changed, 393 insertions(+), 415 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 193e633a56932..87d0a0533ee9d 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -222,7 +222,7 @@ private function register_content_write_abilities( array $post_types, array $sta return $this->execute_content_update( $input ); }, 'permission_callback' => function ( $input ): bool { - return $this->check_post_permission( $input, 'edit_post' ); + return $this->check_update_permission( $input ); }, 'meta' => array( 'annotations' => array( @@ -302,9 +302,9 @@ private function check_query_permission( $input = array() ): bool { /** * Permission callback for the `core/content-create` ability. * - * A coarse gate: a logged-in user, a `data.type` that is exposed to abilities, and the - * capability to create posts of that type. {@see self::execute_content_create()} checks - * the capability again and makes every other decision. + * Requires a logged-in user, a `data.type` that is exposed to abilities, the capability + * to create posts of that type, and the capabilities that the sent attributes need, + * see {@see self::can_set_attributes()}. * * @since 7.3.0 * @@ -320,17 +320,96 @@ private function check_create_permission( $input ): bool { $data = $this->get_input_object( rest_sanitize_object( $input ), 'data' ) ?? array(); $post_type_object = $this->get_exposed_post_type( $data['type'] ?? null ); - return null !== $post_type_object && current_user_can( $post_type_object->cap->create_posts ); + return null !== $post_type_object + && current_user_can( $post_type_object->cap->create_posts ) + && $this->can_set_attributes( $data, $post_type_object, null ); } /** - * Permission callback for the `core/content-update` and `core/content-delete` abilities. + * Permission callback for the `core/content-update` ability. + * + * Requires what {@see self::check_post_permission()} requires for `edit_post`, and the + * capabilities that the sent attributes need, see {@see self::can_set_attributes()}. + * + * @since 7.3.0 + * + * @param mixed $input The ability input, which is not prepared when + * WP_Ability::check_permissions() is called directly. + * @return bool True if the request may proceed, false otherwise. + */ + private function check_update_permission( $input ): bool { + $input = rest_sanitize_object( $input ); + if ( ! $this->check_post_permission( $input, 'edit_post' ) ) { + return false; + } + + $post = $this->get_requested_post( $this->get_input_object( $input, 'where' ) ?? array() ); + if ( ! $post ) { + return true; + } + + // get_requested_post() only returns posts of an exposed post type. + return $this->can_set_attributes( $this->get_input_object( $input, 'data' ) ?? array(), get_post_type_object( $post->post_type ), $post ); + } + + /** + * Checks the capabilities that the sent `status` and `author` need. + * + * A status that makes the post visible needs the capability to publish posts of the type, + * and an author other than the current user needs the capability to edit the posts of + * others. A status or an author that keeps the stored value needs neither. Every other + * rule on these attributes is checked by the execute callbacks. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param WP_Post_Type $post_type_object The post type of the post being written. + * @param WP_Post|null $post The stored post on update, or null on create. + * @return bool True if the user may set the sent attributes, false otherwise. + */ + private function can_set_attributes( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ): bool { + if ( array_key_exists( 'status', $data ) ) { + $status = $data['status']; + if ( ! is_string( $status ) ) { + return false; + } + + /* + * Intentionally strict: every status that makes a post visible needs the capability + * to publish posts of the type, including custom ones. + */ + $status_object = get_post_status_object( $status ); + $makes_visible = $status_object instanceof stdClass + && ( 'future' === $status || ! empty( $status_object->public ) || ! empty( $status_object->private ) || is_post_status_viewable( $status_object ) ); + $is_unchanged = $post && $post->post_status === $status; + if ( $makes_visible && ! $is_unchanged && ! current_user_can( $post_type_object->cap->publish_posts ) ) { + return false; + } + } + + if ( array_key_exists( 'author', $data ) ) { + $author_id = $this->parse_filter_int( $data['author'], 1 ); + if ( null === $author_id ) { + return false; + } + + $is_unchanged = $post && (int) $post->post_author === $author_id; + if ( ! $is_unchanged && get_current_user_id() !== $author_id && ! current_user_can( $post_type_object->cap->edit_others_posts ) ) { + return false; + } + } + + return true; + } + + /** + * Permission callback for the `core/content-delete` ability, also used by the one of + * `core/content-update`. * * Requires a logged-in user, a `where.type` that is exposed to abilities, and the given * capability on the post that `where` names. When `where` names no post of that type, * such as a missing ID or the ID of a post of another type, the request is let through - * so that the execute callback can report it as not found. The execute callbacks check - * the capability again and make every other decision. + * so that the execute callback can report it as not found. * * @since 7.3.0 * @@ -839,8 +918,9 @@ private function execute_content_query( $input = array() ) { * Mirror the REST posts controller: totals come from the underlying WP_Query, * while the row-level checks above may withhold individual returned rows. The * totals can therefore count posts the current user cannot read, such as another - * author's draft listed in `ids`. This is deliberate: it only reveals that such a - * post exists, as a REST collection request with `include` and `status` does. + * author's draft or a trashed post listed in `ids`. This is deliberate: it only + * reveals that such a post exists, as a REST collection request with `include` + * and `status` does. */ return $this->prepare_output( $posts, $total, $total_pages ); } @@ -848,8 +928,9 @@ private function execute_content_query( $input = array() ) { /** * Executes the `core/content-create` ability. * - * The post type and the capability to create posts of it are checked first, then every - * attribute rule, and only then is the post saved, so a rejected call writes nothing. + * The post type is checked first, then every attribute rule, and only then is the post + * saved, so a rejected call writes nothing. The capabilities are checked by + * {@see self::check_create_permission()}. * * @since 7.3.0 * @@ -863,15 +944,6 @@ private function execute_content_create( array $input ) { return $this->not_found_error(); } - // Checked again after the permission callback, whose result a filter can change. - if ( ! current_user_can( $post_type_object->cap->create_posts ) ) { - return new WP_Error( - 'content_cannot_create', - __( 'Sorry, you are not allowed to create posts as this user.' ), - array( 'status' => 403 ) - ); - } - $postarr = $this->prepare_post_data( $data, $post_type_object, null ); if ( is_wp_error( $postarr ) ) { return $postarr; @@ -883,8 +955,9 @@ private function execute_content_create( array $input ) { /** * Executes the `core/content-update` ability. * - * The post is looked up first, then the capability to edit it is checked, then every - * attribute rule, and only then is the post saved, so a rejected call writes nothing. + * The post is looked up first, then every attribute rule, and only then is the post + * saved, so a rejected call writes nothing. The capabilities are checked by + * {@see self::check_update_permission()}. * * @since 7.3.0 * @@ -897,15 +970,6 @@ private function execute_content_update( array $input ) { return $this->not_found_error(); } - // Checked again after the permission callback, whose result a filter can change. - if ( ! current_user_can( 'edit_post', $post->ID ) ) { - return new WP_Error( - 'content_cannot_edit', - __( 'Sorry, you are not allowed to edit this post.' ), - array( 'status' => 403 ) - ); - } - /* * Known issue: a `data.status` sent for a post in the Trash moves it out of the Trash * through a regular save, not through wp_untrash_post(). The `_wp_trash_meta_status` @@ -928,9 +992,9 @@ private function execute_content_update( array $input ) { /** * Executes the `core/content-delete` ability. * - * The post is looked up first, then the capability to delete it is checked. The post is - * deleted permanently when `permanent` is true; otherwise it is moved to the Trash. The - * outcome is confirmed against the stored post afterwards. + * The post is deleted permanently when `permanent` is true; otherwise it is moved to the + * Trash. The outcome is confirmed against the stored post afterwards. The capability is + * checked by {@see self::check_post_permission()}. * * @since 7.3.0 * @@ -943,15 +1007,6 @@ private function execute_content_delete( array $input ) { return $this->not_found_error(); } - // Checked again after the permission callback, whose result a filter can change. - if ( ! current_user_can( 'delete_post', $post->ID ) ) { - return new WP_Error( - 'content_cannot_delete', - __( 'Sorry, you are not allowed to delete this post.' ), - array( 'status' => 403 ) - ); - } - if ( ! empty( $input['permanent'] ) ) { $deleted = wp_delete_post( $post->ID, true ); @@ -1043,7 +1098,7 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, } } - $status = $this->prepare_status( $data, $post_type_object, $post ); + $status = $this->prepare_status( $data, $post ); if ( is_wp_error( $status ) ) { return $status; } @@ -1051,7 +1106,7 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, $postarr['post_status'] = $status; } - $author = $this->prepare_author( $data, $post_type_object, $post ); + $author = $this->prepare_author( $data, $post ); if ( is_wp_error( $author ) ) { return $author; } @@ -1067,11 +1122,7 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, $postarr['post_parent'] = $parent; } - $dates = $this->prepare_date( $data, $post, $status ); - if ( is_wp_error( $dates ) ) { - return $dates; - } - $postarr = array_merge( $postarr, $dates ); + $postarr = array_merge( $postarr, $this->prepare_date( $data, $post, $status ) ); // Without a sent status, the save defaults to `draft`. $final_status = $postarr['post_status'] ?? $post->post_status ?? 'draft'; @@ -1139,12 +1190,11 @@ private function check_supported_attributes( array $data, string $post_type ): ? * * @since 7.3.0 * - * @param array $data The `data` input. - * @param WP_Post_Type $post_type_object The post type of the post being written. - * @param WP_Post|null $post The stored post on update, or null on create. + * @param array $data The `data` input. + * @param WP_Post|null $post The stored post on update, or null on create. * @return string|WP_Error|null The status, null when none is sent or it is unchanged, or a WP_Error. */ - private function prepare_status( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ) { + private function prepare_status( array $data, ?WP_Post $post ) { if ( ! array_key_exists( 'status', $data ) ) { return null; } @@ -1166,38 +1216,22 @@ private function prepare_status( array $data, WP_Post_Type $post_type_object, ?W return null; } - /* - * Intentionally strict: every status that makes a post visible needs the capability - * to publish posts of the type, including custom ones. - */ - $makes_visible = 'future' === $status || ! empty( $status_object->public ) || ! empty( $status_object->private ) || is_post_status_viewable( $status_object ); - if ( $makes_visible && ! current_user_can( $post_type_object->cap->publish_posts ) ) { - return new WP_Error( - 'content_cannot_publish', - 'private' === $status - ? __( 'Sorry, you are not allowed to create private posts in this post type.' ) - : __( 'Sorry, you are not allowed to publish posts in this post type.' ), - array( 'status' => 403 ) - ); - } - return $status; } /** * Checks the sent `author`. * - * The permission to name another user is checked before the user is looked up, so the - * error never tells whether a user ID exists. + * The capability to name another user is checked by the permission callbacks, so only + * users who may name any user learn whether a user ID exists. * * @since 7.3.0 * - * @param array $data The `data` input. - * @param WP_Post_Type $post_type_object The post type of the post being written. - * @param WP_Post|null $post The stored post on update, or null on create. + * @param array $data The `data` input. + * @param WP_Post|null $post The stored post on update, or null on create. * @return int|WP_Error|null The author ID, null when none is sent or it is unchanged, or a WP_Error. */ - private function prepare_author( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ) { + private function prepare_author( array $data, ?WP_Post $post ) { if ( ! array_key_exists( 'author', $data ) ) { return null; } @@ -1213,16 +1247,6 @@ private function prepare_author( array $data, WP_Post_Type $post_type_object, ?W return $author_id; } - if ( ! current_user_can( $post_type_object->cap->edit_others_posts ) ) { - return new WP_Error( - 'content_cannot_edit_others', - $post - ? __( 'Sorry, you are not allowed to update posts as this user.' ) - : __( 'Sorry, you are not allowed to create posts as this user.' ), - array( 'status' => 403 ) - ); - } - // Intentionally strict: the author must be an existing user who belongs to this site. if ( ! get_userdata( $author_id ) || ( is_multisite() && ! is_user_member_of_blog( $author_id ) ) ) { return $this->invalid_attribute_error( 'data[author]', __( 'The author must be an existing user of this site.' ) ); @@ -1278,35 +1302,27 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post } /** - * Checks the sent `date` and returns the date fields to save. + * Returns the date fields to save for the sent `date`. * - * When both `site` and `gmt` are sent, they must name the same time. On update, a sent date - * that equals the stored one counts as not sent, so a draft with a floating date keeps - * floating, unless the call moves it to a status whose dates do not float with a date that - * is still ahead. + * `site` wins when both `site` and `gmt` are sent, so the pair that the query returns can + * be sent back, also after the site's timezone changed. On update, a sent date that equals + * the stored one counts as not sent, so a draft with a floating date keeps floating, unless + * the call moves it to a status whose dates do not float with a date that is still ahead. * * @since 7.3.0 * * @param array $data The `data` input. * @param WP_Post|null $post The stored post on update, or null on create. * @param string|null $status The status to set, or null when none is sent or it is unchanged. - * @return array|WP_Error The date fields to save, empty for none, or a WP_Error. + * @return array The date fields to save, empty for none. */ - private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { + private function prepare_date( array $data, ?WP_Post $post, ?string $status ): array { if ( ! array_key_exists( 'date', $data ) ) { return array(); } - $date = $data['date']; - $site = isset( $date['site'] ) ? rest_get_date_with_gmt( $date['site'] ) : null; - $gmt = isset( $date['gmt'] ) ? rest_get_date_with_gmt( $date['gmt'], true ) : null; - - // Intentionally strict: `site` and `gmt` that name different times are an error. - if ( $site && $gmt && $site[1] !== $gmt[1] ) { - return $this->invalid_attribute_error( 'data[date]', __( 'The `site` and `gmt` dates must name the same time.' ) ); - } - - $dates = $site ?? $gmt; + $date = $data['date']; + $dates = isset( $date['site'] ) ? rest_get_date_with_gmt( $date['site'] ) : rest_get_date_with_gmt( $date['gmt'], true ); $fields = array( 'post_date' => $dates[0], @@ -1355,13 +1371,18 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ) { * @param WP_Post|null $post The stored post on update, or null on create. * @param string $status The status the post will have. * @param int $parent_id The parent ID the post will have. - * @return string|WP_Error|null The slug, null when none is sent, or a WP_Error. + * @return string|WP_Error|null The slug, null when none is sent or it is unchanged, or a WP_Error. */ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_Post $post, string $status, int $parent_id ) { if ( ! array_key_exists( 'slug', $data ) ) { return null; } + // Keeping the stored slug changes nothing, also the empty slug of a draft. + if ( $post && $post->post_name === $data['slug'] ) { + return null; + } + /* * Intentionally strict: the save drops the slug of a pending post when the user cannot * publish it, so the slug is an error here instead of being dropped without notice. @@ -2056,7 +2077,6 @@ private function get_content_write_properties( array $statuses ): array { ), 'slug' => array( 'type' => 'string', - 'minLength' => 1, 'description' => __( 'The post slug. It is sanitized as a post slug is, and made unique among the posts of the type that share its parent.' ), ), 'date' => array( @@ -2075,7 +2095,7 @@ private function get_content_write_properties( array $statuses ): array { 'description' => __( 'The publication date, in ISO 8601 format. Read as GMT unless it has a UTC offset.' ), ), ), - 'description' => __( 'The publication date. When both `site` and `gmt` are given, they must name the same time.' ), + 'description' => __( 'The publication date. When both `site` and `gmt` are given, `site` is used.' ), ), 'author' => array( 'type' => 'integer', diff --git a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php index 659493d0add43..a9dec7b918ae1 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php @@ -127,16 +127,6 @@ protected static function create_shared_fixtures( WP_UnitTest_Factory $factory ) } } - /** - * Asserts that a result is the 403 error of the capability check in the execute callback. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $context What the call was, for the assertion messages. - */ - abstract protected function assert_cannot_write( $result, string $context ): void; - /** * Builds the input that names a post with the given `where`. * @@ -433,23 +423,27 @@ public function test_permission_callback_lets_a_missing_post_through(): void { } /** - * A permission result that a filter turned into a grant does not skip the capability check - * on the post. + * A permission result that a filter turns into a grant is the final permission decision: + * the call runs without a second capability check on the post. * * @ticket 66706 * @since 7.3.0 */ - public function test_granted_permission_result_does_not_skip_the_capability_check(): void { + public function test_permission_result_granted_by_a_filter_is_final(): void { $post_id = self::$post_ids['admin_post']; $this->login_as( 'contributor' ); $this->register_abilities(); + + $input = $this->build_post_input( $post_id ); + $this->assert_refused( $this->run_ability( $input ), 'Precondition: a call without the filter' ); + add_filter( 'wp_ability_permission_result', '__return_true' ); $before = $this->snapshot( $post_id ); - $this->assert_cannot_write( $this->run_ability( $this->build_post_input( $post_id ) ), 'A call granted by a filter' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + $this->assertIsArray( $this->run_ability( $input ), 'A call granted by a filter should succeed.' ); + $this->assertNotSame( $before, $this->snapshot( $post_id ), 'A call granted by a filter should write the post.' ); } /** @@ -624,19 +618,19 @@ public function test_direct_call_with_a_malformed_where_is_not_found( ?array $wh public function test_each_call_checks_the_current_user(): void { $own_id = self::$post_ids['author:draft']; $other_id = self::$post_ids['author:pending']; - $execute = $this->get_ability_callbacks()['execute_callback']; + $this->register_abilities(); $this->login_as( 'author' ); $own_before = $this->snapshot( $own_id ); - $first = $execute( $this->build_post_input( $own_id ) ); + $first = $this->run_ability( $this->build_post_input( $own_id ) ); $this->login_as( 'contributor' ); $other_before = $this->snapshot( $other_id ); - $second = $execute( $this->build_post_input( $other_id ) ); + $second = $this->run_ability( $this->build_post_input( $other_id ) ); $this->assertIsArray( $first, 'The first call as the author should succeed.' ); $this->assertNotSame( $own_before, $this->snapshot( $own_id ), 'The first call should write the post.' ); - $this->assert_cannot_write( $second, 'The second call as a contributor' ); + $this->assert_refused( $second, 'The second call as a contributor' ); $this->assertSame( $other_before, $this->snapshot( $other_id ), 'The second call should write nothing.' ); } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index 6301bec5a1efa..72d5c225d35bd 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -240,7 +240,7 @@ public function test_input_schema(): void { $this->assertSame( 'string', $properties['status']['type'], 'The status should be a string.' ); $this->assertSame( array( 'publish', 'future', 'draft', 'pending', 'private' ), $properties['status']['enum'], 'The status should list the statuses that are not internal.' ); $this->assertSame( 'string', $properties['slug']['type'], 'The slug should be a string.' ); - $this->assertSame( 1, $properties['slug']['minLength'], 'The slug should not be empty.' ); + $this->assertArrayNotHasKey( 'minLength', $properties['slug'], 'The slug should take an empty string, so the empty slug of a draft can be sent back.' ); $this->assertSame( 'integer', $properties['author']['type'], 'The author should be an integer.' ); $this->assertSame( 1, $properties['author']['minimum'], 'The author should be at least 1.' ); $this->assertSame( 'integer', $properties['parent']['type'], 'The parent should be an integer.' ); @@ -480,6 +480,22 @@ public static function data_inputs_refused_by_the_permission_callback(): array { 'type that is a number' => array( array( 'data' => array( 'type' => 1 ) ) ), 'unexposed type' => array( array( 'data' => array( 'type' => 'attachment' ) ) ), 'unregistered type' => array( array( 'data' => array( 'type' => 'cw_missing' ) ) ), + 'status that is a list' => array( + array( + 'data' => array( + 'type' => 'post', + 'status' => array( 'draft' ), + ), + ), + ), + 'author that is a word' => array( + array( + 'data' => array( + 'type' => 'post', + 'author' => 'admin', + ), + ), + ), ); } @@ -499,38 +515,35 @@ public function test_permission_callback_returns_true_for_a_user_who_may_create_ /** * Returns the outcome of creating a post per role and requested status: the role, the - * requested status or null to send none, the stored status or the error code, and the error - * message or null on success. + * requested status or null to send none, and the stored status or null when the permission + * callback refuses the call. * - * @return array The cases. + * @return array The cases. */ public static function data_create_by_role_and_status(): array { - $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; - $private_message = 'Sorry, you are not allowed to create private posts in this post type.'; - $cases = array(); foreach ( array( 'administrator', 'editor', 'author', 'contributor' ) as $role ) { - $cases[ "{$role}, no status" ] = array( $role, null, 'draft', null ); + $cases[ "{$role}, no status" ] = array( $role, null, 'draft' ); foreach ( array( 'draft', 'pending', 'publish', 'future', 'private' ) as $status ) { // A contributor may only create drafts and pending posts. if ( 'contributor' === $role && ! in_array( $status, array( 'draft', 'pending' ), true ) ) { - $cases[ "{$role}, {$status}" ] = array( $role, $status, 'content_cannot_publish', 'private' === $status ? $private_message : $publish_message ); + $cases[ "{$role}, {$status}" ] = array( $role, $status, null ); } else { - $cases[ "{$role}, {$status}" ] = array( $role, $status, $status, null ); + $cases[ "{$role}, {$status}" ] = array( $role, $status, $status ); } } } return $cases + array( - // A custom status that makes a post visible needs `publish_posts`, and the refusal uses the general message. - 'author, cw_public' => array( 'author', 'cw_public', 'cw_public', null ), - 'author, cw_private' => array( 'author', 'cw_private', 'cw_private', null ), - 'author, cw_viewable' => array( 'author', 'cw_viewable', 'cw_viewable', null ), - 'contributor, cw_public' => array( 'contributor', 'cw_public', 'content_cannot_publish', $publish_message ), - 'contributor, cw_private' => array( 'contributor', 'cw_private', 'content_cannot_publish', $publish_message ), - 'contributor, cw_viewable' => array( 'contributor', 'cw_viewable', 'content_cannot_publish', $publish_message ), + // A custom status that makes a post visible needs `publish_posts`. + 'author, cw_public' => array( 'author', 'cw_public', 'cw_public' ), + 'author, cw_private' => array( 'author', 'cw_private', 'cw_private' ), + 'author, cw_viewable' => array( 'author', 'cw_viewable', 'cw_viewable' ), + 'contributor, cw_public' => array( 'contributor', 'cw_public', null ), + 'contributor, cw_private' => array( 'contributor', 'cw_private', null ), + 'contributor, cw_viewable' => array( 'contributor', 'cw_viewable', null ), // A custom status that is neither public, private nor publicly viewable needs nothing beyond `create_posts`. - 'contributor, cw_hidden' => array( 'contributor', 'cw_hidden', 'cw_hidden', null ), + 'contributor, cw_hidden' => array( 'contributor', 'cw_hidden', 'cw_hidden' ), ); } @@ -547,10 +560,9 @@ public static function data_create_by_role_and_status(): array { * * @param string $role The role to create as. * @param string|null $status The requested status, or null to send none. - * @param string $expected The stored status, or the error code. - * @param string|null $message The error message, or null on success. + * @param string|null $expected The stored status, or null when the call is refused. */ - public function test_create_by_role_and_status( string $role, ?string $status, string $expected, ?string $message ): void { + public function test_create_by_role_and_status( string $role, ?string $status, ?string $expected ): void { $this->register_custom_statuses(); $this->login_as( $role ); @@ -575,9 +587,8 @@ public function test_create_by_role_and_status( string $role, ?string $status, s ) ); - if ( null !== $message ) { - $this->assert_error( $result, $expected, 403, 'Creating a post in a status the role may not set' ); - $this->assertSame( $message, $result->get_error_message(), 'The message should name what the role may not do.' ); + if ( null === $expected ) { + $this->assert_refused( $result, 'Creating a post in a status the role may not set' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); return; } @@ -681,83 +692,6 @@ public function test_direct_call_without_an_exposed_type_is_not_found( array $in $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Returns `data` values that are invalid in some way. - * - * @return array}> The data. - */ - public static function data_invalid_data(): array { - return array( - 'parent on a post' => array( - array( - 'type' => 'post', - 'parent' => 1, - ), - ), - 'unregistered status' => array( - array( - 'type' => 'post', - 'status' => 'cw_unregistered', - ), - ), - 'publish' => array( - array( - 'type' => 'post', - 'status' => 'publish', - ), - ), - 'slug that sanitizes to nothing' => array( - array( - 'type' => 'post', - 'title' => 'x', - 'slug' => '!!!', - ), - ), - 'missing author' => array( - array( - 'type' => 'post', - 'title' => 'x', - 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, - ), - ), - 'title that is a list' => array( - array( - 'type' => 'post', - 'title' => array( 'x' ), - ), - ), - 'invalid date' => array( - array( - 'type' => 'post', - 'title' => 'x', - 'date' => array( 'site' => 'not a date' ), - ), - ), - ); - } - - /** - * Called directly by a user without `create_posts`, the call fails on the capability before - * anything about the attributes is checked. - * - * @ticket 66706 - * @dataProvider data_invalid_data - * @since 7.3.0 - * - * @param array $data The data. - */ - public function test_direct_call_without_create_posts_fails_before_the_attributes( array $data ): void { - $this->login_as( 'subscriber' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot(); - $result = $execute( array( 'data' => $data ) ); - - $this->assert_error( $result, 'content_cannot_create', 403, 'A direct call without create_posts' ); - $this->assertSame( 'Sorry, you are not allowed to create posts as this user.', $result->get_error_message(), 'The message should be the create message.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * Returns `data` values that the input schema rejects: keys outside the allow-list, statuses * that can never be set and values of the wrong type. @@ -895,8 +829,8 @@ public function test_status_no_longer_registered_is_rejected(): void { } /** - * A user without `edit_others_posts` gets the same error for an existing and a missing - * author, so the error never tells whether a user exists. + * The permission callback refuses a user without `edit_others_posts` who names another + * author, existing or not, so the refusal never tells whether a user exists. * * @ticket 66706 * @dataProvider data_authors_named_without_edit_others_posts @@ -919,20 +853,7 @@ public function test_naming_another_author_without_edit_others_posts_is_refused( ) ); - $this->assertWPError( $result, 'Naming another author should fail.' ); - $this->assertSame( - array( - 'code' => 'content_cannot_edit_others', - 'message' => 'Sorry, you are not allowed to create posts as this user.', - 'data' => array( 'status' => 403 ), - ), - array( - 'code' => $result->get_error_code(), - 'message' => $result->get_error_message(), - 'data' => $result->get_error_data(), - ), - 'The error should be the same whether the user exists or not.' - ); + $this->assert_refused( $result, 'Naming another author' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1004,19 +925,15 @@ public static function data_invalid_attributes(): array { 'slug', 'The slug must contain characters that are valid in a slug.', ), - // Intentionally strict: dates that name different times are an error instead of one of them winning. - 'site and GMT dates that name different times' => array( + 'empty slug' => array( 'administrator', array( 'type' => 'post', - 'title' => 'Two dates', - 'date' => array( - 'site' => '2016-12-12T14:00:00', - 'gmt' => '2001-01-01T00:00:00', - ), + 'title' => 'Slug', + 'slug' => '', ), - 'date', - 'The `site` and `gmt` dates must name the same time.', + 'slug', + 'The slug must contain characters that are valid in a slug.', ), // Intentionally strict: the save would drop the slug, so it is an error instead of being dropped without notice. 'slug of a pending post without publish_posts' => array( @@ -1034,9 +951,8 @@ public static function data_invalid_attributes(): array { } /** - * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing, - * site and GMT dates that name different times and the slug of a pending post set by a user - * who may not publish posts are invalid attributes. + * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing + * and the slug of a pending post set by a user who may not publish posts are invalid attributes. * * @ticket 66706 * @dataProvider data_invalid_attributes @@ -1125,7 +1041,7 @@ public function test_naming_a_non_member_without_edit_others_posts_is_refused_li $errors[ $key ] = array( $result->get_error_code(), $result->get_error_message(), $result->get_error_data() ); } - $this->assertSame( 'content_cannot_edit_others', $errors['missing'][0], 'The error should be the authorship error.' ); + $this->assertSame( 'ability_invalid_permissions', $errors['missing'][0], 'The permission callback should refuse the call.' ); $this->assertSame( $errors['missing'], $errors['non-member'], 'The error should be the same for a non-member and a missing user.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for either user.' ); } @@ -1545,18 +1461,10 @@ public static function data_dates(): array { 'GMT without offset' => array( array( 'gmt' => '2016-12-12T19:00:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), 'site with an offset' => array( array( 'site' => '2016-12-12T18:00:00-01:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), 'GMT with an offset' => array( array( 'gmt' => '2016-12-12T18:00:00-01:00' ), '2016-12-12 14:00:00', '2016-12-12 19:00:00' ), - 'both, the same time' => array( + 'both, the site wins' => array( array( 'site' => '2016-12-12T14:00:00', - 'gmt' => '2016-12-12T19:00:00', - ), - '2016-12-12 14:00:00', - '2016-12-12 19:00:00', - ), - 'both, with offsets' => array( - array( - 'site' => '2016-12-12T18:00:00-01:00', - 'gmt' => '2016-12-12T20:00:00+01:00', + 'gmt' => '2001-01-01T00:00:00', ), '2016-12-12 14:00:00', '2016-12-12 19:00:00', @@ -1807,8 +1715,8 @@ public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new * @since 7.3.0 */ public function test_each_call_checks_the_current_user(): void { - $execute = $this->get_ability_callbacks()['execute_callback']; - $input = array( + $this->register_abilities(); + $input = array( 'data' => array( 'type' => 'post', 'title' => 'Twice', @@ -1816,14 +1724,14 @@ public function test_each_call_checks_the_current_user(): void { ); $this->login_as( 'author' ); - $first = $execute( $input ); + $first = $this->run_ability( $input ); $this->login_as( 'subscriber' ); $before = $this->snapshot(); - $second = $execute( $input ); + $second = $this->run_ability( $input ); $this->assertSame( self::$user_ids['author'], (int) $this->get_created_post( $first )->post_author, 'The first call should create a post for the author.' ); - $this->assert_error( $second, 'content_cannot_create', 403, 'The second call as a subscriber' ); + $this->assert_refused( $second, 'The second call as a subscriber' ); $this->assertSame( $before, $this->snapshot(), 'The second call should write nothing.' ); } @@ -1969,18 +1877,18 @@ public function test_user_with_only_the_create_capability_creates_a_draft_and_ca ) ); - $this->assert_error( $published, 'content_cannot_publish', 403, 'Publishing with only the create capability' ); + $this->assert_refused( $published, 'Publishing with only the create capability' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for the refused publish.' ); } /** - * Called directly, a user with only the edit capability of a type, and not its create - * capability, may not create a post of it. + * A user with only the edit capability of a type, and not its create capability, may not + * create a post of it. * * @ticket 66706 * @since 7.3.0 */ - public function test_direct_call_with_only_the_edit_capability_cannot_create(): void { + public function test_user_with_only_the_edit_capability_cannot_create(): void { $this->register_type_with_its_own_create_capability(); $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); @@ -1989,10 +1897,10 @@ public function test_direct_call_with_only_the_edit_capability_cannot_create(): $this->assertFalse( current_user_can( 'create_cw_books' ), 'Precondition: the user should not have the create capability.' ); - $execute = $this->get_ability_callbacks()['execute_callback']; + $this->register_abilities(); $before = $this->snapshot(); - $result = $execute( + $result = $this->run_ability( array( 'data' => array( 'type' => 'cw_book', @@ -2001,7 +1909,7 @@ public function test_direct_call_with_only_the_edit_capability_cannot_create(): ) ); - $this->assert_error( $result, 'content_cannot_create', 403, 'A direct call with only the edit capability' ); + $this->assert_refused( $result, 'A user with only the edit capability' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -2023,23 +1931,22 @@ public function test_editor_of_the_first_site_only_is_refused_on_the_second_site $first_before = $this->snapshot(); switch_to_blog( $blog_id ); - $callbacks = $this->get_ability_callbacks(); - $input = array( - 'data' => array( - 'type' => 'post', - 'title' => 'Second site', - ), - ); + $this->register_abilities(); $second_before = $this->snapshot(); - $regular = $this->run_ability( $input ); - $direct = $callbacks['execute_callback']( $input ); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Second site', + ), + ) + ); $second_after = $this->snapshot(); restore_current_blog(); - $this->assert_refused( $regular, 'The regular call on the second site' ); - $this->assert_error( $direct, 'content_cannot_create', 403, 'A direct call on the second site' ); + $this->assert_refused( $result, 'A call on the second site' ); $this->assertSame( $second_before, $second_after, 'Nothing should be written on the second site.' ); $this->assertSame( $first_before, $this->snapshot(), 'Nothing should be written on the first site.' ); } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index 4c29195c5f201..b0defcd7b898c 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -82,19 +82,6 @@ private function delete_input( int $post_id, ?bool $permanent = null ): array { return $input; } - /** - * Asserts that a result is the 403 delete error. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $context What the call was, for the assertion messages. - */ - protected function assert_cannot_write( $result, string $context ): void { - $this->assert_error( $result, 'content_cannot_delete', 403, $context ); - $this->assertSame( 'Sorry, you are not allowed to delete this post.', $result->get_error_message(), "{$context}: the message should be the delete message." ); - } - /** * Asserts that a post was moved to the Trash and the result says so. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index 45751bcddbe81..17be73f3ac485 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -226,19 +226,6 @@ protected function assert_data_written( $result, ?int $post_id ): WP_Post { return $this->assert_updated( $result, (int) $post_id ); } - /** - * Asserts that a result is the 403 edit error. - * - * @since 7.3.0 - * - * @param mixed $result The ability result. - * @param string $context What the call was, for the assertion messages. - */ - protected function assert_cannot_write( $result, string $context ): void { - $this->assert_error( $result, 'content_cannot_edit', 403, $context ); - $this->assertSame( 'Sorry, you are not allowed to edit this post.', $result->get_error_message(), "{$context}: the message should be the edit message." ); - } - /** * Creates a draft with a floating date, stored in the site's timezone with no GMT date. * @@ -312,7 +299,7 @@ public function test_input_schema(): void { $this->assertArrayNotHasKey( 'required', $data, 'The data should require no attribute.' ); $this->assertSame( array( 'title', 'content', 'excerpt', 'status', 'slug', 'date', 'author', 'parent' ), array_keys( $data['properties'] ), 'The data should take the allowed attributes and no post type.' ); $this->assertSame( array( 'publish', 'future', 'draft', 'pending', 'private' ), $data['properties']['status']['enum'], 'The status should list the statuses that are not internal.' ); - $this->assertSame( 1, $data['properties']['slug']['minLength'], 'The slug should not be empty.' ); + $this->assertArrayNotHasKey( 'minLength', $data['properties']['slug'], 'The slug should take an empty string, so the empty slug of a draft can be sent back.' ); $this->assertSame( 1, $data['properties']['author']['minimum'], 'The author should be at least 1.' ); $this->assertSame( 0, $data['properties']['parent']['minimum'], 'The parent should be at least 0.' ); $this->assertSame( 1, $data['properties']['date']['minProperties'], 'The date should hold at least one value.' ); @@ -497,46 +484,6 @@ public function test_update_matrix( string $role, string $owner, string $status, $this->assertSame( array( 'rendered' => 'Matrix update' ), $result['title'], 'The new title should be returned.' ); } - /** - * Returns `data` values that are invalid in some way. - * - * @return array}> The data. - */ - public static function data_invalid_data(): array { - return array( - 'parent on a post' => array( array( 'parent' => 1 ) ), - 'trash' => array( array( 'status' => 'trash' ) ), - 'private without publish_posts' => array( array( 'status' => 'private' ) ), - 'slug that sanitizes to nothing' => array( array( 'slug' => '!!!' ) ), - 'missing author' => array( array( 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ), - 'title that is a list' => array( array( 'title' => array( 'x' ) ) ), - 'invalid date' => array( array( 'date' => array( 'site' => 'not a date' ) ) ), - 'empty data' => array( array() ), - ); - } - - /** - * Called directly by a user who may not edit the post, the call fails on the capability before - * anything about the attributes is checked. - * - * @ticket 66706 - * @dataProvider data_invalid_data - * @since 7.3.0 - * - * @param array $data The data. - */ - public function test_direct_call_without_edit_post_fails_before_the_attributes( array $data ): void { - $post_id = self::$post_ids['admin_post']; - - $this->login_as( 'contributor' ); - $execute = $this->get_ability_callbacks()['execute_callback']; - - $before = $this->snapshot( $post_id ); - - $this->assert_cannot_write( $execute( $this->update_input( $post_id, $data ) ), 'A direct call without edit_post' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * Returns keys that are not part of the input, and where they are sent. * @@ -587,20 +534,17 @@ public function test_status_that_can_never_be_set_fails_input_validation( string * Returns status changes that a contributor, and an author denied `publish_posts`, may not * make on their own draft. * - * @return array The cases. + * @return array The cases. */ public static function data_status_changes_without_publish_posts(): array { - $publish_message = 'Sorry, you are not allowed to publish posts in this post type.'; - $private_message = 'Sorry, you are not allowed to create private posts in this post type.'; - return array( - 'publish by a contributor' => array( 'contributor', 'publish', $publish_message ), - 'future by a contributor' => array( 'contributor', 'future', $publish_message ), - 'private by a contributor' => array( 'contributor', 'private', $private_message ), - 'publish by an author denied publish_posts' => array( 'restricted_author', 'publish', $publish_message ), - 'future by an author denied publish_posts' => array( 'restricted_author', 'future', $publish_message ), - 'private by an author denied publish_posts' => array( 'restricted_author', 'private', $private_message ), - 'publish from the Trash by a contributor' => array( 'contributor', 'publish', $publish_message, 'trash_draft' ), + 'publish by a contributor' => array( 'contributor', 'publish' ), + 'future by a contributor' => array( 'contributor', 'future' ), + 'private by a contributor' => array( 'contributor', 'private' ), + 'publish by an author denied publish_posts' => array( 'restricted_author', 'publish' ), + 'future by an author denied publish_posts' => array( 'restricted_author', 'future' ), + 'private by an author denied publish_posts' => array( 'restricted_author', 'private' ), + 'publish from the Trash by a contributor' => array( 'contributor', 'publish', 'trash_draft' ), ); } @@ -612,12 +556,11 @@ public static function data_status_changes_without_publish_posts(): array { * @dataProvider data_status_changes_without_publish_posts * @since 7.3.0 * - * @param string $role The role or fixture name of the user, who owns the post. - * @param string $status The status. - * @param string $message The error message. - * @param string $stored Optional. The matrix status of the post. Default 'draft'. + * @param string $role The role or fixture name of the user, who owns the post. + * @param string $status The status. + * @param string $stored Optional. The matrix status of the post. Default 'draft'. */ - public function test_status_that_makes_a_post_visible_needs_publish_posts( string $role, string $status, string $message, string $stored = 'draft' ): void { + public function test_status_that_makes_a_post_visible_needs_publish_posts( string $role, string $status, string $stored = 'draft' ): void { $post_id = self::$post_ids[ "{$role}:{$stored}" ]; $this->login_as( $role ); @@ -633,11 +576,40 @@ public function test_status_that_makes_a_post_visible_needs_publish_posts( strin $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, $data ); - $this->assert_error( $result, 'content_cannot_publish', 403, 'A status that makes a post visible without publish_posts' ); - $this->assertSame( $message, $result->get_error_message(), 'The message should name what the role may not do.' ); + $this->assert_refused( $result, 'A status that makes a post visible without publish_posts' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } + /** + * Returns `data` values with a `status` or an `author` of the wrong type. + * + * @return array}> The data. + */ + public static function data_malformed_status_or_author(): array { + return array( + 'status that is a list' => array( array( 'status' => array( 'draft' ) ) ), + 'author that is a word' => array( array( 'author' => 'admin' ) ), + ); + } + + /** + * The permission callback returns false for a `status` or an `author` of the wrong type, + * which only a direct call can send. + * + * @ticket 66706 + * @dataProvider data_malformed_status_or_author + * @since 7.3.0 + * + * @param array $data The data. + */ + public function test_permission_callback_returns_false_for_a_malformed_status_or_author( array $data ): void { + $this->login_as( 'administrator' ); + + $permission = $this->get_ability_callbacks()['permission_callback']; + + $this->assertFalse( $permission( $this->update_input( self::$post_ids['admin_post'], $data ) ), 'The permission callback should return false.' ); + } + /** * A status equal to the stored status changes nothing and passes with no check. * @@ -736,8 +708,8 @@ public function test_status_change_is_stored( string $role, string $stored, stri } /** - * A user without `edit_others_posts` gets the same error for an existing and a missing - * author, so the error never tells whether a user exists. + * The permission callback refuses a user without `edit_others_posts` who names another + * author, existing or not, so the refusal never tells whether a user exists. * * @ticket 66706 * @dataProvider data_authors_named_without_edit_others_posts @@ -754,20 +726,7 @@ public function test_naming_another_author_without_edit_others_posts_is_refused( $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'author' => self::$user_ids[ $author ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $this->assertWPError( $result, 'Naming another author should fail.' ); - $this->assertSame( - array( - 'code' => 'content_cannot_edit_others', - 'message' => 'Sorry, you are not allowed to update posts as this user.', - 'data' => array( 'status' => 403 ), - ), - array( - 'code' => $result->get_error_code(), - 'message' => $result->get_error_message(), - 'data' => $result->get_error_data(), - ), - 'The error should be the same whether the user exists or not.' - ); + $this->assert_refused( $result, 'Naming another author' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -833,36 +792,24 @@ public function test_author_equal_to_the_stored_author_passes_with_no_check(): v */ public static function data_invalid_attributes(): array { return array( - 'missing author named with edit_others_posts' => array( + 'missing author named with edit_others_posts' => array( 'editor', array( 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ), 'author', 'The author must be an existing user of this site.', ), - 'slug that sanitizes to nothing' => array( + 'slug that sanitizes to nothing' => array( 'administrator', array( 'slug' => '!!!' ), 'slug', 'The slug must contain characters that are valid in a slug.', ), - // Intentionally strict: dates that name different times are an error instead of one of them winning. - 'site and GMT dates that name different times' => array( - 'editor', - array( - 'date' => array( - 'site' => '2017-01-01T10:00:00', - 'gmt' => '2001-01-01T00:00:00', - ), - ), - 'date', - 'The `site` and `gmt` dates must name the same time.', - ), ); } /** - * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing - * and site and GMT dates that name different times are invalid attributes. + * A missing author named by a user with `edit_others_posts` and a slug that sanitizes to + * nothing are invalid attributes. * * @ticket 66706 * @dataProvider data_invalid_attributes @@ -1141,6 +1088,74 @@ public static function data_slugs_of_pending_posts(): array { ); } + /** + * Returns posts with an empty slug and the role of their author. + * + * @return array The role and the post, as a fixture name. + */ + public static function data_posts_with_an_empty_slug(): array { + return array( + 'draft' => array( 'author', 'author:draft' ), + 'pending post' => array( 'contributor', 'contributor:pending' ), + ); + } + + /** + * The empty slug of a draft or a pending post, as the query returns it, can be sent back, + * also by a user who may not publish the post. + * + * @ticket 66706 + * @dataProvider data_posts_with_an_empty_slug + * @since 7.3.0 + * + * @param string $role The role of the author of the post. + * @param string $post The post, as a fixture name. + */ + public function test_empty_slug_of_a_post_without_a_slug_can_be_sent_back( string $role, string $post ): void { + $post_id = self::$post_ids[ $post ]; + + $this->assertSame( '', get_post( $post_id )->post_name, 'Precondition: the post should have no slug.' ); + + $this->login_as( $role ); + $this->register_abilities(); + + $result = $this->update_post( + $post_id, + array( + 'title' => 'Round trip', + 'slug' => '', + ), + array( 'slug' ) + ); + + $stored = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'Round trip', $stored->post_title, 'The title should be stored.' ); + $this->assertSame( '', $stored->post_name, 'The post should still have no slug.' ); + $this->assertSame( '', $result['slug'], 'The empty slug should be returned.' ); + } + + /** + * An empty slug sent for a post that has a slug is an invalid slug. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_empty_slug_of_a_post_with_a_slug_is_an_invalid_slug(): void { + $post_id = self::$post_ids['author:publish']; + + $this->assertNotSame( '', get_post( $post_id )->post_name, 'Precondition: the post should have a slug.' ); + + $this->login_as( 'author' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'slug' => '' ) ); + + $this->assert_invalid_attribute( $result, 'slug', 'An empty slug', 'The slug must contain characters that are valid in a slug.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + /** * A user who may not publish the post cannot set the slug of a pending post. * @@ -1381,15 +1396,74 @@ public static function data_new_dates(): array { 'site' => array( array( 'site' => '2017-01-01T10:00:00' ) ), 'gmt' => array( array( 'gmt' => '2017-01-01T15:00:00' ) ), 'site with an offset' => array( array( 'site' => '2017-01-01T16:00:00+01:00' ) ), - 'both, the same time' => array( + 'both, the site wins' => array( array( 'site' => '2017-01-01T10:00:00', - 'gmt' => '2017-01-01T15:00:00', + 'gmt' => '2001-01-01T00:00:00', ), ), ); } + /** + * The date that core/content-query returns can be sent back after the site's timezone + * changed, when its `site` and `gmt` dates name different times: `site` wins, and as it + * equals the stored local date, the stored dates are kept. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_queried_date_can_be_sent_back_after_a_timezone_change(): void { + $post_id = self::factory()->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_status' => 'publish', + 'post_date' => '2017-01-01 10:00:00', + 'post_date_gmt' => '2017-01-01 10:00:00', + ) + ); + + update_option( 'timezone_string', 'Europe/Paris' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $queried = wp_get_ability( 'core/content-query' )->execute( + array( + 'where' => array( + 'type' => 'post', + 'ids' => array( $post_id ), + ), + 'fields' => array( 'date' ), + ) + ); + + $this->assertIsArray( $queried, 'The query should succeed.' ); + $date = $queried['items'][0]['date']; + $this->assertSame( + array( + 'site' => '2017-01-01T10:00:00+01:00', + 'gmt' => '2017-01-01T10:00:00+00:00', + ), + $date, + 'Precondition: the queried `site` and `gmt` dates should name different times.' + ); + + $result = $this->update_post( + $post_id, + array( + 'title' => 'Round trip', + 'date' => $date, + ) + ); + + $post = $this->assert_updated( $result, $post_id ); + + $this->assertSame( 'Round trip', $post->post_title, 'The title should be stored.' ); + $this->assertSame( '2017-01-01 10:00:00', $post->post_date, 'The local date should be kept.' ); + $this->assertSame( '2017-01-01 10:00:00', $post->post_date_gmt, 'The GMT date should be kept.' ); + } + /** * A date that differs from the stored one is stored as sent, local and GMT. * @@ -2034,8 +2108,7 @@ public function test_naming_the_status_a_date_leads_to_needs_publish_posts( stri ) ); - $this->assert_error( $result, 'content_cannot_publish', 403, 'A published or scheduled post moved to the other status without publish_posts' ); - $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $result->get_error_message(), 'The message should be the publish message.' ); + $this->assert_refused( $result, 'A published or scheduled post moved to the other status without publish_posts' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -2107,8 +2180,7 @@ public function test_edit_post_granted_on_one_post_allows_only_editing_that_post $before = $this->snapshot( $post_id ); $publish = $this->update_post( $post_id, array( 'status' => 'publish' ) ); - $this->assert_error( $publish, 'content_cannot_publish', 403, 'Publishing the granted post' ); - $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $publish->get_error_message(), 'The message should be the publish message.' ); + $this->assert_refused( $publish, 'Publishing the granted post' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for the refused publish.' ); } @@ -2224,8 +2296,7 @@ public static function data_custom_visible_statuses_by_stored_status(): array { } /** - * A contributor may not set a custom public or private status on their own post, and the - * refusal uses the general message. + * A contributor may not set a custom public or private status on their own post. * * @ticket 66706 * @dataProvider data_custom_visible_statuses_by_stored_status @@ -2244,8 +2315,7 @@ public function test_contributor_cannot_set_a_custom_visible_status( string $sta $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'status' => $status ) ); - $this->assert_error( $result, 'content_cannot_publish', 403, 'A contributor setting a custom visible status' ); - $this->assertSame( 'Sorry, you are not allowed to publish posts in this post type.', $result->get_error_message(), 'A custom status, also a private one, should get the general message.' ); + $this->assert_refused( $result, 'A contributor setting a custom visible status' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } From 2bd593eb30a8b9efc1240439861750754743a9ad Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 12:08:25 +0000 Subject: [PATCH 19/24] Abilities API: Check the content write parent and pending slug in the permission callbacks. A missing or unreadable parent and the slug of a pending post that the user cannot publish are now refused by the permission callbacks, the same way as the status and author. --- .../abilities/class-wp-abilities-content.php | 68 ++++++---- .../wpRegisterCoreContentCreateAbility.php | 119 ++++++++++++++---- .../wpRegisterCoreContentUpdateAbility.php | 71 ++++++++--- 3 files changed, 193 insertions(+), 65 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 87d0a0533ee9d..b1dc02d0a1533 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -353,12 +353,15 @@ private function check_update_permission( $input ): bool { } /** - * Checks the capabilities that the sent `status` and `author` need. + * Checks the capabilities that the sent `status`, `author`, `parent` and `slug` need. * * A status that makes the post visible needs the capability to publish posts of the type, * and an author other than the current user needs the capability to edit the posts of - * others. A status or an author that keeps the stored value needs neither. Every other - * rule on these attributes is checked by the execute callbacks. + * others. A parent must be a post the current user can read, of a type exposed to + * abilities; a missing post is refused the same way, so the refusal never tells whether a + * post exists. The slug of a post that will be pending needs the capability to publish the + * post. A value that keeps the stored one needs none of these. Every other rule on these + * attributes is checked by the execute callbacks. * * @since 7.3.0 * @@ -399,6 +402,41 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object } } + // A parent sent for a post type that is not hierarchical is refused by the execute callbacks. + if ( array_key_exists( 'parent', $data ) && is_post_type_hierarchical( $post_type_object->name ) ) { + $parent_id = $this->parse_filter_int( $data['parent'], 0 ); + if ( null === $parent_id ) { + return false; + } + + $is_unchanged = $post && (int) $post->post_parent === $parent_id; + if ( ! $is_unchanged && 0 !== $parent_id ) { + $parent = get_post( $parent_id ); + if ( ! $parent instanceof WP_Post || ! $this->check_read_permission( $parent ) ) { + return false; + } + } + } + + if ( array_key_exists( 'slug', $data ) ) { + // Without a sent status, the save keeps the stored status, or defaults to `draft`. + $status = $data['status'] ?? ( $post ? $post->post_status : 'draft' ); + $is_unchanged = $post && $post->post_name === $data['slug']; + + /* + * Intentionally strict: the save drops the slug of a pending post when the user + * cannot publish it, so the slug is refused instead of being dropped without notice. + */ + if ( 'pending' === $status && ! $is_unchanged ) { + $can_publish = $post + ? current_user_can( 'publish_post', $post->ID ) + : current_user_can( $post_type_object->cap->publish_posts ); + if ( ! $can_publish ) { + return false; + } + } + } + return true; } @@ -1258,7 +1296,8 @@ private function prepare_author( array $data, ?WP_Post $post ) { /** * Checks the sent `parent`. * - * Every failure returns the same error, so it never tells whether a post ID exists. + * The permission callbacks refuse a parent that is missing or that the current user cannot + * read, so the errors here never tell whether a post ID exists. * * @since 7.3.0 * @@ -1283,14 +1322,14 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post return 0; } - $reason = __( 'The parent must be 0, or a readable post of the same post type that is neither the post itself nor one of its descendants.' ); + $reason = __( 'The parent must be 0, or a post of the same post type that is neither the post itself nor one of its descendants.' ); /* - * Intentionally strict: a parent must be readable, of the same type and free of - * loops; it is never silently reset. + * Intentionally strict: a parent must be of the same type and free of loops; it is + * never silently reset. */ $parent = get_post( $parent_id ); - if ( ! $parent instanceof WP_Post || $post_type !== $parent->post_type || ! $this->check_read_permission( $parent ) ) { + if ( ! $parent instanceof WP_Post || $post_type !== $parent->post_type ) { return $this->invalid_attribute_error( 'data[parent]', $reason ); } @@ -1383,19 +1422,6 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_ return null; } - /* - * Intentionally strict: the save drops the slug of a pending post when the user cannot - * publish it, so the slug is an error here instead of being dropped without notice. - */ - if ( 'pending' === $status ) { - $can_publish = $post - ? current_user_can( 'publish_post', $post->ID ) - : current_user_can( $post_type_object->cap->publish_posts ); - if ( ! $can_publish ) { - return $this->invalid_attribute_error( 'data[slug]', __( 'The slug of a pending post can only be set by a user who can publish the post.' ) ); - } - } - $slug = sanitize_title( $data['slug'] ); // Intentionally strict: a slug that sanitizes to nothing is an error. diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index 72d5c225d35bd..d0f6430778f6d 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -496,6 +496,14 @@ public static function data_inputs_refused_by_the_permission_callback(): array { ), ), ), + 'parent that is a word' => array( + array( + 'data' => array( + 'type' => 'page', + 'parent' => 'top', + ), + ), + ), ); } @@ -905,7 +913,7 @@ public function test_naming_an_author_the_role_may_name( string $role, string $a */ public static function data_invalid_attributes(): array { return array( - 'missing author named with edit_others_posts' => array( + 'missing author named with edit_others_posts' => array( 'editor', array( 'type' => 'post', @@ -915,7 +923,7 @@ public static function data_invalid_attributes(): array { 'author', 'The author must be an existing user of this site.', ), - 'slug that sanitizes to nothing' => array( + 'slug that sanitizes to nothing' => array( 'administrator', array( 'type' => 'post', @@ -925,7 +933,7 @@ public static function data_invalid_attributes(): array { 'slug', 'The slug must contain characters that are valid in a slug.', ), - 'empty slug' => array( + 'empty slug' => array( 'administrator', array( 'type' => 'post', @@ -935,24 +943,12 @@ public static function data_invalid_attributes(): array { 'slug', 'The slug must contain characters that are valid in a slug.', ), - // Intentionally strict: the save would drop the slug, so it is an error instead of being dropped without notice. - 'slug of a pending post without publish_posts' => array( - 'contributor', - array( - 'type' => 'post', - 'title' => 'Pending slug', - 'status' => 'pending', - 'slug' => 'pending-slug', - ), - 'slug', - 'The slug of a pending post can only be set by a user who can publish the post.', - ), ); } /** - * A missing author named by a user with `edit_others_posts`, a slug that sanitizes to nothing - * and the slug of a pending post set by a user who may not publish posts are invalid attributes. + * A missing author named by a user with `edit_others_posts` and a slug that sanitizes to + * nothing are invalid attributes. * * @ticket 66706 * @dataProvider data_invalid_attributes @@ -1051,30 +1047,29 @@ public function test_naming_a_non_member_without_edit_others_posts_is_refused_li * * @return array The parents, as fixture names. */ - public static function data_parents_that_cannot_be_used(): array { + public static function data_parents_refused_by_the_permission_callback(): array { return array( 'missing post' => array( 'missing' ), - 'post of another type' => array( 'published_post' ), 'page the user may not read' => array( 'editor_draft_page' ), 'revision of a page' => array( 'page_revision' ), ); } /** - * A missing parent, a parent of another type, a parent the user may not read and a revision - * give one identical error, so the error never tells whether a post exists. + * A missing parent, a parent the user may not read and a revision, whose type is not exposed + * to abilities, are refused by the permission callback with one identical error, so the + * refusal never tells whether a post exists. * * @ticket 66706 - * @dataProvider data_parents_that_cannot_be_used + * @dataProvider data_parents_refused_by_the_permission_callback * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. */ - public function test_parent_that_cannot_be_used_gives_one_identical_error( string $parent_key ): void { + public function test_parent_that_is_missing_or_unreadable_is_refused( string $parent_key ): void { $this->login_as( 'page_writer' ); $this->register_abilities(); - $this->assertTrue( current_user_can( get_post_type_object( 'page' )->cap->create_posts ), 'Precondition: the user should be able to create pages.' ); $this->assertFalse( current_user_can( 'read_post', self::$post_ids['editor_draft_page'] ), 'Precondition: the user should not be able to read the draft page of the editor.' ); $create = function ( int $parent_id ) { @@ -1094,9 +1089,50 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin $reference = $create( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER + 1 ); $result = $create( self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); - $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); - $this->assertWPError( $reference, 'Another missing parent should fail.' ); - $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for another missing parent.' ); + $this->assert_refused( $reference, 'Another missing parent' ); + $this->assert_refused( $result, 'A missing or unreadable parent' ); + $this->assertSame( $reference->get_error_message(), $result->get_error_message(), 'The refusal should be the same as for another missing parent.' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + + /** + * Returns readable parents that a new page may not have. + * + * @return array The parents, as fixture names. + */ + public static function data_parents_that_cannot_be_used(): array { + return array( + 'post of another type' => array( 'published_post' ), + ); + } + + /** + * A readable parent of another type is an invalid parent. + * + * @ticket 66706 + * @dataProvider data_parents_that_cannot_be_used + * @since 7.3.0 + * + * @param string $parent_key The parent, as a fixture name. + */ + public function test_parent_that_cannot_be_used_is_an_invalid_parent( string $parent_key ): void { + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $this->assertTrue( current_user_can( 'read_post', self::$post_ids[ $parent_key ] ), 'Precondition: the user should be able to read the parent.' ); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Child', + 'parent' => self::$post_ids[ $parent_key ], + ), + ) + ); + + $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used', 'The parent must be 0, or a post of the same post type that is neither the post itself nor one of its descendants.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1339,6 +1375,35 @@ public function test_slug_is_sanitized_and_stored( string $slug, string $expecte $this->assertSame( $expected, $result['slug'], 'The stored slug should be returned.' ); } + /** + * A user who may not publish posts cannot set the slug of a pending post. + * + * Intentionally strict: the save would drop the slug, so the call is refused instead of the + * slug being dropped without notice. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_slug_of_a_pending_post_is_refused_without_publish_posts(): void { + $this->login_as( 'contributor' ); + $this->register_abilities(); + + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'post', + 'title' => 'Pending slug', + 'status' => 'pending', + 'slug' => 'pending-slug', + ), + ) + ); + + $this->assert_refused( $result, 'A slug on a pending post without publish_posts' ); + $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + } + /** * A user who may publish posts sets the slug of a pending post. * diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index 17be73f3ac485..172d0d7147d70 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -862,29 +862,26 @@ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_inva * * @return array The parents, as fixture names. */ - public static function data_parents_that_cannot_be_used(): array { + public static function data_parents_refused_by_the_permission_callback(): array { return array( 'missing post' => array( 'missing' ), - 'post of another type' => array( 'admin_post' ), 'page the user may not read' => array( 'editor_draft_page' ), - 'the page itself' => array( 'writer_page' ), - 'a child' => array( 'writer_child' ), - 'a grandchild' => array( 'writer_grandchild' ), - 'revision' => array( 'revision' ), + 'revision of a page' => array( 'revision' ), ); } /** - * A missing parent, a parent of another type, a parent the user may not read, a revision and a - * parent that would make a loop give one identical error. + * A missing parent, a parent the user may not read and a revision, whose type is not exposed + * to abilities, are refused by the permission callback with one identical error, so the + * refusal never tells whether a post exists. * * @ticket 66706 - * @dataProvider data_parents_that_cannot_be_used + * @dataProvider data_parents_refused_by_the_permission_callback * @since 7.3.0 * * @param string $parent_key The parent, as a fixture name. */ - public function test_parent_that_cannot_be_used_gives_one_identical_error( string $parent_key ): void { + public function test_parent_that_is_missing_or_unreadable_is_refused( string $parent_key ): void { $post_id = self::$post_ids['writer_page']; $this->login_as( 'page_writer' ); @@ -898,9 +895,49 @@ public function test_parent_that_cannot_be_used_gives_one_identical_error( strin $reference = $this->update_post( $post_id, array( 'parent' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER + 1 ) ); $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $this->assertWPError( $reference, 'The reference call with a missing parent should fail.' ); - $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used' ); - $this->assertSame( $reference->get_error_data(), $result->get_error_data(), 'The error data, with its reason, should be the same as for a missing parent.' ); + $this->assert_refused( $reference, 'Another missing parent' ); + $this->assert_refused( $result, 'A missing or unreadable parent' ); + $this->assertSame( $reference->get_error_message(), $result->get_error_message(), 'The refusal should be the same as for a missing parent.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Returns readable parents that a page may not have. + * + * @return array The parents, as fixture names. + */ + public static function data_parents_that_cannot_be_used(): array { + return array( + 'post of another type' => array( 'admin_post' ), + 'the page itself' => array( 'writer_page' ), + 'a child' => array( 'writer_child' ), + 'a grandchild' => array( 'writer_grandchild' ), + ); + } + + /** + * A readable parent of another type and a parent that would make a loop are invalid parents + * with one identical error. + * + * @ticket 66706 + * @dataProvider data_parents_that_cannot_be_used + * @since 7.3.0 + * + * @param string $parent_key The parent, as a fixture name. + */ + public function test_parent_that_cannot_be_used_is_an_invalid_parent( string $parent_key ): void { + $post_id = self::$post_ids['writer_page']; + + $this->login_as( 'page_writer' ); + $this->register_abilities(); + + $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should be able to edit the page.' ); + $this->assertTrue( current_user_can( 'read_post', self::$post_ids[ $parent_key ] ), 'Precondition: the user should be able to read the parent.' ); + + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ) ); + + $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used', 'The parent must be 0, or a post of the same post type that is neither the post itself nor one of its descendants.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -1159,8 +1196,8 @@ public function test_empty_slug_of_a_post_with_a_slug_is_an_invalid_slug(): void /** * A user who may not publish the post cannot set the slug of a pending post. * - * Intentionally strict: the save would drop the slug, so it is an error instead of being - * dropped without notice. + * Intentionally strict: the save would drop the slug, so the call is refused instead of the + * slug being dropped without notice. * * @ticket 66706 * @dataProvider data_slugs_of_pending_posts @@ -1169,7 +1206,7 @@ public function test_empty_slug_of_a_post_with_a_slug_is_an_invalid_slug(): void * @param string $post The post, as a fixture name. * @param array $data The `data` input. */ - public function test_slug_of_a_pending_post_is_rejected_without_publish_posts( string $post, array $data ): void { + public function test_slug_of_a_pending_post_is_refused_without_publish_posts( string $post, array $data ): void { $post_id = self::$post_ids[ $post ]; $this->login_as( 'contributor' ); @@ -1178,7 +1215,7 @@ public function test_slug_of_a_pending_post_is_rejected_without_publish_posts( s $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, $data ); - $this->assert_invalid_attribute( $result, 'slug', 'A slug on a pending post without publish_posts', 'The slug of a pending post can only be set by a user who can publish the post.' ); + $this->assert_refused( $result, 'A slug on a pending post without publish_posts' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written for a slug that cannot be stored.' ); } From 118c687e9b2f4dc16c81341f7270e13ddde5eff7 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 12:35:26 +0000 Subject: [PATCH 20/24] Abilities API: Correct the content ability docblocks and the pending slug comment. --- .../abilities/class-wp-abilities-content.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index b1dc02d0a1533..cf06f8e6bb002 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -18,8 +18,9 @@ * edit, and the author's name and slug only for authors the current user may see. * * Also registers `core/content-create`, `core/content-update`, and `core/content-delete`, - * which create, update, and trash or delete one post of such a post type, and return the - * written post in the same shape as a `core/content-query` item. + * which create, update, and trash or delete one post of such a post type. Create and update + * return the written post in the same shape as a `core/content-query` item, and delete + * returns whether the post was trashed or deleted. * * Unlike the core abilities registered directly in wp_register_core_abilities() as * self-contained closures, the four content abilities live in one dedicated class because @@ -425,7 +426,9 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object /* * Intentionally strict: the save drops the slug of a pending post when the user - * cannot publish it, so the slug is refused instead of being dropped without notice. + * cannot publish it, so a changed slug is refused instead of being dropped without + * notice. The stored slug sent back is accepted as unchanged, although the save + * still drops it. */ if ( 'pending' === $status && ! $is_unchanged ) { $can_publish = $post @@ -1934,7 +1937,7 @@ private function get_field_paths(): array { } /** - * Builds the schema of the `fields` input, shared by all content abilities. + * Builds the schema of the `fields` input, shared by the query, create, and update abilities. * * @since 7.2.0 * @@ -2276,7 +2279,7 @@ private function get_content_delete_output_schema(): array { } /** - * Builds the output schema of a single post, shared by all content abilities. + * Builds the output schema of a single post, shared by the query, create, and update abilities. * * Only `id` is required, because it is always returned. The other fields are optional * because the `fields` input lets the caller request any subset, and a field is only From c7c7bfc804157103e93b3761c2c990cb44ad4795 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 12:35:26 +0000 Subject: [PATCH 21/24] Tests: Remove content write ability checks that cannot fail and fix test labels. --- .../trait-content-data-write-tests.php | 20 ++--- .../wpRegisterCoreContentCreateAbility.php | 84 ++++--------------- .../wpRegisterCoreContentUpdateAbility.php | 26 +++--- 3 files changed, 39 insertions(+), 91 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php b/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php index 42d0fc6ffa8c6..18c1bf98062eb 100644 --- a/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php +++ b/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php @@ -21,7 +21,7 @@ trait WP_Abilities_Content_Data_Write_Tests { * @param string $role The role, as a fixture name. * @return int|null The post ID, or null when the call creates a post. */ - abstract protected function data_write_post_id( string $role ): ?int; + abstract protected function get_write_post_id( string $role ): ?int; /** * Builds the input that writes the given data to a post of type `post`. @@ -33,7 +33,7 @@ abstract protected function data_write_post_id( string $role ): ?int; * @param list|null $fields Optional. The fields, or null for the default. Default null. * @return array The input. */ - abstract protected function data_write_input( ?int $post_id, array $data, ?array $fields = null ): array; + abstract protected function build_write_input( ?int $post_id, array $data, ?array $fields = null ): array; /** * Asserts that a result is the post item of a successful write and returns the stored post. @@ -56,14 +56,14 @@ abstract protected function assert_data_written( $result, ?int $post_id ): WP_Po * @param string $status The status. */ public function test_direct_call_rejects_a_status_that_can_never_be_set( string $status ): void { - $post_id = $this->data_write_post_id( 'administrator' ); + $post_id = $this->get_write_post_id( 'administrator' ); $this->login_as( 'administrator' ); $execute = $this->get_ability_callbacks()['execute_callback']; $before = $this->snapshot( $post_id ); $result = $execute( - $this->data_write_input( + $this->build_write_input( $post_id, array( 'title' => 'Unusable status', @@ -101,12 +101,12 @@ public static function data_statuses_reported_as_stored(): array { * @param string $expected The stored status. */ public function test_output_reports_the_stored_status( string $status, string $gmt, string $expected ): void { - $post_id = $this->data_write_post_id( 'editor' ); + $post_id = $this->get_write_post_id( 'editor' ); $this->login_as( 'editor' ); $this->register_abilities(); - $input = $this->data_write_input( + $input = $this->build_write_input( $post_id, array( 'title' => 'Scheduled', @@ -128,14 +128,14 @@ public function test_output_reports_the_stored_status( string $status, string $g * @since 7.3.0 */ public function test_empty_post_is_the_save_error(): void { - $post_id = $this->data_write_post_id( 'administrator' ); + $post_id = $this->get_write_post_id( 'administrator' ); $this->login_as( 'administrator' ); $this->register_abilities(); $before = $this->snapshot( $post_id ); $result = $this->run_ability( - $this->data_write_input( + $this->build_write_input( $post_id, array( 'title' => '', @@ -158,12 +158,12 @@ public function test_empty_post_is_the_save_error(): void { */ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip(): void { $text = 'He said "it\'s done" \\ C:\\path\\to\\file \\\\server – café, naïve, 日本語, emoji ✓'; - $post_id = $this->data_write_post_id( 'editor' ); + $post_id = $this->get_write_post_id( 'editor' ); $this->login_as( 'editor' ); $this->register_abilities(); - $input = $this->data_write_input( + $input = $this->build_write_input( $post_id, array( 'title' => $text, diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index d0f6430778f6d..b522f43a23baa 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -150,7 +150,7 @@ private function get_created_post( $result, string $context = 'The create call' * @param string $role The role, as a fixture name. * @return null No post ID. */ - protected function data_write_post_id( string $role ): ?int { + protected function get_write_post_id( string $role ): ?int { return null; } @@ -164,7 +164,7 @@ protected function data_write_post_id( string $role ): ?int { * @param list|null $fields Optional. The fields, or null for the default. Default null. * @return array The input. */ - protected function data_write_input( ?int $post_id, array $data, ?array $fields = null ): array { + protected function build_write_input( ?int $post_id, array $data, ?array $fields = null ): array { $input = array( 'data' => array_merge( array( 'type' => 'post' ), $data ) ); if ( null !== $fields ) { $input['fields'] = $fields; @@ -1002,46 +1002,6 @@ public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_inva $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * On multisite, a user without `edit_others_posts` who names a user who is not a member of the - * site gets the same error as for a missing user. - * - * @group ms-required - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_naming_a_non_member_without_edit_others_posts_is_refused_like_a_missing_user(): void { - $non_member = self::factory()->user->create( array( 'role' => 'author' ) ); - remove_user_from_blog( $non_member, get_current_blog_id() ); - - $this->login_as( 'author' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $errors = array(); - foreach ( array( - 'non-member' => $non_member, - 'missing' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, - ) as $key => $author ) { - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Ghost written', - 'author' => $author, - ), - ) - ); - $this->assertWPError( $result, "Naming the {$key} user should fail." ); - $errors[ $key ] = array( $result->get_error_code(), $result->get_error_message(), $result->get_error_data() ); - } - - $this->assertSame( 'ability_invalid_permissions', $errors['missing'][0], 'The permission callback should refuse the call.' ); - $this->assertSame( $errors['missing'], $errors['non-member'], 'The error should be the same for a non-member and a missing user.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for either user.' ); - } - /** * Returns parents that a page may not have. * @@ -1057,8 +1017,8 @@ public static function data_parents_refused_by_the_permission_callback(): array /** * A missing parent, a parent the user may not read and a revision, whose type is not exposed - * to abilities, are refused by the permission callback with one identical error, so the - * refusal never tells whether a post exists. + * to abilities, are refused by the permission callback, so the refusal never tells whether a + * post exists. * * @ticket 66706 * @dataProvider data_parents_refused_by_the_permission_callback @@ -1072,26 +1032,18 @@ public function test_parent_that_is_missing_or_unreadable_is_refused( string $pa $this->assertFalse( current_user_can( 'read_post', self::$post_ids['editor_draft_page'] ), 'Precondition: the user should not be able to read the draft page of the editor.' ); - $create = function ( int $parent_id ) { - return $this->run_ability( - array( - 'data' => array( - 'type' => 'page', - 'title' => 'Child', - 'parent' => $parent_id, - ), - ) - ); - }; - - // The reference is another missing ID, so the `missing post` row is compared with a different input too. - $before = $this->snapshot(); - $reference = $create( REST_TESTS_IMPOSSIBLY_HIGH_NUMBER + 1 ); - $result = $create( self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); + $before = $this->snapshot(); + $result = $this->run_ability( + array( + 'data' => array( + 'type' => 'page', + 'title' => 'Child', + 'parent' => self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, + ), + ) + ); - $this->assert_refused( $reference, 'Another missing parent' ); $this->assert_refused( $result, 'A missing or unreadable parent' ); - $this->assertSame( $reference->get_error_message(), $result->get_error_message(), 'The refusal should be the same as for another missing parent.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1308,12 +1260,12 @@ static function ( $query ) { } /** - * The after-insert hooks fire once, as in any regular save. + * The `wp_after_insert_post` action fires once, as in any regular save. * * @ticket 66706 * @since 7.3.0 */ - public function test_after_insert_hooks_fire_once(): void { + public function test_wp_after_insert_post_fires_once(): void { $this->login_as( 'administrator' ); $this->register_abilities(); @@ -1434,7 +1386,7 @@ public function test_slug_of_a_pending_post_is_stored_with_publish_posts(): void } /** - * Returns statuses for which the slug of a published post is made unique. + * Returns statuses for which the slug of a new post is made unique against another post. * * @return array The statuses, or null for the default. */ @@ -1768,7 +1720,7 @@ public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new $post = $this->get_created_post( $result ); $this->assertSame( 'publish', $post->post_status, 'The post should be published.' ); - $this->assertFalse( current_user_can( 'edit_post', $post->ID ), 'The user should not be able to edit the published post.' ); + $this->assertFalse( current_user_can( 'edit_post', $post->ID ), 'Precondition: The user should not be able to edit the published post.' ); $this->assertSame( array( 'rendered' => 'Published once' ), $result['title'], 'The title should be returned without its raw value.' ); $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index 172d0d7147d70..196c1d995d23b 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -195,7 +195,7 @@ private function assert_updated( $result, int $post_id ): WP_Post { * @param string $role The role, as a fixture name. * @return int The post ID. */ - protected function data_write_post_id( string $role ): ?int { + protected function get_write_post_id( string $role ): int { return self::$post_ids[ "{$role}:draft" ]; } @@ -209,7 +209,7 @@ protected function data_write_post_id( string $role ): ?int { * @param list|null $fields Optional. The fields, or null for the default. Default null. * @return array The input. */ - protected function data_write_input( ?int $post_id, array $data, ?array $fields = null ): array { + protected function build_write_input( ?int $post_id, array $data, ?array $fields = null ): array { return $this->update_input( (int) $post_id, $data, $fields ); } @@ -866,14 +866,14 @@ public static function data_parents_refused_by_the_permission_callback(): array return array( 'missing post' => array( 'missing' ), 'page the user may not read' => array( 'editor_draft_page' ), - 'revision of a page' => array( 'revision' ), + 'revision of a post' => array( 'revision' ), ); } /** * A missing parent, a parent the user may not read and a revision, whose type is not exposed - * to abilities, are refused by the permission callback with one identical error, so the - * refusal never tells whether a post exists. + * to abilities, are refused by the permission callback, so the refusal never tells whether a + * post exists. * * @ticket 66706 * @dataProvider data_parents_refused_by_the_permission_callback @@ -890,14 +890,10 @@ public function test_parent_that_is_missing_or_unreadable_is_refused( string $pa $this->assertTrue( current_user_can( 'edit_post', $post_id ), 'Precondition: the user should be able to edit the page.' ); $this->assertFalse( current_user_can( 'read_post', self::$post_ids['editor_draft_page'] ), 'Precondition: the user should not be able to read the draft page of the editor.' ); - // The reference names another missing ID than the missing post row, so no row compares a call with itself. - $before = $this->snapshot( $post_id ); - $reference = $this->update_post( $post_id, array( 'parent' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER + 1 ) ); - $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); + $before = $this->snapshot( $post_id ); + $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $this->assert_refused( $reference, 'Another missing parent' ); $this->assert_refused( $result, 'A missing or unreadable parent' ); - $this->assertSame( $reference->get_error_message(), $result->get_error_message(), 'The refusal should be the same as for a missing parent.' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -1737,7 +1733,7 @@ public function test_raw_fields_are_left_out_when_the_writer_can_no_longer_edit_ $result = $this->update_post( $post_id, array( 'status' => 'publish' ), array( 'title', 'content.raw', 'content.rendered' ) ); $this->assertSame( 'publish', $this->assert_updated( $result, $post_id )->post_status, 'The post should be published.' ); - $this->assertFalse( current_user_can( 'edit_post', $post_id ), 'The user should not be able to edit the published post.' ); + $this->assertFalse( current_user_can( 'edit_post', $post_id ), 'Precondition: The user should not be able to edit the published post.' ); $this->assertSame( array( 'rendered' => 'Publish once' ), $result['title'], 'The title should be returned without its raw value.' ); $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); } @@ -1882,15 +1878,15 @@ public function test_stale_page_template_does_not_fail_an_update(): void { } /** - * The after-insert hooks fire once for the updated post, as in any regular save. + * The `wp_after_insert_post` action fires once for the updated post, as in any regular save. * - * A save that stores a revision also fires the hooks for the revision, so only the fires + * A save that stores a revision also fires the action for the revision, so only the fires * for the updated post are counted, and a regular save of an identical post is the reference. * * @ticket 66706 * @since 7.3.0 */ - public function test_after_insert_hooks_fire_once(): void { + public function test_wp_after_insert_post_fires_once(): void { $post_id = self::$post_ids['administrator:draft']; $reference_id = self::factory()->post->create( array( From d8870d9e84637364003104a1247427cb5ef5894c Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 13:10:25 +0000 Subject: [PATCH 22/24] Abilities API: Look up the post once in the content update permission callback and loop the content write steps. The update and delete permission callbacks now share one check that reads the requested post once, and `prepare_post_data()` runs its status, author, parent, date and slug steps in one loop, each returning the fields to save. --- .../abilities/class-wp-abilities-content.php | 162 ++++++++---------- 1 file changed, 68 insertions(+), 94 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index cf06f8e6bb002..67430fd347493 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -223,7 +223,7 @@ private function register_content_write_abilities( array $post_types, array $sta return $this->execute_content_update( $input ); }, 'permission_callback' => function ( $input ): bool { - return $this->check_update_permission( $input ); + return $this->check_post_permission( $input, 'edit_post' ); }, 'meta' => array( 'annotations' => array( @@ -326,33 +326,6 @@ private function check_create_permission( $input ): bool { && $this->can_set_attributes( $data, $post_type_object, null ); } - /** - * Permission callback for the `core/content-update` ability. - * - * Requires what {@see self::check_post_permission()} requires for `edit_post`, and the - * capabilities that the sent attributes need, see {@see self::can_set_attributes()}. - * - * @since 7.3.0 - * - * @param mixed $input The ability input, which is not prepared when - * WP_Ability::check_permissions() is called directly. - * @return bool True if the request may proceed, false otherwise. - */ - private function check_update_permission( $input ): bool { - $input = rest_sanitize_object( $input ); - if ( ! $this->check_post_permission( $input, 'edit_post' ) ) { - return false; - } - - $post = $this->get_requested_post( $this->get_input_object( $input, 'where' ) ?? array() ); - if ( ! $post ) { - return true; - } - - // get_requested_post() only returns posts of an exposed post type. - return $this->can_set_attributes( $this->get_input_object( $input, 'data' ) ?? array(), get_post_type_object( $post->post_type ), $post ); - } - /** * Checks the capabilities that the sent `status`, `author`, `parent` and `slug` need. * @@ -444,19 +417,20 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object } /** - * Permission callback for the `core/content-delete` ability, also used by the one of - * `core/content-update`. + * Permission callback for the `core/content-update` and `core/content-delete` abilities. * * Requires a logged-in user, a `where.type` that is exposed to abilities, and the given - * capability on the post that `where` names. When `where` names no post of that type, - * such as a missing ID or the ID of a post of another type, the request is let through - * so that the execute callback can report it as not found. + * capability on the post that `where` names. An update with `edit_post` also requires the + * capabilities that the sent attributes need, see {@see self::can_set_attributes()}. When + * `where` names no post of that type, such as a missing ID or the ID of a post of another + * type, the request is let through so that the execute callback can report it as not found. * * @since 7.3.0 * * @param mixed $input The ability input, which is not prepared when * WP_Ability::check_permissions() is called directly. - * @param string $capability The capability required on the post: `edit_post` or `delete_post`. + * @param string $capability The capability required on the post: `edit_post` for an + * update, or `delete_post` for a delete. * @return bool True if the request may proceed, false otherwise. */ private function check_post_permission( $input, string $capability ): bool { @@ -464,14 +438,24 @@ private function check_post_permission( $input, string $capability ): bool { return false; } - $where = $this->get_input_object( rest_sanitize_object( $input ), 'where' ) ?? array(); + $input = rest_sanitize_object( $input ); + $where = $this->get_input_object( $input, 'where' ) ?? array(); if ( ! $this->get_exposed_post_type( $where['type'] ?? null ) ) { return false; } $post = $this->get_requested_post( $where ); + if ( ! $post ) { + return true; + } - return ! $post || current_user_can( $capability, $post->ID ); + if ( ! current_user_can( $capability, $post->ID ) ) { + return false; + } + + // get_requested_post() only returns posts of an exposed post type. + return 'edit_post' !== $capability + || $this->can_set_attributes( $this->get_input_object( $input, 'data' ) ?? array(), get_post_type_object( $post->post_type ), $post ); } /** @@ -998,7 +982,7 @@ private function execute_content_create( array $input ) { * * The post is looked up first, then every attribute rule, and only then is the post * saved, so a rejected call writes nothing. The capabilities are checked by - * {@see self::check_update_permission()}. + * {@see self::check_post_permission()}. * * @since 7.3.0 * @@ -1139,42 +1123,32 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, } } - $status = $this->prepare_status( $data, $post ); - if ( is_wp_error( $status ) ) { - return $status; - } - if ( null !== $status ) { - $postarr['post_status'] = $status; - } - - $author = $this->prepare_author( $data, $post ); - if ( is_wp_error( $author ) ) { - return $author; - } - if ( null !== $author ) { - $postarr['post_author'] = $author; - } - - $parent = $this->prepare_parent( $data, $post_type, $post ); - if ( is_wp_error( $parent ) ) { - return $parent; - } - if ( null !== $parent ) { - $postarr['post_parent'] = $parent; - } - - $postarr = array_merge( $postarr, $this->prepare_date( $data, $post, $status ) ); - - // Without a sent status, the save defaults to `draft`. - $final_status = $postarr['post_status'] ?? $post->post_status ?? 'draft'; - $final_parent = $postarr['post_parent'] ?? $post->post_parent ?? 0; + /* + * Each step returns the post fields to save, or the error of its first rule that fails. + * The date reads the status that is set, and the slug the status and parent the post + * will have, so they run after the steps that set them. + */ + $steps = array( + fn( array $postarr ) => $this->prepare_status( $data, $post ), + fn( array $postarr ) => $this->prepare_author( $data, $post ), + fn( array $postarr ) => $this->prepare_parent( $data, $post_type, $post ), + fn( array $postarr ) => $this->prepare_date( $data, $post, $postarr['post_status'] ?? null ), + // Without a sent status, the save keeps the stored status, or defaults to `draft`. + fn( array $postarr ) => $this->prepare_slug( + $data, + $post_type_object, + $post, + $postarr['post_status'] ?? ( $post ? $post->post_status : 'draft' ), + $postarr['post_parent'] ?? ( $post ? $post->post_parent : 0 ) + ), + ); - $slug = $this->prepare_slug( $data, $post_type_object, $post, $final_status, $final_parent ); - if ( is_wp_error( $slug ) ) { - return $slug; - } - if ( null !== $slug ) { - $postarr['post_name'] = $slug; + foreach ( $steps as $step ) { + $fields = $step( $postarr ); + if ( is_wp_error( $fields ) ) { + return $fields; + } + $postarr = array_merge( $postarr, $fields ); } if ( $post ) { @@ -1227,17 +1201,17 @@ private function check_supported_attributes( array $data, string $post_type ): ? } /** - * Checks the sent `status`. + * Checks the sent `status` and returns the field to save. * * @since 7.3.0 * * @param array $data The `data` input. * @param WP_Post|null $post The stored post on update, or null on create. - * @return string|WP_Error|null The status, null when none is sent or it is unchanged, or a WP_Error. + * @return array|WP_Error The status field, empty when no status is sent or it is unchanged, or a WP_Error. */ private function prepare_status( array $data, ?WP_Post $post ) { if ( ! array_key_exists( 'status', $data ) ) { - return null; + return array(); } $status = $data['status']; @@ -1254,14 +1228,14 @@ private function prepare_status( array $data, ?WP_Post $post ) { // Keeping the stored status changes nothing. if ( $post && $post->post_status === $status ) { - return null; + return array(); } - return $status; + return array( 'post_status' => $status ); } /** - * Checks the sent `author`. + * Checks the sent `author` and returns the field to save. * * The capability to name another user is checked by the permission callbacks, so only * users who may name any user learn whether a user ID exists. @@ -1270,22 +1244,22 @@ private function prepare_status( array $data, ?WP_Post $post ) { * * @param array $data The `data` input. * @param WP_Post|null $post The stored post on update, or null on create. - * @return int|WP_Error|null The author ID, null when none is sent or it is unchanged, or a WP_Error. + * @return array|WP_Error The author field, empty when no author is sent or it is unchanged, or a WP_Error. */ private function prepare_author( array $data, ?WP_Post $post ) { if ( ! array_key_exists( 'author', $data ) ) { - return null; + return array(); } $author_id = $data['author']; // Keeping the stored author changes nothing. if ( $post && (int) $post->post_author === $author_id ) { - return null; + return array(); } if ( get_current_user_id() === $author_id ) { - return $author_id; + return array( 'post_author' => $author_id ); } // Intentionally strict: the author must be an existing user who belongs to this site. @@ -1293,11 +1267,11 @@ private function prepare_author( array $data, ?WP_Post $post ) { return $this->invalid_attribute_error( 'data[author]', __( 'The author must be an existing user of this site.' ) ); } - return $author_id; + return array( 'post_author' => $author_id ); } /** - * Checks the sent `parent`. + * Checks the sent `parent` and returns the field to save. * * The permission callbacks refuse a parent that is missing or that the current user cannot * read, so the errors here never tell whether a post ID exists. @@ -1307,22 +1281,22 @@ private function prepare_author( array $data, ?WP_Post $post ) { * @param array $data The `data` input. * @param string $post_type The post type name. * @param WP_Post|null $post The stored post on update, or null on create. - * @return int|WP_Error|null The parent ID, null when none is sent or it is unchanged, or a WP_Error. + * @return array|WP_Error The parent field, empty when no parent is sent or it is unchanged, or a WP_Error. */ private function prepare_parent( array $data, string $post_type, ?WP_Post $post ) { if ( ! array_key_exists( 'parent', $data ) ) { - return null; + return array(); } $parent_id = $data['parent']; // Keeping the stored parent changes nothing. if ( $post && (int) $post->post_parent === $parent_id ) { - return null; + return array(); } if ( 0 === $parent_id ) { - return 0; + return array( 'post_parent' => 0 ); } $reason = __( 'The parent must be 0, or a post of the same post type that is neither the post itself nor one of its descendants.' ); @@ -1340,7 +1314,7 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post return $this->invalid_attribute_error( 'data[parent]', $reason ); } - return $parent_id; + return array( 'post_parent' => $parent_id ); } /** @@ -1404,7 +1378,7 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ): a } /** - * Checks the sent `slug` and returns the slug to save. + * Checks the sent `slug` and returns the field to save. * * @since 7.3.0 * @@ -1413,16 +1387,16 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ): a * @param WP_Post|null $post The stored post on update, or null on create. * @param string $status The status the post will have. * @param int $parent_id The parent ID the post will have. - * @return string|WP_Error|null The slug, null when none is sent or it is unchanged, or a WP_Error. + * @return array|WP_Error The slug field, empty when no slug is sent or it is unchanged, or a WP_Error. */ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_Post $post, string $status, int $parent_id ) { if ( ! array_key_exists( 'slug', $data ) ) { - return null; + return array(); } // Keeping the stored slug changes nothing, also the empty slug of a draft. if ( $post && $post->post_name === $data['slug'] ) { - return null; + return array(); } $slug = sanitize_title( $data['slug'] ); @@ -1441,7 +1415,7 @@ private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_ $slug = wp_unique_post_slug( $slug, $post ? $post->ID : 0, 'publish', $post_type_object->name, $parent_id ); } - return $slug; + return array( 'post_name' => $slug ); } /** From da95e0ddab530812c6c614877e305bd2fb4cb3a0 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 13:10:25 +0000 Subject: [PATCH 23/24] Tests: Share the content create and update tests that write the same data and fix test names and messages. Moves the output schema, author and raw fields tests and the shared fixtures into the data write trait, moves `assert_invalid_input()` to the write base class, and fixes test names, messages and assertions. --- ...testcase-content-existing-post-ability.php | 26 --- .../testcase-content-write-ability.php | 32 ++- .../trait-content-data-write-tests.php | 142 +++++++++++++ .../wpRegisterCoreContentCreateAbility.php | 186 ++---------------- .../wpRegisterCoreContentDeleteAbility.php | 2 +- .../wpRegisterCoreContentUpdateAbility.php | 124 ++---------- 6 files changed, 204 insertions(+), 308 deletions(-) diff --git a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php index a9dec7b918ae1..c0ee770ab305b 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-existing-post-ability.php @@ -168,32 +168,6 @@ protected function build_post_input( int $post_id, ?string $type = null ): array return $this->build_input( $this->build_where( $post_id, $type ) ); } - /** - * Asserts that input validation rejects an input of an administrator and that nothing is - * written to the given post. - * - * Registers the content abilities unless the test already did. - * - * @since 7.3.0 - * - * @param mixed $input The ability input. - * @param int $post_id The ID of the post that should be left as it was. - * @param string $context What the call was, for the assertion messages. - */ - protected function assert_invalid_input( $input, int $post_id, string $context ): void { - $this->login_as( 'administrator' ); - if ( ! wp_has_ability( static::ABILITY ) ) { - $this->register_abilities(); - } - - $before = $this->snapshot( $post_id ); - $result = $this->run_ability( $input ); - - $this->assertWPError( $result, "{$context}: the call should fail." ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), "{$context}: the schema should reject the input." ); - $this->assertSame( $before, $this->snapshot( $post_id ), "{$context}: nothing should be written." ); - } - /** * Returns whether each role may edit and delete each matrix post. * diff --git a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php index 750ffaf0645ae..c650ff4836191 100644 --- a/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php +++ b/tests/phpunit/tests/abilities-api/testcase-content-write-ability.php @@ -346,6 +346,33 @@ protected function assert_invalid_attribute( $result, string $attribute, string $this->assertSame( array( "data[{$attribute}]" => $message ), $result->get_error_data()['params'] ?? array(), "{$context}: the params should hold only the input path of the attribute, with the reason." ); } + /** + * Asserts that input validation rejects an input of an administrator and that nothing is + * written. + * + * Registers the content abilities unless the test already did. + * + * @since 7.3.0 + * + * @param mixed $input The ability input. + * @param int|null $post_id The ID of the post that should be left as it was, or null when + * the call would create a post. + * @param string $context What the call was, for the assertion messages. + */ + protected function assert_invalid_input( $input, ?int $post_id, string $context ): void { + $this->login_as( 'administrator' ); + if ( ! wp_has_ability( static::ABILITY ) ) { + $this->register_abilities(); + } + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( $input ); + + $this->assertWPError( $result, "{$context}: the call should fail." ); + $this->assertSame( 'ability_invalid_input', $result->get_error_code(), "{$context}: the schema should reject the input." ); + $this->assertSame( $before, $this->snapshot( $post_id ), "{$context}: nothing should be written." ); + } + /** * Asserts that a result is the not-found error. * @@ -454,7 +481,8 @@ public function test_is_not_registered_without_exposed_post_types(): void { abstract public static function data_inputs_refused_by_the_permission_callback(): array; /** - * The permission callback returns false for missing or malformed input. + * The permission callback returns false for missing or malformed input, and for input that + * names a post type that is not exposed to abilities. * * @ticket 66706 * @dataProvider data_inputs_refused_by_the_permission_callback @@ -462,7 +490,7 @@ abstract public static function data_inputs_refused_by_the_permission_callback() * * @param mixed $input The input. */ - public function test_permission_callback_returns_false_for_malformed_input( $input ): void { + public function test_permission_callback_returns_false_for_malformed_or_unexposed_input( $input ): void { $this->login_as( 'administrator' ); $permission = $this->get_ability_callbacks()['permission_callback']; diff --git a/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php b/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php index 18c1bf98062eb..5cb68bf70e4df 100644 --- a/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php +++ b/tests/phpunit/tests/abilities-api/trait-content-data-write-tests.php @@ -46,6 +46,31 @@ abstract protected function build_write_input( ?int $post_id, array $data, ?arra */ abstract protected function assert_data_written( $result, ?int $post_id ): WP_Post; + /** + * Creates the users and posts that both test classes read: a `page_writer` who may write + * pages but not read the drafts of other users, a `publisher` who may publish posts but + * not edit published posts, and a draft page of the editor. + * + * @since 7.3.0 + * + * @param WP_UnitTest_Factory $factory The unit test factory. + */ + protected static function create_data_write_fixtures( WP_UnitTest_Factory $factory ): void { + self::$user_ids['page_writer'] = $factory->user->create( array( 'role' => 'subscriber' ) ); + get_userdata( self::$user_ids['page_writer'] )->add_cap( 'edit_pages' ); + + self::$user_ids['publisher'] = $factory->user->create( array( 'role' => 'contributor' ) ); + get_userdata( self::$user_ids['publisher'] )->add_cap( 'publish_posts' ); + + self::$post_ids['editor_draft_page'] = $factory->post->create( + array( + 'post_author' => self::$user_ids['editor'], + 'post_type' => 'page', + 'post_status' => 'draft', + ) + ); + } + /** * Called directly, an internal or unregistered status is an invalid attribute. * @@ -183,4 +208,121 @@ public function test_quotes_backslashes_and_non_ascii_text_survive_a_round_trip( $this->assertSame( $text, $result['content']['raw'], 'The returned content should be unchanged.' ); $this->assertSame( $text, $result['excerpt']['raw'], 'The returned excerpt should be unchanged.' ); } + + /** + * The output is one post item, the same object as an item of the query. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_output_schema_is_a_query_item(): void { + $this->register_abilities(); + + $this->assertTrue( wp_has_ability( self::ABILITY ), 'The ability should be registered.' ); + + $schema = wp_get_ability( self::ABILITY )->get_output_schema(); + $item = wp_get_ability( 'core/content-query' )->get_output_schema()['properties']['items']['items']; + + $this->assertSame( array( 'id' ), $schema['required'], 'The output should always hold the ID.' ); + $this->assertSame( $this->strip_descriptions( $item ), $this->strip_descriptions( $schema ), 'The output should be the same object as a query item.' ); + } + + /** + * The permission callback refuses a user without `edit_others_posts` who names another + * author, existing or not, so the refusal never tells whether a user exists. + * + * @ticket 66706 + * @dataProvider data_authors_named_without_edit_others_posts + * @since 7.3.0 + * + * @param string $author The author, as a fixture name. + */ + public function test_naming_another_author_without_edit_others_posts_is_refused( string $author ): void { + $post_id = $this->get_write_post_id( 'author' ); + + $this->login_as( 'author' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( + $this->build_write_input( + $post_id, + array( + 'title' => 'Ghost written', + 'author' => self::$user_ids[ $author ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, + ) + ) + ); + + $this->assert_refused( $result, 'Naming another author' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * On multisite, a user who is not a member of the site cannot be the author. + * + * @group ms-required + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_invalid_author(): void { + $non_member = self::factory()->user->create( array( 'role' => 'author' ) ); + remove_user_from_blog( $non_member, get_current_blog_id() ); + + $this->assertFalse( is_user_member_of_blog( $non_member ), 'Precondition: the user should not be a member of the site.' ); + + $post_id = $this->get_write_post_id( 'editor' ); + + $this->login_as( 'editor' ); + $this->register_abilities(); + + $before = $this->snapshot( $post_id ); + $result = $this->run_ability( + $this->build_write_input( + $post_id, + array( + 'title' => 'Not a member', + 'author' => $non_member, + ) + ) + ); + + $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site', 'The author must be an existing user of this site.' ); + $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); + } + + /** + * Raw fields are left out when the writer cannot edit the post after the write. + * + * The user may publish posts but not edit published posts. + * + * @ticket 66706 + * @since 7.3.0 + */ + public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_written_post(): void { + $post_id = $this->get_write_post_id( 'publisher' ); + + $this->login_as( 'publisher' ); + $this->register_abilities(); + + $result = $this->run_ability( + $this->build_write_input( + $post_id, + array( + 'title' => 'Published once', + 'content' => 'Body.', + 'status' => 'publish', + ), + array( 'title', 'content.raw', 'content.rendered' ) + ) + ); + + $post = $this->assert_data_written( $result, $post_id ); + + $this->assertSame( 'publish', $post->post_status, 'The post should be published.' ); + $this->assertFalse( current_user_can( 'edit_post', $post->ID ), 'The user should not be able to edit the published post.' ); + $this->assertSame( array( 'rendered' => 'Published once' ), $result['title'], 'The title should be returned without its raw value.' ); + $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); + } } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php index b522f43a23baa..0593dd695e92a 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentCreateAbility.php @@ -59,13 +59,7 @@ public static function wpSetUpBeforeClass( $factory ): void { self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) ); } - // A user who may create pages but not read the drafts of other users. - self::$user_ids['page_writer'] = $factory->user->create( array( 'role' => 'subscriber' ) ); - get_userdata( self::$user_ids['page_writer'] )->add_cap( 'edit_pages' ); - - // A user who may publish posts but not edit published posts. - self::$user_ids['publisher'] = $factory->user->create( array( 'role' => 'contributor' ) ); - get_userdata( self::$user_ids['publisher'] )->add_cap( 'publish_posts' ); + self::create_data_write_fixtures( $factory ); self::$post_ids['published_post'] = $factory->post->create( array( @@ -86,14 +80,6 @@ public static function wpSetUpBeforeClass( $factory ): void { } wp_trash_post( self::$post_ids['trashed_page'] ); - self::$post_ids['editor_draft_page'] = $factory->post->create( - array( - 'post_author' => self::$user_ids['editor'], - 'post_type' => 'page', - 'post_status' => 'draft', - ) - ); - // A child page whose `child` slug data_draft_page_slugs_by_parent() reads. $factory->post->create( array( @@ -238,9 +224,9 @@ public function test_input_schema(): void { $this->assertSame( 'string', $properties['content']['type'], 'The content should be a string.' ); $this->assertSame( 'string', $properties['excerpt']['type'], 'The excerpt should be a string.' ); $this->assertSame( 'string', $properties['status']['type'], 'The status should be a string.' ); - $this->assertSame( array( 'publish', 'future', 'draft', 'pending', 'private' ), $properties['status']['enum'], 'The status should list the statuses that are not internal.' ); + $this->assertSameSets( array( 'publish', 'future', 'draft', 'pending', 'private' ), $properties['status']['enum'], 'The status should list the statuses that are not internal.' ); $this->assertSame( 'string', $properties['slug']['type'], 'The slug should be a string.' ); - $this->assertArrayNotHasKey( 'minLength', $properties['slug'], 'The slug should take an empty string, so the empty slug of a draft can be sent back.' ); + $this->assertArrayNotHasKey( 'minLength', $properties['slug'], 'The slug should take an empty string, since the update ability shares the data schema and takes the empty slug of a draft back.' ); $this->assertSame( 'integer', $properties['author']['type'], 'The author should be an integer.' ); $this->assertSame( 1, $properties['author']['minimum'], 'The author should be at least 1.' ); $this->assertSame( 'integer', $properties['parent']['type'], 'The parent should be an integer.' ); @@ -268,26 +254,6 @@ public function test_input_schema(): void { ); } - /** - * The output is one post item, the same object as an item of the query. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_output_schema_is_a_query_item(): void { - $this->register_abilities(); - - $this->assertTrue( wp_has_ability( self::ABILITY ), 'The create ability should be registered.' ); - - $schema = wp_get_ability( self::ABILITY )->get_output_schema(); - $item = wp_get_ability( 'core/content-query' )->get_output_schema()['properties']['items']['items']; - - $this->assertSame( 'object', $schema['type'], 'The output should be an object.' ); - $this->assertFalse( $schema['additionalProperties'], 'The output should not take unknown keys.' ); - $this->assertSame( array( 'id' ), $schema['required'], 'The output should always hold the ID.' ); - $this->assertSame( $this->strip_descriptions( $item ), $this->strip_descriptions( $schema ), 'The output should be the same object as a query item.' ); - } - /** * The schema enums are built from the site when the ability is registered. * @@ -790,15 +756,7 @@ public static function data_data_that_fails_input_validation(): array { * @param array $data The data. */ public function test_data_fails_input_validation( array $data ): void { - $this->login_as( 'administrator' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( array( 'data' => $data ) ); - - $this->assertWPError( $result, 'The call should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject the data.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); + $this->assert_invalid_input( array( 'data' => $data ), null, 'Invalid data' ); } /** @@ -836,35 +794,6 @@ public function test_status_no_longer_registered_is_rejected(): void { $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * The permission callback refuses a user without `edit_others_posts` who names another - * author, existing or not, so the refusal never tells whether a user exists. - * - * @ticket 66706 - * @dataProvider data_authors_named_without_edit_others_posts - * @since 7.3.0 - * - * @param string $author The author, as a fixture name. - */ - public function test_naming_another_author_without_edit_others_posts_is_refused( string $author ): void { - $this->login_as( 'author' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Ghost written', - 'author' => self::$user_ids[ $author ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER, - ), - ) - ); - - $this->assert_refused( $result, 'Naming another author' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * Returns authors that a role may name: the role and the author, as fixture names. * @@ -970,38 +899,6 @@ public function test_invalid_attribute_is_rejected( string $role, array $data, s $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * On multisite, a user who is not a member of the site cannot be the author. - * - * @group ms-required - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_invalid_author(): void { - $non_member = self::factory()->user->create( array( 'role' => 'author' ) ); - remove_user_from_blog( $non_member, get_current_blog_id() ); - - $this->assertFalse( is_user_member_of_blog( $non_member ), 'Precondition: the user should not be a member of the site.' ); - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $before = $this->snapshot(); - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Not a member', - 'author' => $non_member, - ), - ) - ); - - $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site', 'The author must be an existing user of this site.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); - } - /** * Returns parents that a page may not have. * @@ -1026,7 +923,7 @@ public static function data_parents_refused_by_the_permission_callback(): array * * @param string $parent_key The parent, as a fixture name. */ - public function test_parent_that_is_missing_or_unreadable_is_refused( string $parent_key ): void { + public function test_parent_that_is_missing_unreadable_or_not_exposed_is_refused( string $parent_key ): void { $this->login_as( 'page_writer' ); $this->register_abilities(); @@ -1043,35 +940,23 @@ public function test_parent_that_is_missing_or_unreadable_is_refused( string $pa ) ); - $this->assert_refused( $result, 'A missing or unreadable parent' ); + $this->assert_refused( $result, 'A missing, unreadable or unexposed parent' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } - /** - * Returns readable parents that a new page may not have. - * - * @return array The parents, as fixture names. - */ - public static function data_parents_that_cannot_be_used(): array { - return array( - 'post of another type' => array( 'published_post' ), - ); - } - /** * A readable parent of another type is an invalid parent. * * @ticket 66706 - * @dataProvider data_parents_that_cannot_be_used * @since 7.3.0 - * - * @param string $parent_key The parent, as a fixture name. */ - public function test_parent_that_cannot_be_used_is_an_invalid_parent( string $parent_key ): void { + public function test_parent_of_another_type_is_an_invalid_parent(): void { + $parent_id = self::$post_ids['published_post']; + $this->login_as( 'page_writer' ); $this->register_abilities(); - $this->assertTrue( current_user_can( 'read_post', self::$post_ids[ $parent_key ] ), 'Precondition: the user should be able to read the parent.' ); + $this->assertTrue( current_user_can( 'read_post', $parent_id ), 'Precondition: the user should be able to read the parent.' ); $before = $this->snapshot(); $result = $this->run_ability( @@ -1079,12 +964,12 @@ public function test_parent_that_cannot_be_used_is_an_invalid_parent( string $pa 'data' => array( 'type' => 'page', 'title' => 'Child', - 'parent' => self::$post_ids[ $parent_key ], + 'parent' => $parent_id, ), ) ); - $this->assert_invalid_attribute( $result, 'parent', 'A parent that cannot be used', 'The parent must be 0, or a post of the same post type that is neither the post itself nor one of its descendants.' ); + $this->assert_invalid_attribute( $result, 'parent', 'A parent of another type', 'The parent must be 0, or a post of the same post type that is neither the post itself nor one of its descendants.' ); $this->assertSame( $before, $this->snapshot(), 'Nothing should be written.' ); } @@ -1440,7 +1325,7 @@ public static function data_draft_page_slugs_by_parent(): array { } /** - * A draft page is made unique among the pages that share its parent only. + * The slug of a draft page is made unique among the pages that share its parent only. * * @ticket 66706 * @dataProvider data_draft_page_slugs_by_parent @@ -1693,38 +1578,6 @@ public function test_markup_of_a_super_admin_is_not_filtered(): void { $this->assert_markup_is_stored_unchanged(); } - /** - * Raw fields are left out when the writer cannot edit the post after the write. - * - * The user may publish posts but not edit published posts. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_raw_fields_are_left_out_when_the_writer_cannot_edit_the_new_post(): void { - $this->login_as( 'publisher' ); - $this->register_abilities(); - - $result = $this->run_ability( - array( - 'data' => array( - 'type' => 'post', - 'title' => 'Published once', - 'content' => 'Body.', - 'status' => 'publish', - ), - 'fields' => array( 'title', 'content.raw', 'content.rendered' ), - ) - ); - - $post = $this->get_created_post( $result ); - - $this->assertSame( 'publish', $post->post_status, 'The post should be published.' ); - $this->assertFalse( current_user_can( 'edit_post', $post->ID ), 'Precondition: The user should not be able to edit the published post.' ); - $this->assertSame( array( 'rendered' => 'Published once' ), $result['title'], 'The title should be returned without its raw value.' ); - $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); - } - /** * The current user is checked at each call, also on the same instance. * @@ -1811,19 +1664,16 @@ public function test_type_exposed_after_registration_fails_input_validation(): v get_post_type_object( 'cw_late' )->show_in_abilities = true; - $before = $this->snapshot(); - $result = $this->run_ability( + $this->assert_invalid_input( array( 'data' => array( 'type' => 'cw_late', 'title' => 'Exposed too late', ), - ) + ), + null, + 'A type exposed after registration' ); - - $this->assertWPError( $result, 'A create of a type exposed after registration should fail.' ); - $this->assertSame( 'ability_invalid_input', $result->get_error_code(), 'The schema should reject a type exposed after registration.' ); - $this->assertSame( $before, $this->snapshot(), 'Nothing should be written for a type exposed after registration.' ); } /** @@ -2002,7 +1852,7 @@ public function test_member_of_the_first_site_only_is_an_invalid_author_on_the_s restore_current_blog(); - $this->assert_invalid_attribute( $result, 'author', 'An author who is a member of the first site only' ); + $this->assert_invalid_attribute( $result, 'author', 'An author who is a member of the first site only', 'The author must be an existing user of this site.' ); $this->assertSame( $before, $after, 'Nothing should be written on the second site.' ); } diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php index b0defcd7b898c..49503a648e65c 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php @@ -372,7 +372,7 @@ public function test_administrator_can_delete_a_special_page( string $page, bool * * @param string $page The page, as a fixture name. */ - public function test_site_administrator_trashes_special_pages_on_multisite( string $page ): void { + public function test_site_administrator_may_trash_special_pages_except_the_privacy_policy_page_on_multisite( string $page ): void { $this->set_special_pages(); $post_id = self::$post_ids[ $page ]; diff --git a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php index 196c1d995d23b..c0aa14bb57fe9 100644 --- a/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php +++ b/tests/phpunit/tests/abilities-api/wpRegisterCoreContentUpdateAbility.php @@ -65,21 +65,14 @@ class Tests_Abilities_API_WpRegisterCoreContentUpdateAbility extends WP_Abilitie */ public static function wpSetUpBeforeClass( $factory ): void { self::create_shared_fixtures( $factory ); + self::create_data_write_fixtures( $factory ); - self::$user_ids['page_writer'] = $factory->user->create( array( 'role' => 'subscriber' ) ); - self::$user_ids['publisher'] = $factory->user->create( array( 'role' => 'contributor' ) ); - - // A user who may edit their own draft pages but not read the drafts of other users. - get_userdata( self::$user_ids['page_writer'] )->add_cap( 'edit_pages' ); - - // A user who may publish posts but not edit published posts. - get_userdata( self::$user_ids['publisher'] )->add_cap( 'publish_posts' ); - - self::$post_ids['editor_draft_page'] = $factory->post->create( + self::$post_ids['publisher:draft'] = $factory->post->create( array( - 'post_author' => self::$user_ids['editor'], - 'post_type' => 'page', - 'post_status' => 'draft', + 'post_author' => self::$user_ids['publisher'], + 'post_status' => 'draft', + 'post_title' => 'Publisher draft', + 'post_content' => 'Draft body.', ) ); @@ -298,7 +291,7 @@ public function test_input_schema(): void { $this->assertSame( 1, $data['minProperties'], 'The data should hold at least one attribute.' ); $this->assertArrayNotHasKey( 'required', $data, 'The data should require no attribute.' ); $this->assertSame( array( 'title', 'content', 'excerpt', 'status', 'slug', 'date', 'author', 'parent' ), array_keys( $data['properties'] ), 'The data should take the allowed attributes and no post type.' ); - $this->assertSame( array( 'publish', 'future', 'draft', 'pending', 'private' ), $data['properties']['status']['enum'], 'The status should list the statuses that are not internal.' ); + $this->assertSameSets( array( 'publish', 'future', 'draft', 'pending', 'private' ), $data['properties']['status']['enum'], 'The status should list the statuses that are not internal.' ); $this->assertArrayNotHasKey( 'minLength', $data['properties']['slug'], 'The slug should take an empty string, so the empty slug of a draft can be sent back.' ); $this->assertSame( 1, $data['properties']['author']['minimum'], 'The author should be at least 1.' ); $this->assertSame( 0, $data['properties']['parent']['minimum'], 'The parent should be at least 0.' ); @@ -320,24 +313,6 @@ public function test_input_schema(): void { ); } - /** - * The output is one post item, the same object as an item of the query. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_output_schema_is_a_query_item(): void { - $this->register_abilities(); - - $this->assertTrue( wp_has_ability( self::ABILITY ), 'The update ability should be registered.' ); - - $schema = wp_get_ability( self::ABILITY )->get_output_schema(); - $item = wp_get_ability( 'core/content-query' )->get_output_schema()['properties']['items']['items']; - - $this->assertSame( array( 'id' ), $schema['required'], 'The output should always hold the ID.' ); - $this->assertSame( $this->strip_descriptions( $item ), $this->strip_descriptions( $schema ), 'The output should be the same object as a query item.' ); - } - /** * A new title and excerpt are stored, and the post is returned in the chosen fields. * @@ -707,29 +682,6 @@ public function test_status_change_is_stored( string $role, string $stored, stri $this->assertSame( $status, $result['status'], 'The new status should be returned.' ); } - /** - * The permission callback refuses a user without `edit_others_posts` who names another - * author, existing or not, so the refusal never tells whether a user exists. - * - * @ticket 66706 - * @dataProvider data_authors_named_without_edit_others_posts - * @since 7.3.0 - * - * @param string $author The author, as a fixture name. - */ - public function test_naming_another_author_without_edit_others_posts_is_refused( string $author ): void { - $post_id = self::$post_ids['author:draft']; - - $this->login_as( 'author' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'author' => self::$user_ids[ $author ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - - $this->assert_refused( $result, 'Naming another author' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * A user with `edit_others_posts` may change the author of a post. * @@ -833,30 +785,6 @@ public function test_invalid_attribute_is_rejected( string $role, array $data, s $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } - /** - * On multisite, a user who is not a member of the site cannot be the author. - * - * @group ms-required - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_naming_an_author_who_is_not_a_member_of_the_site_is_an_invalid_author(): void { - $non_member = self::factory()->user->create( array( 'role' => 'author' ) ); - remove_user_from_blog( $non_member, get_current_blog_id() ); - - $post_id = self::$post_ids['editor:draft']; - - $this->login_as( 'editor' ); - $this->register_abilities(); - - $before = $this->snapshot( $post_id ); - $result = $this->update_post( $post_id, array( 'author' => $non_member ) ); - - $this->assert_invalid_attribute( $result, 'author', 'An author who is not a member of the site', 'The author must be an existing user of this site.' ); - $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); - } - /** * Returns parents that a page may not have. * @@ -881,7 +809,7 @@ public static function data_parents_refused_by_the_permission_callback(): array * * @param string $parent_key The parent, as a fixture name. */ - public function test_parent_that_is_missing_or_unreadable_is_refused( string $parent_key ): void { + public function test_parent_that_is_missing_unreadable_or_not_exposed_is_refused( string $parent_key ): void { $post_id = self::$post_ids['writer_page']; $this->login_as( 'page_writer' ); @@ -893,7 +821,7 @@ public function test_parent_that_is_missing_or_unreadable_is_refused( string $pa $before = $this->snapshot( $post_id ); $result = $this->update_post( $post_id, array( 'parent' => self::$post_ids[ $parent_key ] ?? REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ); - $this->assert_refused( $result, 'A missing or unreadable parent' ); + $this->assert_refused( $result, 'A missing, unreadable or unexposed parent' ); $this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' ); } @@ -1290,7 +1218,7 @@ public static function data_draft_page_slugs_by_parent(): array { } /** - * A draft page is made unique among the pages that share the sent parent. + * The slug of a draft page is made unique among the pages that share the sent parent. * * @ticket 66706 * @dataProvider data_draft_page_slugs_by_parent @@ -1653,6 +1581,8 @@ public function test_markup_of_an_author_is_filtered(): void { $this->login_as( 'author' ); $this->register_abilities(); + $this->assertFalse( current_user_can( 'unfiltered_html' ), 'Precondition: the user should not have unfiltered_html.' ); + $markup = 'link'; $result = $this->update_post( $post_id, @@ -1668,6 +1598,7 @@ public function test_markup_of_an_author_is_filtered(): void { $this->assertSame( 'link', $post->post_title, 'The stored title should be filtered.' ); $this->assertSame( 'link', $post->post_content, 'The stored content should be filtered.' ); $this->assertSame( 'link', $result['title']['raw'], 'The returned title should be filtered.' ); + $this->assertSame( 'link', $result['content']['raw'], 'The returned content should be filtered.' ); } /** @@ -1709,35 +1640,6 @@ public function test_markup_of_a_site_administrator_is_filtered_on_multisite(): $this->assertSame( '
div
', $post->post_content, 'The stored content should be filtered.' ); } - /** - * Raw fields are left out when the writer can no longer edit the post after the write. - * - * The user may publish posts but not edit published posts. - * - * @ticket 66706 - * @since 7.3.0 - */ - public function test_raw_fields_are_left_out_when_the_writer_can_no_longer_edit_the_post(): void { - $post_id = self::factory()->post->create( - array( - 'post_author' => self::$user_ids['publisher'], - 'post_status' => 'draft', - 'post_title' => 'Publish once', - 'post_content' => 'Body.', - ) - ); - - $this->login_as( 'publisher' ); - $this->register_abilities(); - - $result = $this->update_post( $post_id, array( 'status' => 'publish' ), array( 'title', 'content.raw', 'content.rendered' ) ); - - $this->assertSame( 'publish', $this->assert_updated( $result, $post_id )->post_status, 'The post should be published.' ); - $this->assertFalse( current_user_can( 'edit_post', $post_id ), 'Precondition: The user should not be able to edit the published post.' ); - $this->assertSame( array( 'rendered' => 'Publish once' ), $result['title'], 'The title should be returned without its raw value.' ); - $this->assertSame( array( 'rendered' => "

Body.

\n" ), $result['content'], 'The content should be returned without its raw value.' ); - } - /** * A password-protected post is returned with its protection flag. * From 7622fa0ffba9c4a13f08b54b9d9700d64451b211 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Nov 2026 14:08:52 +0000 Subject: [PATCH 24/24] Abilities API: Drop unchanged content attributes once and check the status with the other stored attributes. Unchanged status, author, parent and slug values are removed in one helper shared by the permission and execute callbacks, instead of separate checks in each step. --- .../abilities/class-wp-abilities-content.php | 211 ++++++++---------- 1 file changed, 97 insertions(+), 114 deletions(-) diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 67430fd347493..c9946faa6794f 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -334,8 +334,9 @@ private function check_create_permission( $input ): bool { * others. A parent must be a post the current user can read, of a type exposed to * abilities; a missing post is refused the same way, so the refusal never tells whether a * post exists. The slug of a post that will be pending needs the capability to publish the - * post. A value that keeps the stored one needs none of these. Every other rule on these - * attributes is checked by the execute callbacks. + * post. A value that keeps the stored one needs none of these, see + * {@see self::get_changed_data()}. Every other rule on these attributes is checked by the + * execute callbacks. * * @since 7.3.0 * @@ -345,6 +346,8 @@ private function check_create_permission( $input ): bool { * @return bool True if the user may set the sent attributes, false otherwise. */ private function can_set_attributes( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ): bool { + $data = $this->get_changed_data( $data, $post ); + if ( array_key_exists( 'status', $data ) ) { $status = $data['status']; if ( ! is_string( $status ) ) { @@ -358,8 +361,7 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object $status_object = get_post_status_object( $status ); $makes_visible = $status_object instanceof stdClass && ( 'future' === $status || ! empty( $status_object->public ) || ! empty( $status_object->private ) || is_post_status_viewable( $status_object ) ); - $is_unchanged = $post && $post->post_status === $status; - if ( $makes_visible && ! $is_unchanged && ! current_user_can( $post_type_object->cap->publish_posts ) ) { + if ( $makes_visible && ! current_user_can( $post_type_object->cap->publish_posts ) ) { return false; } } @@ -370,8 +372,7 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object return false; } - $is_unchanged = $post && (int) $post->post_author === $author_id; - if ( ! $is_unchanged && get_current_user_id() !== $author_id && ! current_user_can( $post_type_object->cap->edit_others_posts ) ) { + if ( get_current_user_id() !== $author_id && ! current_user_can( $post_type_object->cap->edit_others_posts ) ) { return false; } } @@ -383,8 +384,7 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object return false; } - $is_unchanged = $post && (int) $post->post_parent === $parent_id; - if ( ! $is_unchanged && 0 !== $parent_id ) { + if ( 0 !== $parent_id ) { $parent = get_post( $parent_id ); if ( ! $parent instanceof WP_Post || ! $this->check_read_permission( $parent ) ) { return false; @@ -393,9 +393,8 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object } if ( array_key_exists( 'slug', $data ) ) { - // Without a sent status, the save keeps the stored status, or defaults to `draft`. - $status = $data['status'] ?? ( $post ? $post->post_status : 'draft' ); - $is_unchanged = $post && $post->post_name === $data['slug']; + // Without a changed status, the save keeps the stored status, or defaults to `draft`. + $status = $data['status'] ?? ( $post ? $post->post_status : 'draft' ); /* * Intentionally strict: the save drops the slug of a pending post when the user @@ -403,7 +402,7 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object * notice. The stored slug sent back is accepted as unchanged, although the save * still drops it. */ - if ( 'pending' === $status && ! $is_unchanged ) { + if ( 'pending' === $status ) { $can_publish = $post ? current_user_can( 'publish_post', $post->ID ) : current_user_can( $post_type_object->cap->publish_posts ); @@ -416,6 +415,47 @@ private function can_set_attributes( array $data, WP_Post_Type $post_type_object return true; } + /** + * Removes the sent `status`, `author`, `parent` and `slug` that keep their stored values. + * + * Keeping a stored value, also the empty slug of a draft, changes nothing, so it needs no + * capability and is not checked or saved again. The IDs are read with + * {@see self::parse_filter_int()}, as the permission callbacks read them. On create, + * nothing is stored, so the data is returned as given. + * + * @since 7.3.0 + * + * @param array $data The `data` input. + * @param WP_Post|null $post The stored post on update, or null on create. + * @return array The `data` input without the attributes that keep their stored values. + */ + private function get_changed_data( array $data, ?WP_Post $post ): array { + if ( ! $post ) { + return $data; + } + + $stored = array( + 'status' => $post->post_status, + 'author' => (int) $post->post_author, + 'parent' => (int) $post->post_parent, + 'slug' => $post->post_name, + ); + + foreach ( $stored as $attribute => $stored_value ) { + if ( ! array_key_exists( $attribute, $data ) ) { + continue; + } + + // An author ID is at least 1, a parent ID at least 0. + $value = is_int( $stored_value ) ? $this->parse_filter_int( $data[ $attribute ], 'author' === $attribute ? 1 : 0 ) : $data[ $attribute ]; + if ( $stored_value === $value ) { + unset( $data[ $attribute ] ); + } + } + + return $data; + } + /** * Permission callback for the `core/content-update` and `core/content-delete` abilities. * @@ -1105,37 +1145,31 @@ private function get_requested_post( array $where ): ?WP_Post { private function prepare_post_data( array $data, WP_Post_Type $post_type_object, ?WP_Post $post ) { $post_type = $post_type_object->name; + // Checked first, since an attribute that cannot be stored is refused even when unchanged. $error = $this->check_supported_attributes( $data, $post_type ); if ( $error ) { return $error; } - $postarr = array(); - $text_fields = array( - 'title' => 'post_title', - 'content' => 'post_content', - 'excerpt' => 'post_excerpt', - ); - - foreach ( $text_fields as $attribute => $field ) { - if ( array_key_exists( $attribute, $data ) ) { - $postarr[ $field ] = $data[ $attribute ]; - } - } + $data = $this->get_changed_data( $data, $post ); /* - * Each step returns the post fields to save, or the error of its first rule that fails. - * The date reads the status that is set, and the slug the status and parent the post - * will have, so they run after the steps that set them. + * Each step runs only for a sent attribute, and returns the post fields to save, or + * the error of its first rule that fails. The date reads the status that is set, and + * the slug the status and parent the post will have, so they run after the steps that + * set them. */ $steps = array( - fn( array $postarr ) => $this->prepare_status( $data, $post ), - fn( array $postarr ) => $this->prepare_author( $data, $post ), - fn( array $postarr ) => $this->prepare_parent( $data, $post_type, $post ), - fn( array $postarr ) => $this->prepare_date( $data, $post, $postarr['post_status'] ?? null ), - // Without a sent status, the save keeps the stored status, or defaults to `draft`. - fn( array $postarr ) => $this->prepare_slug( - $data, + 'title' => fn() => array( 'post_title' => $data['title'] ), + 'content' => fn() => array( 'post_content' => $data['content'] ), + 'excerpt' => fn() => array( 'post_excerpt' => $data['excerpt'] ), + 'status' => fn() => array( 'post_status' => $data['status'] ), + 'author' => fn() => $this->prepare_author( $data['author'] ), + 'parent' => fn() => $this->prepare_parent( $data['parent'], $post_type, $post ), + 'date' => fn( array $postarr ) => $this->prepare_date( $data['date'], $post, $postarr['post_status'] ?? null ), + // Without a changed status, the save keeps the stored status, or defaults to `draft`. + 'slug' => fn( array $postarr ) => $this->prepare_slug( + $data['slug'], $post_type_object, $post, $postarr['post_status'] ?? ( $post ? $post->post_status : 'draft' ), @@ -1143,7 +1177,12 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, ), ); - foreach ( $steps as $step ) { + $postarr = array(); + foreach ( $steps as $attribute => $step ) { + if ( ! array_key_exists( $attribute, $data ) ) { + continue; + } + $fields = $step( $postarr ); if ( is_wp_error( $fields ) ) { return $fields; @@ -1164,13 +1203,16 @@ private function prepare_post_data( array $data, WP_Post_Type $post_type_object, } /** - * Rejects the sent attributes that the post type does not support. + * Rejects the sent attributes that cannot be stored. + * + * These are the attributes that the post type does not support, and a status that is not + * registered or is internal. * * @since 7.3.0 * * @param array $data The `data` input. * @param string $post_type The post type name. - * @return WP_Error|null The error for the first unsupported attribute, or null. + * @return WP_Error|null The error for the first attribute that cannot be stored, or null. */ private function check_supported_attributes( array $data, string $post_type ): ?WP_Error { /* @@ -1197,41 +1239,19 @@ private function check_supported_attributes( array $data, string $post_type ): ? return $this->invalid_attribute_error( 'data[parent]', __( 'The parent attribute is only supported for hierarchical post types.' ) ); } - return null; - } - - /** - * Checks the sent `status` and returns the field to save. - * - * @since 7.3.0 - * - * @param array $data The `data` input. - * @param WP_Post|null $post The stored post on update, or null on create. - * @return array|WP_Error The status field, empty when no status is sent or it is unchanged, or a WP_Error. - */ - private function prepare_status( array $data, ?WP_Post $post ) { - if ( ! array_key_exists( 'status', $data ) ) { - return array(); - } - - $status = $data['status']; - $status_object = get_post_status_object( $status ); - /* * Intentionally strict: a status that is not registered is an error and is never * replaced by another status. Internal statuses, such as `trash`, can never be set, * not even when the post already has them. */ - if ( ! $status_object instanceof stdClass || ! empty( $status_object->internal ) ) { - return $this->invalid_attribute_error( 'data[status]', __( 'The status must be a registered post status that is not internal.' ) ); - } - - // Keeping the stored status changes nothing. - if ( $post && $post->post_status === $status ) { - return array(); + if ( array_key_exists( 'status', $data ) ) { + $status_object = get_post_status_object( $data['status'] ); + if ( ! $status_object instanceof stdClass || ! empty( $status_object->internal ) ) { + return $this->invalid_attribute_error( 'data[status]', __( 'The status must be a registered post status that is not internal.' ) ); + } } - return array( 'post_status' => $status ); + return null; } /** @@ -1242,22 +1262,10 @@ private function prepare_status( array $data, ?WP_Post $post ) { * * @since 7.3.0 * - * @param array $data The `data` input. - * @param WP_Post|null $post The stored post on update, or null on create. - * @return array|WP_Error The author field, empty when no author is sent or it is unchanged, or a WP_Error. + * @param int $author_id The sent `author`, which differs from the stored one. + * @return array|WP_Error The author field, or a WP_Error. */ - private function prepare_author( array $data, ?WP_Post $post ) { - if ( ! array_key_exists( 'author', $data ) ) { - return array(); - } - - $author_id = $data['author']; - - // Keeping the stored author changes nothing. - if ( $post && (int) $post->post_author === $author_id ) { - return array(); - } - + private function prepare_author( $author_id ) { if ( get_current_user_id() === $author_id ) { return array( 'post_author' => $author_id ); } @@ -1278,23 +1286,12 @@ private function prepare_author( array $data, ?WP_Post $post ) { * * @since 7.3.0 * - * @param array $data The `data` input. + * @param int $parent_id The sent `parent`, which differs from the stored one. * @param string $post_type The post type name. * @param WP_Post|null $post The stored post on update, or null on create. - * @return array|WP_Error The parent field, empty when no parent is sent or it is unchanged, or a WP_Error. + * @return array|WP_Error The parent field, or a WP_Error. */ - private function prepare_parent( array $data, string $post_type, ?WP_Post $post ) { - if ( ! array_key_exists( 'parent', $data ) ) { - return array(); - } - - $parent_id = $data['parent']; - - // Keeping the stored parent changes nothing. - if ( $post && (int) $post->post_parent === $parent_id ) { - return array(); - } - + private function prepare_parent( $parent_id, string $post_type, ?WP_Post $post ) { if ( 0 === $parent_id ) { return array( 'post_parent' => 0 ); } @@ -1327,17 +1324,12 @@ private function prepare_parent( array $data, string $post_type, ?WP_Post $post * * @since 7.3.0 * - * @param array $data The `data` input. + * @param array $date The sent `date`. * @param WP_Post|null $post The stored post on update, or null on create. * @param string|null $status The status to set, or null when none is sent or it is unchanged. * @return array The date fields to save, empty for none. */ - private function prepare_date( array $data, ?WP_Post $post, ?string $status ): array { - if ( ! array_key_exists( 'date', $data ) ) { - return array(); - } - - $date = $data['date']; + private function prepare_date( array $date, ?WP_Post $post, ?string $status ): array { $dates = isset( $date['site'] ) ? rest_get_date_with_gmt( $date['site'] ) : rest_get_date_with_gmt( $date['gmt'], true ); $fields = array( @@ -1382,24 +1374,15 @@ private function prepare_date( array $data, ?WP_Post $post, ?string $status ): a * * @since 7.3.0 * - * @param array $data The `data` input. + * @param string $slug The sent `slug`, which differs from the stored one. * @param WP_Post_Type $post_type_object The post type of the post being written. * @param WP_Post|null $post The stored post on update, or null on create. * @param string $status The status the post will have. * @param int $parent_id The parent ID the post will have. - * @return array|WP_Error The slug field, empty when no slug is sent or it is unchanged, or a WP_Error. + * @return array|WP_Error The slug field, or a WP_Error. */ - private function prepare_slug( array $data, WP_Post_Type $post_type_object, ?WP_Post $post, string $status, int $parent_id ) { - if ( ! array_key_exists( 'slug', $data ) ) { - return array(); - } - - // Keeping the stored slug changes nothing, also the empty slug of a draft. - if ( $post && $post->post_name === $data['slug'] ) { - return array(); - } - - $slug = sanitize_title( $data['slug'] ); + private function prepare_slug( $slug, WP_Post_Type $post_type_object, ?WP_Post $post, string $status, int $parent_id ) { + $slug = sanitize_title( $slug ); // Intentionally strict: a slug that sanitizes to nothing is an error. if ( '' === $slug ) {