You are here

function ContactAttachContactFormsTestCase::submitWithAttachments in Contact Attach 7

Submits contact forms with attachments after setting the settings.

Parameters

int $rid: The role ID of the role.

string $extensions: The extensions that will be set as allowed for the role.

string $uploadsize: The maximum allowed file size that will be set for the role.

object $user: (optional) A fully-loaded $user object of the user to log in. Defaults to NULL.

1 call to ContactAttachContactFormsTestCase::submitWithAttachments()
ContactAttachContactFormsTestCase::testContactAttachContactForms in ./contact_attach.test
Tests the attachment functionality on the site-wide and user contact forms.

File

./contact_attach.test, line 509
Tests for the Contact Attach module.

Class

ContactAttachContactFormsTestCase
Tests the Contact Attach functionality for site-wide and user contact forms.

Code

function submitWithAttachments($rid, $extensions, $uploadsize, $user = NULL) {
  if ($user) {
    $this
      ->drupalLoginWithUserGlobalRolesUpdate($user);
  }
  foreach ($this->contact_forms_short as $contact_form_short => $contact_form_path) {
    if ($contact_form_short === 'site') {
      $contact_form_permission = 'attach files on site-wide contact form';
      $mail_id = 'contact_page_mail';
      $mail_subject_start = '[Website feedback] ';
    }
    elseif ($contact_form_short === 'user') {
      $contact_form_permission = 'attach files on personal contact forms';
      $mail_id = 'contact_user_mail';
      $mail_subject_start = '[Drupal] ';
    }
    $this->extensions[$rid] = $extensions;
    $this->uploadsizes[$rid] = $uploadsize;
    variable_set('contact_attach_extensions_' . $contact_form_short, $this->extensions);
    variable_set('contact_attach_uploadsize_' . $contact_form_short, $this->uploadsizes);
    $contact_attach_numbers = variable_get('contact_attach_number_' . $contact_form_short, array());
    $roles = _contact_attach_get_valid_roles($contact_form_permission, $contact_attach_numbers);
    $allowed_extensions = _contact_attach_return_allowed_extensions($roles, $contact_form_short);
    $file_size_limit = _contact_attach_return_max_file_size($roles, $contact_form_short);
    $this->message['subject'] = 'Test message - ' . $this->file_field_type . ' - role ' . $rid;

    // Ensure that the user can not send messages with attachments when the
    // file depasses the maximum file size and the file does not have an
    // allowed extension.
    $filename = 'testfile1.txt';
    file_put_contents('public://' . $filename, $this
      ->randomString(1433));
    $this->message['files[contact_attach_1]'] = drupal_realpath('public://' . $filename);
    $this
      ->drupalPost($contact_form_path, $this->message, t('Send message'));
    $this
      ->assertNoText(t('Your message has been sent.'));
    $this
      ->assertUniqueText(t('The specified file @filename could not be uploaded.', array(
      '@filename' => $filename,
    )));
    $this
      ->assertUniqueText(t('Only files with the following extensions are allowed: @extensions.', array(
      '@extensions' => $allowed_extensions,
    )));
    $this
      ->assertUniqueText(t('The file is @filesize exceeding the maximum file size of @maxsize.', array(
      '@filesize' => format_size(filesize('public://' . $filename)),
      '@maxsize' => format_size($file_size_limit),
    )));

    // Ensure that a 1 byte file can be attached, but will still be refused
    // because it is not an allowed extension.
    $tinyfile_name = 'tinyfile.txt';
    file_put_contents('public://' . $tinyfile_name, '1');
    $this->message['files[contact_attach_1]'] = drupal_realpath('public://' . $tinyfile_name);
    $this
      ->drupalPost($contact_form_path, $this->message, t('Send message'));
    $this
      ->assertNoText(t('Your message has been sent.'));
    $this
      ->assertUniqueText(t('The specified file @filename could not be uploaded. Only files with the following extensions are allowed: @extensions.', array(
      '@filename' => $tinyfile_name,
      '@extensions' => $allowed_extensions,
    )));

    // Ensure that a file with an allowed extension can be attached, but will
    // still be refused as it depasses the maximum file size.
    $filename = 'testfile2.' . ltrim(substr($extensions, strrpos($extensions, ' ')));
    file_put_contents('public://' . $filename, $this
      ->randomString(1433));
    $this->message['files[contact_attach_1]'] = drupal_realpath('public://' . $filename);
    $this
      ->drupalPost($contact_form_path, $this->message, t('Send message'));
    $this
      ->assertNoText(t('Your message has been sent.'));
    $this
      ->assertUniqueText(t('The specified file @filename could not be uploaded. The file is @filesize exceeding the maximum file size of @maxsize.', array(
      '@filename' => $filename,
      '@filesize' => format_size(filesize('public://' . $filename)),
      '@maxsize' => format_size($file_size_limit),
    )));

    // Ensure that the user can send messages with attachments when the file
    // does not depass the maximum file size and the file has an allowed
    // extension.
    $file = new stdClass();
    $file->filename = 'testfile3-' . $contact_form_short . '.' . ltrim(substr($extensions, strrpos($extensions, ' ')));
    $file->uri = 'temporary://' . $file->filename;

    // Put the submitted file in another location so that file_save_upload()
    // does not rename its saved file because the filename already exists.
    file_put_contents('public://' . $file->filename, '1');
    $file->filemime = file_get_mimetype($file->filename);
    $this->message['files[contact_attach_1]'] = drupal_realpath('public://' . $file->filename);
    $this
      ->drupalPost($contact_form_path, $this->message, t('Send message'));

    // The file has been deleted in _contact_attach_add_attachment() after the
    // file was embedded, so we need to use the copy from now on.
    $file->uri = 'public://' . $file->filename;
    $this
      ->assertUniqueText(t('Your message has been sent.'));
    $this
      ->assertNoText(t('The specified file @filename could not be uploaded.', array(
      '@filename' => $file->filename,
    )));
    $this
      ->assertNoText(t('Only files with the following extensions are allowed: @extensions.', array(
      '@extensions' => $allowed_extensions,
    )));
    $this
      ->assertNoText(t('The file is @filesize exceeding the maximum file size of @maxsize.', array(
      '@filesize' => format_size(filesize($file->uri)),
      '@maxsize' => format_size($file_size_limit),
    )));

    // Verify that the mail was successfully sent and that the attachment is a
    // part of the body.
    $captured_email = $this
      ->drupalGetMails(array(
      'id' => $mail_id,
      'from' => $this->message['mail'],
      'subject' => $mail_subject_start . $this->message['subject'],
    ));
    $this
      ->assertEqual(count($captured_email), 1, 'One mail successfully sent.');
    list(, $boundary_id) = explode('"', $captured_email[0]['headers']['Content-Type']);
    $this
      ->assertEqual(substr_count($captured_email[0]['body'], $boundary_id), 3, 'Boundary ID appears 3 times in the sent message.');
    $attachment = "--{$boundary_id}\n" . _contact_attach_add_attachment($file, array(), FALSE) . "\n\n--{$boundary_id}--\n";

    // chunk_split() uses \r\n as line ending sequence by default, but a mail
    // function removes \r, so we must do the same for the comparison to work.
    $attachment = str_replace("\r\n", "\n", $attachment);
    $this
      ->assertEqual(strstr($captured_email[0]['body'], $attachment), $attachment, 'Attachment exists in the body of the sent message.');
  }
  if ($user) {
    $this
      ->drupalLogoutWithUserGlobalRolesUpdate();
  }
}