Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#35840 closed defect (bug) (invalid)

number_format_i18n throws a PHP error if a string is passed

Reported by: pbearne's profile pbearne Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.3
Component: I18N Keywords: has-patch has-unit-tests close
Focuses: Cc:

Description

In writing a unit test for number_format_i18n we found it blow up when a string is passed

number_format_i18n( 'seven' )

added a cast to force to a number (0) when a string is passed

included unit test

Attachments (3)

35840.patch (3.5 KB) - added by pbearne 10 years ago.
patch with unit tests
35840_1.patch (2.8 KB) - added by pbearne 10 years ago.
new unittest
35840_2.patch (3.8 KB) - added by pbearne 10 years ago.
Both function (with case to float) and unit tests

Download all attachments as: .zip

Change History (13)

@pbearne
10 years ago

patch with unit tests

#1 @pbearne
10 years ago

We wrote as part of the Virtual Contrib to Core seesion http://www.meetup.com/WPToronto/events/228399539/

#2 @pbearne
10 years ago

  • Keywords has-patch has-unit-tests added

#3 @swissspidy
10 years ago

  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to Future Release

That's a pretty cool event!

Please note that the value passed to number_format_i18n() needs to be a float, not an int. See #35893. Otherwise the precision (second parameter) doesn't really make sense.

That means tests are needed for floating point values, perhaps using a data provider.

Oh, and I wouldn't reset $wp_locale in the tests, as other tests might rely on this global variable. Instead, I'd recommend using custom setUp() and tearDown() methods to properly reset the object.

Let me know if you need any help with that.

@pbearne
10 years ago

new unittest

#5 @pbearne
10 years ago

  • Keywords needs-refresh removed

Added more test to use floats and ints comments etc.

@pbearne
10 years ago

Both function (with case to float) and unit tests

#6 @johnbillion
10 years ago

  • Keywords close added
  • Version changed from trunk to 2.3

Raising a warning here is expected behaviour. PHP notices and warnings exist for a reason - if you pass a string to number_format(), you'll get a warning because it expects a float, so there's no reason why passing a string to number_format_i18n() should silently perform type coercion to avoid triggering a warning.

#7 @swissspidy
10 years ago

I agree with @johnbillion, but I'd like to see some unit tests for this function. I'll get them in shape to commit them, perhaps in a new ticket.

#8 @dd32
10 years ago

  • Milestone Future Release deleted
  • Resolution set to invalid
  • Status changed from new to closed

Agreed, these PHP warnings/notices are expected and aid Developers.

#9 @pbearne
10 years ago

@swissspi if you do rewrite the unit tests do let us know what you changed so we can learn

#10 @swissspidy
10 years ago

@pbearne Sure! I added some notes in the follow-up ticket #36029.

Note: See TracTickets for help on using tickets.