Skip to content

Commit 1186b69

Browse files
committed
add test for class migration
1 parent 4a55f19 commit 1186b69

File tree

4 files changed

+89
-45
lines changed

4 files changed

+89
-45
lines changed

src/ExcelBulkLoader.php

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -109,43 +109,34 @@ public function load($filepath)
109109
Environment::increaseTimeLimitTo(3600);
110110
Environment::increaseMemoryLimitTo('512M');
111111

112-
if ($this->useTransaction) {
113-
DB::get_conn()->transactionStart();
114-
}
115-
116-
try {
117-
//get all instances of the to be imported data object
118-
if ($this->deleteExistingRecords) {
119-
if ($this->getCheckPermissions()) {
120-
// We need to check each record, in case there's some fancy conditional logic in the canDelete method.
121-
// If we can't delete even a single record, we should bail because otherwise the result would not be
122-
// what the user expects.
123-
/** @var DataObject $record */
124-
foreach (DataObject::get($this->objectClass) as $record) {
125-
if (!$record->canDelete()) {
126-
$type = $record->i18n_singular_name();
127-
throw new HTTPResponse_Exception(
128-
_t(__CLASS__ . '.CANNOT_DELETE', "Not allowed to delete '{type}' records", ["type" => $type]),
129-
403
130-
);
131-
}
112+
//get all instances of the to be imported data object
113+
if ($this->deleteExistingRecords) {
114+
if ($this->getCheckPermissions()) {
115+
// We need to check each record, in case there's some fancy conditional logic in the canDelete method.
116+
// If we can't delete even a single record, we should bail because otherwise the result would not be
117+
// what the user expects.
118+
/** @var DataObject $record */
119+
foreach (DataObject::get($this->objectClass) as $record) {
120+
if (!$record->canDelete()) {
121+
$type = $record->i18n_singular_name();
122+
throw new HTTPResponse_Exception(
123+
_t(__CLASS__ . '.CANNOT_DELETE', "Not allowed to delete '{type}' records", ["type" => $type]),
124+
403
125+
);
132126
}
133127
}
134-
DataObject::get($this->objectClass)->removeAll();
135128
}
136-
137-
$result = $this->processAll($filepath);
138-
139129
if ($this->useTransaction) {
140-
DB::get_conn()->transactionEnd();
130+
DB::get_conn()->transactionStart();
141131
}
142-
} catch (Exception $e) {
132+
DataObject::get($this->objectClass)->removeAll();
143133
if ($this->useTransaction) {
144-
DB::get_conn()->transactionRollback();
134+
DB::get_conn()->transactionEnd();
145135
}
146-
$code = $e->getCode() ?: 500;
147-
throw new HTTPResponse_Exception($e->getMessage(), $code);
148136
}
137+
138+
$result = $this->processAll($filepath);
139+
149140
return $result;
150141
}
151142

@@ -186,13 +177,11 @@ protected function processAll($filepath, $preview = false)
186177
{
187178
$this->extend('onBeforeProcessAll', $filepath, $preview);
188179

189-
$results = new BulkLoader_Result();
190-
$ext = $this->getUploadFileExtension();
191-
192180
if (!is_readable($filepath)) {
193181
throw new Exception("Cannot read $filepath");
194182
}
195183

184+
$ext = $this->getUploadFileExtension();
196185
$opts = [
197186
'separator' => $this->delimiter,
198187
'enclosure' => $this->enclosure,
@@ -204,22 +193,50 @@ protected function processAll($filepath, $preview = false)
204193

205194
$data = SpreadCompat::read($filepath, ...$opts);
206195

196+
$results = $this->processData($data, $preview);
197+
198+
$this->extend('onAfterProcessAll', $result, $preview);
199+
200+
return $results;
201+
}
202+
203+
/**
204+
* @param iterable $data
205+
* @param bool $preview
206+
* @return BulkLoader_Result
207+
*/
208+
public function processData($data, $preview = false)
209+
{
210+
$results = new BulkLoader_Result();
211+
207212
$objectClass = $this->objectClass;
208213
$objectConfig = $objectClass::config();
209214
$this->db = $objectConfig->db;
210215
$this->singleton = singleton($objectClass);
211216

212-
foreach ($data as $row) {
213-
$this->processRecord(
214-
$row,
215-
$this->columnMap,
216-
$results,
217-
$preview
218-
);
217+
try {
218+
if ($this->useTransaction) {
219+
DB::get_conn()->transactionStart();
220+
}
221+
foreach ($data as $row) {
222+
$this->processRecord(
223+
$row,
224+
$this->columnMap,
225+
$results,
226+
$preview
227+
);
228+
}
229+
if ($this->useTransaction) {
230+
DB::get_conn()->transactionEnd();
231+
}
232+
} catch (Exception $e) {
233+
if ($this->useTransaction) {
234+
DB::get_conn()->transactionRollback();
235+
}
236+
$code = $e->getCode() ?: 500;
237+
throw new HTTPResponse_Exception($e->getMessage(), $code);
219238
}
220239

221-
$this->extend('onAfterProcessAll', $result, $preview);
222-
223240
return $results;
224241
}
225242

src/ExcelGroupBulkLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class ExcelGroupBulkLoader extends ExcelBulkLoader
1616
/**
1717
* @var array<string,string>
1818
*/
19-
public $duplicateChecks = array(
19+
public $duplicateChecks = [
2020
'Code' => 'Code',
21-
);
21+
];
2222

2323
/**
2424
* @param class-string $objectClass

src/ExcelMemberBulkLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class ExcelMemberBulkLoader extends ExcelBulkLoader
2424
/**
2525
* @var array<string,string>
2626
*/
27-
public $duplicateChecks = array(
27+
public $duplicateChecks = [
2828
'Email' => 'Email',
29-
);
29+
];
3030

3131
/**
3232
* @param class-string $objectClass

tests/ExcelImportExportTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,33 @@ public function testImportMultipleClasses(): void
126126
$this->assertEquals($newTotalCount, $totalCount);
127127
}
128128

129+
public function testImportMigrateClasses(): void
130+
{
131+
// For members, duplicate is done on emails
132+
$duplicateEmail = '[email protected]';
133+
134+
$baseMember = new Member();
135+
$baseMember->Email = $duplicateEmail;
136+
$baseMember->write();
137+
138+
$loader = new ExcelMemberBulkLoader();
139+
$result = $loader->processData([
140+
[
141+
'ID' => $baseMember->ID,
142+
'ClassName' => TestExcelMember::class,
143+
'Email' => $duplicateEmail,
144+
]
145+
]);
146+
147+
$this->assertEquals(0, $result->CreatedCount());
148+
$this->assertEquals(1, $result->UpdatedCount());
149+
150+
// Check that our existing member has the new class
151+
$member = TestExcelMember::get()->filter('Email', $duplicateEmail)->first();
152+
$this->assertNotEmpty($member);
153+
$this->assertEquals($baseMember->ID, $member->ID);
154+
}
155+
129156
public function testTransaction()
130157
{
131158
$beforeCount = Member::get()->count();

0 commit comments

Comments
 (0)