1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016-2022 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Client\Html\Account\Download; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class StandardTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
private $context; |
15
|
|
|
private $object; |
16
|
|
|
private $view; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
protected function setUp() : void |
20
|
|
|
{ |
21
|
|
|
\Aimeos\MShop::cache( true ); |
22
|
|
|
|
23
|
|
|
$this->view = \TestHelperHtml::view(); |
24
|
|
|
$this->context = \TestHelperHtml::context(); |
25
|
|
|
$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() ); |
26
|
|
|
|
27
|
|
|
$this->object = new \Aimeos\Client\Html\Account\Download\Standard( $this->context ); |
28
|
|
|
$this->object->setView( $this->view ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
protected function tearDown() : void |
33
|
|
|
{ |
34
|
|
|
\Aimeos\MShop::cache( false ); |
35
|
|
|
unset( $this->object ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
public function testBody() |
40
|
|
|
{ |
41
|
|
|
$output = $this->object->body(); |
42
|
|
|
$this->assertEquals( '', $output ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
public function testHeader() |
47
|
|
|
{ |
48
|
|
|
$output = $this->object->header(); |
49
|
|
|
$this->assertEquals( '', $output ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public function testInit() |
54
|
|
|
{ |
55
|
|
|
$response = $this->getMockBuilder( \Psr\Http\Message\ResponseInterface::class )->getMock(); |
56
|
|
|
$response->expects( $this->once() )->method( 'withHeader' )->will( $this->returnSelf() ); |
57
|
|
|
$response->expects( $this->once() )->method( 'withStatus' )->will( $this->returnSelf() ); |
58
|
|
|
|
59
|
|
|
$helper = new \Aimeos\MW\View\Helper\Response\Standard( $this->view, $response ); |
60
|
|
|
$this->view->addHelper( 'response', $helper ); |
61
|
|
|
|
62
|
|
|
$this->object->init(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function testInitOK() |
67
|
|
|
{ |
68
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['dl_id' => '-1'] ); |
69
|
|
|
$this->view->addHelper( 'param', $helper ); |
70
|
|
|
|
71
|
|
|
$object = $this->getMockBuilder( \Aimeos\Client\Html\Account\Download\Standard::class ) |
72
|
|
|
->setConstructorArgs( [$this->context] ) |
73
|
|
|
->setMethods( array( 'checkAccess', 'checkDownload' ) ) |
74
|
|
|
->getMock(); |
75
|
|
|
$object->setView( $this->view ); |
76
|
|
|
|
77
|
|
|
$object->expects( $this->once() )->method( 'checkAccess' )->will( $this->returnValue( true ) ); |
78
|
|
|
$object->expects( $this->once() )->method( 'checkDownload' )->will( $this->returnValue( true ) ); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
$attrManagerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Base\\Product\\Attribute\\Standard' ) |
82
|
|
|
->setConstructorArgs( array( $this->context ) ) |
83
|
|
|
->setMethods( ['get'] ) |
84
|
|
|
->getMock(); |
85
|
|
|
|
86
|
|
|
$attrManagerStub->expects( $this->once() )->method( 'get' ) |
87
|
|
|
->will( $this->returnValue( $attrManagerStub->create() ) ); |
88
|
|
|
|
89
|
|
|
\Aimeos\MShop::inject( 'order/base/product/attribute', $attrManagerStub ); |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
$stream = $this->getMockBuilder( \Psr\Http\Message\StreamInterface::class )->getMock(); |
93
|
|
|
$response = $this->getMockBuilder( \Psr\Http\Message\ResponseInterface::class )->getMock(); |
94
|
|
|
$response->expects( $this->exactly( 7 ) )->method( 'withHeader' )->will( $this->returnSelf() ); |
95
|
|
|
|
96
|
|
|
$helper = $this->getMockBuilder( \Aimeos\MW\View\Helper\Response\Standard::class ) |
97
|
|
|
->setConstructorArgs( array( $this->view, $response ) ) |
98
|
|
|
->setMethods( array( 'createStream' ) ) |
99
|
|
|
->getMock(); |
100
|
|
|
$helper->expects( $this->once() )->method( 'createStream' )->will( $this->returnValue( $stream ) ); |
101
|
|
|
$this->view->addHelper( 'response', $helper ); |
102
|
|
|
|
103
|
|
|
$object->init(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
public function testInitCheckAccess() |
108
|
|
|
{ |
109
|
|
|
$this->assertFalse( $this->access( 'checkAccess' )->invokeArgs( $this->object, [-1, -2] ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
public function testInitCheckDownload() |
114
|
|
|
{ |
115
|
|
|
$customerStub = $this->getMockBuilder( \Aimeos\Controller\Frontend\Customer\Standard::class ) |
116
|
|
|
->setConstructorArgs( array( $this->context ) ) |
117
|
|
|
->setMethods( array( 'addListItem', 'store' ) ) |
118
|
|
|
->getMock(); |
119
|
|
|
|
120
|
|
|
$customerStub->expects( $this->once() )->method( 'addListItem' )->will( $this->returnValue( $customerStub ) ); |
121
|
|
|
$customerStub->expects( $this->once() )->method( 'store' )->will( $this->returnValue( $customerStub ) ); |
122
|
|
|
|
123
|
|
|
\Aimeos\Controller\Frontend\Customer\Factory::injectController( '\Aimeos\Controller\Frontend\Customer\Standard', $customerStub ); |
124
|
|
|
$this->assertTrue( $this->access( 'checkDownload' )->invokeArgs( $this->object, [-1, -2] ) ); |
125
|
|
|
\Aimeos\Controller\Frontend\Customer\Factory::injectController( '\Aimeos\Controller\Frontend\Customer\Standard', null ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
public function testInitCheckDownloadMaxCount() |
130
|
|
|
{ |
131
|
|
|
$this->context->config()->set( 'client/html/account/download/maxcount', 0 ); |
132
|
|
|
|
133
|
|
|
$this->assertFalse( $this->access( 'checkDownload' )->invokeArgs( $this->object, [-1, -2] ) ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
public function testAddDownload() |
138
|
|
|
{ |
139
|
|
|
$fs = $this->context->fs( 'fs-secure' ); |
140
|
|
|
$fs->write( 'tmp/download/test.txt', 'test' ); |
141
|
|
|
|
142
|
|
|
$item = \Aimeos\MShop::create( $this->context, 'order/base/product/attribute' )->create(); |
143
|
|
|
$item->setValue( 'tmp/download/test.txt' ); |
144
|
|
|
$item->setName( 'test download' ); |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
$stream = $this->getMockBuilder( \Psr\Http\Message\StreamInterface::class )->getMock(); |
148
|
|
|
$response = $this->getMockBuilder( \Psr\Http\Message\ResponseInterface::class )->getMock(); |
149
|
|
|
$response->expects( $this->exactly( 7 ) )->method( 'withHeader' )->will( $this->returnSelf() ); |
150
|
|
|
|
151
|
|
|
$helper = $this->getMockBuilder( \Aimeos\MW\View\Helper\Response\Standard::class ) |
152
|
|
|
->setConstructorArgs( array( $this->view, $response ) ) |
153
|
|
|
->setMethods( array( 'createStream' ) ) |
154
|
|
|
->getMock(); |
155
|
|
|
$helper->expects( $this->once() )->method( 'createStream' )->will( $this->returnValue( $stream ) ); |
156
|
|
|
$this->view->addHelper( 'response', $helper ); |
157
|
|
|
|
158
|
|
|
$this->access( 'addDownload' )->invokeArgs( $this->object, [$item] ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
public function testAddDownloadRedirect() |
163
|
|
|
{ |
164
|
|
|
$item = \Aimeos\MShop::create( $this->context, 'order/base/product/attribute' )->create(); |
165
|
|
|
$item->setValue( 'http://localhost/dl/test.txt' ); |
166
|
|
|
$item->setName( 'test download' ); |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
$response = $this->getMockBuilder( \Psr\Http\Message\ResponseInterface::class )->getMock(); |
170
|
|
|
$response->expects( $this->once() )->method( 'withHeader' )->will( $this->returnSelf() ); |
171
|
|
|
$response->expects( $this->once() )->method( 'withStatus' )->will( $this->returnSelf() ); |
172
|
|
|
|
173
|
|
|
$helper = new \Aimeos\MW\View\Helper\Response\Standard( $this->view, $response ); |
174
|
|
|
$this->view->addHelper( 'response', $helper ); |
175
|
|
|
|
176
|
|
|
$this->access( 'addDownload' )->invokeArgs( $this->object, [$item] ); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
public function testAddDownloadNotFound() |
181
|
|
|
{ |
182
|
|
|
$item = \Aimeos\MShop::create( $this->context, 'order/base/product/attribute' )->create(); |
183
|
|
|
$item->setValue( 'test.txt' ); |
184
|
|
|
$item->setName( 'test download' ); |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
$response = $this->getMockBuilder( \Psr\Http\Message\ResponseInterface::class )->getMock(); |
188
|
|
|
$response->expects( $this->never() )->method( 'withHeader' )->will( $this->returnSelf() ); |
189
|
|
|
$response->expects( $this->once() )->method( 'withStatus' )->will( $this->returnSelf() ); |
190
|
|
|
|
191
|
|
|
$helper = new \Aimeos\MW\View\Helper\Response\Standard( $this->view, $response ); |
192
|
|
|
$this->view->addHelper( 'response', $helper ); |
193
|
|
|
|
194
|
|
|
$this->access( 'addDownload' )->invokeArgs( $this->object, [$item] ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
protected function access( $name ) |
199
|
|
|
{ |
200
|
|
|
$class = new \ReflectionClass( \Aimeos\Client\Html\Account\Download\Standard::class ); |
201
|
|
|
$method = $class->getMethod( $name ); |
202
|
|
|
$method->setAccessible( true ); |
203
|
|
|
|
204
|
|
|
return $method; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|