UrlHelperTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\Core\Plugin;
  4. use Cake\Http\ServerRequest;
  5. use Cake\Routing\RouteBuilder;
  6. use Cake\Routing\Router;
  7. use Cake\View\View;
  8. use Tools\TestSuite\TestCase;
  9. use Tools\View\Helper\UrlHelper;
  10. /**
  11. * Datetime Test Case
  12. */
  13. class UrlHelperTest extends TestCase {
  14. /**
  15. * @var \Tools\View\Helper\UrlHelper
  16. */
  17. protected $Url;
  18. /**
  19. * @return void
  20. */
  21. public function setUp() {
  22. parent::setUp();
  23. $this->Url = new UrlHelper(new View(null));
  24. $this->Url->request = new ServerRequest();
  25. $this->Url->request->webroot = '';
  26. }
  27. /**
  28. * @return void
  29. */
  30. public function testBuildReset() {
  31. Router::connect('/:controller/:action/*');
  32. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  33. $expected = '/foobar/test';
  34. $this->assertSame($expected, $result);
  35. $this->Url->request->here = '/admin/foobar/test';
  36. $this->Url->request->params['prefix'] = 'admin';
  37. Router::reload();
  38. Router::connect('/:controller/:action/*');
  39. Router::prefix('admin', function (RouteBuilder $routes) {
  40. $routes->fallbacks();
  41. });
  42. Router::pushRequest($this->Url->request);
  43. $result = $this->Url->build(['prefix' => 'admin', 'controller' => 'foobar', 'action' => 'test']);
  44. $expected = '/admin/foobar/test';
  45. $this->assertSame($expected, $result);
  46. $result = $this->Url->build(['controller' => 'foobar', 'action' => 'test']);
  47. $expected = '/admin/foobar/test';
  48. $this->assertSame($expected, $result);
  49. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  50. $expected = '/foobar/test';
  51. $this->assertSame($expected, $result);
  52. }
  53. /**
  54. * @return void
  55. */
  56. public function testBuildResetWithPlugin() {
  57. Router::connect('/:controller/:action/*');
  58. $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']);
  59. $expected = '/foobar/test';
  60. $this->assertSame($expected, $result);
  61. $this->Url->request->here = '/admin/foo/bar/baz/test';
  62. $this->Url->request->params['prefix'] = 'admin';
  63. $this->Url->request->params['plugin'] = 'Foo';
  64. Router::reload();
  65. Router::connect('/:controller/:action/*');
  66. Router::plugin('Foo', function (RouteBuilder $routes) {
  67. $routes->fallbacks();
  68. });
  69. Router::prefix('admin', function (RouteBuilder $routes) {
  70. $routes->plugin('Foo', function (RouteBuilder $routes) {
  71. $routes->fallbacks();
  72. });
  73. });
  74. Plugin::routes();
  75. Router::pushRequest($this->Url->request);
  76. $result = $this->Url->build(['controller' => 'bar', 'action' => 'baz', 'x']);
  77. $expected = '/admin/foo/bar/baz/x';
  78. $this->assertSame($expected, $result);
  79. $result = $this->Url->buildReset(['controller' => 'bar', 'action' => 'baz', 'x']);
  80. $expected = '/bar/baz/x';
  81. $this->assertSame($expected, $result);
  82. }
  83. /**
  84. * @return void
  85. */
  86. public function testBuildComplete() {
  87. $this->Url->request->query['x'] = 'y';
  88. $result = $this->Url->buildComplete(['action' => 'test']);
  89. $expected = '/test?x=y';
  90. $this->assertSame($expected, $result);
  91. $result = $this->Url->buildComplete(['action' => 'test', '?' => ['a' => 'b']]);
  92. $expected = '/test?a=b&amp;x=y';
  93. $this->assertSame($expected, $result);
  94. }
  95. /**
  96. * @return void
  97. */
  98. public function tearDown() {
  99. parent::tearDown();
  100. unset($this->Url);
  101. }
  102. }