Skip to content

Commit

Permalink
#17 Adding unit tests and code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughdevore committed Aug 14, 2017
1 parent 598668c commit 424dc47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Type/MediaItem/Mutation/MediaItemUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 30 additions & 2 deletions tests/test-media-item-mutations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ),
]
];

Expand Down Expand Up @@ -459,6 +466,9 @@ public function updateMediaItemMutation() {
caption
description
mimeType
author {
username
}
}
}
}
Expand Down Expand Up @@ -541,14 +551,31 @@ 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();
$this->assertArrayHasKey( 'errors', $actual );
$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
*
Expand Down Expand Up @@ -586,6 +613,7 @@ public function testUpdateMediaItemMutation() {
'status' => strtolower( $this->updated_status ),
'pingStatus' => $this->updated_pingStatus,
'mimeType' => 'image/gif',
'author' => [ 'username' => 'User 196' ],
],
],
],
Expand Down

0 comments on commit 424dc47

Please sign in to comment.