Open Journal Systems  3.3.0
PKPComponentRouterTest.php
1 <?php
2 
17 require_mock_env('env1');
18 
19 import('lib.pkp.classes.core.PKPComponentRouter');
20 import('lib.pkp.tests.classes.core.PKPRouterTestCase');
21 
26  protected function setUp() : void {
27  parent::setUp();
28  $this->router = new PKPComponentRouter();
29  }
30 
31  public function testSupports() {
32  // This method only exists to override and neutralize the parent class'
33  // testSupports() which is not relevant for component routers.
34  $this->markTestSkipped();
35  }
36 
45  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
46 
47  $_SERVER = array(
48  'SCRIPT_NAME' => '/index.php',
49  'PATH_INFO' => '/context1/context2/$$$call$$$/grid/notifications/task-notifications-grid/fetch-grid'
50  );
51  self::assertTrue($this->router->supports($this->request));
52  }
53 
62  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
63 
64  $_SERVER = array(
65  'SCRIPT_NAME' => '/index.php',
66  'PATH_INFO' => '/context1/context2/page/operation'
67  );
68  self::assertEquals('', $this->router->getRequestedComponent($this->request));
69  self::assertFalse($this->router->supports($this->request));
70  }
71 
80  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
81 
82  $_SERVER = array(
83  'SCRIPT_NAME' => '/index.php',
84  'PATH_INFO' => '/context1/context2/path/to/handler/operation'
85  );
86  self::assertEquals('', $this->router->getRequestedComponent($this->request));
87  self::assertFalse($this->router->supports($this->request));
88  }
89 
98  $this->markTestSkipped();
99  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
100 
101  $_SERVER = array(
102  'SCRIPT_NAME' => '/index.php',
103  'PATH_INFO' => '/context1/context2/$$$call$$$/inexistent/component/fetch-grid'
104  );
105  self::assertEquals('inexistent.ComponentHandler', $this->router->getRequestedComponent($this->request));
106  self::assertFalse($this->router->supports($this->request));
107  }
108 
116  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
117 
118  $_SERVER = array(
119  'SCRIPT_NAME' => '/index.php',
120  'PATH_INFO' => '/context1/context2/$$$call$$$/path/to/some-component/operation'
121  );
122  self::assertEquals('path.to.SomeComponentHandler', $this->router->getRequestedComponent($this->request));
123  }
124 
132  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
133 
134  $_SERVER = array(
135  'SCRIPT_NAME' => '/index.php',
136  'PATH_INFO' => '/context1/context2/$$$call$$$/path/to/some-#component/operation'
137  );
138  self::assertEquals('', $this->router->getRequestedComponent($this->request));
139  }
140 
148  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
149 
150  $_GET = array(
151  'component' => 'path.to.some-component',
152  'op' => 'operation'
153  );
154  self::assertEquals('path.to.SomeComponentHandler', $this->router->getRequestedComponent($this->request));
155  }
156 
163  public function testGetRequestedOpWithPathinfo() {
164  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
165 
166  $_SERVER = array(
167  'SCRIPT_NAME' => '/index.php',
168  'PATH_INFO' => '/context1/context2/$$$call$$$/path/to/some-component/some-op'
169  );
170  self::assertEquals('someOp', $this->router->getRequestedOp($this->request));
171  }
172 
180  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
181 
182  $_GET = array(
183  'component' => 'path.to.some-component',
184  'op' => 'some-op'
185  );
186  self::assertEquals('someOp', $this->router->getRequestedOp($this->request));
187  }
188 
196  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
197 
198  $_GET = array(
199  'op' => 'some-op'
200  );
201  self::assertEquals('', $this->router->getRequestedOp($this->request));
202  }
203 
211  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
212 
213  $_SERVER = array(
214  'SCRIPT_NAME' => '/index.php',
215  'PATH_INFO' => '/context1/context2/$$$call$$$/path/to/some-component/so#me-op'
216  );
217  self::assertEquals('', $this->router->getRequestedOp($this->request));
218  }
219 
229  public function testRoute() {
230  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
231 
232  $_SERVER = array(
233  'SCRIPT_NAME' => '/index.php',
234  'PATH_INFO' => '/context1/context2/$$$call$$$/grid/notifications/task-notifications-grid/fetch-grid'
235  );
236  $_GET = array(
237  'arg1' => 'val1',
238  'arg2' => 'val2'
239  );
240 
241  // Simulate context DAOs
242  $this->_setUpMockDAOs('context1', 'context2');
243 
244  // Route the request. This should call NotificationsGridHandler::fetchGrid()
245  // with a reference to the request object as the first argument.
246  $this->router->route($this->request);
247 
248  self::assertNotNull($serviceEndpoint =& $this->router->getRpcServiceEndpoint($this->request));
249  self::assertInstanceOf('NotificationsGridHandler', $handler =& $serviceEndpoint[0]);
250  $firstContextDao = DAORegistry::getDAO('FirstContextDAO');
251  self::assertInstanceOf('Context', $firstContextDao->getByPath('context1'));
252  $secondContextDao = DAORegistry::getDAO('SecondContextDAO');
253  self::assertInstanceOf('Context', $secondContextDao->getByPath('context2'));
254  }
255 
263  public function testUrlWithPathinfo() {
264  $this->setTestConfiguration('request1', 'classes/core/config'); // restful URLs
265  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
266  $_SERVER = array(
267  'SERVER_NAME' => 'mydomain.org',
268  'SCRIPT_NAME' => '/index.php',
269  'PATH_INFO' => '/current-context1/current-context2/$$$call$$$/current/component-class/current-op'
270  );
271 
272  // Simulate context DAOs
273  $this->_setUpMockDAOs();
274 
275  $result = $this->router->url($this->request);
276  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/$$$call$$$/current/component-class/current-op', $result);
277 
278  $result = $this->router->url($this->request, 'new-context1');
279  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/current/component-class/current-op', $result);
280 
281  $result = $this->router->url($this->request, array('new-context1', 'new?context2'));
282  self::assertEquals('http://mydomain.org/index.php/new-context1/new%3Fcontext2/$$$call$$$/current/component-class/current-op', $result);
283 
284  $result = $this->router->url($this->request, array(), 'new.NewComponentHandler');
285  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/$$$call$$$/new/new-component/current-op', $result);
286 
287  $result = $this->router->url($this->request, array(), null, 'newOp');
288  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/$$$call$$$/current/component-class/new-op', $result);
289 
290  $result = $this->router->url($this->request, 'new-context1', 'new.NewComponentHandler');
291  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/new/new-component/current-op', $result);
292 
293  $result = $this->router->url($this->request, 'new-context1', 'new.NewComponentHandler', 'newOp');
294  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/new/new-component/new-op', $result);
295 
296  $result = $this->router->url($this->request, 'new-context1', null, 'newOp');
297  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/current/component-class/new-op', $result);
298 
299  $result = $this->router->url($this->request, array('firstContext' => null, 'secondContext' => null), null, 'newOp');
300  self::assertEquals('http://mydomain.org/index.php/current-context1/current-context2/$$$call$$$/current/component-class/new-op', $result);
301 
302  $params = array(
303  'key1' => 'val1?',
304  'key2' => array('val2-1', 'val2?2')
305  );
306  $result = $this->router->url($this->request, 'new-context1', null, null, null, $params, null, true);
307  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/current/component-class/current-op?key1=val1%3F&amp;key2%5B%5D=val2-1&amp;key2%5B%5D=val2%3F2', $result);
308 
309  $result = $this->router->url($this->request, 'new-context1', null, null, null, $params, null, false);
310  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/current/component-class/current-op?key1=val1%3F&key2[]=val2-1&key2[]=val2%3F2', $result);
311 
312  $result = $this->router->url($this->request, 'new-context1', null, null, null, null, 'some?anchor');
313  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/current/component-class/current-op#some%3Fanchor', $result);
314 
315  $result = $this->router->url($this->request, 'new-context1', null, 'newOp', null, array('key' => 'val'), 'some-anchor');
316  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/current/component-class/new-op?key=val#some-anchor', $result);
317 
318  $result = $this->router->url($this->request, 'new-context1', null, null, null, array('key1' => 'val1', 'key2' => 'val2'), null, true);
319  self::assertEquals('http://mydomain.org/index.php/new-context1/current-context2/$$$call$$$/current/component-class/current-op?key1=val1&amp;key2=val2', $result);
320  }
321 
330  $this->setTestConfiguration('request1', 'classes/core/config'); // contains 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/$$$call$$$/current/component-class/current-op'
336  );
337 
338  // Simulate context DAOs
339  $this->_setUpMockDAOs('overridden-context');
340 
341  $result = $this->router->url($this->request);
342  self::assertEquals('http://some-domain/xyz-context/current-context2/$$$call$$$/current/component-class/current-op', $result);
343  }
344 
353  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_ENABLED);
354  $_SERVER = array(
355  'SERVER_NAME' => 'mydomain.org',
356  'SCRIPT_NAME' => '/index.php',
357  'PATH_INFO' => '/current-context1/current-context2/$$$call$$$/current/component-class/current-op'
358  );
359 
360  // Simulate context DAOs
361  $this->_setUpMockDAOs('current-context1', 'current-context2', false, true);
362 
363  $result = $this->router->url($this->request);
364  self::assertEquals('http://mydomain.org/index.php/current-context1/index/$$$call$$$/current/component-class/current-op', $result);
365  }
366 
374  public function testUrlWithoutPathinfo() {
375  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
376  $_SERVER = array(
377  'SERVER_NAME' => 'mydomain.org',
378  'SCRIPT_NAME' => '/index.php',
379  );
380  $_GET = array(
381  'firstContext' => 'current-context1',
382  'secondContext' => 'current-context2',
383  'component' => 'current.component-class',
384  'op' => 'current-op'
385  );
386 
387  // Simulate context DAOs
388  $this->_setUpMockDAOs();
389 
390  $result = $this->router->url($this->request);
391  self::assertEquals('http://mydomain.org/index.php?firstContext=current-context1&secondContext=current-context2&component=current.component-class&op=current-op', $result);
392 
393  $result = $this->router->url($this->request, 'new-context1');
394  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&component=current.component-class&op=current-op', $result);
395 
396  $result = $this->router->url($this->request, array('new-context1', 'new-context2'));
397  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=new-context2&component=current.component-class&op=current-op', $result);
398 
399  $result = $this->router->url($this->request, array(), 'new.NewComponentHandler');
400  self::assertEquals('http://mydomain.org/index.php?firstContext=current-context1&secondContext=current-context2&component=new.new-component&op=current-op', $result);
401 
402  $result = $this->router->url($this->request, array(), null, 'newOp');
403  self::assertEquals('http://mydomain.org/index.php?firstContext=current-context1&secondContext=current-context2&component=current.component-class&op=new-op', $result);
404 
405  $result = $this->router->url($this->request, 'new-context1', 'new.NewComponentHandler');
406  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&component=new.new-component&op=current-op', $result);
407 
408  $result = $this->router->url($this->request, 'new-context1', 'new.NewComponentHandler', 'newOp');
409  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&component=new.new-component&op=new-op', $result);
410 
411  $result = $this->router->url($this->request, 'new-context1', null, 'newOp');
412  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&component=current.component-class&op=new-op', $result);
413 
414  $params = array(
415  'key1' => 'val1?',
416  'key2' => array('val2-1', 'val2?2')
417  );
418  $result = $this->router->url($this->request, 'new-context1', null, null, null, $params, null, true);
419  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&amp;secondContext=current-context2&amp;component=current.component-class&amp;op=current-op&amp;key1=val1%3F&amp;key2%5B%5D=val2-1&amp;key2%5B%5D=val2%3F2', $result);
420 
421  $result = $this->router->url($this->request, 'new-context1', null, null, null, $params);
422  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&component=current.component-class&op=current-op&key1=val1%3F&key2[]=val2-1&key2[]=val2%3F2', $result);
423 
424  $result = $this->router->url($this->request, 'new-context1', null, null, null, null, 'some?anchor');
425  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&component=current.component-class&op=current-op#some%3Fanchor', $result);
426 
427  $result = $this->router->url($this->request, 'new-context1', null, 'newOp', null, array('key' => 'val'), 'some-anchor');
428  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&secondContext=current-context2&component=current.component-class&op=new-op&key=val#some-anchor', $result);
429 
430  $result = $this->router->url($this->request, 'new-context1', null, null, null, array('key1' => 'val1', 'key2' => 'val2'), null, true);
431  self::assertEquals('http://mydomain.org/index.php?firstContext=new-context1&amp;secondContext=current-context2&amp;component=current.component-class&amp;op=current-op&amp;key1=val1&amp;key2=val2', $result);
432  }
433 
442  $this->setTestConfiguration('request2', 'classes/core/config'); // contains overridden context
443  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
444  $_SERVER = array(
445  'SERVER_NAME' => 'mydomain.org',
446  'SCRIPT_NAME' => '/index.php',
447  );
448  $_GET = array(
449  'firstContext' => 'overridden-context',
450  'secondContext' => 'current-context2',
451  'component' => 'current.component-class',
452  'op' => 'current-op'
453  );
454 
455  // Simulate context DAOs
456  $this->_setUpMockDAOs('overridden-context');
457 
458  // NB: This also tests whether unusual URL elements like user, password and port
459  // will be handled correctly.
460  $result = $this->router->url($this->request);
461  self::assertEquals('http://some-user:some-pass@some-domain:8080/?firstContext=xyz-context&secondContext=current-context2&component=current.component-class&op=current-op', $result);
462  }
463 
472  $this->setTestConfiguration('request2', 'classes/core/config'); // restful URLs enabled
473  $mockApplication = $this->_setUpMockEnvironment(self::PATHINFO_DISABLED);
474  $_SERVER = array(
475  'SERVER_NAME' => 'mydomain.org',
476  'SCRIPT_NAME' => '/index.php',
477  );
478  $_GET = array(
479  'firstContext' => 'current-context1',
480  'secondContext' => 'current-context2',
481  'component' => 'current.component-class',
482  'op' => 'current-op'
483  );
484 
485  // Simulate context DAOs
486  $this->_setUpMockDAOs('current-context1', 'current-context2', false, true);
487 
488  // NB: This also tests whether restful URLs work correctly.
489  $result = $this->router->url($this->request);
490  self::assertEquals('http://mydomain.org/?firstContext=current-context1&secondContext=index&component=current.component-class&op=current-op', $result);
491  }
492 }
493 
PKPComponentRouterTest\testUrlWithoutPathinfo
testUrlWithoutPathinfo()
Definition: PKPComponentRouterTest.php:374
PKPComponentRouterTest\testSupportsWithPathinfoUnsuccessfulComponentFileDoesNotExist
testSupportsWithPathinfoUnsuccessfulComponentFileDoesNotExist()
Definition: PKPComponentRouterTest.php:97
PKPComponentRouter
Class mapping an HTTP request to a component handler operation.
Definition: PKPComponentRouter.inc.php:66
PKPComponentRouterTest\testUrlWithPathinfo
testUrlWithPathinfo()
Definition: PKPComponentRouterTest.php:263
PKPComponentRouterTest\testGetRequestedOpWithPathinfoDisabled
testGetRequestedOpWithPathinfoDisabled()
Definition: PKPComponentRouterTest.php:179
PKPComponentRouterTest\testUrlWithPathinfoAndSecondContextObjectIsNull
testUrlWithPathinfoAndSecondContextObjectIsNull()
Definition: PKPComponentRouterTest.php:352
PKPRouterTestCase\_setUpMockDAOs
_setUpMockDAOs($firstContextPath='current-context1', $secondContextPath='current-context2', $firstContextIsNull=false, $secondContextIsNull=false)
Definition: PKPRouterTestCase.inc.php:346
PKPComponentRouterTest\testGetRequestedComponentWithPathinfoDisabled
testGetRequestedComponentWithPathinfoDisabled()
Definition: PKPComponentRouterTest.php:147
PKPComponentRouterTest\testSupportsWithPathinfoUnsuccessfulNoComponentNoMarker
testSupportsWithPathinfoUnsuccessfulNoComponentNoMarker()
Definition: PKPComponentRouterTest.php:79
PKPComponentRouterTest\testGetRequestedOpWithPathinfoAndMalformedOpString
testGetRequestedOpWithPathinfoAndMalformedOpString()
Definition: PKPComponentRouterTest.php:210
PKPComponentRouterTest\testRoute
testRoute()
Definition: PKPComponentRouterTest.php:229
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
PKPComponentRouterTest\testSupportsWithPathinfoUnsuccessfulNoComponentNotEnoughPathElements
testSupportsWithPathinfoUnsuccessfulNoComponentNotEnoughPathElements()
Definition: PKPComponentRouterTest.php:61
PKPComponentRouterTest\setUp
setUp()
Definition: PKPComponentRouterTest.php:26
PKPComponentRouterTest\testGetRequestedComponentWithPathinfo
testGetRequestedComponentWithPathinfo()
Definition: PKPComponentRouterTest.php:115
PKPComponentRouterTest\testSupportsWithPathinfoSuccessful
testSupportsWithPathinfoSuccessful()
Definition: PKPComponentRouterTest.php:44
PKPComponentRouterTest
Tests for the PKPComponentRouter class.
Definition: PKPComponentRouterTest.php:25
PKPComponentRouterTest\testSupports
testSupports()
Definition: PKPComponentRouterTest.php:31
PKPTestCase\setTestConfiguration
setTestConfiguration($config, $configPath='config')
Definition: PKPTestCase.inc.php:113
PKPComponentRouterTest\testUrlWithoutPathinfoAndSecondContextObjectIsNull
testUrlWithoutPathinfoAndSecondContextObjectIsNull()
Definition: PKPComponentRouterTest.php:471
PKPComponentRouterTest\testUrlWithPathinfoAndOverriddenBaseUrl
testUrlWithPathinfoAndOverriddenBaseUrl()
Definition: PKPComponentRouterTest.php:329
PKPRouterTestCase\_setUpMockEnvironment
_setUpMockEnvironment($pathInfoEnabled=self::PATHINFO_ENABLED, $contextDepth=2, $contextList=array('firstContext', 'secondContext'))
Definition: PKPRouterTestCase.inc.php:300
PKPComponentRouterTest\testUrlWithoutPathinfoAndOverriddenBaseUrl
testUrlWithoutPathinfoAndOverriddenBaseUrl()
Definition: PKPComponentRouterTest.php:441
PKPRouterTestCase
Base tests class for PKPRouter tests.
Definition: PKPRouterTestCase.inc.php:28
PKPComponentRouterTest\testGetRequestedOpWithPathinfo
testGetRequestedOpWithPathinfo()
Definition: PKPComponentRouterTest.php:163
PKPComponentRouterTest\testGetRequestedComponentWithPathinfoAndMalformedComponentString
testGetRequestedComponentWithPathinfoAndMalformedComponentString()
Definition: PKPComponentRouterTest.php:131
PKPComponentRouterTest\testGetRequestedOpWithPathinfoDisabledAndMissingComponent
testGetRequestedOpWithPathinfoDisabledAndMissingComponent()
Definition: PKPComponentRouterTest.php:195