View source
<?php
namespace Drupal\Tests\webform\Functional\Element;
use Drupal\file\Entity\File;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformElementManagedFileTest extends WebformElementManagedFileTestBase {
public static $modules = [
'file',
'webform',
];
protected static $testWebforms = [
'test_element_managed_file',
'test_element_managed_file_dis',
'test_element_managed_file_name',
];
protected $webform;
protected $adminSubmissionUser;
protected function setUp() {
parent::setUp();
$this->webform = Webform::load('test_element_managed_file');
$this->adminSubmissionUser = $this
->drupalCreateUser([
'administer webform submission',
]);
}
public function testFileUpload() {
$this
->drupalGet('/webform/test_element_managed_file');
$this
->assertRaw('<label for="edit-managed-file-single-button-upload-button--2" class="button button-action webform-file-button">Choose file</label>');
$this
->assertRaw('<label for="edit-managed-file-multiple-button-upload-button--2" class="button button-action webform-file-button">Choose files</label>');
$this
->assertRaw('<label style="color: red" for="edit-managed-file-single-button-custom-upload-button--2" class="button button-action webform-file-button">{Custom label}</label>');
$this
->assertRaw('Allowed types: txt, text.');
$this
->checkFileUpload('single', $this->files[0], $this->files[1]);
$this
->checkFileUpload('multiple', $this->files[2], $this->files[3]);
$this
->drupalGet('/webform/test_element_managed_file');
$this
->assertFieldByName('files[managed_file_multiple_two][]');
$this
->assertFieldByName('managed_file_multiple_two_upload_button');
$this
->drupalGet('/webform/test_element_managed_file');
$this
->assertRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-single-placeholder-file-placeholder" id="edit-managed-file-single-placeholder-file-placeholder">This is the single file upload placeholder</div>');
$this
->assertRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-multiple-placeholder-file-placeholder" id="edit-managed-file-multiple-placeholder-file-placeholder">This is the multiple file upload placeholder</div>');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/webform/test_element_managed_file/test');
$this
->assertNoRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-single-placeholder-file-placeholder" id="edit-managed-file-single-placeholder-file-placeholder">This is the single file upload placeholder</div>');
$this
->assertNoRaw('<div class="webform-managed-file-placeholder managed-file-placeholder js-form-wrapper form-wrapper" data-drupal-selector="edit-managed-file-multiple-placeholder-file-placeholder" id="edit-managed-file-multiple-placeholder-file-placeholder">This is the multiple file upload placeholder</div>');
$this
->drupalLogout();
$webform = Webform::load('test_element_managed_file');
$webform
->setElementProperties('managed_file_single', $webform
->getElementDecoded('managed_file_single') + [
'#required' => TRUE,
'#required_error' => '{Custom required error}',
]);
$webform
->save();
$this
->postSubmission($webform);
$this
->assertRaw('<h2 class="visually-hidden">Error message</h2>');
$this
->assertRaw('{Custom required error}');
}
public function testFileRename() {
$webform = Webform::load('test_element_managed_file_name');
$source_for_filename = $this
->randomMachineName();
$sid = $this
->postSubmission($webform, [
'source_for_filename' => $source_for_filename,
'files[file_single]' => \Drupal::service('file_system')
->realpath($this->files[0]->uri),
'files[file_multiple][]' => \Drupal::service('file_system')
->realpath($this->files[0]->uri),
'files[file_truncate]' => \Drupal::service('file_system')
->realpath($this->files[0]->uri),
]);
$this
->drupalLogin($this->adminSubmissionUser);
$this
->drupalPostForm('/webform/' . $webform
->id() . '/submissions/' . $sid . '/edit', [
'files[file_multiple][]' => \Drupal::service('file_system')
->realpath($this->files[1]->uri),
], 'Save');
$this
->drupalLogout();
$submission = WebformSubmission::load($sid);
$single_file = File::load($submission
->getElementData('file_single'));
$this
->assertEqual('file_single_' . $source_for_filename . '.txt', $single_file
->getFilename());
$multiple_file = File::loadMultiple($submission
->getElementData('file_multiple'));
$this
->assertCount(2, $multiple_file, 'Two files found in the multiple element.');
$i = -1;
foreach ($multiple_file as $file) {
$suffix = $i === -1 ? '' : '_' . $i;
$this
->assertEqual('file_multiple_' . $source_for_filename . $suffix . '.txt', $file
->getFilename());
$i++;
}
$truncate_file = File::load($submission
->getElementData('file_truncate'));
$this
->assertEqual(strlen($truncate_file
->getFileUri()), 250);
$this
->assertEqual('file_truncate_1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901.txt', $truncate_file
->getFilename());
}
public function testFileManagement() {
$this
->drupalLogin($this->rootUser);
$webform = Webform::load('test_element_managed_file');
$sid = $this
->postSubmissionTest($webform);
$submission = WebformSubmission::load($sid);
$managed_file_single = $this
->fileLoad($submission
->getElementData('managed_file_single'));
$this
->debug($submission
->getData());
$this
->assertNotNull($managed_file_single);
$this
->assertFalse($managed_file_single
->isTemporary());
$submission
->delete();
$managed_file_single = $this
->fileLoad($submission
->getElementData('managed_file_single'));
$this
->assertNull($managed_file_single);
$config = \Drupal::configFactory()
->getEditable('webform.settings');
$config
->set('file.delete_temporary_managed_files', FALSE);
$config
->save();
$sid = $this
->postSubmissionTest($webform);
$submission = WebformSubmission::load($sid);
$submission
->delete();
$managed_file_single = $this
->fileLoad($submission
->getElementData('managed_file_single'));
$this
->assertNotNull($managed_file_single);
$this
->assertTrue($managed_file_single
->isTemporary());
$config = \Drupal::configFactory()
->getEditable('webform.settings');
$config
->set('file.make_unused_managed_files_temporary', FALSE);
$config
->save();
$sid = $this
->postSubmissionTest($webform);
$submission = WebformSubmission::load($sid);
$submission
->delete();
$managed_file_single = $this
->fileLoad($submission
->getElementData('managed_file_single'));
$this
->assertNotNull($managed_file_single);
$this
->assertFalse($managed_file_single
->isTemporary());
}
public function testFileUploadWithDisabledResults() {
$this
->drupalLogin($this->rootUser);
$webform = Webform::load('test_element_managed_file_dis');
$sid = $this
->postSubmissionTest($webform);
$file = File::load($this
->getLastFileId());
$this
->assertNull($sid);
$this
->assertEqual($file
->getFileUri(), 'private://webform/test_element_managed_file_dis/_sid_/managed_file.txt');
$this
->assertTrue($file
->isTemporary());
$this
->assertEqual(1, \Drupal::database()
->query('SELECT COUNT(fid) AS total FROM {file_managed}')
->fetchField());
$this
->assertEqual(0, \Drupal::database()
->query('SELECT COUNT(fid) AS total FROM {file_usage}')
->fetchField());
}
protected function checkFileUpload($type, $first_file, $second_file) {
$key = 'managed_file_' . $type;
$parameter_name = $type === 'multiple' ? "files[{$key}][]" : "files[{$key}]";
$edit = [
$parameter_name => \Drupal::service('file_system')
->realpath($first_file->uri),
];
$sid = $this
->postSubmission($this->webform, $edit);
$submission = WebformSubmission::load($sid);
$fid = $this
->getLastFileId();
$file = File::load($fid);
$second = $type === 'multiple' ? [
$fid,
] : $fid;
$this
->assertEqual($submission
->getElementData($key), $second, 'Test file was upload to the current submission');
$this
->assertIdentical([
'webform' => [
'webform_submission' => [
$sid => '1',
],
],
], $this->fileUsage
->listUsage($file), 'The file has 1 usage.');
$this
->assertEqual($file
->getFileUri(), 'private://webform/test_element_managed_file/' . $sid . '/' . $first_file->filename);
$this
->assertFileExists($file
->getFileUri());
$this
->drupalLogin($this->adminSubmissionUser);
$this
->drupalGet('/admin/structure/webform/manage/test_element_managed_file/submission/' . $sid);
if ($type === 'multiple') {
$this
->assertRaw('<label>managed_file_multiple</label>');
$this
->assertRaw('<ul>');
}
if (floatval(\Drupal::VERSION) >= 9.1) {
$this
->assertRaw('<span class="file file--mime-text-plain file--text"><a href="' . file_create_url($file
->getFileUri()) . '" type="text/plain">' . $file
->getFilename() . '</a></span>');
}
else {
$this
->assertRaw('<span class="file file--mime-text-plain file--text"><a href="' . file_create_url($file
->getFileUri()) . '" type="text/plain; length=' . $file
->getSize() . '">' . $file
->getFilename() . '</a></span>');
}
if ($type === 'multiple') {
$edit = [
'managed_file_multiple[file_' . $fid . '][selected]' => TRUE,
];
$submit = 'Remove selected';
}
else {
$edit = [];
$submit = 'Remove';
}
$this
->drupalPostForm('/admin/structure/webform/manage/test_element_managed_file/submission/' . $sid . '/edit', $edit, $submit);
$edit = [
$parameter_name => \Drupal::service('file_system')
->realpath($second_file->uri),
];
$this
->drupalPostForm(NULL, $edit, 'Upload');
$this
->drupalPostForm(NULL, [], 'Save');
$new_fid = $this
->getLastFileId();
$new_file = File::load($new_fid);
\Drupal::entityTypeManager()
->getStorage('webform_submission')
->resetCache();
$submission = WebformSubmission::load($sid);
$second = $type === 'multiple' ? [
$new_fid,
] : $new_fid;
$this
->assertEqual($submission
->getElementData($key), $second, 'Test new file was upload to the current submission');
$this
->assertFileNotExists($file
->getFileUri(), 'Test file deleted from disk');
$this
->assertEqual(0, \Drupal::database()
->query('SELECT COUNT(fid) AS total FROM {file_managed} WHERE fid = :fid', [
':fid' => $fid,
])
->fetchField(), 'Test file 0 deleted from database');
$this
->assertEqual(0, \Drupal::database()
->query('SELECT COUNT(fid) AS total FROM {file_usage} WHERE fid = :fid', [
':fid' => $fid,
])
->fetchField(), 'Test file 0 deleted from database');
$this
->assertIdentical([
'webform' => [
'webform_submission' => [
$sid => '1',
],
],
], $this->fileUsage
->listUsage($new_file), 'The new file has 1 usage.');
$this
->assertFileExists('private://webform/test_element_managed_file/' . $sid . '/');
$submission
->delete();
$this
->assertFileNotExists($new_file
->getFileUri(), 'Test new file deleted from disk');
$this
->assertEqual(0, \Drupal::database()
->query('SELECT COUNT(fid) AS total FROM {file_managed} WHERE fid = :fid', [
':fid' => $new_fid,
])
->fetchField(), 'Test new file deleted from database');
$this
->assertFileNotExists('private://webform/test_element_managed_file/' . $sid . '/');
}
}