View source
<?php
namespace Drupal\Tests\webform_scheduled_email\Functional;
use Drupal\Tests\webform_node\Functional\WebformNodeBrowserTestBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformScheduledEmailTest extends WebformNodeBrowserTestBase {
public static $modules = [
'webform',
'webform_scheduled_email',
'webform_scheduled_email_test',
'webform_node',
];
public function testWebformScheduledEmail() {
$webform_schedule = Webform::load('test_handler_scheduled_email');
$scheduled_manager = \Drupal::service('webform_scheduled_email.manager');
$yesterday = date($scheduled_manager
->getDateFormat(), strtotime('-1 days'));
$tomorrow = date($scheduled_manager
->getDateFormat(), strtotime('+1 days'));
$sid = $this
->postSubmission($webform_schedule, [
'send' => 'yesterday',
]);
$webform_submission = WebformSubmission::load($sid);
$this
->assertText("Test: Handler: Test scheduled email: Submission #{$sid}: Email scheduled by Yesterday handler to be sent on {$yesterday}.");
$scheduled_email = $scheduled_manager
->load($webform_submission, 'yesterday');
$this
->assertEqual($scheduled_email->send, strtotime($yesterday));
$this
->assertEqual($scheduled_email->state, $scheduled_manager::SUBMISSION_SEND);
$scheduled_manager
->cron();
$scheduled_email = $scheduled_manager
->load($webform_submission, 'yesterday');
$this
->assertFalse($scheduled_email);
$sid = $this
->postSubmission($webform_schedule, [
'send' => 'other',
'date[date]' => '2001-01-01',
], 'Save Draft');
$webform_submission = WebformSubmission::load($sid);
$scheduled_email = $scheduled_manager
->load($webform_submission, 'other');
$this
->assertText("Test: Handler: Test scheduled email: Submission #{$sid}: Email scheduled by Other handler to be sent on 2001-01-15.");
$this
->assertEqual($scheduled_email->send, strtotime('2001-01-15'));
$this
->assertEqual($scheduled_email->state, $scheduled_manager::SUBMISSION_SEND);
$this
->postSubmission($webform_schedule, [
'send' => 'other',
'date[date]' => '2002-02-02',
], 'Save Draft');
$scheduled_email = $scheduled_manager
->load($webform_submission, 'other');
$this
->assertText("Test: Handler: Test scheduled email: Submission #{$sid}: Email rescheduled by Other handler to be sent on 2002-02-16.");
$this
->assertEqual($scheduled_email->send, strtotime('2002-02-16'));
$this
->assertEqual($scheduled_email->state, $scheduled_manager::SUBMISSION_SEND);
$webform_submission
->save();
$scheduled_email = $scheduled_manager
->load($webform_submission, 'other');
$this
->assertEqual($scheduled_email->state, $scheduled_manager::SUBMISSION_SEND);
$webform_submission
->delete();
$this
->assertEqual($scheduled_manager
->total(), 0);
$draft_reminder = date($scheduled_manager
->getDateFormat(), strtotime('+14 days'));
$sid = $this
->postSubmission($webform_schedule, [
'send' => 'draft_reminder',
], 'Save Draft');
$this
->assertText("Test: Handler: Test scheduled email: Submission #{$sid}: Email scheduled by Draft reminder handler to be sent on {$draft_reminder}.");
$this
->assertEqual($scheduled_manager
->total(), 1);
$this
->postSubmission($webform_schedule, []);
$this
->assertText("Test: Handler: Test scheduled email: Submission #{$sid}: Email unscheduled for Draft reminder handler.");
$this
->assertEqual($scheduled_manager
->total(), 0);
$sid = $this
->postSubmission($webform_schedule, [
'send' => 'broken',
]);
$this
->assertText("Test: Handler: Test scheduled email: Submission #{$sid}: Email not scheduled for Broken handler because [broken] is not a valid date/token.");
$this
->assertEqual($scheduled_manager
->total($webform_schedule), 0);
\Drupal::configFactory()
->getEditable('webform_scheduled_email.settings')
->set('schedule_type', 'datetime')
->save();
$sid = $this
->postSubmission($webform_schedule, [
'send' => 'other',
'date[date]' => '2001-01-01',
'date[time]' => '02:00:00',
], 'Save Draft');
$webform_submission = WebformSubmission::load($sid);
$scheduled_email = $scheduled_manager
->load($webform_submission, 'other');
$this
->assertText("Test: Handler: Test scheduled email: Submission #{$sid}: Email scheduled by Other handler to be sent on 2001-01-15 02:00:00.");
$this
->assertEqual($scheduled_email->send, strtotime('2001-01-15 02:00:00'));
$this
->assertEqual($scheduled_email->state, $scheduled_manager::SUBMISSION_SEND);
\Drupal::configFactory()
->getEditable('webform_scheduled_email.settings')
->set('schedule_type', 'date')
->save();
$this
->purgeSubmissions();
$this
->postSubmission($webform_schedule, [
'send' => 'tomorrow',
]);
$this
->postSubmission($webform_schedule, [
'send' => 'tomorrow',
]);
$this
->postSubmission($webform_schedule, [
'send' => 'tomorrow',
]);
$this
->assertEqual($scheduled_manager
->total($webform_schedule), 3);
$this
->postSubmission($webform_schedule, [
'send' => 'yesterday',
]);
$this
->postSubmission($webform_schedule, [
'send' => 'yesterday',
]);
$this
->postSubmission($webform_schedule, [
'send' => 'yesterday',
]);
$this
->assertEqual($scheduled_manager
->total($webform_schedule), 6);
$stats = $scheduled_manager
->cron();
$this
->assertEqual($stats['sent'], 3);
$this
->assertEqual($scheduled_manager
->total($webform_schedule), 3);
$scheduled_manager
->schedule($webform_schedule, 'yesterday');
$this
->assertEqual($scheduled_manager
->stats($webform_schedule), [
'total' => 9,
'waiting' => 6,
'queued' => 3,
'ready' => 0,
]);
$stats = $scheduled_manager
->cron();
$this
->assertNotEqual($stats['sent'], 6);
$this
->assertEqual($stats['sent'], 3);
$this
->assertEqual($scheduled_manager
->stats($webform_schedule), [
'total' => 3,
'waiting' => 0,
'queued' => 3,
'ready' => 0,
]);
$scheduled_manager
->schedule($webform_schedule, 'tomorrow');
$this
->assertEqual($scheduled_manager
->total($webform_schedule), 6);
$this
->assertEqual($scheduled_manager
->waiting($webform_schedule), 6);
$this
->assertEqual($scheduled_manager
->ready($webform_schedule), 0);
$this
->purgeSubmissions();
$this
->postSubmission($webform_schedule, [
'send' => 'yesterday',
]);
$this
->postSubmission($webform_schedule, [
'send' => 'yesterday',
]);
$this
->postSubmission($webform_schedule, [
'send' => 'yesterday',
]);
$this
->assertEqual($scheduled_manager
->total($webform_schedule), 3);
$this
->assertEqual($scheduled_manager
->stats(), [
'total' => 3,
'waiting' => 0,
'queued' => 0,
'ready' => 3,
]);
$yesterday_handler = $webform_schedule
->getHandler('yesterday');
$conditions = [
'enabled' => [
':input[name="value"]' => [
'filled' => TRUE,
],
],
];
$yesterday_handler
->setConditions($conditions);
$stats = $scheduled_manager
->cron();
$this
->assertEqual($stats['skipped'], 3);
$this
->assertEqual($scheduled_manager
->stats(), [
'total' => 0,
'waiting' => 0,
'queued' => 0,
'ready' => 0,
]);
$yesterday_handler
->setConditions([]);
$this
->purgeSubmissions();
$sid = $this
->postSubmission($webform_schedule, [
'send' => 'last_year',
]);
$this
->assertEqual($scheduled_manager
->total($webform_schedule), 0);
$this
->assertRaw('<em class="placeholder">Test: Handler: Test scheduled email: Submission #' . $sid . '</em>: Email <b>ignored</b> by <em class="placeholder">Last year</em> handler to be sent on <em class="placeholder">2016-01-01</em>.');
$this
->purgeSubmissions();
$webform_node = $this
->createWebformNode($webform_schedule
->id());
$sids = [
$this
->postNodeSubmission($webform_node, [
'send' => 'tomorrow',
]),
$this
->postNodeSubmission($webform_node, [
'send' => 'tomorrow',
]),
$this
->postNodeSubmission($webform_node, [
'send' => 'tomorrow',
]),
];
$this
->assertEqual($scheduled_manager
->total(), 3);
$sid = $sids[0];
$webform_submission = WebformSubmission::load($sid);
$scheduled_email = $scheduled_manager
->load($webform_submission, 'tomorrow');
$this
->assertEqual($scheduled_manager
->stats(), [
'total' => 3,
'waiting' => 0,
'queued' => 3,
'ready' => 0,
]);
$this
->assertEqual($scheduled_manager
->stats($webform_node), [
'total' => 3,
'waiting' => 0,
'queued' => 3,
'ready' => 0,
]);
$this
->assertEqual($scheduled_email->send, strtotime($tomorrow));
$this
->assertEqual($scheduled_email->state, $scheduled_manager::SUBMISSION_SEND);
$webform_node
->save();
$this
->assertEqual($scheduled_manager
->stats(), [
'total' => 3,
'waiting' => 3,
'queued' => 0,
'ready' => 0,
]);
$this
->assertEqual($scheduled_manager
->stats($webform_node), [
'total' => 3,
'waiting' => 3,
'queued' => 0,
'ready' => 0,
]);
$scheduled_email = $scheduled_manager
->load($webform_submission, 'tomorrow');
$this
->assertEqual($scheduled_email->state, $scheduled_manager::SUBMISSION_RESCHEDULE);
$scheduled_manager
->cron();
$this
->assertEqual($scheduled_manager
->stats(), [
'total' => 3,
'waiting' => 0,
'queued' => 3,
'ready' => 0,
]);
$this
->assertEqual($scheduled_manager
->stats($webform_node), [
'total' => 3,
'waiting' => 0,
'queued' => 3,
'ready' => 0,
]);
$webform_node
->delete();
$this
->assertEqual($scheduled_manager
->stats(), [
'total' => 3,
'waiting' => 3,
'queued' => 0,
'ready' => 0,
]);
$scheduled_manager
->cron();
$this
->assertEqual($scheduled_manager
->total(), 0);
$this
->purgeSubmissions();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/webform/test_handler_scheduled_email/test');
$this
->assertRaw('The <em class="placeholder">Other</em> email will be sent immediately upon submission.');
$this
->drupalPostForm('/webform/test_handler_scheduled_email/test', [
'send' => 'other',
'date[date]' => '2101-01-01',
], 'Submit');
$this
->assertEqual($scheduled_manager
->total(), 0);
$this
->assertRaw('Webform submission from: Test: Handler: Test scheduled email</em> sent to <em class="placeholder">simpletest@example.com</em> from <em class="placeholder">Drupal</em> [<em class="placeholder">simpletest@example.com</em>');
$this
->assertRaw('Debug: Email: Other');
}
protected function purgeSubmissions() {
$webform_submissions = WebformSubmission::loadMultiple();
foreach ($webform_submissions as $webform_submission) {
$webform_submission
->delete();
}
$scheduled_manager = \Drupal::service('webform_scheduled_email.manager');
$this
->assertEqual($scheduled_manager
->total(), 0);
}
}