Changeset View
Changeset View
Standalone View
Standalone View
src/applications/spaces/__tests__/PhabricatorSpacesTestCase.php
| <?php | <?php | ||||
| final class PhabricatorSpacesTestCase extends PhabricatorTestCase { | final class PhabricatorSpacesTestCase 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 testSpacesAnnihilation() { | public function testSpacesAnnihilation() { | ||||
| $this->destroyAllSpaces(); | $this->destroyAllSpaces(); | ||||
| // Test that our helper methods work correctly. | // Test that our helper methods work correctly. | ||||
| $actor = $this->generateNewTestUser(); | $actor = $this->generateNewTestUser(); | ||||
| $this->newSpace($actor, pht('Test Space'), true); | |||||
| $default = $this->newSpace($actor, pht('Test Space'), true); | |||||
| $this->assertEqual(1, count($this->loadAllSpaces())); | $this->assertEqual(1, count($this->loadAllSpaces())); | ||||
| $this->assertEqual( | |||||
| 1, | |||||
| count(PhabricatorSpacesNamespaceQuery::getAllSpaces())); | |||||
| $cache_default = PhabricatorSpacesNamespaceQuery::getDefaultSpace(); | |||||
| $this->assertEqual($default->getPHID(), $cache_default->getPHID()); | |||||
| $this->destroyAllSpaces(); | $this->destroyAllSpaces(); | ||||
| $this->assertEqual(0, count($this->loadAllSpaces())); | $this->assertEqual(0, count($this->loadAllSpaces())); | ||||
| $this->assertEqual( | |||||
| 0, | |||||
| count(PhabricatorSpacesNamespaceQuery::getAllSpaces())); | |||||
| $this->assertEqual( | |||||
| null, | |||||
| PhabricatorSpacesNamespaceQuery::getDefaultSpace()); | |||||
| } | } | ||||
| public function testSpacesSeveralSpaces() { | public function testSpacesSeveralSpaces() { | ||||
| $this->destroyAllSpaces(); | $this->destroyAllSpaces(); | ||||
| // Try creating a few spaces, one of which is a default space. This should | // Try creating a few spaces, one of which is a default space. This should | ||||
| // work fine. | // work fine. | ||||
| $actor = $this->generateNewTestUser(); | $actor = $this->generateNewTestUser(); | ||||
| $this->newSpace($actor, pht('Default Space'), true); | $default = $this->newSpace($actor, pht('Default Space'), true); | ||||
| $this->newSpace($actor, pht('Alternate Space'), false); | $this->newSpace($actor, pht('Alternate Space'), false); | ||||
| $this->assertEqual(2, count($this->loadAllSpaces())); | $this->assertEqual(2, count($this->loadAllSpaces())); | ||||
| $this->assertEqual( | |||||
| 2, | |||||
| count(PhabricatorSpacesNamespaceQuery::getAllSpaces())); | |||||
| $cache_default = PhabricatorSpacesNamespaceQuery::getDefaultSpace(); | |||||
| $this->assertEqual($default->getPHID(), $cache_default->getPHID()); | |||||
| } | } | ||||
| public function testSpacesRequireNames() { | public function testSpacesRequireNames() { | ||||
| $this->destroyAllSpaces(); | $this->destroyAllSpaces(); | ||||
| // Spaces must have nonempty names. | // Spaces must have nonempty names. | ||||
| $actor = $this->generateNewTestUser(); | $actor = $this->generateNewTestUser(); | ||||
| Show All 24 Lines | try { | ||||
| $this->newSpace($actor, pht('Default Space #2'), true); | $this->newSpace($actor, pht('Default Space #2'), true); | ||||
| } catch (AphrontDuplicateKeyQueryException $ex) { | } catch (AphrontDuplicateKeyQueryException $ex) { | ||||
| $caught = $ex; | $caught = $ex; | ||||
| } | } | ||||
| $this->assertTrue(($caught instanceof Exception)); | $this->assertTrue(($caught instanceof Exception)); | ||||
| } | } | ||||
| public function testSpacesPolicyFiltering() { | |||||
| $this->destroyAllSpaces(); | |||||
| $creator = $this->generateNewTestUser(); | |||||
| $viewer = $this->generateNewTestUser(); | |||||
| // Create a new paste. | |||||
| $paste = PhabricatorPaste::initializeNewPaste($creator) | |||||
| ->setViewPolicy(PhabricatorPolicies::POLICY_USER) | |||||
| ->setFilePHID('') | |||||
| ->setLanguage('') | |||||
| ->save(); | |||||
| // It should be visible. | |||||
| $this->assertTrue( | |||||
| PhabricatorPolicyFilter::hasCapability( | |||||
| $viewer, | |||||
| $paste, | |||||
| PhabricatorPolicyCapability::CAN_VIEW)); | |||||
| // Create a default space with an open view policy. | |||||
| $default = $this->newSpace($creator, pht('Default Space'), true) | |||||
| ->setViewPolicy(PhabricatorPolicies::POLICY_USER) | |||||
| ->save(); | |||||
| PhabricatorSpacesNamespaceQuery::destroySpacesCache(); | |||||
| // The paste should now be in the space implicitly, but still visible | |||||
| // because the space view policy is open. | |||||
| $this->assertTrue( | |||||
| PhabricatorPolicyFilter::hasCapability( | |||||
| $viewer, | |||||
| $paste, | |||||
| PhabricatorPolicyCapability::CAN_VIEW)); | |||||
| // Make the space view policy restrictive. | |||||
| $default | |||||
| ->setViewPolicy(PhabricatorPolicies::POLICY_NOONE) | |||||
| ->save(); | |||||
| PhabricatorSpacesNamespaceQuery::destroySpacesCache(); | |||||
| // The paste should be in the space implicitly, and no longer visible. | |||||
| $this->assertFalse( | |||||
| PhabricatorPolicyFilter::hasCapability( | |||||
| $viewer, | |||||
| $paste, | |||||
| PhabricatorPolicyCapability::CAN_VIEW)); | |||||
| // Put the paste in the space explicitly. | |||||
| $paste | |||||
| ->setSpacePHID($default->getPHID()) | |||||
| ->save(); | |||||
| PhabricatorSpacesNamespaceQuery::destroySpacesCache(); | |||||
| // This should still fail, we're just in the space explicitly now. | |||||
| $this->assertFalse( | |||||
| PhabricatorPolicyFilter::hasCapability( | |||||
| $viewer, | |||||
| $paste, | |||||
| PhabricatorPolicyCapability::CAN_VIEW)); | |||||
| // Create an alternate space with more permissive policies, then move the | |||||
| // paste to that space. | |||||
| $alternate = $this->newSpace($creator, pht('Alternate Space'), false) | |||||
| ->setViewPolicy(PhabricatorPolicies::POLICY_USER) | |||||
| ->save(); | |||||
| $paste | |||||
| ->setSpacePHID($alternate->getPHID()) | |||||
| ->save(); | |||||
| PhabricatorSpacesNamespaceQuery::destroySpacesCache(); | |||||
| // Now the paste should be visible again. | |||||
| $this->assertTrue( | |||||
| PhabricatorPolicyFilter::hasCapability( | |||||
| $viewer, | |||||
| $paste, | |||||
| PhabricatorPolicyCapability::CAN_VIEW)); | |||||
| } | |||||
| private function loadAllSpaces() { | private function loadAllSpaces() { | ||||
| return id(new PhabricatorSpacesNamespaceQuery()) | return id(new PhabricatorSpacesNamespaceQuery()) | ||||
| ->setViewer(PhabricatorUser::getOmnipotentUser()) | ->setViewer(PhabricatorUser::getOmnipotentUser()) | ||||
| ->execute(); | ->execute(); | ||||
| } | } | ||||
| private function destroyAllSpaces() { | private function destroyAllSpaces() { | ||||
| PhabricatorSpacesNamespaceQuery::destroySpacesCache(); | |||||
| $spaces = $this->loadAllSpaces(); | $spaces = $this->loadAllSpaces(); | ||||
| foreach ($spaces as $space) { | foreach ($spaces as $space) { | ||||
| $engine = new PhabricatorDestructionEngine(); | $engine = new PhabricatorDestructionEngine(); | ||||
| $engine->destroyObject($space); | $engine->destroyObject($space); | ||||
| } | } | ||||
| } | } | ||||
| private function newSpace( | private function newSpace( | ||||
| ▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines | |||||