function MediaTestBase::setUp

Same name in other branches
  1. 10 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTestBase.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTestBase::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTestBase.php, line 80

Class

MediaTestBase
Base class for CKEditor 5 Media integration tests.

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

protected function setUp() : void {
    parent::setUp();
    EntityViewMode::create([
        'id' => 'media.view_mode_1',
        'targetEntityType' => 'media',
        'status' => TRUE,
        'enabled' => TRUE,
        'label' => 'View Mode 1',
    ])->save();
    EntityViewMode::create([
        'id' => 'media.22222',
        'targetEntityType' => 'media',
        'status' => TRUE,
        'enabled' => TRUE,
        'label' => 'View Mode 2 has Numeric ID',
    ])->save();
    FilterFormat::create([
        'format' => 'test_format',
        'name' => 'Test format',
        'filters' => [
            'filter_html' => [
                'status' => TRUE,
                'settings' => [
                    'allowed_html' => '<p> <br> <strong> <em> <a href> <drupal-media data-entity-type data-entity-uuid data-align data-view-mode data-caption alt>',
                ],
            ],
            'filter_align' => [
                'status' => TRUE,
            ],
            'filter_caption' => [
                'status' => TRUE,
            ],
            'media_embed' => [
                'status' => TRUE,
                'settings' => [
                    'default_view_mode' => 'view_mode_1',
                    'allowed_view_modes' => [
                        'view_mode_1' => 'view_mode_1',
                        '22222' => '22222',
                    ],
                    'allowed_media_types' => [],
                ],
            ],
        ],
    ])->save();
    Editor::create([
        'editor' => 'ckeditor5',
        'format' => 'test_format',
        'settings' => [
            'toolbar' => [
                'items' => [
                    'sourceEditing',
                    'link',
                    'bold',
                    'italic',
                ],
            ],
            'plugins' => [
                'ckeditor5_sourceEditing' => [
                    'allowed_tags' => [],
                ],
                'media_media' => [
                    'allow_view_mode_override' => TRUE,
                ],
            ],
        ],
        'image_upload' => [
            'status' => FALSE,
        ],
    ])->save();
    $this->assertSame([], array_map(function (ConstraintViolationInterface $v) {
        return (string) $v->getMessage();
    }, iterator_to_array(CKEditor5::validatePair(Editor::load('test_format'), FilterFormat::load('test_format')))));
    // Note that media_install() grants 'view media' to all users by default.
    $this->adminUser = $this->drupalCreateUser([
        'use text format test_format',
        'bypass node access',
    ]);
    // Create a sample media entity to be embedded.
    $this->createMediaType('image', [
        'id' => 'image',
    ]);
    File::create([
        'uri' => $this->getTestFiles('image')[0]->uri,
    ])
        ->save();
    $this->media = Media::create([
        'bundle' => 'image',
        'name' => 'Screaming hairy armadillo',
        'field_media_image' => [
            [
                'target_id' => 1,
                'alt' => 'default alt',
                'title' => 'default title',
            ],
        ],
    ]);
    $this->media
        ->save();
    $this->createMediaType('file', [
        'id' => 'file',
    ]);
    File::create([
        'uri' => $this->getTestFiles('text')[0]->uri,
    ])
        ->save();
    $this->mediaFile = Media::create([
        'bundle' => 'file',
        'name' => 'Information about screaming hairy armadillo',
        'field_media_file' => [
            [
                'target_id' => 2,
            ],
        ],
    ]);
    $this->mediaFile
        ->save();
    // Set created media types for each view mode.
    EntityViewDisplay::create([
        'id' => 'media.image.view_mode_1',
        'targetEntityType' => 'media',
        'status' => TRUE,
        'bundle' => 'image',
        'mode' => 'view_mode_1',
    ])->save();
    EntityViewDisplay::create([
        'id' => 'media.image.22222',
        'targetEntityType' => 'media',
        'status' => TRUE,
        'bundle' => 'image',
        'mode' => '22222',
    ])->save();
    // Create a sample host entity to embed media in.
    $this->drupalCreateContentType([
        'type' => 'blog',
    ]);
    $this->host = $this->createNode([
        'type' => 'blog',
        'title' => 'Animals with strange names',
        'body' => [
            'value' => '<drupal-media data-entity-type="media" data-entity-uuid="' . $this->media
                ->uuid() . '" data-caption="baz"></drupal-media>',
            'format' => 'test_format',
        ],
    ]);
    $this->host
        ->save();
    $this->drupalLogin($this->adminUser);
}

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