X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FFunctional%2FViews%2FCommentFieldFilterTest.php;fp=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FFunctional%2FViews%2FCommentFieldFilterTest.php;h=dabacc3bed2545cb1fb44ec4d36564eda4a73481;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/comment/tests/src/Functional/Views/CommentFieldFilterTest.php b/web/core/modules/comment/tests/src/Functional/Views/CommentFieldFilterTest.php new file mode 100644 index 000000000..dabacc3be --- /dev/null +++ b/web/core/modules/comment/tests/src/Functional/Views/CommentFieldFilterTest.php @@ -0,0 +1,120 @@ +drupalLogin($this->drupalCreateUser(['access comments'])); + + // Add two new languages. + ConfigurableLanguage::createFromLangcode('fr')->save(); + ConfigurableLanguage::createFromLangcode('es')->save(); + + // Set up comment titles. + $this->commentTitles = [ + 'en' => 'Food in Paris', + 'es' => 'Comida en Paris', + 'fr' => 'Nouriture en Paris', + ]; + + // Create a new comment. Using the one created earlier will not work, + // as it predates the language set-up. + $comment = [ + 'uid' => $this->loggedInUser->id(), + 'entity_id' => $this->nodeUserCommented->id(), + 'entity_type' => 'node', + 'field_name' => 'comment', + 'cid' => '', + 'pid' => '', + 'node_type' => '', + ]; + $this->comment = Comment::create($comment); + + // Add field values and translate the comment. + $this->comment->subject->value = $this->commentTitles['en']; + $this->comment->comment_body->value = $this->commentTitles['en']; + $this->comment->langcode = 'en'; + $this->comment->save(); + foreach (['es', 'fr'] as $langcode) { + $translation = $this->comment->addTranslation($langcode, []); + $translation->comment_body->value = $this->commentTitles[$langcode]; + $translation->subject->value = $this->commentTitles[$langcode]; + } + $this->comment->save(); + } + + /** + * Tests body and title filters. + */ + public function testFilters() { + // Test the title filter page, which filters for title contains 'Comida'. + // Should show just the Spanish translation, once. + $this->assertPageCounts('test-title-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida title filter'); + + // Test the body filter page, which filters for body contains 'Comida'. + // Should show just the Spanish translation, once. + $this->assertPageCounts('test-body-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida body filter'); + + // Test the title Paris filter page, which filters for title contains + // 'Paris'. Should show each translation once. + $this->assertPageCounts('test-title-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris title filter'); + + // Test the body Paris filter page, which filters for body contains + // 'Paris'. Should show each translation once. + $this->assertPageCounts('test-body-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris body filter'); + } + + /** + * Asserts that the given comment translation counts are correct. + * + * @param string $path + * Path of the page to test. + * @param array $counts + * Array whose keys are languages, and values are the number of times + * that translation should be shown on the given page. + * @param string $message + * Message suffix to display. + */ + protected function assertPageCounts($path, $counts, $message) { + // Get the text of the page. + $this->drupalGet($path); + $text = $this->getTextContent(); + + // Check the counts. Note that the title and body are both shown on the + // page, and they are the same. So the title/body string should appear on + // the page twice as many times as the input count. + foreach ($counts as $langcode => $count) { + $this->assertEqual(substr_count($text, $this->commentTitles[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message); + } + } + +}