1
0

BxDolFormCheckerHelperTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. /**
  4. * Form field validators (no captcha, location, or spam checks).
  5. */
  6. class BxDolFormCheckerHelperTest extends \PHPUnit\Framework\TestCase
  7. {
  8. public static function setUpBeforeClass(): void
  9. {
  10. if (!class_exists('BxDolFormCheckerHelper', false))
  11. bx_import('BxDolForm');
  12. }
  13. #[DataProvider('providerForCheckEmail')]
  14. public function testCheckEmail($s, $bOut)
  15. {
  16. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkEmail($s));
  17. }
  18. static public function providerForCheckEmail()
  19. {
  20. return [
  21. ['user@example.com', true],
  22. ['тест@example.com', true],
  23. ['user.name@example.com', true],
  24. ['user_name@example.com', true],
  25. ['', false],
  26. ['no-at', false],
  27. ['a@b@c.com', false],
  28. ['.user@example.com', false],
  29. ['user@exam_ple.com', false],
  30. ];
  31. }
  32. public function testCheckEmailPlusAddressing()
  33. {
  34. $sKey = 'sys_account_allow_plus_in_email';
  35. $oProp = new ReflectionProperty(BxDolDb::class, '_aParams');
  36. $aParams = $oProp->getValue();
  37. $sOld = $aParams[$sKey] ?? '';
  38. try {
  39. $aParams[$sKey] = 'on';
  40. $oProp->setValue(null, $aParams);
  41. $this->assertTrue((bool)BxDolFormCheckerHelper::checkEmail('user+tag@example.com'));
  42. $aParams[$sKey] = '';
  43. $oProp->setValue(null, $aParams);
  44. $this->assertFalse((bool)BxDolFormCheckerHelper::checkEmail('user+tag@example.com'));
  45. } finally {
  46. $aParams[$sKey] = $sOld;
  47. $oProp->setValue(null, $aParams);
  48. }
  49. }
  50. public function testCheckEmailOrEmpty()
  51. {
  52. $this->assertTrue((bool)BxDolFormCheckerHelper::checkEmailOrEmpty(''));
  53. $this->assertTrue((bool)BxDolFormCheckerHelper::checkEmailOrEmpty('user@example.com'));
  54. $this->assertFalse((bool)BxDolFormCheckerHelper::checkEmailOrEmpty('no-at'));
  55. }
  56. #[DataProvider('providerForCheckLength')]
  57. public function testCheckLength($mixed, $iMin, $iMax, $bOut)
  58. {
  59. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkLength($mixed, $iMin, $iMax));
  60. }
  61. static public function providerForCheckLength()
  62. {
  63. return [
  64. ['ab', 1, 3, true],
  65. ['abcd', 1, 3, false],
  66. ['', 1, 3, false],
  67. ['a', 1, 1, true],
  68. [['ab', 'cd'], 1, 3, true],
  69. [['abcd'], 1, 3, false],
  70. ['тест', 1, 4, true],
  71. ['тест', 1, 3, false],
  72. ];
  73. }
  74. #[DataProvider('providerForCheckPreg')]
  75. public function testCheckPreg($mixed, $sRegex, $bOut)
  76. {
  77. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkPreg($mixed, $sRegex));
  78. }
  79. static public function providerForCheckPreg()
  80. {
  81. return [
  82. ['abc', '#^ab#', true],
  83. ['x', '#^ab#', false],
  84. [['abc', 'abd'], '#^ab#', true],
  85. [['abc', 'x'], '#^ab#', false],
  86. ];
  87. }
  88. #[DataProvider('providerForCheckAvail')]
  89. public function testCheckAvail($mixed, $bOut)
  90. {
  91. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkAvail($mixed));
  92. }
  93. static public function providerForCheckAvail()
  94. {
  95. return [
  96. ['x', true],
  97. ['', false],
  98. ['0', false],
  99. [[], false],
  100. [['x'], true],
  101. [[''], false],
  102. ];
  103. }
  104. #[DataProvider('providerForCheckJson')]
  105. public function testCheckJson($mixed, $bAllowEmpty, $bOut)
  106. {
  107. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkJson($mixed, $bAllowEmpty));
  108. }
  109. static public function providerForCheckJson()
  110. {
  111. return [
  112. ['{"a":1}', false, true],
  113. ['[]', false, true],
  114. ['null', false, true],
  115. ['not json', false, false],
  116. ['', false, false],
  117. ['', true, true],
  118. ];
  119. }
  120. #[DataProvider('providerForCheckDate')]
  121. public function testCheckDate($s, $bOut)
  122. {
  123. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkDate($s));
  124. }
  125. static public function providerForCheckDate()
  126. {
  127. return [
  128. ['2024-01-15', true],
  129. ['2024/01/15', false],
  130. ['15-01-2024', true],
  131. ['not-a-date', false],
  132. ['', false],
  133. ];
  134. }
  135. #[DataProvider('providerForCheckDateTime')]
  136. public function testCheckDateTime($s, $bOut)
  137. {
  138. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkDateTime($s));
  139. }
  140. static public function providerForCheckDateTime()
  141. {
  142. return [
  143. ['2024-01-15 10:30', true],
  144. ['2024-01-15 10:30:00', true],
  145. ['2024-01-15T10:30:00', true],
  146. ['2024-01-15', false],
  147. ['not-a-date', false],
  148. ];
  149. }
  150. #[DataProvider('providerForCheckProfileName')]
  151. public function testCheckProfileName($s, $bOut)
  152. {
  153. $this->assertSame($bOut, (bool)BxDolFormCheckerHelper::checkProfileName($s));
  154. }
  155. static public function providerForCheckProfileName()
  156. {
  157. return [
  158. ['Ada', true],
  159. ['Ada Lovelace', true],
  160. ['', false],
  161. ['@handle', false],
  162. ];
  163. }
  164. }