AddressElementTest.php in Ubercart 8.4
File
uc_store/tests/src/FunctionalJavascript/AddressElementTest.php
View source
<?php
namespace Drupal\Tests\uc_store\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\Tests\uc_store\Traits\AddressTestTrait;
use Drupal\uc_country\Entity\Country;
class AddressElementTest extends JavascriptTestBase {
use AddressTestTrait;
protected static $modules = [
'uc_store',
];
protected $adminUser;
protected static $adminPermissions = [
'access administration pages',
'administer store',
];
protected $testAddresses = [];
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(static::$adminPermissions);
$this
->drupalLogin($this->adminUser);
$countries = \Drupal::service('country_manager')
->getAvailableList();
$country_ids = array_rand($countries, 20);
foreach ($country_ids as $country_id) {
Country::load($country_id)
->enable()
->save();
}
$store_settings = \Drupal::configFactory()
->getEditable('uc_store.settings');
$store_settings
->set('address.country', $country_id)
->save();
$this->testAddresses[] = $this
->createAddress();
}
public function testZoneAjax() {
$address = clone $this->testAddresses[0];
$this
->drupalGet('admin/store/config/store');
$page = $this
->getSession()
->getPage();
$assert = $this
->assertSession();
$page
->clickLink('Store address');
$assert
->assertWaitOnAjaxRequest();
$page
->findField('address[country]')
->selectOption($address
->getCountry());
$assert
->assertWaitOnAjaxRequest();
$field = $page
->findField('address[country]');
$this
->assertNotEmpty($field);
$this
->assertEquals($field
->getValue(), $address
->getCountry());
if ($page
->findField('address[zone]')) {
$page
->findField('address[zone]')
->selectOption($address
->getZone());
$assert
->assertWaitOnAjaxRequest();
$field = $page
->findField('address[zone]');
$this
->assertNotEmpty($field);
$this
->assertEquals($field
->getValue(), $address
->getZone());
}
else {
$zone_list = \Drupal::service('country_manager')
->getZoneList($address
->getCountry());
$assert
->assertEmpty($zone_list, 'Zone field not shown and there are no zones.');
$this
->assertFalse($page
->findField('address[zone]'));
}
$edit = [
'address[street1]' => $address
->getStreet1(),
'address[street2]' => $address
->getStreet2(),
'address[city]' => $address
->getCity(),
'address[postal_code]' => $address
->getPostalCode(),
];
$this
->submitForm($edit, 'Save configuration');
$page
->clickLink('Store address');
$assert
->assertWaitOnAjaxRequest();
$store_settings = \Drupal::configFactory()
->get('uc_store.settings');
$this
->assertEquals($store_settings
->get('address.street1'), $address
->getStreet1());
$this
->assertEquals($store_settings
->get('address.street2'), $address
->getStreet2());
$this
->assertEquals($store_settings
->get('address.zone'), $address
->getZone());
$this
->assertEquals($store_settings
->get('address.country'), $address
->getCountry());
$this
->assertEquals($store_settings
->get('address.postal_code'), $address
->getPostalCode());
$this
->assertEquals($store_settings
->get('address.city'), $address
->getCity());
}
}