You are here

function WebformClearSubmissionTestCase::testWebformClear in Webform Clear 7.2

Test submissions with different webform clear options.

File

./webform_clear.test, line 41

Class

WebformClearSubmissionTestCase

Code

function testWebformClear() {

  // Log in as admin user.
  $this
    ->drupalLogin($this->webform_users['admin']);

  // Do not delete. Check that node still exists before and after cron.
  variable_set('webform_clear_default_time', WEBFORM_CLEAR_DO_NOT_DELETE);
  $this
    ->webformReset();
  list($submission_exists_before, $submission_exists_after) = $this
    ->webformSubmissionExecute(0);
  $this
    ->assertTrue($submission_exists_before, 'Submission exists before cron (do not delete)');
  $this
    ->assertTrue($submission_exists_after, 'Submission exists after cron (do not delete)');

  // Set up immediate clear. Check that node doesn't exist before or after
  // cron.
  variable_set('webform_clear_default_time', 0);
  $this
    ->webformReset();
  list($submission_exists_before, $submission_exists_after) = $this
    ->webformSubmissionExecute(0);
  $this
    ->assertFalse($submission_exists_before, 'Submission does not exist before cron (delete immediately)');
  $this
    ->assertFalse($submission_exists_after, 'Submission does not exist after cron (delete immediately)');

  // Override the deletion by editing webform options:
  // Give admin user the right permission to override default clear times.
  // Reset cached permissions.
  $this
    ->checkPermissions(array(
    'set up webform_clear times',
  ), TRUE);
  $role = $this
    ->drupalCreateRole(array(
    'set up webform_clear times',
  ));
  $this->webform_users['admin']->roles[$role] = $role;
  user_save($this->webform_users['admin']);

  // Override "immediate clear" default value with "Do not delete".
  $node = $this
    ->testWebformForm();
  list($submission_exists_before, $submission_exists_after) = $this
    ->webformSubmissionExecute(0, WEBFORM_CLEAR_DO_NOT_DELETE);
  $this
    ->assertTrue($submission_exists_before, 'Submission exists before cron (do not delete overriding default)');
  $this
    ->assertTrue($submission_exists_after, 'Submission exists after cron (do not delete overriding default');

  // Make sure we keep the custom option upon consequent edit.
  list($submission_exists_before, $submission_exists_after) = $this
    ->webformSubmissionExecute(0);
  $this
    ->assertTrue($submission_exists_before, 'Submission still exists before cron (do not delete overriding default, second edit)');
  $this
    ->assertTrue($submission_exists_after, 'Submission still exists before cron (do not delete overriding default, second edit)');

  // Remove clear times permission from the admin user.
  unset($this->webform_users['admin']->roles[$role]);
  user_save($this->webform_users['admin']);

  // Make sure we still have the custom option.
  list($submission_exists_before, $submission_exists_after) = $this
    ->webformSubmissionExecute(0);
  $this
    ->assertTrue($submission_exists_before, 'Submission still exists before cron (do not delete overriding default, no admin)');
  $this
    ->assertTrue($submission_exists_after, 'Submission still exists before cron (do not delete overriding default, no admin)');

  // Set up clear after 1 day. Check that node exists upon submission but
  // not after 1 day.
  $this
    ->webformReset();
  variable_set('webform_clear_default_time', 1 * 24 * 60 * 60);
  list($submission_exists_before, $submission_exists_after) = $this
    ->webformSubmissionExecute(1 * 24 * 60 * 60);
  $this
    ->assertTrue($submission_exists_before, 'Submission exists before cron (1 day)');
  $this
    ->assertFalse($submission_exists_after, 'Submission does not exist after cron (1 day)');

  // Check that the submission is not deleted before the 1 day has passed.
  $this
    ->webformReset();
  variable_set('webform_clear_default_time', 1 * 24 * 60 * 60);
  list($submission_exists_before, $submission_exists_after) = $this
    ->webformSubmissionExecute(60);
  $this
    ->assertTrue($submission_exists_before, 'Submission exists before cron (1 day)');
  $this
    ->assertTrue($submission_exists_after, 'Submission exists after cron (1 day, cron at 1 hour)');
}