X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FFunctional%2FPlugin%2FNumericFormatPluralTest.php;fp=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FFunctional%2FPlugin%2FNumericFormatPluralTest.php;h=08f2504c54ea92ed07ebb3627d90ebc250e202c6;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/views/tests/src/Functional/Plugin/NumericFormatPluralTest.php b/web/core/modules/views/tests/src/Functional/Plugin/NumericFormatPluralTest.php new file mode 100644 index 000000000..08f2504c5 --- /dev/null +++ b/web/core/modules/views/tests/src/Functional/Plugin/NumericFormatPluralTest.php @@ -0,0 +1,160 @@ +drupalCreateUser(['administer views', 'administer languages']); + $this->drupalLogin($web_user); + } + + /** + * Test plural formatting setting on a numeric views handler. + */ + public function testNumericFormatPlural() { + // Create a file. + $file = $this->createFile(); + + // Assert that the starting configuration is correct. + $config = $this->config('views.view.numeric_test'); + $field_config_prefix = 'display.default.display_options.fields.count.'; + $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE); + $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1' . LOCALE_PLURAL_DELIMITER . '@count'); + + // Assert that the value is displayed. + $this->drupalGet('numeric-test'); + $this->assertRaw('0'); + + // Assert that the user interface has controls to change it. + $this->drupalGet('admin/structure/views/nojs/handler/numeric_test/page_1/field/count'); + $this->assertFieldByName('options[format_plural_values][0]', '1'); + $this->assertFieldByName('options[format_plural_values][1]', '@count'); + + // Assert that changing the settings will change configuration properly. + $edit = ['options[format_plural_values][0]' => '1 time', 'options[format_plural_values][1]' => '@count times']; + $this->drupalPostForm(NULL, $edit, t('Apply')); + $this->drupalPostForm(NULL, [], t('Save')); + + $config = $this->config('views.view.numeric_test'); + $field_config_prefix = 'display.default.display_options.fields.count.'; + $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE); + $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), '1 time' . LOCALE_PLURAL_DELIMITER . '@count times'); + + // Assert that the value is displayed with some sample values. + $numbers = [0, 1, 2, 3, 4, 42]; + foreach ($numbers as $i => $number) { + \Drupal::service('file.usage')->add($file, 'views_ui', 'dummy', $i, $number); + } + $this->drupalGet('numeric-test'); + foreach ($numbers as $i => $number) { + $this->assertRaw('' . $number . ($number == 1 ? ' time' : ' times') . ''); + } + + // Add Slovenian and set its plural formula to test multiple plural forms. + $edit = ['predefined_langcode' => 'sl']; + $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); + $formula = 'nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);'; + $header = new PoHeader(); + list($nplurals, $formula) = $header->parsePluralForms($formula); + \Drupal::service('locale.plural.formula')->setPluralFormula('sl', $nplurals, $formula); + + // Change the view to Slovenian. + $config = $this->config('views.view.numeric_test'); + $config->set('langcode', 'sl')->save(); + + // Assert that the user interface has controls with more inputs now. + $this->drupalGet('admin/structure/views/nojs/handler/numeric_test/page_1/field/count'); + $this->assertFieldByName('options[format_plural_values][0]', '1 time'); + $this->assertFieldByName('options[format_plural_values][1]', '@count times'); + $this->assertFieldByName('options[format_plural_values][2]', ''); + $this->assertFieldByName('options[format_plural_values][3]', ''); + + // Assert that changing the settings will change configuration properly. + $edit = [ + 'options[format_plural_values][0]' => '@count time0', + 'options[format_plural_values][1]' => '@count time1', + 'options[format_plural_values][2]' => '@count time2', + 'options[format_plural_values][3]' => '@count time3', + ]; + $this->drupalPostForm(NULL, $edit, t('Apply')); + $this->drupalPostForm(NULL, [], t('Save')); + $config = $this->config('views.view.numeric_test'); + $field_config_prefix = 'display.default.display_options.fields.count.'; + $this->assertEqual($config->get($field_config_prefix . 'format_plural'), TRUE); + $this->assertEqual($config->get($field_config_prefix . 'format_plural_string'), implode(LOCALE_PLURAL_DELIMITER, array_values($edit))); + + // The view should now use the new plural configuration. + $this->drupalGet('sl/numeric-test'); + $this->assertRaw('0 time3'); + $this->assertRaw('1 time0'); + $this->assertRaw('2 time1'); + $this->assertRaw('3 time2'); + $this->assertRaw('4 time2'); + $this->assertRaw('42 time3'); + + // Add an English configuration translation with English plurals. + $english = \Drupal::languageManager()->getLanguageConfigOverride('en', 'views.view.numeric_test'); + $english->set('display.default.display_options.fields.count.format_plural_string', '1 time' . LOCALE_PLURAL_DELIMITER . '@count times')->save(); + + // The view displayed in English should use the English translation. + $this->drupalGet('numeric-test'); + $this->assertRaw('0 times'); + $this->assertRaw('1 time'); + $this->assertRaw('2 times'); + $this->assertRaw('3 times'); + $this->assertRaw('4 times'); + $this->assertRaw('42 times'); + } + + /** + * Creates and saves a test file. + * + * @return \Drupal\Core\Entity\EntityInterface + * A file entity. + */ + protected function createFile() { + // Create a new file entity. + $file = File::create([ + 'uid' => 1, + 'filename' => 'druplicon.txt', + 'uri' => 'public://druplicon.txt', + 'filemime' => 'text/plain', + 'created' => 1, + 'changed' => 1, + 'status' => FILE_STATUS_PERMANENT, + ]); + file_put_contents($file->getFileUri(), 'hello world'); + + // Save it, inserting a new record. + $file->save(); + + return $file; + } + +}