Skip to content

Commit

Permalink
fix: CCE user cannot update artifact sometimes
Browse files Browse the repository at this point in the history
How to reproduce:
Have an artifact A with a comment containing a cross reference to another
artifact B like `See art #123`.
In the tracker of the artifact A, upload a Custom Code Execution module
in the administration.

There is an example module if you need one:
```
cd plugins/tracker_cce/wasm_modules/post-action-add-comment
nix-shell
cargo build --target wasm32-wasi --release
```
Then you can upload the generated file:
`plugins/tracker_cce/wasm_modules/post-action-add-comment/target/wasm32-wasi/release/post-action-add-comment.wasm`

The example module take an integer field `field_a`, an integer field
`field_b`, and fill a text field `field_sum`. You will need them for the
test. Or use your own module.

Now update the artifact A. The (async) execution of the module should be a
success and a new comment is added to the artifact by CCE user.

Closes request #35862: Artifact userCanView not properly cached
Part of story #35093: execute custom code as artifact post action

Change-Id: Id08ce6570c1dd2c7ebedcd70339f9d9d6b008d58
  • Loading branch information
nterray committed Jan 19, 2024
1 parent 174216e commit 0329e21
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function setUp(): void
$this->tracker = TrackerTestBuilder::aTracker()->build();
$this->artifact = ArtifactTestBuilder::anArtifact(1)
->inTracker($this->tracker)
->userCanView(true)
->userCanView($this->user)
->withChangesets($this->changeset)
->build();
$this->title_field = TrackerFormElementStringFieldBuilder::aStringField(301)->build();
Expand All @@ -93,7 +93,7 @@ public function testItReturnNothingOptionWhenUserCanNotViewArtifact(): void
{
$artifact = ArtifactTestBuilder::anArtifact(1)
->inTracker($this->tracker)
->userCanView(false)
->userCannotView($this->user)
->build();

self::assertTrue($this->builder->build($artifact, $this->user, $this->project)->isNothing());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function testItReturnsAMilestone(): void
->withTitle('title')
->inTracker($planning_tracker)
->withChangesets($changeset)
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function testItCanRetrieveSubMilestonesOfAGivenMilestone(): void
$release_1_0 = ArtifactTestBuilder::anArtifact(1)
->withTitle('release_1_0')
->withChangesets(ChangesetTestBuilder::aChangeset('1')->build())
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand All @@ -149,7 +149,7 @@ public function testItCanRetrieveSubMilestonesOfAGivenMilestone(): void
->withTitle('sprint_1')
->withChangesets(ChangesetTestBuilder::aChangeset('2')->build())
->inTracker($sprints_tracker)
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand All @@ -158,7 +158,7 @@ public function testItCanRetrieveSubMilestonesOfAGivenMilestone(): void
->withTitle('sprint_2')
->withChangesets(ChangesetTestBuilder::aChangeset('3')->build())
->inTracker($sprints_tracker)
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand All @@ -167,7 +167,7 @@ public function testItCanRetrieveSubMilestonesOfAGivenMilestone(): void
->withTitle('hackfest_2012')
->withChangesets(ChangesetTestBuilder::aChangeset('4')->build())
->inTracker($hackfests_tracker)
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand Down Expand Up @@ -228,7 +228,7 @@ public function testItBuildsBareMilestoneFromAnArtifact(): void
->withTitle('release_1_0')
->withChangesets(ChangesetTestBuilder::aChangeset('1')->build())
->inTracker($tracker)
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private function anArtifact(int $artifact_id, \Tracker $milestone_tracker): Arti
->withTitle('title')
->inTracker($milestone_tracker)
->withChangesets($changeset)
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ public function testItReturnsMilestonePerArtifact(): void
$artifact_1 = ArtifactTestBuilder::anArtifact(1)
->inTracker($this->tracker)
->withChangesets(ChangesetTestBuilder::aChangeset('1')->build())
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
->build();
$artifact_2 = ArtifactTestBuilder::anArtifact(2)
->inTracker($this->tracker)
->withChangesets(ChangesetTestBuilder::aChangeset('2')->build())
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testItSkipsArtifactsWithoutChangeset(): void
$artifact_2 = ArtifactTestBuilder::anArtifact(2)
->inTracker($this->tracker)
->withChangesets(ChangesetTestBuilder::aChangeset('1')->build())
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->withAncestors([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private function getAnArtifact(int $id, bool $is_open, string $status, \Tracker
return ArtifactTestBuilder::anArtifact($id)
->inTracker($tracker)
->withChangesets(ChangesetTestBuilder::aChangeset('1')->build())
->userCanView(true)
->userCanView($this->user)
->withParent(null)
->withStatus($status)
->isOpen($is_open)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public function testItSkipsArtifactsThatDoNotHaveStartDate(int $level): void
)
);

$iteration = $this->anArtifactWithoutChangesetValue(201, 'Title', $this->tracker, true);
$iteration = $this->anArtifactWithoutChangesetValue(201, 'Title', $this->tracker);

$this->artifact_factory
->expects(self::once())
Expand Down Expand Up @@ -1293,7 +1293,7 @@ public function testItReturnsPaginatedListOfIterationRepresentation(int $level):
)
);

