PageRenderTime 148ms CodeModel.GetById 67ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/InputFilterTest.php

http://github.com/ccampbell/sonic
PHP | 139 lines | 107 code | 22 blank | 10 comment | 0 complexity | 65e46160605128448be1b4e31b857905 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. use Sonic\UnitTest\TestCase;
  3. use Sonic\Request;
  4. use Sonic\InputFilter;
  5. class InputFilterTest extends TestCase
  6. {
  7. private function _filter($name)
  8. {
  9. $filter = new InputFilter(new Request());
  10. return $filter->filter($name);
  11. }
  12. public function testConstruct()
  13. {
  14. $filter = new InputFilter(new Request());
  15. $this->isTrue($filter instanceof InputFilter);
  16. }
  17. public function testHtml()
  18. {
  19. $_GET['message'] = '<p>This is my message</p>';
  20. $message = $this->_filter('message')->setType('string')->from(Request::GET);
  21. // tags should be stripped
  22. $this->isEqual($message, 'This is my message');
  23. // tags should be allowed
  24. $message = $this->_filter('message')->setType('string')->allowHtml()->from(Request::GET);
  25. $this->isEqual($message, $_GET['message']);
  26. }
  27. public function testTrim()
  28. {
  29. // strings should be trimmed
  30. $_GET['message'] = ' Woah, cool ' . "\n\n";
  31. $message = $this->_filter('message')->setType('string')->from(Request::GET);
  32. $this->isEqual($message, 'Woah, cool');
  33. // don't trim
  34. $message = $this->_filter('message')->setType('string')->noTrim()->from(Request::GET);
  35. $this->isEqual($message, $_GET['message']);
  36. }
  37. public function testDefault()
  38. {
  39. // check default value
  40. $page = $this->_filter('page')->setType('int')->setDefault(1)->from(Request::GET);
  41. $this->isEqual($page, 1);
  42. $_GET['page'] = 5;
  43. $page = $this->_filter('page')->setType('int')->setDefault(1)->from(Request::GET);
  44. $this->isEqual($page, 5);
  45. }
  46. public function testNoDefault()
  47. {
  48. // check no arg with no default value
  49. unset($_POST['brand']);
  50. $brand = $this->_filter('brand')->setType('string')->from(Request::POST);
  51. $this->isNull($brand);
  52. }
  53. public function testArray()
  54. {
  55. // check for value as an array
  56. $_GET['values'][] = 0;
  57. $_GET['values'][] = 1;
  58. $_GET['values'][] = 2;
  59. $values = $this->_filter('values')->setType('array')->from(Request::GET);
  60. $this->isArray($values);
  61. $this->isEqual($values, array(0,1,2));
  62. }
  63. public function testInArray()
  64. {
  65. // check that a value is in an array
  66. $_POST['brand'] = 'BMW';
  67. $brand = $this->_filter('brand')->setType('string')->in(array('BMW', 'Mazda', 'Lexus', 'Mercedes'))->from(Request::POST);
  68. $this->isEqual($brand, 'BMW');
  69. // check with value not in array
  70. $_POST['brand'] = 'Toyota';
  71. $brand = $this->_filter('brand')->setType('string')->in(array('BMW', 'Mazda', 'Lexus', 'Mercedes'))->from(Request::POST);
  72. $this->isEqual($brand, '');
  73. }
  74. public function testBoolean()
  75. {
  76. $cool = $this->_filter('is_cool')->setType('bool')->from(Request::GET);
  77. $this->isFalse($cool);
  78. $this->isEqual($cool, null);
  79. $_GET['is_cool'] = 1;
  80. $cool = $this->_filter('is_cool')->setType('bool')->from(Request::GET);
  81. $this->isTrue($cool);
  82. $this->isEqual($cool, true);
  83. unset($_GET['is_cool']);
  84. $cool = $this->_filter('is_cool')->setType('bool')->setDefault(false)->from(Request::GET);
  85. $this->isFalse($cool);
  86. $this->isEqual($cool, false);
  87. $_GET['is_cool'] = 'false';
  88. $cool = $this->_filter('is_cool')->setType('bool')->from(Request::GET);
  89. $this->isFalse($cool);
  90. $this->isEqual($cool, false);
  91. $_GET['mode'] = 'blah';
  92. $mode = $this->_filter('mode')->setType('bool')->in(array('test'))->setDefault(false)->getValue(Request::GET);
  93. $this->isFalse($mode);
  94. }
  95. public function testHex()
  96. {
  97. $_GET['md5'] = md5('this is an md5');
  98. $value = $this->_filter('md5')->setType('hex')->from(Request::GET);
  99. $this->isEqual($_GET['md5'], $value);
  100. $_GET['md5'] = 'string';
  101. $value = $this->_filter('md5')->setType('hex')->from(Request::GET);
  102. $this->isNull($value);
  103. // numbers still pass hex
  104. $_GET['number'] = 1234;
  105. $value = $this->_filter('number')->setType('hex')->from(Request::GET);
  106. }
  107. public function testCustomFunctions()
  108. {
  109. $_POST['var'] = 'there are spaces';
  110. $var = $this->_filter('var')->setType('string')->addFunction(function ($var) {
  111. return str_replace(' ', '_', $var);
  112. })->addFunction(function ($var) {
  113. return substr($var, 0, 9);
  114. })->from(Request::POST);
  115. $this->isEqual($var, 'there_are');
  116. }
  117. }