A few days ago in response to an email newsletter I sent out one individual responded back asking about testing Terms and Conditions in Magento. When building out Magium, and its many components, I focus first on the things that I find important. If I haven’t used a feature then it won’t be very high on my list of things to build out. But if someone tells me “hey, I’d really like to be able to test this feature” then it jumps in the priority queue.
With that in mind I was asked about testing Terms and Conditions.
There are a few things to note about TnC testing. First, it is not as prevalent as something like Checkout and so I haven’t put as many bells and whistles into it. Secondly, well… no that’s about it.
Enabling Terms and Conditions
If you do not have terms and conditions enabled in the System Configuration you can do so with the following code.
$this->getAction(Login::ACTION)->login(); $this->getNavigator(AdminMenu::NAVIGATOR)->navigateTo('System/Configuration'); $modifier = $this->getAction(SettingModifier::ACTION); /* @var $modifier \Magium\Magento\Actions\Admin\Configuration\SettingModifier */ $modifier->set( 'Checkout/Checkout Options::label=Enable Terms and Conditions', SettingModifier::SETTING_OPTION_YES, true ); // True saves it
This is not part of the terms and conditions functionality. It is the setting modifier functionality. But it might be something you need to script. For example, if you are building a module that integrates with checkout.
Clearing Terms and Conditions
If your test creates new TnC then you may need to clear them in the admin UI. To do that, execute the following code in your test.
Creating New Terms and Conditions
Creating a new Condition for your site involves using the
TermsAndConditions admin action. First you will need to navigate to the Terms and Conditions page. Then you will need to set your terms and conditions, followed by calling the
execute() method. When that method is called the terms and conditions will be saved.
$this->getNavigator(AdminMenu::NAVIGATOR)->navigateTo('Sales/Terms and conditions'); $terms = $this->getAction(TermsAndConditions::ACTION); /* @var $terms TermsAndConditions */ $terms->setName('test'); $terms->setCheckboxText('Terms'); $terms->setStatus('Enabled'); $terms->setStoreView('All Store Views'); $terms->setContent('Conditions'); $terms->execute();
Checking out with Terms and Conditions
Checking out and selecting that you agree with the terms and conditions is fairly simple. First you will retrieve an instance of the
TermsAndConditions checkout step action. You will then need to specify the name of the terms that you will be accepting. This corresponds to the value passed to
setCheckboxText() in the previous example. After that you will need to configure the checkout to inject the the action, though you can have the step instance configure the checkout for you (recommended).
$guestCheckout = $this->getAction(GuestCheckout::ACTION); /* @var $guestCheckout GuestCheckout */ $terms = $this->getAction(\Magium\Magento\Actions\Checkout\Steps\TermsAndConditions::ACTION); /* @var $terms \Magium\Magento\Actions\Checkout\Steps\TermsAndConditions */ // Configuring the checkout $terms->setCheckboxText('Terms'); $terms->configureCheckout($guestCheckout); $guestCheckout->execute();