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