From 3be773fbcf8e3671a3b0cf4fbfc87b8fe5ac10f1 Mon Sep 17 00:00:00 2001 From: AaronGilMartinez Date: Wed, 13 Nov 2024 12:35:50 +0100 Subject: [PATCH] Issue CollaboraOnline/collabora-drupal#52: Add cases for own media permission. --- ...ewLinkTest.php => ViewsLinkFieldsTest.php} | 100 +++++++++++------- 1 file changed, 59 insertions(+), 41 deletions(-) rename tests/src/Kernel/{ViewFieldPreviewLinkTest.php => ViewsLinkFieldsTest.php} (53%) diff --git a/tests/src/Kernel/ViewFieldPreviewLinkTest.php b/tests/src/Kernel/ViewsLinkFieldsTest.php similarity index 53% rename from tests/src/Kernel/ViewFieldPreviewLinkTest.php rename to tests/src/Kernel/ViewsLinkFieldsTest.php index 78fb664a..34ad68a6 100644 --- a/tests/src/Kernel/ViewFieldPreviewLinkTest.php +++ b/tests/src/Kernel/ViewsLinkFieldsTest.php @@ -15,11 +15,11 @@ use Drupal\views\Views; /** - * Tests the core Drupal\views\Plugin\views\field\EntityOperations handler. + * Tests link fields to preview and edit medias in views. * * @group views */ -class ViewFieldPreviewLinkTest extends KernelTestBase { +class ViewsLinkFieldsTest extends KernelTestBase { use UserCreationTrait; use MediaCreationTrait; @@ -34,10 +34,16 @@ class ViewFieldPreviewLinkTest extends KernelTestBase { /** * @todo check schema errors. - * @var */ protected $strictConfigSchema = FALSE; + /** + * Media owned by current user. + * + * @var \Drupal\media\MediaInterface; + */ + protected $ownMedia = NULL; + /** * {@inheritdoc} */ @@ -64,57 +70,66 @@ protected function setUp(): void { $this->installEntitySchema('user'); $this->installConfig(['user', 'views']); $this->installSchema('file', ['file_usage']); - // @todo check user 1. - $this->createUser(); - // Create some media entities. + // Install user module avoid user 1 permissions bypass. + \Drupal::moduleHandler()->loadInclude('user', 'install'); + user_install(); + $this->createMediaType('file', ['id' => 'document']); - for ($i = 0; $i < 2; $i++) { - $this->createMediaEntity('document'); - } + // Create two medias to check access with different scopes, 'any' and 'own'. + $this->createMediaEntity('document'); + $this->ownMedia = $this->createMediaEntity('document');; ViewTestData::createTestViews(static::class, ['collabora_online_test']); } /** - * Tests entity link fields. + * Tests link fields. */ - public function testCollaboraLinks(): void { + public function testLinks(): void { // User without permissions can't see links. - $this->doTestCollaboraLinks([ - 'preview' => FALSE, - 'edit' => FALSE, - ], - $this->createUser([])); - // User with Preview permission can see Preview link. - $this->doTestCollaboraLinks([ - 'preview' => TRUE, - 'edit' => FALSE, - ], + $this->doTestLinks([ + 'preview' => [FALSE, FALSE], + 'edit' => [FALSE, FALSE], + ], + $this->createUser([])); + // User with 'Preview' permission can see preview link. + $this->doTestLinks([ + 'preview' => [TRUE, TRUE], + 'edit' => [FALSE, FALSE], + ], $this->createUser([ - 'preview document in collabora' + 'preview document in collabora' + ])); + // User with 'Edit any' permission can see edit link. + $this->doTestLinks([ + 'preview' => [FALSE, FALSE], + 'edit' => [TRUE, TRUE], + ], + $this->createUser([ + 'edit any document in collabora' + ])); + // User with 'Edit own' permission can see edit link for entities they own. + $this->doTestLinks([ + 'preview' => [FALSE, FALSE], + 'edit' => [FALSE, TRUE], + ], + $this->createUser([ + 'edit own document in collabora' ])); - // User with Edit permission can see Edit link. - $this->doTestCollaboraLinks([ - 'preview' => FALSE, - 'edit' => TRUE, - ], - $this->createUser([ - 'edit any document in collabora' - ])); - // @ Todo Edit own. } /** - * Tests whether entity links behave as expected. + * Tests that links behave as expected. * - * @param bool[] $expected_results + * @param [] $expected_results * An associative array of expected results keyed by operation. * @param \Drupal\Core\Session\AccountInterface $account * The user account to be used to run the test; */ - protected function doTestCollaboraLinks(array $expected_results, AccountInterface $account) { - $this->setCurrentUser($account); - + protected function doTestLinks(array $expected_results, AccountInterface $account) { + $this->setCurrentUser(account: $account); + // Set the current user as the owner to check 'edit own' access. + $this->ownMedia->setOwnerId($account->id())->save(); $view = Views::getView('test_collabora_links'); $view->preview(); @@ -131,18 +146,21 @@ protected function doTestCollaboraLinks(array $expected_results, AccountInterfac ], ]; - $index = 0; - foreach (Media::loadMultiple() as $media) { + $i = 0; + // Check each expected results for every media. + foreach(Media::loadMultiple() as $media) { foreach ($expected_results as $operation => $expected_result) { $expected_link = ''; - if ($expected_result) { + // The operation array contains results for each of the entities. + if ($expected_result[$i]) { $path = Url::fromRoute($info[$operation]['route'], ['media' => $media->id()])->toString(); $expected_link = '' . $info[$operation]['label'] . ''; } - $link = $view->style_plugin->getField($index, $info[$operation]['field_id']); + // We check the output, whether it is a link or is empty (access denied). + $link = $view->style_plugin->getField($i, $info[$operation]['field_id']); $this->assertEquals($expected_link, (string) $link); } - $index++; + $i++; } }