CookieComponentControllerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Controller\Component;
  17. use Cake\Core\Configure;
  18. use Cake\Routing\DispatcherFactory;
  19. use Cake\Routing\Router;
  20. use Cake\TestSuite\IntegrationTestCase;
  21. use Cake\Utility\Security;
  22. /**
  23. * CookieComponentControllerTest class
  24. */
  25. class CookieComponentControllerTest extends IntegrationTestCase
  26. {
  27. /**
  28. * reset environment.
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. Configure::write('App.namespace', 'TestApp');
  36. Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
  37. Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
  38. DispatcherFactory::clear();
  39. DispatcherFactory::add('Routing');
  40. DispatcherFactory::add('ControllerFactory');
  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. * @return array
  78. */
  79. public function encryptionKeyCases()
  80. {
  81. return [
  82. ['foo!foo!foo!foo!foo!foo!foo!foo!'],
  83. ['bar!bar!bar!bar!bar!bar!bar!bar!'],
  84. ];
  85. }
  86. /**
  87. * Can specify the encryption key.
  88. *
  89. * @dataProvider encryptionKeyCases
  90. * @param string $key Encryption Key
  91. */
  92. public function testCanSpecifyEncryptionKey($key)
  93. {
  94. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes', $key);
  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 be used Security::salt() as the encryption key.
  101. *
  102. * @dataProvider encryptionKeyCases
  103. * @param string $key Encryption Key
  104. */
  105. public function testCanBeUsedSecuritySaltAsEncryptionKey($key)
  106. {
  107. Security::salt($key);
  108. $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes');
  109. $this->get('/cookie_component_test/view/' . urlencode($key));
  110. $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
  111. $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
  112. }
  113. }