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

testGenerateExportOverMultipleSteps()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 43
rs 8.8571
cc 3
eloc 27
nc 3
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->jobFinished()) {
95
            if ($job->currentStep) {
96
                $job->prepareForRestart();
97
            } else {
98
                $job->setup();
99
            }
100
            $job->process();
101
        }
102
        $job->afterComplete();
103
        $this->assertFileExists($path);
104
105
        // Test that the output matches the expected
106
        $expected = [
107
            'Title,Content,"Publish On"',
108
            '"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01"',
109
            '"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01"',
110
            '"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"',
111
            '',
112
        ];
113
        $actual = file_get_contents($path);
114
        $this->assertEquals(implode("\r\n", $expected), $actual);
115
    }
116
117
    /**
118
     * Rough copy of GridFieldQueuedExportButton::startExport
119
     *
120
     * @param GridField $gridField
121
     * @param array $session
122
     * @return GenerateCSVJob
123
     */
124
    protected function createJob($gridField, $session)
125
    {
126
        $job = new GenerateCSVJob();
127
        $job->setGridField($gridField);
128
        $job->setSession($session);
129
        $job->setSeparator(',');
130
        $job->setIncludeHeader(true);
131
        return $job;
132
    }
133
}
134