Skip to content

Commit

Permalink
wp-graphql#156 Adding mediaItem full test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughdevore committed Oct 5, 2017
1 parent ca0acfd commit 35b41c3
Show file tree
Hide file tree
Showing 5 changed files with 493 additions and 200 deletions.
45 changes: 20 additions & 25 deletions src/Type/MediaItem/Mutation/MediaItemCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
throw new \Exception( __( 'Sorry, you are not allowed to upload mediaItems', 'wp-graphql' ) );
}

/**
* If the mediaItem being created is being assigned to another user that's not the current user, make sure
* the current user has permission to edit others mediaItems
*/
if ( ! empty( $input['authorId'] ) && get_current_user_id() !== $input['authorId'] && ! current_user_can( $post_type_object->cap->edit_others_posts ) ) {
throw new \Exception( __( 'Sorry, you are not allowed to create mediaItems as this user', 'wp-graphql' ) );
}

/**
* Set the file name, whether it's a local file or from a URL.
* Then set the url for the uploaded file
Expand All @@ -78,7 +70,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
/**
* If the mediaItem file is from a local server, use wp_upload_bits before saving it to the uploads folder
*/
if ( 'file' === parse_url( $input['filePath'], PHP_URL_SCHEME) ) {
if ( 'file' === parse_url( $input['filePath'], PHP_URL_SCHEME ) ) {
$uploaded_file = wp_upload_bits( $file_name, null, file_get_contents( $input['filePath'] ) );
$uploaded_file_url = $uploaded_file['url'];
}
Expand All @@ -102,7 +94,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
*/
$file_data = [
'name' => $file_name,
'type' => $input['fileType'],
'type' => ! empty ( $input['fileType'] ) ? $input['fileType'] : wp_check_filetype( $temp_file ),
'tmp_name' => $temp_file,
'error' => 0,
'size' => filesize( $temp_file ),
Expand Down Expand Up @@ -152,35 +144,38 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
}

/**
* Insert the mediaItem
* If the mediaItem being created is being assigned to another user that's not the current user, make sure
* the current user has permission to edit others mediaItems
*/
$attachment_id = wp_insert_attachment( $media_item_args, $file['file'], $attachment_parent_id );
if ( ! empty( $input['authorId'] ) && get_current_user_id() !== $input['authorId'] && ! current_user_can( $post_type_object->cap->edit_others_posts ) ) {
throw new \Exception( __( 'Sorry, you are not allowed to create mediaItems as this user', 'wp-graphql' ) );
}

/**
* Handle the error from wp_insert_attachment if it occurs
* Insert the mediaItem
*
* Required Argument defaults are set in the main MediaItemMutation.php if they aren't set
* by the user during input, they are:
* post_title (pulled from file if not entered)
* post_content (empty string if not entered)
* post_status (inherit if not entered)
* post_mime_type (pulled from the file if not entered in the mutation)
*/
if ( 0 === $attachment_id ) {
throw new \Exception( __( 'Sorry, the mediaItem failed to create', 'wp-graphql' ) );
}
$attachment_id = wp_insert_attachment( $media_item_args, $file['file'], $attachment_parent_id );

/**
* Check if the wp_generate_attachment_metadata method exists and include it if not
*/
require_once( ABSPATH . 'wp-admin/includes/image.php' );

/**
* Generate and update the mediaItem's metadata
* Generate and update the mediaItem's metadata.
* If we make it this far the file and attachment
* have been validated and we will not receive any errors
*/
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file['file'] );
$attachment_data_update = wp_update_attachment_metadata( $attachment_id, $attachment_data );

/**
* Handle the error from wp_update_attachment_metadata if it occurs
*/
if ( false === $attachment_data_update ) {
throw new \Exception( __( 'Sorry, the mediaItem metadata failed to update', 'wp-graphql' ) );
}

