PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Framework/Session/ConfigTest.php

https://bitbucket.org/minosss/magento2
PHP | 379 lines | 302 code | 43 blank | 34 comment | 8 complexity | 608ac84f6d94f3018a24368dc6282901 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreStart
  7. namespace {
  8. $mockPHPFunctions = false;
  9. }
  10. namespace Magento\Framework\Session {
  11. use Magento\Framework\App\Filesystem\DirectoryList;
  12. // @codingStandardsIgnoreEnd
  13. /**
  14. * Mock ini_get global function
  15. *
  16. * @return string
  17. */
  18. function ini_get($varName)
  19. {
  20. global $mockPHPFunctions;
  21. if ($mockPHPFunctions == 1) {
  22. switch ($varName) {
  23. case 'session.save_path':
  24. return 'preset_save_path';
  25. case 'session.save_handler':
  26. return 'php';
  27. default:
  28. return '';
  29. }
  30. } elseif ($mockPHPFunctions == 2) {
  31. return null;
  32. }
  33. return call_user_func_array('\ini_get', func_get_args());
  34. }
  35. /**
  36. * @magentoAppIsolation enabled
  37. */
  38. class ConfigTest extends \PHPUnit\Framework\TestCase
  39. {
  40. /** @var string */
  41. private $_cacheLimiter = 'private_no_expire';
  42. /** @var \Magento\TestFramework\ObjectManager */
  43. private $_objectManager;
  44. /** @var string Default value for session.save_path setting */
  45. private $defaultSavePath;
  46. /** @var \Magento\Framework\App\DeploymentConfig | \PHPUnit_Framework_MockObject_MockObject */
  47. private $deploymentConfigMock;
  48. protected function setUp()
  49. {
  50. $this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  51. $this->deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  52. $this->deploymentConfigMock
  53. ->method('get')
  54. ->willReturnCallback(function ($configPath) {
  55. switch ($configPath) {
  56. case Config::PARAM_SESSION_SAVE_METHOD:
  57. return 'files';
  58. case Config::PARAM_SESSION_CACHE_LIMITER:
  59. return $this->_cacheLimiter;
  60. default:
  61. return null;
  62. }
  63. });
  64. $this->defaultSavePath = $this->_objectManager
  65. ->get(\Magento\Framework\Filesystem\DirectoryList::class)
  66. ->getPath(DirectoryList::SESSION);
  67. }
  68. /**
  69. * @magentoAppIsolation enabled
  70. */
  71. public function testDefaultConfiguration()
  72. {
  73. $model = $this->getModel();
  74. /** @var \Magento\Framework\Filesystem $filesystem */
  75. $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  76. \Magento\Framework\Filesystem::class
  77. );
  78. $path = ini_get('session.save_path') ?:
  79. $filesystem->getDirectoryRead(DirectoryList::SESSION)->getAbsolutePath();
  80. $this->assertEquals(
  81. $path,
  82. $model->getSavePath()
  83. );
  84. $this->assertEquals(
  85. \Magento\Framework\Session\Config::COOKIE_LIFETIME_DEFAULT,
  86. $model->getCookieLifetime()
  87. );
  88. $this->assertEquals($this->_cacheLimiter, $model->getCacheLimiter());
  89. $this->assertEquals('/', $model->getCookiePath());
  90. $this->assertEquals('localhost', $model->getCookieDomain());
  91. $this->assertEquals(false, $model->getCookieSecure());
  92. $this->assertEquals(true, $model->getCookieHttpOnly());
  93. $this->assertEquals($model->getSavePath(), $model->getOption('save_path'));
  94. }
  95. public function testSetOptionsInvalidValue()
  96. {
  97. $model = $this->getModel();
  98. $preValue = $model->getOptions();
  99. $model->setOptions('');
  100. $this->assertEquals($preValue, $model->getOptions());
  101. }
  102. /**
  103. * @dataProvider optionsProvider
  104. */
  105. public function testSetOptions($option, $getter, $value)
  106. {
  107. $model = $this->getModel();
  108. $options = [$option => $value];
  109. $model->setOptions($options);
  110. $this->assertSame($value, $model->{$getter}());
  111. }
  112. public function optionsProvider()
  113. {
  114. return [
  115. ['save_path', 'getSavePath', __DIR__],
  116. ['name', 'getName', 'FOOBAR'],
  117. ['gc_probability', 'getGcProbability', 42],
  118. ['gc_divisor', 'getGcDivisor', 3],
  119. ['gc_maxlifetime', 'getGcMaxlifetime', 180],
  120. ['serialize_handler', 'getSerializeHandler', 'php_binary'],
  121. ['cookie_lifetime', 'getCookieLifetime', 180],
  122. ['cookie_path', 'getCookiePath', '/foo/bar'],
  123. ['cookie_domain', 'getCookieDomain', 'framework.zend.com'],
  124. ['cookie_secure', 'getCookieSecure', true],
  125. ['cookie_httponly', 'getCookieHttpOnly', true],
  126. ['use_cookies', 'getUseCookies', false],
  127. ['use_only_cookies', 'getUseOnlyCookies', true],
  128. ['referer_check', 'getRefererCheck', 'foobar'],
  129. ['entropy_file', 'getEntropyFile', __FILE__],
  130. ['entropy_length', 'getEntropyLength', 42],
  131. ['cache_limiter', 'getCacheLimiter', 'private'],
  132. ['cache_expire', 'getCacheExpire', 42],
  133. ['use_trans_sid', 'getUseTransSid', true],
  134. ['hash_function', 'getHashFunction', 'md5'],
  135. ['hash_bits_per_character', 'getHashBitsPerCharacter', 5],
  136. ['url_rewriter_tags', 'getUrlRewriterTags', 'a=href']
  137. ];
  138. }
  139. public function testNameIsMutable()
  140. {
  141. $model = $this->getModel();
  142. $model->setName('FOOBAR');
  143. $this->assertEquals('FOOBAR', $model->getName());
  144. }
  145. public function testCookieLifetimeIsMutable()
  146. {
  147. $model = $this->getModel();
  148. $model->setCookieLifetime(20);
  149. $this->assertEquals(20, $model->getCookieLifetime());
  150. }
  151. public function testCookieLifetimeCanBeZero()
  152. {
  153. $model = $this->getModel();
  154. $model->setCookieLifetime(0);
  155. $this->assertEquals(0, $model->getCookieLifetime());
  156. }
  157. public function testSettingInvalidCookieLifetime()
  158. {
  159. $model = $this->getModel();
  160. $preVal = $model->getCookieLifetime();
  161. $model->setCookieLifetime('foobar_bogus');
  162. $this->assertEquals($preVal, $model->getCookieLifetime());
  163. }
  164. public function testSettingInvalidCookieLifetime2()
  165. {
  166. $model = $this->getModel();
  167. $preVal = $model->getCookieLifetime();
  168. $model->setCookieLifetime(-1);
  169. $this->assertEquals($preVal, $model->getCookieLifetime());
  170. }
  171. public function testWrongMethodCall()
  172. {
  173. $model = $this->getModel();
  174. $this->expectException(
  175. '\BadMethodCallException',
  176. 'Method "methodThatNotExist" does not exist in Magento\Framework\Session\Config'
  177. );
  178. $model->methodThatNotExist();
  179. }
  180. public function testCookieSecureDefaultsToIniSettings()
  181. {
  182. $model = $this->getModel();
  183. $this->assertSame((bool)ini_get('session.cookie_secure'), $model->getCookieSecure());
  184. }
  185. public function testSetCookieSecureInOptions()
  186. {
  187. $model = $this->getModel();
  188. $model->setCookieSecure(true);
  189. $this->assertTrue($model->getCookieSecure());
  190. }
  191. public function testCookieDomainIsMutable()
  192. {
  193. $model = $this->getModel();
  194. $model->setCookieDomain('example.com');
  195. $this->assertEquals('example.com', $model->getCookieDomain());
  196. }
  197. public function testCookieDomainCanBeEmpty()
  198. {
  199. $model = $this->getModel();
  200. $model->setCookieDomain('');
  201. $this->assertEquals('', $model->getCookieDomain());
  202. }
  203. public function testSettingInvalidCookieDomain()
  204. {
  205. $model = $this->getModel();
  206. $preVal = $model->getCookieDomain();
  207. $model->setCookieDomain(24);
  208. $this->assertEquals($preVal, $model->getCookieDomain());
  209. }
  210. public function testSettingInvalidCookieDomain2()
  211. {
  212. $model = $this->getModel();
  213. $preVal = $model->getCookieDomain();
  214. $model->setCookieDomain('D:\\WINDOWS\\System32\\drivers\\etc\\hosts');
  215. $this->assertEquals($preVal, $model->getCookieDomain());
  216. }
  217. public function testSetCookieHttpOnlyInOptions()
  218. {
  219. $model = $this->getModel();
  220. $model->setCookieHttpOnly(true);
  221. $this->assertTrue($model->getCookieHttpOnly());
  222. }
  223. public function testUseCookiesDefaultsToIniSettings()
  224. {
  225. $model = $this->getModel();
  226. $this->assertSame((bool)ini_get('session.use_cookies'), $model->getUseCookies());
  227. }
  228. public function testSetUseCookiesInOptions()
  229. {
  230. $model = $this->getModel();
  231. $model->setUseCookies(true);
  232. $this->assertTrue($model->getUseCookies());
  233. }
  234. public function testUseOnlyCookiesDefaultsToIniSettings()
  235. {
  236. $model = $this->getModel();
  237. $this->assertSame((bool)ini_get('session.use_only_cookies'), $model->getUseOnlyCookies());
  238. }
  239. public function testSetUseOnlyCookiesInOptions()
  240. {
  241. $model = $this->getModel();
  242. $model->setOption('use_only_cookies', true);
  243. $this->assertTrue((bool)$model->getOption('use_only_cookies'));
  244. }
  245. public function testRefererCheckDefaultsToIniSettings()
  246. {
  247. $model = $this->getModel();
  248. $this->assertSame(ini_get('session.referer_check'), $model->getRefererCheck());
  249. }
  250. public function testRefererCheckIsMutable()
  251. {
  252. $model = $this->getModel();
  253. $model->setOption('referer_check', 'FOOBAR');
  254. $this->assertEquals('FOOBAR', $model->getOption('referer_check'));
  255. }
  256. public function testRefererCheckMayBeEmpty()
  257. {
  258. $model = $this->getModel();
  259. $model->setOption('referer_check', '');
  260. $this->assertEquals('', $model->getOption('referer_check'));
  261. }
  262. public function testSetSavePath()
  263. {
  264. $model = $this->getModel();
  265. $model->setSavePath('some_save_path');
  266. $this->assertEquals($model->getOption('save_path'), 'some_save_path');
  267. }
  268. /**
  269. * @param $mockPHPFunctionNum
  270. * @param $givenSavePath
  271. * @param $expectedSavePath
  272. * @param $givenSaveHandler
  273. * @param $expectedSaveHandler
  274. * @dataProvider constructorDataProvider
  275. */
  276. public function testConstructor(
  277. $mockPHPFunctionNum,
  278. $givenSavePath,
  279. $expectedSavePath,
  280. $givenSaveHandler,
  281. $expectedSaveHandler
  282. ) {
  283. global $mockPHPFunctions;
  284. $mockPHPFunctions = $mockPHPFunctionNum;
  285. $sessionSaveHandler = ini_get('session.save_handler');
  286. if ($expectedSavePath === 'default') {
  287. $expectedSavePath = $this->defaultSavePath . '/';
  288. }
  289. if ($expectedSaveHandler === 'php') {
  290. $expectedSaveHandler = $sessionSaveHandler;
  291. }
  292. $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  293. $deploymentConfigMock
  294. ->method('get')
  295. ->willReturnCallback(function ($configPath) use ($givenSavePath, $givenSaveHandler) {
  296. switch ($configPath) {
  297. case Config::PARAM_SESSION_SAVE_METHOD:
  298. return $givenSaveHandler;
  299. case Config::PARAM_SESSION_CACHE_LIMITER:
  300. return $this->_cacheLimiter;
  301. case Config::PARAM_SESSION_SAVE_PATH:
  302. return $givenSavePath;
  303. default:
  304. return null;
  305. }
  306. });
  307. $model = $this->_objectManager->create(
  308. \Magento\Framework\Session\Config::class,
  309. ['deploymentConfig' => $deploymentConfigMock]
  310. );
  311. $this->assertEquals($expectedSavePath, $model->getOption('save_path'));
  312. $this->assertEquals($expectedSaveHandler, $model->getOption('session.save_handler'));
  313. global $mockPHPFunctions;
  314. $mockPHPFunctions = false;
  315. }
  316. public function constructorDataProvider()
  317. {
  318. // preset value (null = not set), input value (null = not set), expected value
  319. $savePathGiven = 'explicit_save_path';
  320. $presetPath = 'preset_save_path';
  321. return [
  322. [2, $savePathGiven, $savePathGiven, 'db', 'db'],
  323. [2, null, 'default', 'redis', 'redis'],
  324. [1, $savePathGiven, $savePathGiven, null, 'php'],
  325. [1, null, $presetPath, 'files', 'files'],
  326. ];
  327. }
  328. private function getModel(): \Magento\Framework\Session\Config
  329. {
  330. return $this->_objectManager->create(
  331. \Magento\Framework\Session\Config::class,
  332. ['deploymentConfig' => $this->deploymentConfigMock]
  333. );
  334. }
  335. }
  336. }