Merge pull request #6877 from saurabhbatra96/comment-fixes-utils
[civicrm-core.git] / tests / phpunit / CRM / Utils / TypeTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4
5 /**
6 * Class CRM_Utils_TypeTest
7 */
8 class CRM_Utils_TypeTest extends CiviUnitTestCase {
9
10 public function setUp() {
11 parent::setUp();
12 }
13
14 /**
15 * @dataProvider validateDataProvider
16 * @param $inputData
17 * @param $inputType
18 * @param $expectedResult
19 */
20 public function testValidate($inputData, $inputType, $expectedResult) {
21 $this->assertTrue($expectedResult === CRM_Utils_Type::validate($inputData, $inputType, FALSE));
22 }
23
24 /**
25 * @return array
26 */
27 public function validateDataProvider() {
28 return array(
29 array(10, 'Int', 10),
30 array('145E+3', 'Int', NULL),
31 array('10', 'Integer', 10),
32 array(-10, 'Int', -10),
33 array('-10', 'Integer', -10),
34 array('-10foo', 'Int', NULL),
35 array(10, 'Positive', 10),
36 array('145.0E+3', 'Positive', NULL),
37 array('10', 'Positive', 10),
38 array(-10, 'Positive', NULL),
39 array('-10', 'Positive', NULL),
40 array('-10foo', 'Positive', NULL),
41 );
42 }
43
44 /**
45 * @dataProvider escapeDataProvider
46 * @param $inputData
47 * @param $inputType
48 * @param $expectedResult
49 */
50 public function testEscape($inputData, $inputType, $expectedResult) {
51 $this->assertTrue($expectedResult === CRM_Utils_Type::escape($inputData, $inputType, FALSE));
52 }
53
54 /**
55 * @return array
56 */
57 public function escapeDataProvider() {
58 return array(
59 array(10, 'Int', 10),
60 array('145E+3', 'Int', NULL),
61 array('10', 'Integer', 10),
62 array(-10, 'Int', -10),
63 array(array(), 'Integer', NULL),
64 array('-10foo', 'Int', NULL),
65 array(10, 'Positive', 10),
66 array('145.0E+3', 'Positive', NULL),
67 array('10', 'Positive', 10),
68 array(-10, 'Positive', NULL),
69 array('-10', 'Positive', NULL),
70 array('-10foo', 'Positive', NULL),
71 array(array('10', 20), 'Country', array('10', 20)),
72 array(array('10', '-10foo'), 'Country', NULL),
73 array('', 'Timestamp', ''),
74 array('', 'ContactReference', ''),
75 array('3', 'ContactReference', 3),
76 array('-3', 'ContactReference', NULL),
77 // Escape function is meant for sql, not xss
78 array('<p onclick="alert(\'xss\');">Hello</p>', 'Memo', '<p onclick=\\"alert(\\\'xss\\\');\\">Hello</p>'),
79 );
80 }
81
82 }