ConfigTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test class for \Magento\Framework\Session\Config
  8. */
  9. namespace Magento\Framework\Session\Test\Unit;
  10. use \Magento\Framework\Session\Config;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class ConfigTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  18. */
  19. protected $helper;
  20. /**
  21. * @var \Magento\Framework\Session\Config
  22. */
  23. protected $config;
  24. /**
  25. * @var \Magento\Framework\App\Config\ScopeConfigInterface | \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $configMock;
  28. /**
  29. * @var \Magento\Framework\ValidatorFactory | \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $validatorFactoryMock;
  32. /**
  33. * @var \Magento\Framework\Validator\ValidatorInterface | \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $validatorMock;
  36. /**
  37. * @var \Magento\Framework\App\Request\Http | \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $requestMock;
  40. /**
  41. * @var \Magento\Framework\Filesystem | \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $filesystem;
  44. protected function setUp()
  45. {
  46. $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  47. $this->validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->validatorMock->expects($this->any())
  51. ->method('isValid')
  52. ->willReturn(true);
  53. }
  54. public function testSetOptionsInvalidValue()
  55. {
  56. $this->getModel($this->validatorMock);
  57. $preVal = $this->config->getOptions();
  58. $this->config->setOptions('');
  59. $this->assertEquals($preVal, $this->config->getOptions());
  60. }
  61. /**
  62. * @dataProvider optionsProvider
  63. */
  64. public function testSetOptions($option, $getter, $value)
  65. {
  66. $this->getModel($this->validatorMock);
  67. $options = [$option => $value];
  68. $this->config->setOptions($options);
  69. $this->assertSame($value, $this->config->{$getter}());
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function optionsProvider()
  75. {
  76. return [
  77. ['save_path', 'getSavePath', __DIR__],
  78. ['name', 'getName', 'FOOBAR'],
  79. ['gc_probability', 'getGcProbability', 42],
  80. ['gc_divisor', 'getGcDivisor', 3],
  81. ['gc_maxlifetime', 'getGcMaxlifetime', 180],
  82. ['serialize_handler', 'getSerializeHandler', 'php_binary'],
  83. ['cookie_lifetime', 'getCookieLifetime', 180],
  84. ['cookie_path', 'getCookiePath', '/foo/bar'],
  85. ['cookie_domain', 'getCookieDomain', 'framework.zend.com'],
  86. ['cookie_secure', 'getCookieSecure', true],
  87. ['cookie_httponly', 'getCookieHttpOnly', true],
  88. ['use_cookies', 'getUseCookies', false],
  89. ['use_only_cookies', 'getUseOnlyCookies', true],
  90. ['referer_check', 'getRefererCheck', 'foobar'],
  91. ['entropy_file', 'getEntropyFile', __FILE__],
  92. ['entropy_length', 'getEntropyLength', 42],
  93. ['cache_limiter', 'getCacheLimiter', 'private'],
  94. ['cache_expire', 'getCacheExpire', 42],
  95. ['use_trans_sid', 'getUseTransSid', true],
  96. ['hash_function', 'getHashFunction', 'md5'],
  97. ['hash_bits_per_character', 'getHashBitsPerCharacter', 5],
  98. ['url_rewriter_tags', 'getUrlRewriterTags', 'a=href']
  99. ];
  100. }
  101. public function testGetOptions()
  102. {
  103. $this->getModel($this->validatorMock);
  104. $appStateProperty = new \ReflectionProperty(\Magento\Framework\Session\Config::class, 'options');
  105. $appStateProperty->setAccessible(true);
  106. $original = $appStateProperty->getValue($this->config);
  107. $valueForTest = ['test' => 'test2'];
  108. $appStateProperty->setValue($this->config, $valueForTest);
  109. $this->assertEquals($valueForTest, $this->config->getOptions());
  110. $this->assertEquals($valueForTest, $this->config->toArray());
  111. $appStateProperty->setValue($this->config, $original);
  112. $this->assertEquals($original, $this->config->getOptions());
  113. $this->assertEquals($original, $this->config->toArray());
  114. }
  115. public function testNameIsMutable()
  116. {
  117. $this->getModel($this->validatorMock);
  118. $this->config->setName('FOOBAR');
  119. $this->assertEquals('FOOBAR', $this->config->getName());
  120. }
  121. public function testCookieLifetimeIsMutable()
  122. {
  123. $this->getModel($this->validatorMock);
  124. $this->config->setCookieLifetime(20);
  125. $this->assertEquals(20, $this->config->getCookieLifetime());
  126. }
  127. public function testCookieLifetimeCanBeZero()
  128. {
  129. $this->getModel($this->validatorMock);
  130. $this->config->setCookieLifetime(0);
  131. $this->assertEquals(0, ini_get('session.cookie_lifetime'));
  132. }
  133. public function testSettingInvalidCookieLifetime()
  134. {
  135. $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
  136. ->disableOriginalConstructor()
  137. ->getMock();
  138. $validatorMock->expects($this->any())
  139. ->method('isValid')
  140. ->willReturn(false);
  141. $this->getModel($validatorMock);
  142. $preVal = $this->config->getCookieLifetime();
  143. $this->config->setCookieLifetime('foobar_bogus');
  144. $this->assertEquals($preVal, $this->config->getCookieLifetime());
  145. }
  146. public function testSettingInvalidCookieLifetime2()
  147. {
  148. $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
  149. ->disableOriginalConstructor()
  150. ->getMock();
  151. $validatorMock->expects($this->any())
  152. ->method('isValid')
  153. ->willReturn(false);
  154. $this->getModel($validatorMock);
  155. $preVal = $this->config->getCookieLifetime();
  156. $this->config->setCookieLifetime(-1);
  157. $this->assertEquals($preVal, $this->config->getCookieLifetime());
  158. }
  159. public function testWrongMethodCall()
  160. {
  161. $this->getModel($this->validatorMock);
  162. $this->expectException('\BadMethodCallException');
  163. $this->expectExceptionMessage('Method "methodThatNotExist" does not exist in Magento\Framework\Session\Config');
  164. $this->config->methodThatNotExist();
  165. }
  166. public function testCookieSecureDefaultsToIniSettings()
  167. {
  168. $this->getModel($this->validatorMock);
  169. $this->assertSame((bool)ini_get('session.cookie_secure'), $this->config->getCookieSecure());
  170. }
  171. public function testCookieSecureIsMutable()
  172. {
  173. $this->getModel($this->validatorMock);
  174. $value = ini_get('session.cookie_secure') ? false : true;
  175. $this->config->setCookieSecure($value);
  176. $this->assertEquals($value, $this->config->getCookieSecure());
  177. }
  178. public function testCookieDomainIsMutable()
  179. {
  180. $this->getModel($this->validatorMock);
  181. $this->config->setCookieDomain('example.com');
  182. $this->assertEquals('example.com', $this->config->getCookieDomain());
  183. }
  184. public function testCookieDomainCanBeEmpty()
  185. {
  186. $this->getModel($this->validatorMock);
  187. $this->config->setCookieDomain('');
  188. $this->assertEquals('', $this->config->getCookieDomain());
  189. }
  190. public function testSettingInvalidCookieDomain()
  191. {
  192. $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
  193. ->disableOriginalConstructor()
  194. ->getMock();
  195. $validatorMock->expects($this->any())
  196. ->method('isValid')
  197. ->willReturn(false);
  198. $this->getModel($validatorMock);
  199. $preVal = $this->config->getCookieDomain();
  200. $this->config->setCookieDomain(24);
  201. $this->assertEquals($preVal, $this->config->getCookieDomain());
  202. }
  203. public function testSettingInvalidCookieDomain2()
  204. {
  205. $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
  206. ->disableOriginalConstructor()
  207. ->getMock();
  208. $validatorMock->expects($this->any())
  209. ->method('isValid')
  210. ->willReturn(false);
  211. $this->getModel($validatorMock);
  212. $preVal = $this->config->getCookieDomain();
  213. $this->config->setCookieDomain('D:\\WINDOWS\\System32\\drivers\\etc\\hosts');
  214. $this->assertEquals($preVal, $this->config->getCookieDomain());
  215. }
  216. public function testCookieHttpOnlyDefaultsToIniSettings()
  217. {
  218. $this->getModel($this->validatorMock);
  219. $this->assertSame((bool)ini_get('session.cookie_httponly'), $this->config->getCookieHttpOnly());
  220. }
  221. public function testCookieHttpOnlyIsMutable()
  222. {
  223. $this->getModel($this->validatorMock);
  224. $value = ini_get('session.cookie_httponly') ? false : true;
  225. $this->config->setCookieHttpOnly($value);
  226. $this->assertEquals($value, $this->config->getCookieHttpOnly());
  227. }
  228. public function testUseCookiesDefaultsToIniSettings()
  229. {
  230. $this->getModel($this->validatorMock);
  231. $this->assertSame((bool)ini_get('session.use_cookies'), $this->config->getUseCookies());
  232. }
  233. public function testUseCookiesIsMutable()
  234. {
  235. $this->getModel($this->validatorMock);
  236. $value = ini_get('session.use_cookies') ? false : true;
  237. $this->config->setUseCookies($value);
  238. $this->assertEquals($value, (bool)$this->config->getUseCookies());
  239. }
  240. public function testUseOnlyCookiesDefaultsToIniSettings()
  241. {
  242. $this->getModel($this->validatorMock);
  243. $this->assertSame((bool)ini_get('session.use_only_cookies'), $this->config->getUseOnlyCookies());
  244. }
  245. public function testUseOnlyCookiesIsMutable()
  246. {
  247. $this->getModel($this->validatorMock);
  248. $value = ini_get('session.use_only_cookies') ? false : true;
  249. $this->config->setOption('use_only_cookies', $value);
  250. $this->assertEquals($value, (bool)$this->config->getOption('use_only_cookies'));
  251. }
  252. public function testRefererCheckDefaultsToIniSettings()
  253. {
  254. $this->getModel($this->validatorMock);
  255. $this->assertSame(ini_get('session.referer_check'), $this->config->getRefererCheck());
  256. }
  257. public function testRefererCheckIsMutable()
  258. {
  259. $this->getModel($this->validatorMock);
  260. $this->config->setOption('referer_check', 'FOOBAR');
  261. $this->assertEquals('FOOBAR', $this->config->getOption('referer_check'));
  262. }
  263. public function testRefererCheckMayBeEmpty()
  264. {
  265. $this->getModel($this->validatorMock);
  266. $this->config->setOption('referer_check', '');
  267. $this->assertEquals('', $this->config->getOption('referer_check'));
  268. }
  269. public function testSetSavePath()
  270. {
  271. $this->getModel($this->validatorMock);
  272. $this->config->setSavePath('some_save_path');
  273. $this->assertEquals($this->config->getOption('save_path'), 'some_save_path');
  274. }
  275. /**
  276. * @param bool $isValidSame
  277. * @param bool $isValid
  278. * @param array $expected
  279. * @dataProvider constructorDataProvider
  280. */
  281. public function testConstructor($isValidSame, $isValid, $expected)
  282. {
  283. $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
  284. ->disableOriginalConstructor()
  285. ->getMock();
  286. if ($isValidSame) {
  287. $validatorMock->expects($this->any())
  288. ->method('isValid')
  289. ->willReturn($isValid);
  290. } else {
  291. for ($x = 0; $x<6; $x++) {
  292. if ($x % 2 == 0) {
  293. $validatorMock->expects($this->at($x))
  294. ->method('isValid')
  295. ->willReturn(false);
  296. } else {
  297. $validatorMock->expects($this->at($x))
  298. ->method('isValid')
  299. ->willReturn(true);
  300. }
  301. }
  302. }
  303. $this->getModel($validatorMock);
  304. $this->assertEquals($expected, $this->config->getOptions());
  305. }
  306. /**
  307. * @return array
  308. */
  309. public function constructorDataProvider()
  310. {
  311. return [
  312. 'all valid' => [
  313. true,
  314. true,
  315. [
  316. 'session.cache_limiter' => 'private_no_expire',
  317. 'session.cookie_lifetime' => 7200,
  318. 'session.cookie_path' => '/',
  319. 'session.cookie_domain' => 'init.host',
  320. 'session.cookie_httponly' => false,
  321. 'session.cookie_secure' => false,
  322. 'session.save_handler' => 'files'
  323. ],
  324. ],
  325. 'all invalid' => [
  326. true,
  327. false,
  328. [
  329. 'session.cache_limiter' => 'private_no_expire',
  330. 'session.cookie_httponly' => false,
  331. 'session.cookie_secure' => false,
  332. 'session.save_handler' => 'files'
  333. ],
  334. ],
  335. 'invalid_valid' => [
  336. false,
  337. true,
  338. [
  339. 'session.cache_limiter' => 'private_no_expire',
  340. 'session.cookie_lifetime' => 3600,
  341. 'session.cookie_path' => '/',
  342. 'session.cookie_domain' => 'init.host',
  343. 'session.cookie_httponly' => false,
  344. 'session.cookie_secure' => false,
  345. 'session.save_handler' => 'files'
  346. ],
  347. ],
  348. ];
  349. }
  350. /**
  351. * Get test model
  352. *
  353. * @param $validator
  354. * @return Config
  355. */
  356. protected function getModel($validator)
  357. {
  358. $this->requestMock = $this->createPartialMock(
  359. \Magento\Framework\App\Request\Http::class,
  360. ['getBasePath', 'isSecure', 'getHttpHost']
  361. );
  362. $this->requestMock->expects($this->atLeastOnce())->method('getBasePath')->will($this->returnValue('/'));
  363. $this->requestMock->expects(
  364. $this->atLeastOnce()
  365. )->method(
  366. 'getHttpHost'
  367. )->will(
  368. $this->returnValue('init.host')
  369. );
  370. $this->validatorFactoryMock = $this->getMockBuilder(\Magento\Framework\ValidatorFactory::class)
  371. ->setMethods(['setInstanceName', 'create'])
  372. ->disableOriginalConstructor()
  373. ->getMock();
  374. $this->validatorFactoryMock->expects($this->any())
  375. ->method('setInstanceName')
  376. ->willReturnSelf();
  377. $this->validatorFactoryMock->expects($this->any())
  378. ->method('create')
  379. ->willReturn($validator);
  380. $this->configMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  381. $getValueReturnMap = [
  382. ['test_web/test_cookie/test_cookie_lifetime', 'store', null, 7200],
  383. ['web/cookie/cookie_path', 'store', null, ''],
  384. ];
  385. $this->configMock->method('getValue')
  386. ->will($this->returnValueMap($getValueReturnMap));
  387. $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  388. $dirMock = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
  389. $filesystemMock->expects($this->any())
  390. ->method('getDirectoryWrite')
  391. ->will($this->returnValue($dirMock));
  392. $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
  393. $deploymentConfigMock
  394. ->method('get')
  395. ->willReturnCallback(function ($configPath) {
  396. switch ($configPath) {
  397. case Config::PARAM_SESSION_SAVE_METHOD:
  398. return 'files';
  399. case Config::PARAM_SESSION_CACHE_LIMITER:
  400. return 'private_no_expire';
  401. default:
  402. return null;
  403. }
  404. });
  405. $this->config = $this->helper->getObject(
  406. \Magento\Framework\Session\Config::class,
  407. [
  408. 'scopeConfig' => $this->configMock,
  409. 'validatorFactory' => $this->validatorFactoryMock,
  410. 'scopeType' => \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  411. 'cacheLimiter' => 'files',
  412. 'lifetimePath' => 'test_web/test_cookie/test_cookie_lifetime',
  413. 'request' => $this->requestMock,
  414. 'filesystem' => $filesystemMock,
  415. 'deploymentConfig' => $deploymentConfigMock,
  416. ]
  417. );
  418. return $this->config;
  419. }
  420. }