@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add a bunch of policy tests for projects

Summary: Improve test coverage for policy rules in project edits.

Test Plan: Ran uint tests.

Reviewers: btrahan, vrana

Reviewed By: btrahan

CC: aran, alanh

Maniphest Tasks: T603

Differential Revision: https://secure.phabricator.com/D3294

+154
+154
src/applications/project/editor/__tests__/PhabricatorProjectEditorTestCase.php
··· 24 24 ); 25 25 } 26 26 27 + public function testViewProject() { 28 + $user = $this->createUser(); 29 + $user->save(); 30 + 31 + $user2 = $this->createUser(); 32 + $user2->save(); 33 + 34 + $proj = $this->createProject(); 35 + $proj->setAuthorPHID($user->getPHID()); 36 + $proj->save(); 37 + 38 + $proj = $this->refreshProject($proj, $user, true); 39 + 40 + PhabricatorProjectEditor::applyJoinProject($proj, $user); 41 + $proj->setViewPolicy(PhabricatorPolicies::POLICY_USER); 42 + $proj->save(); 43 + 44 + $can_view = PhabricatorPolicyCapability::CAN_VIEW; 45 + 46 + // When the view policy is set to "users", any user can see the project. 47 + $this->assertEqual( 48 + true, 49 + (bool)$this->refreshProject($proj, $user)); 50 + $this->assertEqual( 51 + true, 52 + (bool)$this->refreshProject($proj, $user2)); 53 + 54 + 55 + // When the view policy is set to "no one", members can still see the 56 + // project. 57 + $proj->setViewPolicy(PhabricatorPolicies::POLICY_NOONE); 58 + $proj->save(); 59 + 60 + $this->assertEqual( 61 + true, 62 + (bool)$this->refreshProject($proj, $user)); 63 + $this->assertEqual( 64 + false, 65 + (bool)$this->refreshProject($proj, $user2)); 66 + } 67 + 68 + public function testEditProject() { 69 + $user = $this->createUser(); 70 + $user->save(); 71 + 72 + $user2 = $this->createUser(); 73 + $user2->save(); 74 + 75 + $proj = $this->createProject(); 76 + $proj->setAuthorPHID($user->getPHID()); 77 + $proj->save(); 78 + 79 + 80 + // When edit and view policies are set to "user", anyone can edit. 81 + $proj->setViewPolicy(PhabricatorPolicies::POLICY_USER); 82 + $proj->setEditPolicy(PhabricatorPolicies::POLICY_USER); 83 + $proj->save(); 84 + 85 + $this->assertEqual( 86 + true, 87 + $this->attemptProjectEdit($proj, $user)); 88 + 89 + 90 + // When edit policy is set to "no one", no one can edit. 91 + $proj->setEditPolicy(PhabricatorPolicies::POLICY_NOONE); 92 + $proj->save(); 93 + 94 + $caught = null; 95 + try { 96 + $this->attemptProjectEdit($proj, $user); 97 + } catch (Exception $ex) { 98 + $caught = $ex; 99 + } 100 + $this->assertEqual(true, ($caught instanceof Exception)); 101 + } 102 + 103 + private function attemptProjectEdit( 104 + PhabricatorProject $proj, 105 + PhabricatorUser $user, 106 + $skip_refresh = false) { 107 + 108 + $proj = $this->refreshProject($proj, $user, true); 109 + 110 + $new_name = $proj->getName().' '.mt_rand(); 111 + 112 + $xaction = new PhabricatorProjectTransaction(); 113 + $xaction->setTransactionType(PhabricatorProjectTransactionType::TYPE_NAME); 114 + $xaction->setNewValue($new_name); 115 + 116 + $editor = new PhabricatorProjectEditor($proj); 117 + $editor->setUser($user); 118 + $editor->applyTransactions(array($xaction)); 119 + 120 + return true; 121 + } 122 + 27 123 public function testJoinLeaveProject() { 28 124 $user = $this->createUser(); 29 125 $user->save(); ··· 88 184 false, 89 185 $proj->isUserMember($user->getPHID()), 90 186 'Leaving an already-left project is a no-op.'); 187 + 188 + 189 + // If a user can't edit or join a project, joining fails. 190 + $proj->setEditPolicy(PhabricatorPolicies::POLICY_NOONE); 191 + $proj->setJoinPolicy(PhabricatorPolicies::POLICY_NOONE); 192 + $proj->save(); 193 + 194 + $proj = $this->refreshProject($proj, $user, true); 195 + $caught = null; 196 + try { 197 + PhabricatorProjectEditor::applyJoinProject($proj, $user); 198 + } catch (Exception $ex) { 199 + $caught = $ex; 200 + } 201 + $this->assertEqual(true, ($ex instanceof Exception)); 202 + 203 + 204 + // If a user can edit a project, they can join. 205 + $proj->setEditPolicy(PhabricatorPolicies::POLICY_USER); 206 + $proj->setJoinPolicy(PhabricatorPolicies::POLICY_NOONE); 207 + $proj->save(); 208 + 209 + $proj = $this->refreshProject($proj, $user, true); 210 + PhabricatorProjectEditor::applyJoinProject($proj, $user); 211 + $proj = $this->refreshProject($proj, $user, true); 212 + $this->assertEqual( 213 + true, 214 + $proj->isUserMember($user->getPHID()), 215 + 'Join allowed with edit permission.'); 216 + PhabricatorProjectEditor::applyLeaveProject($proj, $user); 217 + 218 + 219 + // If a user can join a project, they can join, even if they can't edit. 220 + $proj->setEditPolicy(PhabricatorPolicies::POLICY_NOONE); 221 + $proj->setJoinPolicy(PhabricatorPolicies::POLICY_USER); 222 + $proj->save(); 223 + 224 + $proj = $this->refreshProject($proj, $user, true); 225 + PhabricatorProjectEditor::applyJoinProject($proj, $user); 226 + $proj = $this->refreshProject($proj, $user, true); 227 + $this->assertEqual( 228 + true, 229 + $proj->isUserMember($user->getPHID()), 230 + 'Join allowed with join permission.'); 231 + 232 + 233 + // A user can leave a project even if they can't edit it or join. 234 + $proj->setEditPolicy(PhabricatorPolicies::POLICY_NOONE); 235 + $proj->setJoinPolicy(PhabricatorPolicies::POLICY_NOONE); 236 + $proj->save(); 237 + 238 + $proj = $this->refreshProject($proj, $user, true); 239 + PhabricatorProjectEditor::applyLeaveProject($proj, $user); 240 + $proj = $this->refreshProject($proj, $user, true); 241 + $this->assertEqual( 242 + false, 243 + $proj->isUserMember($user->getPHID()), 244 + 'Leave allowed without any permission.'); 91 245 } 92 246 93 247 private function refreshProject(