| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP Project
- * @since 3.1.6
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Controller;
- use Cake\Controller\Component;
- use Cake\Core\Configure;
- use Cake\Routing\DispatcherFactory;
- use Cake\Routing\Router;
- use Cake\TestSuite\IntegrationTestCase;
- use Cake\Utility\Security;
- /**
- * CookieComponentControllerTest class
- */
- class CookieComponentControllerTest extends IntegrationTestCase
- {
- /**
- * reset environment.
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- Configure::write('App.namespace', 'TestApp');
- Security::salt('abcdabcdabcdabcdabcdabcdabcdabcdabcd');
- Router::connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);
- DispatcherFactory::clear();
- DispatcherFactory::add('Routing');
- DispatcherFactory::add('ControllerFactory');
- }
- /**
- * Can encrypt/decrypt the cookie value.
- */
- public function testCanEncryptAndDecryptWithAes()
- {
- $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes');
- $this->get('/cookie_component_test/view/');
- $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
- $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
- }
- /**
- * Can encrypt/decrypt the cookie value by default.
- */
- public function testCanEncryptAndDecryptCookieValue()
- {
- $this->cookieEncrypted('NameOfCookie', 'Value of Cookie');
- $this->get('/cookie_component_test/view/');
- $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
- $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
- }
- /**
- * Can encrypt/decrypt even if the cookie value are array.
- */
- public function testCanEncryptAndDecryptEvenIfCookieValueIsArray()
- {
- $this->cookieEncrypted('NameOfCookie', ['Value1 of Cookie', 'Value2 of Cookie']);
- $this->get('/cookie_component_test/view/');
- $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
- $this->assertEquals(
- ['Value1 of Cookie', 'Value2 of Cookie'],
- $this->viewVariable('ValueFromCookieComponent'),
- 'Decrypted'
- );
- }
- /**
- * @return array
- */
- public function encryptionKeyCases()
- {
- return [
- ['foo!foo!foo!foo!foo!foo!foo!foo!'],
- ['bar!bar!bar!bar!bar!bar!bar!bar!'],
- ];
- }
- /**
- * Can specify the encryption key.
- *
- * @dataProvider encryptionKeyCases
- * @param string $key Encryption Key
- */
- public function testCanSpecifyEncryptionKey($key)
- {
- $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes', $key);
- $this->get('/cookie_component_test/view/' . urlencode($key));
- $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
- $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
- }
- /**
- * Can be used Security::salt() as the encryption key.
- *
- * @dataProvider encryptionKeyCases
- * @param string $key Encryption Key
- */
- public function testCanBeUsedSecuritySaltAsEncryptionKey($key)
- {
- Security::salt($key);
- $this->cookieEncrypted('NameOfCookie', 'Value of Cookie', 'aes');
- $this->get('/cookie_component_test/view/' . urlencode($key));
- $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
- $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
- }
- }
|