PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Session/SessionTest.php

https://bitbucket.org/ksekar/campus
PHP | 1028 lines | 685 code | 85 blank | 258 comment | 23 complexity | e7ce3dd2a59d6d6949deccf56dd6b2b9 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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: SessionTest.php 24594 2012-01-05 21:27:01Z matthew $
  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. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  824. *
  825. * @return void
  826. */
  827. public function setExpireSessionVarsByHops()
  828. {
  829. $s = new Zend_Session_Namespace('expireGuava');
  830. $expireBeforeHop = 4;
  831. $s->setExpirationHops($expireBeforeHop, 'g');
  832. $s->g = 'guava';
  833. $s->p = 'peach';
  834. $s->p = 'plum';
  835. $id = session_id();
  836. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  837. session_write_close(); // release session so process below can use it
  838. exec("$this->_script expireAll $id expireGuava", $result);
  839. session_start(); // resume artificially suspended session
  840. $result = $this->sortResult($result);
  841. if ($i > $expireBeforeHop) {
  842. $this->assertTrue($result === ';p === plum',
  843. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  844. } else {
  845. $this->assertTrue($result === ';g === guava;p === plum',
  846. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  847. }
  848. }
  849. }
  850. /**
  851. * @todo PHP 5.2.1 is required (fixes a bug with magic __get() returning by reference)
  852. * @see http://framework.zend.com/issues/browse/ZF-800
  853. */
  854. public function testArrays()
  855. {
  856. $this->markTestIncomplete();
  857. $s = new Zend_Session_Namespace('aspace');
  858. $id = Zend_Session::getId();
  859. $this->assertSame($id, session_id());
  860. $s->top = 'begin';
  861. session_write_close(); // release session so process below can use it
  862. exec("$this->_script setArray $id aspace 1 2 3 4 5", $result);
  863. exec("$this->_script getArray $id aspace", $result);
  864. session_start(); // resume artificially suspended session
  865. $result = array_pop($result);
  866. $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";};';
  867. $this->assertTrue($result === $expect,
  868. "iteration over default Zend_Session namespace failed; expecting result ===\n$expect\n, but got\n$result\n)");
  869. }
  870. /**
  871. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  872. *
  873. * @runInSeparateProcess
  874. * @return void
  875. */
  876. public function testSetExpireSessionVarsByHopsOnUse()
  877. {
  878. $s = new Zend_Session_Namespace('expireGuava');
  879. $expireBeforeHop = 2;
  880. $s->setExpirationHops($expireBeforeHop, 'g', true); // only count a hop, when namespace is used
  881. $s->g = 'guava';
  882. $s->p = 'peach';
  883. $s->p = 'plum';
  884. $id = session_id();
  885. // we are not accessing (using) the "expireGuava" namespace, so these hops should have no effect
  886. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  887. session_write_close(); // release session so process below can use it
  888. exec("$this->_script expireAll $id notused", $result);
  889. session_start(); // resume artificially suspended session
  890. $result = $this->sortResult($result);
  891. $this->assertTrue($result === '',
  892. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  893. }
  894. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  895. session_write_close(); // release session so process below can use it
  896. exec("$this->_script expireAll $id expireGuava", $result);
  897. session_start(); // resume artificially suspended session
  898. $result = $this->sortResult($result);
  899. if ($i > $expireBeforeHop) {
  900. $expect = ';p === plum';
  901. $this->assertTrue($result === $expect,
  902. "unexpected results iterating over named Zend_Session namespace (result='$result'; expected '$expect'; hop #$i)");
  903. } else {
  904. $expect = ';g === guava;p === plum';
  905. $this->assertTrue($result === $expect,
  906. "unexpected results iterating over named Zend_Session namespace (result='$result'; expected '$expect'; hop #$i)");
  907. }
  908. }
  909. // Do not destroy session since it still may be used by other tests
  910. // Zend_Session::destroy();
  911. }
  912. /**
  913. * @group ZF-5003
  914. */
  915. public function testProcessSessionMetadataShouldNotThrowAnError()
  916. {
  917. Zend_Session::$_unitTestEnabled = true;
  918. if (isset($_SESSION) && isset($_SESSION['__ZF'])) {
  919. unset($_SESSION['__ZF']);
  920. }
  921. Zend_Session::start();
  922. }
  923. /**
  924. * test for method getNamespace()
  925. *
  926. * @group ZF-1982
  927. * @return void
  928. */
  929. public function testGetNameSpaceMethod()
  930. {
  931. Zend_Session::$_unitTestEnabled = true;
  932. $namespace = array(
  933. 'FooBar',
  934. 'Foo_Bar',
  935. 'Foo-Bar',
  936. 'Foo1000'
  937. );
  938. foreach ($namespace as $v) {
  939. $s = new Zend_Session_Namespace($v);
  940. $this->assertEquals($v, $s->getNamespace());
  941. }
  942. }
  943. }