3 namespace Drupal\Tests\node\Functional\Views;
5 use Drupal\language\Entity\ConfigurableLanguage;
8 * Tests node field filters with translations.
12 class NodeFieldFilterTest extends NodeTestBase {
17 public static $modules = ['language'];
20 * Views used by this test.
24 public static $testViews = ['test_field_filters'];
27 * List of node titles by language.
31 public $nodeTitles = [];
36 protected function setUp($import_test_views = TRUE) {
37 parent::setUp($import_test_views);
39 // Create Page content type.
40 if ($this->profile != 'standard') {
41 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
44 // Add two new languages.
45 ConfigurableLanguage::createFromLangcode('fr')->save();
46 ConfigurableLanguage::createFromLangcode('es')->save();
48 // Set up node titles.
50 'en' => 'Food in Paris',
51 'es' => 'Comida en Paris',
52 'fr' => 'Nouriture en Paris',
55 // Create node with translations.
56 $node = $this->drupalCreateNode(['title' => $this->nodeTitles['en'], 'langcode' => 'en', 'type' => 'page', 'body' => [['value' => $this->nodeTitles['en']]]]);
57 foreach (['es', 'fr'] as $langcode) {
58 $translation = $node->addTranslation($langcode, ['title' => $this->nodeTitles[$langcode]]);
59 $translation->body->value = $this->nodeTitles[$langcode];
65 * Tests body and title filters.
67 public function testFilters() {
68 // Test the title filter page, which filters for title contains 'Comida'.
69 // Should show just the Spanish translation, once.
70 $this->assertPageCounts('test-title-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida title filter');
72 // Test the body filter page, which filters for body contains 'Comida'.
73 // Should show just the Spanish translation, once.
74 $this->assertPageCounts('test-body-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida body filter');
76 // Test the title Paris filter page, which filters for title contains
77 // 'Paris'. Should show each translation once.
78 $this->assertPageCounts('test-title-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris title filter');
80 // Test the body Paris filter page, which filters for body contains
81 // 'Paris'. Should show each translation once.
82 $this->assertPageCounts('test-body-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris body filter');
86 * Asserts that the given node translation counts are correct.
89 * Path of the page to test.
90 * @param array $counts
91 * Array whose keys are languages, and values are the number of times
92 * that translation should be shown on the given page.
93 * @param string $message
94 * Message suffix to display.
96 protected function assertPageCounts($path, $counts, $message) {
97 // Disable read more links.
98 entity_get_display('node', 'page', 'teaser')->removeComponent('links')->save();
100 // Get the text of the page.
101 $this->drupalGet($path);
102 $text = $this->getTextContent();
104 // Check the counts. Note that the title and body are both shown on the
105 // page, and they are the same. So the title/body string should appear on
106 // the page twice as many times as the input count.
107 foreach ($counts as $langcode => $count) {
108 $this->assertEqual(substr_count($text, $this->nodeTitles[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);