function EditorFileUsageTest::testFileSaveOperations

Same name in other branches
  1. 9 core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php \Drupal\Tests\editor\Kernel\EditorFileUsageTest::testFileSaveOperations()
  2. 8.9.x core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php \Drupal\Tests\editor\Kernel\EditorFileUsageTest::testFileSaveOperations()
  3. 10 core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php \Drupal\Tests\editor\Kernel\EditorFileUsageTest::testFileSaveOperations()

Tests file save operations when node with referenced files is saved.

File

core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php, line 84

Class

EditorFileUsageTest
Tests tracking of file usage by the Text Editor module.

Namespace

Drupal\Tests\editor\Kernel

Code

public function testFileSaveOperations() : void {
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.