Completed
Push — master ( 2e10c1...bb3077 )
by Aimeos
02:17
created

StandardTest::testProcessEmpty()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 */
7
8
9
namespace Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $endpoint;
16
17
18
	protected function setUp()
19
	{
20
		\Aimeos\MShop\Factory::setCache( true );
21
22
		$this->context = \TestHelperCntl::getContext();
23
		$this->endpoint = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Done( $this->context, [] );
24
	}
25
26
27
	protected function tearDown()
28
	{
29
		\Aimeos\MShop\Factory::setCache( false );
30
		\Aimeos\MShop\Factory::clear();
31
	}
32
33
34
	public function testProcess()
35
	{
36
		$mapping = array(
37
			0 => 'media.languageid',
38
			1 => 'media.label',
39
			2 => 'media.mimetype',
40
			3 => 'media.preview',
41
			4 => 'media.url',
42
			5 => 'media.status',
43
		);
44
45
		$data = array(
46
			0 => 'de',
47
			1 => 'test image',
48
			2 => 'image/jpeg',
49
			3 => 'path/to/preview',
50
			4 => 'path/to/file',
51
			5 => 1,
52
		);
53
54
		$catalog = $this->create( 'job_csv_test' );
55
56
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
57
		$object->process( $catalog, $data );
58
59
		$catalog = $this->get( 'job_csv_test' );
60
		$this->delete( $catalog );
61
62
63
		$listItems = $catalog->getListItems();
64
		$listItem = reset( $listItems );
65
66
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
67
		$this->assertEquals( 1, count( $listItems ) );
68
69
		$this->assertEquals( 1, $listItem->getStatus() );
70
		$this->assertEquals( 0, $listItem->getPosition() );
71
		$this->assertEquals( 'media', $listItem->getDomain() );
72
		$this->assertEquals( 'default', $listItem->getType() );
73
74
		$refItem = $listItem->getRefItem();
75
76
		$this->assertEquals( 1, $refItem->getStatus() );
77
		$this->assertEquals( 'default', $refItem->getType() );
78
		$this->assertEquals( 'catalog', $refItem->getDomain() );
79
		$this->assertEquals( 'test image', $refItem->getLabel() );
80
		$this->assertEquals( 'image/jpeg', $refItem->getMimetype() );
81
		$this->assertEquals( 'path/to/preview', $refItem->getPreview() );
82
		$this->assertEquals( 'path/to/file', $refItem->getUrl() );
83
		$this->assertEquals( 'de', $refItem->getLanguageId() );
84
	}
85
86
87
	public function testProcessMultiple()
88
	{
89
		$mapping = array(
90
			0 => 'media.url',
91
		);
92
93
		$data = array(
94
			0 => "path/to/0\npath/to/1\npath/to/2\npath/to/3",
95
		);
96
97
		$catalog = $this->create( 'job_csv_test' );
98
99
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
100
		$object->process( $catalog, $data );
101
102
		$catalog = $this->get( 'job_csv_test' );
103
		$this->delete( $catalog );
104
105
106
		$pos = 0;
107
		$listItems = $catalog->getListItems();
108
		$expected = array( 'path/to/0', 'path/to/1', 'path/to/2', 'path/to/3' );
109
110
		$this->assertEquals( 4, count( $listItems ) );
111
112
		foreach( $listItems as $listItem )
113
		{
114
			$this->assertEquals( $expected[$pos], $listItem->getRefItem()->getUrl() );
115
			$pos++;
116
		}
117
	}
118
119
120
	public function testProcessMultipleFields()
121
	{
122
		$mapping = array(
123
			0 => 'media.url',
124
			1 => 'media.url',
125
			2 => 'media.url',
126
			3 => 'media.url',
127
		);
128
129
		$data = array(
130
			0 => 'path/to/0',
131
			1 => 'path/to/1',
132
			2 => 'path/to/2',
133
			3 => 'path/to/3',
134
		);
135
136
		$catalog = $this->create( 'job_csv_test' );
137
138
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
139
		$object->process( $catalog, $data );
140
141
		$catalog = $this->get( 'job_csv_test' );
142
		$this->delete( $catalog );
143
144
145
		$pos = 0;
146
		$listItems = $catalog->getListItems();
147
148
		$this->assertEquals( 4, count( $listItems ) );
149
150
		foreach( $listItems as $listItem )
151
		{
152
			$this->assertEquals( $data[$pos], $listItem->getRefItem()->getUrl() );
153
			$pos++;
154
		}
155
	}
156
157
158
	public function testProcessUpdate()
