UrlHelperTest.php 3.1 KB

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