Passed
Push — master ( ce780a...b27fbc )
by Aimeos
03:18
created

StandardTest::testRunBackupInvalid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
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
	public static function setUpBeforeClass() : void
20
	{
21
		$context = \TestHelper::context();
22
23
		$fs = $context->fs( 'fs-import' );
24
		$fs->has( 'product' ) ?: $fs->mkdir( 'product' );
25
		$fs->writef( 'product/empty.csv', __DIR__ . '/_testfiles/empty.csv' );
26
27
		$fs->has( 'product/valid' ) ?: $fs->mkdir( 'product/valid' );
28
		$fs->writef( 'product/valid/products.csv', __DIR__ . '/_testfiles/valid/products.csv' );
29
		$fs->writef( 'product/valid/products.csv', __DIR__ . '/_testfiles/valid/products.csv' );
30
31
		$fs->has( 'product/position' ) ?: $fs->mkdir( 'product/position' );
32
		$fs->writef( 'product/position/products.csv', __DIR__ . '/_testfiles/position/products.csv' );
33
		$fs->writef( 'product/position/products.csv', __DIR__ . '/_testfiles/position/products.csv' );
34
35
		$fs = $context->fs( 'fs-media' );
36
		$fs->has( 'path/to' ) ?: $fs->mkdir( 'path/to' );
37
		$fs->write( 'path/to/file2.jpg', 'test' );
38
		$fs->write( 'path/to/file.jpg', 'test' );
39
40
		$fs = $context->fs( 'fs-mimeicon' );
41
		$fs->write( 'unknown.png', 'icon' );
42
	}
43
44
45
	protected function setUp() : void
46
	{
47
		\Aimeos\MShop::cache( true );
48
49
		$this->context = \TestHelper::context();
50
		$this->aimeos = \TestHelper::getAimeos();
51
52
		$config = $this->context->config();
53
		$config->set( 'controller/jobs/product/import/csv/skip-lines', 1 );
54
		$config->set( 'controller/jobs/product/import/csv/location', 'product/valid' );
55
56
		$this->object = new \Aimeos\Controller\Jobs\Product\Import\Csv\Standard( $this->context, $this->aimeos );
57
	}
58
59
60
	protected function tearDown() : void
61
	{
62
		\Aimeos\MShop::cache( false );
63
		unset( $this->object );
64
65
		if( file_exists( 'tmp/import.zip' ) ) {
66
			unlink( 'tmp/import.zip' );
67
		}
68
	}
69
70
71
	public function testGetName()
72
	{
73
		$this->assertEquals( 'Product import CSV', $this->object->getName() );
74
	}
75
76
77
	public function testGetDescription()
78
	{
79
		$text = 'Imports new and updates existing products from CSV files';
80
		$this->assertEquals( $text, $this->object->getDescription() );
81
	}
82
83
84
	public function testRun()
85
	{
86
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
87
		$nondelete = array( 'attribute', 'product', 'catalog' );
88
		$delete = array( 'media', 'price', 'text' );
89
90
		$this->object->run();
91
92
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
93
		$properties = $this->getProperties( array_keys( $result ) );
94
		$this->delete( $prodcodes, $delete );
95
96
		$this->assertEquals( 2, count( $result ) );
97
		$this->assertEquals( 2, count( $properties ) );
98
99
		foreach( $result as $product ) {
100
			$this->assertEquals( 6, count( $product->getListItems() ) );
101
		}
102
	}
103
104
105
	public function testRunUpdate()
106
	{
107
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
108
		$nondelete = array( 'attribute', 'product', 'catalog' );
109
		$delete = array( 'media', 'price', 'text' );
110
111
		$this->object->run();
112
		$this->object->run();
113
114
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
115
		$properties = $this->getProperties( array_keys( $result ) );
116
		$this->delete( $prodcodes, $delete );
117
118
		$this->assertEquals( 2, count( $result ) );
119
		$this->assertEquals( 2, count( $properties ) );
120
121
		foreach( $result as $product ) {
122
			$this->assertEquals( 6, count( $product->getListItems() ) );
123
		}
124
	}
125
126
127
	public function testRunPosition()
128
	{
129
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
130
		$nondelete = array( 'attribute', 'product' );
131
		$delete = array( 'media', 'price', 'text' );
132
133
		$config = $this->context->config();
134
		$mapping = $config->get( 'controller/jobs/product/import/csv/mapping', [] );
135
		$mapping['item'] = array( 0 => 'product.label', 1 => 'product.code' );
136
137
		$config->set( 'controller/jobs/product/import/csv/mapping', $mapping );
138
		$config->set( 'controller/jobs/product/import/csv/location', 'product/position' );
139
140
		$this->object->run();
141
142
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
143
		$this->delete( $prodcodes, $delete );
144
145
		$this->assertEquals( 2, count( $result ) );
146
	}
147
148
149
	public function testRunProcessorInvalidMapping()
150
	{
151
		$mapping = array(
152
			'media' => array(
153
					8 => 'media.url',
154
			),
155
		);
156
157
		$this->context->config()->set( 'controller/jobs/product/import/csv/mapping', $mapping );
158
159
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
160
		$this->object->run();
161
	}
162
163
164
	public function testRunBackup()
165
	{
166
		$config = $this->context->config();
167
		$config->set( 'controller/jobs/product/import/csv/backup', 'backup-%Y-%m-%d.csv' );
168
		$config->set( 'controller/jobs/product/import/csv/location', 'product' );
169
170
		$this->object->run();
171
172
		$filename = \Aimeos\Base\Str::strtime( 'backup-%Y-%m-%d.csv' );
173
		$this->assertTrue( $this->context->fs( 'fs-import' )->has( $filename ) );
174
175
		$this->context->fs( 'fs-import' )->rm( $filename );
176
	}
177
178
179
	protected function delete( array $prodcodes, array $delete )
180
	{
181
		$productManager = \Aimeos\MShop::create( $this->context, 'product' );
182
183
		foreach( $this->get( $prodcodes, $delete ) as $id => $product )
184
		{
185
			foreach( $delete as $domain )
186
			{
187
				$ids = $product->getListItems( $domain )->getRefId()->all();
188
				\Aimeos\MShop::create( $this->context, $domain )->delete( $ids );
189
			}
190
191
			$productManager->delete( $product->getId() );
192
		}
193
194
195
		$attrManager = \Aimeos\MShop::create( $this->context, 'attribute' );
196
197
		$search = $attrManager->filter();
198
		$search->setConditions( $search->compare( '==', 'attribute.code', 'import-test' ) );
199
200
		$attrManager->delete( $attrManager->search( $search ) );
201
	}
202
203
204
	protected function get( array $prodcodes, array $domains ) : array
205
	{
206
		$productManager = \Aimeos\MShop::create( $this->context, 'product' );
207
208
		$search = $productManager->filter();
209
		$search->setConditions( $search->compare( '==', 'product.code', $prodcodes ) );
210
211
		return $productManager->search( $search, $domains )->all();
212
	}
213
214
215
	protected function getProperties( array $prodids ) : array
216
	{
217
		$manager = \Aimeos\MShop::create( $this->context, 'product/property' );
218
219
		$search = $manager->filter()->order( 'product.property.type' )
220
			->add( ['product.property.parentid' => $prodids] );
221
222
		return $manager->search( $search )->all();
223
	}
224
}
225