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