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