$iteration = $this->anArtifact(123, "Sprint W42", $this->tracker, true);
$iteration = $this->anArtifact(123, "Sprint W42", $this->tracker);

$this->artifact_factory
->expects(self::once())
Expand Down Expand Up @@ -1353,7 +1353,7 @@ private function mockRoadmapConfig(array $db_result): void
->willReturn($db_result);
}

private function anArtifact(int $id, string $title, Tracker $tracker, bool $readable): Artifact
private function anArtifact(int $id, string $title, Tracker $tracker): Artifact
{
$changeset = $this->createMock(\Tracker_Artifact_Changeset::class);
$changeset_value = $this->createMock(\Tracker_Artifact_ChangesetValue_Date::class);
Expand All @@ -1364,13 +1364,13 @@ private function anArtifact(int $id, string $title, Tracker $tracker, bool $read
->withTitle($title)
->inTracker($tracker)
->withChangesets($changeset)
->userCanView($readable)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->build();
}

private function anArtifactWithoutChangesetValue(int $id, string $title, Tracker $tracker, bool $readable): Artifact
private function anArtifactWithoutChangesetValue(int $id, string $title, Tracker $tracker): Artifact
{
$changeset = $this->createMock(\Tracker_Artifact_Changeset::class);
$changeset_value = $this->createMock(\Tracker_Artifact_ChangesetValue_Date::class);
Expand All @@ -1381,7 +1381,7 @@ private function anArtifactWithoutChangesetValue(int $id, string $title, Tracker
->withTitle($title)
->inTracker($tracker)
->withChangesets($changeset)
->userCanView($readable)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ public function testItReturnsAPaginatedListOfReadableTaskRepresentation(): void

$task_201 = $this->anArtifact(201, 'Do this', $tracker, true, $semantic_timeframe);
$task_202 = $this->anArtifact(202, 'Do that', $tracker, false, $semantic_timeframe);
$task_203 = $this->anArtifactWithoutStartDate(203, 'Do those', $tracker, true, $semantic_timeframe);
$task_203 = $this->anArtifactWithoutStartDate(203, 'Do those', $tracker, $semantic_timeframe);
$task_204 = $this->anArtifact(204, 'Done more than 1 year ago', $tracker, true, $semantic_timeframe);

$this->mockEffort($total_effort_field, [201 => 8, 203 => 3]);
Expand Down Expand Up @@ -806,7 +806,7 @@ public function testItReturnsAPaginatedListOfReadableTaskRepresentationFromRepor

$task_201 = $this->anArtifact(201, 'Do this', $tracker, true, $semantic_timeframe);
$task_202 = $this->anArtifact(202, 'Do that', $tracker, false, $semantic_timeframe);
$task_203 = $this->anArtifactWithoutStartDate(203, 'Do those', $tracker, true, $semantic_timeframe);
$task_203 = $this->anArtifactWithoutStartDate(203, 'Do those', $tracker, $semantic_timeframe);
$task_204 = $this->anArtifact(204, 'Done more than 1 year ago', $tracker, true, $semantic_timeframe);

$this->mockEffort($total_effort_field, [201 => 8, 203 => 3]);
Expand Down Expand Up @@ -1079,7 +1079,7 @@ public function testItReturnsAPaginatedListOfReadableTaskRepresentationBelonging
});

$task_201 = $this->anArtifact(201, 'Do this', $tracker, true, $semantic_timeframe_tracker);
$task_203 = $this->anArtifactWithoutStartDate(203, 'Do those', $another_tracker, true, $semantic_timeframe_another_tracker);
$task_203 = $this->anArtifactWithoutStartDate(203, 'Do those', $another_tracker, $semantic_timeframe_another_tracker);


$artifacts = [$task_201, $task_203];
Expand Down Expand Up @@ -1222,17 +1222,19 @@ private function anArtifact(int $id, string $title, Tracker $tracker, bool $read
);
}

return ArtifactTestBuilder::anArtifact($id)
$artifact_test_builder = ArtifactTestBuilder::anArtifact($id)
->withTitle($title)
->inTracker($tracker)
->withChangesets($changeset)
->userCanView($readable)
->withParent(null)
->isOpen(true)
->build();
->isOpen(true);

return $readable
? $artifact_test_builder->userCanView($this->user)->build()
: $artifact_test_builder->userCannotView($this->user)->build();
}

