Skip to content

Commit

Permalink
Issue 83: Assert that file owner is set correctly on save.
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Jan 9, 2025
1 parent 7517583 commit cef2e7b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 2 additions & 0 deletions tests/src/Kernel/Controller/WopiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 15 additions & 1 deletion tests/src/Kernel/Controller/WopiControllerTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);

Expand Down
17 changes: 11 additions & 6 deletions tests/src/Traits/MediaCreationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cef2e7b

Please sign in to comment.