FilterTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../TestHelper.php';
  26. /**
  27. * @see Zend_Filter
  28. */
  29. require_once 'Zend/Filter.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Filter
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Filter
  37. */
  38. class Zend_FilterTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Zend_Filter object
  42. *
  43. * @var Zend_Filter
  44. */
  45. protected $_filter;
  46. /**
  47. * Creates a new Zend_Filter object for each test method
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. $this->error = null;
  54. $this->_filter = new Zend_Filter();
  55. }
  56. /**
  57. * Resets the default namespaces
  58. *
  59. * @return void
  60. */
  61. public function tearDown()
  62. {
  63. Zend_Filter::setDefaultNamespaces(array());
  64. }
  65. /**
  66. * Ensures expected return value from empty filter chain
  67. *
  68. * @return void
  69. */
  70. public function testEmpty()
  71. {
  72. $value = 'something';
  73. $this->assertEquals($value, $this->_filter->filter($value));
  74. }
  75. /**
  76. * Ensures that filters are executed in the expected order (FIFO)
  77. *
  78. * @return void
  79. */
  80. public function testFilterOrder()
  81. {
  82. $this->_filter->addFilter(new Zend_FilterTest_LowerCase())
  83. ->addFilter(new Zend_FilterTest_StripUpperCase());
  84. $value = 'AbC';
  85. $valueExpected = 'abc';
  86. $this->assertEquals($valueExpected, $this->_filter->filter($value));
  87. }
  88. /**
  89. * Ensures that filters can be prepended and will be executed in the
  90. * expected order
  91. */
  92. public function testFilterPrependOrder()
  93. {
  94. $this->_filter->appendFilter(new Zend_FilterTest_StripUpperCase())
  95. ->prependFilter(new Zend_FilterTest_LowerCase());
  96. $value = 'AbC';
  97. $valueExpected = 'abc';
  98. $this->assertEquals($valueExpected, $this->_filter->filter($value));
  99. }
  100. /**
  101. * Ensures that we can call the static method get()
  102. * to instantiate a named validator by its class basename
  103. * and it returns the result of filter() with the input.
  104. */
  105. public function testStaticFactory()
  106. {
  107. $filteredValue = Zend_Filter::filterStatic('1a2b3c4d', 'Digits');
  108. $this->assertEquals('1234', $filteredValue);
  109. }
  110. /**
  111. * Ensures that a validator with constructor arguments can be called
  112. * with the static method get().
  113. */
  114. public function testStaticFactoryWithConstructorArguments()
  115. {
  116. // Test HtmlEntities with one ctor argument.
  117. $filteredValue = Zend_Filter::filterStatic('"O\'Reilly"', 'HtmlEntities', array(array('quotestyle' => ENT_COMPAT)));
  118. $this->assertEquals('&quot;O\'Reilly&quot;', $filteredValue);
  119. // Test HtmlEntities with a different ctor argument,
  120. // and make sure it gives the correct response
  121. // so we know it passed the arg to the ctor.
  122. $filteredValue = Zend_Filter::filterStatic('"O\'Reilly"', 'HtmlEntities', array(array('quotestyle' => ENT_QUOTES)));
  123. $this->assertEquals('&quot;O&#039;Reilly&quot;', $filteredValue);
  124. }
  125. /**
  126. * Ensures that if we specify a validator class basename that doesn't
  127. * exist in the namespace, get() throws an exception.
  128. *
  129. * Refactored to conform with ZF-2724.
  130. *
  131. * @group ZF-2724
  132. * @return void
  133. */
  134. public function testStaticFactoryClassNotFound()
  135. {
  136. set_error_handler(array($this, 'handleNotFoundError'), E_WARNING);
  137. try {
  138. Zend_Filter::filterStatic('1234', 'UnknownFilter');
  139. } catch (Zend_Filter_Exception $e) {
  140. }
  141. restore_error_handler();
  142. $this->assertTrue($this->error);
  143. $this->assertTrue(isset($e));
  144. $this->assertContains('Filter class not found', $e->getMessage());
  145. }
  146. /**
  147. * Handle file not found errors
  148. *
  149. * @group ZF-2724
  150. * @param int $errnum
  151. * @param string $errstr
  152. * @return void
  153. */
  154. public function handleNotFoundError($errnum, $errstr)
  155. {
  156. if (strstr($errstr, 'No such file')) {
  157. $this->error = true;
  158. }
  159. }
  160. /**
  161. * Testing Namespaces
  162. *
  163. * @return void
  164. */
  165. public function testNamespaces()
  166. {
  167. $this->assertEquals(array(), Zend_Filter::getDefaultNamespaces());
  168. $this->assertFalse(Zend_Filter::hasDefaultNamespaces());
  169. Zend_Filter::setDefaultNamespaces('TestDir');
  170. $this->assertEquals(array('TestDir'), Zend_Filter::getDefaultNamespaces());
  171. Zend_Filter::setDefaultNamespaces('OtherTestDir');
  172. $this->assertEquals(array('OtherTestDir'), Zend_Filter::getDefaultNamespaces());
  173. $this->assertTrue(Zend_Filter::hasDefaultNamespaces());
  174. Zend_Filter::setDefaultNamespaces(array());
  175. $this->assertEquals(array(), Zend_Filter::getDefaultNamespaces());
  176. $this->assertFalse(Zend_Filter::hasDefaultNamespaces());
  177. Zend_Filter::addDefaultNamespaces(array('One', 'Two'));
  178. $this->assertEquals(array('One', 'Two'), Zend_Filter::getDefaultNamespaces());
  179. Zend_Filter::addDefaultNamespaces('Three');
  180. $this->assertEquals(array('One', 'Two', 'Three'), Zend_Filter::getDefaultNamespaces());
  181. Zend_Filter::setDefaultNamespaces(array());
  182. }
  183. /**
  184. * ZF-2105
  185. */
  186. public function testUsageOfOldStaticFactory()
  187. {
  188. set_error_handler(array($this, 'errorHandlerIgnore'));
  189. $filteredValue = Zend_Filter::get('1a2b3c4d', 'Digits');
  190. $this->assertEquals('1234', $filteredValue);
  191. restore_error_handler();
  192. }
  193. /**
  194. * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
  195. *
  196. * @param integer $errno
  197. * @param string $errstr
  198. * @param string $errfile
  199. * @param integer $errline
  200. * @param array $errcontext
  201. * @return void
  202. */
  203. public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $errcontext)
  204. {
  205. $this->_errorOccurred = true;
  206. }
  207. }
  208. class Zend_FilterTest_LowerCase implements Zend_Filter_Interface
  209. {
  210. public function filter($value)
  211. {
  212. return strtolower($value);
  213. }
  214. }
  215. class Zend_FilterTest_StripUpperCase implements Zend_Filter_Interface
  216. {
  217. public function filter($value)
  218. {
  219. return preg_replace('/[A-Z]/', '', $value);
  220. }
  221. }