Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / Views / BlockContentFieldFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional\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    * {@inheritdoc}
36    */
37   public function setUp($import_test_views = TRUE) {
38     parent::setUp($import_test_views);
39
40     // Add two new languages.
41     ConfigurableLanguage::createFromLangcode('fr')->save();
42     ConfigurableLanguage::createFromLangcode('es')->save();
43
44     // Make the body field translatable. The info is already translatable by
45     // definition.
46     $field_storage = FieldStorageConfig::loadByName('block_content', 'body');
47     $field_storage->setTranslatable(TRUE);
48     $field_storage->save();
49
50     // Set up block_content infos.
51     $this->blockContentInfos = [
52       'en' => 'Food in Paris',
53       'es' => 'Comida en Paris',
54       'fr' => 'Nouriture en Paris',
55     ];
56
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];
62     }
63     $block_content->save();
64   }
65
66   /**
67    * Tests body and info filters.
68    */
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');
73
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');
77
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');
81
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');
85   }
86
87   /**
88    * Asserts that the given block_content translation counts are correct.
89    *
90    * @param string $path
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.
97    */
98   protected function assertPageCounts($path, $counts, $message) {
99     // Get the text of the page.
100     $this->drupalGet($path);
101     $text = $this->getTextContent();
102
103     foreach ($counts as $langcode => $count) {
104       $this->assertEqual(substr_count($text, $this->blockContentInfos[$langcode]), $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
105     }
106   }
107
108 }