159
	{
160
		$mapping = array(
161
			0 => 'media.url',
162
			1 => 'media.languageid',
163
		);
164
165
		$data = array(
166
			0 => 'path/to/file',
167
			1 => 'de',
168
		);
169
170
		$dataUpdate = array(
171
			0 => 'path/to/new',
172
			1 => '',
173
		);
174
175
		$catalog = $this->create( 'job_csv_test' );
176
177
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
178
		$object->process( $catalog, $data );
179
180
		$catalog = $this->get( 'job_csv_test' );
181
182
		$object->process( $catalog, $dataUpdate );
183
184
		$catalog = $this->get( 'job_csv_test' );
185
		$this->delete( $catalog );
186
187
188
		$listItems = $catalog->getListItems();
189
		$listItem = reset( $listItems );
190
191
		$this->assertEquals( 1, count( $listItems ) );
192
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
193
194
		$this->assertEquals( 'path/to/new', $listItem->getRefItem()->getUrl() );
195
		$this->assertEquals( null, $listItem->getRefItem()->getLanguageId() );
196
	}
197
198
199
	public function testProcessDelete()
200
	{
201
		$mapping = array(
202
			0 => 'media.url',
203
		);
204
205
		$data = array(
206
			0 => '/path/to/file',
207
		);
208
209
		$catalog = $this->create( 'job_csv_test' );
210
211
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
212
		$object->process( $catalog, $data );
213
214
		$catalog = $this->get( 'job_csv_test' );
215
216
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, [], $this->endpoint );
217
		$object->process( $catalog, [] );
218
219
		$catalog = $this->get( 'job_csv_test' );
220
		$this->delete( $catalog );
221
222
223
		$listItems = $catalog->getListItems();
224
225
		$this->assertEquals( 0, count( $listItems ) );
226
	}
227
228
229
	public function testProcessEmpty()
230
	{
231
		$mapping = array(
232
			0 => 'media.url',
233
			1 => 'media.url',
234
		);
235
236
		$data = array(
237
			0 => 'path/to/file',
238
			1 => '',
239
		);
240
241
		$catalog = $this->create( 'job_csv_test' );
242
243
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
244
		$object->process( $catalog, $data );
245
246
		$catalog = $this->get( 'job_csv_test' );
247
		$this->delete( $catalog );
248
249
250
		$listItems = $catalog->getListItems();
251
252
		$this->assertEquals( 1, count( $listItems ) );
253
	}
254
255
256
	public function testProcessListtypes()
257
	{
258
		$mapping = array(
259
			0 => 'media.url',
260
			1 => 'catalog.lists.type',
261
			2 => 'media.url',
262
			3 => 'catalog.lists.type',
263
		);
264
265
		$data = array(
266
			0 => 'path/to/file',
267
			1 => 'download',
268
			2 => 'path/to/file2',
269
			3 => 'default',
270
		);
271
272
		$this->context->getConfig()->set( 'controller/common/catalog/import/csv/processor/media/listtypes', array( 'default' ) );
273
274
		$catalog = $this->create( 'job_csv_test' );
275
276
		$object = new \Aimeos\Controller\Common\Catalog\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
277
		$object->process( $catalog, $data );
278
279
		$catalog = $this->get( 'job_csv_test' );
280
		$this->delete( $catalog );
281
282
283
		$listItems = $catalog->getListItems();
284
		$listItem = reset( $listItems );
285
286
		$this->assertEquals( 1, count( $listItems ) );
287
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
288
289
		$this->assertEquals( 'default', $listItem->getType() );
290
		$this->assertEquals( 'path/to/file2', $listItem->getRefItem()->getUrl() );
291
	}
292
293
294
	/**
295
	 * @param string $code
296
	 */
297
	protected function create( $code )
298
	{
299
		$manager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context );
300
301
		$item = $manager->createItem();
302
		$item->setCode( $code );
303
304
		return $manager->insertItem( $item );
305
	}
306
307
308
	protected function delete( \Aimeos\MShop\Catalog\Item\Iface $catalog )
309
	{
310
		$mediaManager = \Aimeos\MShop\Media\Manager\Factory::createManager( $this->context );
311
		$manager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context );
312
		$listManager = $manager->getSubManager( 'lists' );
313
314
		foreach( $catalog->getListItems('media') as $listItem )
315
		{
316
			$mediaManager->deleteItem( $listItem->getRefItem()->getId() );
317
			$listManager->deleteItem( $listItem->getId() );
318
		}
319
320
		$manager->deleteItem( $catalog->getId() );
321
	}
322
323
324
	/**
325
	 * @param string $code
326
	 */
327
	protected function get( $code )
328
	{
329
		$manager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context );
330
		return $manager->findItem( $code, array('media') );
331
	}
332
}