9
9
use SilverStripe \Forms \Tests \GridField \GridFieldExportButtonTest \Team ;
10
10
use SilverStripe \ORM \DataList ;
11
11
use SilverStripe \ORM \ArrayList ;
12
- use SilverStripe \ORM \DataObject ;
13
12
use SilverStripe \Dev \SapphireTest ;
14
13
use SilverStripe \Forms \GridField \GridFieldConfig ;
15
14
use SilverStripe \Forms \GridField \GridFieldExportButton ;
16
15
use SilverStripe \Forms \GridField \GridField ;
17
16
use SilverStripe \Forms \GridField \GridFieldDataColumns ;
18
17
use SilverStripe \Forms \GridField \GridFieldPaginator ;
18
+ use SilverStripe \ORM \FieldType \DBDatetime ;
19
19
use SilverStripe \ORM \FieldType \DBField ;
20
20
use SilverStripe \View \ArrayData ;
21
21
@@ -32,6 +32,16 @@ class GridFieldExportButtonTest extends SapphireTest
32
32
*/
33
33
protected $ gridField ;
34
34
35
+ /**
36
+ * @var GridFieldConfig
37
+ */
38
+ protected $ gridFieldConfig ;
39
+
40
+ /**
41
+ * @var GridFieldExportButton
42
+ */
43
+ protected $ exportButton ;
44
+
35
45
protected static $ fixture_file = 'GridFieldExportButtonTest.yml ' ;
36
46
37
47
protected static $ extra_dataobjects = [
@@ -45,8 +55,10 @@ protected function setUp(): void
45
55
46
56
$ this ->list = new DataList (Team::class);
47
57
$ this ->list = $ this ->list ->sort ('Name ' );
48
- $ config = GridFieldConfig::create ()->addComponent (new GridFieldExportButton ());
49
- $ this ->gridField = new GridField ('testfield ' , 'testfield ' , $ this ->list , $ config );
58
+ $ this ->gridFieldConfig = GridFieldConfig::create ()->addComponent (
59
+ $ this ->exportButton = new GridFieldExportButton ()
60
+ );
61
+ $ this ->gridField = new GridField ('testfield ' , 'testfield ' , $ this ->list , $ this ->gridFieldConfig );
50
62
}
51
63
52
64
public function testCanView ()
@@ -161,8 +173,8 @@ public function testArrayListInput()
161
173
$ button = new GridFieldExportButton ();
162
174
$ columns = new GridFieldDataColumns ();
163
175
$ columns ->setDisplayFields (['ID ' => 'ID ' ]);
164
- $ this ->gridField -> getConfig () ->addComponent ($ columns );
165
- $ this ->gridField -> getConfig () ->addComponent (new GridFieldPaginator ());
176
+ $ this ->gridFieldConfig ->addComponent ($ columns );
177
+ $ this ->gridFieldConfig ->addComponent (new GridFieldPaginator ());
166
178
167
179
//Create an ArrayList 1 greater the Paginator's default 15 rows
168
180
$ arrayList = new ArrayList ();
@@ -218,6 +230,64 @@ public function testGetExportColumnsForGridFieldThrowsException()
218
230
$ reflectionMethod ->invoke ($ component , $ gridField );
219
231
}
220
232
233
+ public function testSetExportFileName ()
234
+ {
235
+ $ this ->exportButton ->setExportFileName ('export.csv ' );
236
+
237
+ $ this ->assertEquals (
238
+ 'export.csv ' ,
239
+ $ this ->exportButton ->getExportFileName ($ this ->gridField )
240
+ );
241
+
242
+ $ this ->exportButton ->setExportFileName ('[classname]-export.csv ' );
243
+
244
+ $ this ->assertEquals (
245
+ 'team-export.csv ' ,
246
+ $ this ->exportButton ->getExportFileName ($ this ->gridField )
247
+ );
248
+
249
+ $ mockDate = '2024-12-31 22:10:59 ' ;
250
+ DBDatetime::set_mock_now ($ mockDate );
251
+
252
+ $ this ->exportButton ->setExportFileName ('export-[timestamp].csv ' );
253
+
254
+ $ this ->assertEquals (
255
+ 'export-2024-12-31-22-10-59.csv ' ,
256
+ $ this ->exportButton ->getExportFileName ($ this ->gridField )
257
+ );
258
+
259
+ $ this ->exportButton ->setExportFileName ('[classname]-export-[timestamp].csv ' );
260
+
261
+ $ this ->assertEquals (
262
+ 'team-export-2024-12-31-22-10-59.csv ' ,
263
+ $ this ->exportButton ->getExportFileName ($ this ->gridField )
264
+ );
265
+
266
+ DBDatetime::clear_mock_now ();
267
+ }
268
+
269
+ public function testSetTimeStampFormat ()
270
+ {
271
+ $ mockDate = '2024-12-31 22:10:59 ' ;
272
+ DBDatetime::set_mock_now ($ mockDate );
273
+
274
+ $ this ->exportButton ->setTimeStampFormat ('yyyyMMdd-HHmmss ' );
275
+
276
+ $ this ->assertEquals (
277
+ 'team-export-20241231-221059.csv ' ,
278
+ $ this ->exportButton ->getExportFileName ($ this ->gridField )
279
+ );
280
+
281
+ $ this ->exportButton ->setTimeStampFormat ('dd-MM-yyyy ' );
282
+
283
+ $ this ->assertEquals (
284
+ 'team-export-31-12-2024.csv ' ,
285
+ $ this ->exportButton ->getExportFileName ($ this ->gridField )
286
+ );
287
+
288
+ DBDatetime::clear_mock_now ();
289
+ }
290
+
221
291
protected function createReader ($ string )
222
292
{
223
293
$ reader = Reader::createFromString ($ string );
0 commit comments