PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/suites/unit/joomla/session/JSessionTest.php

http://github.com/joomla/joomla-platform
PHP | 393 lines | 180 code | 38 blank | 175 comment | 1 complexity | 900c98e99d45cdf14c96d06aae350962 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. /**
  9. * Test class for JSession.
  10. * Generated by PHPUnit on 2011-10-26 at 19:33:07.
  11. *
  12. * @package Joomla.UnitTest
  13. * @subpackage Session
  14. * @since 11.1
  15. */
  16. class JSessionTest extends TestCase
  17. {
  18. /**
  19. * @var JSession
  20. */
  21. protected $object;
  22. /**
  23. * Sets up the fixture, for example, opens a network connection.
  24. * This method is called before a test is executed.
  25. *
  26. * @return void
  27. */
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->saveFactoryState();
  32. $this->object = JSession::getInstance('none', array('expire' => 20, 'force_ssl' => true, 'name' => 'name', 'id' => 'id', 'security' => 'security'));
  33. $this->input = new JInput;
  34. $this->input->cookie = $this->getMock('JInputCookie', array('set', 'get'));
  35. $this->object->initialise($this->input);
  36. $this->input->cookie->expects($this->any())
  37. ->method('set');
  38. $this->input->cookie->expects($this->any())
  39. ->method('get')
  40. ->will($this->returnValue(null));
  41. $this->object->start();
  42. }
  43. /**
  44. * Tears down the fixture, for example, closes a network connection.
  45. * This method is called after a test is executed.
  46. *
  47. * @return void
  48. */
  49. protected function tearDown()
  50. {
  51. if (session_id())
  52. {
  53. session_unset();
  54. session_destroy();
  55. }
  56. $this->restoreFactoryState();
  57. }
  58. /**
  59. * Test cases for getInstance
  60. * string handler of type JSessionStorage: none or database
  61. * array arguments for $options in form of associative array
  62. * string message if test case fails
  63. *
  64. * @return array
  65. */
  66. Public function casesGetInstance()
  67. {
  68. return array(
  69. 'first_instance' => array(
  70. 'none',
  71. array('expire' => 99),
  72. 'Line: ' . __LINE__ . ': ' . 'Should not be a different instance and options should not change'
  73. ),
  74. 'second_instance' => array(
  75. 'database',
  76. array(),
  77. 'Line: ' . __LINE__ . ': ' . 'Should not be a different instance '
  78. )
  79. );
  80. }
  81. /**
  82. * Test getInstance
  83. *
  84. * @param string $store @todo
  85. * @param array $options @todo
  86. *
  87. * @dataProvider casesGetInstance
  88. * @covers JSession::getInstance
  89. *
  90. * @return void
  91. */
  92. public function testGetInstance($store, $options)
  93. {
  94. $oldSession = $this->object;
  95. $newSession = JSession::getInstance($store, $options);
  96. // The properties and values should be identical to each other.
  97. $this->assertThat(
  98. $oldSession,
  99. $this->identicalTo($newSession)
  100. );
  101. // They should be the same object.
  102. $this->assertSame($oldSession, $newSession);
  103. }
  104. /**
  105. * Test getState
  106. *
  107. * @covers JSession::getState
  108. *
  109. * @return void
  110. */
  111. public function testGetState()
  112. {
  113. $this->assertEquals(
  114. TestReflection::getValue($this->object, '_state'),
  115. $this->object->getState(),
  116. 'Session state should be the same'
  117. );
  118. }
  119. /**
  120. * Test getExpire()
  121. *
  122. * @covers JSession::getExpire
  123. *
  124. * @return void
  125. */
  126. public function testGetExpire()
  127. {
  128. $this->assertEquals(
  129. TestReflection::getValue($this->object, '_expire'),
  130. $this->object->getExpire(),
  131. 'Session expire time should be the same'
  132. );
  133. }
  134. /**
  135. * Test getToken
  136. *
  137. * @covers JSession::getToken
  138. *
  139. * @return void
  140. */
  141. public function testGetToken()
  142. {
  143. $this->object->set('session.token', 'abc');
  144. $this->assertEquals('abc', $this->object->getToken(), 'Token should be abc');
  145. $this->object->set('session.token', null);
  146. $token = $this->object->getToken();
  147. $this->assertEquals(32, strlen($token), 'Line: ' . __LINE__ . ' Token should be length 32');
  148. $token2 = $this->object->getToken(true);
  149. $this->assertNotEquals($token, $token2, 'Line: ' . __LINE__ . ' New token should be different');
  150. }
  151. /**
  152. * Test hasToken
  153. *
  154. * @covers JSession::hasToken
  155. *
  156. * @return void
  157. */
  158. public function testHasToken()
  159. {
  160. $token = $this->object->getToken();
  161. $this->assertTrue($this->object->hasToken($token), 'Line: ' . __LINE__ . ' Correct token should be true');
  162. $this->assertFalse($this->object->hasToken('abc', false), 'Line: ' . __LINE__ . ' Should return false with wrong token');
  163. $this->assertEquals('active', $this->object->getState(), 'Line: ' . __LINE__ . ' State should not be set to expired');
  164. $this->assertFalse($this->object->hasToken('abc'), 'Line: ' . __LINE__ . ' Should return false with wrong token');
  165. $this->assertEquals('expired', $this->object->getState(), 'Line: ' . __LINE__ . ' State should be set to expired by default');
  166. }
  167. /**
  168. * Test getFormToken
  169. *
  170. * @covers JSession::getFormToken
  171. *
  172. * @return void
  173. */
  174. public function testGetFormToken()
  175. {
  176. $user = JFactory::getUser();
  177. JFactory::$application = $this->getMock('JInputCookie', array('set', 'get'));
  178. JFactory::$application->expects($this->once())
  179. ->method('get')
  180. ->with($this->equalTo('secret'))
  181. ->will($this->returnValue('abc'));
  182. $this->object->set('secret', 'abc');
  183. $expected = md5('abc' . $user->get('id', 0) . $this->object->getToken(false));
  184. $this->assertEquals($expected, $this->object->getFormToken(), 'Form token should be calculated as above.');
  185. }
  186. /**
  187. * Test getName
  188. *
  189. * @covers JSession::getName
  190. *
  191. * @return void
  192. */
  193. public function testGetName()
  194. {
  195. $this->assertEquals(session_name(), $this->object->getName(), 'Session names should match.');
  196. }
  197. /**
  198. * Test getId
  199. *
  200. * @covers JSession::getId
  201. *
  202. * @return void
  203. */
  204. public function testGetId()
  205. {
  206. $this->assertEquals(session_id(), $this->object->getId(), 'Session ids should match.');
  207. }
  208. /**
  209. * Test getStores
  210. *
  211. * @covers JSession::getStores
  212. *
  213. * @return void
  214. */
  215. public function testGetStores()
  216. {
  217. $return = JSession::getStores();
  218. $this->assertTrue(
  219. is_array($return),
  220. 'Line: ' . __LINE__ . ' JSession::getStores must return an array.'
  221. );
  222. $this->assertContains(
  223. 'database',
  224. $return,
  225. 'Line: ' . __LINE__ . ' session storage database should always be available.'
  226. );
  227. $this->assertContains(
  228. 'none',
  229. $return,
  230. 'Line: ' . __LINE__ . ' session storage "none" should always be available.'
  231. );
  232. }
  233. /**
  234. * Test isNew
  235. *
  236. * @return void
  237. */
  238. public function testIsNew()
  239. {
  240. $this->object->set('session.counter', 1);
  241. $this->assertEquals(true, $this->object->isNew(), '$isNew should be true.');
  242. }
  243. /**
  244. * Test...
  245. *
  246. * @todo Implement testGet().
  247. *
  248. * @return void
  249. */
  250. public function testGet()
  251. {
  252. // Remove the following lines when you implement this test.
  253. $this->markTestIncomplete(
  254. 'This test has not been implemented yet.'
  255. );
  256. }
  257. /**
  258. * Test...
  259. *
  260. * @todo Implement testSet().
  261. *
  262. * @return void
  263. */
  264. public function testSet()
  265. {
  266. // Remove the following lines when you implement this test.
  267. $this->markTestIncomplete(
  268. 'This test has not been implemented yet.'
  269. );
  270. }
  271. /**
  272. * Test...
  273. *
  274. * @todo Implement testHas().
  275. *
  276. * @return void
  277. */
  278. public function testHas()
  279. {
  280. // Remove the following lines when you implement this test.
  281. $this->markTestIncomplete(
  282. 'This test has not been implemented yet.'
  283. );
  284. }
  285. /**
  286. * Test...
  287. *
  288. * @todo Implement testClear().
  289. *
  290. * @return void
  291. */
  292. public function testClear()
  293. {
  294. // Remove the following lines when you implement this test.
  295. $this->markTestIncomplete(
  296. 'This test has not been implemented yet.'
  297. );
  298. }
  299. /**
  300. * Test...
  301. *
  302. * @todo Implement testDestroy().
  303. *
  304. * @return void
  305. */
  306. public function testDestroy()
  307. {
  308. // Remove the following lines when you implement this test.
  309. $this->markTestIncomplete(
  310. 'This test has not been implemented yet.'
  311. );
  312. }
  313. /**
  314. * Test...
  315. *
  316. * @todo Implement testRestart().
  317. *
  318. * @return void
  319. */
  320. public function testRestart()
  321. {
  322. // Remove the following lines when you implement this test.
  323. $this->markTestIncomplete(
  324. 'This test has not been implemented yet.'
  325. );
  326. }
  327. /**
  328. * Test...
  329. *
  330. * @todo Implement testFork().
  331. *
  332. * @return void
  333. */
  334. public function testFork()
  335. {
  336. // Remove the following lines when you implement this test.
  337. $this->markTestIncomplete(
  338. 'This test has not been implemented yet.'
  339. );
  340. }
  341. /**
  342. * Test...
  343. *
  344. * @todo Implement testClose().
  345. *
  346. * @return void
  347. */
  348. public function testClose()
  349. {
  350. // Remove the following lines when you implement this test.
  351. $this->markTestIncomplete(
  352. 'This test has not been implemented yet.'
  353. );
  354. }
  355. }