Open Journal Systems  3.3.0
PKPRouterTestCase.inc.php
1 <?php
2 
18 import('lib.pkp.tests.PKPTestCase');
19 import('lib.pkp.classes.core.PKPRouter');
20 import('lib.pkp.classes.core.PKPRequest');
21 import('lib.pkp.classes.plugins.HookRegistry'); // This imports a mock HookRegistry implementation.
22 import('classes.core.Application');
23 import('lib.pkp.classes.db.DAORegistry');
24 
29  const
32 
33  protected
36 
37  protected function setUp() : void {
38  parent::setUp();
40  $this->router = new PKPRouter();
41  }
42 
43  protected function tearDown() : void {
44  parent::tearDown();
46  }
47 
52  public function testGetSetApplication() {
54  self::assertSame($application, $this->router->getApplication());
55  }
56 
61  public function testGetSetDispatcher() {
63  $dispatcher = $application->getDispatcher();
64  self::assertSame($dispatcher, $this->router->getDispatcher());
65  }
66 
70  public function testSupports() {
71  $this->request = new PKPRequest();
72  self::assertTrue($this->router->supports($this->request));
73  }
74 
78  public function testIsCacheable() {
79  $this->markTestSkipped(); // Not currently working
80  $this->request = new PKPRequest();
81  self::assertFalse($this->router->isCacheable($this->request));
82  }
83 
89  // Context depth = 1 but we try to access context level 2
90  $this->_setUpMockEnvironment(self::PATHINFO_ENABLED, 1, array('oneContext'));
91  $this->expectError();
92  $this->router->getRequestedContextPath($this->request, 2);
93  }
94 
99  $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
100  $_SERVER['PATH_INFO'] = null;
101  self::assertEquals(array('index', 'index'),
102  $this->router->getRequestedContextPaths($this->request));
103  }
104 
110  $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
112  $_SERVER['PATH_INFO'] = '/context1/context2/other/path/vars';
113  self::assertEquals(array('context1', 'context2'),
114  $this->router->getRequestedContextPaths($this->request));
115  self::assertEquals('context1',
116  $this->router->getRequestedContextPath($this->request, 1));
117  self::assertEquals('context2',
118  $this->router->getRequestedContextPath($this->request, 2));
119  self::assertEquals(
120  array(array('Router::getRequestedContextPaths', array(array('context1', 'context2')))),
122  );
123  }
124 
129  $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
130  $_SERVER['PATH_INFO'] = '/context';
131  self::assertEquals(array('context', 'index'),
132  $this->router->getRequestedContextPaths($this->request));
133  }
134 
139  $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
140  $_SERVER['PATH_INFO'] = '/context:?#/';
141  self::assertEquals(array('context', 'index'),
142  $this->router->getRequestedContextPaths($this->request));
143  }
144 
149  $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
150  $_GET['firstContext'] = null;
151  $_GET['secondContext'] = null;
152  self::assertEquals(array('index', 'index'),
153  $this->router->getRequestedContextPaths($this->request));
154  }
155 
161  $this->markTestSkipped('Plugins (or something) appear to interfere with the expectations of the called hook list test in Travis environment');
162  $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
164  $_GET['firstContext'] = 'context1';
165  $_GET['secondContext'] = 'context2';
166  self::assertEquals(array('context1', 'context2'),
167  $this->router->getRequestedContextPaths($this->request));
168  self::assertEquals('context1',
169  $this->router->getRequestedContextPath($this->request, 1));
170  self::assertEquals('context2',
171  $this->router->getRequestedContextPath($this->request, 2));
172  self::assertEquals(
173  array(array('Router::getRequestedContextPaths', array(array('context1', 'context2')))),
175  );
176  }
177 
184  $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
185  $_GET['firstContext'] = 'context';
186  self::assertEquals(array('context', 'index'),
187  $this->router->getRequestedContextPaths($this->request));
188  }
189 
196  public function testGetContext() {
197  // We use a 1-level context
198  $this->_setUpMockEnvironment(true, 1, array('someContext'));
199  $_SERVER['PATH_INFO'] = '/contextPath';
200 
201  // Simulate a context DAO
203  $contextDao = $application->getContextDAO();
204  $mockDao = $this->getMockBuilder(get_class($contextDao))
205  ->setMethods(array('getByPath'))
206  ->getMock();
207  DAORegistry::registerDAO('SomeContextDAO', $mockDao);
208 
209  // Set up the mock DAO get-by-path method which
210  // should be called with the context path from
211  // the path info.
212  $expectedResult = $this->getMockBuilder(get_class($contextDao->newDataObject()))->getMock();
213  $mockDao->expects($this->once())
214  ->method('getByPath')
215  ->with('contextPath')
216  ->will($this->returnValue($expectedResult));
217  $result = $this->router->getContext($this->request, 1);
218  self::assertInstanceOf('Context', $result);
219  self::assertEquals($expectedResult, $result);
220 
221  $resultByName = $this->router->getContextByName($this->request, 'someContext');
222  self::assertInstanceOf('Context', $resultByName);
223  self::assertEquals($expectedResult, $resultByName);
224  }
225 
230  public function testGetContextForIndex() {
231  // We use a 1-level context
232  $this->_setUpMockEnvironment(true, 1, array('someContext'));
233  $_SERVER['PATH_INFO'] = '/';
234 
235  $result = $this->router->getContext($this->request, 1);
236  self::assertNull($result);
237 
238  $resultByName = $this->router->getContextByName($this->request, 'someContext');
239  self::assertNull($resultByName);
240  }
241 
245  public function testGetIndexUrl() {
246  $this->_setUpMockEnvironment();
247  $this->setTestConfiguration('request1', 'classes/core/config'); // no restful URLs
248  $_SERVER = array(
249  'SERVER_NAME' => 'mydomain.org',
250  'SCRIPT_NAME' => '/base/index.php'
251  );
253 
254  self::assertEquals('http://mydomain.org/base/index.php', $this->router->getIndexUrl($this->request));
255 
256  // Several hooks should have been triggered.
257  self::assertEquals(
258  array(
259  array('Request::getServerHost', array('mydomain.org', false, true)),
260  array('Request::getProtocol', array('http')),
261  array('Request::getBasePath', array('/base')),
262  array('Request::getBaseUrl', array('http://mydomain.org/base')),
263  array('Router::getIndexUrl' , array('http://mydomain.org/base/index.php'))
264  ),
266  );
267 
268  // Calling getIndexUrl() twice should return the same
269  // result without triggering the hooks again.
271  self::assertEquals('http://mydomain.org/base/index.php', $this->router->getIndexUrl($this->request));
272  self::assertEquals(
273  array(),
275  );
276  }
277 
281  public function testGetIndexUrlRestful() {
282  $this->_setUpMockEnvironment();
283  $this->setTestConfiguration('request2', 'classes/core/config'); // restful URLs
284  $_SERVER = array(
285  'SERVER_NAME' => 'mydomain.org',
286  'SCRIPT_NAME' => '/base/index.php'
287  );
288 
289  self::assertEquals('http://mydomain.org/base', $this->router->getIndexUrl($this->request));
290  }
291 
300  protected function _setUpMockEnvironment($pathInfoEnabled = self::PATHINFO_ENABLED,
301  $contextDepth = 2, $contextList = array('firstContext', 'secondContext')) {
302  // Mock application object without calling its constructor.
303  $mockApplication = $this->getMockBuilder(Application::class)
304  ->setMethods(array('getContextDepth', 'getContextList'))
305  ->getMock();
306 
307  // Set up the getContextDepth() method
308  $mockApplication->expects($this->any())
309  ->method('getContextDepth')
310  ->will($this->returnValue($contextDepth));
311 
312  // Set up the getContextList() method
313  $mockApplication->expects($this->any())
314  ->method('getContextList')
315  ->will($this->returnValue($contextList));
316 
317  $this->router->setApplication($mockApplication);
318  Registry::set('application', $mockApplication);
319 
320  // Dispatcher
321  $dispatcher = $mockApplication->getDispatcher();
322  $this->router->setDispatcher($dispatcher);
323 
324  // Mock request
325  $this->request = $this->getMockBuilder(PKPRequest::class)
326  ->setMethods(array('isPathInfoEnabled'))
327  ->getMock();
328  $this->request->setRouter($this->router);
329  $this->request->expects($this->any())
330  ->method('isPathInfoEnabled')
331  ->will($this->returnValue($pathInfoEnabled));
332 
333  return $mockApplication;
334  }
335 
346  protected function _setUpMockDAOs($firstContextPath = 'current-context1', $secondContextPath = 'current-context2', $firstContextIsNull = false, $secondContextIsNull = false) {
348  $contextDao = $application->getContextDAO();
349  $contextClassName = get_class($contextDao->newDataObject());
350  $mockFirstContextDao = $this->getMockBuilder(get_class($contextDao))
351  ->setMethods(array('getByPath'))
352  ->getMock();
353  if (!$firstContextIsNull) {
354  $firstContextInstance = $this->getMockBuilder($contextClassName)
355  ->setMethods(array('getPath', 'getSetting'))
356  ->getMock();
357  $firstContextInstance->expects($this->any())
358  ->method('getPath')
359  ->will($this->returnValue($firstContextPath));
360  $firstContextInstance->expects($this->any())
361  ->method('getSetting')
362  ->will($this->returnValue(null));
363  $mockFirstContextDao->expects($this->any())
364  ->method('getByPath')
365  ->with($firstContextPath)
366  ->will($this->returnValue($firstContextInstance));
367  }
368  DAORegistry::registerDAO('FirstContextDAO', $mockFirstContextDao);
369 
370  $mockSecondContextDao = $this->getMockBuilder($contextClassName)
371  ->setMethods(array('getByPath'))
372  ->getMock();
373  if (!$secondContextIsNull) {
374  $secondContextInstance = $this->getMockBuilder($contextClassName)
375  ->setMethods(array('getPath'))
376  ->getMock();
377  $secondContextInstance->expects($this->any())
378  ->method('getPath')
379  ->will($this->returnValue($secondContextPath));
380  $mockSecondContextDao->expects($this->any())
381  ->method('getByPath')
382  ->with($secondContextPath)
383  ->will($this->returnValue($secondContextInstance));
384  }
385  DAORegistry::registerDAO('SecondContextDAO', $mockSecondContextDao);
386  }
387 }
388 
PKPRouterTestCase\testGetIndexUrlRestful
testGetIndexUrlRestful()
Definition: PKPRouterTestCase.inc.php:281
HookRegistry\resetCalledHooks
static resetCalledHooks($leaveAlive=false)
Definition: HookRegistry.inc.php:143
PKPRouterTestCase\_setUpMockDAOs
_setUpMockDAOs($firstContextPath='current-context1', $secondContextPath='current-context2', $firstContextIsNull=false, $secondContextIsNull=false)
Definition: PKPRouterTestCase.inc.php:346
PKPRouterTestCase\PATHINFO_DISABLED
const PATHINFO_DISABLED
Definition: PKPRouterTestCase.inc.php:31
$application
$application
Definition: index.php:65
PKPRouterTestCase\testGetRequestedContextPathWithEmptyContextParameters
testGetRequestedContextPathWithEmptyContextParameters()
Definition: PKPRouterTestCase.inc.php:148
PKPRouterTestCase\testGetRequestedContextPathWithPartialContextParameters
testGetRequestedContextPathWithPartialContextParameters()
Definition: PKPRouterTestCase.inc.php:183
PKPRouterTestCase\testGetRequestedContextPathWithInvalidLevel
testGetRequestedContextPathWithInvalidLevel()
Definition: PKPRouterTestCase.inc.php:88
PKPRouterTestCase\testGetSetApplication
testGetSetApplication()
Definition: PKPRouterTestCase.inc.php:52
HookRegistry\rememberCalledHooks
static rememberCalledHooks($askOnly=false, $updateTo=true)
Definition: HookRegistry.inc.php:128
PKPRouterTestCase\setUp
setUp()
Definition: PKPRouterTestCase.inc.php:37
PKPTestCase
Class that implements functionality common to all PKP unit test cases.
Definition: PKPTestCase.inc.php:27
Registry\set
static set($key, &$value)
Definition: Registry.inc.php:53
PKPRouterTestCase\testGetRequestedContextPathWithFullContextParameters
testGetRequestedContextPathWithFullContextParameters()
Definition: PKPRouterTestCase.inc.php:160
PKPRouterTestCase\testGetRequestedContextPathWithInvalidPathInfo
testGetRequestedContextPathWithInvalidPathInfo()
Definition: PKPRouterTestCase.inc.php:138
PKPRouterTestCase\testGetContextForIndex
testGetContextForIndex()
Definition: PKPRouterTestCase.inc.php:230
PKPRouterTestCase\testGetContext
testGetContext()
Definition: PKPRouterTestCase.inc.php:196
HookRegistry\getCalledHooks
static & getCalledHooks()
Definition: HookRegistry.inc.php:153
PKPRouterTestCase\tearDown
tearDown()
Definition: PKPRouterTestCase.inc.php:43
PKPRequest
Class providing operations associated with HTTP requests.
Definition: PKPRequest.inc.php:17
DAORegistry\registerDAO
static registerDAO($name, $dao)
Definition: DAORegistry.inc.php:40
PKPTestCase\setTestConfiguration
setTestConfiguration($config, $configPath='config')
Definition: PKPTestCase.inc.php:113
PKPRouterTestCase\$request
$request
Definition: PKPRouterTestCase.inc.php:35
PKPRouterTestCase\testGetRequestedContextPathWithEmptyPathInfo
testGetRequestedContextPathWithEmptyPathInfo()
Definition: PKPRouterTestCase.inc.php:98
PKPRouterTestCase\testIsCacheable
testIsCacheable()
Definition: PKPRouterTestCase.inc.php:78
PKPRouterTestCase\testGetRequestedContextPathWithPartialPathInfo
testGetRequestedContextPathWithPartialPathInfo()
Definition: PKPRouterTestCase.inc.php:128
PKPRouterTestCase\testSupports
testSupports()
Definition: PKPRouterTestCase.inc.php:70
PKPRouterTestCase\_setUpMockEnvironment
_setUpMockEnvironment($pathInfoEnabled=self::PATHINFO_ENABLED, $contextDepth=2, $contextList=array('firstContext', 'secondContext'))
Definition: PKPRouterTestCase.inc.php:300
PKPRouterTestCase\PATHINFO_ENABLED
const PATHINFO_ENABLED
Definition: PKPRouterTestCase.inc.php:30
PKPRouterTestCase
Base tests class for PKPRouter tests.
Definition: PKPRouterTestCase.inc.php:28
PKPRouter
Basic router class that has functionality common to all routers.
Definition: PKPRouter.inc.php:57
PKPRouterTestCase\testGetRequestedContextPathWithFullPathInfo
testGetRequestedContextPathWithFullPathInfo()
Definition: PKPRouterTestCase.inc.php:109
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
PKPRouterTestCase\testGetIndexUrl
testGetIndexUrl()
Definition: PKPRouterTestCase.inc.php:245
PKPRouterTestCase\$router
$router
Definition: PKPRouterTestCase.inc.php:34
PKPRouterTestCase\testGetSetDispatcher
testGetSetDispatcher()
Definition: PKPRouterTestCase.inc.php:61