[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/policy/__tests__/ -> PhabricatorPolicyDataTestCase.php (source)

   1  <?php
   2  
   3  final class PhabricatorPolicyDataTestCase extends PhabricatorTestCase {
   4  
   5    protected function getPhabricatorTestCaseConfiguration() {
   6      return array(
   7        self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,
   8      );
   9    }
  10  
  11    public function testProjectPolicyMembership() {
  12      $author = $this->generateNewTestUser();
  13  
  14      $proj_a = id(new PhabricatorProject())
  15        ->setName('A')
  16        ->setAuthorPHID($author->getPHID())
  17        ->setIcon(PhabricatorProject::DEFAULT_ICON)
  18        ->setColor(PhabricatorProject::DEFAULT_COLOR)
  19        ->setIsMembershipLocked(0)
  20        ->save();
  21      $proj_b = id(new PhabricatorProject())
  22        ->setName('B')
  23        ->setAuthorPHID($author->getPHID())
  24        ->setIcon(PhabricatorProject::DEFAULT_ICON)
  25        ->setColor(PhabricatorProject::DEFAULT_COLOR)
  26        ->setIsMembershipLocked(0)
  27        ->save();
  28  
  29      $proj_a->setViewPolicy($proj_b->getPHID())->save();
  30      $proj_b->setViewPolicy($proj_a->getPHID())->save();
  31  
  32      $user = new PhabricatorUser();
  33  
  34      $results = id(new PhabricatorProjectQuery())
  35        ->setViewer($user)
  36        ->execute();
  37  
  38      $this->assertEqual(0, count($results));
  39    }
  40  
  41    public function testCustomPolicyRuleUser() {
  42      $user_a = $this->generateNewTestUser();
  43      $user_b = $this->generateNewTestUser();
  44      $author = $this->generateNewTestUser();
  45  
  46      $policy = id(new PhabricatorPolicy())
  47        ->setRules(
  48          array(
  49            array(
  50              'action' => PhabricatorPolicy::ACTION_ALLOW,
  51              'rule' => 'PhabricatorPolicyRuleUsers',
  52              'value' => array($user_a->getPHID()),
  53            ),
  54          ))
  55        ->save();
  56  
  57      $task = ManiphestTask::initializeNewTask($author);
  58      $task->setViewPolicy($policy->getPHID());
  59      $task->save();
  60  
  61      $can_a_view = PhabricatorPolicyFilter::hasCapability(
  62        $user_a,
  63        $task,
  64        PhabricatorPolicyCapability::CAN_VIEW);
  65  
  66      $this->assertTrue($can_a_view);
  67  
  68      $can_b_view = PhabricatorPolicyFilter::hasCapability(
  69        $user_b,
  70        $task,
  71        PhabricatorPolicyCapability::CAN_VIEW);
  72  
  73      $this->assertFalse($can_b_view);
  74    }
  75  
  76    public function testCustomPolicyRuleAdministrators() {
  77      $user_a = $this->generateNewTestUser();
  78      $user_a->setIsAdmin(true)->save();
  79      $user_b = $this->generateNewTestUser();
  80      $author = $this->generateNewTestUser();
  81  
  82      $policy = id(new PhabricatorPolicy())
  83        ->setRules(
  84          array(
  85            array(
  86              'action' => PhabricatorPolicy::ACTION_ALLOW,
  87              'rule' => 'PhabricatorPolicyRuleAdministrators',
  88              'value' => null,
  89            ),
  90          ))
  91        ->save();
  92  
  93      $task = ManiphestTask::initializeNewTask($author);
  94      $task->setViewPolicy($policy->getPHID());
  95      $task->save();
  96  
  97      $can_a_view = PhabricatorPolicyFilter::hasCapability(
  98        $user_a,
  99        $task,
 100        PhabricatorPolicyCapability::CAN_VIEW);
 101  
 102      $this->assertTrue($can_a_view);
 103  
 104      $can_b_view = PhabricatorPolicyFilter::hasCapability(
 105        $user_b,
 106        $task,
 107        PhabricatorPolicyCapability::CAN_VIEW);
 108  
 109      $this->assertFalse($can_b_view);
 110    }
 111  
 112    public function testCustomPolicyRuleLunarPhase() {
 113      $user_a = $this->generateNewTestUser();
 114      $author = $this->generateNewTestUser();
 115  
 116      $policy = id(new PhabricatorPolicy())
 117        ->setRules(
 118          array(
 119            array(
 120              'action' => PhabricatorPolicy::ACTION_ALLOW,
 121              'rule' => 'PhabricatorPolicyRuleLunarPhase',
 122              'value' => 'new',
 123            ),
 124          ))
 125        ->save();
 126  
 127      $task = ManiphestTask::initializeNewTask($author);
 128      $task->setViewPolicy($policy->getPHID());
 129      $task->save();
 130  
 131      $time_a = PhabricatorTime::pushTime(934354800, 'UTC');
 132  
 133        $can_a_view = PhabricatorPolicyFilter::hasCapability(
 134          $user_a,
 135          $task,
 136          PhabricatorPolicyCapability::CAN_VIEW);
 137        $this->assertTrue($can_a_view);
 138  
 139      unset($time_a);
 140  
 141  
 142      $time_b = PhabricatorTime::pushTime(1116745200, 'UTC');
 143  
 144        $can_a_view = PhabricatorPolicyFilter::hasCapability(
 145          $user_a,
 146          $task,
 147          PhabricatorPolicyCapability::CAN_VIEW);
 148        $this->assertFalse($can_a_view);
 149  
 150      unset($time_b);
 151    }
 152  
 153  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1