WebformCompositeCustomFileTest.php in Webform 8.5
File
tests/src/Functional/Composite/WebformCompositeCustomFileTest.php
View source
<?php
namespace Drupal\Tests\webform\Functional\Composite;
use Drupal\file\Entity\File;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\Tests\webform\Functional\Element\WebformElementManagedFileTestBase;
class WebformCompositeCustomFileTest extends WebformElementManagedFileTestBase {
protected static $testWebforms = [
'test_composite_custom_file',
];
public function testCustom() {
$webform = Webform::load('test_composite_custom_file');
$first_file = $this->files[0];
$edit = [
'webform_custom_composite_file[items][0][_item_][textfield]' => '{textfield}',
'files[webform_custom_composite_file_items_0__item__managed_file]' => \Drupal::service('file_system')
->realpath($first_file->uri),
];
$this
->drupalPostForm('webform/test_composite_custom_file', $edit, 'webform_custom_composite_file_table_add');
$this
->assertRaw(basename($first_file->uri));
$this
->drupalPostForm(NULL, [], 'webform_custom_composite_file_table_add');
$this
->assertRaw(basename($first_file->uri));
$this
->drupalPostForm(NULL, [], 'Submit');
$sid = $this
->getLastSubmissionId($webform);
$webform_submission = WebformSubmission::load($sid);
$fid = $this
->getLastFileId();
$file = File::load($fid);
$this
->assertRaw('<label>Attachments</label>');
$this
->assertRaw('<strong><a href="' . file_create_url($file
->getFileUri()) . '">' . $file
->getFileName() . '</a></strong> (text/plain) - 1 KB');
$this
->assertTrue($file
->isPermanent());
$element_data = $webform_submission
->getElementData('webform_custom_composite_file');
$this
->assertEqual($element_data[0]['managed_file'], $fid, '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_composite_custom_file/' . $sid . '/' . $first_file->filename);
$this
->assertFileExists($file
->getFileUri());
$this
->drupalLogin($this->rootUser);
$this
->drupalGet("/admin/structure/webform/manage/test_composite_custom_file/submission/{$sid}/edit");
$this
->assertLink($file
->getFileName());
$this
->drupalPostForm(NULL, [], 'webform_custom_composite_file_table_add');
$this
->assertLink($file
->getFileName());
}
}