c2ee2f27637cfb70c6ea96ba4215b7f0e1d76b32
[yaffs-website] / web / core / modules / node / tests / src / Functional / Views / NodeFieldFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional\Views;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6
7 /**
8  * Tests node field filters with translations.
9  *
10  * @group node
11  */
12 class NodeFieldFilterTest extends NodeTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['language'];
18
19   /**
20    * Views used by this test.
21    *
22    * @var array
23    */
24   public static $testViews = ['test_field_filters'];
25
26   /**
27    * List of node titles by language.
28    *
29    * @var array
30    */
31   public $nodeTitles = [];
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp($import_test_views = TRUE) {
37     parent::setUp($import_test_views);
38
39     // Create Page content type.
40     if ($this->profile != 'standard') {
41       $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
42     }
43
44     // Add two new languages.
45     ConfigurableLanguage::createFromLangcode('fr')->save();
46     ConfigurableLanguage::createFromLangcode('es')->save();
47
48     // Set up node titles.
49     $this->nodeTitles = [
50       'en' => 'Food in Paris',
51       'es' => 'Comida en Paris',
52       'fr' => 'Nouriture en Paris',
53     ];
54
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];
60     }
61     $node->save();
62   }
63
64   /**
65    * Tests body and title filters.
66    */
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');
71
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');
75
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');
79
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');
83   }
84
85   /**
86    * Asserts that the given node translation counts are correct.
87    *
88    * @param string $path
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.
95    */
96   protected function assertPageCounts($path, $counts, $message) {
97     // Disable read more links.
98     entity_get_display('node', 'page', 'teaser')->removeComponent('links')->save();
99
100     // Get the text of the page.
101     $this->drupalGet($path);
102     $text = $this->getTextContent();
103
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);
109     }
110   }
111
112 }