Completed
Push — master ( 29242b...64551a )
by Simon
01:44
created

PartialSubmissionJobTest::testFromAddressSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\PartialUserforms\Tests;
4
5
use Firesphere\PartialUserforms\Jobs\PartialSubmissionJob;
6
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\Debug;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\ORM\FieldType\DBDatetime;
13
use SilverStripe\Security\Security;
14
use SilverStripe\SiteConfig\SiteConfig;
15
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
16
use Symbiote\QueuedJobs\Services\QueuedJobService;
17
18
class PartialSubmissionJobTest extends SapphireTest
19
{
20
    protected static $fixture_file = '../fixtures/submission.yml';
21
    /**
22
     * @var PartialSubmissionJob
23
     */
24
    protected $job;
25
26
    public function testGetTitle()
27
    {
28
        $this->assertEquals('Export partial submissions to Email', $this->job->getTitle());
29
    }
30
31
    public function testProcess()
32
    {
33
        $this->assertTrue(method_exists($this->job, 'process'));
34
        $config = SiteConfig::current_site_config();
35
        $config->SendDailyEmail = true;
36
        $config->SendMailTo = '[email protected]';
37
        $config->write();
38
        $this->job->process();
39
40
        $this->assertEmailSent('[email protected]');
41
        $this->assertFileExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv');
42
    }
43
44
    public function testProcessNoMail()
45
    {
46
        $this->assertTrue(method_exists($this->job, 'process'));
47
        $config = SiteConfig::current_site_config();
48
        $config->SendDailyEmail = true;
49
        $config->SendMailTo = '';
50
        Security::setCurrentUser(null);
51
        $config->write();
52
        $this->job->process();
53
54
        $messages = $this->job->getMessages();
55
        foreach ($messages as $message) {
56
            $this->assertContains('Can not process without valid email', $message);
57
        }
58
    }
59
60
    public function testIsSend()
61
    {
62
        $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
63
        $config = SiteConfig::current_site_config();
64
        $config->SendDailyEmail = true;
65
        $config->SendMailTo = '[email protected]';
66
        $config->write();
67
        $this->job->process();
68
69
        $processedSubmission = $submission->get()->byID($submission->ID);
70
71
        $this->assertTrue((bool)$processedSubmission->IsSend);
72
    }
73
74
    public function testIsDeleted()
75
    {
76
        $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
77
        $id = $submission->write();
78
        $config = SiteConfig::current_site_config();
79
        $config->SendDailyEmail = true;
80
        $config->CleanupAfterSend = true;
81
        $config->SendMailTo = '[email protected]';
82
        $config->write();
83
        $this->job->process();
84
        $this->job->afterComplete();
85
86
        $processedSubmission = $submission->get()->byID($id);
87
88
        $this->assertNull($processedSubmission);
89
    }
90
91 View Code Duplication
    public function testFilesRemoved()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        $config = SiteConfig::current_site_config();
94
        $config->SendDailyEmail = true;
95
        $config->SendMailTo = '[email protected]';
96
        $config->write();
97
        $this->job->process();
98
        $this->job->afterComplete();
99
100
        $this->assertFileNotExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv');
101
    }
102
103
    public function testNewJobCreated()
104
    {
105
        $config = SiteConfig::current_site_config();
106
        $config->SendDailyEmail = true;
107
        $config->SendMailTo = '[email protected]';
108
        $config->write();
109
110
        $this->job->process();
111
        $this->job->afterComplete();
112
113
        $jobs = QueuedJobDescriptor::get()->filter([
114
            'Implementation'         => PartialSubmissionJob::class,
115
            'StartAfter:GreaterThan' => DBDatetime::now()->Format(DBDatetime::ISO_DATETIME)
116
        ]);
117
118
        $this->assertEquals(1, $jobs->count());
119
        $this->assertEquals('2018-01-02 00:00:00', $jobs->first()->StartAfter);
120
    }
121
122
    public function testInvalidEmail()
123
    {
124
        $config = SiteConfig::current_site_config();
125
        $config->SendDailyEmail = true;
126
        $config->SendMailTo = '[email protected], error, non-existing, [email protected]';
127
        $config->write();
128
129
        /** @var PartialSubmissionJob $job */
130
        $job = Injector::inst()->get(PartialSubmissionJob::class);
131
132
        $job->process();
133
134
        $emails = $job->getAddresses();
135
136
        $expected = [
137
            '[email protected]',
138
            '[email protected]'
139
        ];
140
        $this->assertEquals($expected, $emails);
141
    }
142
143 View Code Duplication
    public function testCommaSeparatedUsers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        $config = SiteConfig::current_site_config();
146
        $config->SendDailyEmail = true;
147
        $config->SendMailTo = '[email protected], [email protected] , [email protected]';
148
        $config->write();
149
150
        $this->job->process();
151
        $this->assertEmailSent('[email protected]');
152
        $this->assertEmailSent('[email protected]');
153
        $this->assertEmailSent('[email protected]');
154
    }
155
156 View Code Duplication
    public function testFromAddressSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $config = SiteConfig::current_site_config();
159
        $config->SendDailyEmail = true;
160
        $config->SendMailTo = '[email protected]';
161
        $config->SendMailFrom = '[email protected]';
162
        $config->write();
163
164
        $this->job->process();
165
        $this->assertEmailSent('[email protected]', '[email protected]');
166
    }
167
168 View Code Duplication
    public function testFromAddressNotSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170
        $config = SiteConfig::current_site_config();
171
        $config->SendDailyEmail = true;
172
        $config->SendMailTo = '[email protected]';
173
        $config->write();
174
175
        $this->job->process();
176
        $this->assertEmailSent('[email protected]', 'site@' . Director::host());
177
    }
178
179
    protected function setUp()
180
    {
181
        parent::setUp();
182
        $this->usesDatabase = true;
183
        DBDatetime::set_mock_now('2018-01-01 12:00:00');
184
        /** @var PartialSubmissionJob $job */
185
        $this->job = Injector::inst()->get(PartialSubmissionJob::class);
186
        Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false);
187
    }
188
189
    protected function tearDown()
190
    {
191
        parent::tearDown();
192
    }
193
}
194