|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package framework |
|
5
|
|
|
* @subpackage tests |
|
6
|
|
|
*/ |
|
7
|
|
|
class GridFieldExportButtonTest extends SapphireTest { |
|
8
|
|
|
|
|
9
|
|
|
protected $list; |
|
10
|
|
|
|
|
11
|
|
|
protected $gridField; |
|
12
|
|
|
|
|
13
|
|
|
protected $form; |
|
14
|
|
|
|
|
15
|
|
|
protected static $fixture_file = 'GridFieldExportButtonTest.yml'; |
|
16
|
|
|
|
|
17
|
|
|
protected $extraDataObjects = array( |
|
18
|
|
|
'GridFieldExportButtonTest_Team', |
|
19
|
|
|
'GridFieldExportButtonTest_NoView' |
|
20
|
|
|
); |
|
21
|
|
|
|
|
22
|
|
|
public function setUp() { |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
|
|
25
|
|
|
$this->list = new DataList('GridFieldExportButtonTest_Team'); |
|
26
|
|
|
$this->list = $this->list->sort('Name'); |
|
27
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton()); |
|
28
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testCanView() { |
|
32
|
|
|
$list = new DataList('GridFieldExportButtonTest_NoView'); |
|
33
|
|
|
|
|
34
|
|
|
$button = new GridFieldExportButton(); |
|
35
|
|
|
$button->setExportColumns(array('Name' => 'My Name')); |
|
36
|
|
|
|
|
37
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton()); |
|
38
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals( |
|
41
|
|
|
"\"My Name\"\n", |
|
42
|
|
|
$button->generateExportFileData($gridField) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testGenerateFileDataBasicFields() { |
|
47
|
|
|
$button = new GridFieldExportButton(); |
|
48
|
|
|
$button->setExportColumns(array('Name' => 'My Name')); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals( |
|
51
|
|
|
"\"My Name\"\n\"Test\"\n\"Test2\"\n", |
|
52
|
|
|
$button->generateExportFileData($this->gridField) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testXLSSanitisation() { |
|
57
|
|
|
// Create risky object |
|
58
|
|
|
$object = new GridFieldExportButtonTest_Team(); |
|
59
|
|
|
$object->Name = '=SUM(1, 2)'; |
|
|
|
|
|
|
60
|
|
|
$object->write(); |
|
61
|
|
|
|
|
62
|
|
|
// Export |
|
63
|
|
|
$button = new GridFieldExportButton(); |
|
64
|
|
|
$button->setExportColumns(array('Name' => 'My Name')); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals( |
|
67
|
|
|
"\"My Name\"\n\"\t=SUM(1, 2)\"\n\"Test\"\n\"Test2\"\n", |
|
68
|
|
|
$button->generateExportFileData($this->gridField) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testGenerateFileDataAnonymousFunctionField() { |
|
73
|
|
|
$button = new GridFieldExportButton(); |
|
74
|
|
|
$button->setExportColumns(array( |
|
75
|
|
|
'Name' => 'Name', |
|
76
|
|
|
'City' => function($obj) { |
|
77
|
|
|
return $obj->getValue() . ' city'; |
|
78
|
|
|
} |
|
79
|
|
|
)); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals( |
|
82
|
|
|
"\"Name\",\"City\"\n\"Test\",\"City city\"\n\"Test2\",\"City2 city\"\n", |
|
83
|
|
|
$button->generateExportFileData($this->gridField) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testBuiltInFunctionNameCanBeUsedAsHeader() { |
|
88
|
|
|
$button = new GridFieldExportButton(); |
|
89
|
|
|
$button->setExportColumns(array( |
|
90
|
|
|
'Name' => 'Name', |
|
91
|
|
|
'City' => 'strtolower' |
|
92
|
|
|
)); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals( |
|
95
|
|
|
"\"Name\",\"strtolower\"\n\"Test\",\"City\"\n\"Test2\",\"City2\"\n", |
|
96
|
|
|
$button->generateExportFileData($this->gridField) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testNoCsvHeaders() { |
|
101
|
|
|
$button = new GridFieldExportButton(); |
|
102
|
|
|
$button->setExportColumns(array( |
|
103
|
|
|
'Name' => 'Name', |
|
104
|
|
|
'City' => 'City' |
|
105
|
|
|
)); |
|
106
|
|
|
$button->setCsvHasHeader(false); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertEquals( |
|
109
|
|
|
"\"Test\",\"City\"\n\"Test2\",\"City2\"\n", |
|
110
|
|
|
$button->generateExportFileData($this->gridField) |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testArrayListInput() { |
|
115
|
|
|
$button = new GridFieldExportButton(); |
|
116
|
|
|
$this->gridField->getConfig()->addComponent(new GridFieldPaginator()); |
|
117
|
|
|
|
|
118
|
|
|
//Create an ArrayList 1 greater the Paginator's default 15 rows |
|
119
|
|
|
$arrayList = new ArrayList(); |
|
120
|
|
|
for ($i = 1; $i <= 16; $i++) { |
|
121
|
|
|
$dataobject = new DataObject( |
|
122
|
|
|
array ( 'ID' => $i ) |
|
123
|
|
|
); |
|
124
|
|
|
$arrayList->add($dataobject); |
|
125
|
|
|
} |
|
126
|
|
|
$this->gridField->setList($arrayList); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertEquals( |
|
129
|
|
|
"\"ID\"\n\"1\"\n\"2\"\n\"3\"\n\"4\"\n\"5\"\n\"6\"\n\"7\"\n\"8\"\n" |
|
130
|
|
|
."\"9\"\n\"10\"\n\"11\"\n\"12\"\n\"13\"\n\"14\"\n\"15\"\n\"16\"\n", |
|
131
|
|
|
$button->generateExportFileData($this->gridField) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testZeroValue() { |
|
136
|
|
|
$button = new GridFieldExportButton(); |
|
137
|
|
|
$button->setExportColumns(array( |
|
138
|
|
|
'RugbyTeamNumber' => 'Rugby Team Number' |
|
139
|
|
|
)); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertEquals( |
|
142
|
|
|
"\"Rugby Team Number\"\n\"2\"\n\"0\"\n", |
|
143
|
|
|
$button->generateExportFileData($this->gridField) |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @package framework |
|
150
|
|
|
* @subpackage tests |
|
151
|
|
|
*/ |
|
152
|
|
|
class GridFieldExportButtonTest_Team extends DataObject implements TestOnly { |
|
153
|
|
|
|
|
154
|
|
|
private static $db = array( |
|
155
|
|
|
'Name' => 'Varchar', |
|
156
|
|
|
'City' => 'Varchar', |
|
157
|
|
|
'RugbyTeamNumber' => 'Int' |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
public function canView($member = null) { |
|
161
|
|
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @package framework |
|
168
|
|
|
* @subpackage tests |
|
169
|
|
|
*/ |
|
170
|
|
|
class GridFieldExportButtonTest_NoView extends DataObject implements TestOnly { |
|
171
|
|
|
|
|
172
|
|
|
private static $db = array( |
|
173
|
|
|
'Name' => 'Varchar', |
|
174
|
|
|
'City' => 'Varchar', |
|
175
|
|
|
'RugbyTeamNumber' => 'Int' |
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
public function canView($member = null) { |
|
179
|
|
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.