WebformSubmissionPurgeApiTest.php in Webform 8.5
File
tests/src/Kernel/WebformSubmissionPurgeApiTest.php
View source
<?php
namespace Drupal\Tests\webform\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\WebformSubmissionStorageInterface;
class WebformSubmissionPurgeApiTest extends KernelTestBase {
public static $modules = [
'system',
'user',
'path',
'path_alias',
'webform_test_handler',
'field',
'webform',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('path_alias');
$this
->installSchema('webform', [
'webform',
]);
$this
->installConfig('webform');
$this
->installEntitySchema('webform_submission');
$this
->installEntitySchema('user');
}
public function testPurgeApis() {
$webform = Webform::create([
'id' => $this
->randomMachineName(),
]);
$webform
->setSetting('purge', WebformSubmissionStorageInterface::PURGE_ALL);
$webform
->setSetting('purge_days', 14);
$webform
->addWebformHandler(\Drupal::service('plugin.manager.webform.handler')
->createInstance('test_purge'));
$webform
->save();
$submission_ids = [];
for ($i = 0; $i < 10; $i++) {
$webform_submission = WebformSubmission::create([
'webform_id' => $webform
->id(),
]);
$webform_submission->in_draft = FALSE;
$webform_submission
->setCreatedTime(time() - 15 * 86400);
$webform_submission
->save();
$submission_ids[$webform_submission
->id()] = $webform_submission
->id();
}
\Drupal::entityTypeManager()
->getStorage('webform_submission')
->purge(10);
$this
->assertEquals($submission_ids, \Drupal::state()
->get('webform_test_purge_handler_pre'));
$this
->assertEquals($submission_ids, \Drupal::state()
->get('webform_test_purge_handler_post'));
$this
->assertEquals($submission_ids, \Drupal::state()
->get('webform_test_purge_hook_pre'));
$this
->assertEquals($submission_ids, \Drupal::state()
->get('webform_test_purge_hook_post'));
}
}