Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: image helper all methods testing #843

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions library/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,12 @@ public static function addSideloadedIdentifierToAttachment(int $attachmentId)
*/
public static function getAttachmentByRemoteUrl(string $remoteUrl)
{
require_once(ABSPATH . 'wp-admin/includes/file.php');
self::includeFile();
$file = download_url($remoteUrl);

if (is_wp_error($file)) {
return $file;
}

$fileHash = md5_file($file);

if ($fileHash === false) {
Expand All @@ -184,11 +183,20 @@ public static function getAttachmentByRemoteUrl(string $remoteUrl)
return $foundPosts[0];
}

/**
* Requires file.php from wp-admin
*/
protected static function includeFile()
{
require_once(ABSPATH . 'wp-admin/includes/file.php');
}

/**
* Gets correct image data
*
* @param int $id ID of the image attachment
* @param array|string $size Size should be an array containing two int values (height and width). Can also be a string matching predefined sizes (ex. medium).
* @param array|string $size Size should be an array containing two int values (height and width).
* Can also be a string matching predefined sizes (ex. medium).
* @return array
*/
public static function getImageAttachmentData($id, $size = 'full')
Expand Down
3 changes: 2 additions & 1 deletion patchwork.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"redefinable-internals": [
"extension_loaded",
"md5_file",
"class_exists"
]
}
}
196 changes: 195 additions & 1 deletion tests/phpunit/tests/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WP_Mock;
use WP_Mock\Tools\TestCase;
use Municipio\Helper\Image;
use tad\FunctionMocker\FunctionMocker;

/**
* Class ImageTest
Expand Down Expand Up @@ -60,6 +61,31 @@ public function testResizeReturnsImageIfImageUrlIsPresent()
$this->assertEquals('//test.url/test-100x100.jpg', $result);
}

/**
* @testdox resize returns a original image
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testResizeReturnsOriginalImage()
{
// Given
$this->mockUpDataForImage();
Mockery::mock('alias:' . \Municipio\Helper\File::class)
->shouldReceive('fileExists')
->andReturn(true, false);

WP_Mock::userFunction('image_make_intermediate_size', [
'times' => 1,
'return' => false
]);

// When
$result = Image::resize('https://test.url/test.jpg', 100, 100);

// Then
$this->assertEquals('https://test.url/test.jpg', $result);
}

/**
* @testdox urlToPath Null if global server variable isnt available.
* @runInSeparateProcess
Expand Down Expand Up @@ -174,10 +200,171 @@ public function testGetImageAttachmentDataReturnsImageIfFound()
$this->assertArrayHasKey('byline', $result);
}

/**
* @testdox addSideloadedIdentifierToAttachment Returns WP_Error if no file found.
*/
public function testAddSideloadedIdentifierToAttachmentReturnsWPErrorIfNoFileFound()
{
// Given
$this->mockUpDataForImage(false);

// When
$result = Image::addSideloadedIdentifierToAttachment(1);

// Then
$this->assertInstanceOf('WP_Error', $result);
}

/**
* @testdox addSideloadedIdentifierToAttachment Returns WP_Error if hash couldnt be created from file.
*/
public function testAddSideloadedIdentifierToAttachmentReturnsFalseIfNoHash()
{
// Given
$this->mockUpDataForImage('/test/path');

// When
$result = Image::addSideloadedIdentifierToAttachment(1);

// Then
$this->assertInstanceOf('WP_Error', $result);
}

/**
* @testdox addSideloadedIdentifierToAttachment Updates post and returns nothing when
* file found and hash calculated.
*/
public function testAddSideloadedIdentifierToAttachmentUpdatesPost()
{
// Given
$this->mockUpDataForImage('/test/path', 'testHash');

WP_Mock::userFunction('update_post_meta', [
'times' => 1,
'return' => true

]);

// When
$result = Image::addSideloadedIdentifierToAttachment(1);

// Then
$this->assertNull($result);
}

/**
* @testdox getAttachmentByRemoteUrl Returns WP_Error when no download url is found.
*/
public function testGetAttachmentByRemoteUrlReturnsWpErrorIfNoDownloadUrl()
{
// Given
$wpError = Mockery::mock(\WP_Error::class);
$this->mockedDataForGetAttachmentByRemoteUrl($wpError, true);

// When
$result = Image::getAttachmentByRemoteUrl(1);

// Then
$this->assertInstanceOf('WP_Error', $result);
}

/**
* @testdox getAttachmentByRemoteUrl Returns null if no file hash.
*/
public function testGetAttachmentByRemoteUrlReturnsNullIfNoFileHash()
{
// Given
$this->mockedDataForGetAttachmentByRemoteUrl('https://test.com', false, false);

// When
$result = Image::getAttachmentByRemoteUrl(1);

// Then
$this->assertNull($result);
}

/**
* @testdox getAttachmentByRemoteUrl Returns null if no posts found
*/
public function testGetAttachmentByRemoteUrlReturnsNullIfNoPostsFound()
{
// Given
$this->mockedDataForGetAttachmentByRemoteUrl(
'https://test.com',
false,
"testHash",
[]
);

// When
$result = Image::getAttachmentByRemoteUrl(1);

// Then
$this->assertNull($result);
}

/**
* @testdox getAttachmentByRemoteUrl Returns first post if posts found.
*/
public function testGetAttachmentByRemoteUrlReturnsFirstPostIfPostsFound()
{
// Given
$this->mockedDataForGetAttachmentByRemoteUrl(
'https://test.com',
false,
"testHash",
[$this->getMockedPost(), $this->getMockedPost()]
);

// When
$result = Image::getAttachmentByRemoteUrl(1);

// Then
$this->assertInstanceOf('WP_Post', $result);
}

/**
* Mocked post
*/
private function getMockedPost()
{
$post = Mockery::mock('WP_Post');
$post->ID = 1;

return $post;
}

/**
* Mockup data
*/
private function mockedDataForGetAttachmentByRemoteUrl(
$downloadUrl = 'test',
$isWpError = false,
$md5File = false,
$getPosts = []
) {
$mock = $this->mockStaticMethod('Municipio\Helper\Image', 'includeFile');
$mock->once()->andReturn(true);

FunctionMocker::replace('md5_file', $md5File);

WP_Mock::userFunction('download_url', [
'return' => $downloadUrl
]);

WP_Mock::userFunction('is_wp_error', [
'return' => $isWpError
]);

WP_Mock::userFunction('get_posts', [
'return' => $getPosts
]);
}

/**
* Mockup data
*/
private function mockUpDataForImage()
private function mockUpDataForImage($getAttachedFile = false, $md5File = false)
{
WP_Mock::userFunction('wp_get_attachment_url', [
'return' => 'https://test.url/test.jpg'
Expand All @@ -187,6 +374,13 @@ private function mockUpDataForImage()
'return' => true
]);

WP_Mock::userFunction('get_attached_file', [
'return' => $getAttachedFile,
]);

Mockery::mock(\WP_Error::class);
FunctionMocker::replace('md5_file', $md5File);

$_SERVER = [];
$_SERVER['DOCUMENT_ROOT'] = 'root';
$_SERVER['HTTP_HOST'] = 'test.url';
Expand Down
Loading