You are here

public function WebformSubmissionPurgeApiTest::testPurgeApis in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Kernel/WebformSubmissionPurgeApiTest.php \Drupal\Tests\webform\Kernel\WebformSubmissionPurgeApiTest::testPurgeApis()

Tests webform handler prePurge and postPurge methods and associated hooks.

File

tests/src/Kernel/WebformSubmissionPurgeApiTest.php, line 47

Class

WebformSubmissionPurgeApiTest
Defines a class for testing webform submission purge APIs.

Namespace

Drupal\Tests\webform\Kernel

Code

public function testPurgeApis() {

  /** @var \Drupal\webform\WebformInterface $webform */
  $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;

    // 15 days ago.
    $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'));
}