Testing Magento’s Contact Form

It’s one of those really stupid things. Testing the “Contact Us” form. When was the last time you tested the contact us form?

I’ll tell you.

Never.

Or at least as close to “never” as matters.

But it’s kind of something that is important to know if it’s working. With that in mind, here is some quick code that allows you to easily test your contact form.

Basic Usage

<?php

use Magium\Magento\AbstractMagentoTestCase;
use Magium\Magento\Actions\Misc\SubmitContactForm;

class SendContactFormTest extends AbstractMagentoTestCase
{

    public function testContactFormWorks()
    {
        $this->commandOpen($this->getTheme()->getBaseUrl());
        $this->byText('{{Contact Us}}')->click();
        $contactForm = $this->getAction(SubmitContactForm::ACTION);
        $contactForm->setComment('This is a comment');
        $contactForm->execute();
    }

}

Integrating With GMail

But how do you KNOW that it’s working? Well, you could also test the email system. Say that you use GMail for your testing. You can use the magium/gmail component to make sure that it works. Append this code onto your test (making sure to configure it properly)

$this->getAction(Login::ACTION)->login();
$this->getNavigator(SubjectEquals::NAVIGATOR)->navigateTo('Contact Form');

Customizing the Sender

The SubmitContactForm object has a dependency on the Customer identity. So if you want to change the sender’s email, name, or phone number you can easily do so prior to calling the action.

class SendContactFormTest extends AbstractMagentoTestCase
{

public function testContactFormWorks()
{
      $this->getIdentity()->setFirstName('Bob');
      $this->getIdentity()->setLastName('Smith');

    $this->commandOpen($this->getTheme()->getBaseUrl());
    $this->byText('{{Contact Us}}')->click();
    $contactForm = $this->getAction(SubmitContactForm::ACTION);
    $contactForm->setComment('This is a comment');
    $contactForm->execute();
}

}