Completed
Pull Request — master (#24)
by Daniel
01:32
created

testGenerateExportOverMultipleSteps()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 0
1
<?php
2
3
namespace SilverStripe\GridFieldQueuedExport\Tests;
4
5
use SilverStripe\Assets\Filesystem;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\GridfieldQueuedExport\Jobs\GenerateCSVJob;
11
12
class GenerateCSVJobTest extends SapphireTest
13
{
14
15
    protected static $fixture_file = 'GenerateCSVJobTest.yml';
16
17
    protected static $extra_dataobjects = [GenerateCSVJobTestRecord::class];
18
19
    protected static $extra_controllers = [GenerateCSVJobTestController::class];
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        Config::modify()->merge(Director::class, 'rules', [
26
            'jobtest//$Action/$ID/$OtherID' => GenerateCSVJobTestController::class
27
        ]);
28
    }
29
30
    protected $paths = [];
31
32
    protected function tearDown()
33
    {
34
        foreach ($this->paths as $path) {
35
            Filesystem::removeFolder(dirname($path));
36
        }
37
        parent::tearDown();
38
    }
39
40
    public function testGenerateExport()
41
    {
42
        // Build session
43
        $memberID = $this->logInWithPermission('ADMIN');
44
        $session = ['loggedInAs' => $memberID];
45
46
        // Build controller
47
        $controller = new GenerateCSVJobTestController();
48
        $form = $controller->Form();
49
        $gridfield = $form->Fields()->fieldByName('MyGridfield');
50
51
        // Build job
52
        $job = $this->createJob($gridfield, $session);
53
        $path = sprintf('%1$s/.exports/%2$s/%2$s.csv', ASSETS_PATH, $job->getSignature());
54
        $this->paths[] = $path; // Mark for cleanup later
55
56
        // Test that the job runs
57
        $this->assertFileNotExists($path);
58
        $job->process();
59
        $this->assertFileExists($path);
60
61
        // Test that the output matches the expected
62
        $expected = [
63
            'Title,Content,"Publish On"',
64
            '"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01"',
65
            '"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01"',
66
            '"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"',
67
            '',
68
        ];
69
        $actual = file_get_contents($path);
70
        $this->assertEquals(implode("\r\n", $expected), $actual);
71
    }
72
73
    public function testGenerateExportOverMultipleSteps()
74
    {
75
        Config::modify()->set(GenerateCSVJob::class, 'chunk_size', 1);
76
77
        // Build session
78
        $memberID = $this->logInWithPermission('ADMIN');
79
        $session = ['loggedInAs' => $memberID];
80
81
        // Build controller
82
        $controller = new GenerateCSVJobTestController();
83
        $form = $controller->Form();
84
        /** @var GridField $gridfield */
85
        $gridfield = $form->Fields()->fieldByName('MyGridfield');
86
87
        // Build job
88
        $job = $this->createJob($gridfield, $session);
89
        $path = sprintf('%1$s/.exports/%2$s/%2$s.csv', ASSETS_PATH, $job->getSignature());
90
        $this->paths[] = $path; // Mark for cleanup later
91
92
        // Test that the job runs
93
        $this->assertFileNotExists($path);
94
        while (!$job->isComplete) {
95
            $job->process();
96
        }
97
        $this->assertFileExists($path);
98
99
        // Test that the output matches the expected
100
        $expected = [
101
            'Title,Content,"Publish On"',
102
            '"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01"',
103
            '"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01"',
104
            '"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"',
105
            '',
106
        ];
107
        $actual = file_get_contents($path);
108
        $this->assertEquals(implode("\r\n", $expected), $actual);
109
    }
110
111
    /**
112
     * Rough copy of GridFieldQueuedExportButton::startExport
113
     *
114
     * @param GridField $gridField
115
     * @param array $session
116
     * @return GenerateCSVJob
117
     */
118
    protected function createJob($gridField, $session)
119
    {
120
        $job = new GenerateCSVJob();
121
        $job->setGridField($gridField);
122
        $job->setSession($session);
123
        $job->setSeparator(',');
124
        $job->setIncludeHeader(true);
125
        return $job;
126
    }
127
}
128