2
0

SessionTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Session
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Session
  24. */
  25. require_once 'Zend/Session.php';
  26. /**
  27. * Black box testing for Zend_Session
  28. *
  29. * @category Zend
  30. * @package Zend_Session
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Session
  35. */
  36. class Zend_SessionTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Helper script invoked via exec()
  40. *
  41. * @var string
  42. */
  43. protected $_script = null;
  44. /**
  45. * Storage for session.save_path, so that unit tests may change the value without side effect
  46. *
  47. * @var string
  48. */
  49. protected $_savePath;
  50. /**
  51. * Initializes instance data
  52. *
  53. * @return void
  54. */
  55. public function __construct($name = NULL, array $data = array(), $dataName = '')
  56. {
  57. parent::__construct($name, $data, $dataName);
  58. $this->_script = 'php '
  59. . '-c ' . escapeshellarg(php_ini_loaded_file()) . ' '
  60. . escapeshellarg(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'SessionTestHelper.php');
  61. $this->_savePath = ini_get('session.save_path');
  62. }
  63. /**
  64. * Set up tests environment
  65. */
  66. function setUp()
  67. {
  68. // _unitTestEnabled is utilised by other tests to handle session data processing
  69. // Zend_Session tests should pass with _unitTestEnabled turned off
  70. Zend_Session::$_unitTestEnabled = false;
  71. }
  72. /**
  73. * Cleanup operations after each test method is run
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. ini_set('session.save_path', $this->_savePath);
  80. $this->assertSame(
  81. E_ALL | E_STRICT,
  82. error_reporting( E_ALL | E_STRICT ),
  83. 'A test altered error_reporting to something other than E_ALL | E_STRICT'
  84. );
  85. Zend_Session_Namespace::unlockAll();
  86. // unset all namespaces
  87. foreach (Zend_Session::getIterator() as $space) {
  88. try {
  89. Zend_Session::namespaceUnset($space);
  90. } catch (Zend_Session_Exception $e) {
  91. $this->assertRegexp('/read.only/i', $e->getMessage());
  92. return;
  93. }
  94. }
  95. }
  96. /**
  97. * Sorts the compound result returned by SessionTestHelper, so that the
  98. * order of iteration over namespace items do not impact analysis of test results.
  99. *
  100. * @param array $result output of exec()'ing SessionTestHelper
  101. * @return string sorted alphabetically
  102. */
  103. public function sortResult(array $result)
  104. {
  105. $results = explode(';', array_pop($result));
  106. sort($results);
  107. return implode(';', $results);
  108. }
  109. /**
  110. * test session id manipulations; expect isRegenerated flag == true
  111. *
  112. * @return void
  113. */
  114. public function testRegenerateId()
  115. {
  116. // Check if session hasn't already been started by another test
  117. if (!Zend_Session::isStarted()) {
  118. Zend_Session::setId('myid123');
  119. Zend_Session::regenerateId();
  120. $this->assertFalse(Zend_Session::isRegenerated());
  121. $id = Zend_Session::getId();
  122. $this->assertTrue($id === 'myid123',
  123. 'getId() reported something different than set via setId("myid123")');
  124. Zend_Session::start();
  125. } else {
  126. // Start session if it's not actually started
  127. // That may happen if Zend_Session::$_unitTestEnabled is turned on while some other
  128. // Unit tests utilize Zend_Session functionality
  129. if (!defined('SID')) {
  130. session_start();
  131. }
  132. // only regenerate session id if session has already been started
  133. Zend_Session::regenerateId();
  134. }
  135. $this->assertTrue(Zend_Session::isRegenerated());
  136. try {
  137. Zend_Session::setId('someo-therid-123');
  138. $this->fail('No exception was returned when trying to set the session id, after session_start()');
  139. } catch (Zend_Session_Exception $e) {
  140. $this->assertRegexp('/already.*started/i', $e->getMessage());
  141. }
  142. }
  143. /**
  144. * Ensures that setOptions() behaves as expected
  145. *
  146. * @return void
  147. */
  148. public function testSetOptions()
  149. {
  150. try {
  151. Zend_Session::setOptions(array('foo' => 'break me'));
  152. $this->fail('Expected Zend_Session_Exception not thrown when trying to set an invalid option');
  153. } catch (Zend_Session_Exception $e) {
  154. $this->assertRegexp('/unknown.option/i', $e->getMessage());
  155. }
  156. Zend_Session::setOptions(array('save_path' => '1;777;/tmp'));
  157. Zend_Session::setOptions(array('save_path' => '2;/tmp'));
  158. Zend_Session::setOptions(array('save_path' => '/tmp'));
  159. }
  160. /**
  161. * test for initialisation without parameter; expect instance
  162. *
  163. * @return void
  164. */
  165. public function testInit()
  166. {
  167. $s = new Zend_Session_Namespace();
  168. $this->assertTrue($s instanceof Zend_Session_Namespace,'Zend_Session Object not returned');
  169. }
  170. /**
  171. * test for initialisation with empty string; expect failure
  172. *
  173. * @return void
  174. */
  175. public function testInitEmpty()
  176. {
  177. try {
  178. $s = new Zend_Session_Namespace('');
  179. } catch (Zend_Session_Exception $e) {
  180. $this->assertRegexp('/non.empty.string/i', $e->getMessage());
  181. return;
  182. }
  183. $this->fail('No exception was returned when trying to create a namespace having the empty string as '
  184. . 'its name; expected Zend_Session_Exception');
  185. }
  186. /**
  187. * test for initialisation with Session parameter; expect instance
  188. *
  189. * @return void
  190. */
  191. public function testInitSession()
  192. {
  193. $s = new Zend_Session_Namespace('namespace');
  194. $this->assertTrue($s instanceof Zend_Session_Namespace, 'Zend_Session_Namespace object not returned');
  195. }
  196. /**
  197. * test for initialisation with single instance; expected instance
  198. *
  199. * @return void
  200. */
  201. public function testInitSingleInstance()
  202. {
  203. $s = new Zend_Session_Namespace('single', true);
  204. try {
  205. $s = new Zend_Session_Namespace('single', true);
  206. } catch (Zend_Session_Exception $e) {
  207. // session namespace 'single' already exists and is set to be the only instance of this namespace
  208. $this->assertRegexp('/already.*exist/i', $e->getMessage());
  209. return;
  210. }
  211. $this->fail('No exception was returned when creating a duplicate session for the same namespace, '
  212. . 'even though "single instance" was specified; expected Zend_Session_Exception');
  213. }
  214. /**
  215. * test for retrieval of non-existent keys in a valid namespace; expected null value
  216. * returned by getter for an unset key
  217. *
  218. * @return void
  219. */
  220. public function testNamespaceGetNull()
  221. {
  222. try {
  223. $s = new Zend_Session_Namespace();
  224. $s->tree = 'fig';
  225. $dog = $s->dog;
  226. $this->assertTrue($dog === null, "getting value of non-existent key failed to return null ($dog)");
  227. } catch (Zend_Session_Exception $e) {
  228. $this->fail('Unexpected exception returned when attempting to fetch the value of non-existent key');
  229. }
  230. }
  231. /**
  232. * test for existence of namespace; expected true
  233. *
  234. * @return void
  235. */
  236. public function testNamespaceIsset()
  237. {
  238. try {
  239. $this->assertFalse(Zend_Session::namespaceIsset('trees'),
  240. 'namespaceIsset() should have returned false for a namespace with no keys set');
  241. $s = new Zend_Session_Namespace('trees');
  242. $this->assertFalse(Zend_Session::namespaceIsset('trees'),
  243. 'namespaceIsset() should have returned false for a namespace with no keys set');
  244. $s->cherry = 'bing';
  245. $this->assertTrue(Zend_Session::namespaceIsset('trees'),
  246. 'namespaceIsset() should have returned true for a namespace with keys set');
  247. } catch (Zend_Session_Exception $e) {
  248. $this->fail('Unexpected exception returned when attempting to fetch the value of non-existent key');
  249. }
  250. }
  251. /**
  252. * test magic methods with improper variable interpolation; expect no exceptions
  253. *
  254. * @return void
  255. */
  256. public function testMagicMethodsEmpty()
  257. {
  258. $s = new Zend_Session_Namespace();
  259. $name = 'fruit';
  260. $s->$name = 'apples';
  261. $this->assertTrue(isset($s->fruit), 'isset() failed - returned false, but should have been true');
  262. try {
  263. $name = ''; // simulate a common bug, where user refers to an unset/empty variable
  264. $s->$name = 'pear';
  265. $this->fail('No exception was returned when trying to __set() a key named ""; expected '
  266. . 'Zend_Session_Exception');
  267. } catch (Zend_Session_Exception $e) {
  268. $this->assertRegexp('/non.empty.string/i', $e->getMessage());
  269. }
  270. try {
  271. $name = ''; // simulate a common bug, where user refers to an unset/empty variable
  272. $nothing = $s->$name;
  273. $this->fail('No exception was returned when trying to __set() a key named ""; expected '
  274. . 'Zend_Session_Exception');
  275. } catch (Zend_Session_Exception $e) {
  276. $this->assertRegexp('/non.empty.string/i', $e->getMessage());
  277. }
  278. try {
  279. $name = ''; // simulate a common bug, where user refers to an unset/empty variable
  280. if (isset($s->$name)) { true; }
  281. $this->fail('No exception was returned when trying to __set() a key named ""; expected '
  282. . 'Zend_Session_Exception');
  283. } catch (Zend_Session_Exception $e) {
  284. $this->assertRegexp('/non.empty.string/i', $e->getMessage());
  285. }
  286. try {
  287. $name = ''; // simulate a common bug, where user refers to an unset/empty variable
  288. unset($s->$name);
  289. $this->fail('No exception was returned when trying to __set() a key named ""; expected '
  290. . 'Zend_Session_Exception');
  291. } catch (Zend_Session_Exception $e) {
  292. $this->assertRegexp('/non.empty.string/i', $e->getMessage());
  293. }
  294. }
  295. /**
  296. * test for proper separation of namespace "spaces"; expect variables in different namespaces are
  297. * different variables (i.e., not shared values)
  298. *
  299. * @return void
  300. */
  301. public function testInitNamespaces()
  302. {
  303. $s1 = new Zend_Session_Namespace('namespace1');
  304. $s1b = new Zend_Session_Namespace('namespace1');
  305. $s2 = new Zend_Session_Namespace('namespace2');
  306. $s2b = new Zend_Session_Namespace('namespace2');
  307. $s3 = new Zend_Session_Namespace();
  308. $s3b = new Zend_Session_Namespace();
  309. $s1->a = 'apple';
  310. $s2->a = 'pear';
  311. $s3->a = 'orange';
  312. $this->assertTrue(($s1->a != $s2->a && $s1->a != $s3->a && $s2->a != $s3->a),
  313. 'Zend_Session improperly shared namespaces');
  314. $this->assertTrue(($s1->a === $s1b->a),'Zend_Session namespace error');
  315. $this->assertTrue(($s2->a === $s2b->a),'Zend_Session namespace error');
  316. $this->assertTrue(($s3->a === $s3b->a),'Zend_Session namespace error');
  317. }
  318. /**
  319. * test for detection of illegal namespace names; expect exception complaining about name beginning
  320. * with an underscore
  321. *
  322. * @return void
  323. */
  324. public function testInitNamespaceUnderscore()
  325. {
  326. try {
  327. $s = new Zend_Session_Namespace('_namespace');
  328. $this->fail('No exception was returned when requesting a namespace having a name beginning with '
  329. . 'an underscore');
  330. } catch (Zend_Session_Exception $e) {
  331. $this->assertRegexp('/underscore/i', $e->getMessage());
  332. }
  333. }
  334. /**
  335. * test for detection of illegal namespace names; expect exception complaining about name beginning
  336. * with an underscore
  337. *
  338. * @return void
  339. */
  340. public function testInitNamespaceNumber()
  341. {
  342. try {
  343. $s = new Zend_Session_Namespace('0namespace');
  344. $this->fail('No exception was returned when requesting a namespace having a name beginning with '
  345. . 'a number');
  346. } catch (Zend_Session_Exception $e) {
  347. $this->assertRegexp('/number/i', $e->getMessage());
  348. }
  349. }
  350. /**
  351. * test iteration; expect native PHP foreach statement is able to properly iterate all items in a session namespace
  352. *
  353. * @return void
  354. */
  355. public function testGetIterator()
  356. {
  357. $s = new Zend_Session_Namespace();
  358. $s->a = 'apple';
  359. $s->p = 'pear';
  360. $s->o = 'orange';
  361. $result = '';
  362. foreach ($s->getIterator() as $key => $val) {
  363. $result .= "$key === $val;";
  364. }
  365. $this->assertTrue($result === 'a === apple;p === pear;o === orange;',
  366. 'iteration over default Zend_Session namespace failed: result="' . $result . '"');
  367. $s = new Zend_Session_Namespace('namespace');
  368. $s->g = 'guava';
  369. $s->p = 'peach';
  370. $s->p = 'plum';
  371. $result = '';
  372. foreach ($s->getIterator() as $key => $val) {
  373. $result .= "$key === $val;";
  374. }
  375. $this->assertTrue($result === 'g === guava;p === plum;',
  376. 'iteration over named Zend_Session namespace failed');
  377. }
  378. /**
  379. * test locking of the Default namespace (i.e. make namespace readonly); expect exceptions when trying to write to
  380. * locked namespace
  381. *
  382. * @return void
  383. */
  384. public function testLock()
  385. {
  386. $s = new Zend_Session_Namespace();
  387. $s->a = 'apple';
  388. $s->p = 'pear';
  389. $s->lock();
  390. try {
  391. $s->o = 'orange';
  392. $this->fail('No exception was returned when setting a variable in the "Default" namespace, '
  393. . 'after marking the namespace as read-only; expected Zend_Session_Exception');
  394. } catch (Zend_Session_Exception $e) {
  395. // session namespace 'single' already exists and is set to be the only instance of this namespace
  396. $this->assertRegexp('/read.only/i', $e->getMessage());
  397. }
  398. $s->unlock();
  399. $s->o = 'orange';
  400. $s->p = 'papaya';
  401. $s->c = 'cherry';
  402. $s->lock();
  403. try {
  404. $s->o = 'orange';
  405. $this->fail('No exception was returned when setting a variable in the "Default" namespace, '
  406. . 'after marking the namespace as read-only; expected Zend_Session_Exception');
  407. } catch (Zend_Session_Exception $e) {
  408. // session namespace 'single' already exists and is set to be the only instance of this namespace
  409. $this->assertRegexp('/read.only/i', $e->getMessage());
  410. }
  411. }
  412. /**
  413. * test locking of named namespaces (i.e. make namespace readonly); expect exceptions when trying to write
  414. * to locked namespace
  415. *
  416. * @return void
  417. */
  418. public function testLockNamespace()
  419. {
  420. $s = new Zend_Session_Namespace('somenamespace');
  421. $s->a = 'apple';
  422. $s->p = 'pear';
  423. $s->lock();
  424. try {
  425. $s->o = 'orange';
  426. $this->fail('No exception was returned when setting a variable in the "Default" namespace, '
  427. . 'after marking the namespace as read-only; expected Zend_Session_Exception');
  428. } catch (Zend_Session_Exception $e) {
  429. // session namespace 'single' already exists and is set to be the only instance of this namespace
  430. $this->assertRegexp('/read.only/i', $e->getMessage());
  431. }
  432. $s = new Zend_Session_Namespace('somenamespace');
  433. $s2 = new Zend_Session_Namespace('mayday');
  434. $s2->lock();
  435. $s->unlock();
  436. $s->o = 'orange';
  437. $s->p = 'papaya';
  438. $s->c = 'cherry';
  439. $s = new Zend_Session_Namespace('somenamespace');
  440. $s->lock();
  441. $s2->unlock();
  442. try {
  443. $s->o = 'orange';
  444. $this->fail('No exception was returned when setting a variable in the "Default" namespace, '
  445. . 'after marking the namespace as read-only; expected Zend_Session_Exception');
  446. } catch (Zend_Session_Exception $e) {
  447. $this->assertRegexp('/read.only/i', $e->getMessage());
  448. }
  449. }
  450. /**
  451. * test unlocking of the Default namespace (i.e. make namespace readonly); expected no exceptions
  452. *
  453. * @return void
  454. */
  455. public function testUnlock()
  456. {
  457. $s = new Zend_Session_Namespace();
  458. try {
  459. $s->a = 'apple';
  460. $s->p = 'pear';
  461. $s->lock();
  462. $s->unlock();
  463. $s->o = 'orange';
  464. $s->p = 'prune';
  465. $s->lock();
  466. $s->unlock();
  467. $s->o = 'orange';
  468. $s->p = 'papaya';
  469. $s->c = 'cherry';
  470. } catch (Zend_Session_Exception $e) {
  471. $this->fail('Unexpected exception when writing to namespaces after unlocking it.');
  472. }
  473. }
  474. /**
  475. * test combinations of locking and unlocking of the Default namespace (i.e. make namespace readonly)
  476. * expected no exceptions
  477. *
  478. * @return void
  479. */
  480. public function testUnLockAll()
  481. {
  482. $sessions = array('one', 'two', 'default', 'three');
  483. foreach ($sessions as $namespace) {
  484. $s = new Zend_Session_Namespace($namespace);
  485. $s->a = 'apple';
  486. $s->p = 'pear';
  487. $s->lock();
  488. $s->unlock();
  489. $s->o = 'orange';
  490. $s->p = 'prune';
  491. $s->lock();
  492. $s->unlock();
  493. $s->o = 'orange';
  494. $s->p = 'papaya';
  495. $s->c = 'cherry';
  496. $s->lock();
  497. $this->assertTrue($s->isLocked(), 'isLocked() returned incorrect status (not locked)');
  498. try {
  499. $s->p = 'prune';
  500. $s->f = 'fig';
  501. $this->fail('No exception was returned when setting a variable in the "Default" namespace, '
  502. . 'after marking the namespace as read-only; expected Zend_Session_Exception');
  503. } catch (Zend_Session_Exception $e) {
  504. $this->assertRegexp('/read.only/i', $e->getMessage());
  505. }
  506. }
  507. $s->unlockAll();
  508. foreach ($sessions as $namespace) {
  509. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  510. $s->p = 'pear';
  511. $s->f = 'fig';
  512. $s->l = 'lime';
  513. }
  514. }
  515. /**
  516. * test isLocked() unary comparison operator under various situations; expect lock status remains synchronized
  517. * with last call to unlock() or lock(); expect no exceptions
  518. *
  519. * @return void
  520. */
  521. public function testIsLocked()
  522. {
  523. try {
  524. $s = new Zend_Session_Namespace();
  525. $s->a = 'apple';
  526. $s->p = 'pear';
  527. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  528. $s->lock();
  529. $this->assertTrue($s->isLocked(), 'isLocked() returned incorrect status (unlocked)');
  530. $s->unlock();
  531. $s->o = 'orange';
  532. $s->p = 'prune';
  533. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  534. $s->lock();
  535. $this->assertTrue($s->isLocked(), 'isLocked() returned incorrect status (unlocked)');
  536. $s->unlock();
  537. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  538. $s->o = 'orange';
  539. $s->p = 'papaya';
  540. $s->c = 'cherry';
  541. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  542. } catch (Zend_Session_Exception $e) {
  543. $this->fail('Unexpected exception when writing to named namespaces after unlocking them.');
  544. }
  545. }
  546. /**
  547. * test unlocking of named namespaces (i.e., make namespace readonly); expect no exceptions
  548. *
  549. * @return void
  550. */
  551. public function testUnLockNamespace()
  552. {
  553. $s = new Zend_Session_Namespace('somenamespace');
  554. try {
  555. $s->a = 'apple';
  556. $s->p = 'pear';
  557. $s->lock();
  558. $s2 = new Zend_Session_Namespace('mayday');
  559. $s2->lock();
  560. $s->unlock();
  561. $s->o = 'orange';
  562. $s->p = 'prune';
  563. $s->lock();
  564. $s->unlock();
  565. $s->o = 'orange';
  566. $s->p = 'papaya';
  567. $s->c = 'cherry';
  568. } catch (Zend_Session_Exception $e) {
  569. $this->fail('Unexpected exception when writing to named namespaces after unlocking them.');
  570. }
  571. }
  572. /**
  573. * test isLocked() unary comparison operator under various situations; expect lock status remains synchronized with
  574. * last call to unlock() or lock(); expect no exceptions
  575. *
  576. * @return void
  577. */
  578. public function testIsLockedNamespace()
  579. {
  580. try {
  581. $s = new Zend_Session_Namespace('somenamespace');
  582. $s->a = 'apple';
  583. $s->p = 'pear';
  584. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  585. $s->lock();
  586. $s2 = new Zend_Session_Namespace('mayday');
  587. $s2->lock();
  588. $this->assertTrue($s->isLocked(), 'isLocked() returned incorrect status (unlocked)');
  589. $s->unlock();
  590. $s->o = 'orange';
  591. $s->p = 'prune';
  592. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  593. $s->lock();
  594. $s2->unlock();
  595. $this->assertTrue($s->isLocked(), 'isLocked() returned incorrect status (unlocked)');
  596. $s->unlock();
  597. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  598. $s->o = 'orange';
  599. $s->p = 'papaya';
  600. $s->c = 'cherry';
  601. $this->assertFalse($s->isLocked(), 'isLocked() returned incorrect status (locked)');
  602. } catch (Zend_Session_Exception $e) {
  603. $this->fail('Unexpected exception when writing to named namespaces after unlocking them.');
  604. }
  605. }
  606. /**
  607. * test unsetAll keys in default namespace; expect namespace contains only keys not unset()
  608. *
  609. * @return void
  610. */
  611. public function testUnsetAll()
  612. {
  613. $s = new Zend_Session_Namespace();
  614. $result = '';
  615. foreach ($s->getIterator() as $key => $val) {
  616. $result .= "$key === $val;";
  617. }
  618. $this->assertTrue(empty($result), "tearDown failure, found keys in default namespace: '$result'");
  619. $s->a = 'apple';
  620. $s->lock();
  621. $s->unlock();
  622. $s->p = 'papaya';
  623. $s->c = 'cherry';
  624. $s = new Zend_Session_Namespace();
  625. $result = '';
  626. foreach ($s->getIterator() as $key => $val) {
  627. $result .= "$key === $val;";
  628. }
  629. $this->assertTrue($result === 'a === apple;p === papaya;c === cherry;',
  630. "unsetAll() setup for test failed: '$result'");
  631. $s->unsetAll();
  632. $result = '';
  633. foreach ($s->getIterator() as $key => $val) {
  634. $result .= "$key === $val;";
  635. }
  636. $this->assertTrue(empty($result), "unsetAll() did not remove keys from namespace: '$result'");
  637. }
  638. /**
  639. * test unset() keys in default namespace; expect namespace contains only keys not unset()
  640. *
  641. * @return void
  642. */
  643. public function testUnset()
  644. {
  645. $s = new Zend_Session_Namespace();
  646. $result = '';
  647. foreach ($s->getIterator() as $key => $val) {
  648. $result .= "$key === $val;";
  649. }
  650. $this->assertTrue(empty($result), "tearDown failure, found keys in default namespace: '$result'");
  651. $s->a = 'apple';
  652. $s->lock();
  653. $s->unlock();
  654. $s->p = 'papaya';
  655. $s->c = 'cherry';
  656. $s = new Zend_Session_Namespace();
  657. foreach ($s->getIterator() as $key => $val) {
  658. unset($s->$key);
  659. }
  660. $result = '';
  661. foreach ($s->getIterator() as $key => $val) {
  662. $result .= "$key === $val;";
  663. }
  664. $this->assertTrue(empty($result), "unsetAll() did not remove keys from namespace: '$result'");
  665. }
  666. /**
  667. * test unset() keys in non-default namespace; expect namespace contains only keys not unset()
  668. *
  669. * @return void
  670. */
  671. public function testUnsetNamespace()
  672. {
  673. $s = new Zend_Session_Namespace('foobar');
  674. $result = '';
  675. foreach ($s->getIterator() as $key => $val) {
  676. $result .= "$key === $val;";
  677. }
  678. $this->assertTrue(empty($result), "tearDown failure, found keys in default namespace: '$result'");
  679. $s->a = 'apple';
  680. $s->lock();
  681. $s->unlock();
  682. $s->p = 'papaya';
  683. $s->c = 'cherry';
  684. $s = new Zend_Session_Namespace('foobar');
  685. foreach ($s->getIterator() as $key => $val) {
  686. unset($s->$key);
  687. }
  688. $result = '';
  689. foreach ($s->getIterator() as $key => $val) {
  690. $result .= "$key === $val;";
  691. }
  692. $this->assertTrue(empty($result), "unsetAll() did not remove keys from namespace: '$result'");
  693. }
  694. /**
  695. * test unsetAll keys in default namespace; expect namespace will contain no keys
  696. *
  697. * @return void
  698. */
  699. public function testUnsetAllNamespace()
  700. {
  701. $s = new Zend_Session_Namespace('somenamespace');
  702. $result = '';
  703. foreach ($s->getIterator() as $key => $val) {
  704. $result .= "$key === $val;";
  705. }
  706. $this->assertTrue(empty($result), "tearDown failure, found keys in 'somenamespace' namespace: '$result'");
  707. $s->a = 'apple';
  708. $s->lock();
  709. $s->unlock();
  710. $s->p = 'papaya';
  711. $s->c = 'cherry';
  712. $s = new Zend_Session_Namespace('somenamespace');
  713. $result = '';
  714. foreach ($s->getIterator() as $key => $val) {
  715. $result .= "$key === $val;";
  716. }
  717. $this->assertTrue($result === 'a === apple;p === papaya;c === cherry;',
  718. "unsetAll() setup for test failed: '$result'");
  719. $s->unsetAll();
  720. $result = '';
  721. foreach ($s->getIterator() as $key => $val) {
  722. $result .= "$key === $val;";
  723. }
  724. $this->assertTrue(empty($result), "unsetAll() did not remove keys from namespace: '$result'");
  725. }
  726. /**
  727. * test expiration of namespaces and namespace variables by seconds; expect expiration of specified keys/namespace
  728. *
  729. * @runInSeparateProcess
  730. * @return void
  731. */
  732. public function testSetExpirationSeconds()
  733. {
  734. // Calculate common script execution time
  735. $startTime = time();
  736. exec($this->_script, $result, $returnValue);
  737. $execTime = time() - $startTime;
  738. $s = new Zend_Session_Namespace('expireAll');
  739. $s->a = 'apple';
  740. $s->p = 'pear';
  741. $s->o = 'orange';
  742. $s->setExpirationSeconds($execTime*2 + 5);
  743. Zend_Session::regenerateId();
  744. $id = Zend_Session::getId();
  745. sleep(4); // not long enough for things to expire
  746. session_write_close(); // release session so process below can use it
  747. exec("$this->_script expireAll $id expireAll", $result, $returnValue);
  748. session_start(); // resume artificially suspended session
  749. $result = $this->sortResult($result);
  750. $expect = ';a === apple;o === orange;p === pear';
  751. $this->assertTrue($result === $expect,
  752. "iteration over default Zend_Session namespace failed; expecting result === '$expect', but got '$result'");
  753. sleep($execTime*2 + 2); // long enough for things to expire (total of $execTime*2 + 6 seconds waiting, but expires in $execTime*2 + 5)
  754. session_write_close(); // release session so process below can use it
  755. exec("$this->_script expireAll $id expireAll", $result, $returnValue);
  756. session_start(); // resume artificially suspended session
  757. $this->assertNull(array_pop($result));
  758. // We could split this into a separate test, but actually, if anything leftover from above
  759. // contaminates the tests below, that is also a bug that we want to know about.
  760. $s = new Zend_Session_Namespace('expireGuava');
  761. $s->setExpirationSeconds(5, 'g'); // now try to expire only 1 of the keys in the namespace
  762. $s->g = 'guava';
  763. $s->p = 'peach';
  764. $s->p = 'plum';
  765. sleep(6); // not long enough for things to expire
  766. session_write_close(); // release session so process below can use it
  767. exec("$this->_script expireAll $id expireGuava", $result, $returnValue);
  768. session_start(); // resume artificially suspended session
  769. $result = $this->sortResult($result);
  770. $this->assertTrue($result === ';p === plum',
  771. "iteration over named Zend_Session namespace failed (result=$result)");
  772. }
  773. /**
  774. * test expiration of namespaces by hops; expect expiration of specified namespace in the proper number of hops
  775. *
  776. * @runInSeparateProcess
  777. * @return void
  778. */
  779. public function testSetExpireSessionHops()
  780. {
  781. $s = new Zend_Session_Namespace('expireAll');
  782. $s->a = 'apple';
  783. $s->p = 'pear';
  784. $s->o = 'orange';
  785. $expireBeforeHop = 3;
  786. $s->setExpirationHops($expireBeforeHop);
  787. $id = session_id();
  788. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  789. session_write_close(); // release session so process below can use it
  790. exec("$this->_script expireAll $id expireAll", $result, $returnValue);
  791. session_start(); // resume artificially suspended session
  792. $result = $this->sortResult($result);
  793. if ($i > $expireBeforeHop) {
  794. $this->assertTrue($result === '',
  795. "iteration over default Zend_Session namespace failed (result='$result'; hop #$i)");
  796. } else {
  797. $this->assertTrue($result === ';a === apple;o === orange;p === pear',
  798. "iteration over default Zend_Session namespace failed (result='$result'; hop #$i)");
  799. }
  800. }
  801. }
  802. /**
  803. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  804. *
  805. * @runInSeparateProcess
  806. * @return void
  807. */
  808. public function testSetExpireSessionVarsByHops1()
  809. {
  810. $this->setExpireSessionVarsByHops();
  811. }
  812. /**
  813. * sanity check .. we should be able to repeat this test without problems
  814. *
  815. * @runInSeparateProcess
  816. * @return void
  817. */
  818. public function testSetExpireSessionVarsByHops2()
  819. {
  820. $this->setExpireSessionVarsByHops();
  821. }
  822. /**
  823. * @group ZF-7196
  824. * @runInSeparateProcess
  825. */
  826. public function testUnsettingNamespaceKeyWithoutUnsettingCompleteExpirationData()
  827. {
  828. $namespace = new Zend_Session_Namespace('DummyNamespace');
  829. $namespace->foo = 23;
  830. $namespace->bar = 42;
  831. $namespace->setExpirationHops(1);
  832. $sessionId = session_id();
  833. session_write_close();
  834. exec($this->_script . ' expireAll ' . $sessionId . ' DummyNamespace ZF-7196', $result, $returnValue);
  835. session_start();
  836. $result = $this->sortResult($result);
  837. $this->assertSame(';bar === 42', $result);
  838. }
  839. /**
  840. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  841. *
  842. * @return void
  843. */
  844. public function setExpireSessionVarsByHops()
  845. {
  846. $s = new Zend_Session_Namespace('expireGuava');
  847. $expireBeforeHop = 4;
  848. $s->setExpirationHops($expireBeforeHop, 'g');
  849. $s->g = 'guava';
  850. $s->p = 'peach';
  851. $s->p = 'plum';
  852. $id = session_id();
  853. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  854. session_write_close(); // release session so process below can use it
  855. exec("$this->_script expireAll $id expireGuava", $result);
  856. session_start(); // resume artificially suspended session
  857. $result = $this->sortResult($result);
  858. if ($i > $expireBeforeHop) {
  859. $this->assertTrue($result === ';p === plum',
  860. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  861. } else {
  862. $this->assertTrue($result === ';g === guava;p === plum',
  863. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  864. }
  865. }
  866. }
  867. /**
  868. * @todo PHP 5.2.1 is required (fixes a bug with magic __get() returning by reference)
  869. * @see http://framework.zend.com/issues/browse/ZF-800
  870. */
  871. public function testArrays()
  872. {
  873. $this->markTestIncomplete();
  874. $s = new Zend_Session_Namespace('aspace');
  875. $id = Zend_Session::getId();
  876. $this->assertSame($id, session_id());
  877. $s->top = 'begin';
  878. session_write_close(); // release session so process below can use it
  879. exec("$this->_script setArray $id aspace 1 2 3 4 5", $result);
  880. exec("$this->_script getArray $id aspace", $result);
  881. session_start(); // resume artificially suspended session
  882. $result = array_pop($result);
  883. $expect = 'top === begin;astring === happy;someArray === Array;(;[0]=>aspace;[1]=>1;[2]=>2;[3]=>3;[4]=>4;[5]=>5;[bee]=>honey;[ant]=>sugar;[dog]=>cat;);;serializedArray === a:8:{i:0;s:6:"aspace";i:1;s:1:"1";i:2;s:1:"2";i:3;s:1:"3";i:4;s:1:"4";i:5;s:1:"5";s:3:"ant";s:5:"sugar";s:3:"dog";s:3:"cat";};';
  884. $this->assertTrue($result === $expect,
  885. "iteration over default Zend_Session namespace failed; expecting result ===\n$expect\n, but got\n$result\n)");
  886. }
  887. /**
  888. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  889. *
  890. * @runInSeparateProcess
  891. * @return void
  892. */
  893. public function testSetExpireSessionVarsByHopsOnUse()
  894. {
  895. $s = new Zend_Session_Namespace('expireGuava');
  896. $expireBeforeHop = 2;
  897. $s->setExpirationHops($expireBeforeHop, 'g', true); // only count a hop, when namespace is used
  898. $s->g = 'guava';
  899. $s->p = 'peach';
  900. $s->p = 'plum';
  901. $id = session_id();
  902. // we are not accessing (using) the "expireGuava" namespace, so these hops should have no effect
  903. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  904. session_write_close(); // release session so process below can use it
  905. exec("$this->_script expireAll $id notused", $result);
  906. session_start(); // resume artificially suspended session
  907. $result = $this->sortResult($result);
  908. $this->assertTrue($result === '',
  909. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  910. }
  911. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  912. session_write_close(); // release session so process below can use it
  913. exec("$this->_script expireAll $id expireGuava", $result);
  914. session_start(); // resume artificially suspended session
  915. $result = $this->sortResult($result);
  916. if ($i > $expireBeforeHop) {
  917. $expect = ';p === plum';
  918. $this->assertTrue($result === $expect,
  919. "unexpected results iterating over named Zend_Session namespace (result='$result'; expected '$expect'; hop #$i)");
  920. } else {
  921. $expect = ';g === guava;p === plum';
  922. $this->assertTrue($result === $expect,
  923. "unexpected results iterating over named Zend_Session namespace (result='$result'; expected '$expect'; hop #$i)");
  924. }
  925. }
  926. // Do not destroy session since it still may be used by other tests
  927. // Zend_Session::destroy();
  928. }
  929. /**
  930. * @group ZF-5003
  931. */
  932. public function testProcessSessionMetadataShouldNotThrowAnError()
  933. {
  934. Zend_Session::$_unitTestEnabled = true;
  935. if (isset($_SESSION) && isset($_SESSION['__ZF'])) {
  936. unset($_SESSION['__ZF']);
  937. }
  938. Zend_Session::start();
  939. }
  940. /**
  941. * test for method getNamespace()
  942. *
  943. * @group ZF-1982
  944. * @return void
  945. */
  946. public function testGetNameSpaceMethod()
  947. {
  948. Zend_Session::$_unitTestEnabled = true;
  949. $namespace = array(
  950. 'FooBar',
  951. 'Foo_Bar',
  952. 'Foo-Bar',
  953. 'Foo1000'
  954. );
  955. foreach ($namespace as $v) {
  956. $s = new Zend_Session_Namespace($v);
  957. $this->assertEquals($v, $s->getNamespace());
  958. }
  959. }
  960. /**
  961. * @group ZF-11186
  962. */
  963. public function testNoNoticesIfNoValidatorDataInSession()
  964. {
  965. try {
  966. Zend_Session::start();
  967. require_once dirname(__FILE__) . '/Validator/NoticeValidator.php';
  968. Zend_Session::registerValidator(new Zend_Session_Validator_NoticeValidator);
  969. } catch (PHPUnit_Framework_Error_Notice $exception) {
  970. $this->fail($exception->getMessage());
  971. }
  972. }
  973. }