View source
<?php
namespace Drupal\Tests\acquia_contenthub_s3\Kernel;
use Acquia\ContentHubClient\Settings;
use Drupal\acquia_contenthub\ContentHubCommonActions;
use Drupal\acquia_contenthub_s3\S3FileOriginLocator;
use Drupal\acquia_contenthub_s3_test\EventSubscriber\GetSettings\OverwriteContentHubAdminSettings;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\ImageStyleInterface;
use Prophecy\Argument;
class S3FileOriginLocatorTest extends S3FileKernelTestBase {
use S3FileTestTrait;
use S3FileMapTestTrait;
protected static $modules = [
'acquia_contenthub_s3_test',
'filter',
'image',
'system',
];
protected $s3FileMap;
protected $locator;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('image_style');
$this
->installSchema('file', 'file_usage');
$this->s3FileMap = $this->container
->get('acquia_contenthub_s3.file_map');
}
public function testGetS3FileSourcePublisherHasRoot(string $bucket, string $root_folder, bool $with_image_style) : void {
$this
->setS3fsConfig($bucket, $root_folder);
$this
->runTestWith('test-bucket', 'test-root', $with_image_style);
}
public function testGetS3FileSourcePublisherHasNoRoot(string $bucket, string $root_folder, bool $with_image_style) : void {
$this
->setS3fsConfig($bucket, $root_folder);
$this
->runTestWith('test-bucket', '', $with_image_style);
}
public function sourceDataProvider() : array {
return [
[
'test-bucket',
'test-root',
FALSE,
],
[
'test-bucket',
'test-root',
TRUE,
],
[
'test-bucket',
'not-same-root',
FALSE,
],
[
'test-bucket',
'not-same-root',
TRUE,
],
[
'not-same-bucket',
'test-root',
FALSE,
],
[
'not-same-bucket',
'test-root',
TRUE,
],
[
'not-same-bucket',
'',
FALSE,
],
[
'not-same-bucket',
'',
TRUE,
],
];
}
public function testNoRecordLocally() : void {
$this
->setS3fsConfig('pub-bucket', 'pub-root');
OverwriteContentHubAdminSettings::overwrite(new Settings('test-client', 'a76696bf-be45-4d42-b5c3-08cebee93798', 'api-key', 'secret-key', 'http://example.com'));
$file = $this
->createFileEntity('test.png', 's3');
$this
->createFileEntity('test.png', 's3');
$this
->createFileEntity('test.png', 's3');
$cdf_doc = $this->container
->get('acquia_contenthub_common_actions')
->getLocalCdfDocument($file);
$cdf = $cdf_doc
->getCdfEntity($file
->uuid());
$this
->truncateS3FileMap();
$uuid = $file
->uuid();
$common = $this
->prophesize(ContentHubCommonActions::class);
$common
->getRemoteEntity(Argument::type('string'))
->will(function ($args) use ($uuid, $cdf) {
return current($args) === $uuid ? $cdf : NULL;
});
$this->container
->set('acquia_contenthub_common_actions', $common
->reveal());
$this
->setS3fsConfig('sub-bucket', 'sub-root');
$this
->assertCount(0, $this
->fetchAllData());
$source = $this
->constructS3Locator()
->getS3FileSource($file
->getFileUri());
$expected = [
'bucket' => 'pub-bucket',
'root_folder' => 'pub-root',
];
$this
->assertEqual($source, $expected);
}
protected function runTestWith(string $origin_bucket, string $origin_root_folder, bool $with_image_style) : void {
$file = $this
->initFileFixture($origin_bucket, $origin_root_folder);
$uri = $file
->getFileUri();
if ($with_image_style) {
$image_style = $this
->createImageStyle();
$uri = $image_style
->buildUri($file
->getFileUri());
}
$source = $this
->constructS3Locator()
->getS3FileSource($uri);
$expected = [
'bucket' => $origin_bucket,
'root_folder' => $origin_root_folder,
];
$this
->assertEqual($source, $expected);
}
protected function constructS3Locator() : S3FileOriginLocator {
return new S3FileOriginLocator($this->container
->get('acquia_contenthub_s3.file_map'), $this->container
->get('acquia_contenthub_s3.file_storage'), $this->container
->get('acquia_contenthub_common_actions'), $this->container
->get('config.factory')
->get('acquia_contenthub.admin_settings'));
}
protected function initFileFixture(string $bucket, string $root_folder) : FileInterface {
$file = $this
->createFileEntity('test.png', 's3');
$origin = '96c3cc3f-17cf-48f3-8354-33a71300c676';
$this->s3FileMap
->record($file
->uuid(), $bucket, $root_folder, $origin);
return $file;
}
protected function createImageStyle() : ImageStyleInterface {
$style = ImageStyle::create([
'name' => 'test',
]);
$style
->save();
return $style;
}
}