Differential D14859 Diff 35937 src/applications/project/__tests__/PhabricatorProjectCoreTestCase.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/project/__tests__/PhabricatorProjectCoreTestCase.php
- This file was moved from src/applications/project/editor/__tests__/PhabricatorProjectEditorTestCase.php.
| <?php | <?php | ||||
| final class PhabricatorProjectEditorTestCase extends PhabricatorTestCase { | final class PhabricatorProjectCoreTestCase extends PhabricatorTestCase { | ||||
| protected function getPhabricatorTestCaseConfiguration() { | protected function getPhabricatorTestCaseConfiguration() { | ||||
| return array( | return array( | ||||
| self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true, | self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true, | ||||
| ); | ); | ||||
| } | } | ||||
| public function testViewProject() { | public function testViewProject() { | ||||
| Show All 22 Lines | public function testViewProject() { | ||||
| // project. | // project. | ||||
| $proj->setViewPolicy(PhabricatorPolicies::POLICY_NOONE); | $proj->setViewPolicy(PhabricatorPolicies::POLICY_NOONE); | ||||
| $proj->save(); | $proj->save(); | ||||
| $this->assertTrue((bool)$this->refreshProject($proj, $user)); | $this->assertTrue((bool)$this->refreshProject($proj, $user)); | ||||
| $this->assertFalse((bool)$this->refreshProject($proj, $user2)); | $this->assertFalse((bool)$this->refreshProject($proj, $user2)); | ||||
| } | } | ||||
| public function testIsViewerMemberOrWatcher() { | |||||
| $user1 = $this->createUser() | |||||
| ->save(); | |||||
| $user2 = $this->createUser() | |||||
| ->save(); | |||||
| $user3 = $this->createUser() | |||||
| ->save(); | |||||
| $proj1 = $this->createProject($user1); | |||||
| $proj1 = $this->refreshProject($proj1, $user1); | |||||
| $this->joinProject($proj1, $user1); | |||||
| $this->joinProject($proj1, $user3); | |||||
| $this->watchProject($proj1, $user3); | |||||
| $proj1 = $this->refreshProject($proj1, $user1); | |||||
| $this->assertTrue($proj1->isUserMember($user1->getPHID())); | |||||
| $proj1 = $this->refreshProject($proj1, $user1, false, true); | |||||
| $this->assertTrue($proj1->isUserMember($user1->getPHID())); | |||||
| $this->assertFalse($proj1->isUserWatcher($user1->getPHID())); | |||||
| $proj1 = $this->refreshProject($proj1, $user1, true, false); | |||||
| $this->assertTrue($proj1->isUserMember($user1->getPHID())); | |||||
| $this->assertFalse($proj1->isUserMember($user2->getPHID())); | |||||
| $this->assertTrue($proj1->isUserMember($user3->getPHID())); | |||||
| $proj1 = $this->refreshProject($proj1, $user1, true, true); | |||||
| $this->assertTrue($proj1->isUserMember($user1->getPHID())); | |||||
| $this->assertFalse($proj1->isUserMember($user2->getPHID())); | |||||
| $this->assertTrue($proj1->isUserMember($user3->getPHID())); | |||||
| $this->assertFalse($proj1->isUserWatcher($user1->getPHID())); | |||||
| $this->assertFalse($proj1->isUserWatcher($user2->getPHID())); | |||||
| $this->assertTrue($proj1->isUserWatcher($user3->getPHID())); | |||||
| } | |||||
| public function testEditProject() { | public function testEditProject() { | ||||
| $user = $this->createUser(); | $user = $this->createUser(); | ||||
| $user->save(); | $user->save(); | ||||
| $user2 = $this->createUser(); | $user2 = $this->createUser(); | ||||
| $user2->save(); | $user2->save(); | ||||
| $proj = $this->createProject($user); | $proj = $this->createProject($user); | ||||
| ▲ Show 20 Lines • Show All 155 Lines • ▼ Show 20 Lines | public function testJoinLeaveProject() { | ||||
| $this->assertFalse( | $this->assertFalse( | ||||
| $proj->isUserMember($user->getPHID()), | $proj->isUserMember($user->getPHID()), | ||||
| pht('Leave allowed without any permission.')); | pht('Leave allowed without any permission.')); | ||||
| } | } | ||||
| private function refreshProject( | private function refreshProject( | ||||
| PhabricatorProject $project, | PhabricatorProject $project, | ||||
| PhabricatorUser $viewer, | PhabricatorUser $viewer, | ||||
| $need_members = false) { | $need_members = false, | ||||
| $need_watchers = false) { | |||||
| $results = id(new PhabricatorProjectQuery()) | $results = id(new PhabricatorProjectQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->needMembers($need_members) | ->needMembers($need_members) | ||||
| ->needWatchers($need_watchers) | |||||
| ->withIDs(array($project->getID())) | ->withIDs(array($project->getID())) | ||||
| ->execute(); | ->execute(); | ||||
| if ($results) { | if ($results) { | ||||
| return head($results); | return head($results); | ||||
| } else { | } else { | ||||
| return null; | return null; | ||||
| } | } | ||||
| Show All 24 Lines | private function createUser() { | ||||
| $user->setRealName(pht('Unit Test User %d', $rand)); | $user->setRealName(pht('Unit Test User %d', $rand)); | ||||
| return $user; | return $user; | ||||
| } | } | ||||
| private function joinProject( | private function joinProject( | ||||
| PhabricatorProject $project, | PhabricatorProject $project, | ||||
| PhabricatorUser $user) { | PhabricatorUser $user) { | ||||
| $this->joinOrLeaveProject($project, $user, '+'); | return $this->joinOrLeaveProject($project, $user, '+'); | ||||
| } | } | ||||
| private function leaveProject( | private function leaveProject( | ||||
| PhabricatorProject $project, | PhabricatorProject $project, | ||||
| PhabricatorUser $user) { | PhabricatorUser $user) { | ||||
| $this->joinOrLeaveProject($project, $user, '-'); | return $this->joinOrLeaveProject($project, $user, '-'); | ||||
| } | |||||
| private function watchProject( | |||||
| PhabricatorProject $project, | |||||
| PhabricatorUser $user) { | |||||
| return $this->watchOrUnwatchProject($project, $user, '+'); | |||||
| } | |||||
| private function unwatchProject( | |||||
| PhabricatorProject $project, | |||||
| PhabricatorUser $user) { | |||||
| return $this->watchOrUnwatchProject($project, $user, '-'); | |||||
| } | } | ||||
| private function joinOrLeaveProject( | private function joinOrLeaveProject( | ||||
| PhabricatorProject $project, | PhabricatorProject $project, | ||||
| PhabricatorUser $user, | PhabricatorUser $user, | ||||
| $operation) { | $operation) { | ||||
| return $this->applyProjectEdgeTransaction( | |||||
| $project, | |||||
| $user, | |||||
| $operation, | |||||
| PhabricatorProjectProjectHasMemberEdgeType::EDGECONST); | |||||
| } | |||||
| private function watchOrUnwatchProject( | |||||
| PhabricatorProject $project, | |||||
| PhabricatorUser $user, | |||||
| $operation) { | |||||
| return $this->applyProjectEdgeTransaction( | |||||
| $project, | |||||
| $user, | |||||
| $operation, | |||||
| PhabricatorObjectHasWatcherEdgeType::EDGECONST); | |||||
| } | |||||
| private function applyProjectEdgeTransaction( | |||||
| PhabricatorProject $project, | |||||
| PhabricatorUser $user, | |||||
| $operation, | |||||
| $edge_type) { | |||||
| $spec = array( | $spec = array( | ||||
| $operation => array($user->getPHID() => $user->getPHID()), | $operation => array($user->getPHID() => $user->getPHID()), | ||||
| ); | ); | ||||
| $xactions = array(); | $xactions = array(); | ||||
| $xactions[] = id(new PhabricatorProjectTransaction()) | $xactions[] = id(new PhabricatorProjectTransaction()) | ||||
| ->setTransactionType(PhabricatorTransactions::TYPE_EDGE) | ->setTransactionType(PhabricatorTransactions::TYPE_EDGE) | ||||
| ->setMetadataValue( | ->setMetadataValue('edge:type', $edge_type) | ||||
| 'edge:type', | |||||
| PhabricatorProjectProjectHasMemberEdgeType::EDGECONST) | |||||
| ->setNewValue($spec); | ->setNewValue($spec); | ||||
| $editor = id(new PhabricatorProjectTransactionEditor()) | $editor = id(new PhabricatorProjectTransactionEditor()) | ||||
| ->setActor($user) | ->setActor($user) | ||||
| ->setContentSource(PhabricatorContentSource::newConsoleSource()) | ->setContentSource(PhabricatorContentSource::newConsoleSource()) | ||||
| ->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
| ->applyTransactions($project, $xactions); | ->applyTransactions($project, $xactions); | ||||
| return $project; | |||||
| } | } | ||||
| } | } | ||||