BmTestBase.test 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * @file
  4. * Shared functionality to make the rest of the tests simpler.
  5. */
  6. /**
  7. * Base class for testing a module's custom tags.
  8. */
  9. abstract class BmTestBase extends DrupalWebTestCase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function setUp(array $modules = array()) {
  14. $modules[] = 'backup_migrate';
  15. parent::setUp($modules);
  16. }
  17. /**
  18. * Log in as user 1.
  19. *
  20. * The benefit of doing this is that it ignores permissions entirely, so the
  21. * raw functionality can be tested.
  22. */
  23. protected function loginUser1() {
  24. // Load user 1.
  25. $account = user_load(1, TRUE);
  26. // Reset the password.
  27. $password = user_password();
  28. $edit = array(
  29. 'pass' => $password,
  30. );
  31. user_save($account, $edit);
  32. $account->pass_raw = $password;
  33. // Login.
  34. $this->drupalLogin($account);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function verbose($message, $title = NULL) {
  40. // Handle arrays, objects, etc.
  41. if (!is_string($message)) {
  42. $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
  43. }
  44. // Optional title to go before the output.
  45. if (!empty($title)) {
  46. $title = '<h2>' . check_plain($title) . "</h2>\n";
  47. }
  48. parent::verbose($title . $message);
  49. }
  50. /**
  51. * Confirm that a selector has the expected items.
  52. */
  53. protected function assertSelectOptions($select_id, array $options, $message = '') {
  54. $elements = $this
  55. ->xpath('//select[@id=:id]//option', array(
  56. ':id' => $select_id,
  57. ));
  58. $results = $this->assertEqual(count($elements), count($options), t('The same number of items were found as were requested'));
  59. $this->verbose($elements);
  60. foreach ($options as $option) {
  61. $elements = $this
  62. ->xpath('//select[@id=:id]//option[@value=:option]', array(
  63. ':id' => $select_id,
  64. ':option' => $option,
  65. ));
  66. $this->verbose($elements);
  67. $results *= $this->assertTrue(isset($elements[0]), $message ? $message : t('Option @option for field @id is present.', array(
  68. '@option' => $option,
  69. '@id' => $select_id,
  70. )), t('Browser'));
  71. }
  72. return $results;
  73. }
  74. /**
  75. * Confirm that a specific selector does not have items selected.
  76. */
  77. protected function assertNoOptionsSelected($id, $message = '') {
  78. $elements = $this
  79. ->xpath('//select[@id=:id]//option[@selected="selected"]', array(
  80. ':id' => $id,
  81. ));
  82. return $this
  83. ->assertTrue(!isset($elements[0]), $message ? $message : t('Field @id does not have any selected items.', array(
  84. '@id' => $id,
  85. )), t('Browser'));
  86. }
  87. /**
  88. * Work out which compressor systems are supported by PHP.
  89. *
  90. * @return array
  91. * The list of supported compressors. Will always include the item 'none'.
  92. */
  93. protected function supportedCompressors() {
  94. $items = array('none');
  95. // Work out which systems are supported.
  96. if (@function_exists("gzencode")) {
  97. $items[] = 'gzip';
  98. }
  99. if (@function_exists("bzcompress")) {
  100. $items[] = 'bzip';
  101. }
  102. if (class_exists('ZipArchive')) {
  103. $items[] = 'zip';
  104. }
  105. return $items;
  106. }
  107. /**
  108. * Get a list of the files in a specific destination.
  109. *
  110. * @param string $destination_id
  111. * The ID of the destination to check. Defaults to the manual file path.
  112. *
  113. * @return array
  114. * The backup files found in the requested backup destination.
  115. */
  116. protected function listBackupFiles($destination_id = 'manual') {
  117. require_once dirname(__FILE__) . '/../includes/destinations.inc';
  118. $items = array();
  119. // Load the destination object.
  120. $destination = backup_migrate_get_destination($destination_id);
  121. if (!empty($destination)) {
  122. $items = $destination->list_files();
  123. }
  124. return $items;
  125. }
  126. /**
  127. * Run a specific backup.
  128. *
  129. * @param string $destination_id
  130. * The ID of the destination to check. Defaults to the manual file path.
  131. * @param string $source_id
  132. * The ID of the source to check. Defaults to the database.
  133. */
  134. protected function runBackup($destination_id = 'manual', $source_id = 'db') {
  135. $this->drupalGet(BACKUP_MIGRATE_MENU_PATH);
  136. $this->assertResponse(200);
  137. $edit = array(
  138. 'destination_id' => $destination_id,
  139. 'source_id' => $source_id,
  140. );
  141. $this->drupalPost(NULL, $edit, 'Backup now');
  142. $this->assertResponse(200);
  143. // Confirm the response is as expected. This is split up into separate
  144. // pieces because it'd be more effort than is necessary right now to confirm
  145. // what the exact filename is.
  146. if ($source_id === 'db') {
  147. $this->assertText('Default Database backed up successfully');
  148. }
  149. else {
  150. $this->assertText(' backed up successfully');
  151. }
  152. $this->assertText('in destination');
  153. if ($source_id === 'db') {
  154. $this->assertLink('download');
  155. $this->assertLink('restore');
  156. $this->assertLink('delete');
  157. }
  158. }
  159. /**
  160. * Delete all of the files in a specific backup destination.
  161. *
  162. * @param string $destination_id
  163. * The ID of the destination to check. Defaults to the manual file path.
  164. */
  165. protected function deleteBackups($destination_id = 'manual') {
  166. $destination = backup_migrate_get_destination($destination_id);
  167. $files = $this->listBackupFiles($destination_id);
  168. if (!empty($files)) {
  169. foreach ($files as $file_id => $file) {
  170. $destination->delete_file($file_id);
  171. }
  172. }
  173. }
  174. /**
  175. * Work out whether a backup filename includes a timestamp.
  176. *
  177. * @param object $file
  178. * The backup file to examine.
  179. *
  180. * @return mixed
  181. * Returns 1 if found, 0 if not found, FALSE if an error occurs.
  182. */
  183. protected function fileHasTimestamp($file) {
  184. require_once dirname(__FILE__) . '/../includes/files.inc';
  185. // Get the default filename, this is used later.
  186. $default_filename = _backup_migrate_default_filename();
  187. $ext = implode('.', $file->ext);
  188. $pattern = "/{$default_filename}-(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d)-(\d\d)-(\d\d).{$ext}/";
  189. return preg_match($pattern, $file->file_info['filename']);
  190. }
  191. /**
  192. * Confirm that a backup filename includes a timestamp.
  193. *
  194. * @param object $file
  195. * The backup file to examine.
  196. *
  197. * @return bool
  198. * Indicates whether the file includes a timestamp.
  199. */
  200. protected function assertFileTimestamp($file) {
  201. return $this->assertTrue($this->fileHasTimestamp($file));
  202. }
  203. /**
  204. * Confirm that a backup filename does not include a timestamp.
  205. *
  206. * @param object $file
  207. * The backup file to examine.
  208. *
  209. * @return bool
  210. * Indicates whether the file does not include a timestamp.
  211. */
  212. protected function assertNoFileTimestamp($file) {
  213. return !$this->assertFalse($this->fileHasTimestamp($file));
  214. }
  215. /**
  216. * Get a profile.
  217. *
  218. * @param string $profile_id
  219. * The name of the profile to load. Defaults to 'default'.
  220. *
  221. * @return object
  222. * The profile object.
  223. */
  224. protected function getProfile($profile_id = 'default') {
  225. require_once dirname(__FILE__) . '/../includes/profiles.inc';
  226. return backup_migrate_get_profile($profile_id);
  227. }
  228. /**
  229. * Confirm the most recently sent e-mail(s) contain the expected string.
  230. *
  231. * @param string $field_name
  232. * Name of field or message property to assert: subject, body, id, etc.
  233. * @param string $string
  234. * String to search for.
  235. * @param int $expected
  236. * Number of times $string should occur in the field.
  237. * @param int $email_depth
  238. * Number of emails to search for string, starting with most recent.
  239. *
  240. * @return bool
  241. * TRUE on pass, FALSE on fail.
  242. *
  243. * @see DrupalWebTestCase::assertMailString()
  244. */
  245. protected function assertMailStringCount($field_name, $string, $expected, $email_depth) {
  246. $mails = $this->drupalGetMails();
  247. $number_of_mails = count($mails);
  248. $string_count = 0;
  249. for ($i = $number_of_mails - 1; $i >= $number_of_mails - $email_depth && $i >= 0; $i--) {
  250. $mail = $mails[$i];
  251. // Normalize whitespace, as it isn't eacy to know what the mail system
  252. // might have done. Any run of whitespace becomes a single space.
  253. $normalized_mail = preg_replace('/\\s+/', ' ', $mail[$field_name]);
  254. $normalized_string = preg_replace('/\\s+/', ' ', $string);
  255. $string_count += substr_count($normalized_mail, $normalized_string);
  256. }
  257. $mail_count = min($number_of_mails, $email_depth);
  258. $message = format_plural($mail_count, 'Text "@needle" was found @stringcount times in field "@field" of the most recent email message.', 'Text "@needle" was found @stringcount times in field "@field" of the @mailcount most recent email messages.', array(
  259. '@needle' => $string,
  260. '@stringcount' => $string_count,
  261. '@field' => $field_name,
  262. '@mailcount' => $mail_count,
  263. ));
  264. return $this->assertTrue(($string_count === $expected), $message);
  265. }
  266. }