|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2020 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
namespace Aimeos\Controller\Jobs\Supplier\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/supplier/import/csv/skip-lines', 1 ); |
|
28
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/location', __DIR__ . '/_testfiles/valid' ); |
|
29
|
|
|
|
|
30
|
|
|
$this->object = new \Aimeos\Controller\Jobs\Supplier\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( 'Supplier import CSV', $this->object->getName() ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
public function testGetDescription() |
|
52
|
|
|
{ |
|
53
|
|
|
$text = 'Imports new and updates existing suppliers 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('address'); |
|
62
|
|
|
$delete = array('media', 'text'); |
|
63
|
|
|
|
|
64
|
|
|
$convert = array( |
|
65
|
|
|
1 => 'Text/LatinUTF8', |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->context->getConfig()->set( 'controller/jobs/supplier/import/csv/converter', $convert ); |
|
69
|
|
|
|
|
70
|
|
|
$this->object->run(); |
|
71
|
|
|
|
|
72
|
|
|
$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) ); |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$addresses = $this->getAddresses( $result->keys()->toArray() ); |
|
76
|
|
|
$this->delete( $prodcodes, $delete, $nondelete ); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertEquals( 2, count( $result ) ); |
|
79
|
|
|
$this->assertEquals( 2, count( $addresses ) ); |
|
80
|
|
|
|
|
81
|
|
|
foreach( $result as $supplier ) { |
|
82
|
|
|
$this->assertEquals( 2, count( $supplier->getListItems() ) ); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
public function testRunUpdate() |
|
88
|
|
|
{ |
|
89
|
|
|
$prodcodes = array('job_csv_test', 'job_csv_test2'); |
|
90
|
|
|
$nondelete = array('address'); |
|
91
|
|
|
$delete = array('media', 'text'); |
|
92
|
|
|
|
|
93
|
|
|
$this->object->run(); |
|
94
|
|
|
$this->object->run(); |
|
95
|
|
|
|
|
96
|
|
|
$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) ); |
|
97
|
|
|
$addresses = $this->getAddresses( $result->keys()->toArray() ); |
|
98
|
|
|
$this->delete( $prodcodes, $delete, $nondelete ); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals( 2, count( $result ) ); |
|
101
|
|
|
$this->assertEquals( 2, count( $addresses ) ); |
|
102
|
|
|
|
|
103
|
|
|
foreach( $result as $supplier ) { |
|
104
|
|
|
$this->assertEquals( 2, count( $supplier->getListItems() ) ); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
public function testRunPosition() |
|
110
|
|
|
{ |
|
111
|
|
|
$prodcodes = array('job_csv_test', 'job_csv_test2'); |
|
112
|
|
|
$nondelete = array('address'); |
|
113
|
|
|
$delete = array('media', 'text'); |
|
114
|
|
|
|
|
115
|
|
|
$config = $this->context->getConfig(); |
|
116
|
|
|
$mapping = $config->get( 'controller/jobs/supplier/import/csv/mapping', [] ); |
|
117
|
|
|
$mapping['item'] = array(0 => 'supplier.label', 1 => 'supplier.code'); |
|
118
|
|
|
|
|
119
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/mapping', $mapping ); |
|
120
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/location', __DIR__ . '/_testfiles/position' ); |
|
121
|
|
|
|
|
122
|
|
|
$this->object->run(); |
|
123
|
|
|
|
|
124
|
|
|
$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) ); |
|
125
|
|
|
$this->delete( $prodcodes, $delete, $nondelete ); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals( 2, count( $result ) ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
public function testRunProcessorInvalidMapping() |
|
132
|
|
|
{ |
|
133
|
|
|
$mapping = array( |
|
134
|
|
|
'media' => array( |
|
135
|
|
|
8 => 'media.url', |
|
136
|
|
|
), |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
$this->context->getConfig()->set( 'controller/jobs/supplier/import/csv/mapping', $mapping ); |
|
140
|
|
|
|
|
141
|
|
|
$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' ); |
|
142
|
|
|
$this->object->run(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
public function testRunBackup() |
|
147
|
|
|
{ |
|
148
|
|
|
$config = $this->context->getConfig(); |
|
149
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/container/type', 'Zip' ); |
|
150
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/location', 'tmp/import.zip' ); |
|
151
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/backup', 'tmp/test-%Y-%m-%d.zip' ); |
|
152
|
|
|
|
|
153
|
|
|
if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) { |
|
154
|
|
|
throw new \RuntimeException( 'Unable to copy test file' ); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$this->object->run(); |
|
158
|
|
|
|
|
159
|
|
|
$filename = strftime( 'tmp/test-%Y-%m-%d.zip' ); |
|
160
|
|
|
$this->assertTrue( file_exists( $filename ) ); |
|
161
|
|
|
|
|
162
|
|
|
unlink( $filename ); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
public function testRunBackupInvalid() |
|
167
|
|
|
{ |
|
168
|
|
|
$config = $this->context->getConfig(); |
|
169
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/container/type', 'Zip' ); |
|
170
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/location', 'tmp/import.zip' ); |
|
171
|
|
|
$config->set( 'controller/jobs/supplier/import/csv/backup', 'tmp/notexist/import.zip' ); |
|
172
|
|
|
|
|
173
|
|
|
if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) { |
|
174
|
|
|
throw new \RuntimeException( 'Unable to copy test file' ); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' ); |
|
178
|
|
|
$this->object->run(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
protected function delete( array $prodcodes, array $delete, array $nondelete ) |
|
183
|
|
|
{ |
|
184
|
|
|
$catListManager = \Aimeos\MShop\Catalog\Manager\Factory::create( $this->context )->getSubmanager( 'lists' ); |
|
185
|
|
|
$supplierManager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context ); |
|
186
|
|
|
$listManager = $supplierManager->getSubManager( 'lists' ); |
|
187
|
|
|
|
|
188
|
|
|
foreach( $this->get( $prodcodes, $delete + $nondelete ) as $id => $supplier ) { |
|
189
|
|
|
foreach( $delete as $domain ) { |
|
190
|
|
|
$manager = \Aimeos\MShop::create( $this->context, $domain ); |
|
191
|
|
|
|
|
192
|
|
|
foreach( $supplier->getListItems( $domain ) as $listItem ) { |
|
193
|
|
|
$manager->deleteItem( $listItem->getRefItem()->getId() ); |
|
194
|
|
|
$listManager->deleteItem( $listItem->getId() ); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
foreach( $nondelete as $domain ) { |
|
199
|
|
|
$listManager->deleteItems( $supplier->getListItems( $domain )->toArray() ); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$supplierManager->deleteItem( $supplier->getId() ); |
|
203
|
|
|
|
|
204
|
|
|
$search = $catListManager->createSearch(); |
|
205
|
|
|
$search->setConditions( $search->compare( '==', 'catalog.lists.refid', $id ) ); |
|
206
|
|
|
$result = $catListManager->searchItems( $search ); |
|
207
|
|
|
|
|
208
|
|
|
$catListManager->deleteItems( $result->toArray() ); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context ); |
|
213
|
|
|
|
|
214
|
|
|
$search = $attrManager->createSearch(); |
|
215
|
|
|
$search->setConditions( $search->compare( '==', 'attribute.code', 'import-test' ) ); |
|
216
|
|
|
|
|
217
|
|
|
$result = $attrManager->searchItems( $search ); |
|
218
|
|
|
|
|
219
|
|
|
$attrManager->deleteItems( $result->toArray() ); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
protected function get( array $prodcodes, array $domains ) |
|
224
|
|
|
{ |
|
225
|
|
|
$supplierManager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context ); |
|
226
|
|
|
|
|
227
|
|
|
$search = $supplierManager->createSearch(); |
|
228
|
|
|
$search->setConditions( $search->compare( '==', 'supplier.code', $prodcodes ) ); |
|
229
|
|
|
|
|
230
|
|
|
return $supplierManager->searchItems( $search, $domains ); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected function getAddresses( array $prodids ) |
|
234
|
|
|
{ |
|
235
|
|
|
$manager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context )->getSubManager( 'address' ); |
|
236
|
|
|
|
|
237
|
|
|
$search = $manager->createSearch(); |
|
238
|
|
|
$search->setConditions( $search->compare( '==', 'supplier.address.parentid', $prodids ) ); |
|
239
|
|
|
|
|
240
|
|
|
return $manager->searchItems( $search ); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|