/vendor/delight-im/cookie/tests/index.php

https://gitlab.com/tekoestudio/tipsy · PHP · 221 lines · 176 code · 33 blank · 12 comment · 3 complexity · 7f76ede7f49131ecd4350f183fd78ac1 MD5 · raw file

  1. <?php
  2. /*
  3. * PHP-Cookie (https://github.com/delight-im/PHP-Cookie)
  4. * Copyright (c) delight.im (https://www.delight.im/)
  5. * Licensed under the MIT License (https://opensource.org/licenses/MIT)
  6. */
  7. // enable error reporting
  8. error_reporting(E_ALL);
  9. ini_set('display_errors', 'stdout');
  10. header('Content-type: text/plain; charset=utf-8');
  11. require __DIR__.'/../vendor/autoload.php';
  12. /* BEGIN TEST COOKIES */
  13. // start output buffering
  14. ob_start();
  15. testCookie(null);
  16. testCookie(false);
  17. testCookie('');
  18. testCookie(0);
  19. testCookie('hello');
  20. testCookie('hello', false);
  21. testCookie('hello', true);
  22. testCookie('hello', null);
  23. testCookie('hello', '');
  24. testCookie('hello', 0);
  25. testCookie('hello', 1);
  26. testCookie('hello', 'world');
  27. testCookie('hello', 123);
  28. testCookie(123, 'world');
  29. testCookie('greeting', '¡Buenos días!');
  30. testCookie('¡Buenos días!', 'greeting');
  31. testCookie('%a|b}c_$d!f"g-h(i)j$', 'value value value');
  32. testCookie('%a|b}c_$d!f"g-h(i)j$', '%a|b}c_$d!f"g-h(i)j$');
  33. testCookie('hello', 'world', '!');
  34. testCookie('hello', 'world', '');
  35. testCookie('hello', 'world', false);
  36. testCookie('hello', 'world', null);
  37. testCookie('hello', 'world', true);
  38. testCookie('hello', 'world', 0);
  39. testCookie('hello', 'world', '');
  40. testCookie('hello', 'world', -1);
  41. testCookie('hello', 'world', 234234);
  42. testCookie('hello', 'world', time() + 60 * 60 * 24);
  43. testCookie('hello', 'world', time() + 60 * 60 * 24 * 30);
  44. testCookie('hello', 'world', time() + 86400, null);
  45. testCookie('hello', 'world', time() + 86400, false);
  46. testCookie('hello', 'world', time() + 86400, true);
  47. testCookie('hello', 'world', time() + 86400, 0);
  48. testCookie('hello', 'world', time() + 86400, '');
  49. testCookie('hello', 'world', time() + 86400, '/');
  50. testCookie('hello', 'world', time() + 86400, '/foo');
  51. testCookie('hello', 'world', time() + 86400, '/foo/');
  52. testCookie('hello', 'world', time() + 86400, '/buenos/días/');
  53. testCookie('hello', 'world', time() + 86400, '/buenos días/');
  54. testCookie('hello', 'world', time() + 86400, '/foo/', null);
  55. testCookie('hello', 'world', time() + 86400, '/foo/', false);
  56. testCookie('hello', 'world', time() + 86400, '/foo/', true);
  57. testCookie('hello', 'world', time() + 86400, '/foo/', 0);
  58. testCookie('hello', 'world', time() + 86400, '/foo/', '');
  59. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com');
  60. testCookie('hello', 'world', time() + 86400, '/foo/', '.example.com');
  61. testCookie('hello', 'world', time() + 86400, '/foo/', 'www.example.com');
  62. testCookie('hello', 'world', time() + 86400, '/foo/', 'días.example.com');
  63. testCookie('hello', 'world', time() + 86400, '/foo/', 'localhost');
  64. testCookie('hello', 'world', time() + 86400, '/foo/', '127.0.0.1');
  65. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', null);
  66. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false);
  67. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true);
  68. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', 0);
  69. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', '');
  70. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', 'hello');
  71. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', 7);
  72. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', -7);
  73. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, null);
  74. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, false);
  75. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, true);
  76. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, 0);
  77. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, '');
  78. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, 'hello');
  79. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, 5);
  80. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', false, -5);
  81. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, null);
  82. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, false);
  83. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, true);
  84. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, 0);
  85. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, '');
  86. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, 'hello');
  87. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, 5);
  88. testCookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, -5);
  89. testCookie('TestCookie', 'php.net');
  90. testCookie('TestCookie', 'php.net', time() + 3600);
  91. testCookie('TestCookie', 'php.net', time() + 3600, '/~rasmus/', 'example.com', 1);
  92. testCookie('TestCookie', '', time() - 3600);
  93. testCookie('TestCookie', '', time() - 3600, '/~rasmus/', 'example.com', 1);
  94. testCookie('cookie[three]', 'cookiethree');
  95. testCookie('cookie[two]', 'cookietwo');
  96. testCookie('cookie[one]', 'cookieone');
  97. testEqual((new \Delight\Cookie\Cookie('SID'))->setValue('31d4d96e407aad42')->setDomain('localhost')->setSameSiteRestriction('Strict'), 'Set-Cookie: SID=31d4d96e407aad42; path=/; httponly; SameSite=Strict');
  98. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('localhost'), 'Set-Cookie: key=value; path=/; httponly; SameSite=Lax');
  99. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('.localhost'), 'Set-Cookie: key=value; path=/; httponly; SameSite=Lax');
  100. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('127.0.0.1'), 'Set-Cookie: key=value; path=/; httponly; SameSite=Lax');
  101. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('.local'), 'Set-Cookie: key=value; path=/; httponly; SameSite=Lax');
  102. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
  103. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('.example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
  104. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('www.example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
  105. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('.www.example.com'), 'Set-Cookie: key=value; path=/; domain=.example.com; httponly; SameSite=Lax');
  106. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('www.example.com', true), 'Set-Cookie: key=value; path=/; domain=.www.example.com; httponly; SameSite=Lax');
  107. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('.www.example.com', true), 'Set-Cookie: key=value; path=/; domain=.www.example.com; httponly; SameSite=Lax');
  108. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('blog.example.com'), 'Set-Cookie: key=value; path=/; domain=.blog.example.com; httponly; SameSite=Lax');
  109. testEqual((new \Delight\Cookie\Cookie('key'))->setValue('value')->setDomain('.blog.example.com'), 'Set-Cookie: key=value; path=/; domain=.blog.example.com; httponly; SameSite=Lax');
  110. setcookie('hello', 'world', time() + 86400, '/foo/', 'example.com', true, true);
  111. testEqual(\Delight\Cookie\Cookie::parse(\Delight\Http\ResponseHeader::take('Set-Cookie')), (new \Delight\Cookie\Cookie('hello'))->setValue('world')->setMaxAge(86400)->setPath('/foo/')->setDomain('example.com')->setHttpOnly(true)->setSecureOnly(true));
  112. /* END TEST COOKIES */
  113. /* BEGIN TEST SESSION */
  114. // enable assertions
  115. ini_set('assert.active', 1);
  116. ini_set('zend.assertions', 1);
  117. ini_set('assert.exception', 1);
  118. assert(isset($_SESSION) === false);
  119. assert(\Delight\Cookie\Session::id() === '');
  120. \Delight\Cookie\Session::start();
  121. assert(isset($_SESSION) === true);
  122. assert(\Delight\Cookie\Session::id() !== '');
  123. $oldSessionId = \Delight\Cookie\Session::id();
  124. \Delight\Cookie\Session::regenerate();
  125. assert(\Delight\Cookie\Session::id() !== $oldSessionId);
  126. assert(\Delight\Cookie\Session::id() !== null);
  127. session_unset();
  128. assert(isset($_SESSION['key1']) === false);
  129. assert(\Delight\Cookie\Session::has('key1') === false);
  130. assert(\Delight\Cookie\Session::get('key1') === null);
  131. assert(\Delight\Cookie\Session::get('key1', 5) === 5);
  132. assert(\Delight\Cookie\Session::get('key1', 'monkey') === 'monkey');
  133. \Delight\Cookie\Session::set('key1', 'value1');
  134. assert(isset($_SESSION['key1']) === true);
  135. assert(\Delight\Cookie\Session::has('key1') === true);
  136. assert(\Delight\Cookie\Session::get('key1') === 'value1');
  137. assert(\Delight\Cookie\Session::get('key1', 5) === 'value1');
  138. assert(\Delight\Cookie\Session::get('key1', 'monkey') === 'value1');
  139. assert(\Delight\Cookie\Session::take('key1') === 'value1');
  140. assert(\Delight\Cookie\Session::take('key1') === null);
  141. assert(\Delight\Cookie\Session::take('key1', 'value2') === 'value2');
  142. assert(isset($_SESSION['key1']) === false);
  143. assert(\Delight\Cookie\Session::has('key1') === false);
  144. \Delight\Cookie\Session::set('key2', 'value3');
  145. assert(isset($_SESSION['key2']) === true);
  146. assert(\Delight\Cookie\Session::has('key2') === true);
  147. assert(\Delight\Cookie\Session::get('key2', 'value4') === 'value3');
  148. \Delight\Cookie\Session::delete('key2');
  149. assert(\Delight\Cookie\Session::get('key2', 'value4') === 'value4');
  150. assert(\Delight\Cookie\Session::get('key2') === null);
  151. assert(\Delight\Cookie\Session::has('key2') === false);
  152. session_destroy();
  153. /* END TEST SESSION */
  154. echo 'ALL TESTS PASSED'."\n";
  155. function testCookie($name, $value = null, $expire = 0, $path = null, $domain = null, $secure = false, $httpOnly = false) {
  156. $actualValue = \Delight\Cookie\Cookie::buildCookieHeader($name, $value, $expire, $path, $domain, $secure, $httpOnly);
  157. if (is_null($actualValue)) {
  158. $expectedValue = @simulateSetCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
  159. }
  160. else {
  161. $expectedValue = simulateSetCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
  162. }
  163. testEqual($actualValue, $expectedValue);
  164. }
  165. function testEqual($actualValue, $expectedValue) {
  166. $actualValue = (string) $actualValue;
  167. $expectedValue = (string) $expectedValue;
  168. echo '[';
  169. echo $expectedValue;
  170. echo ']';
  171. echo "\n";
  172. if (strcasecmp($actualValue, $expectedValue) !== 0) {
  173. echo 'FAILED: ';
  174. echo '[';
  175. echo $actualValue;
  176. echo ']';
  177. echo ' !== ';
  178. echo '[';
  179. echo $expectedValue;
  180. echo ']';
  181. echo "\n";
  182. exit;
  183. }
  184. }
  185. function simulateSetCookie($name, $value = null, $expire = 0, $path = null, $domain = null, $secure = false, $httpOnly = false) {
  186. setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
  187. return \Delight\Http\ResponseHeader::take('Set-Cookie');
  188. }