How to avoid redirection to “enable-cookies” page during controller unit test?
-
16-10-2019 - |
문제
I'm doing a very simple controller test using Ecomdev_PHPUnit
:
class Foo_BarWishlist_Test_Controller_IndexController
extends EcomDev_PHPUnit_Test_Case_Controller
{
/**
* @test
* @loadFixture ~Foo_BarWishlist/default
*/
public function fromcartAction()
{
$this->dispatch('checkout/cart/add', array('product' => 10));
$this->assertRequestDispatched();
$body = self::getResponse()->getOutputBody();
$this->assertResponseBodyContains('success-msg');
}
...
This test fails because $body
is empty. When checking self::getResponse()
more closely, I see that a redirect to the enable-cookies
CMS page was sent.
How can I take care of this? Do I have to set the check-cookie manually? Do I have to open another page before (I tried already a $this->dispatch('/')
before the actual dispatch)? Or is there a general cookie issue during the controller tests?
해결책
There's a setting for it in the admin panel:
Web > (Browser Capabilities Detection) Redirect to CMS-page if Cookies are Disabled
This setting can also be set during the tests using a config fixture.
다른 팁
I believe this stack overflow answer will help:
https://stackoverflow.com/questions/14612349/cant-get-session-singleton-in-ecomdev-phpunit-test
The basic idea is to mock the session so then your "add to cart" action is successful instead of redirecting to the "cookies disabled" page.
$sessionMock = $this->getModelMockBuilder('admin/session')
// This one removes `session_start` and other methods
->disableOriginalConstructor()
// Enables original methods usage, because by default it overrides all methods
->setMethods(null)
->getMock();
$this->replaceByMock('singleton', 'admin/session', $sessionMock);