WebformElementManagedFilePrivateTest.php in Webform 8.5
File
tests/src/Functional/Element/WebformElementManagedFilePrivateTest.php
View source
<?php
namespace Drupal\Tests\webform\Functional\Element;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformElementManagedFilePrivateTest extends WebformElementManagedFileTestBase {
protected static $testWebforms = [
'test_element_managed_file',
];
public function testPrivateFiles() {
$admin_submission_user = $this
->drupalCreateUser([
'administer webform submission',
]);
$webform = Webform::load('test_element_managed_file');
$elements = $webform
->getElementsDecoded();
$elements['managed_file_single']['#uri_scheme'] = 'private';
$webform
->setElements($elements);
$webform
->save();
$this
->drupalLogin($admin_submission_user);
$edit = [
'files[managed_file_single]' => \Drupal::service('file_system')
->realpath($this->files[0]->uri),
];
$sid = $this
->postSubmission($webform, $edit);
$submission = WebformSubmission::load($sid);
$fid = $this
->getLastFileId();
$file = File::load($fid);
$this
->assertEqual($submission
->getElementData('managed_file_single'), $fid, 'Test file 3 was upload to the current submission');
$this
->assertIdentical([
'webform' => [
'webform_submission' => [
$sid => '1',
],
],
], $this->fileUsage
->listUsage($file), 'The file has 3 usage.');
$this
->assertEqual($file
->getFileUri(), 'private://webform/test_element_managed_file/' . $sid . '/' . $this->files[0]->filename);
$this
->drupalGet(file_create_url($file
->getFileUri()));
$this
->assertResponse(200);
$this
->drupalLogout();
$this
->drupalGet(file_create_url($file
->getFileUri()));
$this
->assertResponse(200);
$destination_url = Url::fromUri('base://system/files', [
'query' => [
'file' => 'webform/test_element_managed_file/' . $sid . '/' . $this->files[0]->filename,
],
]);
$this
->assertUrl('user/login', [
'query' => [
'destination' => $destination_url
->toString(),
],
]);
$edit = [
'files[managed_file_single]' => \Drupal::service('file_system')
->realpath($this->files[1]->uri),
];
$this
->drupalPostForm('/webform/' . $webform
->id(), $edit, 'Preview');
$temp_file_uri = file_create_url('private://webform/test_element_managed_file/_sid_/' . basename($this->files[1]->uri));
$this
->assertNoRaw('<span class="file file--mime-text-plain file--text"><a href="' . $temp_file_uri . '" type="text/plain; length=16384">text-1.txt</a></span>');
$this
->assertRaw('<span class="file file--mime-text-plain file--text">' . basename($this->files[1]->uri) . '</span>');
$this
->drupalGet($temp_file_uri);
$this
->assertResponse(403);
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet($temp_file_uri);
$this
->assertResponse(403);
\Drupal::configFactory()
->getEditable('webform.settings')
->set('file.file_private_redirect', FALSE)
->save();
$this
->drupalLogout();
$this
->drupalGet(file_create_url($file
->getFileUri()));
$this
->assertResponse(403);
$this
->assertUrl('system/files/webform/test_element_managed_file/' . $sid . '/' . $this->files[0]->filename);
}
}