99use SilverStripe \Forms \Tests \GridField \GridFieldExportButtonTest \Team ;
1010use SilverStripe \ORM \DataList ;
1111use SilverStripe \ORM \ArrayList ;
12- use SilverStripe \ORM \DataObject ;
1312use SilverStripe \Dev \SapphireTest ;
1413use SilverStripe \Forms \GridField \GridFieldConfig ;
1514use SilverStripe \Forms \GridField \GridFieldExportButton ;
1615use SilverStripe \Forms \GridField \GridField ;
1716use SilverStripe \Forms \GridField \GridFieldDataColumns ;
1817use SilverStripe \Forms \GridField \GridFieldPaginator ;
18+ use SilverStripe \ORM \FieldType \DBDatetime ;
1919use SilverStripe \ORM \FieldType \DBField ;
2020use SilverStripe \View \ArrayData ;
2121
@@ -32,6 +32,16 @@ class GridFieldExportButtonTest extends SapphireTest
3232 */
3333 protected $ gridField ;
3434
35+ /**
36+ * @var GridFieldConfig
37+ */
38+ protected $ gridFieldConfig ;
39+
40+ /**
41+ * @var GridFieldExportButton
42+ */
43+ protected $ exportButton ;
44+
3545 protected static $ fixture_file = 'GridFieldExportButtonTest.yml ' ;
3646
3747 protected static $ extra_dataobjects = [
@@ -45,8 +55,10 @@ protected function setUp(): void
4555
4656 $ this ->list = new DataList (Team::class);
4757 $ 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 );
5062 }
5163
5264 public function testCanView ()
@@ -161,8 +173,8 @@ public function testArrayListInput()
161173 $ button = new GridFieldExportButton ();
162174 $ columns = new GridFieldDataColumns ();
163175 $ 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 ());
166178
167179 //Create an ArrayList 1 greater the Paginator's default 15 rows
168180 $ arrayList = new ArrayList ();
@@ -218,6 +230,64 @@ public function testGetExportColumnsForGridFieldThrowsException()
218230 $ reflectionMethod ->invoke ($ component , $ gridField );
219231 }
220232
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 ('Ymd-His ' );
275+
276+ $ this ->assertEquals (
277+ 'export-20241231-221059.csv ' ,
278+ $ this ->exportButton ->getExportFileName ($ this ->gridField )
279+ );
280+
281+ $ this ->exportButton ->setTimeStampFormat ('d-m-Y ' );
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+
221291 protected function createReader ($ string )
222292 {
223293 $ reader = Reader::createFromString ($ string );
0 commit comments