3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Component\Gettext\PoHeader;
6 use Drupal\file\Entity\File;
7 use Drupal\Tests\views\Functional\ViewTestBase;
10 * Tests the creation of numeric fields.
14 class NumericFormatPluralTest extends ViewTestBase {
21 public static $modules = ['views_ui', 'file', 'language', 'locale'];
24 * Views used by this test.
28 public static $testViews = ['numeric_test'];
30 protected function setUp($import_test_views = TRUE) {
31 parent::setUp($import_test_views);
33 $web_user = $this->drupalCreateUser(['administer views', 'administer languages']);
34 $this->drupalLogin($web_user);
38 * Test plural formatting setting on a numeric views handler.
40 public function testNumericFormatPlural() {
42 $file = $this->createFile();
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');
50 // Assert that the value is displayed.
51 $this->drupalGet('numeric-test');
52 $this->assertRaw('<span class="field-content">0</span>');
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');
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'));
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');
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);
74 $this->drupalGet('numeric-test');
75 foreach ($numbers as $i => $number) {
76 $this->assertRaw('<span class="field-content">' . $number . ($number == 1 ? ' time' : ' times') . '</span>');
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);
87 // Change the view to Slovenian.
88 $config = $this->config('views.view.numeric_test');
89 $config->set('langcode', 'sl')->save();
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]', '');
98 // Assert that changing the settings will change configuration properly.
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',
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)));
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>');
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();
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>');
136 * Creates and saves a test file.
138 * @return \Drupal\Core\Entity\EntityInterface
141 protected function createFile() {
142 // Create a new file entity.
143 $file = File::create([
145 'filename' => 'druplicon.txt',
146 'uri' => 'public://druplicon.txt',
147 'filemime' => 'text/plain',
150 'status' => FILE_STATUS_PERMANENT,
152 file_put_contents($file->getFileUri(), 'hello world');
154 // Save it, inserting a new record.