private function anArtifactWithoutStartDate(int $id, string $title, Tracker $tracker, bool $readable, SemanticTimeframe $semantic_timeframe): Artifact
private function anArtifactWithoutStartDate(int $id, string $title, Tracker $tracker, SemanticTimeframe $semantic_timeframe): Artifact
{
$changeset = ChangesetTestBuilder::aChangeset('1')->build();
$start_field = $semantic_timeframe->getStartDateField();
Expand All @@ -1252,7 +1254,7 @@ private function anArtifactWithoutStartDate(int $id, string $title, Tracker $tra
->withTitle($title)
->inTracker($tracker)
->withChangesets($changeset)
->userCanView($readable)
->userCanView($this->user)
->withParent(null)
->isOpen(true)
->build();
Expand Down
27 changes: 17 additions & 10 deletions plugins/tracker/include/Tracker/Artifact/Artifact.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,13 @@ class Artifact implements Recent_Element_Interface, Tracker_Dispatchable_Interfa
* @var DBTransactionExecutor
*/
private $transaction_executor;
private bool | null $user_permission_cache = null;
private ?Artifact $parent = null;
private bool $has_set_parent = false;
private bool | null $is_open = null;
/**
* @var array<int, bool>
*/
private array $user_permission_cache = [];
private ?Artifact $parent = null;
private bool $has_set_parent = false;
private bool | null $is_open = null;

/**
* Constructor
Expand Down Expand Up @@ -309,7 +312,10 @@ public function useArtifactPermissions()
return $this->use_artifact_permissions;
}

public function setUserCanView(bool $user_can_view): void
/**
* @param array<int, bool> $user_can_view
*/
public function setUserCanView(array $user_can_view): void
{
$this->user_permission_cache = $user_can_view;
}
Expand All @@ -323,8 +329,8 @@ public function setUserCanView(bool $user_can_view): void
*/
public function userCanView(?PFUser $user = null)
{
if ($this->user_permission_cache !== null) {
return $this->user_permission_cache;
if ($user && isset($this->user_permission_cache[(int) $user->getId()])) {
return $this->user_permission_cache[(int) $user->getId()];
}

$user_manager = $this->getUserManager();
Expand All @@ -342,10 +348,11 @@ public function userCanView(?PFUser $user = null)
if ($user === null) {
$user = $user_manager->getCurrentUser();
}
if (! isset($this->user_permission_cache[(int) $user->getId()])) {
$this->user_permission_cache[(int) $user->getId()] = PermissionsCache::userCanView($this, $user, $permission_checker);
}

$this->user_permission_cache = PermissionsCache::userCanView($this, $user, $permission_checker);

return $this->user_permission_cache;
return $this->user_permission_cache[(int) $user->getId()];
}

public function userCanUpdate(PFUser $user): bool
Expand Down
20 changes: 14 additions & 6 deletions plugins/tracker/tests/unit/Builders/ArtifactTestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ final class ArtifactTestBuilder
* @var \Tracker_ArtifactFactory | null
*/
private $artifact_factory;
private bool|null $user_can_view = null;

/**
* @var array<int, bool>
*/
private array $user_can_view = [];

/**
* @var \Project|null
Expand Down Expand Up @@ -136,9 +140,15 @@ public function withChangesets(\Tracker_Artifact_Changeset $last_changeset, \Tra
return $this;
}

public function userCanView(bool $user_can_view): self
public function userCanView(PFUser $user): self
{
$this->user_can_view[$user->getId()] = true;
return $this;
}

public function userCannotView(PFUser $user): self
{
$this->user_can_view = $user_can_view;
$this->user_can_view[$user->getId()] = false;
return $this;
}

Expand Down Expand Up @@ -206,9 +216,7 @@ public function build(): Artifact
if ($this->last_changeset) {
$artifact->setLastChangeset($this->last_changeset);
}
if ($this->user_can_view !== null) {
$artifact->setUserCanView($this->user_can_view);
}
$artifact->setUserCanView($this->user_can_view);
if ($this->has_parent) {
$artifact->setParent($this->parent);
}
Expand Down
45 changes: 45 additions & 0 deletions plugins/tracker/tests/unit/Tracker/Artifact/ArtifactTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright (c) Enalean, 2024 - Present. All Rights Reserved.
*
* This file is a part of Tuleap.
*
* Tuleap is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tuleap is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Tuleap\Tracker\Artifact;

use Tuleap\Test\Builders\UserTestBuilder;
use Tuleap\Test\PHPUnit\TestCase;

final class ArtifactTest extends TestCase
{
public function testUserCanViewCache(): void
{
$artifact = new Artifact(1, 2, 3, 4, false);

$bob = UserTestBuilder::anActiveUser()->withId(1)->build();
$alice = UserTestBuilder::anActiveUser()->withId(2)->build();

$artifact->setUserCanView([
$bob->getId() => false,
$alice->getId() => true,
]);

self::assertFalse($artifact->userCanView($bob));
self::assertTrue($artifact->userCanView($alice));
}
}
Loading

0 comments on commit 0329e21

Please sign in to comment.