From 83d6aa6fb48092c2ab9ae6f3e70b8fba1643542f Mon Sep 17 00:00:00 2001 From: famora Date: Thu, 9 Feb 2017 15:25:34 +0100 Subject: [PATCH] Phalcon\Session\Bag: add of initialization for remove() --- CHANGELOG.md | 2 ++ phalcon/session/bag.zep | 4 ++++ tests/unit/Session/BagTest.php | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d611db7384c..7aefa3b3070 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +- Fixed `Phalcon\Session\Bag::remove` to initialize the bag before removing a value + # [3.1.0](https://github.com/phalcon/cphalcon/releases/tag/v3.1.0) (2016-XX-XX) - Added `Phalcon\Validation\Validator\Callback`, `Phalcon\Validation::getData` - Added the ability to truncate database tables diff --git a/phalcon/session/bag.zep b/phalcon/session/bag.zep index 21356525473..6550b568ff5 100644 --- a/phalcon/session/bag.zep +++ b/phalcon/session/bag.zep @@ -231,6 +231,10 @@ class Bag implements InjectionAwareInterface, BagInterface, \IteratorAggregate, */ public function remove(string! property) -> boolean { + if this->_initialized === false { + this->initialize(); + } + var data; let data = this->_data; diff --git a/tests/unit/Session/BagTest.php b/tests/unit/Session/BagTest.php index a8ae07b714f..2d53368baa5 100644 --- a/tests/unit/Session/BagTest.php +++ b/tests/unit/Session/BagTest.php @@ -83,4 +83,45 @@ function () { } ); } + + /** + * Delete a value in a bag (not initialized internally) + * + * @author Fabio Mora + * @since 2017-02-21 + */ + public function testDeleteInitializeInternalData() + { + $this->specify( + "Delete a value in a non initialized bag has failed", + function () { + $reflectionClass = new \ReflectionClass(\Phalcon\Session\Bag::class); + $_data = $reflectionClass->getProperty('_data'); + $_data->setAccessible(true); + $_initialized = $reflectionClass->getProperty('_initialized'); + $_initialized->setAccessible(true); + + // Setup a bag with a value + $bag = new \Phalcon\Session\Bag('fruit'); + $bag->set('apples', 10); + expect($bag->get('apples'))->same(10); + expect($_data->getValue($bag))->same(['apples' => 10]); + expect($_initialized->getValue($bag))->true(); + + // Emulate a reset of the internal status (e.g. as would be done by a sleep/wakeup handler) + $serializedBag = serialize($bag); + unset($bag); + + $bag = unserialize($serializedBag); + $_data->setValue($bag, NULL); + $_initialized->setValue($bag, false); + + // Delete + expect($_initialized->getValue($bag))->false(); + expect($bag->remove('apples'))->true(); + expect($bag->get('apples'))->null(); + expect($_initialized->getValue($bag))->true(); + } + ); + } }