Skip to content

Commit

Permalink
Issue CollaboraOnline#52: Add cases for own media permission.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGilMartinez committed Nov 13, 2024
1 parent 5589f38 commit 3be773f
Showing 1 changed file with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}
*/
Expand All @@ -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();

Expand All @@ -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 = '<a href="' . $path . '">' . $info[$operation]['label'] . '</a>';
}
$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++;
}
}

Expand Down

0 comments on commit 3be773f

Please sign in to comment.