Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / Views / TaxonomyFieldFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional\Views;
4
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;
14
15 /**
16  * Tests taxonomy field filters with translations.
17  *
18  * @group taxonomy
19  */
20 class TaxonomyFieldFilterTest extends ViewTestBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static $modules = ['language', 'taxonomy', 'taxonomy_test_views', 'text', 'views', 'node'];
26
27   /**
28    * Views used by this test.
29    *
30    * @var array
31    */
32   public static $testViews = ['test_field_filters'];
33
34   /**
35    * The vocabulary used for creating terms.
36    *
37    * @var \Drupal\taxonomy\VocabularyInterface
38    */
39   protected $vocabulary;
40
41   /**
42    * List of taxonomy term names by language.
43    *
44    * @var array
45    */
46   public $termNames = [];
47
48   public function setUp($import_test_views = TRUE) {
49     parent::setUp($import_test_views);
50
51     // Add two new languages.
52     ConfigurableLanguage::createFromLangcode('fr')->save();
53     ConfigurableLanguage::createFromLangcode('es')->save();
54
55     // Set up term names.
56     $this->termNames = [
57       'en' => 'Food in Paris',
58       'es' => 'Comida en Paris',
59       'fr' => 'Nouriture en Paris',
60     ];
61
62     // Create a vocabulary.
63     $this->vocabulary = Vocabulary::create([
64       'name' => 'Views testing tags',
65       'vid' => 'views_testing_tags',
66     ]);
67     $this->vocabulary->save();
68
69     // Add a translatable field to the vocabulary.
70     $field = FieldStorageConfig::create([
71       'field_name' => 'field_foo',
72       'entity_type' => 'taxonomy_term',
73       'type' => 'text',
74     ]);
75     $field->save();
76     FieldConfig::create([
77       'field_name' => 'field_foo',
78       'entity_type' => 'taxonomy_term',
79       'label' => 'Foo',
80       'bundle' => 'views_testing_tags',
81     ])->save();
82
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];
89     }
90     $taxonomy->save();
91
92     Views::viewsData()->clear();
93
94     ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
95     $this->container->get('router.builder')->rebuild();
96   }
97
98   /**
99    * Tests description and term name filters.
100    */
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');
105
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');
109
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');
113
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');
117
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');
121
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');
125
126   }
127
128   /**
129    * Asserts that the given taxonomy translation counts are correct.
130    *
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.
138    */
139   protected function assertPageCounts($path, $counts, $message) {
140     // Get the text of the page.
141     $this->drupalGet($path);
142     $text = $this->getTextContent();
143
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);
149     }
150   }
151
152   /**
153    * Creates a taxonomy term with specified name and other properties.
154    *
155    * @param array $properties
156    *   Array of properties and field values to set.
157    *
158    * @return \Drupal\taxonomy\TermInterface
159    *   The created taxonomy term.
160    */
161   protected function createTermWithProperties($properties) {
162     // Use the first available text format.
163     $filter_formats = filter_formats();
164     $format = array_pop($filter_formats);
165
166     $properties += [
167       'name' => $this->randomMachineName(),
168       'description' => $this->randomMachineName(),
169       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
170       'field_foo' => $this->randomMachineName(),
171     ];
172
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'],
179     ]);
180     $term->field_foo->value = $properties['field_foo'];
181     $term->save();
182     return $term;
183   }
184
185 }