/**
* Update alt text postmeta for mediaItem
*/
Expand Down Expand Up @@ -208,7 +203,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
private static function input_fields( $post_type_object ) {

/**
* Update mutations require an filePath to be passed
* Creating mutations requires a filePath to be passed
*/
return array_merge(
[
Expand Down
17 changes: 9 additions & 8 deletions src/Type/MediaItem/Mutation/MediaItemDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
*/
$media_item_before_delete = get_post( absint( $id_parts['id'] ) );

/**
* If the mediaItem isn't of the attachment post type, throw an error
*/
if ( 'attachment' !== $media_item_before_delete->post_type ) {
throw new \Exception( sprintf( __( 'Sorry, the item you are trying to delete is a %1%s, not a mediaItem', 'wp-graphql' ), $media_item_before_delete->post_type ) );
}

/**
* If the mediaItem is already in the trash, and the forceDelete input was not passed,
* don't remove from the trash
Expand All @@ -108,17 +115,11 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
}

/**
* Delete the mediaItem
* Delete the mediaItem. This will not throw false thanks to
* all of the above validation
*/
$deleted = wp_delete_attachment( $id_parts['id'], $force_delete );

/**
* Handle the error from wp_delete_attachment if it occurs
*/
if ( false === $deleted ) {
throw new \Exception( __( 'Sorry, the mediaItem failed to delete', 'wp-graphql' ) );
}

/**
* If the post was moved to the trash, spoof the object's status before returning it
*/
Expand Down
10 changes: 7 additions & 3 deletions src/Type/MediaItem/Mutation/MediaItemMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static function prepare_media_item( $input, $post_type_object, $mutation_
}

$author_id_parts = ! empty( $input['authorId'] ) ? Relay::fromGlobalId( $input['authorId'] ) : null;
if ( is_array( $author_id_parts ) && ! empty( $author_id_parts['id'] ) && is_int( $author_id_parts['id'] ) ) {
if ( is_array( $author_id_parts ) && ! empty( $author_id_parts['id'] ) ) {
$insert_post_args['post_author'] = absint( $author_id_parts['id'] );
}

Expand All @@ -164,6 +164,8 @@ public static function prepare_media_item( $input, $post_type_object, $mutation_

if ( ! empty( $input['description'] ) ) {
$insert_post_args['post_content'] = $input['description'];
} else {
$insert_post_args['post_content'] = '';
}

if ( ! empty( $file['type'] ) ) {
Expand All @@ -172,7 +174,7 @@ public static function prepare_media_item( $input, $post_type_object, $mutation_
$insert_post_args['post_mime_type'] = $input['fileType'];
}

if ( ! empty( $input['parentId'] ) && null !== get_post( $input['parentId'] ) ) {
if ( ! empty( $input['parentId'] ) ) {
$insert_post_args['post_parent'] = $input['parentId'];
}
$parent_id_parts = ( ! empty( $input['parentId'] ) ? Relay::fromGlobalId( $input['parentId'] ) : null );
Expand Down Expand Up @@ -207,7 +209,9 @@ public static function update_additional_media_item_data( $media_item_id, $input
/**
* Update alt text postmeta for the mediaItem
*/
update_post_meta( $media_item_id, '_wp_attachment_image_alt', $input['altText'] );
if ( ! empty( $input['altText'] ) ) {
update_post_meta( $media_item_id, '_wp_attachment_image_alt', $input['altText'] );
}

/**
* Run an action after the additional data has been updated. This is a great spot to hook into to
Expand Down
30 changes: 13 additions & 17 deletions src/Type/MediaItem/Mutation/MediaItemUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
/**
* If there's no existing mediaItem, throw an exception
*/
if ( empty( $id_parts['id'] ) || null === $existing_media_item ) {
throw new \Exception( __( 'No mediaItems could be found to update', 'wp-graphql' ) );
if ( null === $existing_media_item ) {
throw new \Exception( __( 'No mediaItem with that ID could be found to update', 'wp-graphql' ) );
} else {
$author_id = $existing_media_item->post_author;
}

/**
Expand All @@ -75,41 +77,35 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
/**
* If the mutation is setting the author to be someone other than the user making the request
* make sure they have permission to edit others posts
*
* Set the Author ID
*
* Check to see if it matches the current user, if not they need to be able to edit others posts
*/
**/
if ( ! empty( $input['authorId'] ) ) {
$author_id_parts = Relay::fromGlobalId( $input['authorId'] );
$author_id = $author_id_parts['id'];
} else {
$author_id = $existing_media_item->post_author;
}

/**
* Check to see if the existing_media_item author matches the current user,
* if not they need to be able to edit others posts to proceed
*/
if ( get_current_user_id() !== $author_id && ! current_user_can( $post_type_object->cap->edit_others_posts ) ) {
throw new \Exception( __( 'Sorry, you are not allowed to update mediaItems as this user.', 'wp-graphql' ) );
}

/**
* insert the post object and get the ID
*/
$post_args = MediaItemMutation::prepare_media_item( $input, $post_type_object, $mutation_name, false );
$post_args = MediaItemMutation::prepare_media_item( $input, $post_type_object, $mutation_name, false );
$post_args['ID'] = absint( $id_parts['id'] );
$post_args['post_author'] = $author_id;

/**
* Insert the post and retrieve the ID
*
* This will not fail as long as we have an ID in $post_args
* Thanks to the validation above we will always have the ID
*/
$post_id = wp_update_post( wp_slash( (array) $post_args ), true );

/**
* Throw an exception if the post failed to update
*/
if ( is_wp_error( $post_id ) ) {
throw new \Exception( __( 'The object failed to update but no error was provided', 'wp-graphql' ) );
}

/**
* This updates additional data not part of the posts table (postmeta, terms, other relations, etc)
*
Expand Down
Loading

0 comments on commit 35b41c3

Please sign in to comment.