Browse Source

Add `IntegrationTestCase::assertCookieEncrypted()` which is to compare cookies when they are encrypted.

Wataru Terada 10 years ago
parent
commit
e2cff0f2da

+ 27 - 0
src/TestSuite/IntegrationTestCase.php

@@ -838,4 +838,31 @@ abstract class IntegrationTestCase extends TestCase
         $result = $this->_response->cookie($name);
         $this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
     }
+
+    /**
+     * Asserts cookie values which are encrypted by the
+     * CookieComponent.
+     *
+     * The difference from assertCookie() is this decrypts the cookie
+     * value like the CookieComponent for this assertion.
+     *
+     * @param string $expected The expected contents.
+     * @param string $name The cookie name.
+     * @param string|bool $encrypt Encryption mode to use.
+     * @param string|null $key Encryption key used. Defaults
+     *   to Security.salt.
+     * @param string $message The failure message that will be appended to the generated message.
+     * @return void
+     * @see CookieCryptTrait::_encrypt
+     */
+    public function assertCookieEncrypted($expected, $name, $encrypt = 'aes', $key = null, $message = '')
+    {
+        if (empty($this->_response)) {
+            $this->fail('Not response set, cannot assert cookies.');
+        }
+        $result = $this->_response->cookie($name);
+        $this->_cookieEncriptionKey = $key;
+        $result['value'] = $this->_decrypt($result['value'], $encrypt);
+        $this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
+    }
 }

+ 28 - 0
tests/TestCase/TestSuite/CookieEncryptedUsingControllerTest.php

@@ -109,4 +109,32 @@ class CookieEncryptedUsingControllerTest extends IntegrationTestCase
         $this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
         $this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
     }
+
+    /**
+     * Can AssertCookie even if the value is encrypted by
+     * the CookieComponent.
+     */
+    public function testCanAssertCookieEncrypted() {
+        $this->get('/cookie_component_test/set_cookie');
+        $this->assertCookieEncrypted('abc', 'NameOfCookie');
+    }
+
+    /**
+     * Can AssertCookie even if encrypted with the aes.
+     */
+    public function testCanAssertCookieEncryptedWithAes() {
+        $this->get('/cookie_component_test/set_cookie');
+        $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes');
+    }
+
+    /**
+     * Can AssertCookie even if encrypted with the another
+     * encrypted key.
+     */
+    public function testCanAssertCookieEncryptedWithAnotherEncryptionKey() {
+        $key = 'another salt xxxxxxxxxxxxxxxxxxx';
+        Security::salt($key);
+        $this->get('/cookie_component_test/set_cookie');
+        $this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes', $key);
+    }
 }

+ 15 - 0
tests/test_app/TestApp/Controller/CookieComponentTestController.php

@@ -41,4 +41,19 @@ class CookieComponentTestController extends Controller
         $this->set('ValueFromRequest', $this->request->cookie('NameOfCookie'));
         $this->set('ValueFromCookieComponent', $this->Cookie->read('NameOfCookie'));
     }
+
+    /**
+     * action to set a cookie
+     *
+     * @param string|null $key Encryption key used. By defaults,
+     *   CookieComponent::_config['key'].
+     */
+    public function set_cookie($key = null)
+    {
+        $this->autoRender = false;
+        if (isset($key)) {
+            $this->Cookie->config('key', $key);
+        }
+        $this->Cookie->write('NameOfCookie', 'abc');
+    }
 }