3 namespace Drupal\Tests\taxonomy\Functional\Views;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\Tests\views\Functional\ViewTestBase;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\views\Views;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\taxonomy\Entity\Vocabulary;
13 use Drupal\taxonomy\Entity\Term;
16 * Tests taxonomy field filters with translations.
20 class TaxonomyFieldFilterTest extends ViewTestBase {
25 public static $modules = ['language', 'taxonomy', 'taxonomy_test_views', 'text', 'views', 'node'];
28 * Views used by this test.
32 public static $testViews = ['test_field_filters'];
35 * The vocabulary used for creating terms.
37 * @var \Drupal\taxonomy\VocabularyInterface
39 protected $vocabulary;
42 * List of taxonomy term names by language.
46 public $termNames = [];
48 public function setUp($import_test_views = TRUE) {
49 parent::setUp($import_test_views);
51 // Add two new languages.
52 ConfigurableLanguage::createFromLangcode('fr')->save();
53 ConfigurableLanguage::createFromLangcode('es')->save();
57 'en' => 'Food in Paris',
58 'es' => 'Comida en Paris',
59 'fr' => 'Nouriture en Paris',
62 // Create a vocabulary.
63 $this->vocabulary = Vocabulary::create([
64 'name' => 'Views testing tags',
65 'vid' => 'views_testing_tags',
67 $this->vocabulary->save();
69 // Add a translatable field to the vocabulary.
70 $field = FieldStorageConfig::create([
71 'field_name' => 'field_foo',
72 'entity_type' => 'taxonomy_term',
77 'field_name' => 'field_foo',
78 'entity_type' => 'taxonomy_term',
80 'bundle' => 'views_testing_tags',
83 // Create term with translations.
84 $taxonomy = $this->createTermWithProperties(['name' => $this->termNames['en'], 'langcode' => 'en', 'description' => $this->termNames['en'], 'field_foo' => $this->termNames['en']]);
85 foreach (['es', 'fr'] as $langcode) {
86 $translation = $taxonomy->addTranslation($langcode, ['name' => $this->termNames[$langcode]]);
87 $translation->description->value = $this->termNames[$langcode];
88 $translation->field_foo->value = $this->termNames[$langcode];
92 Views::viewsData()->clear();
94 ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
95 $this->container->get('router.builder')->rebuild();
99 * Tests description and term name filters.
101 public function testFilters() {
102 // Test the name filter page, which filters for name contains 'Comida'.
103 // Should show just the Spanish translation, once.
104 $this->assertPageCounts('test-name-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida name filter');
106 // Test the description filter page, which filters for description contains
107 // 'Comida'. Should show just the Spanish translation, once.
108 $this->assertPageCounts('test-desc-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida description filter');
110 // Test the field filter page, which filters for field_foo contains
111 // 'Comida'. Should show just the Spanish translation, once.
112 $this->assertPageCounts('test-field-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida field filter');
114 // Test the name Paris filter page, which filters for name contains
115 // 'Paris'. Should show each translation once.
116 $this->assertPageCounts('test-name-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris name filter');
118 // Test the description Paris page, which filters for description contains
119 // 'Paris'. Should show each translation, once.
120 $this->assertPageCounts('test-desc-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris description filter');
122 // Test the field Paris filter page, which filters for field_foo contains
123 // 'Paris'. Should show each translation once.
124 $this->assertPageCounts('test-field-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris field filter');
129 * Asserts that the given taxonomy translation counts are correct.
131 * @param string $path
132 * Path of the page to test.
133 * @param array $counts
134 * Array whose keys are languages, and values are the number of times
135 * that translation should be shown on the given page.
136 * @param string $message
137 * Message suffix to display.
139 protected function assertPageCounts($path, $counts, $message) {
140 // Get the text of the page.
141 $this->drupalGet($path);
142 $text = $this->getTextContent();
144 // Check the counts. Note that the title and body are both shown on the
145 // page, and they are the same. So the title/body string should appear on
146 // the page twice as many times as the input count.
147 foreach ($counts as $langcode => $count) {
148 $this->assertEqual(substr_count($text, $this->termNames[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
153 * Creates a taxonomy term with specified name and other properties.
155 * @param array $properties
156 * Array of properties and field values to set.
158 * @return \Drupal\taxonomy\TermInterface
159 * The created taxonomy term.
161 protected function createTermWithProperties($properties) {
162 // Use the first available text format.
163 $filter_formats = filter_formats();
164 $format = array_pop($filter_formats);
167 'name' => $this->randomMachineName(),
168 'description' => $this->randomMachineName(),
169 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
170 'field_foo' => $this->randomMachineName(),
173 $term = Term::create([
174 'name' => $properties['name'],
175 'description' => $properties['description'],
176 'format' => $format->id(),
177 'vid' => $this->vocabulary->id(),
178 'langcode' => $properties['langcode'],
180 $term->field_foo->value = $properties['field_foo'];