BmTestBase.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. $items = array();
  118. backup_migrate_include('destinations');
  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. */
  132. protected function runBackup($destination_id = 'manual') {
  133. $this->drupalGet(BACKUP_MIGRATE_MENU_PATH);
  134. $this->assertResponse(200);
  135. $edit = array(
  136. 'destination_id' => $destination_id,
  137. );
  138. $this->drupalPost(NULL, $edit, 'Backup now');
  139. $this->assertResponse(200);
  140. // Confirm the response is as expected. This is split up into separate
  141. // pieces because it'd be more effort than is necessary right now to confirm
  142. // what the exact filename is.
  143. $this->assertText('Default Database backed up successfully');
  144. $this->assertText('in destination');
  145. $this->assertLink('download');
  146. $this->assertLink('restore');
  147. $this->assertLink('delete');
  148. }
  149. /**
  150. * Delete all of the files in a specific backup destination.
  151. *
  152. * @param string $destination_id
  153. * The ID of the destination to check. Defaults to the manual file path.
  154. */
  155. protected function deleteBackups($destination_id = 'manual') {
  156. $destination = backup_migrate_get_destination($destination_id);
  157. $files = $this->listBackupFiles($destination_id);
  158. if (!empty($files)) {
  159. foreach ($files as $file_id => $file) {
  160. $destination->delete_file($file_id);
  161. }
  162. }
  163. }
  164. /**
  165. * Work out whether a backup filename includes a timestamp.
  166. *
  167. * @param object $file
  168. * The backup file to examine.
  169. *
  170. * @return mixed
  171. * Returns 1 if found, 0 if not found, FALSE if an error occurs.
  172. */
  173. protected function fileHasTimestamp($file) {
  174. // Get the default filename, this is used later.
  175. backup_migrate_include('files');
  176. $default_filename = _backup_migrate_default_filename();
  177. $ext = implode('.', $file->ext);
  178. $pattern = "/{$default_filename}-(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d)-(\d\d)-(\d\d).{$ext}/";
  179. return preg_match($pattern, $file->file_info['filename']);
  180. }
  181. /**
  182. * Confirm that a backup filename includes a timestamp.
  183. *
  184. * @param object $file
  185. * The backup file to examine.
  186. *
  187. * @return bool
  188. * Indicates whether the file includes a timestamp.
  189. */
  190. protected function assertFileTimestamp($file) {
  191. return $this->assertTrue($this->fileHasTimestamp($file));
  192. }
  193. /**
  194. * Confirm that a backup filename does not include a timestamp.
  195. *
  196. * @param object $file
  197. * The backup file to examine.
  198. *
  199. * @return bool
  200. * Indicates whether the file does not include a timestamp.
  201. */
  202. protected function assertNoFileTimestamp($file) {
  203. return !$this->assertFalse($this->fileHasTimestamp($file));
  204. }
  205. /**
  206. * Get a profile.
  207. *
  208. * @param string $profile_id
  209. * The name of the profile to load. Defaults to 'default'.
  210. *
  211. * @return object
  212. * The profile object.
  213. */
  214. protected function getProfile($profile_id = 'default') {
  215. backup_migrate_include('profiles');
  216. return backup_migrate_get_profile($profile_id);
  217. }
  218. }