Make WordPress Core

Ticket #36029: 36029.2.diff

File 36029.2.diff, 1.9 KB (added by swissspidy, 8 years ago)

Only clone WP_Locale object when necessary

  • new file tests/phpunit/tests/functions/numberFormatI18n.php

    diff --git tests/phpunit/tests/functions/numberFormatI18n.php tests/phpunit/tests/functions/numberFormatI18n.php
    new file mode 100644
    index 0000000..571efdb
    - +  
     1<?php
     2
     3/**
     4 * Tests for number_format_i18n()
     5 *
     6 * @group functions.php
     7 * @group i18n
     8 */
     9class Tests_Functions_Number_Format_I18n extends WP_UnitTestCase {
     10        public function test_should_fall_back_to_number_format_when_wp_locale_is_not_set() {
     11                $locale               = clone $GLOBALS['wp_locale'];
     12                $GLOBALS['wp_locale'] = null;
     13
     14                $actual_1 = number_format_i18n( 123456.789, 0 );
     15                $actual_2 = number_format_i18n( 123456.789, 4 );
     16
     17                $GLOBALS['wp_locale'] = $locale;
     18
     19                $this->assertEquals( '123,457', $actual_1 );
     20                $this->assertEquals( '123,456.7890', $actual_2 );
     21        }
     22
     23        public function test_should_respect_number_format_of_locale() {
     24                $decimal_point = $GLOBALS['wp_locale']->number_format['decimal_point'];
     25                $thousands_sep = $GLOBALS['wp_locale']->number_format['thousands_sep'];
     26
     27                $GLOBALS['wp_locale']->number_format['decimal_point'] = '@';
     28                $GLOBALS['wp_locale']->number_format['thousands_sep'] = '^';
     29
     30                $actual_1 = number_format_i18n( 123456.789, 0 );
     31                $actual_2 = number_format_i18n( 123456.789, 4 );
     32
     33                $GLOBALS['wp_locale']->number_format['decimal_point'] = $decimal_point;
     34                $GLOBALS['wp_locale']->number_format['thousands_sep'] = $thousands_sep;
     35
     36                $this->assertEquals( '123^457', $actual_1 );
     37                $this->assertEquals( '123^456@7890', $actual_2 );
     38        }
     39
     40        public function test_should_default_to_en_us_format() {
     41                $this->assertEquals( '123,457', number_format_i18n( 123456.789, 0 ) );
     42                $this->assertEquals( '123,456.7890', number_format_i18n( 123456.789, 4 ) );
     43        }
     44
     45        public function test_should_handle_negative_precision() {
     46                $this->assertEquals( '123,457', number_format_i18n( 123456.789, 0 ) );
     47                $this->assertEquals( '123,456.7890', number_format_i18n( 123456.789, - 4 ) );
     48        }
     49}