/** * @return void */ public function testBuildResetWithPlugin() { Router::connect('/:controller/:action/*'); $result = $this->Url->buildReset(['controller' => 'foobar', 'action' => 'test']); $expected = '/foobar/test'; $this->assertSame($expected, $result); $this->Url->request->here = '/admin/foo/bar/baz/test'; $this->Url->request->params['prefix'] = 'admin'; $this->Url->request->params['plugin'] = 'Foo'; Router::reload(); Router::connect('/:controller/:action/*'); Router::plugin('Foo', function ($routes) { $routes->fallbacks(); }); Router::prefix('admin', function ($routes) { $routes->plugin('Foo', function ($routes) { $routes->fallbacks(); }); }); Plugin::routes(); Router::pushRequest($this->Url->request); $result = $this->Url->build(['controller' => 'bar', 'action' => 'baz', 'x']); $expected = '/admin/foo/bar/baz/x'; $this->assertSame($expected, $result); $result = $this->Url->buildReset(['controller' => 'bar', 'action' => 'baz', 'x']); $expected = '/bar/baz/x'; $this->assertSame($expected, $result); }
/** * @return void */ public function setUp() { parent::setUp(); Configure::write('Captcha', ['debug' => false]); $this->request = new Request(); $this->request->env('REMOTE_ADDR', '127.0.0.1'); $this->session = $this->getMockBuilder(Session::class)->setMethods(['id'])->getMock(); $this->session->expects($this->once())->method('id')->willReturn(1); $this->request->session($this->session); Router::pushRequest($this->request); Router::reload(); $this->Captchas = TableRegistry::get('Captcha.Captchas'); $this->Comments = TableRegistry::get('Captcha.Comments'); $this->Comments->addBehavior('Captcha.Captcha'); }
public function dispatch(Request $request, Response $response) { Router::pushRequest($request); $beforeEvent = $this->dispatchEvent('Dispatcher.beforeDispatch', compact('request', 'response')); $request = $beforeEvent->data['request']; if ($beforeEvent->result instanceof Response) { return $beforeEvent->result; } $controller = $this->factory->create($request, $response); $response = $this->_invoke($controller); if (isset($request->params['return'])) { return $response; } $afterEvent = $this->dispatchEvent('Dispatcher.afterDispatch', compact('request', 'response')); return $afterEvent->data['response']; }
/** * Dispatches a Request & Response * * @param \Cake\Network\Request $request The request to dispatch. * @param \Cake\Network\Response $response The response to dispatch. * @return \Cake\Network\Response a modified/replaced response. */ public function dispatch(Request $request, Response $response) { if (Router::getRequest(true) !== $request) { Router::pushRequest($request); } $beforeEvent = $this->dispatchEvent('Dispatcher.beforeDispatch', compact('request', 'response')); $request = $beforeEvent->data['request']; if ($beforeEvent->result instanceof Response) { return $beforeEvent->result; } // Use the controller built by an beforeDispatch // event handler if there is one. if (isset($beforeEvent->data['controller'])) { $controller = $beforeEvent->data['controller']; } else { $controller = $this->factory->create($request, $response); } $response = $this->_invoke($controller); if (isset($request->params['return'])) { return $response; } $afterEvent = $this->dispatchEvent('Dispatcher.afterDispatch', compact('request', 'response')); return $afterEvent->data['response']; }
/** * Default to loginRedirect, if set, on authError. * * @return void * @triggers Controller.startup $Controller */ public function testDefaultToLoginRedirect() { $url = '/party/on'; $this->Auth->request = $request = new Request($url); $request->env('HTTP_REFERER', false); $request->addParams(Router::parse($url)); $request->addPaths(['base' => 'dirname', 'webroot' => '/dirname/']); Router::pushRequest($request); $this->Auth->config('authorize', ['Controller']); $this->Auth->setUser(['username' => 'mariano', 'password' => 'cake']); $this->Auth->config('loginRedirect', ['controller' => 'something', 'action' => 'else']); $response = new Response(); $Controller = $this->getMock('Cake\\Controller\\Controller', ['on', 'redirect'], [$request, $response]); $event = new Event('Controller.startup', $Controller); // Should not contain basedir when redirect is called. $expected = '/something/else'; $Controller->expects($this->once())->method('redirect')->with($this->equalTo($expected)); $this->Auth->startup($event); }
/** * Test that the referer is not absolute if it is '/'. * * This avoids the base path being applied twice on string urls. * * @return void */ public function testRefererSlash() { $request = $this->getMockBuilder('Cake\\Network\\Request')->setMethods(['referer'])->getMock(); $request->base = '/base'; Router::pushRequest($request); $request->expects($this->any())->method('referer')->will($this->returnValue('/')); $controller = new Controller($request); $result = $controller->referer('/', true); $this->assertEquals('/', $result); $controller = new Controller($request); $result = $controller->referer('/some/path', true); $this->assertEquals('/base/some/path', $result); }
/** * test get request. * * @return void */ public function testGetRequest() { $requestA = new Request('/'); $requestB = new Request('/posts'); Router::pushRequest($requestA); Router::pushRequest($requestB); $this->assertSame($requestA, Router::getRequest(false)); $this->assertSame($requestB, Router::getRequest(true)); }
/** * Test that image() works with fullBase and a webroot not equal to / * * @return void */ public function testImageWithFullBase() { $result = $this->Html->image('test.gif', array('fullBase' => true)); $here = $this->Html->url('/', true); $this->assertTags($result, array('img' => array('src' => $here . 'img/test.gif', 'alt' => ''))); $result = $this->Html->image('sub/test.gif', array('fullBase' => true)); $here = $this->Html->url('/', true); $this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => ''))); $request = $this->Html->request; $request->webroot = '/myproject/'; $request->base = '/myproject'; Router::pushRequest($request); $result = $this->Html->image('sub/test.gif', array('fullBase' => true)); $here = $this->Html->url('/', true); $this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => ''))); }
/** * Test that image() works with fullBase and a webroot not equal to / * * @return void */ public function testImageWithFullBase() { $result = $this->Html->image('test.gif', ['fullBase' => true]); $here = $this->Html->Url->build('/', true); $expected = ['img' => ['src' => $here . 'img/test.gif', 'alt' => '']]; $this->assertHtml($expected, $result); $result = $this->Html->image('sub/test.gif', ['fullBase' => true]); $here = $this->Html->Url->build('/', true); $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']]; $this->assertHtml($expected, $result); $request = $this->Html->request; $request->webroot = '/myproject/'; $request->base = '/myproject'; Router::pushRequest($request); $result = $this->Html->image('sub/test.gif', ['fullBase' => true]); $here = $this->Html->Url->build('/', true); $expected = ['img' => ['src' => $here . 'img/sub/test.gif', 'alt' => '']]; $this->assertHtml($expected, $result); }