View source
<?php
namespace Drupal\Tests\filefield_sources\Functional;
use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Tests\file\Functional\FileFieldTestBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\user\Entity\Role;
abstract class FileFieldSourcesTestBase extends FileFieldTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'filefield_sources',
];
protected $adminUser;
protected $typeName = 'article';
protected $fieldName;
protected $node;
protected function setUp() {
parent::setUp();
$roles = $this->adminUser
->getRoles(TRUE);
$rid = array_pop($roles);
$role = Role::load($rid);
$this
->grantPermissions($role, [
'administer node form display',
]);
$this->node = $this
->drupalCreateNode();
$this->fieldName = strtolower($this
->randomMachineName());
$this
->createFileField($this->fieldName, 'node', $this->typeName);
}
protected function setUpImce() {
foreach ($this->adminUser
->getRoles(TRUE) as $rid) {
$role = Role::load($rid);
$this
->grantPermissions($role, [
'administer imce',
]);
$edit["roles_profiles[{$rid}][public]"] = 'member';
$this
->drupalPostForm('admin/config/media/imce', $edit, t('Save configuration'));
}
}
public function enableSources($sources = []) {
$sources += [
'upload' => TRUE,
];
$map = [
'upload' => 'Upload',
'remote' => 'Remote URL',
'clipboard' => 'Clipboard',
'reference' => 'Reference existing',
'attach' => 'File attach',
'imce' => 'File browser',
];
$sources = array_intersect_key($sources, $map);
ksort($sources);
$manage_display = 'admin/structure/types/manage/' . $this->typeName . '/form-display';
$this
->drupalGet($manage_display);
$this
->assertSession()
->responseContains("File field sources: upload");
$this
->drupalPostForm(NULL, [], $this->fieldName . "_settings_edit");
$prefix = 'fields[' . $this->fieldName . '][settings_edit_form][third_party_settings][filefield_sources][filefield_sources][sources]';
$edit = [];
foreach ($sources as $source => $enabled) {
$edit[$prefix . '[' . $source . ']'] = $enabled ? TRUE : FALSE;
}
$this
->drupalPostForm(NULL, $edit, $this->fieldName . '_plugin_settings_update');
$this
->assertSession()
->responseContains("File field sources: " . implode(', ', array_keys($sources)));
$this
->drupalPostForm(NULL, [], t('Save'));
$add_node = 'node/add/' . $this->typeName;
$this
->drupalGet($add_node);
if (count($sources) > 1) {
foreach ($sources as $source => $enabled) {
$label = $map[$source];
$this
->assertSession()
->linkExists($label);
}
}
else {
foreach ($map as $source => $label) {
$this
->assertSession()
->linkNotExists($label);
}
}
}
public function createPermanentFileEntity() {
$file = $this
->createTemporaryFileEntity();
$file->status = FILE_STATUS_PERMANENT;
$file->uid = $this->adminUser
->id();
$file
->save();
\Drupal::service('file.usage')
->add($file, 'file', 'node', $this->node
->id());
return $file;
}
public function createTemporaryFileEntity() {
$file = $this
->createTemporaryFile();
$file->filesize = filesize($file->uri);
return File::create((array) $file);
}
public function createTemporaryFile($path = '', $filename = NULL) {
if (is_null($filename)) {
$filename = $this
->randomMachineName() . '.txt';
}
if (empty($path)) {
$path = \Drupal::config('system.file')
->get('default_scheme') . '://';
}
$uri = $path . $filename;
$contents = $this
->randomString();
\Drupal::service('file_system')
->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
\Drupal::getContainer()
->get('file_system')
->chmod($path, FileSystem::CHMOD_DIRECTORY);
file_put_contents($uri, $contents);
$this
->assertTrue(is_file($uri), 'The temporary file has been created.');
\Drupal::getContainer()
->get('file_system')
->chmod($uri, FileSystem::CHMOD_FILE);
$file = new \stdClass();
$file->uri = $uri;
$file->filename = $filename;
$file->name = pathinfo($filename, PATHINFO_FILENAME);
return $file;
}
public function updateFilefieldSourcesSettings($source_key, $key, $value) {
$manage_display = 'admin/structure/types/manage/' . $this->typeName . '/form-display';
$this
->drupalGet($manage_display);
$this
->drupalPostForm(NULL, [], $this->fieldName . "_settings_edit");
$name = 'fields[' . $this->fieldName . '][settings_edit_form][third_party_settings][filefield_sources][filefield_sources]' . "[{$source_key}][{$key}]";
$edit = [
$name => $value,
];
$this
->drupalPostForm(NULL, $edit, $this->fieldName . '_plugin_settings_update');
$this
->drupalPostForm(NULL, [], t('Save'));
}
public function uploadFileByAttachSource($uri = '', $filename = '', $delta = 0) {
if ($uri) {
$edit = [
$this->fieldName . '[' . $delta . '][filefield_attach][filename]' => $uri,
];
}
else {
$edit = [];
}
$this
->drupalPostForm(NULL, $edit, $this->fieldName . '_' . $delta . '_attach');
if ($filename) {
$this
->assertFileUploaded($filename, $delta);
}
else {
$this
->assertFileNotUploaded($delta);
}
}
public function assertFileUploaded($filename, $delta = 0) {
$this
->assertSession()
->linkExists($filename);
$xpath = '//input[@name="' . $this->fieldName . '_' . $delta . '_remove_button"]';
$fields = $this
->xpath($xpath);
foreach ($fields as $field) {
$this
->assertTrue($field
->getAttribute('value') == t('Remove'), 'After uploading a file, "Remove" button is displayed.');
}
}
public function assertFileNotUploaded($delta = 0) {
$xpath = '//input[@name="' . $this->fieldName . '_' . $delta . '_remove_button"]';
$fields = $this
->xpath($xpath);
$this
->assertTrue(empty($fields), '"Remove" button is not displayed.');
}
public function uploadFileByReferenceSource($fid = 0, $filename = '', $delta = 0) {
$name = $this->fieldName . '[' . $delta . '][filefield_reference][autocomplete]';
$value = $fid ? $filename . ' [fid:' . $fid . ']' : '';
$edit = [
$name => $value,
];
$this
->drupalPostForm(NULL, $edit, $this->fieldName . '_' . $delta . '_autocomplete_select');
if ($filename) {
$this
->assertFileUploaded($filename, $delta);
}
else {
$this
->assertFileNotUploaded($delta);
}
}
public function uploadFileByClipboardSource($uri = '', $filename = '', $delta = 0) {
$prefix = $this->fieldName . '[' . $delta . '][filefield_clipboard]';
$file_content = $uri ? 'data:text/plain;base64,' . base64_encode(file_get_contents($uri)) : '';
$this
->getSession()
->getPage()
->find('css', 'input[name="' . $prefix . '[filename]"]')
->setValue($filename);
$this
->getSession()
->getPage()
->find('css', 'input[name="' . $prefix . '[contents]"]')
->setValue($file_content);
$this
->getSession()
->getPage()
->pressButton($this->fieldName . '_' . $delta . '_clipboard_upload_button');
if ($filename) {
$this
->assertFileUploaded($filename, $delta);
}
else {
$this
->assertFileNotUploaded($delta);
}
}
public function uploadFileByImceSource($uri = '', $filename = '', $delta = 0) {
$scheme = parse_url($uri, PHP_URL_SCHEME);
$imce_path = str_replace("{$scheme}://", '', $uri);
$field_name = $this->fieldName . '[' . $delta . '][filefield_imce][imce_paths]';
$this
->getSession()
->getPage()
->find('css', 'input[name="' . $field_name . '"]')
->setValue($imce_path);
$this
->getSession()
->getPage()
->pressButton($this->fieldName . '_' . $delta . '_imce_select');
if ($filename) {
$this
->assertFileUploaded($filename, $delta);
}
else {
$this
->assertFileNotUploaded($delta);
}
}
public function uploadFileByRemoteSource($url = '', $filename = '', $delta = 0) {
$name = $this->fieldName . '[' . $delta . '][filefield_remote][url]';
$edit = [
$name => $url,
];
$this
->drupalPostForm(NULL, $edit, $this->fieldName . '_' . $delta . '_transfer');
if ($filename) {
$this
->assertFileUploaded($filename, $delta);
}
else {
$this
->assertFileNotUploaded($delta);
}
}
public function uploadFileByUploadSource($uri = '', $filename = '', $delta = 0, $multiple = FALSE) {
$name = 'files[' . $this->fieldName . '_' . $delta . ']';
if ($multiple) {
$name .= '[]';
}
$edit = [
$name => $uri ? \Drupal::getContainer()
->get('file_system')
->realPath($uri) : '',
];
$this
->drupalPostForm(NULL, $edit, $this->fieldName . '_' . $delta . '_upload_button');
if ($filename) {
$this
->assertFileUploaded($filename, $delta);
}
else {
$this
->assertFileNotUploaded($delta);
}
}
public function removeFile($filename, $delta = 0) {
$this
->drupalPostForm(NULL, [], $this->fieldName . '_' . $delta . '_remove_button');
$this
->assertFileRemoved($filename);
}
public function assertFileRemoved($filename) {
$this
->assertSession()
->linkNotExists($filename);
}
public function getFieldSetting($setting_name) {
$field_definition = FieldConfig::load("node.{$this->typeName}.{$this->fieldName}");
return $field_definition
->getSetting($setting_name);
}
}