public function WebformElementManagedFileTest::testFileRename in Webform 8.5
Same name and namespace in other branches
- 6.x tests/src/Functional/Element/WebformElementManagedFileTest.php \Drupal\Tests\webform\Functional\Element\WebformElementManagedFileTest::testFileRename()
Test the file renaming feature.
The property #file_name_pattern is tested.
File
- tests/
src/ Functional/ Element/ WebformElementManagedFileTest.php, line 150
Class
- WebformElementManagedFileTest
- Test for webform element managed file handling.
Namespace
Drupal\Tests\webform\Functional\ElementCode
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);
// Edit the submission and insert 1 extra file into the multiple element.
$this
->drupalPostForm('/webform/' . $webform
->id() . '/submissions/' . $sid . '/edit', [
'files[file_multiple][]' => \Drupal::service('file_system')
->realpath($this->files[1]->uri),
], 'Save');
$this
->drupalLogout();
/** @var \Drupal\webform\WebformSubmissionInterface $submission */
$submission = WebformSubmission::load($sid);
/** @var \Drupal\file\FileInterface $single_file */
$single_file = File::load($submission
->getElementData('file_single'));
$this
->assertEqual('file_single_' . $source_for_filename . '.txt', $single_file
->getFilename());
/** @var \Drupal\file\FileInterface[] $multiple_file */
$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++;
}
/** @var \Drupal\file\FileInterface $truncate_file */
$truncate_file = File::load($submission
->getElementData('file_truncate'));
$this
->assertEqual(strlen($truncate_file
->getFileUri()), 250);
$this
->assertEqual('file_truncate_1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901.txt', $truncate_file
->getFilename());
}