View source
<?php
namespace Drupal\Tests\webform\Functional\Element;
use Drupal\file\Entity\File;
use Drupal\webform\Entity\WebformSubmission;
class WebformElementMediaFileTest extends WebformElementManagedFileTestBase {
public static $modules = [
'file',
'image',
'webform',
];
protected static $testWebforms = [
'test_element_media_file',
];
public function testMediaFileUpload() {
$this
->drupalGet('/webform/test_element_media_file');
$this
->assertRaw('<input data-drupal-selector="edit-document-file-upload" type="file" id="edit-document-file-upload" name="files[document_file]" size="22" class="js-form-file form-file" />');
$this
->assertRaw('<input data-drupal-selector="edit-audio-file-upload" accept="audio/*" type="file" id="edit-audio-file-upload" name="files[audio_file]" size="22" class="js-form-file form-file" />');
$this
->assertRaw('<input data-drupal-selector="edit-image-file-upload" accept="image/*" type="file" id="edit-image-file-upload" name="files[image_file]" size="22" class="js-form-file form-file" />');
$this
->assertRaw('<input data-drupal-selector="edit-video-file-upload" accept="video/mp4,video/x-m4v,video/*" type="file" id="edit-video-file-upload" name="files[video_file]" size="22" class="js-form-file form-file" />');
$this
->drupalLogin($this->rootUser);
$this
->drupalPostForm('/webform/test_element_media_file/test', [], 'Preview');
$this
->assertRaw('<source src="' . $this
->getAbsoluteUrl('/system/files/webform/test_element_media_file/_sid_/audio_file_mp3.mp3') . '" type="audio/mpeg">');
$this
->assertRaw('<img class="webform-image-file" alt="image_file_jpg.jpg" title="image_file_jpg.jpg" src="' . $this
->getAbsoluteUrl('/system/files/webform/test_element_media_file/_sid_/image_file_jpg.jpg') . '" />');
$this
->assertRaw('/system/files/webform/test_element_media_file/_sid_/image_file_jpg_modal.jpg" class="js-webform-image-file-modal webform-image-file-modal">');
$this
->assertRaw('/system/files/styles/thumbnail/private/webform/test_element_media_file/_sid_/image_file_jpg_modal.jpg?itok=');
$this
->assertRaw('<source src="' . $this
->getAbsoluteUrl('/system/files/webform/test_element_media_file/_sid_/video_file_mp4.mp4') . '" type="video/mp4">');
}
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('<div class="item-list">');
}
$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.');
$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');
}
}