Unverified Commit aeefe066 authored by Alex Pott's avatar Alex Pott
Browse files

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
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -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());
+11 −0
Original line number Diff line number Diff line
@@ -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);
}
+39 −0
Original line number Diff line number Diff line
@@ -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.
   */