View source
<?php
namespace Drupal\Tests\media_acquiadam\Unit;
use Drupal\Component\Datetime\Time;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Queue\QueueWorkerManager;
use Drupal\Core\State\State;
use Drupal\media_acquiadam\Client;
use Drupal\media_acquiadam\ClientFactory;
use Drupal\media_acquiadam\Form\AcquiadamConfig;
use Drupal\media_acquiadam\Plugin\QueueWorker\AssetRefresh;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamConfigTrait;
use Drupal\Tests\UnitTestCase;
class AcquiadamConfigFormTest extends UnitTestCase {
use AcquiadamConfigTrait;
protected $container;
protected $acquiaDamConfig;
protected $state;
protected $queueWorker;
public function testGetFormId() {
$this
->assertEquals('acquiadam_config', $this->acquiaDamConfig
->getFormId());
}
public function testBuildForm() {
$form = $this->acquiaDamConfig
->buildForm([], new FormState());
$this
->assertArrayHasKey('authentication', $form);
$this
->assertArrayHasKey('username', $form['authentication']);
$this
->assertArrayHasKey('password', $form['authentication']);
$this
->assertArrayHasKey('client_id', $form['authentication']);
$this
->assertArrayHasKey('secret', $form['authentication']);
$this
->assertEquals('WDusername', $form['authentication']['username']['#default_value']);
$this
->assertEquals('WDpassword', $form['authentication']['password']['#default_value']);
$this
->assertEquals('WDclient-id', $form['authentication']['client_id']['#default_value']);
$this
->assertEquals('WDsecret', $form['authentication']['secret']['#default_value']);
$this
->assertArrayHasKey('cron', $form);
$this
->assertEquals('14400', $form['cron']['sync_interval']['#default_value']);
$this
->assertEquals(1, $form['cron']['notifications_sync']['#default_value']);
$this
->assertArrayHasKey('image', $form);
$this
->assertEquals(1280, $form['image']['size_limit']['#default_value']);
$this
->assertArrayHasKey('manual_sync', $form);
$this
->assertArrayHasKey('perform_manual_sync', $form['manual_sync']);
}
public function testPerformManualSync() {
$form = [];
$form_state = $this
->getMockBuilder(FormStateInterface::class)
->getMock();
$this
->assertFalse($this->acquiaDamConfig
->performManualSync($form, $form_state));
$this->acquiaDamConfig
->method('getActiveMediaIds')
->willReturn([
0,
1,
2,
]);
$this
->assertEquals([
0,
1,
2,
], $this->acquiaDamConfig
->performManualSync($form, $form_state));
}
public function testProcessBatchItems() {
$media_ids = range(1, 12);
$total_media_ids = count($media_ids);
$context = [];
$queue_worker_expected_arguments = array_map(function ($value) {
return [
[
'media_id' => $value,
],
];
}, $media_ids);
$this->queueWorker
->expects($this
->any())
->method('processItem')
->withConsecutive(...$queue_worker_expected_arguments)
->willReturn(TRUE);
foreach (range(1, 3) as $run) {
$this->acquiaDamConfig
->processBatchItems($media_ids, $context);
$this
->assertBatchRun($context, $run, $total_media_ids);
}
$this->state
->expects($this
->exactly(3))
->method('set')
->withConsecutive([
$this
->equalTo('media_acquiadam.notifications_starttime'),
$this
->equalTo(1560000000),
], [
$this
->equalTo('media_acquiadam.notifications_endtime'),
$this
->equalTo(NULL),
], [
$this
->equalTo('media_acquiadam.notifications_next_page'),
$this
->equalTo(NULL),
]);
$this->acquiaDamConfig
->finishBatchOperation(NULL, $context['results'], []);
}
protected function assertBatchRun(array $context, int $run, int $total_media_ids) : void {
$processed = AcquiadamConfig::BATCH_SIZE * $run;
$processed = $processed > $total_media_ids ? $total_media_ids : $processed;
$this
->assertNotEmpty($context);
$this
->assertEquals($processed, $context['sandbox']['progress']);
$this
->assertEquals($total_media_ids, $context['sandbox']['max']);
$this
->assertEquals($total_media_ids, $context['results']['total']);
if ($processed < $total_media_ids) {
$this
->assertEquals(range(1 + $processed, $total_media_ids), $context['sandbox']['items']);
}
else {
$this
->assertEmpty($context['sandbox']['items']);
}
$this
->assertEquals(1560000000, $context['results']['start_time']);
$this
->assertEquals($processed, $context['results']['processed']);
$this
->assertEquals($processed / $total_media_ids, $context['finished']);
}
protected function setUp() {
parent::setUp();
$dam_client = $this
->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$dam_client
->expects($this
->any())
->method('getAccountSubscriptionDetails')
->willReturn([]);
$acquiadam_client_factory = $this
->getMockBuilder(ClientFactory::class)
->disableOriginalConstructor()
->getMock();
$acquiadam_client_factory
->expects($this
->any())
->method('getWithCredentials')
->willReturn($dam_client);
$time = $this
->getMockBuilder(Time::class)
->disableOriginalConstructor()
->getMock();
$time
->method('getRequestTime')
->willReturn(1560000000);
$this->queueWorker = $this
->getMockBuilder(AssetRefresh::class)
->disableOriginalConstructor()
->getMock();
$queue_worker_manager = $this
->getMockBuilder(QueueWorkerManager::class)
->disableOriginalConstructor()
->getMock();
$queue_worker_manager
->expects($this
->any())
->method('createInstance')
->willReturn($this->queueWorker);
$this->state = $this
->getMockBuilder(State::class)
->disableOriginalConstructor()
->getMock();
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this
->getStringTranslationStub());
$this->container
->set('media_acquiadam.client_factory', $acquiadam_client_factory);
$this->container
->set('config.factory', $this
->getConfigFactoryStub());
$this->container
->set('datetime.time', $time);
$this->container
->set('plugin.manager.queue_worker', $queue_worker_manager);
$this->container
->set('state', $this->state);
\Drupal::setContainer($this->container);
$this->acquiaDamConfig = $this
->getMockedAcquidamConfig();
}
protected function getMockedAcquidamConfig() {
$messenger = $this
->getMockBuilder(Messenger::class)
->setMethods([
'addWarning',
'addStatus',
])
->disableOriginalConstructor()
->getMock();
$config = $this
->getMockBuilder(AcquiadamConfig::class)
->setConstructorArgs([
$this->container
->get('config.factory'),
$this->container
->get('media_acquiadam.client_factory'),
new BatchBuilder(),
$this->container
->get('datetime.time'),
$this->container
->get('plugin.manager.queue_worker'),
$this->container
->get('state'),
])
->setMethods([
'batchSet',
'getActiveMediaIds',
'messenger',
])
->getMock();
$config
->method('messenger')
->willReturn($messenger);
return $config;
}
}