View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\node\NodeInterface;
use Drupal\Tests\TestFileCreationTrait;
class FileTest extends EntityShareClientFunctionalTestBase {
use TestFileCreationTrait;
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected static $filesData = [
'public_file' => [
'filename' => 'test.txt',
'filemime' => 'text/plain',
'uri' => 'public://test.txt',
'file_content' => 'Drupal',
],
'public_file_sub_directory' => [
'filename' => 'test_sub_directory.txt',
'filemime' => 'text/plain',
'uri' => 'public://sub-directory/test_sub_directory.txt',
'file_content_callback' => 'generateBigFile',
],
'private_file_sub_directory' => [
'filename' => 'test_private.txt',
'filemime' => 'text/plain',
'uri' => 'private://sub-directory/test_private.txt',
'file_content' => 'Drupal',
],
'public_jpg' => [
'filename' => 'image-test.jpg',
'filemime' => 'image/jpeg',
'uri' => 'public://image-test.jpg',
],
'public_png' => [
'filename' => 'image-test.png',
'filemime' => 'image/png',
'uri' => 'public://image-test.png',
],
];
protected $filesSize = [];
protected function setUp() : void {
parent::setUp();
$this
->getTestFiles('image');
if (isset(static::$filesData['public_jpg'])) {
$this->filesSize['public_jpg'] = filesize(static::$filesData['public_jpg']['uri']);
}
if (isset(static::$filesData['public_png'])) {
$this->filesSize['public_png'] = filesize(static::$filesData['public_png']['uri']);
}
$this
->postSetupFixture();
}
protected function getImportConfigProcessorSettings() {
$processors = parent::getImportConfigProcessorSettings();
$processors['physical_file'] = [
'rename' => FALSE,
'weights' => [
'process_entity' => 0,
],
];
return $processors;
}
protected function postSetupFixture() {
$this
->prepareContent();
$this
->populateRequestService();
foreach (static::$filesData as $file_data) {
$this->fileSystem
->delete($file_data['uri']);
}
$this
->deleteContent();
}
protected function getEntitiesDataArray() {
return [
'file' => [
'en' => $this
->preparePhysicalFilesAndFileEntitiesData(),
],
'node' => [
'en' => [
'es_test_public_file' => $this
->getCompleteNodeInfos([
'field_es_test_file' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_file'),
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_public_file_description_hidden' => $this
->getCompleteNodeInfos([
'field_es_test_file' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_file'),
'display' => 0,
'description' => 'Description 1',
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_public_file_description_shown' => $this
->getCompleteNodeInfos([
'field_es_test_file' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_file'),
'display' => 1,
'description' => 'Description 2',
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_public_file_sub_directory' => $this
->getCompleteNodeInfos([
'field_es_test_file' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_file_sub_directory'),
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_private_file_sub_directory' => $this
->getCompleteNodeInfos([
'field_es_test_file' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'private_file_sub_directory'),
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_png' => $this
->getCompleteNodeInfos([
'field_es_test_image' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_png'),
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_jpeg_alt' => $this
->getCompleteNodeInfos([
'field_es_test_image' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_jpg'),
'alt' => 'Alt text 1',
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_jpeg_title' => $this
->getCompleteNodeInfos([
'field_es_test_image' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_jpg'),
'title' => 'Title text 1',
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_jpeg_alt_title' => $this
->getCompleteNodeInfos([
'field_es_test_image' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'public_jpg'),
'alt' => 'Alt text 2',
'title' => 'Title text 2',
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testBasicPull() {
$this
->commonBasicPull();
$this
->removePluginFromImportConfig('physical_file');
$this
->resetImportedContent();
foreach (static::$filesData as $file_data) {
$this
->assertFalse(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' has been deleted.');
}
$this
->pullEveryChannels();
$this
->checkCreatedEntities();
foreach (static::$filesData as $file_data) {
$this
->assertFalse(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' has not been pulled and recreated.');
}
$this
->mergePluginsToImportConfig([
'physical_file' => [
'rename' => FALSE,
'weights' => [
'process_entity' => 0,
],
],
]);
$this
->resetImportedContent();
$this
->pullEveryChannels();
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
$this
->pullEveryChannels();
foreach (static::$filesData as $file_data) {
$this
->assertTrue(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' has been pulled and recreated.');
$replaced_file_info = $this
->getReplacedFileInfo($file_data);
$this
->assertFalse(file_exists($replaced_file_info['uri']), 'The physical file ' . $replaced_file_info['filename'] . ' has not been renamed.');
}
$this
->resetImportedContent();
$this
->mergePluginsToImportConfig([
'physical_file' => [
'rename' => TRUE,
'weights' => [
'process_entity' => 0,
],
],
]);
$this
->pullEveryChannels();
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
foreach (static::$filesData as $file_data) {
$this
->assertTrue(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' has been pulled and recreated.');
$replaced_file_info = $this
->getReplacedFileInfo($file_data);
$this
->assertFalse(file_exists($replaced_file_info['uri']), 'The physical file ' . $replaced_file_info['filename'] . ' has not been renamed.');
}
$this
->pullEveryChannels();
foreach (static::$filesData as $file_data) {
$this
->assertTrue(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' still exists.');
$replaced_file_info = $this
->getReplacedFileInfo($file_data);
$this
->assertTrue(file_exists($replaced_file_info['uri']), 'The physical file ' . $replaced_file_info['filename'] . ' has been created.');
}
}
protected function generateBigFile($file_uuid, array $file_data) {
$size = 100000000;
$file_pointer = fopen($file_data['uri'], 'w');
fseek($file_pointer, $size - 1, SEEK_CUR);
fwrite($file_pointer, 'a');
fclose($file_pointer);
$this->filesSize[$file_uuid] = filesize($file_data['uri']);
}
protected function getReplacedFileInfo(array $file_data) {
$replaced_file_data = $file_data;
$uri = $file_data['uri'];
$filename = $file_data['filename'];
$parts = pathinfo($filename);
$replaced_file_name = $parts['filename'] . '_0.' . $parts['extension'];
$replaced_file_data['filename'] = $replaced_file_name;
$replaced_file_data['uri'] = str_replace($filename, $replaced_file_name, $uri);
return $replaced_file_data;
}
}