DeleteExpiredImagesTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Test\Unit\Cron;
  7. class DeleteExpiredImagesTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * CAPTCHA helper
  11. *
  12. * @var \Magento\Captcha\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. protected $_helper;
  15. /**
  16. * CAPTCHA helper
  17. *
  18. * @var \Magento\Captcha\Helper\Adminhtml\Data|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_adminHelper;
  21. /**
  22. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_filesystem;
  25. /**
  26. * @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_storeManager;
  29. /**
  30. * @var \Magento\Captcha\Cron\DeleteExpiredImages
  31. */
  32. protected $_deleteExpiredImages;
  33. /**
  34. * @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $_directory;
  37. /**
  38. * @var int
  39. */
  40. public static $currentTime;
  41. /**
  42. * Create mocks and model
  43. */
  44. protected function setUp()
  45. {
  46. $this->_helper = $this->createMock(\Magento\Captcha\Helper\Data::class);
  47. $this->_adminHelper = $this->createMock(\Magento\Captcha\Helper\Adminhtml\Data::class);
  48. $this->_filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
  49. $this->_directory = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
  50. $this->_storeManager = $this->createMock(\Magento\Store\Model\StoreManager::class);
  51. $this->_filesystem->expects(
  52. $this->once()
  53. )->method(
  54. 'getDirectoryWrite'
  55. )->will(
  56. $this->returnValue($this->_directory)
  57. );
  58. $this->_deleteExpiredImages = new \Magento\Captcha\Cron\DeleteExpiredImages(
  59. $this->_helper,
  60. $this->_adminHelper,
  61. $this->_filesystem,
  62. $this->_storeManager
  63. );
  64. }
  65. /**
  66. * @dataProvider getExpiredImages
  67. */
  68. public function testDeleteExpiredImages($website, $isFile, $filename, $mTime, $timeout)
  69. {
  70. $this->_storeManager->expects(
  71. $this->once()
  72. )->method(
  73. 'getWebsites'
  74. )->will(
  75. $this->returnValue(isset($website) ? [$website] : [])
  76. );
  77. if (isset($website)) {
  78. $this->_helper->expects(
  79. $this->once()
  80. )->method(
  81. 'getConfig'
  82. )->with(
  83. $this->equalTo('timeout'),
  84. new \PHPUnit\Framework\Constraint\IsIdentical($website->getDefaultStore())
  85. )->will(
  86. $this->returnValue($timeout)
  87. );
  88. } else {
  89. $this->_helper->expects($this->never())->method('getConfig');
  90. }
  91. $this->_adminHelper->expects(
  92. $this->once()
  93. )->method(
  94. 'getConfig'
  95. )->with(
  96. $this->equalTo('timeout'),
  97. new \PHPUnit\Framework\Constraint\IsNull()
  98. )->will(
  99. $this->returnValue($timeout)
  100. );
  101. $timesToCall = isset($website) ? 2 : 1;
  102. $this->_directory->expects(
  103. $this->exactly($timesToCall)
  104. )->method(
  105. 'read'
  106. )->will(
  107. $this->returnValue([$filename])
  108. );
  109. $this->_directory->expects($this->exactly($timesToCall))->method('isFile')->will($this->returnValue($isFile));
  110. $this->_directory->expects($this->any())->method('stat')->will($this->returnValue(['mtime' => $mTime]));
  111. $this->_deleteExpiredImages->execute();
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function getExpiredImages()
  117. {
  118. $website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['__wakeup', 'getDefaultStore']);
  119. $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['__wakeup']);
  120. $website->expects($this->any())->method('getDefaultStore')->will($this->returnValue($store));
  121. $time = time();
  122. return [
  123. [null, true, 'test.png', 50, ($time - 60) / 60, true],
  124. [$website, false, 'test.png', 50, ($time - 60) / 60, false],
  125. [$website, true, 'test.jpg', 50, ($time - 60) / 60, false],
  126. [$website, true, 'test.png', 50, ($time - 20) / 60, false]
  127. ];
  128. }
  129. }
  130. /**
  131. * Fix current time
  132. *
  133. * @return int
  134. */
  135. function time()
  136. {
  137. if (!isset(DeleteExpiredImagesTest::$currentTime)) {
  138. DeleteExpiredImagesTest::$currentTime = \time();
  139. }
  140. return DeleteExpiredImagesTest::$currentTime;
  141. }