View source
<?php
namespace Drupal\Tests\taxonomy\Functional\Views;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
class TaxonomyFieldFilterTest extends ViewTestBase {
protected static $modules = [
'language',
'taxonomy',
'taxonomy_test_views',
'text',
'views',
'node',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_field_filters',
];
protected $vocabulary;
public $termNames = [];
public function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
ConfigurableLanguage::createFromLangcode('fr')
->save();
ConfigurableLanguage::createFromLangcode('es')
->save();
$this->termNames = [
'en' => 'Food in Paris',
'es' => 'Comida en Paris',
'fr' => 'Nourriture en Paris',
];
$this->vocabulary = Vocabulary::create([
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
]);
$this->vocabulary
->save();
$field = FieldStorageConfig::create([
'field_name' => 'field_foo',
'entity_type' => 'taxonomy_term',
'type' => 'text',
]);
$field
->save();
FieldConfig::create([
'field_name' => 'field_foo',
'entity_type' => 'taxonomy_term',
'label' => 'Foo',
'bundle' => 'views_testing_tags',
])
->save();
$taxonomy = $this
->createTermWithProperties([
'name' => $this->termNames['en'],
'langcode' => 'en',
'description' => $this->termNames['en'],
'field_foo' => $this->termNames['en'],
]);
foreach ([
'es',
'fr',
] as $langcode) {
$translation = $taxonomy
->addTranslation($langcode, [
'name' => $this->termNames[$langcode],
]);
$translation->description->value = $this->termNames[$langcode];
$translation->field_foo->value = $this->termNames[$langcode];
}
$taxonomy
->save();
Views::viewsData()
->clear();
ViewTestData::createTestViews(static::class, [
'taxonomy_test_views',
]);
}
public function testFilters() {
$this
->assertPageCounts('test-name-filter', [
'es' => 1,
'fr' => 0,
'en' => 0,
], 'Comida name filter');
$this
->assertPageCounts('test-desc-filter', [
'es' => 1,
'fr' => 0,
'en' => 0,
], 'Comida description filter');
$this
->assertPageCounts('test-field-filter', [
'es' => 1,
'fr' => 0,
'en' => 0,
], 'Comida field filter');
$this
->assertPageCounts('test-name-paris', [
'es' => 1,
'fr' => 1,
'en' => 1,
], 'Paris name filter');
$this
->assertPageCounts('test-desc-paris', [
'es' => 1,
'fr' => 1,
'en' => 1,
], 'Paris description filter');
$this
->assertPageCounts('test-field-paris', [
'es' => 1,
'fr' => 1,
'en' => 1,
], 'Paris field filter');
}
protected function assertPageCounts($path, $counts, $message) {
$this
->drupalGet($path);
$text = $this
->getTextContent();
foreach ($counts as $langcode => $count) {
$this
->assertEquals(2 * $count, substr_count($text, $this->termNames[$langcode]), 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
}
}
protected function createTermWithProperties($properties) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$properties += [
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'field_foo' => $this
->randomMachineName(),
];
$term = Term::create([
'name' => $properties['name'],
'description' => $properties['description'],
'format' => $format
->id(),
'vid' => $this->vocabulary
->id(),
'langcode' => $properties['langcode'],
]);
$term->field_foo->value = $properties['field_foo'];
$term
->save();
return $term;
}
}