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 = [];
38 public function setUp($import_test_views = TRUE) {
39 parent::setUp($import_test_views);
41 // Add two new languages.
42 ConfigurableLanguage::createFromLangcode('fr')->save();
43 ConfigurableLanguage::createFromLangcode('es')->save();
45 // Make the body field translatable. The info is already translatable by
47 $field_storage = FieldStorageConfig::loadByName('block_content', 'body');
48 $field_storage->setTranslatable(TRUE);
49 $field_storage->save();
51 // Set up block_content infos.
52 $this->blockContentInfos = [
53 'en' => 'Food in Paris',
54 'es' => 'Comida en Paris',
55 'fr' => 'Nouriture en Paris',
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];
64 $block_content->save();
68 * Tests body and info filters.
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');
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');
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');
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');
89 * Asserts that the given block_content translation counts are correct.
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.
99 protected function assertPageCounts($path, $counts, $message) {
100 // Get the text of the page.
101 $this->drupalGet($path);
102 $text = $this->getTextContent();
104 foreach ($counts as $langcode => $count) {
105 $this->assertEqual(substr_count($text, $this->blockContentInfos[$langcode]), $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);