phone.int.test 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. //require_once drupal_get_path('module', 'phone') . 'includes/phone.int.inc';
  3. class PhoneIntTest extends DrupalWebTestCase {
  4. public static function getInfo() {
  5. return array(
  6. 'name' => 'International phone number test',
  7. 'description' => 'Tests international phone number validation.',
  8. 'group' => 'Phone',
  9. );
  10. }
  11. public function setUp() {
  12. // Enable the phone module.
  13. parent::setUp(array('phone'));
  14. }
  15. private function assertConversion($input, $expect = TRUE, $field = array(), $expect_error = FALSE) {
  16. $error = FALSE;
  17. if (!isset($field['phone_int_max_length'])) {
  18. $field['phone_int_max_length'] = '15';
  19. }
  20. if (!isset($field['phone_default_country_code'])) {
  21. $field['phone_default_country_code'] = '1';
  22. }
  23. if ($expect === FALSE) {
  24. $this->assertFalse(valid_phone_number('int', $input, $field, $error));
  25. $this->assertIdentical($error, $expect_error);
  26. return;
  27. }
  28. elseif ($expect === TRUE) {
  29. $expect = $input;
  30. }
  31. $this->assertTrue(valid_phone_number('int', $input, $field, $error));
  32. $this->assertIdentical($error, FALSE);
  33. $result = format_phone_number('int', $input, $field);
  34. $this->assertIdentical($result, $expect);
  35. }
  36. public function testBasic() {
  37. $this->assertConversion('+1 7329018493');
  38. }
  39. public function testBasicWithThreeCountryCode() {
  40. $this->assertConversion('+672 565434');
  41. }
  42. public function testBasicWithFourCountryCode() {
  43. $this->assertConversion('+6724 565434', FALSE, array(), array(
  44. 'Invalid international phone number: Country code "+%cc" is too long; valid country codes are three digits or less.',
  45. array('%cc' => '6724')
  46. ));
  47. }
  48. public function testBasicWithSpaces() {
  49. $this->assertConversion('+1 732 901 8493');
  50. }
  51. public function testBasicNormalizeOtherCharacters() {
  52. $this->assertConversion('+1 (732) 901-8493', '+1 732 901 8493');
  53. }
  54. public function testRemoveNDD() {
  55. $this->assertConversion('+54 0435344', '+54 435344');
  56. }
  57. public function testRemoveNonStandardNDD() {
  58. $this->assertConversion('+374 (8) 435344', '+374 435344');
  59. }
  60. public function testAddCountryCode() {
  61. $this->assertConversion('732 343 2333', '+1 732 343 2333', array('phone_default_country_code' => '1'));
  62. }
  63. public function testOverlongNumber() {
  64. $this->assertConversion('+123 456 789 012 3456', FALSE, array(),
  65. 'Invalid international phone number: Phone number is too long; international phone numbers are limited to 15 digits.'
  66. );
  67. }
  68. public function testOverlongNumberWithoutCountryCode() {
  69. $this->assertConversion('456 789 012 3456', FALSE, array('phone_default_country_code' => '123'),
  70. 'Invalid international phone number: Phone number is too long; international phone numbers are limited to 15 digits.'
  71. );
  72. }
  73. public function testLetters() {
  74. $this->assertConversion('+1 343 CALL US', FALSE, array(),
  75. 'Invalid international phone number: Phone number contains invalid characters; only allowed characters are numbers and punctuation.'
  76. );
  77. }
  78. }