From cef2e7b6770b017deb54924b67932e2e313c3e23 Mon Sep 17 00:00:00 2001 From: Andreas Hennings Date: Thu, 9 Jan 2025 15:53:13 +0100 Subject: [PATCH] Issue 83: Assert that file owner is set correctly on save. --- .../Kernel/Controller/WopiControllerTest.php | 2 ++ .../Controller/WopiControllerTestBase.php | 16 +++++++++++++++- tests/src/Traits/MediaCreationTrait.php | 17 +++++++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/tests/src/Kernel/Controller/WopiControllerTest.php b/tests/src/Kernel/Controller/WopiControllerTest.php index 1964931d..1ee4eb9c 100644 --- a/tests/src/Kernel/Controller/WopiControllerTest.php +++ b/tests/src/Kernel/Controller/WopiControllerTest.php @@ -157,6 +157,8 @@ protected function doTestWopiPutFile( $this->assertSame('public://test_' . $i . '.txt', $new_file->getFileUri()); // The file name is preserved. $this->assertSame('test.txt', $new_file->getFilename()); + // The file owner is preserved. + $this->assertSame($this->fileOwner->id(), $new_file->getOwnerId()); $actual_file_content = file_get_contents($new_file->getFileUri()); $this->assertSame($new_file_content, $actual_file_content); } diff --git a/tests/src/Kernel/Controller/WopiControllerTestBase.php b/tests/src/Kernel/Controller/WopiControllerTestBase.php index b409a50e..69dcae4d 100644 --- a/tests/src/Kernel/Controller/WopiControllerTestBase.php +++ b/tests/src/Kernel/Controller/WopiControllerTestBase.php @@ -35,6 +35,13 @@ abstract class WopiControllerTestBase extends CollaboraKernelTestBase { */ protected UserInterface $user; + /** + * The user with access to perform operations. + * + * @var \Drupal\user\UserInterface + */ + protected UserInterface $fileOwner; + /** * The media where to perform operations. * @@ -74,11 +81,18 @@ protected function setUp(): void { \Drupal::database()->query("ALTER TABLE {media} AUTO_INCREMENT = 1000"); \Drupal::database()->query("ALTER TABLE {file_managed} AUTO_INCREMENT = 2000"); - $this->media = $this->createMediaEntity('document'); $this->user = $this->createUser([ 'access content', 'edit any document in collabora', ]); + // Create a separate user as file owner, to verify that the file owner id is + // set correctly. + $this->fileOwner = $this->createUser([]); + $this->media = $this->createMediaEntity( + 'document', + ['uid' => $this->user->id()], + ['uid' => $this->fileOwner->id()], + ); $fid = $this->media->getSource()->getSourceFieldValue($this->media); $this->file = File::load($fid); diff --git a/tests/src/Traits/MediaCreationTrait.php b/tests/src/Traits/MediaCreationTrait.php index b9e50732..d542ed7c 100644 --- a/tests/src/Traits/MediaCreationTrait.php +++ b/tests/src/Traits/MediaCreationTrait.php @@ -28,23 +28,28 @@ trait MediaCreationTrait { * * @param string $type * Media type. - * @param array $values + * @param array $media_values * Values for the media entity. + * @param array $file_values + * Values for the file entity. + * This should not contain 'uri'. * * @return \Drupal\media\MediaInterface * New media entity. */ - protected function createMediaEntity(string $type, array $values = []): MediaInterface { + protected function createMediaEntity(string $type, array $media_values = [], array $file_values = []): MediaInterface { file_put_contents('public://test.txt', 'Hello test'); - $file = File::create([ + $this->assertArrayNotHasKey('uri', $file_values); + $file_values += [ 'uri' => 'public://test.txt', - ]); + ]; + $file = File::create($file_values); $file->save(); - $values += [ + $media_values += [ 'bundle' => $type, 'field_media_file' => $file->id(), ]; - $media = Media::create($values); + $media = Media::create($media_values); $media->save(); return $media;