SessionTest.php 39 KB

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