Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / NumericFormatPluralTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Component\Gettext\PoHeader;
6 use Drupal\file\Entity\File;
7 use Drupal\Tests\views\Functional\ViewTestBase;
8
9 /**
10  * Tests the creation of numeric fields.
11  *
12  * @group field
13  */
14 class NumericFormatPluralTest extends ViewTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['views_ui', 'file', 'language', 'locale'];
22
23   /**
24    * Views used by this test.
25    *
26    * @var array
27    */
28   public static $testViews = ['numeric_test'];
29
30   protected function setUp($import_test_views = TRUE) {
31     parent::setUp($import_test_views);
32
33     $web_user = $this->drupalCreateUser(['administer views', 'administer languages']);
34     $this->drupalLogin($web_user);
35   }
36
37   /**
38    * Test plural formatting setting on a numeric views handler.
39    */
40   public function testNumericFormatPlural() {
41     // Create a file.
42     $file = $this->createFile();
43
44     // Assert that the starting configuration is correct.
45     $config = $this->config('views.view.numeric_test');
46     $field_config_prefix = 'display.default.display_options.fields.count.';
47     $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
48     $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1' . LOCALE_PLURAL_DELIMITER . '@count');
49
50     // Assert that the value is displayed.
51     $this->drupalGet('numeric-test');
52     $this->assertRaw('<span class="field-content">0</span>');
53
54     // Assert that the user interface has controls to change it.
55     $this->drupalGet('admin/structure/views/nojs/handler/numeric_test/page_1/field/count');
56     $this->assertFieldByName('options[format_plural_values][0]', '1');
57     $this->assertFieldByName('options[format_plural_values][1]', '@count');
58
59     // Assert that changing the settings will change configuration properly.
60     $edit = ['options[format_plural_values][0]' => '1 time', 'options[format_plural_values][1]' => '@count times'];
61     $this->drupalPostForm(NULL, $edit, t('Apply'));
62     $this->drupalPostForm(NULL, [], t('Save'));
63
64     $config = $this->config('views.view.numeric_test');
65     $field_config_prefix = 'display.default.display_options.fields.count.';
66     $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
67     $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1 time' . LOCALE_PLURAL_DELIMITER . '@count times');
68
69     // Assert that the value is displayed with some sample values.
70     $numbers = [0, 1, 2, 3, 4, 42];
71     foreach ($numbers as $i => $number) {
72       \Drupal::service('file.usage')->add($file, 'views_ui', 'dummy', $i, $number);
73     }
74     $this->drupalGet('numeric-test');
75     foreach ($numbers as $i => $number) {
76       $this->assertRaw('<span class="field-content">' . $number . ($number == 1 ? ' time' : ' times') . '</span>');
77     }
78
79     // Add Slovenian and set its plural formula to test multiple plural forms.
80     $edit = ['predefined_langcode' => 'sl'];
81     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
82     $formula = 'nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);';
83     $header = new PoHeader();
84     list($nplurals, $formula) = $header->parsePluralForms($formula);
85     \Drupal::service('locale.plural.formula')->setPluralFormula('sl', $nplurals, $formula);
86
87     // Change the view to Slovenian.
88     $config = $this->config('views.view.numeric_test');
89     $config->set('langcode', 'sl')->save();
90
91     // Assert that the user interface has controls with more inputs now.
92     $this->drupalGet('admin/structure/views/nojs/handler/numeric_test/page_1/field/count');
93     $this->assertFieldByName('options[format_plural_values][0]', '1 time');
94     $this->assertFieldByName('options[format_plural_values][1]', '@count times');
95     $this->assertFieldByName('options[format_plural_values][2]', '');
96     $this->assertFieldByName('options[format_plural_values][3]', '');
97
98     // Assert that changing the settings will change configuration properly.
99     $edit = [
100       'options[format_plural_values][0]' => '@count time0',
101       'options[format_plural_values][1]' => '@count time1',
102       'options[format_plural_values][2]' => '@count time2',
103       'options[format_plural_values][3]' => '@count time3',
104     ];
105     $this->drupalPostForm(NULL, $edit, t('Apply'));
106     $this->drupalPostForm(NULL, [], t('Save'));
107     $config = $this->config('views.view.numeric_test');
108     $field_config_prefix = 'display.default.display_options.fields.count.';
109     $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE);
110     $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), implode(LOCALE_PLURAL_DELIMITER, array_values($edit)));
111
112     // The view should now use the new plural configuration.
113     $this->drupalGet('sl/numeric-test');
114     $this->assertRaw('<span class="field-content">0 time3</span>');
115     $this->assertRaw('<span class="field-content">1 time0</span>');
116     $this->assertRaw('<span class="field-content">2 time1</span>');
117     $this->assertRaw('<span class="field-content">3 time2</span>');
118     $this->assertRaw('<span class="field-content">4 time2</span>');
119     $this->assertRaw('<span class="field-content">42 time3</span>');
120
121     // Add an English configuration translation with English plurals.
122     $english = \Drupal::languageManager()->getLanguageConfigOverride('en', 'views.view.numeric_test');
123     $english->set('display.default.display_options.fields.count.format_plural_string', '1 time' . LOCALE_PLURAL_DELIMITER . '@count times')->save();
124
125     // The view displayed in English should use the English translation.
126     $this->drupalGet('numeric-test');
127     $this->assertRaw('<span class="field-content">0 times</span>');
128     $this->assertRaw('<span class="field-content">1 time</span>');
129     $this->assertRaw('<span class="field-content">2 times</span>');
130     $this->assertRaw('<span class="field-content">3 times</span>');
131     $this->assertRaw('<span class="field-content">4 times</span>');
132     $this->assertRaw('<span class="field-content">42 times</span>');
133   }
134
135   /**
136    * Creates and saves a test file.
137    *
138    * @return \Drupal\Core\Entity\EntityInterface
139    *   A file entity.
140    */
141   protected function createFile() {
142     // Create a new file entity.
143     $file = File::create([
144       'uid' => 1,
145       'filename' => 'druplicon.txt',
146       'uri' => 'public://druplicon.txt',
147       'filemime' => 'text/plain',
148       'created' => 1,
149       'changed' => 1,
150       'status' => FILE_STATUS_PERMANENT,
151     ]);
152     file_put_contents($file->getFileUri(), 'hello world');
153
154     // Save it, inserting a new record.
155     $file->save();
156
157     return $file;
158   }
159
160 }