db backup prior to drupal security update
[yaffs-website] / web / core / modules / comment / src / Tests / Views / CommentFieldFilterTest.php
1 <?php
2
3 namespace Drupal\comment\Tests\Views;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\comment\Entity\Comment;
7
8 /**
9  * Tests comment field filters with translations.
10  *
11  * @group comment
12  */
13 class CommentFieldFilterTest extends CommentTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['language'];
19
20   /**
21    * Views used by this test.
22    *
23    * @var array
24    */
25   public static $testViews = ['test_field_filters'];
26
27   /**
28    * List of comment titles by language.
29    *
30    * @var array
31    */
32   public $commentTitles = [];
33
34   public function setUp() {
35     parent::setUp();
36     $this->drupalLogin($this->drupalCreateUser(['access comments']));
37
38     // Add two new languages.
39     ConfigurableLanguage::createFromLangcode('fr')->save();
40     ConfigurableLanguage::createFromLangcode('es')->save();
41
42     // Set up comment titles.
43     $this->commentTitles = [
44       'en' => 'Food in Paris',
45       'es' => 'Comida en Paris',
46       'fr' => 'Nouriture en Paris',
47     ];
48
49     // Create a new comment. Using the one created earlier will not work,
50     // as it predates the language set-up.
51     $comment = [
52       'uid' => $this->loggedInUser->id(),
53       'entity_id' => $this->nodeUserCommented->id(),
54       'entity_type' => 'node',
55       'field_name' => 'comment',
56       'cid' => '',
57       'pid' => '',
58       'node_type' => '',
59     ];
60     $this->comment = Comment::create($comment);
61
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];
71     }
72     $this->comment->save();
73   }
74
75   /**
76    * Tests body and title filters.
77    */
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');
82
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');
86
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');
90
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');
94   }
95
96   /**
97    * Asserts that the given comment translation counts are correct.
98    *
99    * @param string $path
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.
106    */
107   protected function assertPageCounts($path, $counts, $message) {
108     // Get the text of the page.
109     $this->drupalGet($path);
110     $text = $this->getTextContent();
111
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);
117     }
118   }
119
120 }