S3FileTestTrait.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_s3/tests/src/Kernel/S3FileTestTrait.php
View source
<?php
namespace Drupal\Tests\acquia_contenthub_s3\Kernel;
use Acquia\ContentHubClient\CDF\CDFObject;
use Drupal\Core\Language\LanguageInterface;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use PHPUnit\Framework\Assert;
use function GuzzleHttp\Psr7\mimetype_from_extension;
trait S3FileTestTrait {
protected function getFileFixture(string $file_name) : array {
$location = drupal_get_path('module', 'acquia_contenthub_s3') . "/tests/fixtures/file/{$file_name}";
$data = json_decode(file_get_contents($location), TRUE);
return is_array($data) ? $data : [];
}
protected function createFileEntity(string $file_name, string $scheme, array $values = []) : FileInterface {
if ($scheme === 's3') {
$config = \Drupal::configFactory()
->getEditable('s3fs.settings');
if (!$config
->get('bucket')) {
$config
->set('bucket', 'test-bucket')
->save();
}
}
$path = explode('/', $file_name);
$data = [
'uuid' => \Drupal::service('uuid')
->generate(),
'langcode' => 'en',
'uid' => 1,
'filename' => end($path),
'uri' => sprintf("{$scheme}://%s", implode('/', $path)),
'filemime' => mimetype_from_extension($file_name),
'filesize' => rand(1000, 5000),
'status' => 1,
'created' => time(),
];
$data = array_merge($data, $values);
$file = File::create($data);
$file
->save();
return $file;
}
protected function assertCdfAttribute(CDFObject $cdf, string $attribute, $expected) : void {
$attr = $cdf
->getAttribute($attribute);
if (is_null($attr)) {
Assert::fail("Attribute ({$attribute}) doesn't exist!");
}
Assert::assertEquals($cdf
->getAttribute($attribute)
->getValue()[LanguageInterface::LANGCODE_NOT_SPECIFIED], $expected, 'CDF attribute value and expected value do not match.');
}
protected function setS3fsConfig(string $bucket, string $root_folder) : void {
\Drupal::getContainer()
->get('config.factory')
->getEditable('s3fs.settings')
->set('bucket', $bucket)
->set('root_folder', $root_folder)
->save();
}
}