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