Skip to content

Commit

Permalink
Merge 60f95bc into f1b10bb
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed Jun 12, 2020
2 parents f1b10bb + 60f95bc commit c295456
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 13 additions & 0 deletions php/class-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ public function process() {

$file = $this->import_image();
$attachment = $this->create_attachment( $file );
// @codeCoverageIgnoreStart
if ( is_wp_error( $attachment ) ) {
return $attachment;
}
// @codeCoverageIgnoreEnd

$this->process_meta();
$this->process_tags();
Expand Down Expand Up @@ -128,9 +130,12 @@ public function import_image() {
$file = $this->image->get_image_url( 'full' );
$tmp = download_url( $file );
// If there was an error downloading, return the error.

// @codeCoverageIgnoreStart
if ( is_wp_error( $tmp ) ) {
return $tmp;
}
// @codeCoverageIgnoreEnd

$file_array['name'] = $this->image->get_field( 'file' );
$file_array['tmp_name'] = $tmp;
Expand All @@ -150,18 +155,22 @@ public function import_image() {

// See https://github.com/WordPress/WordPress/blob/12709269c19d435de019b54d2bda7e4bd1ad664e/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L747-L750 .
$size_check = $this->check_upload_size( $file_array );
// @codeCoverageIgnoreStart
if ( is_wp_error( $size_check ) ) {
return $size_check;
}
// @codeCoverageIgnoreEnd

$file = wp_handle_upload( $file_array, $overrides );
// @codeCoverageIgnoreStart
if ( isset( $file['error'] ) ) {
$file = new WP_Error(
'rest_upload_unknown_error',
$file['error'],
[ 'status' => 500 ]
);
}
// @codeCoverageIgnoreEnd

return $file;
}
Expand All @@ -174,13 +183,15 @@ public function import_image() {
* @return int|WP_Error
*/
public function create_attachment( $file ) {
// @codeCoverageIgnoreStart
if ( is_wp_error( $file ) ) {
return $file;
}

if ( empty( $file ) || ! is_array( $file ) ) {
return new WP_Error( 'no_file_found', esc_html__( 'No file found', 'unsplash' ), [ 'status' => 500 ] );
}
// @codeCoverageIgnoreEnd

$url = $file['url'];
$file = $file['file'];
Expand All @@ -197,13 +208,15 @@ public function create_attachment( $file ) {

// do the validation and storage stuff.
$this->attachment_id = wp_insert_attachment( wp_slash( $attachment ), $file, $this->parent, true );
// @codeCoverageIgnoreStart
if ( is_wp_error( $this->attachment_id ) ) {
if ( 'db_update_error' === $this->attachment_id->get_error_code() ) {
$this->attachment_id->add_data( [ 'status' => 500 ] );
} else {
$this->attachment_id->add_data( [ 'status' => 400 ] );
}
}
// @codeCoverageIgnoreEnd

return $this->attachment_id;
}
Expand Down
36 changes: 35 additions & 1 deletion tests/phpunit/php/class-test-hotlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* @coversDefaultClass \Unsplash\Hotlink
*/
class Test_Hotlink extends \WP_UnitTestCase {

/**
* Admin user for test.
*
* @var int
*/
protected static $admin_id;
/**
* Hotlink instance.
*
Expand Down Expand Up @@ -49,6 +54,9 @@ class Test_Hotlink extends \WP_UnitTestCase {
* @param object $factory Factory object.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$admin_id = $factory->user->create(
[ 'role' => 'administrator' ]
);
self::$test_file = 'canola.jpg';
self::$attachment_id = $factory->attachment->create_object(
self::$test_file,
Expand All @@ -64,6 +72,13 @@ public static function wpSetUpBeforeClass( $factory ) {
self::$image_tag = get_image_tag( self::$attachment_id, 'alt', 'title', 'left' );
}

/**
* Remove fake data.
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
}

/**
* Setup.
*
Expand Down Expand Up @@ -162,6 +177,18 @@ public function test_wp_get_attachment_image_src() {
$image = image_downsize( self::$attachment_id, 'medium' );
$this->assertInternalType( 'array', $image );
$this->assertEquals( $image[0], 'https://images.unsplash.com/test.jpg?fm=jpg&q=85&w=300&h=300' );

$image = image_downsize( self::$attachment_id, [ 333, 444 ] );
$this->assertInternalType( 'array', $image );
$this->assertEquals( $image[0], 'https://images.unsplash.com/test.jpg?fm=jpg&q=85&fit=crop&w=333&h=444' );

$image = image_downsize( self::$attachment_id, 'invalid' );
$this->assertInternalType( 'array', $image );
$this->assertEquals( $image[0], 'https://images.unsplash.com/test.jpg' );

$image = image_downsize( self::$attachment_id, [ 0, 0 ] );
$this->assertInternalType( 'array', $image );
$this->assertEquals( $image[0], 'https://images.unsplash.com/test.jpg' );
}

/**
Expand Down Expand Up @@ -568,6 +595,7 @@ public function test_rest_prepare_attachment_3() {
* @covers ::rest_prepare_attachment()
*/
public function test_rest_prepare_attachment_4() {
wp_set_current_user( self::$admin_id );
$image = get_post( self::$attachment_id );
$photo = [

Expand All @@ -585,6 +613,12 @@ public function test_rest_prepare_attachment_4() {
$result = $this->hotlink->rest_prepare_attachment( $reponse, $image, $request );
$data = $result->get_data();
$this->assertArrayHasKey( 'nonces', $data );
$this->assertArrayHasKey( 'update', $data['nonces'] );
$this->assertArrayHasKey( 'edit', $data['nonces'] );
$this->assertArrayHasKey( 'delete', $data['nonces'] );
$this->assertNotFalse( $data['nonces']['update'] );
$this->assertNotFalse( $data['nonces']['edit'] );
$this->assertNotFalse( $data['nonces']['delete'] );
}

/**
Expand Down

0 comments on commit c295456

Please sign in to comment.