Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Customer attribute example
Let’s assume that new module (app/code/community/NewCustomerAttributeModule) adds new select attribute to customer backend form which is displayed on ‘My Dashboard’ in customer account on Frontend.
We are going to test this attribute while creating customer on Backend.
The following needs to be done:
1) As you added a new namespace, file dev/tests/functional/composer.json should be updated to use it:
"psr-4": {
"Magento\\": ["lib/Magento/", "vendor/magento/mtf/Magento/", "testsuites/Magento/"],
"Mage\\": ["generated/Mage/", "tests/app/Mage/"],
"Enterprise\\": ["generated/Community/", "tests/app/Community/"],
"Community\\": ["generated/Community/", "tests/app/Community/"],
"Test\\": "generated/Test/"
}
2) Remove composer.lock file and run command ‘composer install’.
3) Create new folder in dev/tests/functional/Community/NewCustomerAttributeModule.
4) Create folder ‘Test’.
5) Create folder ‘TestCase’.
6) Add CreateCustomerFromBackendTest.xml file which will extend existing customer creation, content could be something like this:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/mtf/etc/variations.xsd">
<testCase name="Mage\Customer\Test\TestCase\CreateCustomerFromBackendTest">
<variation name="Create customer from backend with new attribute">
<data name="customer/data/website_id" xsi:type="string">Main Website</data>
<data name="customer/data/group_id/dataSet" xsi:type="string">General</data>
<data name="customer/data/firstname" xsi:type="string">John%isolation%</data>
<data name="customer/data/lastname" xsi:type="string">Doe%isolation%</data>
<data name="customer/data/email" xsi:type="string">JohnDoe%isolation%@example.com</data>
<data name="customer/data/password" xsi:type="string">123123q</data>
<data name="customer/data/new_attribute" xsi:type="string">123123q</data>
<constraint name="Mage\Customer\Test\Constraint\AssertCustomerSuccessSaveMessage" />
<constraint name="Mage\Customer\Test\Constraint\AssertNewCustomerAttributeOnFrontend" />
</variation>
</testCase>
</config>
7) As we added new attribute customer fixture should be modified as well:
a. create folder dev/tests/functional/Community/NewCustomerAttributeModule/Test/Fixture
b. create file Customer.xml with content
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="../../../../../../vendor/magento/mtf/etc/fixture.xsd">
<fixture name="address">
<field name="house" is_required="1" group="addresses">
<default_value xsi:type="string" />
</field>
</fixture>
</config>
c. Run dev/tests/functional/utils/generate.php to generate updated customer fixture.
8) Now let’s tell Magento how we are going to fill this field on Backend. Create folder dev/tests/functional/Community/NewCustomerAttributeModule/Test/Block.
9) We are going to extend \Mage\Adminhtml\Test\Block\Customer\Edit\CustomerForm class, but all what we need is to extend field declarations dev/tests/functional/tests/app/Mage/Adminhtml/Test/Block/Customer/Edit/CustomerForm.xml. So create file CustomerForm.xml in dev/tests/functional/Community/NewCustomerAttributeModule/Test/Block/Customer/Edit folder (this two files will be merged while filling the form)
<tabs>
<account_information>
<class>\Mage\Adminhtml\Test\Block\Widget\Tab</class>
<selector>#customer_info_tabs_account</selector>
<strategy>css selector</strategy>
<wrapper>account</wrapper>
<fields>
<new_attribute>
<input>select</input>
</new_attribute>
</fields>
</account_information>
</tabs>
10) That’s all what we need to fill the attribute. Now let’s verify it.
11) Create folder Constraint in dev/tests/functional/Community/NewCustomerAttributeModule/Test.
12) Add new assert class AssertNewCustomerAttributeOnFrontend and write there how you are going to verify this attribute.
13) Implement assertion and run test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.