3 namespace Drupal\comment\Tests\Views;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\comment\Entity\Comment;
9 * Tests comment field filters with translations.
13 class CommentFieldFilterTest extends CommentTestBase {
18 public static $modules = ['language'];
21 * Views used by this test.
25 public static $testViews = ['test_field_filters'];
28 * List of comment titles by language.
32 public $commentTitles = [];
34 public function setUp() {
36 $this->drupalLogin($this->drupalCreateUser(['access comments']));
38 // Add two new languages.
39 ConfigurableLanguage::createFromLangcode('fr')->save();
40 ConfigurableLanguage::createFromLangcode('es')->save();
42 // Set up comment titles.
43 $this->commentTitles = [
44 'en' => 'Food in Paris',
45 'es' => 'Comida en Paris',
46 'fr' => 'Nouriture en Paris',
49 // Create a new comment. Using the one created earlier will not work,
50 // as it predates the language set-up.
52 'uid' => $this->loggedInUser->id(),
53 'entity_id' => $this->nodeUserCommented->id(),
54 'entity_type' => 'node',
55 'field_name' => 'comment',
60 $this->comment = Comment::create($comment);
62 // Add field values and translate the comment.
63 $this->comment->subject->value = $this->commentTitles['en'];
64 $this->comment->comment_body->value = $this->commentTitles['en'];
65 $this->comment->langcode = 'en';
66 $this->comment->save();
67 foreach (['es', 'fr'] as $langcode) {
68 $translation = $this->comment->addTranslation($langcode, []);
69 $translation->comment_body->value = $this->commentTitles[$langcode];
70 $translation->subject->value = $this->commentTitles[$langcode];
72 $this->comment->save();
76 * Tests body and title filters.
78 public function testFilters() {
79 // Test the title filter page, which filters for title contains 'Comida'.
80 // Should show just the Spanish translation, once.
81 $this->assertPageCounts('test-title-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida title filter');
83 // Test the body filter page, which filters for body contains 'Comida'.
84 // Should show just the Spanish translation, once.
85 $this->assertPageCounts('test-body-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida body filter');
87 // Test the title Paris filter page, which filters for title contains
88 // 'Paris'. Should show each translation once.
89 $this->assertPageCounts('test-title-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris title filter');
91 // Test the body Paris filter page, which filters for body contains
92 // 'Paris'. Should show each translation once.
93 $this->assertPageCounts('test-body-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris body filter');
97 * Asserts that the given comment translation counts are correct.
100 * Path of the page to test.
101 * @param array $counts
102 * Array whose keys are languages, and values are the number of times
103 * that translation should be shown on the given page.
104 * @param string $message
105 * Message suffix to display.
107 protected function assertPageCounts($path, $counts, $message) {
108 // Get the text of the page.
109 $this->drupalGet($path);
110 $text = $this->getTextContent();
112 // Check the counts. Note that the title and body are both shown on the
113 // page, and they are the same. So the title/body string should appear on
114 // the page twice as many times as the input count.
115 foreach ($counts as $langcode => $count) {
116 $this->assertEqual(substr_count($text, $this->commentTitles[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);