3 namespace Drupal\Tests\block_content\Functional\Views;
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\language\Entity\ConfigurableLanguage;
9 * Tests block_content field filters with translations.
11 * @group block_content
13 class BlockContentFieldFilterTest extends BlockContentTestBase {
18 public static $modules = ['language'];
21 * Views used by this test.
25 public static $testViews = ['test_field_filters'];
28 * List of block_content infos by language.
32 public $blockContentInfos = [];
37 public function setUp($import_test_views = TRUE) {
38 parent::setUp($import_test_views);
40 // Add two new languages.
41 ConfigurableLanguage::createFromLangcode('fr')->save();
42 ConfigurableLanguage::createFromLangcode('es')->save();
44 // Make the body field translatable. The info is already translatable by
46 $field_storage = FieldStorageConfig::loadByName('block_content', 'body');
47 $field_storage->setTranslatable(TRUE);
48 $field_storage->save();
50 // Set up block_content infos.
51 $this->blockContentInfos = [
52 'en' => 'Food in Paris',
53 'es' => 'Comida en Paris',
54 'fr' => 'Nouriture en Paris',
57 // Create block_content with translations.
58 $block_content = $this->createBlockContent(['info' => $this->blockContentInfos['en'], 'langcode' => 'en', 'type' => 'basic', 'body' => [['value' => $this->blockContentInfos['en']]]]);
59 foreach (['es', 'fr'] as $langcode) {
60 $translation = $block_content->addTranslation($langcode, ['info' => $this->blockContentInfos[$langcode]]);
61 $translation->body->value = $this->blockContentInfos[$langcode];
63 $block_content->save();
67 * Tests body and info filters.
69 public function testFilters() {
70 // Test the info filter page, which filters for info contains 'Comida'.
71 // Should show just the Spanish translation, once.
72 $this->assertPageCounts('test-info-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida info filter');
74 // Test the body filter page, which filters for body contains 'Comida'.
75 // Should show just the Spanish translation, once.
76 $this->assertPageCounts('test-body-filter', ['es' => 1, 'fr' => 0, 'en' => 0], 'Comida body filter');
78 // Test the info Paris filter page, which filters for info contains
79 // 'Paris'. Should show each translation once.
80 $this->assertPageCounts('test-info-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris info filter');
82 // Test the body Paris filter page, which filters for body contains
83 // 'Paris'. Should show each translation once.
84 $this->assertPageCounts('test-body-paris', ['es' => 1, 'fr' => 1, 'en' => 1], 'Paris body filter');
88 * Asserts that the given block_content translation counts are correct.
91 * Path of the page to test.
92 * @param array $counts
93 * Array whose keys are languages, and values are the number of times
94 * that translation should be shown on the given page.
95 * @param string $message
96 * Message suffix to display.
98 protected function assertPageCounts($path, $counts, $message) {
99 // Get the text of the page.
100 $this->drupalGet($path);
101 $text = $this->getTextContent();
103 foreach ($counts as $langcode => $count) {
104 $this->assertEqual(substr_count($text, $this->blockContentInfos[$langcode]), $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);