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\Debug; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
12
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
public function testIsDeleted() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$submission = $this->objFromFixture(PartialFormSubmission::class, 'submission1'); |
73
|
|
|
$config = SiteConfig::current_site_config(); |
74
|
|
|
$config->SendDailyEmail = true; |
75
|
|
|
$config->CleanupAfterSend = true; |
76
|
|
|
$config->SendMailTo = '[email protected]'; |
77
|
|
|
$config->write(); |
78
|
|
|
$this->job->process(); |
79
|
|
|
$this->job->afterComplete(); |
80
|
|
|
|
81
|
|
|
$processedSubmission = $submission->get()->byID($submission->ID); |
82
|
|
|
|
83
|
|
|
$this->assertNull($processedSubmission); |
84
|
|
|
} |
85
|
|
|
// |
|
|
|
|
86
|
|
|
// public function testNewJobCreated() |
87
|
|
|
// { |
88
|
|
|
// $config = SiteConfig::current_site_config(); |
89
|
|
|
// $config->SendDailyEmail = true; |
90
|
|
|
// $config->CleanupAfterSend = true; |
91
|
|
|
// $config->SendMailTo = '[email protected]'; |
92
|
|
|
// $config->write(); |
93
|
|
|
// $this->job->process(); |
94
|
|
|
// $this->job->afterComplete(); |
95
|
|
|
// } |
96
|
|
|
|
97
|
|
|
protected function setUp() |
98
|
|
|
{ |
99
|
|
|
$this->usesDatabase = true; |
100
|
|
|
Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false); |
101
|
|
|
DBDatetime::set_mock_now('2018-01-01 12:00:00'); |
102
|
|
|
/** @var PartialSubmissionJob $job */ |
103
|
|
|
$this->job = Injector::inst()->get(PartialSubmissionJob::class); |
104
|
|
|
|
105
|
|
|
return parent::setUp(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function tearDown() |
109
|
|
|
{ |
110
|
|
|
parent::tearDown(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.