Skip to content

Ticket 49631 - Test coverage for media_sideload_image source_url meta - #190

Closed
killua99 wants to merge 5 commits into
WordPress:masterfrom
killua99:feature/49631-test-coverage-media-sideload-image
Closed

Ticket 49631 - Test coverage for media_sideload_image source_url meta#190
killua99 wants to merge 5 commits into
WordPress:masterfrom
killua99:feature/49631-test-coverage-media-sideload-image

Conversation

@killua99

@killua99 killua99 commented Mar 18, 2020

Copy link
Copy Markdown

@joemcgill joemcgill left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is starting to look really good, thanks for working on it. I've left a few comments that I think need to be adjusted before this is ready.

self::$different_post = $factory->post->create_and_get();
}

public function wpTearDownAfterClass() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this teardown is necessary. WordPress should handle cleaning up these fixtures itself.

@killua99 killua99 Mar 23, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually analyzing others wpTearDownAfterClass I've to be more radical and delete the post I did create.

add_filter( 'pre_http_request', [ $this, 'internal_fake_download_url' ], 10, 3 );
}

public function tearDown() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This teardown is also not needed. The reason you need to add the filter in setUp() is because it runs before every test method and filters get reset between each test method so the filter will already be removed, and WP should clean up uploads itself, I believe.

@killua99 killua99 Mar 23, 2020

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the teardown on the Unit Test Tests_Post_Attachments they do the same to clear the file system. Theme Dir they remove filter on tearDown.

More specific this method $this->remove_added_uploads();

// Just need an image content string to fill the new image, keeping the same mime type.
file_put_contents( $parsed_args['filename'], file_get_contents( DIR_TESTDATA . '/images/canola.jpg' ) );

return [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems incorrect to me. Based on my reading on where this filter should be called, I think that this would need to return more than just a response code in order to test this accurately unless I'm missing something.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hook could return a:

// An array containing 'headers', 'body', 'response', 'cookies', and 'filename' elements

Then the function download_url does need a 200 response only to continue.

	if ( 200 != $response_code ) {

So only by shocking (applying a short-circuit as class-http.php said) on the request I mock a 200 response and just copy the content into the tmp-name file where the process will continue and save the attachment and added to the post.

/**
* @ticket 49631
*/
public function test_media_sideload_image() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be broken up into several separate test methods—one for each assertion being made—or to refactor this to work with a data provider so you can run multiple independent tests by passing different inputs to a single test method.

Also, through each of these assertions, the "expected" value should be passed as the first parameter and the value being tested should be second.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this could be broken in several tests. I thought was for the same purpose, test the return value of the function and the meta of the post attached.

I'm pushing a change with the latest assert that I mix the order 👍

@mukeshpanchal27

Copy link
Copy Markdown
Member

Closing in favour of #13397

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants