View source
<?php
namespace Drupal\Tests\address\Functional\Views;
use Drupal\Tests\BrowserTestBase;
use Drupal\views\Views;
class AdministrativeAreaFilterTest extends BrowserTestBase {
protected static $modules = [
'node',
'user',
'views',
'address',
'address_test',
];
protected $defaultTheme = 'stark';
protected $user;
protected function setUp() : void {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'access content',
]);
$this
->drupalLogin($this->user);
}
public function testStaticCountryAdministrativeAreaOptions() {
$view = Views::getView('address_test_filter_administrative_area');
$filters = $view
->getDisplay()
->getOption('filters');
$filters['field_address_test_administrative_area']['country']['country_source'] = 'static';
$filters['field_address_test_administrative_area']['country']['country_static_code'] = 'BR';
$view
->getDisplay()
->overrideOption('filters', $filters);
$view
->save();
$this
->drupalGet('address-test/views/filter-administrative-area');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('field_address_test_administrative_area');
$this
->assertAdministrativeAreaOptions('BR');
}
public function testContextualCountryFilterAdministrativeAreaOptions() {
$view = Views::getView('address_test_filter_administrative_area');
$filters = $view
->getDisplay()
->getOption('filters');
$filters['field_address_test_administrative_area']['country']['country_source'] = 'argument';
$filters['field_address_test_administrative_area']['country']['country_argument_id'] = 'field_address_test_country_code';
$view
->getDisplay()
->overrideOption('filters', $filters);
$view
->save();
$this
->drupalGet('address-test/views/filter-administrative-area');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldNotExists('field_address_test_administrative_area');
$this
->drupalGet('address-test/views/filter-administrative-area/CR');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldNotExists('field_address_test_administrative_area');
foreach ([
'BR',
'EG',
'MX',
'US',
] as $country) {
$this
->drupalGet("address-test/views/filter-administrative-area/{$country}");
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('field_address_test_administrative_area');
$this
->assertAdministrativeAreaOptions($country);
}
}
public function testExposedCountryFilterAdministrativeAreaOptions() {
$view = Views::getView('address_test_filter_administrative_area');
$filters = $view
->getDisplay()
->getOption('filters');
$filters['field_address_test_administrative_area']['country']['country_source'] = 'filter';
$filters['field_address_test_administrative_area']['country']['country_filter_id'] = 'field_address_test_country_code';
$view
->getDisplay()
->overrideOption('filters', $filters);
$view
->save();
$this
->drupalGet('address-test/views/filter-administrative-area');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldNotExists('field_address_test_administrative_area');
$options = [
'query' => [
'field_address_test_country_code' => 'CR',
],
];
$this
->drupalGet('address-test/views/filter-administrative-area', $options);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldNotExists('field_address_test_administrative_area');
foreach ([
'BR',
'EG',
'MX',
'US',
] as $country) {
$options = [
'query' => [
'field_address_test_country_code' => $country,
],
];
$this
->drupalGet('address-test/views/filter-administrative-area', $options);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->fieldExists('field_address_test_administrative_area');
$this
->assertAdministrativeAreaOptions($country);
}
}
public function testDynamicAdministrativeAreaLabels() {
$static_label = 'Administrative area (static label)';
$dynamic_labels = [
'AE' => 'Emirate',
'BR' => 'State',
'CA' => 'Province',
'US' => 'State',
];
$view = Views::getView('address_test_filter_administrative_area');
$filters = $view
->getDisplay()
->getOption('filters');
$filters['field_address_test_administrative_area']['country']['country_source'] = 'argument';
$filters['field_address_test_administrative_area']['country']['country_argument_id'] = 'field_address_test_country_code';
$filters['field_address_test_administrative_area']['expose']['label_type'] = 'dynamic';
$filters['field_address_test_administrative_area']['expose']['label'] = $static_label;
$view
->getDisplay()
->overrideOption('filters', $filters);
$view
->save();
foreach ($dynamic_labels as $country => $dynamic_label) {
$this
->drupalGet("address-test/views/filter-administrative-area/{$country}");
$this
->assertSession()
->pageTextNotContains($static_label);
$this
->assertSession()
->pageTextContains($dynamic_label);
}
}
protected function assertAdministrativeAreaOptions($active_country) {
$areas = [
'BR' => [
'AM' => 'Amazonas',
'BA' => 'Bahia',
'PE' => 'Pernambuco',
'RJ' => 'Rio de Janeiro',
],
'EG' => [
'Alexandria Governorate' => 'Alexandria Governorate',
'Cairo Governorate' => 'Cairo Governorate',
],
'MX' => [
'Chis.' => 'Chiapas',
'Jal.' => 'Jalisco',
'Oax.' => 'Oaxaca',
'Ver.' => 'Veracruz',
],
'US' => [
'LA' => 'Louisiana',
'MA' => 'Massachusetts',
'WI' => 'Wisconsin',
],
];
foreach ($areas as $country => $areas) {
foreach ($areas as $area_key => $area_value) {
if ($country == $active_country) {
$this
->assertSession()
->optionExists('edit-field-address-test-administrative-area', $area_key);
$this
->assertSession()
->optionExists('edit-field-address-test-administrative-area', $area_value);
}
else {
$this
->assertSession()
->pageTextNotContains($area_value);
}
}
}
}
}