Skip to content

Commit

Permalink
#17 Adding final code review changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughdevore committed Aug 15, 2017
1 parent 6110808 commit 8351a52
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 30 deletions.
5 changes: 4 additions & 1 deletion src/Type/MediaItem/Mutation/MediaItemCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
*/
$timeout_seconds = 300;
$temp_file = download_url( $uploaded_file_url, $timeout_seconds );

/**
* Handle the error from download_url if it occurs
*/
Expand Down Expand Up @@ -120,6 +121,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
* Insert the mediaItem and retrieve it's data
*/
$file = wp_handle_sideload( $file_data, $overrides );

/**
* Handle the error from wp_handle_sideload if it occurs
*/
Expand All @@ -140,7 +142,6 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
/**
* Stop now if a user isn't allowed to edit the parent post
*/
// Attaching media to a post requires ability to edit said post.
$parent = get_post( $attachment_parent_id );

if ( null !== get_post( $attachment_parent_id ) ) {
Expand All @@ -154,6 +155,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
* Insert the mediaItem
*/
$attachment_id = wp_insert_attachment( $media_item_args, $file['file'], $attachment_parent_id );

/**
* Handle the error from wp_insert_attachment if it occurs
*/
Expand All @@ -171,6 +173,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
*/
$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
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Type/MediaItem/Mutation/MediaItemDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
* Delete the mediaItem
*/
$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
108 changes: 80 additions & 28 deletions tests/test-media-item-mutations.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function createMediaItemMutation() {
*
* @source wp-content/plugins/wp-graphql/src/Type/MediaItem/Mutation/MediaItemCreate.php:81
*/
public function testCmiFilePath() {
public function testCreateMediaItemFilePath() {
wp_set_current_user( $this->admin );
$this->create_variables['input']['filePath'] = "file:///Users/hdevore/Desktop/Current/colorado_lake.jpeg";
$actual = $this->createMediaItemMutation();
Expand All @@ -248,7 +248,7 @@ public function testCmiFilePath() {
* @access public
* @return void
*/
public function testCmiNoInput() {
public function testCreateMediaItemNoInput() {

/**
* Set up the createMediaItem mutation
Expand Down Expand Up @@ -277,7 +277,7 @@ public function testCmiNoInput() {
* @access public
* @return void
*/
public function testCmiAsSubscriber() {
public function testCreateMediaItemAsSubscriber() {
wp_set_current_user( $this->subscriber );
$actual = $this->createMediaItemMutation();
$this->assertArrayHasKey( 'errors', $actual );
Expand All @@ -291,7 +291,7 @@ public function testCmiAsSubscriber() {
* @access public
* @return void
*/
public function testCmiOtherAuthor() {
public function testCreateMediaItemOtherAuthor() {

/**
* Set up the createMediaItem mutation
Expand Down Expand Up @@ -341,13 +341,39 @@ public function testCmiOtherAuthor() {
*
* @source wp-content/plugins/wp-graphql/src/Type/MediaItem/Mutation/MediaItemCreate.php:76
*/
public function testCmiRequireFilePhp() {
public function testCreateMediaItemRequireFilePhp() {

/**
* download_url_file will equal true if the file is already included
*/
$download_url_file = require_once( ABSPATH . 'wp-admin/includes/file.php' );
$this->assertTrue( $download_url_file );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
wp_set_current_user( $this->admin );
$actual = $this->createMediaItemMutation();

$media_item_id = $actual["data"]["createMediaItem"]["mediaItem"]["id"];
$attachment_id = $actual["data"]["createMediaItem"]["mediaItem"]["mediaItemId"];

$expected = [
'data' => [
'createMediaItem' => [
'clientMutationId' => $this->clientMutationId,
'mediaItem' => [
'id' => $media_item_id,
'mediaItemId' => $attachment_id,
'title' => $this->title,
'description' => apply_filters( 'the_content', $this->description ),
'altText' => $this->altText,
'caption' => apply_filters( 'the_content', $this->caption ),
'commentStatus' => $this->commentStatus,
'date' => $this->date,
'dateGmt' => $this->dateGmt,
'slug' => $this->slug,
'status' => strtolower( $this->status ),
'pingStatus' => $this->pingStatus,
'mimeType' => 'image/gif',
],
],
],
];

$this->assertEquals( $expected, $actual );
}

/**
Expand All @@ -356,13 +382,39 @@ public function testCmiRequireFilePhp() {
*
* @source wp-content/plugins/wp-graphql/src/Type/MediaItem/Mutation/MediaItemCreate.php:167
*/
public function testCmiRequireImagePhp() {
public function testCreateMediaItemRequireImagePhp() {

/**
* download_url_file will equal true if the file is already included
*/
$image_file = require_once( ABSPATH . 'wp-admin/includes/image.php' );
$this->assertTrue( $image_file );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
wp_set_current_user( $this->admin );
$actual = $this->createMediaItemMutation();

$media_item_id = $actual["data"]["createMediaItem"]["mediaItem"]["id"];
$attachment_id = $actual["data"]["createMediaItem"]["mediaItem"]["mediaItemId"];

$expected = [
'data' => [
'createMediaItem' => [
'clientMutationId' => $this->clientMutationId,
'mediaItem' => [
'id' => $media_item_id,
'mediaItemId' => $attachment_id,
'title' => $this->title,
'description' => apply_filters( 'the_content', $this->description ),
'altText' => $this->altText,
'caption' => apply_filters( 'the_content', $this->caption ),
'commentStatus' => $this->commentStatus,
'date' => $this->date,
'dateGmt' => $this->dateGmt,
'slug' => $this->slug,
'status' => strtolower( $this->status ),
'pingStatus' => $this->pingStatus,
'mimeType' => 'image/gif',
],
],
],
];

$this->assertEquals( $expected, $actual );
}

/**
Expand All @@ -373,7 +425,7 @@ public function testCmiRequireImagePhp() {
* @access public
* @return void
*/
public function testCmiWithInvalidUrl() {
public function testCreateMediaItemWithInvalidUrl() {
wp_set_current_user( $this->author );
$this->create_variables['input']['filePath'] = 'htt://vice.co.um/images/2016/09/16/bill-murray-has-a-couple-of-shifts-at-a-brooklyn-bar-this-weekend-body-image-1473999364.jpg?crop=1xw:1xh;center,center&resize=1440:*';
$actual = $this->createMediaItemMutation();
Expand All @@ -389,7 +441,7 @@ public function testCmiWithInvalidUrl() {
* @access public
* @return void
*/
public function testCmiWithNoFile() {
public function testCreateMediaItemWithNoFile() {
wp_set_current_user( $this->author );
$this->create_variables['input']['filePath'] = 'https://i-d-images.vice.com/images/2016/09/16/bill-murray-has-a-couple-of-shifts-at-a-brooklyn-bar-this-weekend-body-image-1473999364.jpg?crop=1xw:1xh;center,center&resize=1440:*';
$actual = $this->createMediaItemMutation();
Expand All @@ -406,7 +458,7 @@ public function testCmiWithNoFile() {
* @access public
* @return void
*/
public function testCmiEditOthersPosts() {
public function testCreateMediaItemEditOthersPosts() {
$post = $this->factory()->post->create( [
'post_author' => $this->admin,
] );
Expand Down Expand Up @@ -518,7 +570,7 @@ public function updateMediaItemMutation() {
* @access public
* @return void
*/
public function testUmiInvalidId() {
public function testUpdateMediaItemInvalidId() {
$this->update_variables['input']['id'] = '12345';
$actual = $this->updateMediaItemMutation();
$this->assertArrayHasKey( 'errors', $actual );
Expand All @@ -530,7 +582,7 @@ public function testUmiInvalidId() {
*
* @souce wp-content/plugins/wp-graphql/src/Type/MediaItem/Mutation/MediaItemUpdate.php:63
*/
public function testUmiUpdatePost() {
public function testUpdateMediaItemUpdatePost() {
$test_post = $this->factory()->post->create();
$this->update_variables['input']['id'] = \GraphQLRelay\Relay::toGlobalId( 'post', $test_post );
$actual = $this->updateMediaItemMutation();
Expand All @@ -546,7 +598,7 @@ public function testUmiUpdatePost() {
* @access public
* @return void
*/
public function testUmiAsSubscriber() {
public function testUpdateMediaItemAsSubscriber() {
wp_set_current_user( $this->subscriber );
$actual = $this->updateMediaItemMutation();
$this->assertArrayHasKey( 'errors', $actual );
Expand All @@ -561,7 +613,7 @@ public function testUmiAsSubscriber() {
* @access public
* @return void
*/
public function testUmiEditOthersPosts() {
public function testUpdateMediaItemEditOthersPosts() {
$post = $this->factory()->post->create( [
'post_author' => $this->admin,
] );
Expand All @@ -581,7 +633,7 @@ public function testUmiEditOthersPosts() {
* @access public
* @return void
*/
public function testUmiAddOtherAuthorsAsAuthor() {
public function testUpdateMediaItemAddOtherAuthorsAsAuthor() {
wp_set_current_user( $this->author );
$variables['input']['authorId'] = \GraphQLRelay\Relay::toGlobalId( 'user', $this->admin );
$actual = $this->updateMediaItemMutation();
Expand All @@ -597,7 +649,7 @@ public function testUmiAddOtherAuthorsAsAuthor() {
* @access public
* @return void
*/
public function testUmiAddOtherAuthorsAsAdmin() {
public function testUpdateMediaItemAddOtherAuthorsAsAdmin() {
wp_set_current_user( $this->admin );
$variables['input']['authorId'] = \GraphQLRelay\Relay::toGlobalId( 'user', $this->author );
$actual = $this->updateMediaItemMutation();
Expand Down Expand Up @@ -694,7 +746,7 @@ public function deleteMediaItemMutation() {
* @access public
* @return void
*/
public function testDmiInvalidId() {
public function testDeleteMediaItemInvalidId() {
$this->delete_variables['input']['id'] = 12345;
$actual = $this->deleteMediaItemMutation();
$this->assertArrayHasKey( 'errors', $actual );
Expand All @@ -709,7 +761,7 @@ public function testDmiInvalidId() {
* @access public
* @return void
*/
public function testDmiAsSubscriber() {
public function testDeleteMediaItemAsSubscriber() {
wp_set_current_user( $this->subscriber );
$actual = $this->deleteMediaItemMutation();
$this->assertArrayHasKey( 'errors', $actual );
Expand All @@ -721,7 +773,7 @@ public function testDmiAsSubscriber() {
* @access public
* @return array $actual
*/
public function testDmiAlreadyInTrash() {
public function testDeleteMediaItemAlreadyInTrash() {

$deleted_media_item = $this->factory()->attachment->create( ['post_status' => 'trash'] );
$post = get_post( $deleted_media_item );
Expand Down

0 comments on commit 8351a52

Please sign in to comment.