1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests\GridField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldExportButtonTest\NoView; |
6
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldExportButtonTest\Team; |
7
|
|
|
use SilverStripe\ORM\DataList; |
8
|
|
|
use SilverStripe\ORM\ArrayList; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton; |
13
|
|
|
use SilverStripe\Forms\GridField\GridField; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator; |
15
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
16
|
|
|
|
17
|
|
|
class GridFieldExportButtonTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var DataList |
22
|
|
|
*/ |
23
|
|
|
protected $list; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var GridField |
27
|
|
|
*/ |
28
|
|
|
protected $gridField; |
29
|
|
|
|
30
|
|
|
protected static $fixture_file = 'GridFieldExportButtonTest.yml'; |
31
|
|
|
|
32
|
|
|
protected static $extra_dataobjects = [ |
33
|
|
|
Team::class, |
34
|
|
|
NoView::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
parent::setUp(); |
40
|
|
|
|
41
|
|
|
$this->list = new DataList(Team::class); |
42
|
|
|
$this->list = $this->list->sort('Name'); |
43
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton()); |
44
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCanView() |
48
|
|
|
{ |
49
|
|
|
$list = new DataList(NoView::class); |
50
|
|
|
|
51
|
|
|
$button = new GridFieldExportButton(); |
52
|
|
|
$button->setExportColumns(['Name' => 'My Name']); |
53
|
|
|
|
54
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton()); |
55
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config); |
56
|
|
|
|
57
|
|
|
$this->assertEquals( |
58
|
|
|
"\"My Name\"\n", |
59
|
|
|
$button->generateExportFileData($gridField) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGenerateFileDataBasicFields() |
64
|
|
|
{ |
65
|
|
|
$button = new GridFieldExportButton(); |
66
|
|
|
$button->setExportColumns(['Name' => 'My Name']); |
67
|
|
|
|
68
|
|
|
$this->assertEquals( |
69
|
|
|
'"My Name"'."\n". |
70
|
|
|
'Test'."\n". |
71
|
|
|
'Test2'."\n", |
72
|
|
|
$button->generateExportFileData($this->gridField) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testXLSSanitisation() |
77
|
|
|
{ |
78
|
|
|
// Create risky object |
79
|
|
|
$object = new Team(); |
80
|
|
|
$object->Name = '=SUM(1, 2)'; |
|
|
|
|
81
|
|
|
$object->write(); |
82
|
|
|
|
83
|
|
|
// Export |
84
|
|
|
$button = new GridFieldExportButton(); |
85
|
|
|
$button->setExportColumns(['Name' => 'My Name']); |
86
|
|
|
|
87
|
|
|
$this->assertEquals( |
88
|
|
|
"\"My Name\"\n\"\t=SUM(1, 2)\"\nTest\nTest2\n", |
89
|
|
|
$button->generateExportFileData($this->gridField) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGenerateFileDataAnonymousFunctionField() |
94
|
|
|
{ |
95
|
|
|
$button = new GridFieldExportButton(); |
96
|
|
|
$button->setExportColumns([ |
97
|
|
|
'Name' => 'Name', |
98
|
|
|
'City' => function (DBField $obj) { |
99
|
|
|
return $obj->getValue() . ' city'; |
100
|
|
|
} |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
$this->assertEquals( |
104
|
|
|
'Name,City'."\n". |
105
|
|
|
'Test,"City city"'."\n". |
106
|
|
|
'Test2,"Quoted ""City"" 2 city"'."\n", |
107
|
|
|
$button->generateExportFileData($this->gridField) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testBuiltInFunctionNameCanBeUsedAsHeader() |
112
|
|
|
{ |
113
|
|
|
$button = new GridFieldExportButton(); |
114
|
|
|
$button->setExportColumns([ |
115
|
|
|
'Name' => 'Name', |
116
|
|
|
'City' => 'strtolower', |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
$this->assertEquals( |
120
|
|
|
'Name,strtolower'."\n". |
121
|
|
|
'Test,City'."\n". |
122
|
|
|
'Test2,"Quoted ""City"" 2"'."\n", |
123
|
|
|
$button->generateExportFileData($this->gridField) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testNoCsvHeaders() |
128
|
|
|
{ |
129
|
|
|
$button = new GridFieldExportButton(); |
130
|
|
|
$button->setExportColumns([ |
131
|
|
|
'Name' => 'Name', |
132
|
|
|
'City' => 'City', |
133
|
|
|
]); |
134
|
|
|
$button->setCsvHasHeader(false); |
135
|
|
|
|
136
|
|
|
$this->assertEquals( |
137
|
|
|
'Test,City'."\n". |
138
|
|
|
'Test2,"Quoted ""City"" 2"'."\n", |
139
|
|
|
$button->generateExportFileData($this->gridField) |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testArrayListInput() |
144
|
|
|
{ |
145
|
|
|
$button = new GridFieldExportButton(); |
146
|
|
|
$this->gridField->getConfig()->addComponent(new GridFieldPaginator()); |
147
|
|
|
|
148
|
|
|
//Create an ArrayList 1 greater the Paginator's default 15 rows |
149
|
|
|
$arrayList = new ArrayList(); |
150
|
|
|
for ($i = 1; $i <= 16; $i++) { |
151
|
|
|
$dataobject = new DataObject(['ID' => $i]); |
152
|
|
|
$arrayList->add($dataobject); |
153
|
|
|
} |
154
|
|
|
$this->gridField->setList($arrayList); |
155
|
|
|
|
156
|
|
|
$this->assertEquals( |
157
|
|
|
"ID\n". |
158
|
|
|
"1\n". |
159
|
|
|
"2\n". |
160
|
|
|
"3\n". |
161
|
|
|
"4\n". |
162
|
|
|
"5\n". |
163
|
|
|
"6\n". |
164
|
|
|
"7\n". |
165
|
|
|
"8\n". |
166
|
|
|
"9\n". |
167
|
|
|
"10\n". |
168
|
|
|
"11\n". |
169
|
|
|
"12\n". |
170
|
|
|
"13\n". |
171
|
|
|
"14\n". |
172
|
|
|
"15\n". |
173
|
|
|
"16\n", |
174
|
|
|
$button->generateExportFileData($this->gridField) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testZeroValue() |
179
|
|
|
{ |
180
|
|
|
$button = new GridFieldExportButton(); |
181
|
|
|
$button->setExportColumns([ |
182
|
|
|
'RugbyTeamNumber' => 'Rugby Team Number' |
183
|
|
|
]); |
184
|
|
|
|
185
|
|
|
$this->assertEquals( |
186
|
|
|
"\"Rugby Team Number\"\n2\n0\n", |
187
|
|
|
$button->generateExportFileData($this->gridField) |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|