function MediaEntityMetadataApiTest::testApi

Same name in other branches
  1. 10 core/modules/ckeditor5/tests/src/Functional/MediaEntityMetadataApiTest.php \Drupal\Tests\ckeditor5\Functional\MediaEntityMetadataApiTest::testApi()
  2. 11.x core/modules/ckeditor5/tests/src/Functional/MediaEntityMetadataApiTest.php \Drupal\Tests\ckeditor5\Functional\MediaEntityMetadataApiTest::testApi()

Tests the media entity metadata API.

File

core/modules/ckeditor5/tests/src/Functional/MediaEntityMetadataApiTest.php, line 212

Class

MediaEntityMetadataApiTest
Tests the media entity metadata API.

Namespace

Drupal\Tests\ckeditor5\Functional

Code

public function testApi() {
    $path = '/ckeditor5/filtered_html/media-entity-metadata';
    $token = $this->container
        ->get('csrf_token')
        ->get(ltrim($path, '/'));
    $uuid = $this->mediaImage
        ->uuid();
    $this->drupalGet($path, [
        'query' => [
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(400);
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $uuid,
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSame(json_encode([
        "type" => "image",
        'imageSourceMetadata' => [
            'alt' => 'default alt',
        ],
    ]), $this->getSession()
        ->getPage()
        ->getContent());
    $this->mediaImage
        ->set('field_media_image', [
        'target_id' => 1,
        'alt' => '',
        'title' => 'default title',
    ])
        ->save();
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $uuid,
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSame(json_encode([
        'type' => 'image',
        'imageSourceMetadata' => [
            'alt' => '',
        ],
    ]), $this->getSession()
        ->getPage()
        ->getContent());
    // Test that setting the media image field to not display alt field also
    // omits it from the API (which will in turn instruct the CKE5 plugin to not
    // show it).
    FieldConfig::loadByName('media', 'image', 'field_media_image')->setSetting('alt_field', FALSE)
        ->save();
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $uuid,
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSame(json_encode([
        'type' => 'image',
    ]), $this->getSession()
        ->getPage()
        ->getContent());
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $this->mediaFile
                ->uuid(),
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSame(json_encode([
        'type' => 'file',
    ]), $this->getSession()
        ->getPage()
        ->getContent());
    // Ensure that unpublished media returns 403.
    $this->mediaImage
        ->setUnpublished()
        ->save();
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $uuid,
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(403);
    // Ensure that valid, but non-existing UUID returns 404.
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $this->uuidService
                ->generate(),
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(404);
    // Ensure that invalid UUID returns 400.
    $this->drupalGet($path, [
        'query' => [
            'uuid' => '🦙',
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(400);
    // Ensure that users that don't have access to the filter format receive
    // either 404 or 403.
    $this->drupalLogout();
    $token = $this->container
        ->get('csrf_token')
        ->get(ltrim($path, '/'));
    $this->drupalGet($path, [
        'token' => $token,
    ]);
    $this->assertSession()
        ->statusCodeEquals(400);
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $uuid,
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(403);
    $this->mediaImage
        ->setPublished()
        ->save();
    $this->drupalGet($path, [
        'query' => [
            'uuid' => $uuid,
            'token' => $token,
        ],
    ]);
    $this->assertSession()
        ->statusCodeEquals(403);
}

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