Open Monograph Press  3.3.0
PKPPageRouterTest.php
1 <?php
2 
18 require_mock_env('env1');
19 
20 import('lib.pkp.classes.core.PKPPageRouter');
21 import('lib.pkp.tests.classes.core.PKPRouterTestCase');
22 import('classes.security.Validation'); // This will import our mock validation class.
23 import('classes.i18n.AppLocale'); // This will import our mock locale.
24 
29  protected function setUp() : void {
30  parent::setUp();
31  $this->router = $this->getMockBuilder(PKPPageRouter::class)
32  ->setMethods(array('getCacheablePages'))
33  ->getMock();
34  $this->router->expects($this->any())
35  ->method('getCacheablePages')
36  ->will($this->returnValue(array('cacheable')));
37  }
38 
42  public function testIsCacheableNotInstalled() {
43  $this->setTestConfiguration('request2', 'classes/core/config'); // not installed
44  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
45  self::assertFalse($this->router->isCacheable($this->request));
46  }
47 
51  public function testIsCacheableWithPost() {
52  $this->setTestConfiguration('request1', 'classes/core/config'); // installed
53  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
54  $_POST = array('somevar' => 'someval');
55  self::assertFalse($this->router->isCacheable($this->request));
56  }
57 
61  public function testIsCacheableWithPathinfo() {
62  $this->setTestConfiguration('request1', 'classes/core/config'); // installed
63  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
64  $_GET = array('somevar' => 'someval');
65  $_SERVER = array(
66  'PATH_INFO' => '/context1/context2/somepage',
67  'SCRIPT_NAME' => '/index.php',
68  );
69  self::assertFalse($this->router->isCacheable($this->request));
70 
71  $_GET = array();
72  self::assertFalse($this->router->isCacheable($this->request));
73 
74  }
75 
82  $this->setTestConfiguration('request1', 'classes/core/config'); // installed
83  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
84  $_GET = array();
85  $_SERVER = array(
86  'PATH_INFO' => '/context1/context2/cacheable',
87  'SCRIPT_NAME' => '/index.php',
88  );
89 
90  self::assertTrue($this->router->isCacheable($this->request, true));
91 
93  self::assertFalse($this->router->isCacheable($this->request, true));
95  }
96 
100  public function testIsCacheableWithoutPathinfo() {
101  $this->setTestConfiguration('request1', 'classes/core/config'); // installed
102  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
103  $_GET = array('somevar' => 'someval');
104  self::assertFalse($this->router->isCacheable($this->request, true));
105 
106  $_GET = array(
107  'firstContext' => 'something',
108  'secondContext' => 'something',
109  'page' => 'something',
110  'op' => 'something',
111  'path' => 'something'
112  );
113  self::assertFalse($this->router->isCacheable($this->request, true));
114  }
115 
122  $this->setTestConfiguration('request1', 'classes/core/config'); // installed
123  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
124 
125  $_GET = array(
126  'page' => 'cacheable'
127  );
128  self::assertTrue($this->router->isCacheable($this->request, true));
129  }
130 
134  public function testGetCacheFilename() {
135  // Override parent test
136  $this->markTestSkipped();
137  }
138 
143  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
144  $_SERVER = array(
145  'PATH_INFO' => '/context1/context2/index',
146  'SCRIPT_NAME' => '/index.php',
147  );
148  $expectedId = '/context1/context2/index-en_US';
149  self::assertEquals(Core::getBaseDir().'/cache/wc-'.md5($expectedId).'.html', $this->router->getCacheFilename($this->request));
150  }
151 
156  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
157  $_GET = array(
158  'firstContext' => 'something',
159  'secondContext' => 'something',
160  'page' => 'index'
161  );
162  $expectedId = 'something-something-index---en_US';
163  self::assertEquals(Core::getBaseDir().'/cache/wc-'.md5($expectedId).'.html', $this->router->getCacheFilename($this->request));
164  }
165 
170  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
171 
172  $_SERVER = array(
173  'PATH_INFO' => '/context1/context2/some#page',
174  'SCRIPT_NAME' => '/index.php',
175  );
176  self::assertEquals('somepage', $this->router->getRequestedPage($this->request));
177  }
178 
183  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
184 
185  $_GET['page'] = 'some#page';
186  self::assertEquals('somepage', $this->router->getRequestedPage($this->request));
187  }
188 
193  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
194 
195  $_SERVER = array(
196  'PATH_INFO' => '/context1/context2',
197  'SCRIPT_NAME' => '/index.php',
198  );
199  self::assertEquals('', $this->router->getRequestedPage($this->request));
200  }
201 
206  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
207 
208  unset($_GET['page']);
209  self::assertEquals('', $this->router->getRequestedPage($this->request));
210  }
211 
215  public function testGetRequestedOpWithPathinfo() {
216  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
217 
218  $_SERVER = array(
219  'PATH_INFO' => '/context1/context2/somepage/some#op',
220  'SCRIPT_NAME' => '/index.php',
221  );
222  self::assertEquals('someop', $this->router->getRequestedOp($this->request));
223  }
224 
229  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
230 
231  $_GET['op'] = 'some#op';
232  self::assertEquals('someop', $this->router->getRequestedOp($this->request));
233  }
234 
238  public function testGetRequestedOpWithEmptyOp() {
239  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
240 
241  $_SERVER = array(
242  'PATH_INFO' => '/context1/context2/somepage',
243  'SCRIPT_NAME' => '/index.php',
244  );
245  self::assertEquals('index', $this->router->getRequestedOp($this->request));
246  }
247 
252  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
253 
254  unset($_GET['op']);
255  self::assertEquals('index', $this->router->getRequestedOp($this->request));
256  }
257 
261  public function testUrlWithPathinfo() {
262  $this->setTestConfiguration('request1', 'classes/core/config'); // restful URLs
263  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
264  $_SERVER = array(
265  'SERVER_NAME' => 'mydomain.org',
266  'SCRIPT_NAME' => '/index.php',
267  'PATH_INFO' => '/current-context1/current-context2/current-page/current-op'
268  );
269 
270  // Simulate context DAOs
271  $this->_setUpMockDAOs();
272 
273  $result = $this->router->url($this->request);
274  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/current-page/current-op', $result);
275 
276  $result = $this->router->url($this->request, 'new-context1');
277  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2', $result);
278 
279  $result = $this->router->url($this->request, array('new-context1', 'new?context2'));
280  self::assertEquals('http://mydomain.org/index.php/new-context1/new%3Fcontext2', $result);
281 
282  $result = $this->router->url($this->request, array(), 'new-page');
283  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/new-page', $result);
284 
285  $result = $this->router->url($this->request, array(), null, 'new-op');
286  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/current-page/new-op', $result);
287 
288  $result = $this->router->url($this->request, 'new-context1', 'new-page');
289  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/new-page', $result);
290 
291  $result = $this->router->url($this->request, 'new-context1', 'new-page', 'new-op');
292  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/new-page/new-op', $result);
293 
294  $result = $this->router->url($this->request, 'new-context1', null, 'new-op');
295  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/index/new-op', $result);
296 
297  $result = $this->router->url($this->request, 'new-context1', null, null, 'add?path');
298  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/index/index/add%3Fpath', $result);
299 
300  $result = $this->router->url($this->request, 'new-context1', null, null, array('add-path1', 'add?path2'));
301  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/index/index/add-path1/add%3Fpath2', $result);
302 
303  $result = $this->router->url($this->request, array('firstContext' => null, 'secondContext' => null), null, 'new-op', 'add-path');
304  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/current-page/new-op/add-path', $result);
305 
306  $result = $this->router->url($this->request, 'new-context1', null, null, null,
307  array(
308  'key1' => 'val1?',
309  'key2' => array('val2-1', 'val2?2')
310  )
311  );
312  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2?key1=val1%3F&key2[]=val2-1&key2[]=val2%3F2', $result);
313 
314  $result = $this->router->url($this->request, 'new-context1', null, null, null, null, 'some?anchor');
315  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2#some%3Fanchor', $result);
316 
317  $result = $this->router->url($this->request, 'new-context1', null, 'new-op', 'add-path', array('key' => 'val'), 'some-anchor');
318  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/index/new-op/add-path?key=val#some-anchor', $result);
319 
320  $result = $this->router->url($this->request, 'new-context1', null, null, null, array('key1' => 'val1', 'key2' => 'val2'), null, true);
321  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2?key1=val1&amp;key2=val2', $result);
322  }
323 
328  $this->setTestConfiguration('request1', 'classes/core/config'); // contains overridden context
329 
330  // Set up a request with an overridden context
331  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
332  $_SERVER = array(
333  'SERVER_NAME' => 'mydomain.org',
334  'SCRIPT_NAME' => '/index.php',
335  'PATH_INFO' => '/overridden-context/current-context2/current-page/current-op'
336  );
337  $this->_setUpMockDAOs('overridden-context');
338  $result = $this->router->url($this->request);
339  self::assertEquals('http://some-domain/xyz-context/current-context2/current-page/current-op', $result);
340  }
341 
346  $this->setTestConfiguration('request1', 'classes/core/config'); // contains overridden context
347 
348  // Same set-up as in testUrlWithPathinfoAndOverriddenBaseUrl()
349  // but this time use a request with non-overridden context and
350  // 'overridden-context' as new context. (Reproduces #5118)
351  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
352  $_SERVER = array(
353  'SERVER_NAME' => 'mydomain.org',
354  'SCRIPT_NAME' => '/index.php',
355  'PATH_INFO' => '/current-context1/current-context2/current-page/current-op'
356  );
357  $this->_setUpMockDAOs('current-context1', 'current-context2', true);
358  $result = $this->router->url($this->request, 'overridden-context', 'new-page');
359  self::assertEquals('http://some-domain/xyz-context/current-context2/new-page', $result);
360  }
361 
366  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
367  $_SERVER = array(
368  'SERVER_NAME' => 'mydomain.org',
369  'SCRIPT_NAME' => '/index.php',
370  'PATH_INFO' => '/current-context1/current-context2/current-page/current-op'
371  );
372 
373  // Simulate context DAOs
374  $this->_setUpMockDAOs('current-context1', 'current-context2', false, true);
375 
376  $result = $this->router->url($this->request);
377  self::assertEquals('http://mydomain.org/index.php/current-context1/index/current-page/current-op', $result);
378  }
379 
383  public function testUrlWithoutPathinfo() {
384  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
385  $_SERVER = array(
386  'SERVER_NAME' => 'mydomain.org',
387  'SCRIPT_NAME' => '/index.php',
388  );
389  $_GET = array(
390  'firstContext' => 'current-context1',
391  'secondContext' => 'current-context2',
392  'page' => 'current-page',
393  'op' => 'current-op'
394  );
395 
396  // Simulate context DAOs
397  $this->_setUpMockDAOs();
398 
399  $result = $this->router->url($this->request);
400  self::assertEquals('http://mydomain.org/index.php?firstContext=current-context1&secondContext=current-context2&page=current-page&op=current-op', $result);
401 
402  $result = $this->router->url($this->request, 'new-context1');
403  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2', $result);
404 
405  $result = $this->router->url($this->request, array('new-context1', 'new-context2'));
406  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=new-context2', $result);
407 
408  $result = $this->router->url($this->request, array(), 'new-page');
409  self::assertEquals('http://mydomain.org/index.php?firstContext=current-context1&secondContext=current-context2&page=new-page', $result);
410 
411  $result = $this->router->url($this->request, array(), null, 'new-op');
412  self::assertEquals('http://mydomain.org/index.php?firstContext=current-context1&secondContext=current-context2&page=current-page&op=new-op', $result);
413 
414  $result = $this->router->url($this->request, 'new-context1', 'new-page');
415  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&page=new-page', $result);
416 
417  $result = $this->router->url($this->request, 'new-context1', 'new-page', 'new-op');
418  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&page=new-page&op=new-op', $result);
419 
420  $result = $this->router->url($this->request, 'new-context1', null, 'new-op');
421  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&page=index&op=new-op', $result);
422 
423  $result = $this->router->url($this->request, 'new-context1', null, null, 'add?path');
424  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&page=index&op=index&path[]=add%3Fpath', $result);
425 
426  $result = $this->router->url($this->request, 'new-context1', null, null, array('add-path1', 'add?path2'));
427  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&page=index&op=index&path[]=add-path1&path[]=add%3Fpath2', $result);
428 
429  $result = $this->router->url($this->request, 'new-context1', null, null, null,
430  array(
431  'key1' => 'val1?',
432  'key2' => array('val2-1', 'val2?2')
433  )
434  );
435  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&key1=val1%3F&key2[]=val2-1&key2[]=val2%3F2', $result);
436 
437  $result = $this->router->url($this->request, 'new-context1', null, null, null, null, 'some?anchor');
438  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2#some%3Fanchor', $result);
439 
440  $result = $this->router->url($this->request, 'new-context1', null, 'new-op', 'add-path', array('key' => 'val'), 'some-anchor');
441  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&page=index&op=new-op&path[]=add-path&key=val#some-anchor', $result);
442 
443  $result = $this->router->url($this->request, 'new-context1', null, null, null, array('key1' => 'val1', 'key2' => 'val2'), null, true);
444  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&amp;secondContext=current-context2&amp;key1=val1&amp;key2=val2', $result);
445  }
446 
451  $this->setTestConfiguration('request2', 'classes/core/config'); // contains overridden context
452  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
453  $_SERVER = array(
454  'SERVER_NAME' => 'mydomain.org',
455  'SCRIPT_NAME' => '/index.php',
456  );
457  $_GET = array(
458  'firstContext' => 'overridden-context',
459  'secondContext' => 'current-context2',
460  'page' => 'current-page',
461  'op' => 'current-op'
462  );
463 
464  // Simulate context DAOs
465  $this->_setUpMockDAOs('overridden-context');
466 
467  // NB: This also tests whether unusual URL elements like user, password and port
468  // will be handled correctly.
469  $result = $this->router->url($this->request);
470  self::assertEquals('http://some-user:some-pass@some-domain:8080/?firstContext=xyz-context&secondContext=current-context2&page=current-page&op=current-op', $result);
471  }
472 
477  $this->setTestConfiguration('request2', 'classes/core/config'); // restful URLs enabled
478  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
479  $_SERVER = array(
480  'SERVER_NAME' => 'mydomain.org',
481  'SCRIPT_NAME' => '/index.php',
482  );
483  $_GET = array(
484  'firstContext' => 'current-context1',
485  'secondContext' => 'current-context2',
486  'page' => 'current-page',
487  'op' => 'current-op'
488  );
489 
490  // Simulate context DAOs
491  $this->_setUpMockDAOs('current-context1', 'current-context2', false, true);
492 
493  // NB: This also tests whether restful URLs work correctly.
494  $result = $this->router->url($this->request);
495  self::assertEquals('http://mydomain.org/?firstContext=current-context1&secondContext=index&page=current-page&op=current-op', $result);
496  }
497 }
498 
PKPRouterTestCase\_setUpMockDAOs
_setUpMockDAOs($firstContextPath='current-context1', $secondContextPath='current-context2', $firstContextIsNull=false, $secondContextIsNull=false)
Definition: PKPRouterTestCase.inc.php:346
PKPPageRouterTest\testUrlWithoutPathinfoAndSecondContextObjectIsNull
testUrlWithoutPathinfoAndSecondContextObjectIsNull()
Definition: PKPPageRouterTest.php:476
PKPPageRouterTest\testUrlWithPathinfo
testUrlWithPathinfo()
Definition: PKPPageRouterTest.php:261
PKPPageRouterTest
Tests for the PKPPageRouter class.
Definition: PKPPageRouterTest.php:28
PKPPageRouterTest\testIsCacheableWithPathinfoSuccess
testIsCacheableWithPathinfoSuccess()
Definition: PKPPageRouterTest.php:81
PKPPageRouterTest\testIsCacheableNotInstalled
testIsCacheableNotInstalled()
Definition: PKPPageRouterTest.php:42
PKPPageRouterTest\testGetRequestedOpWithEmptyOp
testGetRequestedOpWithEmptyOp()
Definition: PKPPageRouterTest.php:238
PKPPageRouterTest\testUrlWithoutPathinfoAndOverriddenBaseUrl
testUrlWithoutPathinfoAndOverriddenBaseUrl()
Definition: PKPPageRouterTest.php:450
PKPPageRouterTest\testGetRequestedOpWithPathinfoDisabled
testGetRequestedOpWithPathinfoDisabled()
Definition: PKPPageRouterTest.php:228
PKPPageRouterTest\testUrlWithoutPathinfo
testUrlWithoutPathinfo()
Definition: PKPPageRouterTest.php:383
PKPPageRouterTest\testGetRequestedPageWithEmtpyPage
testGetRequestedPageWithEmtpyPage()
Definition: PKPPageRouterTest.php:192
PKPPageRouterTest\testIsCacheableWithPost
testIsCacheableWithPost()
Definition: PKPPageRouterTest.php:51
PKPPageRouterTest\testGetRequestedPageWithPathinfoDisabled
testGetRequestedPageWithPathinfoDisabled()
Definition: PKPPageRouterTest.php:182
PKPPageRouterTest\testUrlWithPathinfoAndOverriddenNewContext
testUrlWithPathinfoAndOverriddenNewContext()
Definition: PKPPageRouterTest.php:345
PKPPageRouterTest\setUp
setUp()
Definition: PKPPageRouterTest.php:29
PKPPageRouterTest\testUrlWithPathinfoAndOverriddenBaseUrl
testUrlWithPathinfoAndOverriddenBaseUrl()
Definition: PKPPageRouterTest.php:327
PKPTestCase\setTestConfiguration
setTestConfiguration($config, $configPath='config')
Definition: PKPTestCase.inc.php:113
PKPPageRouterTest\testIsCacheableWithoutPathinfoSuccess
testIsCacheableWithoutPathinfoSuccess()
Definition: PKPPageRouterTest.php:121
PKPPageRouterTest\testGetRequestedPageWithPathinfo
testGetRequestedPageWithPathinfo()
Definition: PKPPageRouterTest.php:169
PKPPageRouterTest\testGetRequestedOpWithPathinfo
testGetRequestedOpWithPathinfo()
Definition: PKPPageRouterTest.php:215
PKPPageRouterTest\testGetCacheFilenameWithoutPathinfo
testGetCacheFilenameWithoutPathinfo()
Definition: PKPPageRouterTest.php:155
PKPPageRouterTest\testGetRequestedOpWithPathinfoDisabledAndEmptyOp
testGetRequestedOpWithPathinfoDisabledAndEmptyOp()
Definition: PKPPageRouterTest.php:251
PKPPageRouterTest\testIsCacheableWithoutPathinfo
testIsCacheableWithoutPathinfo()
Definition: PKPPageRouterTest.php:100
PKPPageRouterTest\testGetRequestedPageWithPathinfoDisabledAndEmtpyPage
testGetRequestedPageWithPathinfoDisabledAndEmtpyPage()
Definition: PKPPageRouterTest.php:205
PKPRouterTestCase\_setUpMockEnvironment
_setUpMockEnvironment($pathInfoEnabled=self::PATHINFO_ENABLED, $contextDepth=2, $contextList=array('firstContext', 'secondContext'))
Definition: PKPRouterTestCase.inc.php:300
PKPRouterTestCase
Base tests class for PKPRouter tests.
Definition: PKPRouterTestCase.inc.php:28
Validation\setIsLoggedIn
static setIsLoggedIn($isLoggedIn)
Definition: MockValidation.inc.php:24
PKPPageRouterTest\testIsCacheableWithPathinfo
testIsCacheableWithPathinfo()
Definition: PKPPageRouterTest.php:61
PKPPageRouterTest\testGetCacheFilename
testGetCacheFilename()
Definition: PKPPageRouterTest.php:134
Core\getBaseDir
static getBaseDir()
Definition: Core.inc.php:37
PKPPageRouterTest\testUrlWithPathinfoAndSecondContextObjectIsNull
testUrlWithPathinfoAndSecondContextObjectIsNull()
Definition: PKPPageRouterTest.php:365
PKPPageRouterTest\testGetCacheFilenameWithPathinfo
testGetCacheFilenameWithPathinfo()
Definition: PKPPageRouterTest.php:142