SecurityTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\template\helper;
  9. use lithium\template\helper\Security;
  10. use lithium\tests\mocks\template\helper\MockFormRenderer;
  11. class SecurityTest extends \lithium\test\Unit {
  12. public $subject;
  13. public $context;
  14. public static function key($token) {
  15. return 'WORKING';
  16. }
  17. public static function hash($token) {
  18. return $token;
  19. }
  20. public function setUp() {
  21. $this->context = new MockFormRenderer(compact('request'));
  22. $this->subject = new Security(array('context' => $this->context));
  23. }
  24. /**
  25. * Tests that the helper correctly generates a security token field.
  26. */
  27. public function testRequestToken() {
  28. $result = explode(' ', $this->subject->requestToken());
  29. $this->assertEqual('<input', $result[0]);
  30. $this->assertEqual('type="hidden"', $result[1]);
  31. $this->assertEqual('name="security[token]"', $result[2]);
  32. $this->assertEqual('/>', $result[4]);
  33. $result = explode('=', $result[3]);
  34. $this->assertEqual('value', $result[0]);
  35. $result = trim($result[1], '"');
  36. $this->assertPattern('/^\$\d\w\$\d{2}\$[A-Za-z0-9\.\/]{53}$/', $result);
  37. }
  38. /**
  39. * Tests that the helper can be constructed with a custom configuration.
  40. */
  41. public function testConstruct() {
  42. $this->subject = new Security(array('context' => $this->context, 'classes' => array(
  43. 'password' => __CLASS__,
  44. 'requestToken' => __CLASS__
  45. )));
  46. $this->assertPattern('/value="WORKING"/', $this->subject->requestToken());
  47. }
  48. }
  49. ?>