Skip to content

Commit

Permalink
PRG Plugin fixes: Incorrect use of session hops expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
cgmartin committed Nov 19, 2012
1 parent 7af2761 commit 44cfeae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions library/Zend/Mvc/Controller/Plugin/FilePostRedirectGet.php
Expand Up @@ -46,6 +46,7 @@ public function __invoke($form, $redirect = null, $redirectToUrl = false)
if (!$form->isValid()) {
$container->errors = $form->getMessages();
}
$container->setExpirationHops(1, array('post', 'errors'));

return $this->redirect($redirect, $redirectToUrl);
} else {
Expand All @@ -67,11 +68,10 @@ public function __invoke($form, $redirect = null, $redirectToUrl = false)
}
}

protected function getSessionContainer()
public function getSessionContainer()
{
if (!isset($this->sessionContainer)) {
$this->sessionContainer = new Container('file_prg_post1');
$this->sessionContainer->setExpirationHops(1, array('post', 'errors'));
}
return $this->sessionContainer;
}
Expand Down
4 changes: 2 additions & 2 deletions library/Zend/Mvc/Controller/Plugin/PostRedirectGet.php
Expand Up @@ -36,6 +36,7 @@ public function __invoke($redirect = null, $redirectToUrl = false)

if ($request->isPost()) {
$container->post = $request->getPost()->toArray();
$container->setExpirationHops(1, 'post');
return $this->redirect($redirect, $redirectToUrl);
} else {
if ($container->post !== null) {
Expand All @@ -48,11 +49,10 @@ public function __invoke($redirect = null, $redirectToUrl = false)
}
}

protected function getSessionContainer()
public function getSessionContainer()
{
if (!isset($this->sessionContainer)) {
$this->sessionContainer = new Container('prg_post1');
$this->sessionContainer->setExpirationHops(1, 'post');
}
return $this->sessionContainer;
}
Expand Down
Expand Up @@ -194,6 +194,13 @@ public function testReturnsPostOnRedirectGet()

$this->assertEquals($params, $prgResult);
$this->assertEquals($params['postval1'], $this->form->get('postval1')->getValue());

// Do GET again to make sure data is empty
$this->request = new Request();
$this->controller->dispatch($this->request, $this->response);
$prgResult = $this->controller->fileprg($this->form, '/test/getPage', true);

$this->assertFalse($prgResult);
}

public function testAppliesFormErrorsOnPostRedirectGet()
Expand Down
31 changes: 31 additions & 0 deletions tests/ZendTest/Mvc/Controller/Plugin/PostRedirectGetTest.php
Expand Up @@ -108,6 +108,37 @@ public function testRedirectsToRouteOnPost()
$this->assertEquals(303, $prgResultRoute->getStatusCode());
}

public function testReturnsPostOnRedirectGet()
{
$params = array(
'postval1' => 'value1'
);
$this->request->setMethod('POST');
$this->request->setPost(new Parameters($params));

$result = $this->controller->dispatch($this->request, $this->response);
$prgResultRoute = $this->controller->prg('home');

$this->assertInstanceOf('Zend\Http\Response', $prgResultRoute);
$this->assertTrue($prgResultRoute->getHeaders()->has('Location'));
$this->assertEquals('/', $prgResultRoute->getHeaders()->get('Location')->getUri());
$this->assertEquals(303, $prgResultRoute->getStatusCode());

// Do GET
$this->request = new Request();
$this->controller->dispatch($this->request, $this->response);
$prgResult = $this->controller->prg('home');

$this->assertEquals($params, $prgResult);

// Do GET again to make sure data is empty
$this->request = new Request();
$this->controller->dispatch($this->request, $this->response);
$prgResult = $this->controller->prg('home');

$this->assertFalse($prgResult);
}

/**
* @expectedException Zend\Mvc\Exception\RuntimeException
*/
Expand Down

0 comments on commit 44cfeae

Please sign in to comment.