Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
PolicyTestCase.inc.php
1 <?php
2 
18 import('lib.pkp.tests.PKPTestCase');
19 import('lib.pkp.classes.security.UserGroup');
20 import('lib.pkp.classes.core.PKPRequest');
21 
22 define('ROLE_ID_TEST', 0x9999);
23 
24 abstract class PolicyTestCase extends PKPTestCase {
26  private $contextObjects;
27 
29  private $authorizationContextManipulationPolicy;
30 
34  protected function getMockedRegistryKeys() {
35  return array('user');
36  }
37 
42  private function getContextObjects() {
43  return $this->contextObjects;
44  }
45 
50  private function setContextObjects($contextObjects) {
51  $this->contextObjects = $contextObjects;
52  }
53 
61  protected function getAuthorizationContextManipulationPolicy() {
62  if (is_null($this->authorizationContextManipulationPolicy)) {
63  // Use a policy to prepare an authorized context
64  // with a user group.
65  $policy = $this->getMock('AuthorizationPolicy', array('effect'));
66  $policy->expects($this->any())
67  ->method('effect')
68  ->will($this->returnCallback(array($this, 'mockEffect')));
69  $this->authorizationContextManipulationPolicy = $policy;
70  }
71  return $this->authorizationContextManipulationPolicy;
72  }
73 
79  public function mockEffect() {
80  // Add a user group to the authorized context
81  // of the authorization context manipulation policy.
83  $userGroup = new UserGroup();
84  $userGroup->setRoleId(ROLE_ID_TEST);
85  $policy->addAuthorizedContextObject(ASSOC_TYPE_USER_GROUP, $userGroup);
86 
87  // Add user roles array to the authorized context.
88  $userRoles = array(ROLE_ID_TEST, ROLE_ID_SITE_ADMIN);
89  $policy->addAuthorizedContextObject(ASSOC_TYPE_USER_ROLES, $userRoles);
90  return AUTHORIZATION_PERMIT;
91  }
92 
101  protected function getMockRequest($requestedOp, $context = null, $user = null) {
102  // Mock a request to the permitted operation.
103  $request = new PKPRequest();
104 
105  $this->setContextObjects($context);
106 
107  // Mock a router.
108  $router = $this->getMock('PKPRouter', array('getRequestedOp', 'getContext'));
109 
110  // Mock the getRequestedOp() method.
111  $router->expects($this->any())
112  ->method('getRequestedOp')
113  ->will($this->returnValue($requestedOp));
114 
115  // Mock the getContext() method.
116  $router->expects($this->any())
117  ->method('getContext')
118  ->will($this->returnCallback(array($this, 'mockGetContext')));
119 
120  // Put a user into the registry if one has been
121  // passed in.
122  if ($user instanceof User) {
123  Registry::set('user', $user);
124  }
125 
126  $request->setRouter($router);
127  return $request;
128  }
129 
136  public function mockGetContext() {
137  $functionArgs = func_get_args();
138  $contextLevel = $functionArgs[1];
139 
140  $contextObjects = $this->getContextObjects();
141  if (!empty($contextObjects[$contextLevel - 1])) {
142  return $contextObjects[$contextLevel - 1];
143  }
144  return null;
145  }
146 }
147 ?>
static set($key, &$value)
getAuthorizationContextManipulationPolicy()
Basic class describing users existing in the system.
Definition: User.inc.php:20
Abstract base test class that provides infrastructure for several types of policy tests...
Describes user groups.
getMockRequest($requestedOp, $context=null, $user=null)
Class providing operations associated with HTTP requests.
Class that implements functionality common to all PKP unit test cases.