Skip to content

Commit

Permalink
Remove multivalued field configuration and assertion improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGilMartinez committed Jan 8, 2025
1 parent 7b789e0 commit 601e3e2
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions tests/src/Kernel/CoolPreviewFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
Expand Down Expand Up @@ -53,11 +52,9 @@ protected function setUp(): void {
$this->installEntitySchema('entity_test');
$this->createMediaType('file', ['id' => 'document']);

// Create test files.
// Create test file.
file_put_contents('public://file-1.txt', $this->randomString());
File::create(['uri' => 'public://test-1.txt'])->save();
file_put_contents('public://file-2.txt', $this->randomString());
File::create(['uri' => 'public://test-2.txt'])->save();
}

/**
Expand All @@ -72,7 +69,7 @@ public function testViewElements(): void {

$media = Media::create([
'bundle' => 'document',
$field_name => ['1', '2'],
$field_name => '1',
]);
$media->save();

Expand All @@ -82,21 +79,6 @@ public function testViewElements(): void {
]));

// Field is rendered correctly.
$this->assertCoolPreviewField(
[
$media->getName(),
$media->getName(),
],
[
'contexts' => ['user.permissions'],
'tags' => ['media:1'],
'max-age' => Cache::PERMANENT,
],
$media_display->build($media)[$field_name]
);

// After deleting one of the files, only one iframe is displayed.
File::load('1')->delete();
$this->assertCoolPreviewField(
[
$media->getName(),
Expand All @@ -112,7 +94,7 @@ public function testViewElements(): void {
// Iframe is not displayed for other entities than media.
$test_display = $this->createCoolPreviewField($field_name, 'entity_test', 'entity_test');
$entity = EntityTest::create([
$field_name => ['1', '2'],
$field_name => '1',
]);
$entity->save();
$this->assertCoolPreviewField(
Expand Down Expand Up @@ -142,7 +124,7 @@ public function testViewElements(): void {
'access content',
'preview document in collabora',
]));
File::load('2')->delete();
File::load('1')->delete();
$this->assertCoolPreviewField(
[],
[
Expand Down Expand Up @@ -172,7 +154,6 @@ protected function createCoolPreviewField(string $field_name, string $entity_typ
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'file',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
]);
$field_storage->save();

Expand Down Expand Up @@ -208,8 +189,13 @@ protected function assertCoolPreviewField(array $expected_medias, array $expecte
$this->assertEqualsCanonicalizing($expected_cache, $build['#cache']);

$crawler = new Crawler((string) \Drupal::service('renderer')->renderRoot($build));

// Library is present in case we have medias to render.
$expected_libraries = $expected_medias ? ['library' => ['collabora_online/cool.previewer']] : [];
$this->assertEquals($expected_libraries, $build['#attached']);

$elements = $crawler->filter('div.cool-preview__wrapper');
$this->assertCount(count($expected_medias), $elements);
$this->assertSameSize($expected_medias, $elements);

// Check each of the files from the media.
foreach ($expected_medias as $i => $media) {
Expand All @@ -224,8 +210,6 @@ protected function assertCoolPreviewField(array $expected_medias, array $expecte
$dialog = $elements->eq($i)->filter('dialog#cool-editor__dialog.cool-editor__dialog');
$this->assertCount(1, $dialog);
$this->assertCount(1, $dialog->filter('iframe.cool-frame__preview'));
// Library for each file.
$this->assertEquals(['collabora_online/cool.previewer'], $build[$i]['#attached']['library']);
}
}

Expand Down

0 comments on commit 601e3e2

Please sign in to comment.