woocommerce/tests/unit-tests/util/validation.php

190 lines
5.7 KiB
PHP
Raw Normal View History

2015-03-06 17:27:18 +00:00
<?php
/**
* Unit tests for validation.
*
2015-03-06 17:27:18 +00:00
* @package WooCommerce\Tests\Util
*/
/**
* Class WC_Tests_Validation.
2015-03-06 17:27:18 +00:00
* @since 2.3
*/
class WC_Tests_Validation extends WC_Unit_Test_Case {
2015-03-06 17:27:18 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test is_email().
2015-03-06 17:27:18 +00:00
*
* @since 2.3
*/
public function test_is_email() {
$this->assertEquals( 'email@domain.com', WC_Validation::is_email( 'email@domain.com' ) );
$this->assertFalse( WC_Validation::is_email( 'not a mail' ) );
$this->assertFalse( WC_Validation::is_email( 'http://test.com' ) );
2015-03-06 17:27:18 +00:00
}
2015-04-14 13:34:35 +00:00
/**
2015-11-03 13:31:20 +00:00
* Data provider for test_is_phone.
2015-04-14 13:34:35 +00:00
*
* @since 2.4
*/
public function data_provider_test_is_phone() {
return array(
array( true, WC_Validation::is_phone( '+00 000 00 00 000' ) ),
array( true, WC_Validation::is_phone( '+00-000-00-00-000' ) ),
array( true, WC_Validation::is_phone( '(000) 00 00 000' ) ),
array( true, WC_Validation::is_phone( '+00.000.00.00.000' ) ),
array( false, WC_Validation::is_phone( '+00 aaa dd ee fff' ) ),
2015-04-14 13:34:35 +00:00
);
}
2015-03-06 17:27:18 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test is_phone().
2015-03-06 17:27:18 +00:00
*
2019-04-10 23:12:27 +00:00
* @param mixed $assert Expected value.
* @param mixed $values Actual value.
*
2015-04-14 13:34:35 +00:00
* @dataProvider data_provider_test_is_phone
2015-03-06 17:27:18 +00:00
* @since 2.3
*/
2015-04-14 13:34:35 +00:00
public function test_is_phone( $assert, $values ) {
$this->assertEquals( $assert, $values );
}
/**
2015-11-03 13:31:20 +00:00
* Data provider for test_is_postcode().
2015-04-14 13:34:35 +00:00
*
* @since 2.4
*/
public function data_provider_test_is_postcode() {
$it = array(
array( true, WC_Validation::is_postcode( '99999', 'IT' ) ),
array( false, WC_Validation::is_postcode( '9999', 'IT' ) ),
array( false, WC_Validation::is_postcode( 'ABC 999', 'IT' ) ),
array( false, WC_Validation::is_postcode( 'ABC-999', 'IT' ) ),
array( false, WC_Validation::is_postcode( 'ABC_123', 'IT' ) ),
2015-04-14 13:34:35 +00:00
);
$gb = array(
array( true, WC_Validation::is_postcode( 'A9 9AA', 'GB' ) ),
array( false, WC_Validation::is_postcode( '99999', 'GB' ) ),
2015-04-14 13:34:35 +00:00
);
$us = array(
array( true, WC_Validation::is_postcode( '99999', 'US' ) ),
array( true, WC_Validation::is_postcode( '99999-9999', 'US' ) ),
array( false, WC_Validation::is_postcode( 'ABCDE', 'US' ) ),
array( false, WC_Validation::is_postcode( 'ABCDE-9999', 'US' ) ),
2015-04-14 13:34:35 +00:00
);
$ch = array(
array( true, WC_Validation::is_postcode( '9999', 'CH' ) ),
array( false, WC_Validation::is_postcode( '99999', 'CH' ) ),
array( false, WC_Validation::is_postcode( 'ABCDE', 'CH' ) ),
2015-04-14 13:34:35 +00:00
);
$br = array(
array( true, WC_Validation::is_postcode( '99999-999', 'BR' ) ),
array( true, WC_Validation::is_postcode( '99999999', 'BR' ) ),
array( false, WC_Validation::is_postcode( '99999 999', 'BR' ) ),
array( false, WC_Validation::is_postcode( '99999-ABC', 'BR' ) ),
2015-04-14 13:34:35 +00:00
);
2016-07-11 14:56:35 +00:00
$ca = array(
array( true, WC_Validation::is_postcode( 'A9A 9A9', 'CA' ) ),
array( true, WC_Validation::is_postcode( 'A9A9A9', 'CA' ) ),
2016-07-11 14:56:35 +00:00
array( true, WC_Validation::is_postcode( 'a9a9a9', 'CA' ) ),
array( false, WC_Validation::is_postcode( 'D0A 9A9', 'CA' ) ),
2016-07-11 14:56:35 +00:00
array( false, WC_Validation::is_postcode( '99999', 'CA' ) ),
array( false, WC_Validation::is_postcode( 'ABC999', 'CA' ) ),
array( false, WC_Validation::is_postcode( '0A0A0A', 'CA' ) ),
2016-07-11 14:56:35 +00:00
);
2015-04-14 13:34:35 +00:00
return array_merge( $it, $gb, $us, $ch, $br, $ca );
2015-03-06 17:27:18 +00:00
}
2015-04-14 13:34:35 +00:00
2015-04-08 15:56:02 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test is_postcode().
2015-04-08 15:56:02 +00:00
*
2019-04-10 23:12:27 +00:00
* @param mixed $assert Expected value.
* @param mixed $values Actual value.
*
2015-04-14 13:34:35 +00:00
* @dataProvider data_provider_test_is_postcode
2015-04-08 15:56:02 +00:00
* @since 2.4
*/
2015-04-14 13:34:35 +00:00
public function test_is_postcode( $assert, $values ) {
$this->assertEquals( $assert, $values );
2015-04-08 15:56:02 +00:00
}
2015-04-14 13:34:35 +00:00
/**
* Data provider for test_is_gb_postcode.
2015-04-14 13:34:35 +00:00
*
* @since 2.4
*/
public function data_provider_test_is_gb_postcode() {
2015-04-14 13:34:35 +00:00
return array(
array( true, WC_Validation::is_gb_postcode( 'AA9A 9AA' ) ),
array( true, WC_Validation::is_gb_postcode( 'A9A 9AA' ) ),
array( true, WC_Validation::is_gb_postcode( 'A9 9AA' ) ),
array( true, WC_Validation::is_gb_postcode( 'A99 9AA' ) ),
array( true, WC_Validation::is_gb_postcode( 'AA99 9AA' ) ),
array( true, WC_Validation::is_gb_postcode( 'BFPO 801' ) ),
array( false, WC_Validation::is_gb_postcode( '99999' ) ),
array( false, WC_Validation::is_gb_postcode( '9999 999' ) ),
array( false, WC_Validation::is_gb_postcode( '999 999' ) ),
array( false, WC_Validation::is_gb_postcode( '99 999' ) ),
array( false, WC_Validation::is_gb_postcode( '9A A9A' ) ),
2015-04-14 13:34:35 +00:00
);
}
2015-04-08 15:56:02 +00:00
/**
* Test is_gb_postcode().
2015-04-08 15:56:02 +00:00
*
2019-04-10 23:12:27 +00:00
* @param mixed $assert Expected value.
* @param mixed $values Actual value.
*
* @dataProvider data_provider_test_is_gb_postcode
2015-04-08 15:56:02 +00:00
* @since 2.4
*/
public function test_is_gb_postcode( $assert, $values ) {
2015-04-14 13:34:35 +00:00
$this->assertEquals( $assert, $values );
2015-04-08 15:56:02 +00:00
}
2015-04-14 13:34:35 +00:00
/**
2015-11-03 13:31:20 +00:00
* Data provider for test_format_postcode.
2015-04-14 13:34:35 +00:00
*
* @since 2.4
*/
public function data_provider_test_format_postcode() {
return array(
array( '99999', WC_Validation::format_postcode( '99999', 'IT' ) ),
array( '99999', WC_Validation::format_postcode( ' 99999 ', 'IT' ) ),
array( '99999', WC_Validation::format_postcode( '999 99', 'IT' ) ),
array( 'ABCDE', WC_Validation::format_postcode( 'abcde', 'IT' ) ),
array( 'AB CDE', WC_Validation::format_postcode( 'abcde', 'GB' ) ),
array( 'AB CDE', WC_Validation::format_postcode( 'abcde', 'CA' ) ),
2015-04-14 13:34:35 +00:00
);
}
2015-04-08 15:56:02 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test format_postcode().
2015-04-08 15:56:02 +00:00
*
2019-04-10 23:12:27 +00:00
* @param mixed $assert Expected value.
* @param mixed $values Actual value.
*
2015-04-14 13:34:35 +00:00
* @dataProvider data_provider_test_format_postcode
2015-04-08 15:56:02 +00:00
* @since 2.4
*/
2015-04-14 13:34:35 +00:00
public function test_format_postcode( $assert, $values ) {
$this->assertEquals( $assert, $values );
2015-04-08 15:56:02 +00:00
}
2015-04-14 13:34:35 +00:00
2015-04-08 15:56:02 +00:00
/**
2015-11-03 13:31:20 +00:00
* Test format_phone().
2015-04-08 15:56:02 +00:00
*
* @since 2.4
*/
public function test_format_phone() {
$this->assertEquals( '+00-000-00-00-000', WC_Validation::format_phone( '+00.000.00.00.000' ) );
$this->assertEquals( '+00 000 00 00 000', WC_Validation::format_phone( '+00 000 00 00 000' ) );
2015-04-08 15:56:02 +00:00
}
2015-03-06 17:27:18 +00:00
}