Browse Source

Merge pull request #5543 from cakephp/3.0-session-consume-delete

3.0: Session::clear()
Mark Story 11 years ago
parent
commit
ae19757218
2 changed files with 30 additions and 5 deletions
  1. 9 4
      src/Network/Session.php
  2. 21 1
      tests/TestCase/Network/SessionTest.php

+ 9 - 4
src/Network/Session.php

@@ -468,7 +468,7 @@ class Session
      * Removes a variable from session.
      *
      * @param string $name Session variable to remove
-     * @return bool Success
+     * @return void
      */
     public function delete($name)
     {
@@ -518,14 +518,19 @@ class Session
     }
 
     /**
-     * Clears the session, the session id, and renews the session.
+     * Clears the session.
+     *
+     * Optionally it also clears the session id and renews the session.
      *
+     * @param bool $renew If session should be renewed, as well. Defaults to false.
      * @return void
      */
-    public function clear()
+    public function clear($renew = false)
     {
         $_SESSION = [];
-        $this->renew();
+        if ($renew) {
+            $this->renew();
+        }
     }
 
     /**

+ 21 - 1
tests/TestCase/Network/SessionTest.php

@@ -302,7 +302,22 @@ class SessionTest extends TestCase
     }
 
     /**
-     * testDel method
+     * testClear method
+     *
+     * @return void
+     */
+    public function testClear()
+    {
+        $session = new Session();
+        $session->write('Delete.me', 'Clearing out');
+
+        $session->clear();
+        $this->assertFalse($session->check('Delete.me'));
+        $this->assertFalse($session->check('Delete'));
+    }
+
+    /**
+     * testDelete method
      *
      * @return void
      */
@@ -315,6 +330,11 @@ class SessionTest extends TestCase
         $this->assertTrue($session->check('Delete'));
 
         $session->write('Clearing.sale', 'everything must go');
+        $session->delete('');
+        $this->assertTrue($session->check('Clearing.sale'));
+        $session->delete(null);
+        $this->assertTrue($session->check('Clearing.sale'));
+
         $session->delete('Clearing');
         $this->assertFalse($session->check('Clearing.sale'));
         $this->assertFalse($session->check('Clearing'));