GenerateCSVJobTest::testGenerateExport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
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()
26
            ->merge(Director::class, 'rules', [
27
                'jobtest//$Action/$ID/$OtherID' => GenerateCSVJobTestController::class
28
            ])
29
            ->set(GenerateCSVJob::class, 'sync_sleep_seconds', 0);
30
    }
31
32
    protected $paths = [];
33
34
    protected function tearDown()
35
    {
36
        foreach ($this->paths as $path) {
37
            Filesystem::removeFolder(dirname($path));
38
        }
39
        parent::tearDown();
40
    }
41
42
    public function testGenerateExport()
43
    {
44
        // Build session
45
        $memberID = $this->logInWithPermission('ADMIN');
46
        $session = ['loggedInAs' => $memberID];
47
48
        // Build controller
49
        $controller = new GenerateCSVJobTestController();
50
        $form = $controller->Form();
51
        $gridfield = $form->Fields()->fieldByName('MyGridfield');
52
53
        // Build job
54
        $job = $this->createJob($gridfield, $session);
55
        $path = sprintf('%1$s/.exports/%2$s/%2$s.csv', ASSETS_PATH, $job->getSignature());
56
        $this->paths[] = $path; // Mark for cleanup later
57
58
        // Test that the job runs
59
        $this->assertFileNotExists($path);
60
        $job->setup();
61
        $job->process();
62
        $job->afterComplete();
63
        $this->assertFileExists($path);
64
65
        // Test that the output matches the expected
66
        $expected = [
67
            '"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01"',
68
            '"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01"',
69
            '"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"',
70
            '',
71
        ];
72
        $actual = file_get_contents($path);
73
        // Note: strtolower() is for case insensitive comparison, since field label casing changed in SS 4.3
74
        $this->assertContains('title,content,"publish on"', strtolower($actual));
75
        $this->assertContains(implode("\r\n", $expected), $actual);
76
    }
77
78
    public function testGenerateExportOverMultipleSteps()
79
    {
80
        Config::modify()->set(GenerateCSVJob::class, 'chunk_size', 1);
81
82
        // Build session
83
        $memberID = $this->logInWithPermission('ADMIN');
84
        $session = ['loggedInAs' => $memberID];
85
86
        // Build controller
87
        $controller = new GenerateCSVJobTestController();
88
        $form = $controller->Form();
89
        /** @var GridField $gridfield */
90
        $gridfield = $form->Fields()->fieldByName('MyGridfield');
91
92
        // Build job
93
        $job = $this->createJob($gridfield, $session);
94
        $path = sprintf('%1$s/.exports/%2$s/%2$s.csv', ASSETS_PATH, $job->getSignature());
95
        $this->paths[] = $path; // Mark for cleanup later
96
97
        // Test that the job runs
98
        $this->assertFileNotExists($path);
99
        $count = 0;
100
        while (!$job->jobFinished()) {
101
            ++$count;
102
            if ($job->currentStep) {
103
                $job->prepareForRestart();
104
            } else {
105
                $job->setup();
106
            }
107
            $job->process();
108
        }
109
        $job->afterComplete();
110
        $this->assertFileExists($path);
111
        $this->assertEquals(3, $count);
112
113
        // Test that the output matches the expected
114
        $expected = [
115
            '"Record 1","<p>""Record 1"" Body</p>","2015-01-01 23:34:01"',
116
            '"Record 2","<p>""Record 2"" Body</p>","2015-01-02 23:34:01"',
117
            '"Record 3","<p>""Record 3"" Body</p>","2015-01-03 23:34:01"',
118
            '',
119
        ];
120
        $actual = file_get_contents($path);
121
        // Note: strtolower() is for case insensitive comparison, since field label casing changed in SS 4.3
122
        $this->assertContains('title,content,"publish on"', strtolower($actual));
123
        $this->assertContains(implode("\r\n", $expected), $actual);
124
    }
125
126
    /**
127
     * Rough copy of GridFieldQueuedExportButton::startExport
128
     *
129
     * @param GridField $gridField
130
     * @param array $session
131
     * @return GenerateCSVJob
132
     */
133
    protected function createJob($gridField, $session)
134
    {
135
        $job = new GenerateCSVJob();
136
        $job->setGridField($gridField);
137
        $job->setSession($session);
138
        $job->setSeparator(',');
139
        $job->setIncludeHeader(true);
140
        return $job;
141
    }
142
}
143