Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add project join/leave tests

Summary: Add test coverage for joining and leaving projects.

Test Plan: Ran tests.

Reviewers: vrana, btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T603

Differential Revision: https://secure.phabricator.com/D3220
  • Loading branch information...
commit b00c28a3608afafd6a0edd8c76fe3e03a8fe1326 1 parent bd0be1c
@epriestley epriestley authored
View
2  src/__phutil_library_map__.php
@@ -906,6 +906,7 @@
'PhabricatorProjectCreateController' => 'applications/project/controller/PhabricatorProjectCreateController.php',
'PhabricatorProjectDAO' => 'applications/project/storage/PhabricatorProjectDAO.php',
'PhabricatorProjectEditor' => 'applications/project/editor/PhabricatorProjectEditor.php',
+ 'PhabricatorProjectEditorTestCase' => 'applications/project/editor/__tests__/PhabricatorProjectEditorTestCase.php',
'PhabricatorProjectListController' => 'applications/project/controller/PhabricatorProjectListController.php',
'PhabricatorProjectMembersEditController' => 'applications/project/controller/PhabricatorProjectMembersEditController.php',
'PhabricatorProjectNameCollisionException' => 'applications/project/exception/PhabricatorProjectNameCollisionException.php',
@@ -1986,6 +1987,7 @@
'PhabricatorProjectController' => 'PhabricatorController',
'PhabricatorProjectCreateController' => 'PhabricatorProjectController',
'PhabricatorProjectDAO' => 'PhabricatorLiskDAO',
+ 'PhabricatorProjectEditorTestCase' => 'PhabricatorTestCase',
'PhabricatorProjectListController' => 'PhabricatorProjectController',
'PhabricatorProjectMembersEditController' => 'PhabricatorProjectController',
'PhabricatorProjectNameCollisionException' => 'Exception',
View
40 src/applications/project/controller/PhabricatorProjectUpdateController.php
@@ -31,7 +31,11 @@ public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
- $project = id(new PhabricatorProject())->load($this->id);
+ $project = id(new PhabricatorProjectQuery())
+ ->setViewer($user)
+ ->needMembers(true)
+ ->withIDs(array($this->id))
+ ->executeOne();
if (!$project) {
return new Aphront404Response();
}
@@ -51,44 +55,14 @@ public function processRequest() {
$project_uri = '/project/view/'.$project->getID().'/';
if ($process_action) {
- $xactions = array();
-
-
switch ($this->action) {
case 'join':
- $member_phids = $project->loadMemberPHIDs();
- $member_map = array_fill_keys($member_phids, true);
- if (empty($member_map[$user->getPHID()])) {
- $member_map[$user->getPHID()] = true;
-
- $xaction = new PhabricatorProjectTransaction();
- $xaction->setTransactionType(
- PhabricatorProjectTransactionType::TYPE_MEMBERS);
- $xaction->setNewValue(array_keys($member_map));
- $xactions[] = $xaction;
- }
+ PhabricatorProjectEditor::applyJoinProject($project, $user);
break;
case 'leave':
- $member_phids = $project->loadMemberPHIDs();
- $member_map = array_fill_keys($member_phids, true);
- if (isset($member_map[$user->getPHID()])) {
- unset($member_map[$user->getPHID()]);
-
- $xaction = new PhabricatorProjectTransaction();
- $xaction->setTransactionType(
- PhabricatorProjectTransactionType::TYPE_MEMBERS);
- $xaction->setNewValue(array_keys($member_map));
- $xactions[] = $xaction;
- }
+ PhabricatorProjectEditor::applyLeaveProject($project, $user);
break;
}
-
- if ($xactions) {
- $editor = new PhabricatorProjectEditor($project);
- $editor->setUser($user);
- $editor->applyTransactions($xactions);
- }
-
return id(new AphrontRedirectResponse())->setURI($project_uri);
}
View
45 src/applications/project/editor/PhabricatorProjectEditor.php
@@ -25,6 +25,51 @@
private $addEdges = array();
private $remEdges = array();
+ public static function applyJoinProject(
+ PhabricatorProject $project,
+ PhabricatorUser $user) {
+
+ $members = $project->getMemberPHIDs();
+ $members[] = $user->getPHID();
+
+ self::applyOneTransaction(
+ $project,
+ $user,
+ PhabricatorProjectTransactionType::TYPE_MEMBERS,
+ $members);
+ }
+
+ public static function applyLeaveProject(
+ PhabricatorProject $project,
+ PhabricatorUser $user) {
+
+ $members = array_fill_keys($project->getMemberPHIDs(), true);
+ unset($members[$user->getPHID()]);
+ $members = array_keys($members);
+
+ self::applyOneTransaction(
+ $project,
+ $user,
+ PhabricatorProjectTransactionType::TYPE_MEMBERS,
+ $members);
+ }
+
+ private static function applyOneTransaction(
+ PhabricatorProject $project,
+ PhabricatorUser $user,
+ $type,
+ $new_value) {
+
+ $xaction = new PhabricatorProjectTransaction();
+ $xaction->setTransactionType($type);
+ $xaction->setNewValue($new_value);
+
+ $editor = new PhabricatorProjectEditor($project);
+ $editor->setUser($user);
+ $editor->applyTransactions(array($xaction));
+ }
+
+
public function __construct(PhabricatorProject $project) {
$this->project = $project;
}
View
138 src/applications/project/editor/__tests__/PhabricatorProjectEditorTestCase.php
@@ -0,0 +1,138 @@
+<?php
+
+/*
+ * Copyright 2012 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+final class PhabricatorProjectEditorTestCase extends PhabricatorTestCase {
+
+ protected function getPhabricatorTestCaseConfiguration() {
+ return array(
+ self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,
+ );
+ }
+
+ public function testJoinLeaveProject() {
+ $user = $this->createUser();
+ $user->save();
+
+ $proj = $this->createProjectWithNewAuthor();
+ $proj->save();
+
+ $proj = $this->refreshProject($proj, $user, true);
+ $this->assertEqual(
+ true,
+ (bool)$proj,
+ 'Assumption that projects are default visible to any user when created.');
+
+ $this->assertEqual(
+ false,
+ $proj->isUserMember($user->getPHID()),
+ 'Arbitrary user not member of project.');
+
+ // Join the project.
+ PhabricatorProjectEditor::applyJoinProject($proj, $user);
+
+ $proj = $this->refreshProject($proj, $user, true);
+ $this->assertEqual(true, (bool)$proj);
+
+ $this->assertEqual(
+ true,
+ $proj->isUserMember($user->getPHID()),
+ 'Join works.');
+
+
+ // Join the project again.
+ PhabricatorProjectEditor::applyJoinProject($proj, $user);
+
+ $proj = $this->refreshProject($proj, $user, true);
+ $this->assertEqual(true, (bool)$proj);
+
+ $this->assertEqual(
+ true,
+ $proj->isUserMember($user->getPHID()),
+ 'Joining an already-joined project is a no-op.');
+
+
+ // Leave the project.
+ PhabricatorProjectEditor::applyLeaveProject($proj, $user);
+
+ $proj = $this->refreshProject($proj, $user, true);
+ $this->assertEqual(true, (bool)$proj);
+
+ $this->assertEqual(
+ false,
+ $proj->isUserMember($user->getPHID()),
+ 'Leave works.');
+
+
+ // Leave the project again.
+ PhabricatorProjectEditor::applyLeaveProject($proj, $user);
+
+ $proj = $this->refreshProject($proj, $user, true);
+ $this->assertEqual(true, (bool)$proj);
+
+ $this->assertEqual(
+ false,
+ $proj->isUserMember($user->getPHID()),
+ 'Leaving an already-left project is a no-op.');
+ }
+
+ private function refreshProject(
+ PhabricatorProject $project,
+ PhabricatorUser $viewer,
+ $need_members = false) {
+
+ $results = id(new PhabricatorProjectQuery())
+ ->setViewer($viewer)
+ ->needMembers($need_members)
+ ->withIDs(array($project->getID()))
+ ->execute();
+
+ if ($results) {
+ return head($results);
+ } else {
+ return null;
+ }
+ }
+
+ private function createProject() {
+ $project = new PhabricatorProject();
+ $project->setName('Test Project '.mt_rand());
+
+ return $project;
+ }
+
+ private function createProjectWithNewAuthor() {
+ $author = $this->createUser();
+ $author->save();
+
+ $project = $this->createProject();
+ $project->setAuthorPHID($author->getPHID());
+
+ return $project;
+ }
+
+ private function createUser() {
+ $rand = mt_rand();
+
+ $user = new PhabricatorUser();
+ $user->setUsername('unittestuser'.$rand);
+ $user->setRealName('Unit Test User '.$rand);
+
+ return $user;
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.