1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class GenerateCSVJobTest extends SapphireTest { |
|
|
|
|
4
|
|
|
|
5
|
|
|
protected static $fixture_file = 'GenerateCSVJobTest.yml'; |
6
|
|
|
|
7
|
|
|
protected $extraDataObjects = array('GenerateCSVJobTest_Record'); |
8
|
|
|
|
9
|
|
|
public function setUp() { |
10
|
|
|
parent::setUp(); |
11
|
|
|
Config::inst()->update('Director', 'rules', array( |
12
|
|
|
'jobtest//$Action/$ID/$OtherID' => 'GenerateCSVJobTest_Controller' |
13
|
|
|
)); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
protected $paths = array(); |
17
|
|
|
|
18
|
|
|
public function tearDown() { |
19
|
|
|
foreach($this->paths as $path) { |
20
|
|
|
Filesystem::removeFolder(dirname($path)); |
21
|
|
|
} |
22
|
|
|
parent::tearDown(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testGenerateExport() { |
26
|
|
|
// Build session |
27
|
|
|
$memberID = $this->logInWithPermission('ADMIN'); |
28
|
|
|
$session = array('loggedInAs' => $memberID); |
29
|
|
|
|
30
|
|
|
// Build controller |
31
|
|
|
$controller = new GenerateCSVJobTest_Controller(); |
32
|
|
|
$form = $controller->Form(); |
33
|
|
|
$gridfield = $form->Fields()->fieldByName('MyGridfield'); |
34
|
|
|
|
35
|
|
|
// Build job |
36
|
|
|
$job = $this->createJob($gridfield, $session); |
37
|
|
|
$path = sprintf('%1$s/.exports/%2$s/%2$s.csv', ASSETS_PATH, $job->getSignature()); |
38
|
|
|
$this->paths[] = $path; // Mark for cleanup later |
39
|
|
|
|
40
|
|
|
// Test that the job runs |
41
|
|
|
$this->assertFileNotExists($path); |
|
|
|
|
42
|
|
|
$job->process(); |
43
|
|
|
$this->assertFileExists($path); |
|
|
|
|
44
|
|
|
|
45
|
|
|
// Test that the output matches the expected |
46
|
|
|
$expected = <<<EOS |
47
|
|
|
"Title","Content","Publish On" |
48
|
|
|
"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01" |
49
|
|
|
"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01" |
50
|
|
|
"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01" |
51
|
|
|
|
52
|
|
|
EOS; |
53
|
|
|
$actual = file_get_contents($path); |
54
|
|
|
$this->assertEquals($expected, $actual); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Rough copy of GridFieldQueuedExportButton::startExport |
59
|
|
|
* |
60
|
|
|
* @param GridField $gridField |
61
|
|
|
* @param array $session |
62
|
|
|
* @return GenerateCSVJob |
63
|
|
|
*/ |
64
|
|
|
protected function createJob($gridField, $session) { |
65
|
|
|
$job = new GenerateCSVJob(); |
66
|
|
|
$job->setGridField($gridField); |
67
|
|
|
$job->setSession($session); |
68
|
|
|
$job->setSeparator(','); |
69
|
|
|
$job->setIncludeHeader(true); |
70
|
|
|
return $job; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
class GenerateCSVJobTest_Record extends DataObject implements TestOnly { |
|
|
|
|
76
|
|
|
|
77
|
|
|
private static $summary_fields = array( |
|
|
|
|
78
|
|
|
'Title', |
79
|
|
|
'Content', |
80
|
|
|
'PublishOn', |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
private static $db = array( |
|
|
|
|
84
|
|
|
'Title' => 'Varchar', |
85
|
|
|
'Content' => 'Varchar', |
86
|
|
|
'PublishOn' => 'SS_DateTime' |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
class GenerateCSVJobTest_Controller extends Controller implements TestOnly { |
|
|
|
|
91
|
|
|
private static $allowed_actions = array('Form'); |
|
|
|
|
92
|
|
|
|
93
|
|
|
public function Link() { |
94
|
|
|
return 'jobtest/'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function Form() { |
98
|
|
|
// Get records |
99
|
|
|
$records = GenerateCSVJobTest_Record::get(); |
100
|
|
|
|
101
|
|
|
// Set config |
102
|
|
|
$config = GridFieldConfig_RecordEditor::create(); |
103
|
|
|
$config->removeComponentsByType('GridFieldExportButton'); |
104
|
|
|
$config->addComponent(new GridFieldQueuedExportButton('buttons-after-left')); |
105
|
|
|
$fields = new GridField('MyGridfield', 'My Records', $records, $config); |
106
|
|
|
return new Form($this, 'Form', new FieldList($fields), new FieldList()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.