6

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?

3

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 am just afraid, that the session is not properly initiated, so even if I disable this setting, something else might not work? – Alex Feb 12 '13 at 10:25
  • You can add the session id in the URL trough Web > Session Validation > Use SID on Frontend – Sander Mangel Mar 14 '13 at 11:03
  • @Alex did you find that there were any issues with disabling the Redirect? Did you need to add the SID to the URL like sander suggested? – Jonathan Day Jul 17 '13 at 5:39
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.