|
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" . 'Test' . "\n" . 'Test2' . "\n", |
|
70
|
|
|
$button->generateExportFileData($this->gridField) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testXLSSanitisation() |
|
75
|
|
|
{ |
|
76
|
|
|
// Create risky object |
|
77
|
|
|
$object = new Team(); |
|
78
|
|
|
$object->Name = '=SUM(1, 2)'; |
|
|
|
|
|
|
79
|
|
|
$object->write(); |
|
80
|
|
|
|
|
81
|
|
|
// Export |
|
82
|
|
|
$button = new GridFieldExportButton(); |
|
83
|
|
|
$button->setExportColumns(['Name' => 'My Name']); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals( |
|
86
|
|
|
"\"My Name\"\n\"\t=SUM(1, 2)\"\nTest\nTest2\n", |
|
87
|
|
|
$button->generateExportFileData($this->gridField) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testGenerateFileDataAnonymousFunctionField() |
|
92
|
|
|
{ |
|
93
|
|
|
$button = new GridFieldExportButton(); |
|
94
|
|
|
$button->setExportColumns([ |
|
95
|
|
|
'Name' => 'Name', |
|
96
|
|
|
'City' => function (DBField $obj) { |
|
97
|
|
|
return $obj->getValue() . ' city'; |
|
98
|
|
|
} |
|
99
|
|
|
]); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertEquals( |
|
102
|
|
|
'Name,City' . "\n" . 'Test,"City city"' . "\n" . 'Test2,"Quoted ""City"" 2 city"' . "\n", |
|
103
|
|
|
$button->generateExportFileData($this->gridField) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testBuiltInFunctionNameCanBeUsedAsHeader() |
|
108
|
|
|
{ |
|
109
|
|
|
$button = new GridFieldExportButton(); |
|
110
|
|
|
$button->setExportColumns([ |
|
111
|
|
|
'Name' => 'Name', |
|
112
|
|
|
'City' => 'strtolower', |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals( |
|
116
|
|
|
'Name,strtolower' . "\n" . 'Test,City' . "\n" . 'Test2,"Quoted ""City"" 2"' . "\n", |
|
117
|
|
|
$button->generateExportFileData($this->gridField) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testNoCsvHeaders() |
|
122
|
|
|
{ |
|
123
|
|
|
$button = new GridFieldExportButton(); |
|
124
|
|
|
$button->setExportColumns([ |
|
125
|
|
|
'Name' => 'Name', |
|
126
|
|
|
'City' => 'City', |
|
127
|
|
|
]); |
|
128
|
|
|
$button->setCsvHasHeader(false); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertEquals( |
|
131
|
|
|
'Test,City' . "\n" . 'Test2,"Quoted ""City"" 2"' . "\n", |
|
132
|
|
|
$button->generateExportFileData($this->gridField) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testArrayListInput() |
|
137
|
|
|
{ |
|
138
|
|
|
$button = new GridFieldExportButton(); |
|
139
|
|
|
$this->gridField->getConfig()->addComponent(new GridFieldPaginator()); |
|
140
|
|
|
|
|
141
|
|
|
//Create an ArrayList 1 greater the Paginator's default 15 rows |
|
142
|
|
|
$arrayList = new ArrayList(); |
|
143
|
|
|
for ($i = 1; $i <= 16; $i++) { |
|
144
|
|
|
$dataobject = new DataObject(['ID' => $i]); |
|
145
|
|
|
$arrayList->add($dataobject); |
|
146
|
|
|
} |
|
147
|
|
|
$this->gridField->setList($arrayList); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertEquals( |
|
150
|
|
|
"ID\n" . "1\n" . "2\n" . "3\n" . "4\n" . "5\n" . "6\n" . "7\n" . "8\n" . "9\n" . "10\n" . "11\n" . "12\n" . "13\n" . "14\n" . "15\n" . "16\n", |
|
151
|
|
|
$button->generateExportFileData($this->gridField) |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testZeroValue() |
|
156
|
|
|
{ |
|
157
|
|
|
$button = new GridFieldExportButton(); |
|
158
|
|
|
$button->setExportColumns([ |
|
159
|
|
|
'RugbyTeamNumber' => 'Rugby Team Number' |
|
160
|
|
|
]); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertEquals( |
|
163
|
|
|
"\"Rugby Team Number\"\n2\n0\n", |
|
164
|
|
|
$button->generateExportFileData($this->gridField) |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|