BmTestEmail.test 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Test email delivery.
  5. */
  6. /**
  7. * Test email delivery.
  8. */
  9. class BmTestEmail extends BmTestBase {
  10. /**
  11. * Define this test class.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Email handling',
  16. 'description' => 'Confirm email handling works as intended.',
  17. 'group' => 'backup_migrate',
  18. );
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function setUp(array $modules = array()) {
  24. parent::setUp($modules);
  25. // Log in as user 1, so that permissions are irrelevant.
  26. $this->loginUser1();
  27. }
  28. /**
  29. * Confirm the whole email process.
  30. */
  31. public function testAddEmailDestination() {
  32. // See if we can add a destination.
  33. // Load the email destination Add page.
  34. $this->drupalGet(BACKUP_MIGRATE_MENU_PATH . '/settings/destination/add/email');
  35. $this->assertResponse(200);
  36. // Verify all of the expected fields exist.
  37. $this->assertFieldByName('name');
  38. $this->assertFieldByName('name', 'Untitled Destination');
  39. $this->assertFieldByName('machine_name');
  40. $this->assertFieldByName('location');
  41. // Set up test values.
  42. $test_source = strtolower($this->randomName(16));
  43. $address = strtolower($this->randomName(10)) . '@example.com';
  44. $test_destination = strtolower($this->randomName(16));
  45. // Submit e-mail destination form with invalid values.
  46. $this->submitDestinationEmail('', $test_destination, $address);
  47. $this->assertText(t('Destination name field is required.'), 'Name required.');
  48. $this->submitDestinationEmail($this->randomString(16), '', $address);
  49. $this->assertText(t('Machine-readable name field is required.'), 'Machine name required.');
  50. $this->submitDestinationEmail($this->randomString(16), $test_destination, '');
  51. $this->assertText(t('Email Address field is required.'), 'E-mail required.');
  52. // Step 1, set-up.
  53. // Create a test destination.
  54. $this->submitDestinationEmail($this->randomString(16), $test_destination, $address);
  55. $this->assertText(t('Your destination was saved'));
  56. // Create a test source.
  57. // @todo Test the source add form.
  58. // @todo Maybe add a note somewhere about how we are using a small
  59. // source on purpose, so as to not break the testing system.
  60. $this->submitSourceFiles($this->randomString(16), $test_source, drupal_get_path('module', 'backup_migrate') . '/tests/files/');
  61. $this->assertText(t('Your source was saved'));
  62. // Run a manual back-up so we have a file to compare against.
  63. $this->runBackup('manual', $test_source);
  64. $files = $this->listBackupFiles();
  65. $key = key($files);
  66. // Confirm the back-up file exists.
  67. $this->assertNotEqual($key, NULL, 'A backup file was found.');
  68. $file = $files[$key];
  69. // Copied from destinations.email.inc to build a compatible parameter.
  70. $attachment = new stdClass();
  71. $attachment->filename = $file->name;
  72. $attachment->path = $file->path;
  73. // Step 2, e-mail generated through the Quick Backup form.
  74. // Run a backup per e-mail.
  75. $this->runBackup($test_destination, $test_source);
  76. // Confirm that an e-mail was sent.
  77. $captured_emails = $this->drupalGetMails(array('id' => 'backup_migrate_destination_mail', 'to' => $address));
  78. $this->verboseEmail();
  79. $this->assertEqual(count($captured_emails), 1, 'A back-up was mailed.');
  80. // Does the attachment occur in the captured e-mail?
  81. // Note that the e-mail from Step 2 is tested against the back-up file from
  82. // Step 1.
  83. $file_data = fread(fopen($attachment->path, "r"), filesize($attachment->path));
  84. $encoded_file_data = chunk_split(base64_encode($file_data), 70, "\r\n");
  85. $this->assertMailString('body', $encoded_file_data, 1);
  86. // Step 3, e-mail generated through the main helper function.
  87. // Make sure the new $address is never the same as the old one.
  88. $address = strtolower($this->randomName(1 + strlen($address))) . '@example.com';
  89. _backup_migrate_destination_email_mail_backup($attachment, $address);
  90. // Confirm that an e-mail is sent.
  91. $captured_emails = $this->drupalGetMails(array('id' => 'backup_migrate_destination_mail', 'to' => $address));
  92. $this->verboseEmail();
  93. $this->assertEqual(count($captured_emails), 1, 'A back-up was mailed.');
  94. // Check if the Subject field contains the filename.
  95. $this->assertMailString('subject', $attachment->filename, 1);
  96. // Check if the body contains the filename.
  97. $this->assertMailString('body', $attachment->filename, 1);
  98. // Step 4, (partial) e-mail generated through a mime_mail() object.
  99. // Build an e-mail.
  100. $test_mail = new mime_mail();
  101. $boundary = "b" . md5(uniqid(time()));
  102. // Make sure the new $address is never the same as the old one.
  103. $address = strtolower($this->randomName(1 + strlen($address))) . '@example.com';
  104. $attach = fread(fopen($attachment->path, "r"), filesize($attachment->path));
  105. $test_mail->add_attachment($attach, $attachment->filename, "application/octet-stream");
  106. $message = $test_mail->build_multipart($boundary);
  107. $params = array(
  108. 'body' => $message,
  109. 'subject' => 'test',
  110. 'headers' => array(
  111. 'MIME-Version' => "1.0",
  112. 'Content-Type' => "multipart/mixed; boundary=\"$boundary\"",
  113. ),
  114. );
  115. drupal_mail('backup_migrate', 'destination_mail', $address, '', $params);
  116. $captured_emails = $this->drupalGetMails(array('id' => 'backup_migrate_destination_mail', 'to' => $address));
  117. $this->verboseEmail();
  118. // Confirm that an e-mail was sent.
  119. $this->assertEqual(count($captured_emails), 1, 'A back-up was mailed.');
  120. // Does the boundary occur in the captured e-mail?
  121. $this->assertMailString('body', '--' . $boundary, 1);
  122. // How many boundaries?
  123. $expected = count($test_mail->parts) + 1;
  124. $this->assertMailStringCount('body', '--' . $boundary, $expected, 1);
  125. // Cleanup - purge all backups.
  126. $this->deleteBackups();
  127. }
  128. /**
  129. * Submits the destination form for E-mails.
  130. *
  131. * @param string $name
  132. * The name of the destination.
  133. * @param string $machine_name
  134. * The machine name of the destination.
  135. * @param string $mail
  136. * The e-mail address of the destination.
  137. */
  138. public function submitDestinationEmail($name, $machine_name, $mail) {
  139. $this->drupalGet('admin/config/system/backup_migrate/settings/destination/add/email');
  140. $this->assertResponse(200);
  141. $edit = array();
  142. $edit['name'] = $name;
  143. $edit['machine_name'] = $machine_name;
  144. $edit['location'] = $mail;
  145. $this->drupalPost(NULL, $edit, t('Save destination'));
  146. $this->assertResponse(200);
  147. }
  148. /**
  149. * Submits the source form for Files.
  150. *
  151. * @param string $name
  152. * The name of the source.
  153. * @param string $machine_name
  154. * The machine name of the source.
  155. * @param string $path
  156. * The path of the source.
  157. */
  158. public function submitSourceFiles($name, $machine_name, $path) {
  159. $this->drupalGet('admin/config/system/backup_migrate/settings/source/add/filesource');
  160. $this->assertResponse(200);
  161. $edit = array();
  162. $edit['name'] = $name;
  163. $edit['machine_name'] = $machine_name;
  164. $edit['location'] = $path;
  165. $this->drupalPost(NULL, $edit, t('Save source'));
  166. $this->assertResponse(200);
  167. }
  168. }