Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigTest.php
Go to the documentation of this file.
1 <?php
11 
12 use \Magento\Framework\Session\Config;
13 
17 class ConfigTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $helper;
23 
27  protected $config;
28 
32  protected $configMock;
33 
38 
42  protected $validatorMock;
43 
47  protected $requestMock;
48 
52  protected $filesystem;
53 
54  protected function setUp()
55  {
56  $this->helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
57 
58  $this->validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
59  ->disableOriginalConstructor()
60  ->getMock();
61  $this->validatorMock->expects($this->any())
62  ->method('isValid')
63  ->willReturn(true);
64  }
65 
66  public function testSetOptionsInvalidValue()
67  {
68  $this->getModel($this->validatorMock);
69  $preVal = $this->config->getOptions();
70  $this->config->setOptions('');
71  $this->assertEquals($preVal, $this->config->getOptions());
72  }
73 
77  public function testSetOptions($option, $getter, $value)
78  {
79  $this->getModel($this->validatorMock);
80  $options = [$option => $value];
81  $this->config->setOptions($options);
82  $this->assertSame($value, $this->config->{$getter}());
83  }
84 
88  public function optionsProvider()
89  {
90  return [
91  ['save_path', 'getSavePath', __DIR__],
92  ['name', 'getName', 'FOOBAR'],
93  ['gc_probability', 'getGcProbability', 42],
94  ['gc_divisor', 'getGcDivisor', 3],
95  ['gc_maxlifetime', 'getGcMaxlifetime', 180],
96  ['serialize_handler', 'getSerializeHandler', 'php_binary'],
97  ['cookie_lifetime', 'getCookieLifetime', 180],
98  ['cookie_path', 'getCookiePath', '/foo/bar'],
99  ['cookie_domain', 'getCookieDomain', 'framework.zend.com'],
100  ['cookie_secure', 'getCookieSecure', true],
101  ['cookie_httponly', 'getCookieHttpOnly', true],
102  ['use_cookies', 'getUseCookies', false],
103  ['use_only_cookies', 'getUseOnlyCookies', true],
104  ['referer_check', 'getRefererCheck', 'foobar'],
105  ['entropy_file', 'getEntropyFile', __FILE__],
106  ['entropy_length', 'getEntropyLength', 42],
107  ['cache_limiter', 'getCacheLimiter', 'private'],
108  ['cache_expire', 'getCacheExpire', 42],
109  ['use_trans_sid', 'getUseTransSid', true],
110  ['hash_function', 'getHashFunction', 'md5'],
111  ['hash_bits_per_character', 'getHashBitsPerCharacter', 5],
112  ['url_rewriter_tags', 'getUrlRewriterTags', 'a=href']
113  ];
114  }
115 
116  public function testGetOptions()
117  {
118  $this->getModel($this->validatorMock);
119  $appStateProperty = new \ReflectionProperty(\Magento\Framework\Session\Config::class, 'options');
120  $appStateProperty->setAccessible(true);
121  $original = $appStateProperty->getValue($this->config);
122  $valueForTest = ['test' => 'test2'];
123  $appStateProperty->setValue($this->config, $valueForTest);
124  $this->assertEquals($valueForTest, $this->config->getOptions());
125  $this->assertEquals($valueForTest, $this->config->toArray());
126  $appStateProperty->setValue($this->config, $original);
127  $this->assertEquals($original, $this->config->getOptions());
128  $this->assertEquals($original, $this->config->toArray());
129  }
130 
131  public function testNameIsMutable()
132  {
133  $this->getModel($this->validatorMock);
134  $this->config->setName('FOOBAR');
135  $this->assertEquals('FOOBAR', $this->config->getName());
136  }
137 
138  public function testCookieLifetimeIsMutable()
139  {
140  $this->getModel($this->validatorMock);
141  $this->config->setCookieLifetime(20);
142  $this->assertEquals(20, $this->config->getCookieLifetime());
143  }
144 
145  public function testCookieLifetimeCanBeZero()
146  {
147  $this->getModel($this->validatorMock);
148  $this->config->setCookieLifetime(0);
149  $this->assertEquals(0, ini_get('session.cookie_lifetime'));
150  }
151 
153  {
154  $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
155  ->disableOriginalConstructor()
156  ->getMock();
157  $validatorMock->expects($this->any())
158  ->method('isValid')
159  ->willReturn(false);
160  $this->getModel($validatorMock);
161  $preVal = $this->config->getCookieLifetime();
162  $this->config->setCookieLifetime('foobar_bogus');
163  $this->assertEquals($preVal, $this->config->getCookieLifetime());
164  }
165 
167  {
168  $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
169  ->disableOriginalConstructor()
170  ->getMock();
171  $validatorMock->expects($this->any())
172  ->method('isValid')
173  ->willReturn(false);
174  $this->getModel($validatorMock);
175  $preVal = $this->config->getCookieLifetime();
176  $this->config->setCookieLifetime(-1);
177  $this->assertEquals($preVal, $this->config->getCookieLifetime());
178  }
179 
180  public function testWrongMethodCall()
181  {
182  $this->getModel($this->validatorMock);
183  $this->expectException('\BadMethodCallException');
184  $this->expectExceptionMessage('Method "methodThatNotExist" does not exist in Magento\Framework\Session\Config');
185  $this->config->methodThatNotExist();
186  }
187 
189  {
190  $this->getModel($this->validatorMock);
191  $this->assertSame((bool)ini_get('session.cookie_secure'), $this->config->getCookieSecure());
192  }
193 
194  public function testCookieSecureIsMutable()
195  {
196  $this->getModel($this->validatorMock);
197  $value = ini_get('session.cookie_secure') ? false : true;
198  $this->config->setCookieSecure($value);
199  $this->assertEquals($value, $this->config->getCookieSecure());
200  }
201 
202  public function testCookieDomainIsMutable()
203  {
204  $this->getModel($this->validatorMock);
205  $this->config->setCookieDomain('example.com');
206  $this->assertEquals('example.com', $this->config->getCookieDomain());
207  }
208 
209  public function testCookieDomainCanBeEmpty()
210  {
211  $this->getModel($this->validatorMock);
212  $this->config->setCookieDomain('');
213  $this->assertEquals('', $this->config->getCookieDomain());
214  }
215 
217  {
218  $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
219  ->disableOriginalConstructor()
220  ->getMock();
221  $validatorMock->expects($this->any())
222  ->method('isValid')
223  ->willReturn(false);
224  $this->getModel($validatorMock);
225  $preVal = $this->config->getCookieDomain();
226  $this->config->setCookieDomain(24);
227  $this->assertEquals($preVal, $this->config->getCookieDomain());
228  }
229 
231  {
232  $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
233  ->disableOriginalConstructor()
234  ->getMock();
235  $validatorMock->expects($this->any())
236  ->method('isValid')
237  ->willReturn(false);
238  $this->getModel($validatorMock);
239  $preVal = $this->config->getCookieDomain();
240  $this->config->setCookieDomain('D:\\WINDOWS\\System32\\drivers\\etc\\hosts');
241  $this->assertEquals($preVal, $this->config->getCookieDomain());
242  }
243 
245  {
246  $this->getModel($this->validatorMock);
247  $this->assertSame((bool)ini_get('session.cookie_httponly'), $this->config->getCookieHttpOnly());
248  }
249 
250  public function testCookieHttpOnlyIsMutable()
251  {
252  $this->getModel($this->validatorMock);
253  $value = ini_get('session.cookie_httponly') ? false : true;
254  $this->config->setCookieHttpOnly($value);
255  $this->assertEquals($value, $this->config->getCookieHttpOnly());
256  }
257 
259  {
260  $this->getModel($this->validatorMock);
261  $this->assertSame((bool)ini_get('session.use_cookies'), $this->config->getUseCookies());
262  }
263 
264  public function testUseCookiesIsMutable()
265  {
266  $this->getModel($this->validatorMock);
267  $value = ini_get('session.use_cookies') ? false : true;
268  $this->config->setUseCookies($value);
269  $this->assertEquals($value, (bool)$this->config->getUseCookies());
270  }
271 
273  {
274  $this->getModel($this->validatorMock);
275  $this->assertSame((bool)ini_get('session.use_only_cookies'), $this->config->getUseOnlyCookies());
276  }
277 
278  public function testUseOnlyCookiesIsMutable()
279  {
280  $this->getModel($this->validatorMock);
281  $value = ini_get('session.use_only_cookies') ? false : true;
282  $this->config->setOption('use_only_cookies', $value);
283  $this->assertEquals($value, (bool)$this->config->getOption('use_only_cookies'));
284  }
285 
287  {
288  $this->getModel($this->validatorMock);
289  $this->assertSame(ini_get('session.referer_check'), $this->config->getRefererCheck());
290  }
291 
292  public function testRefererCheckIsMutable()
293  {
294  $this->getModel($this->validatorMock);
295  $this->config->setOption('referer_check', 'FOOBAR');
296  $this->assertEquals('FOOBAR', $this->config->getOption('referer_check'));
297  }
298 
299  public function testRefererCheckMayBeEmpty()
300  {
301  $this->getModel($this->validatorMock);
302  $this->config->setOption('referer_check', '');
303  $this->assertEquals('', $this->config->getOption('referer_check'));
304  }
305 
306  public function testSetSavePath()
307  {
308  $this->getModel($this->validatorMock);
309  $this->config->setSavePath('some_save_path');
310  $this->assertEquals($this->config->getOption('save_path'), 'some_save_path');
311  }
312 
319  public function testConstructor($isValidSame, $isValid, $expected)
320  {
321  $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator\ValidatorInterface::class)
322  ->disableOriginalConstructor()
323  ->getMock();
324  if ($isValidSame) {
325  $validatorMock->expects($this->any())
326  ->method('isValid')
327  ->willReturn($isValid);
328  } else {
329  for ($x = 0; $x<6; $x++) {
330  if ($x % 2 == 0) {
331  $validatorMock->expects($this->at($x))
332  ->method('isValid')
333  ->willReturn(false);
334  } else {
335  $validatorMock->expects($this->at($x))
336  ->method('isValid')
337  ->willReturn(true);
338  }
339  }
340  }
341 
342  $this->getModel($validatorMock);
343 
344  $this->assertEquals($expected, $this->config->getOptions());
345  }
346 
350  public function constructorDataProvider()
351  {
352  return [
353  'all valid' => [
354  true,
355  true,
356  [
357  'session.cache_limiter' => 'private_no_expire',
358  'session.cookie_lifetime' => 7200,
359  'session.cookie_path' => '/',
360  'session.cookie_domain' => 'init.host',
361  'session.cookie_httponly' => false,
362  'session.cookie_secure' => false,
363  'session.save_handler' => 'files'
364  ],
365  ],
366  'all invalid' => [
367  true,
368  false,
369  [
370  'session.cache_limiter' => 'private_no_expire',
371  'session.cookie_httponly' => false,
372  'session.cookie_secure' => false,
373  'session.save_handler' => 'files'
374  ],
375  ],
376  'invalid_valid' => [
377  false,
378  true,
379  [
380  'session.cache_limiter' => 'private_no_expire',
381  'session.cookie_lifetime' => 3600,
382  'session.cookie_path' => '/',
383  'session.cookie_domain' => 'init.host',
384  'session.cookie_httponly' => false,
385  'session.cookie_secure' => false,
386  'session.save_handler' => 'files'
387  ],
388  ],
389  ];
390  }
391 
398  protected function getModel($validator)
399  {
400  $this->requestMock = $this->createPartialMock(
401  \Magento\Framework\App\Request\Http::class,
402  ['getBasePath', 'isSecure', 'getHttpHost']
403  );
404  $this->requestMock->expects($this->atLeastOnce())->method('getBasePath')->will($this->returnValue('/'));
405  $this->requestMock->expects(
406  $this->atLeastOnce()
407  )->method(
408  'getHttpHost'
409  )->will(
410  $this->returnValue('init.host')
411  );
412 
413  $this->validatorFactoryMock = $this->getMockBuilder(\Magento\Framework\ValidatorFactory::class)
414  ->setMethods(['setInstanceName', 'create'])
415  ->disableOriginalConstructor()
416  ->getMock();
417  $this->validatorFactoryMock->expects($this->any())
418  ->method('setInstanceName')
419  ->willReturnSelf();
420  $this->validatorFactoryMock->expects($this->any())
421  ->method('create')
422  ->willReturn($validator);
423 
424  $this->configMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
425  $getValueReturnMap = [
426  ['test_web/test_cookie/test_cookie_lifetime', 'store', null, 7200],
427  ['web/cookie/cookie_path', 'store', null, ''],
428  ];
429  $this->configMock->method('getValue')
430  ->will($this->returnValueMap($getValueReturnMap));
431 
432  $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
433  $dirMock = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
434  $filesystemMock->expects($this->any())
435  ->method('getDirectoryWrite')
436  ->will($this->returnValue($dirMock));
437 
438  $deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
439  $deploymentConfigMock
440  ->method('get')
441  ->willReturnCallback(function ($configPath) {
442  switch ($configPath) {
444  return 'files';
446  return 'private_no_expire';
447  default:
448  return null;
449  }
450  });
451 
452  $this->config = $this->helper->getObject(
453  \Magento\Framework\Session\Config::class,
454  [
455  'scopeConfig' => $this->configMock,
456  'validatorFactory' => $this->validatorFactoryMock,
457  'scopeType' => \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
458  'cacheLimiter' => 'files',
459  'lifetimePath' => 'test_web/test_cookie/test_cookie_lifetime',
460  'request' => $this->requestMock,
461  'filesystem' => $filesystemMock,
462  'deploymentConfig' => $deploymentConfigMock,
463  ]
464  );
465  return $this->config;
466  }
467 }
testConstructor($isValidSame, $isValid, $expected)
Definition: ConfigTest.php:319
return false
Definition: gallery.phtml:36
$config
Definition: fraud_order.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
defined('MTF_BOOT_FILE')||define('MTF_BOOT_FILE' __FILE__
Definition: bootstrap.php:7
$value
Definition: gender.phtml:16
testSetOptions($option, $getter, $value)
Definition: ConfigTest.php:77