Completed
Push — master ( 70c3fb...bc6738 )
by Simon
01:48
created

testCommaSeparatedUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

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 7
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\Core\Config\Config;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\FieldType\DBDatetime;
11
use SilverStripe\SiteConfig\SiteConfig;
12
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
13
use Symbiote\QueuedJobs\Services\QueuedJobService;
14
15
class PartialSubmissionJobTest extends SapphireTest
16
{
17
    protected static $fixture_file = '../fixtures/submission.yml';
18
    /**
19
     * @var PartialSubmissionJob
20
     */
21
    protected $job;
22
23
    public function testGetTitle()
24
    {
25
        $this->assertEquals('Export partial submissions to Email', $this->job->getTitle());
26
    }
27
28
    public function testProcess()
29
    {
30
        $this->assertTrue(method_exists($this->job, 'process'));
31
        $config = SiteConfig::current_site_config();
32
        $config->SendDailyEmail = true;
33
        $config->SendMailTo = '[email protected]';
34
        $config->write();
35
        $this->job->process();
36
37
        $this->assertEmailSent('[email protected]');
38
        $this->assertFileExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv');
39
    }
40
41
    public function testProcessNoMail()
42
    {
43
        $this->assertTrue(method_exists($this->job, 'process'));
44
        $config = SiteConfig::current_site_config();
45
        $config->SendDailyEmail = true;
46
        $config->SendMailTo = '';
47
        $config->write();
48
        $this->job->process();
49
50
        $messages = $this->job->getMessages();
51
        foreach ($messages as $message) {
52
            $this->assertContains('Can not process without valid email', $message);
53
        }
54
    }
55
56
    public function testIsSend()
57
    {
58
        $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
59
        $config = SiteConfig::current_site_config();
60
        $config->SendDailyEmail = true;
61
        $config->SendMailTo = '[email protected]';
62
        $config->write();
63
        $this->job->process();
64
65
        $processedSubmission = $submission->get()->byID($submission->ID);
66
67
        $this->assertTrue((bool)$processedSubmission->IsSend);
68
    }
69
70
    public function testIsDeleted()
71
    {
72
        $submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1');
73
        $id = $submission->write();
74
        $config = SiteConfig::current_site_config();
75
        $config->SendDailyEmail = true;
76
        $config->CleanupAfterSend = true;
77
        $config->SendMailTo = '[email protected]';
78
        $config->write();
79
        $this->job->process();
80
        $this->job->afterComplete();
81
82
        $processedSubmission = $submission->get()->byID($id);
83
84
        $this->assertNull($processedSubmission);
85
    }
86
87 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...
88
    {
89
        $config = SiteConfig::current_site_config();
90
        $config->SendDailyEmail = true;
91
        $config->SendMailTo = '[email protected]';
92
        $config->write();
93
        $this->job->process();
94
        $this->job->afterComplete();
95
96
        $this->assertFileNotExists('/tmp/Export of TestForm - 2018-01-01 12:00:00.csv');
97
    }
98
99
    public function testNewJobCreated()
100
    {
101
        $config = SiteConfig::current_site_config();
102
        $config->SendDailyEmail = true;
103
        $config->SendMailTo = '[email protected]';
104
        $config->write();
105
106
        $this->job->process();
107
        $this->job->afterComplete();
108
109
        $jobs = QueuedJobDescriptor::get()->filter([
110
            'Implementation'         => PartialSubmissionJob::class,
111
            'StartAfter:GreaterThan' => DBDatetime::now()->Format(DBDatetime::ISO_DATETIME)
112
        ]);
113
114
        $this->assertEquals(1, $jobs->count());
115
        $this->assertEquals('2018-01-02 00:00:00', $jobs->first()->StartAfter);
116
    }
117
118
    public function testInvalidEmail()
119
    {
120
        $config = SiteConfig::current_site_config();
121
        $config->SendDailyEmail = true;
122
        $config->SendMailTo = '[email protected], error, non-existing, [email protected]';
123
        $config->write();
124
125
        /** @var PartialSubmissionJob $job */
126
        $job = Injector::inst()->get(PartialSubmissionJob::class);
127
128
        $emails = $job->getAddresses();
129
130
        $this->assertArrayNotHasKey('error', $emails);
131
        $this->assertArrayNotHasKey('non-existing', $emails);
132
        $this->assertArrayNotHasKey(' [email protected]', $emails);
133
        $this->assertArrayHasKey('[email protected]', $emails);
134
        $this->assertArrayHasKey('[email protected]', $emails);
135
    }
136
137 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...
138
    {
139
        $config = SiteConfig::current_site_config();
140
        $config->SendDailyEmail = true;
141
        $config->SendMailTo = '[email protected], [email protected] , [email protected]';
142
        $config->write();
143
144
        $this->job->process();
145
        $this->job->afterComplete();
146
147
    }
148
149
    protected function setUp()
150
    {
151
        parent::setUp();
152
        $this->usesDatabase = true;
153
        DBDatetime::set_mock_now('2018-01-01 12:00:00');
154
        /** @var PartialSubmissionJob $job */
155
        $this->job = Injector::inst()->get(PartialSubmissionJob::class);
156
        Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false);
157
    }
158
159
    protected function tearDown()
160
    {
161
        parent::tearDown();
162
    }
163
}
164