CookieTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
  2. /**
  3. * Tests the cookie class
  4. *
  5. * @group kohana
  6. * @group kohana.core
  7. * @group kohana.core.cookie
  8. *
  9. * @package Kohana
  10. * @category Tests
  11. * @author Kohana Team
  12. * @author Jeremy Bush <[email protected]>
  13. * @copyright (c) 2008-2012 Kohana Team
  14. * @license http://kohanaframework.org/license
  15. */
  16. class Kohana_CookieTest extends Unittest_TestCase
  17. {
  18. protected $_default_salt = 'AdaoidadnA£ASDNadnaoiwdnawd';
  19. /**
  20. * Sets up the environment
  21. */
  22. // @codingStandardsIgnoreStart
  23. public function setUp()
  24. // @codingStandardsIgnoreEnd
  25. {
  26. parent::setUp();
  27. Cookie::$salt = $this->_default_salt;
  28. }
  29. /**
  30. * Tears down the environment
  31. */
  32. // @codingStandardsIgnoreStart
  33. public function tearDown()
  34. // @codingStandardsIgnoreEnd
  35. {
  36. parent::tearDown();
  37. Cookie::$salt = NULL;
  38. }
  39. /**
  40. * Provides test data for test_set()
  41. *
  42. * @return array
  43. */
  44. public function provider_set()
  45. {
  46. return array(
  47. array('foo', 'bar', NULL, TRUE),
  48. array('foo', 'bar', 10, TRUE),
  49. );
  50. }
  51. /**
  52. * Tests cookie::set()
  53. *
  54. * @test
  55. * @dataProvider provider_set
  56. * @covers cookie::set
  57. * @param mixed $key key to use
  58. * @param mixed $value value to set
  59. * @param mixed $exp exp to set
  60. * @param boolean $expected Output for cookie::set()
  61. */
  62. public function test_set($key, $value, $exp, $expected)
  63. {
  64. if (headers_sent()) {
  65. $this->markTestSkipped('Cannot test setting cookies as headers have already been sent');
  66. }
  67. $this->assertSame($expected, cookie::set($key, $value, $exp));
  68. }
  69. /**
  70. * Provides test data for test_get()
  71. *
  72. * @return array
  73. */
  74. public function provider_get()
  75. {
  76. // setUp is called after the provider so we need to specify a
  77. // salt here in order to use it in the provider
  78. Cookie::$salt = $this->_default_salt;
  79. return array(
  80. array('foo', Cookie::salt('foo', 'bar').'~bar', 'bar'),
  81. array('bar', Cookie::salt('foo', 'bar').'~bar', NULL),
  82. array(NULL, Cookie::salt('foo', 'bar').'~bar', NULL),
  83. );
  84. }
  85. /**
  86. * Tests cookie::set()
  87. *
  88. * @test
  89. * @dataProvider provider_get
  90. * @covers cookie::get
  91. * @param mixed $key key to use
  92. * @param mixed $value value to set
  93. * @param boolean $expected Output for cookie::get()
  94. */
  95. public function test_get($key, $value, $expected)
  96. {
  97. if (headers_sent()) {
  98. $this->markTestSkipped('Cannot test setting cookies as headers have already been sent');
  99. }
  100. // Force $_COOKIE
  101. if ($key !== NULL)
  102. {
  103. $_COOKIE[$key] = $value;
  104. }
  105. $this->assertSame($expected, cookie::get($key));
  106. }
  107. /**
  108. * Provides test data for test_delete()
  109. *
  110. * @return array
  111. */
  112. public function provider_delete()
  113. {
  114. return array(
  115. array('foo', TRUE),
  116. );
  117. }
  118. /**
  119. * Tests cookie::delete()
  120. *
  121. * @test
  122. * @dataProvider provider_delete
  123. * @covers cookie::delete
  124. * @param mixed $key key to use
  125. * @param boolean $expected Output for cookie::delete()
  126. */
  127. public function test_delete($key, $expected)
  128. {
  129. if (headers_sent()) {
  130. $this->markTestSkipped('Cannot test setting cookies as headers have already been sent');
  131. }
  132. $this->assertSame($expected, cookie::delete($key));
  133. }
  134. /**
  135. * Provides test data for test_salt()
  136. *
  137. * @return array
  138. */
  139. public function provider_salt()
  140. {
  141. return array(
  142. array('foo', 'bar', 'b5773a6255d1deefc23f9f69bcc40fdc998e5802'),
  143. );
  144. }
  145. /**
  146. * Tests cookie::salt()
  147. *
  148. * @test
  149. * @dataProvider provider_salt
  150. * @covers cookie::salt
  151. * @param mixed $key key to use
  152. * @param mixed $value value to salt with
  153. * @param boolean $expected Output for cookie::delete()
  154. */
  155. public function test_salt($key, $value, $expected)
  156. {
  157. $this->assertSame($expected, cookie::salt($key, $value));
  158. }
  159. }