1ff4fb55baf74daf117f430b31df552611d28c82
[yaffs-website] / web / core / modules / block_content / src / Tests / Views / BlockContentFieldFilterTest.php
1 <?php
2
3 namespace Drupal\block_content\Tests\Views;
4
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\language\Entity\ConfigurableLanguage;
7
8 /**
9  * Tests block_content field filters with translations.
10  *
11  * @group block_content
12  */
13 class BlockContentFieldFilterTest extends BlockContentTestBase {
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 block_content infos by language.
29    *
30    * @var array
31    */
32   public $blockContentInfos = [];
33
34
35   /**
36    * {@inheritdoc}
37    */
38   public function setUp() {
39     parent::setUp();
40
41     // Add two new languages.
42     ConfigurableLanguage::createFromLangcode('fr')->save();
43     ConfigurableLanguage::createFromLangcode('es')->save();
44
45     // Make the body field translatable. The info is already translatable by
46     // definition.
47     $field_storage = FieldStorageConfig::loadByName('block_content', 'body');
48     $field_storage->setTranslatable(TRUE);
49     $field_storage->save();
50
51     // Set up block_content infos.
52     $this->blockContentInfos = [
53       'en' => 'Food in Paris',
54       'es' => 'Comida en Paris',
55       'fr' => 'Nouriture en Paris',
56     ];
57
58     // Create block_content with translations.
59     $block_content = $this->createBlockContent(['info' => $this->blockContentInfos['en'], 'langcode' => 'en', 'type' => 'basic', 'body' => [['value' => $this->blockContentInfos['en']]]]);
60     foreach (['es', 'fr'] as $langcode) {
61       $translation = $block_content->addTranslation($langcode, ['info' => $this->blockContentInfos[$langcode]]);
62       $translation->body->value = $this->blockContentInfos[$langcode];
63     }
64     $block_content->save();
65   }
66
67   /**
68    * Tests body and info filters.
69    */
70   public function testFilters() {
71     // Test the info filter page, which filters for info contains 'Comida'.
72     // Should show just the Spanish translation, once.
73     $this->assertPageCounts('test-info-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida info filter');
74
75     // Test the body filter page, which filters for body contains 'Comida'.
76     // Should show just the Spanish translation, once.
77     $this->assertPageCounts('test-body-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida body filter');
78
79     // Test the info Paris filter page, which filters for info contains
80     // 'Paris'. Should show each translation once.
81     $this->assertPageCounts('test-info-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris info filter');
82
83     // Test the body Paris filter page, which filters for body contains
84     // 'Paris'. Should show each translation once.
85     $this->assertPageCounts('test-body-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris body filter');
86   }
87
88   /**
89    * Asserts that the given block_content translation counts are correct.
90    *
91    * @param string $path
92    *   Path of the page to test.
93    * @param array $counts
94    *   Array whose keys are languages, and values are the number of times
95    *   that translation should be shown on the given page.
96    * @param string $message
97    *   Message suffix to display.
98    */
99   protected function assertPageCounts($path, $counts, $message) {
100     // Get the text of the page.
101     $this->drupalGet($path);
102     $text = $this->getTextContent();
103
104     foreach ($counts as $langcode => $count) {
105       $this->assertEqual(substr_count($text, $this->blockContentInfos[$langcode]), $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
106     }
107   }
108
109 }