SessionTest.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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-2015 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-2015 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. if (getenv('TRAVIS')) {
  736. $this->markTestSkipped(
  737. 'Test randomly fail on Travis CI.'
  738. );
  739. }
  740. // Calculate common script execution time
  741. $startTime = time();
  742. exec($this->_script, $result, $returnValue);
  743. $execTime = time() - $startTime;
  744. $s = new Zend_Session_Namespace('expireAll');
  745. $s->a = 'apple';
  746. $s->p = 'pear';
  747. $s->o = 'orange';
  748. $s->setExpirationSeconds($execTime*2 + 5);
  749. Zend_Session::regenerateId();
  750. $id = Zend_Session::getId();
  751. sleep(4); // not long enough for things to expire
  752. session_write_close(); // release session so process below can use it
  753. exec("$this->_script expireAll $id expireAll", $result, $returnValue);
  754. session_start(); // resume artificially suspended session
  755. $result = $this->sortResult($result);
  756. $expect = ';a === apple;o === orange;p === pear';
  757. $this->assertTrue($result === $expect,
  758. "iteration over default Zend_Session namespace failed; expecting result === '$expect', but got '$result'");
  759. sleep($execTime*2 + 2); // long enough for things to expire (total of $execTime*2 + 6 seconds waiting, but expires in $execTime*2 + 5)
  760. session_write_close(); // release session so process below can use it
  761. exec("$this->_script expireAll $id expireAll", $result, $returnValue);
  762. session_start(); // resume artificially suspended session
  763. $this->assertNull(array_pop($result));
  764. // We could split this into a separate test, but actually, if anything leftover from above
  765. // contaminates the tests below, that is also a bug that we want to know about.
  766. $s = new Zend_Session_Namespace('expireGuava');
  767. $s->setExpirationSeconds(5, 'g'); // now try to expire only 1 of the keys in the namespace
  768. $s->g = 'guava';
  769. $s->p = 'peach';
  770. $s->p = 'plum';
  771. sleep(6); // not long enough for things to expire
  772. session_write_close(); // release session so process below can use it
  773. exec("$this->_script expireAll $id expireGuava", $result, $returnValue);
  774. session_start(); // resume artificially suspended session
  775. $result = $this->sortResult($result);
  776. $this->assertTrue($result === ';p === plum',
  777. "iteration over named Zend_Session namespace failed (result=$result)");
  778. }
  779. /**
  780. * test expiration of namespaces by hops; expect expiration of specified namespace in the proper number of hops
  781. *
  782. * @runInSeparateProcess
  783. * @return void
  784. */
  785. public function testSetExpireSessionHops()
  786. {
  787. $s = new Zend_Session_Namespace('expireAll');
  788. $s->a = 'apple';
  789. $s->p = 'pear';
  790. $s->o = 'orange';
  791. $expireBeforeHop = 3;
  792. $s->setExpirationHops($expireBeforeHop);
  793. $id = session_id();
  794. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  795. session_write_close(); // release session so process below can use it
  796. exec("$this->_script expireAll $id expireAll", $result, $returnValue);
  797. session_start(); // resume artificially suspended session
  798. $result = $this->sortResult($result);
  799. if ($i > $expireBeforeHop) {
  800. $this->assertTrue($result === '',
  801. "iteration over default Zend_Session namespace failed (result='$result'; hop #$i)");
  802. } else {
  803. $this->assertTrue($result === ';a === apple;o === orange;p === pear',
  804. "iteration over default Zend_Session namespace failed (result='$result'; hop #$i)");
  805. }
  806. }
  807. }
  808. /**
  809. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  810. *
  811. * @runInSeparateProcess
  812. * @return void
  813. */
  814. public function testSetExpireSessionVarsByHops1()
  815. {
  816. $this->setExpireSessionVarsByHops();
  817. }
  818. /**
  819. * sanity check .. we should be able to repeat this test without problems
  820. *
  821. * @runInSeparateProcess
  822. * @return void
  823. */
  824. public function testSetExpireSessionVarsByHops2()
  825. {
  826. $this->setExpireSessionVarsByHops();
  827. }
  828. /**
  829. * @group ZF-7196
  830. * @runInSeparateProcess
  831. */
  832. public function testUnsettingNamespaceKeyWithoutUnsettingCompleteExpirationData()
  833. {
  834. $namespace = new Zend_Session_Namespace('DummyNamespace');
  835. $namespace->foo = 23;
  836. $namespace->bar = 42;
  837. $namespace->setExpirationHops(1);
  838. $sessionId = session_id();
  839. session_write_close();
  840. exec($this->_script . ' expireAll ' . $sessionId . ' DummyNamespace ZF-7196', $result, $returnValue);
  841. session_start();
  842. $result = $this->sortResult($result);
  843. $this->assertSame(';bar === 42', $result);
  844. }
  845. /**
  846. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  847. *
  848. * @return void
  849. */
  850. public function setExpireSessionVarsByHops()
  851. {
  852. $s = new Zend_Session_Namespace('expireGuava');
  853. $expireBeforeHop = 4;
  854. $s->setExpirationHops($expireBeforeHop, 'g');
  855. $s->g = 'guava';
  856. $s->p = 'peach';
  857. $s->p = 'plum';
  858. $id = session_id();
  859. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  860. session_write_close(); // release session so process below can use it
  861. exec("$this->_script expireAll $id expireGuava", $result);
  862. session_start(); // resume artificially suspended session
  863. $result = $this->sortResult($result);
  864. if ($i > $expireBeforeHop) {
  865. $this->assertTrue($result === ';p === plum',
  866. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  867. } else {
  868. $this->assertTrue($result === ';g === guava;p === plum',
  869. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  870. }
  871. }
  872. }
  873. /**
  874. * @todo PHP 5.2.1 is required (fixes a bug with magic __get() returning by reference)
  875. * @see http://framework.zend.com/issues/browse/ZF-800
  876. */
  877. public function testArrays()
  878. {
  879. $this->markTestIncomplete();
  880. $s = new Zend_Session_Namespace('aspace');
  881. $id = Zend_Session::getId();
  882. $this->assertSame($id, session_id());
  883. $s->top = 'begin';
  884. session_write_close(); // release session so process below can use it
  885. exec("$this->_script setArray $id aspace 1 2 3 4 5", $result);
  886. exec("$this->_script getArray $id aspace", $result);
  887. session_start(); // resume artificially suspended session
  888. $result = array_pop($result);
  889. $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";};';
  890. $this->assertTrue($result === $expect,
  891. "iteration over default Zend_Session namespace failed; expecting result ===\n$expect\n, but got\n$result\n)");
  892. }
  893. /**
  894. * test expiration of namespace variables by hops; expect expiration of specified keys in the proper number of hops
  895. *
  896. * @runInSeparateProcess
  897. * @return void
  898. */
  899. public function testSetExpireSessionVarsByHopsOnUse()
  900. {
  901. $s = new Zend_Session_Namespace('expireGuava');
  902. $expireBeforeHop = 2;
  903. $s->setExpirationHops($expireBeforeHop, 'g', true); // only count a hop, when namespace is used
  904. $s->g = 'guava';
  905. $s->p = 'peach';
  906. $s->p = 'plum';
  907. $id = session_id();
  908. // we are not accessing (using) the "expireGuava" namespace, so these hops should have no effect
  909. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  910. session_write_close(); // release session so process below can use it
  911. exec("$this->_script expireAll $id notused", $result);
  912. session_start(); // resume artificially suspended session
  913. $result = $this->sortResult($result);
  914. $this->assertTrue($result === '',
  915. "iteration over named Zend_Session namespace failed (result='$result'; hop #$i)");
  916. }
  917. for ($i = 1; $i <= ($expireBeforeHop + 2); $i++) {
  918. session_write_close(); // release session so process below can use it
  919. exec("$this->_script expireAll $id expireGuava", $result);
  920. session_start(); // resume artificially suspended session
  921. $result = $this->sortResult($result);
  922. if ($i > $expireBeforeHop) {
  923. $expect = ';p === plum';
  924. $this->assertTrue($result === $expect,
  925. "unexpected results iterating over named Zend_Session namespace (result='$result'; expected '$expect'; hop #$i)");
  926. } else {
  927. $expect = ';g === guava;p === plum';
  928. $this->assertTrue($result === $expect,
  929. "unexpected results iterating over named Zend_Session namespace (result='$result'; expected '$expect'; hop #$i)");
  930. }
  931. }
  932. // Do not destroy session since it still may be used by other tests
  933. // Zend_Session::destroy();
  934. }
  935. /**
  936. * @group ZF-5003
  937. */
  938. public function testProcessSessionMetadataShouldNotThrowAnError()
  939. {
  940. Zend_Session::$_unitTestEnabled = true;
  941. if (isset($_SESSION) && isset($_SESSION['__ZF'])) {
  942. unset($_SESSION['__ZF']);
  943. }
  944. Zend_Session::start();
  945. }
  946. /**
  947. * test for method getNamespace()
  948. *
  949. * @group ZF-1982
  950. * @return void
  951. */
  952. public function testGetNameSpaceMethod()
  953. {
  954. Zend_Session::$_unitTestEnabled = true;
  955. $namespace = array(
  956. 'FooBar',
  957. 'Foo_Bar',
  958. 'Foo-Bar',
  959. 'Foo1000'
  960. );
  961. foreach ($namespace as $v) {
  962. $s = new Zend_Session_Namespace($v);
  963. $this->assertEquals($v, $s->getNamespace());
  964. }
  965. }
  966. /**
  967. * @group ZF-11186
  968. */
  969. public function testNoNoticesIfNoValidatorDataInSession()
  970. {
  971. try {
  972. Zend_Session::start();
  973. require_once dirname(__FILE__) . '/Validator/NoticeValidator.php';
  974. Zend_Session::registerValidator(new Zend_Session_Validator_NoticeValidator);
  975. } catch (PHPUnit_Framework_Error_Notice $exception) {
  976. $this->fail($exception->getMessage());
  977. }
  978. }
  979. /**
  980. * @group ZF-3378
  981. */
  982. public function testInvalidPreexistingSessionIdDoesNotPreventRegenerationOfSid()
  983. {
  984. // Pattern: [0-9a-v]*
  985. ini_set('session.hash_bits_per_character', 5);
  986. // Session store
  987. $sessionCharSet = array_merge(range(0,9), range('a','v'));
  988. $sessionStore = dirname(__FILE__)
  989. . DIRECTORY_SEPARATOR . "_files"
  990. . DIRECTORY_SEPARATOR . "ZF-3378";
  991. if ( !is_dir($sessionStore) ) @mkdir($sessionStore, 0755, true);
  992. ini_set('session.save_path', "1;666;" . $sessionStore);
  993. // When using subdirs for session.save_path, the directory structure
  994. // is your own responsibility...set it up, or else bad things happen
  995. foreach ( $sessionCharSet as $subdir ) {
  996. @mkdir($sessionStore . DIRECTORY_SEPARATOR . $subdir);
  997. }
  998. // Set session ID to invalid value
  999. session_id('xxx');
  1000. // Attempt to start the session
  1001. try {
  1002. /** @see Zend_Session */
  1003. require_once "Zend/Session.php";
  1004. Zend_Session::start();
  1005. } catch (Zend_Session_Exception $e) {
  1006. Zend_Session::regenerateId();
  1007. }
  1008. // Get the current SID
  1009. $sid = Zend_Session::getId();
  1010. // We don't need the session any more, clean it up
  1011. //but we don't to want to destroy it completely, while other tests can start
  1012. Zend_Session::$_unitTestEnabled = true;
  1013. Zend_Session::destroy();
  1014. foreach ( $sessionCharSet as $subdir ) {
  1015. @rmdir($sessionStore . DIRECTORY_SEPARATOR . $subdir);
  1016. }
  1017. @rmdir($sessionStore);
  1018. // Check the result
  1019. $this->assertRegExp('/^[0-9a-v]+$/', $sid);
  1020. $this->assertNotEquals('xxx', $sid);
  1021. }
  1022. }