View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\file\Entity\File;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\webform\Entity\Webform;
class WebformEditorTest extends WebformBrowserTestBase {
use TestFileCreationTrait;
public static $modules = [
'file',
'filter',
'webform',
'webform_ui',
];
protected $fileUsage;
protected function setUp() {
parent::setUp();
$this
->createFilters();
$this->fileUsage = $this->container
->get('file.usage');
}
public function testWebformSettingsFiles() {
$this
->drupalLogin($this->rootUser);
$images = $this
->getTestFiles('image');
$images = array_slice($images, 0, 5);
foreach ($images as $index => $image_file) {
$images[$index] = File::create((array) $image_file);
$images[$index]
->save();
}
$this
->assertTrue($images[0]
->isTemporary());
$this
->assertTrue($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$this
->assertTrue($images[3]
->isTemporary());
$edit = [
'description[value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[0]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/manage/contact/settings', $edit, 'Save');
$this
->reloadImages($images);
$this
->assertFalse($images[0]
->isTemporary());
$this
->assertTrue($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$this
->assertTrue($images[3]
->isTemporary());
$this
->assertIdentical([
'editor' => [
'webform' => [
'contact' => '1',
],
],
], $this->fileUsage
->listUsage($images[0]), 'The file has 1 usage.');
$edit = [
'description[value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[0]
->uuid() . '"/><img data-entity-type="file" data-entity-uuid="' . $images[1]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/manage/contact/settings', $edit, 'Save');
$this
->reloadImages($images);
$this
->assertFalse($images[0]
->isTemporary());
$this
->assertFalse($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$this
->assertTrue($images[3]
->isTemporary());
$this
->assertIdentical([
'editor' => [
'webform' => [
'contact' => '1',
],
],
], $this->fileUsage
->listUsage($images[0]), 'The file has 1 usage.');
$this
->assertIdentical([
'editor' => [
'webform' => [
'contact' => '1',
],
],
], $this->fileUsage
->listUsage($images[1]), 'The file has 1 usage.');
$edit = [
'description[value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[1]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/manage/contact/settings', $edit, 'Save');
$this
->reloadImages($images);
$this
->assertTrue($images[0]
->isTemporary());
$this
->assertFalse($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$this
->assertTrue($images[3]
->isTemporary());
$this
->assertIdentical([], $this->fileUsage
->listUsage($images[0]), 'The file has 0 usage.');
$this
->assertIdentical([
'editor' => [
'webform' => [
'contact' => '1',
],
],
], $this->fileUsage
->listUsage($images[1]), 'The file has 1 usage.');
$edit = [
'key' => 'test',
'properties[text][value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[3]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/manage/contact/element/add/processed_text', $edit, 'Save');
$this
->reloadImages($images);
$this
->assertFalse($images[3]
->isTemporary());
$this
->drupalPostForm('admin/structure/webform/manage/contact/element/test/delete', [], 'Delete');
$this
->reloadImages($images);
$this
->assertTrue($images[3]
->isTemporary());
\Drupal::configFactory()
->getEditable('webform.settings')
->set('html_editor.make_unused_managed_files_temporary', FALSE)
->save();
$this
->assertTrue($images[0]
->isTemporary());
$this
->assertTrue($images[0]
->isTemporary());
$edit = [
'description[value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[0]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/manage/contact/settings', $edit, 'Save');
$this
->reloadImages($images);
$this
->assertFalse($images[0]
->isTemporary());
$edit = [
'description[value]' => '',
];
$this
->drupalPostForm('/admin/structure/webform/manage/contact/settings', $edit, 'Save');
$this
->reloadImages($images);
$this
->assertFalse($images[0]
->isTemporary());
\Drupal::configFactory()
->getEditable('webform.settings')
->set('html_editor.make_unused_managed_files_temporary', TRUE)
->save();
$edit = [
'description[value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[0]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/manage/contact/settings', $edit, 'Save');
$this
->reloadImages($images);
$this
->assertFalse($images[0]
->isTemporary());
$this
->reloadWebform('contact')
->delete();
$this
->reloadImages($images);
$this
->assertTrue($images[0]
->isTemporary());
}
public function testWebformConfigurationFiles() {
$this
->drupalLogin($this->rootUser);
$images = $this
->getTestFiles('image');
$images = array_slice($images, 0, 5);
foreach ($images as $index => $image_file) {
$images[$index] = File::create((array) $image_file);
$images[$index]
->save();
}
$this
->assertTrue($images[0]
->isTemporary());
$this
->assertTrue($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$edit = [
'form_settings[default_form_open_message][value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[0]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/config', $edit, 'Save configuration');
$this
->reloadImages($images);
$this
->assertFalse($images[0]
->isTemporary());
$this
->assertTrue($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$this
->assertIdentical([
'editor' => [
'config' => [
'webform.settings' => '1',
],
],
], $this->fileUsage
->listUsage($images[0]), 'The file has 1 usage.');
$edit = [
'form_settings[default_form_open_message][value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[0]
->uuid() . '"/><img data-entity-type="file" data-entity-uuid="' . $images[1]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/config', $edit, 'Save configuration');
$this
->reloadImages($images);
$this
->assertFalse($images[0]
->isTemporary());
$this
->assertFalse($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$this
->assertIdentical([
'editor' => [
'config' => [
'webform.settings' => '1',
],
],
], $this->fileUsage
->listUsage($images[0]), 'The file has 1 usage.');
$this
->assertIdentical([
'editor' => [
'config' => [
'webform.settings' => '1',
],
],
], $this->fileUsage
->listUsage($images[1]), 'The file has 1 usage.');
$edit = [
'form_settings[default_form_open_message][value]' => '<img data-entity-type="file" data-entity-uuid="' . $images[1]
->uuid() . '"/>',
];
$this
->drupalPostForm('/admin/structure/webform/config', $edit, 'Save configuration');
$this
->reloadImages($images);
$this
->assertTrue($images[0]
->isTemporary());
$this
->assertFalse($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
$this
->assertIdentical([], $this->fileUsage
->listUsage($images[0]), 'The file has 0 usage.');
$this
->assertIdentical([
'editor' => [
'config' => [
'webform.settings' => '1',
],
],
], $this->fileUsage
->listUsage($images[1]), 'The file has 1 usage.');
$config = \Drupal::configFactory()
->get('webform.settings');
_webform_config_delete($config);
$this
->reloadImages($images);
$this
->assertTrue($images[0]
->isTemporary());
$this
->assertTrue($images[1]
->isTemporary());
$this
->assertTrue($images[2]
->isTemporary());
}
protected function reloadImages(array &$images) {
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
foreach ($images as $index => $image) {
$images[$index] = File::load($image
->id());
}
}
}