From 424dc476ae58cfa51a64078a7642c6f33a9cec53 Mon Sep 17 00:00:00 2001 From: hughdevore Date: Mon, 14 Aug 2017 16:30:30 -0600 Subject: [PATCH] #17 Adding unit tests and code cleanup. --- .../MediaItem/Mutation/MediaItemUpdate.php | 3 ++ tests/test-media-item-mutations.php | 32 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Type/MediaItem/Mutation/MediaItemUpdate.php b/src/Type/MediaItem/Mutation/MediaItemUpdate.php index 6d830f48e..45c869ccd 100644 --- a/src/Type/MediaItem/Mutation/MediaItemUpdate.php +++ b/src/Type/MediaItem/Mutation/MediaItemUpdate.php @@ -96,6 +96,9 @@ public static function mutate( \WP_Post_Type $post_type_object ) { */ $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; + + update_option( 'hd_update_post', $post_args ); /** * Insert the post and retrieve the ID diff --git a/tests/test-media-item-mutations.php b/tests/test-media-item-mutations.php index 9c94abbee..bb7c2de8a 100644 --- a/tests/test-media-item-mutations.php +++ b/tests/test-media-item-mutations.php @@ -44,9 +44,12 @@ class WP_GraphQL_Test_Media_Item_Mutations extends WP_UnitTestCase { public $update_variables; public $delete_variables; - public $author; public $subscriber; + public $subscriber_name; + public $author; + public $author_name; public $admin; + public $admin_name; public $attachment_id; public $media_item_id; @@ -96,14 +99,17 @@ public function setUp() { $this->subscriber = $this->factory->user->create( [ 'role' => 'subscriber', ] ); + $this->subscriber_name = 'User ' . $this->subscriber; $this->author = $this->factory->user->create( [ 'role' => 'author', ] ); + $this->author_name = 'User ' . $this->author; $this->admin = $this->factory->user->create( [ 'role' => 'administrator', ] ); + $this->admin_name = 'User ' . $this->admin; /** * Create a mediaItem to update and store it's WordPress post ID @@ -151,6 +157,7 @@ public function setUp() { 'slug' => $this->updated_slug, 'status' => $this->updated_status, 'pingStatus' => $this->updated_pingStatus, + 'authorId' => \GraphQLRelay\Relay::toGlobalId( 'user', $this->admin ), ] ]; @@ -459,6 +466,9 @@ public function updateMediaItemMutation() { caption description mimeType + author { + username + } } } } @@ -541,7 +551,7 @@ public function testUmiEditOthersPosts() { * @access public * @return void */ - public function testUmiAddOtherAuthors() { + public function testUmiAddOtherAuthorsAsAuthor() { wp_set_current_user( $this->author ); $variables['input']['authorId'] = \GraphQLRelay\Relay::toGlobalId( 'user', $this->admin ); $actual = $this->updateMediaItemMutation(); @@ -549,6 +559,23 @@ public function testUmiAddOtherAuthors() { $variables['input']['authorId'] = false; } + /** + * Create a post as the admin and then try to upload a mediaItem + * to that post as an admin. It should be created. + * + * @source wp-content/plugins/wp-graphql/src/Type/MediaItem/MediaItemUpdate.php:83 + * @access public + * @return void + */ + public function testUmiAddOtherAuthorsAsAdmin() { + wp_set_current_user( $this->admin ); + $variables['input']['authorId'] = \GraphQLRelay\Relay::toGlobalId( 'user', $this->author ); + $actual = $this->updateMediaItemMutation(); + $actual_created = $actual['data']['updateMediaItem']['mediaItem']; + $this->assertArrayHasKey( 'id', $actual_created ); + $variables['input']['authorId'] = false; + } + /** * This function tests the updateMediaItem mutation * @@ -586,6 +613,7 @@ public function testUpdateMediaItemMutation() { 'status' => strtolower( $this->updated_status ), 'pingStatus' => $this->updated_pingStatus, 'mimeType' => 'image/gif', + 'author' => [ 'username' => 'User 196' ], ], ], ],