View source
<?php
namespace Drupal\Tests\media_acquiadam\Unit;
use cweagans\webdam\Entity\Asset;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\media_acquiadam\Acquiadam;
use Drupal\media_acquiadam\AssetDataInterface;
use Drupal\media_acquiadam\Client;
use Drupal\media_acquiadam\ClientFactory;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamAssetDataTrait;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamConfigTrait;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamLoggerFactoryTrait;
use Drupal\Tests\UnitTestCase;
class AcquiadamServiceTest extends UnitTestCase {
use AcquiadamConfigTrait, AcquiadamLoggerFactoryTrait, AcquiadamAssetDataTrait;
protected $container;
protected $acquiaDamClient;
public function testStaticAssetCache() {
$asset = $this
->getAssetData();
$this
->assertNull($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this->acquiaDamClient
->staticAssetCache('set', $asset->id, FALSE);
$this
->assertFalse($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this->acquiaDamClient
->staticAssetCache('set', $asset->id, $asset);
$this
->assertInstanceOf(Asset::class, $this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this->acquiaDamClient
->staticAssetCache('clear');
$this
->assertNull($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
}
public function testGetAsset() {
$asset = $this
->getAssetData();
$this
->assertNull($this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this
->assertInstanceOf(Asset::class, $this->acquiaDamClient
->getAsset($asset->id));
$this
->assertInstanceOf(Asset::class, $this->acquiaDamClient
->staticAssetCache('get', $asset->id));
$this->acquiaDamClient
->staticAssetCache('set', $asset->id, FALSE);
$this
->assertFalse($this->acquiaDamClient
->getAsset($asset->id));
$this
->assertFalse($this->acquiaDamClient
->getAsset(1234567890));
}
public function testGetFlattenedFolderList() {
$folders = $this->acquiaDamClient
->getFlattenedFolderList();
$this
->assertArrayEquals($this
->getFlattenedTopLevelFoldersData(), $folders);
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90672);
$this
->assertArrayEquals([
90673 => 'Slideshows',
90674 => 'Ad Ideas',
], $folders);
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90786);
$this
->assertArrayEquals([
90787 => 'Spreadsheets',
90788 => 'Logos',
], $folders);
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90832);
$this
->assertArrayEquals([], $folders);
$folders = $this->acquiaDamClient
->getFlattenedFolderList(90673);
$this
->assertArrayEquals([], $folders);
}
public function testSelfGetFolderData() {
$folder = $this
->getFolderData(90786);
$this
->assertObjectHasAttribute('id', $folder);
$this
->assertEquals(90786, $folder->id);
$folder = $this
->getFolderData(90788);
$this
->assertObjectHasAttribute('id', $folder);
$this
->assertEquals(90788, $folder->id);
}
protected function getFlattenedTopLevelFoldersData() {
return [
90672 => 'Marketing',
90673 => 'Slideshows',
90674 => 'Ad Ideas',
90786 => 'Sales',
90787 => 'Spreadsheets',
90788 => 'Logos',
90832 => 'Support',
];
}
protected function getFolderData($folderId, array $folders = []) {
if (empty($folders)) {
$folders = $this
->getTopLevelFoldersData();
}
foreach ($folders as $folder) {
if (!empty($folder->id) && $folder->id == $folderId) {
return $folder;
}
elseif (!empty($folder->folders)) {
$child = $this
->getFolderData($folderId, $folder->folders);
if (!empty($child)) {
return $child;
}
}
}
return NULL;
}
protected function getTopLevelFoldersData() {
return [
(object) [
'id' => '90672',
'name' => 'Marketing',
'folders' => [
(object) [
'id' => '90673',
'name' => 'Slideshows',
],
(object) [
'id' => '90674',
'name' => 'Ad Ideas',
],
],
],
(object) [
'id' => '90786',
'name' => 'Sales',
'folders' => [
(object) [
'id' => '90787',
'name' => 'Spreadsheets',
],
(object) [
'id' => '90788',
'name' => 'Logos',
],
],
],
(object) [
'id' => '90832',
'name' => 'Support',
],
];
}
protected function setUp() {
parent::setUp();
$dam_client = $this
->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$dam_client
->expects($this
->any())
->method('getFolder')
->willReturnCallback(function ($folderId) {
return $this
->getFolderData($folderId);
});
$dam_client
->expects($this
->any())
->method('getTopLevelFolders')
->willReturn($this
->getTopLevelFoldersData());
$dam_client
->method('getAsset')
->willReturnMap([
[
$this
->getAssetData()->id,
TRUE,
$this
->getAssetData(),
],
[
$this
->getAssetData()->id,
FALSE,
$this
->getAssetData(),
],
]);
$acquiadam_client_factory = $this
->getMockBuilder(ClientFactory::class)
->disableOriginalConstructor()
->getMock();
$acquiadam_client_factory
->expects($this
->any())
->method('get')
->willReturn($dam_client);
$acquiadam_asset_data = $this
->getMockBuilder(AssetDataInterface::class)
->disableOriginalConstructor()
->getMock();
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this
->getStringTranslationStub());
$this->container
->set('config.factory', $this
->getConfigFactoryStub());
$this->container
->set('logger.factory', $this
->getLoggerFactoryStub());
$this->container
->set('media_acquiadam.client_factory', $acquiadam_client_factory);
$this->container
->set('media_acquiadam.asset_data', $acquiadam_asset_data);
\Drupal::setContainer($this->container);
$this->acquiaDamClient = Acquiadam::create($this->container);
}
public function tearDown() {
$this->acquiaDamClient
->staticAssetCache('clear');
}
}