/dev/tests/integration/testsuite/Mage/Core/Model/Resource/SessionTest.php

https://bitbucket.org/jokusafet/magento2 · PHP · 208 lines · 99 code · 25 blank · 84 comment · 0 complexity · 5c41d97ec8da33003833ea181aa6197a MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package Mage_Core
  23. * @subpackage integration_tests
  24. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  25. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  26. */
  27. class Mage_Core_Model_Resource_SessionTest extends PHPUnit_Framework_TestCase
  28. {
  29. /**
  30. * Test session ID
  31. */
  32. const SESSION_ID = 'session_id_value';
  33. /**#@+
  34. * Session keys
  35. */
  36. const SESSION_NEW = 'session_new';
  37. const SESSION_EXISTS = 'session_exists';
  38. /**#@-*/
  39. /**#@+
  40. * Table column names
  41. */
  42. const COLUMN_SESSION_ID = 'session_id';
  43. const COLUMN_SESSION_DATA = 'session_data';
  44. const COLUMN_SESSION_EXPIRES = 'session_expires';
  45. /**#@-*/
  46. /**
  47. * Test session data
  48. *
  49. * @var array
  50. */
  51. protected $_sourceData = array(
  52. self::SESSION_NEW => array('new key' => 'new value'),
  53. self::SESSION_EXISTS => array('existing key' => 'existing value'),
  54. );
  55. /**
  56. * Data as objects for serialization
  57. *
  58. * @var array
  59. */
  60. protected $_sessionData;
  61. /**
  62. * @var Magento_Test_ObjectManager
  63. */
  64. protected $_objectManager;
  65. /**
  66. * Model under test
  67. *
  68. * @var Mage_Core_Model_Resource_Session
  69. */
  70. protected $_model;
  71. /**
  72. * Write connection adapter
  73. *
  74. * @var Varien_Db_Adapter_Interface
  75. */
  76. protected $_connection;
  77. /**
  78. * Session table name
  79. *
  80. * @var string
  81. */
  82. protected $_sessionTable;
  83. public function setUp()
  84. {
  85. $this->_objectManager = Mage::getObjectManager();
  86. $this->_model = $this->_objectManager->get('Mage_Core_Model_Resource_Session');
  87. /** @var $resource Mage_Core_Model_Resource */
  88. $resource = $this->_objectManager->get('Mage_Core_Model_Resource');
  89. $this->_connection = $resource->getConnection('core_write');
  90. $this->_sessionTable = $resource->getTableName('core_session');
  91. // session stores serialized objects with protected properties
  92. // we need to test this case to ensure that DB adapter successfully processes "\0" symbols in serialized data
  93. foreach ($this->_sourceData as $key => $data) {
  94. $this->_sessionData[$key] = new Varien_Object($data);
  95. }
  96. }
  97. protected function tearDown()
  98. {
  99. unset($this->_objectManager);
  100. unset($this->_model);
  101. unset($this->_connection);
  102. unset($this->_sessionTable);
  103. unset($this->_sessionData);
  104. }
  105. public function testHasConnection()
  106. {
  107. $this->assertTrue($this->_model->hasConnection());
  108. }
  109. public function testOpenAndClose()
  110. {
  111. $this->assertTrue($this->_model->open('', 'test'));
  112. $this->assertTrue($this->_model->close());
  113. }
  114. public function testWriteReadDestroy()
  115. {
  116. $data = serialize($this->_sessionData[self::SESSION_NEW]);
  117. $this->_model->write(self::SESSION_ID, $data);
  118. $this->assertEquals($data, $this->_model->read(self::SESSION_ID));
  119. $data = serialize($this->_sessionData[self::SESSION_EXISTS]);
  120. $this->_model->write(self::SESSION_ID, $data);
  121. $this->assertEquals($data, $this->_model->read(self::SESSION_ID));
  122. $this->_model->destroy(self::SESSION_ID);
  123. $this->assertEmpty($this->_model->read(self::SESSION_ID));
  124. }
  125. public function testGc()
  126. {
  127. $this->_model->write('test', 'test');
  128. $this->assertEquals('test', $this->_model->read('test'));
  129. $this->_model->gc(-1);
  130. $this->assertEmpty($this->_model->read('test'));
  131. }
  132. /**
  133. * Assert that session data writes to DB in base64 encoding
  134. */
  135. public function testWriteEncoded()
  136. {
  137. $data = serialize($this->_sessionData[self::SESSION_NEW]);
  138. $this->_model->write(self::SESSION_ID, $data);
  139. $select = $this->_connection->select()
  140. ->from($this->_sessionTable)
  141. ->where(self::COLUMN_SESSION_ID . ' = :' . self::COLUMN_SESSION_ID);
  142. $bind = array(self::COLUMN_SESSION_ID => self::SESSION_ID);
  143. $session = $this->_connection->fetchRow($select, $bind);
  144. $this->assertEquals(self::SESSION_ID, $session[self::COLUMN_SESSION_ID]);
  145. $this->assertTrue(
  146. ctype_digit((string)$session[self::COLUMN_SESSION_EXPIRES]),
  147. 'Value of session expire field must have integer type'
  148. );
  149. $this->assertEquals($data, base64_decode($session[self::COLUMN_SESSION_DATA]));
  150. }
  151. /**
  152. * Data provider for testReadEncoded
  153. *
  154. * @return array
  155. */
  156. public function readEncodedDataProvider()
  157. {
  158. // we can't use object data as a fixture because not encoded serialized object
  159. // might cause DB adapter fatal error, so we have to use array as a fixture
  160. $sessionData = serialize($this->_sourceData[self::SESSION_NEW]);
  161. return array(
  162. 'session_encoded' => array('$sessionData' => base64_encode($sessionData)),
  163. 'session_not_encoded' => array('$sessionData' => $sessionData),
  164. );
  165. }
  166. /**
  167. * Assert that session data reads from DB correctly regardless of encoding
  168. *
  169. * @param string $sessionData
  170. *
  171. * @dataProvider readEncodedDataProvider
  172. */
  173. public function testReadEncoded($sessionData)
  174. {
  175. $sessionRecord = array(
  176. self::COLUMN_SESSION_ID => self::SESSION_ID,
  177. self::COLUMN_SESSION_DATA => $sessionData,
  178. );
  179. $this->_connection->insertOnDuplicate($this->_sessionTable, $sessionRecord, array(self::COLUMN_SESSION_DATA));
  180. $sessionData = $this->_model->read(self::SESSION_ID);
  181. $this->assertEquals($this->_sourceData[self::SESSION_NEW], unserialize($sessionData));
  182. }
  183. }