UrlComponentTest.php 2.9 KB

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