Passed
Push — master ( 42527d...ccd2e1 )
by Aimeos
02:41
created

StandardTest::testRunProcessorInvalidPosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Product\Import\Csv;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $context;
16
	private $aimeos;
17
18
19
	protected function setUp() : void
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelperJobs::getContext();
24
		$this->aimeos = \TestHelperJobs::getAimeos();
25
		$config = $this->context->getConfig();
26
27
		$config->set( 'controller/jobs/product/import/csv/skip-lines', 1 );
28
		$config->set( 'controller/jobs/product/import/csv/location', __DIR__ . '/_testfiles/valid' );
29
30
		$this->object = new \Aimeos\Controller\Jobs\Product\Import\Csv\Standard( $this->context, $this->aimeos );
31
	}
32
33
34
	protected function tearDown() : void
35
	{
36
		\Aimeos\MShop::cache( false );
37
		$this->object = null;
38
39
		if( file_exists( 'tmp/import.zip' ) ) {
40
			unlink( 'tmp/import.zip' );
41
		}
42
	}
43
44
45
	public function testGetName()
46
	{
47
		$this->assertEquals( 'Product import CSV', $this->object->getName() );
48
	}
49
50
51
	public function testGetDescription()
52
	{
53
		$text = 'Imports new and updates existing products from CSV files';
54
		$this->assertEquals( $text, $this->object->getDescription() );
55
	}
56
57
58
	public function testRun()
59
	{
60
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
61
		$nondelete = array( 'attribute', 'product' );
62
		$delete = array( 'media', 'price', 'text' );
63
64
		$convert = array(
65
			1 => 'Text/LatinUTF8',
66
		);
67
68
		$this->context->getConfig()->set( 'controller/jobs/product/import/csv/converter', $convert );
69
70
		$this->object->run();
71
72
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
73
		$properties = $this->getProperties( array_keys( $result ) );
74
		$this->delete( $prodcodes, $delete, $nondelete );
75
76
		$this->assertEquals( 2, count( $result ) );
77
		$this->assertEquals( 2, count( $properties ) );
78
79
		foreach( $result as $product ) {
80
			$this->assertEquals( 5, count( $product->getListItems() ) );
81
		}
82
	}
83
84
85
	public function testRunUpdate()
86
	{
87
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
88
		$nondelete = array( 'attribute', 'product' );
89
		$delete = array( 'media', 'price', 'text' );
90
91
		$this->object->run();
92
		$this->object->run();
93
94
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
95
		$properties = $this->getProperties( array_keys( $result ) );
96
		$this->delete( $prodcodes, $delete, $nondelete );
97
98
		$this->assertEquals( 2, count( $result ) );
99
		$this->assertEquals( 2, count( $properties ) );
100
101
		foreach( $result as $product ) {
102
			$this->assertEquals( 5, count( $product->getListItems() ) );
103
		}
104
	}
105
106
107
	public function testRunPosition()
108
	{
109
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
110
		$nondelete = array( 'attribute', 'product' );
111
		$delete = array( 'media', 'price', 'text' );
112
113
		$config = $this->context->getConfig();
114
		$mapping = $config->get( 'controller/jobs/product/import/csv/mapping', [] );
115
		$mapping['item'] = array( 0 => 'product.label', 1 => 'product.code' );
116
117
		$config->set( 'controller/jobs/product/import/csv/mapping', $mapping );
118
		$config->set( 'controller/jobs/product/import/csv/location', __DIR__ . '/_testfiles/position' );
119
120
		$this->object->run();
121
122
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
123
		$this->delete( $prodcodes, $delete, $nondelete );
124
125
		$this->assertEquals( 2, count( $result ) );
126
	}
127
128
129
	public function testRunProcessorInvalidMapping()
130
	{
131
		$mapping = array(
132
			'media' => array(
133
					8 => 'media.url',
134
			),
135
		);
136
137
		$this->context->getConfig()->set( 'controller/jobs/product/import/csv/mapping', $mapping );
138
139
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
140
		$this->object->run();
141
	}
142
143
144
	public function testRunBackup()
145
	{
146
		$config = $this->context->getConfig();
147
		$config->set( 'controller/jobs/product/import/csv/container/type', 'Zip' );
148
		$config->set( 'controller/jobs/product/import/csv/location', 'tmp/import.zip' );
149
		$config->set( 'controller/jobs/product/import/csv/backup', 'tmp/test-%Y-%m-%d.zip' );
150
151
		if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) {
152
			throw new \RuntimeException( 'Unable to copy test file' );
153
		}
154
155
		$this->object->run();
156
157
		$filename = strftime( 'tmp/test-%Y-%m-%d.zip' );
158
		$this->assertTrue( file_exists( $filename ) );
159
160
		unlink( $filename );
161
	}
162
163
164
	public function testRunBackupInvalid()
165
	{
166
		$config = $this->context->getConfig();
167
		$config->set( 'controller/jobs/product/import/csv/container/type', 'Zip' );
168
		$config->set( 'controller/jobs/product/import/csv/location', 'tmp/import.zip' );
169
		$config->set( 'controller/jobs/product/import/csv/backup', 'tmp/notexist/import.zip' );
170
171
		if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) {
172
			throw new \RuntimeException( 'Unable to copy test file' );
173
		}
174
175
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
176
		$this->object->run();
177
	}
178
179
180
	protected function delete( array $prodcodes, array $delete, array $nondelete )
181
	{
182
		$catListManager = \Aimeos\MShop\Catalog\Manager\Factory::create( $this->context )->getSubmanager( 'lists' );
183
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
184
		$listManager = $productManager->getSubManager( 'lists' );
185
186
		foreach( $this->get( $prodcodes, $delete + $nondelete ) as $id => $product )
187
		{
188
			foreach( $delete as $domain )
189
			{
190
				$manager = \Aimeos\MShop::create( $this->context, $domain );
191
192
				foreach( $product->getListItems( $domain ) as $listItem )
193
				{
194
					$manager->deleteItem( $listItem->getRefItem()->getId() );
195
					$listManager->deleteItem( $listItem->getId() );
196
				}
197
			}
198
199
			foreach( $nondelete as $domain )
200
			{
201
				$ids = array_keys( $product->getListItems( $domain ) );
202
				$listManager->deleteItems( $ids );
203
			}
204
205
			$productManager->deleteItem( $product->getId() );
206
207
			$search = $catListManager->createSearch();
208
			$search->setConditions( $search->compare( '==', 'catalog.lists.refid', $id ) );
209
			$result = $catListManager->searchItems( $search );
210
211
			$catListManager->deleteItems( array_keys( $result ) );
212
		}
213
214
215
		$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
216
217
		$search = $attrManager->createSearch();
218
		$search->setConditions( $search->compare( '==', 'attribute.code', 'import-test' ) );
219
220
		$result = $attrManager->searchItems( $search );
221
222
		$attrManager->deleteItems( array_keys( $result ) );
223
	}
224
225
226
	protected function get( array $prodcodes, array $domains )
227
	{
228
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
229
230
		$search = $productManager->createSearch();
231
		$search->setConditions( $search->compare( '==', 'product.code', $prodcodes ) );
232
233
		return $productManager->searchItems( $search, $domains );
234
	}
235
236
237
	protected function getProperties( array $prodids )
238
	{
239
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context )->getSubManager( 'property' );
240
241
		$search = $manager->createSearch();
242
		$search->setConditions( $search->compare( '==', 'product.property.parentid', $prodids ) );
243
		$search->setSortations( array( $search->sort( '+', 'product.property.type' ) ) );
244
245
		return $manager->searchItems( $search );
246
	}
247
}
248