mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-10 08:52:39 +01:00
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
This commit is contained in:
parent
bd0be1c650
commit
b00c28a360
4 changed files with 192 additions and 33 deletions
|
@ -906,6 +906,7 @@ phutil_register_library_map(array(
|
|||
'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 @@ phutil_register_library_map(array(
|
|||
'PhabricatorProjectController' => 'PhabricatorController',
|
||||
'PhabricatorProjectCreateController' => 'PhabricatorProjectController',
|
||||
'PhabricatorProjectDAO' => 'PhabricatorLiskDAO',
|
||||
'PhabricatorProjectEditorTestCase' => 'PhabricatorTestCase',
|
||||
'PhabricatorProjectListController' => 'PhabricatorProjectController',
|
||||
'PhabricatorProjectMembersEditController' => 'PhabricatorProjectController',
|
||||
'PhabricatorProjectNameCollisionException' => 'Exception',
|
||||
|
|
|
@ -31,7 +31,11 @@ final class PhabricatorProjectUpdateController
|
|||
$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 @@ final class PhabricatorProjectUpdateController
|
|||
$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);
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,51 @@ final class PhabricatorProjectEditor {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue