View source
<?php
namespace Drupal\Tests\media_acquiadam\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\File\FileSystem;
use Drupal\Core\Image\ImageFactory;
use Drupal\media_acquiadam\Service\AssetImageHelper;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamAssetDataTrait;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamConfigTrait;
use Drupal\Tests\UnitTestCase;
use GuzzleHttp\Client as GuzzleClient;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
class AssetImageHelperTest extends UnitTestCase {
use AcquiadamAssetDataTrait, AcquiadamConfigTrait;
protected $container;
protected $assetImageHelper;
public function testGetThumbnailUrlBySize() {
$asset = $this
->getAssetData();
$tn_url = $this->assetImageHelper
->getThumbnailUrlBySize($asset, 50);
$this
->assertEquals('http://subdomain.webdamdb.com/s/100th_sm_0UerYozlI3.jpg', $tn_url);
$tn_url = $this->assetImageHelper
->getThumbnailUrlBySize($asset, 100);
$this
->assertEquals('http://subdomain.webdamdb.com/s/100th_sm_0UerYozlI3.jpg', $tn_url);
$tn_url = $this->assetImageHelper
->getThumbnailUrlBySize($asset, 120);
$this
->assertEquals('http://subdomain.webdamdb.com/s/100th_sm_0UerYozlI3.jpg', $tn_url);
$tn_url = $this->assetImageHelper
->getThumbnailUrlBySize($asset, 350);
$this
->assertEquals('http://subdomain.webdamdb.com/s/310th_sm_0UerYozlI3.jpg', $tn_url);
$tn_url = $this->assetImageHelper
->getThumbnailUrlBySize($asset, 12000);
$this
->assertEquals('http://subdomain.webdamdb.com/s/md_0UerYozlI3.jpg', $tn_url);
$tn_url = $this->assetImageHelper
->getThumbnailUrlBySize($asset);
$this
->assertEquals('http://subdomain.webdamdb.com/s/md_0UerYozlI3.jpg', $tn_url);
}
public function testGetFallbackThumbnail() {
$this->assetImageHelper
->method('phpFileExists')
->with('public://webdam.png')
->willReturnOnConsecutiveCalls(FALSE, FALSE, TRUE);
$this
->assertEquals('public://webdam.png_copy', $this->assetImageHelper
->getFallbackThumbnail(), 'File should be copied to new location');
$this
->assertEquals('public://webdam.png', $this->assetImageHelper
->getFallbackThumbnail(), 'Existing file should be used');
}
public function testGetGenericMediaIcon() {
$mimetype = [
'discrete' => 'image',
'sub' => 'jpg',
];
$helper = $this
->getMockedAssetImageHelper();
$helper
->method('phpFileExists')
->willReturn(TRUE);
$this
->assertEquals('public://media-icons/image-jpg.png', $helper
->getGenericMediaIcon($mimetype));
$helper = $this
->getMockedAssetImageHelper();
$helper
->method('phpFileExists')
->willReturnOnConsecutiveCalls(FALSE, TRUE);
$this
->assertEquals('public://media-icons/jpg.png', $helper
->getGenericMediaIcon($mimetype));
$helper = $this
->getMockedAssetImageHelper();
$helper
->method('phpFileExists')
->willReturnOnConsecutiveCalls(FALSE, FALSE, TRUE);
$this
->assertEquals('public://media-icons/generic.png', $helper
->getGenericMediaIcon($mimetype));
$helper = $this
->getMockedAssetImageHelper();
$helper
->method('phpFileExists')
->willReturn(FALSE);
$this
->assertFalse($helper
->getGenericMediaIcon($mimetype));
}
public function testGetMimeTypeFromFileType() {
$this
->assertArrayEquals([
'discrete' => 'image',
'sub' => 'jpg',
], $this->assetImageHelper
->getMimeTypeFromFileType('jpg'));
$this
->assertArrayEquals([
'discrete' => 'video',
'sub' => 'quicktime',
], $this->assetImageHelper
->getMimeTypeFromFileType('mov'));
$this
->assertArrayEquals([
'discrete' => 'application',
'sub' => 'pdf',
], $this->assetImageHelper
->getMimeTypeFromFileType('pdf'));
$this
->assertFalse($this->assetImageHelper
->getMimeTypeFromFileType('abc123'));
}
protected function getMockedAssetImageHelper() {
$helper = $this
->getMockBuilder(AssetImageHelper::class)
->setConstructorArgs([
$this->container
->get('config.factory'),
$this->container
->get('file_system'),
$this->container
->get('http_client'),
$this->container
->get('file.mime_type.guesser'),
$this->container
->get('image.factory'),
])
->setMethods([
'phpFileExists',
'getAcquiaDamModulePath',
'saveFallbackThumbnail',
])
->getMock();
$helper
->method('getAcquiaDamModulePath')
->willReturn('modules/contrib/media_acquiadam');
$helper
->method('saveFallbackThumbnail')
->willReturn(NULL);
return $helper;
}
protected function setUp() {
parent::setUp();
$http_client = $this
->getMockBuilder(GuzzleClient::class)
->disableOriginalConstructor()
->getMock();
$file_system = $this
->getMockBuilder(FileSystem::class)
->disableOriginalConstructor()
->setMethods([
'copy',
])
->getMockForAbstractClass();
$file_system
->method('copy')
->willReturnCallback(function ($source, $target) {
return is_string($target) ? $target . '_copy' : $target . '_blah';
});
$mime_type_guesser = $this
->getMockBuilder(MimeTypeGuesserInterface::class)
->disableOriginalConstructor()
->getMock();
$mime_type_guesser
->method('guess')
->willReturnCallback(function ($uri) {
$map = [
'public://nothing.jpg' => 'image/jpg',
'public://nothing.mov' => 'video/quicktime',
'public://nothing.pdf' => 'application/pdf',
];
return $map[$uri] ?? FALSE;
});
$image_factory = $this
->getMockBuilder(ImageFactory::class)
->disableOriginalConstructor()
->getMock();
$this->container = new ContainerBuilder();
$this->container
->set('http_client', $http_client);
$this->container
->set('file_system', $file_system);
$this->container
->set('file.mime_type.guesser', $mime_type_guesser);
$this->container
->set('image.factory', $image_factory);
$this->container
->set('config.factory', $this
->getConfigFactoryStub());
\Drupal::setContainer($this->container);
$this->assetImageHelper = $this
->getMockedAssetImageHelper();
}
}