RouterTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Elgg;
  3. class RouterTest extends \PHPUnit_Framework_TestCase {
  4. /**
  5. * @var \Elgg\PluginHooksService
  6. */
  7. protected $hooks;
  8. /**
  9. * @var \Elgg\Router
  10. */
  11. protected $router;
  12. /**
  13. * @var string
  14. */
  15. protected $pages;
  16. /**
  17. * @var int
  18. */
  19. protected $fooHandlerCalls = 0;
  20. function setUp() {
  21. $this->hooks = new \Elgg\PluginHooksService();
  22. $this->router = new \Elgg\Router($this->hooks);
  23. $this->pages = dirname(dirname(__FILE__)) . '/test_files/pages';
  24. $this->fooHandlerCalls = 0;
  25. }
  26. function hello_page_handler($segments, $identifier) {
  27. include "{$this->pages}/hello.php";
  28. return true;
  29. }
  30. function testCanRegisterFunctionsAsPageHandlers() {
  31. $registered = $this->router->registerPageHandler('hello', array($this, 'hello_page_handler'));
  32. $this->assertTrue($registered);
  33. $path = "hello/1/\xE2\x82\xAC"; // euro sign
  34. $qs = http_build_query(array('__elgg_uri' => $path));
  35. $request = \Elgg\Http\Request::create("http://localhost/?$qs");
  36. ob_start();
  37. $handled = $this->router->route($request);
  38. $output = ob_get_clean();
  39. $this->assertTrue($handled);
  40. $this->assertEquals($path, $output);
  41. $this->assertEquals(array(
  42. 'hello' => array($this, 'hello_page_handler')
  43. ), $this->router->getPageHandlers());
  44. }
  45. function testFailToRegisterInvalidCallback() {
  46. $registered = $this->router->registerPageHandler('hello', new \stdClass());
  47. $this->assertFalse($registered);
  48. }
  49. function testCanUnregisterPageHandlers() {
  50. $this->router->registerPageHandler('hello', array($this, 'hello_page_handler'));
  51. $this->router->unregisterPageHandler('hello');
  52. $request = \Elgg\Http\Request::create('http://localhost/hello/');
  53. ob_start();
  54. $handled = $this->router->route($request);
  55. $output = ob_get_clean();
  56. // Normally we would expect the router to return false for this request,
  57. // but since it checks for headers_sent() and PHPUnit issues output before
  58. // this test runs, the headers have already been sent. It's enough to verify
  59. // that the output we buffered is empty.
  60. // $this->assertFalse($handled);
  61. $this->assertEmpty($output);
  62. }
  63. /**
  64. * 1. Register a page handler for `/foo`
  65. * 2. Register a plugin hook that uses the "handler" result param
  66. * to route all `/bar/*` requests to the `/foo` handler.
  67. * 3. Route a request for a `/bar` page.
  68. * 4. Check that the `/foo` handler was called.
  69. */
  70. function testRouteSupportsSettingHandlerInHookResultForBackwardsCompatibility() {
  71. $this->router->registerPageHandler('foo', array($this, 'foo_page_handler'));
  72. $this->hooks->registerHandler('route', 'bar', array($this, 'bar_route_handler'));
  73. $query = http_build_query(array('__elgg_uri' => 'bar/baz'));
  74. ob_start();
  75. $this->router->route(\Elgg\Http\Request::create("http://localhost/?$query"));
  76. ob_end_clean();
  77. $this->assertEquals(1, $this->fooHandlerCalls);
  78. }
  79. /**
  80. * 1. Register a page handler for `/foo`
  81. * 2. Register a plugin hook that uses the "handler" result param
  82. * to route all `/bar/*` requests to the `/foo` handler.
  83. * 3. Route a request for a `/bar` page.
  84. * 4. Check that the `/foo` handler was called.
  85. */
  86. function testRouteSupportsSettingIdentifierInHookResultForBackwardsCompatibility() {
  87. $this->router->registerPageHandler('foo', array($this, 'foo_page_handler'));
  88. $this->hooks->registerHandler('route', 'bar', array($this, 'bar_route_identifier'));
  89. $query = http_build_query(array('__elgg_uri' => 'bar/baz'));
  90. ob_start();
  91. $this->router->route(\Elgg\Http\Request::create("http://localhost/?$query"));
  92. ob_end_clean();
  93. $this->assertEquals(1, $this->fooHandlerCalls);
  94. }
  95. function testRouteOverridenFromHook() {
  96. $this->router->registerPageHandler('foo', array($this, 'foo_page_handler'));
  97. $this->hooks->registerHandler('route', 'foo', array($this, 'bar_route_override'));
  98. $query = http_build_query(array('__elgg_uri' => 'foo'));
  99. ob_start();
  100. $this->router->route(\Elgg\Http\Request::create("http://localhost/?$query"));
  101. $result = ob_get_contents();
  102. ob_end_clean();
  103. $this->assertEquals("Page handler override from hook", $result);
  104. $this->assertEquals(0, $this->fooHandlerCalls);
  105. }
  106. function foo_page_handler() {
  107. $this->fooHandlerCalls++;
  108. return true;
  109. }
  110. function bar_route_handler($hook, $type, $value, $params) {
  111. $value['handler'] = 'foo';
  112. return $value;
  113. }
  114. function bar_route_identifier($hook, $type, $value, $params) {
  115. $value['identifier'] = 'foo';
  116. return $value;
  117. }
  118. function bar_route_override($hook, $type, $value, $params) {
  119. echo "Page handler override from hook";
  120. return false;
  121. }
  122. }