Commit aeefe066 authored by alexpott's avatar alexpott

Issue #2923428 by undertext, Manuel Garcia, andypost, badjava, amateescu,...

Issue #2923428 by undertext, Manuel Garcia, andypost, badjava, amateescu, douggreen: Wrong check for file status in _editor_record_file_usage
parent d948f9cb
......@@ -441,8 +441,9 @@ function editor_entity_revision_delete(EntityInterface $entity) {
function _editor_record_file_usage(array $uuids, EntityInterface $entity) {
foreach ($uuids as $uuid) {
if ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid)) {
if ($file->status !== FILE_STATUS_PERMANENT) {
$file->status = FILE_STATUS_PERMANENT;
/** @var \Drupal\file\FileInterface $file */
if ($file->isTemporary()) {
$file->setPermanent();
$file->save();
}
\Drupal::service('file.usage')->add($file, 'editor', $entity->getEntityTypeId(), $entity->id());
......
......@@ -6,6 +6,7 @@
*/
use Drupal\filter\FilterFormatInterface;
use Drupal\file\FileInterface;
/**
* Implements hook_editor_js_settings_alter().
......@@ -44,3 +45,13 @@ function editor_test_editor_info_alter(&$items) {
unset($items['trex']);
}
}
/**
* Implements hook_ENTITY_TYPE_presave() for file entities.
*/
function editor_test_file_presave(FileInterface $file) {
// Use state to keep track of how many times a file is saved.
$file_save_count = \Drupal::state()->get('editor_test.file_save_count', []);
$file_save_count[$file->getFilename()] = isset($file_save_count[$file->getFilename()]) ? $file_save_count[$file->getFilename()] + 1 : 1;
\Drupal::state()->set('editor_test.file_save_count', $file_save_count);
}
......@@ -59,6 +59,45 @@ protected function setUp() {
node_add_body_field($type);
}
/**
* Tests file save operations when node with referenced files is saved.
*/
public function testFileSaveOperations() {
$permanent_image = File::create([
'uri' => 'core/misc/druplicon.png',
'status' => 1,
]);
$permanent_image->save();
$temporary_image = File::create([
'uri' => 'core/misc/tree.png',
'status' => 0,
]);
$temporary_image->save();
$body_value = '<img data-entity-type="file" data-entity-uuid="' . $permanent_image->uuid() . '" />';
$body_value .= '<img data-entity-type="file" data-entity-uuid="' . $temporary_image->uuid() . '" />';
$body[] = [
'value' => $body_value,
'format' => 'filtered_html',
];
$node = Node::create([
'type' => 'page',
'title' => 'test',
'body' => $body,
'uid' => 1,
]);
$node->save();
$file_save_count = \Drupal::state()->get('editor_test.file_save_count', []);
$this->assertEquals(1, $file_save_count[$permanent_image->getFilename()]);
$this->assertEquals(2, $file_save_count[$temporary_image->getFilename()]);
// Assert both images are now permanent.
$permanent_image = File::load($permanent_image->id());
$temporary_image = File::load($temporary_image->id());
$this->assertTrue($permanent_image->isPermanent(), 'Permanent image was saved as permanent.');
$this->assertTrue($temporary_image->isPermanent(), 'Temporary image was saved as permanent.');
}
/**
* Tests the configurable text editor manager.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment