CookieEncryptedUsingControllerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 3.1.6
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller;
  16. use Cake\Core\Configure;
  17. use Cake\Routing\DispatcherFactory;
  18. use Cake\Routing\Router;
  19. use Cake\TestSuite\IntegrationTestCase;
  20. use Cake\Utility\Security;
  21. /**
  22. * CookieEncryptedUsingControllerTest class
  23. */
  24. class CookieEncryptedUsingControllerTest extends IntegrationTestCase
  25. {
  26. /**
  27. * reset environment.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. Configure::write('App.namespace', 'TestApp');
  35. Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  36. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  37. DispatcherFactory::clear();
  38. DispatcherFactory::add('Routing');
  39. DispatcherFactory::add('ControllerFactory');
  40. $this->useHttpServer(false);
  41. }
  42. /**
  43. * Can encrypt/decrypt the cookie value.
  44. */
  45. public function testCanEncryptAndDecryptWithAes()
  46. {
  47. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes');
  48. $this->get('/cookie_component_test/view/');
  49. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  50. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  51. }
  52. /**
  53. * Can encrypt/decrypt the cookie value by default.
  54. */
  55. public function testCanEncryptAndDecryptCookieValue()
  56. {
  57. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie');
  58. $this->get('/cookie_component_test/view/');
  59. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  60. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  61. }
  62. /**
  63. * Can encrypt/decrypt even if the cookie value are array.
  64. */
  65. public function testCanEncryptAndDecryptEvenIfCookieValueIsArray()
  66. {
  67. $this->cookieEncrypted('NameOfCookie', ['Value1 of Cookie', 'Value2 of Cookie']);
  68. $this->get('/cookie_component_test/view/');
  69. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  70. $this->assertEquals(
  71. ['Value1 of Cookie', 'Value2 of Cookie'],
  72. $this->viewVariable('ValueFromCookieComponent'),
  73. 'Decrypted'
  74. );
  75. }
  76. /**
  77. * Can specify the encryption key.
  78. */
  79. public function testCanSpecifyEncryptionKey()
  80. {
  81. $key = 'another salt xxxxxxxxxxxxxxxxxxx';
  82. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes', $key);
  83. $this->get('/cookie_component_test/view/' . urlencode($key));
  84. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  85. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  86. }
  87. /**
  88. * Can be used Security::salt() as the encryption key.
  89. */
  90. public function testCanBeUsedSecuritySaltAsEncryptionKey()
  91. {
  92. $key = 'another salt xxxxxxxxxxxxxxxxxxx';
  93. Security::salt($key);
  94. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes');
  95. $this->get('/cookie_component_test/view/' . urlencode($key));
  96. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  97. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  98. }
  99. /**
  100. * Can AssertCookie even if the value is encrypted by
  101. * the CookieComponent.
  102. */
  103. public function testCanAssertCookieEncrypted()
  104. {
  105. $this->get('/cookie_component_test/set_cookie');
  106. $this->assertCookieEncrypted('abc', 'NameOfCookie');
  107. }
  108. /**
  109. * Can AssertCookie even if encrypted with the aes.
  110. */
  111. public function testCanAssertCookieEncryptedWithAes()
  112. {
  113. $this->get('/cookie_component_test/set_cookie');
  114. $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes');
  115. }
  116. /**
  117. * Can AssertCookie even if encrypted with the another
  118. * encrypted key.
  119. */
  120. public function testCanAssertCookieEncryptedWithAnotherEncryptionKey()
  121. {
  122. $key = 'another salt xxxxxxxxxxxxxxxxxxx';
  123. Security::salt($key);
  124. $this->get('/cookie_component_test/set_cookie');
  125. $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes', $key);
  126. }
  127. /**
  128. * Can AssertCookie even if encrypted with the aes when using PSR7 server.
  129. */
  130. public function testCanAssertCookieEncryptedWithAesWhenUsingPsr7()
  131. {
  132. $this->useHttpServer(true);
  133. $this->get('/cookie_component_test/set_cookie');
  134. $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes');
  135. }
  136. }