View source
<?php
namespace Drupal\Tests\uc_country\Functional;
use Drupal\Tests\BrowserTestBase;
class CountryTest extends BrowserTestBase {
protected static $modules = [
'uc_country',
'uc_store',
];
public function testCountryUi() {
$assert = $this
->assertSession();
$this
->drupalLogin($this
->drupalCreateUser([
'administer countries',
'administer store',
]));
$countries = \Drupal::service('country_manager')
->getAvailableList();
$country_ids = array_rand($countries, 8);
$last_country = array_pop($country_ids);
foreach ($country_ids as $country_id) {
$this
->drupalGet('admin/store/config/country');
$assert
->linkByHrefExists('admin/store/config/country/' . $country_id . '/enable', 0, format_string('%country is not enabled by default.', [
'%country' => $countries[$country_id],
]));
$this
->clickLinkInRow($countries[$country_id], 'Enable');
$assert
->pageTextContains('The country ' . $countries[$country_id] . ' has been enabled.');
$assert
->linkByHrefExists('admin/store/config/country/' . $country_id . '/disable', 0, format_string('%country is now enabled.', [
'%country' => $countries[$country_id],
]));
}
$this
->drupalGet('admin/store/config/store');
$assert
->optionNotExists('edit-address-country', $last_country);
$this
->drupalGet('admin/store/config/country');
$this
->clickLinkInRow($countries[$last_country], 'Enable');
$assert
->pageTextContains('The country ' . $countries[$last_country] . ' has been enabled.');
$assert
->linkByHrefExists('admin/store/config/country/' . $last_country . '/disable', 0, format_string('%country is now enabled.', [
'%country' => $countries[$last_country],
]));
$this
->drupalGet('admin/store/config/store');
$assert
->optionExists('edit-address-country', $last_country);
$this
->drupalGet('admin/store/config/country');
$this
->clickLink('Disable', 7);
$assert
->pageTextContains('The country ' . $countries[$last_country] . ' has been disabled.');
$assert
->linkByHrefExists('admin/store/config/country/' . $last_country . '/enable', 0, format_string('%country is now disabled.', [
'%country' => $countries[$last_country],
]));
}
public function testAllDisabled() {
$assert = $this
->assertSession();
$this
->drupalLogin($this
->drupalCreateUser([
'administer countries',
'administer store',
'access administration pages',
]));
$manager = \Drupal::service('country_manager');
$countries = $manager
->getEnabledList();
foreach (array_keys($countries) as $code) {
$manager
->getCountry($code)
->disable()
->save();
}
$this
->drupalGet('admin/store');
$assert
->pageTextContains('No countries are enabled.');
$this
->drupalGet('admin/store/config/store');
$assert
->pageTextNotContains('State/Province');
$assert
->pageTextNotContains('Country');
}
protected function clickLinkInRow($label, $link) {
return $this
->clickLinkHelper($label, 0, '//td[normalize-space()=:label]/ancestor::tr[1]//a[normalize-space()="' . $link . '"]');
}
protected function clickLinkHelper($label, $index, $pattern) {
$label = (string) $label;
$url_before = $this
->getUrl();
$urls = $this
->xpath($pattern, [
':label' => $label,
]);
if (isset($urls[$index])) {
$url_target = $this
->getAbsoluteUrl($urls[$index]
->getAttribute('href'));
$this
->assertTrue(TRUE, format_string('Clicked link %label (@url_target) from @url_before', [
'%label' => $label,
'@url_target' => $url_target,
'@url_before' => $url_before,
]), 'Browser');
return $this
->drupalGet($url_target);
}
$this
->fail(format_string('Link %label does not exist on @url_before', [
'%label' => $label,
'@url_before' => $url_before,
]), 'Browser');
return FALSE;
}
}