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() {
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 postSetupFixture() {
$this
->prepareContent();
$this
->populateRequestService();
$file_system = \Drupal::service('file_system');
foreach (static::$filesData as $file_data) {
$file_system
->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() {
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_uuid => $file_data) {
$this
->assertTrue(file_exists($file_data['uri']), 'The physical file ' . $file_data['filename'] . ' has been pulled and recreated.');
if (isset($file_data['file_content'])) {
$recreated_file_data = file_get_contents($file_data['uri']);
$this
->assertEquals($file_data['file_content'], $recreated_file_data, 'The recreated physical file ' . $file_data['filename'] . ' has the same content.');
}
if (isset($this->filesSize[$file_uuid])) {
$this
->assertEquals($this->filesSize[$file_uuid], filesize($file_data['uri']), 'The recreated physical file ' . $file_data['filename'] . ' has the same size has the original.');
}
}
}
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']);
}
}