diff --git a/tests/src/Kernel/CoolPreviewFormatterTest.php b/tests/src/Kernel/CoolPreviewFormatterTest.php index b7b29dd5..5be1617c 100644 --- a/tests/src/Kernel/CoolPreviewFormatterTest.php +++ b/tests/src/Kernel/CoolPreviewFormatterTest.php @@ -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; @@ -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(); } /** @@ -72,7 +69,7 @@ public function testViewElements(): void { $media = Media::create([ 'bundle' => 'document', - $field_name => ['1', '2'], + $field_name => '1', ]); $media->save(); @@ -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(), @@ -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( @@ -142,7 +124,7 @@ public function testViewElements(): void { 'access content', 'preview document in collabora', ])); - File::load('2')->delete(); + File::load('1')->delete(); $this->assertCoolPreviewField( [], [ @@ -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(); @@ -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) { @@ -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']); } }