|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\ExcelImportExport\Test; |
|
4
|
|
|
|
|
5
|
|
|
use LeKoala\ExcelImportExport\ExcelGridFieldExportButton; |
|
6
|
|
|
use SilverStripe\Security\Group; |
|
7
|
|
|
use SilverStripe\Security\Member; |
|
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
9
|
|
|
use LeKoala\ExcelImportExport\ExcelImportExport; |
|
10
|
|
|
use LeKoala\ExcelImportExport\ExcelMemberBulkLoader; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Tests for ExcelImportExport |
|
14
|
|
|
*/ |
|
15
|
|
|
class ExcelImportExportTest extends SapphireTest |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Defines the fixture file to use for this test class |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $fixture_file = 'ExcelImportExportTest.yml'; |
|
22
|
|
|
|
|
23
|
|
|
public function testGetAllFields(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$fields = ExcelImportExport::allFieldsForClass(Member::class); |
|
26
|
|
|
$this->assertNotEmpty($fields); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testExportedFields(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$fields = ExcelImportExport::exportFieldsForClass(Member::class); |
|
32
|
|
|
$this->assertNotEmpty($fields); |
|
33
|
|
|
$this->assertNotContains('Password', $fields); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testCanImportMembers(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$count = Member::get()->count(); |
|
39
|
|
|
/** @var Group $firstGroup */ |
|
40
|
|
|
$firstGroup = Group::get()->filter('Code', 'Administrators')->first(); |
|
41
|
|
|
$membersCount = $firstGroup->Members()->count(); |
|
42
|
|
|
|
|
43
|
|
|
// ; separator is properly detected thanks to auto separator feature |
|
44
|
|
|
$loader = new ExcelMemberBulkLoader(); |
|
45
|
|
|
$result = $loader->load(__DIR__ . '/data/members.csv'); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals(1, $result->CreatedCount()); |
|
48
|
|
|
|
|
49
|
|
|
$newCount = Member::get()->count(); |
|
50
|
|
|
/** @var Group $firstGroup */ |
|
51
|
|
|
$firstGroup = Group::get()->filter('Code', 'Administrators')->first(); |
|
52
|
|
|
$newMembersCount = $firstGroup->Members()->count(); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEquals($count + 1, $newCount); |
|
55
|
|
|
$this->assertEquals($membersCount + 1, $newMembersCount, "Groups are not updated"); |
|
56
|
|
|
|
|
57
|
|
|
// format is handled according to file extension |
|
58
|
|
|
$loader = new ExcelMemberBulkLoader(); |
|
59
|
|
|
$result = $loader->load(__DIR__ . '/data/members.xlsx'); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertEquals(1, $result->CreatedCount()); |
|
62
|
|
|
$this->assertEquals(0, $result->UpdatedCount()); |
|
63
|
|
|
|
|
64
|
|
|
$newCount = Member::get()->count(); |
|
65
|
|
|
/** @var Group $firstGroup */ |
|
66
|
|
|
$firstGroup = Group::get()->filter('Code', 'Administrators')->first(); |
|
67
|
|
|
$newMembersCount = $firstGroup->Members()->count(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertEquals($count + 2, $newCount); |
|
70
|
|
|
$this->assertEquals($membersCount + 2, $newMembersCount, "Groups are not updated"); |
|
71
|
|
|
|
|
72
|
|
|
// Loading again does nothing new |
|
73
|
|
|
$result = $loader->load(__DIR__ . '/data/members.xlsx'); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertEquals(0, $result->CreatedCount()); |
|
76
|
|
|
$this->assertEquals(1, $result->UpdatedCount()); |
|
77
|
|
|
|
|
78
|
|
|
$newCount = Member::get()->count(); |
|
79
|
|
|
/** @var Group $firstGroup */ |
|
80
|
|
|
$firstGroup = Group::get()->filter('Code', 'Administrators')->first(); |
|
81
|
|
|
$newMembersCount = $firstGroup->Members()->count(); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals($count + 2, $newCount); |
|
84
|
|
|
$this->assertEquals($membersCount + 2, $newMembersCount, "Groups are not updated"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testSanitize(): void |
|
88
|
|
|
{ |
|
89
|
|
|
$dangerousInput = '=1+2";=1+2'; |
|
90
|
|
|
|
|
91
|
|
|
$actual = ExcelGridFieldExportButton::sanitizeValue($dangerousInput); |
|
92
|
|
|
$expected = "\t" . $dangerousInput; |
|
93
|
|
|
$this->assertEquals($expected, $actual); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|