BmTestBasics.test 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * @file
  4. * Tests for different parts of the Backup Migrate system.
  5. */
  6. /**
  7. * Test that the front page still loads.
  8. */
  9. class BmTestBasics extends BmTestBase {
  10. /**
  11. * Define this test class.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Basic tests',
  16. 'description' => 'Run through basic scenarios and functionality.',
  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. * Verify the main page has the expected form, then run a basic backup.
  30. */
  31. public function testQuickBackup() {
  32. // Ensure that the private file system is working correctly.
  33. $this->drupalGet('admin/config/media/file-system');
  34. $this->assertResponse(200);
  35. $edit = array();
  36. $this->drupalPost(NULL, $edit, 'Save configuration');
  37. $this->assertResponse(200);
  38. $this->assertText('The configuration options have been saved.');
  39. // Load the main B&M page.
  40. $this->drupalGet(BACKUP_MIGRATE_MENU_PATH);
  41. $this->assertResponse(200);
  42. // @todo Confirm each of the tabs are present.
  43. // @todo Confirm each of the local tasks are present.
  44. // Confirm the form has the expected fields.
  45. $this->assertFieldByName('source_id');
  46. $this->assertFieldByName('destination_id');
  47. $this->assertFieldByName('profile_id');
  48. $this->assertFieldByName('copy');
  49. $this->assertFieldByName('copy_destination_id');
  50. $this->assertFieldByName('description_enabled');
  51. // This item should not have a value "selected", it just defaults to the
  52. // first item being the active item.
  53. $items = array('db', 'files', 'archive');
  54. $this->assertSelectOptions('edit-source-id', $items);
  55. $this->assertNoOptionsSelected('edit-source-id');
  56. // This item should have a value "selected", not just the first item. Note:
  57. // if the 'manual' backup option isn't available then the private directory
  58. // path is not set correctly.
  59. $items = array('manual', 'download');
  60. $this->assertSelectOptions('edit-destination-id', $items);
  61. $this->assertOptionSelected('edit-destination-id', 'download');
  62. // This item should not have a value "selected", it just defaults to the
  63. // first item being the active item.
  64. $items = array('default');
  65. $this->assertSelectOptions('edit-profile-id', $items);
  66. $this->assertNoOptionsSelected('edit-profile-id');
  67. // This item should not have a value "selected", it just defaults to the
  68. // first item being the active item.
  69. $items = array('manual', 'download');
  70. $this->assertSelectOptions('edit-copy-destination-id', $items);
  71. $this->assertNoOptionsSelected('edit-copy-destination-id');
  72. // Generate a backup and confirm it was created correctly.
  73. $edit = array(
  74. 'destination_id' => 'manual',
  75. );
  76. $this->drupalPost(NULL, $edit, 'Backup now');
  77. $this->assertResponse(200);
  78. // Confirm the response is as expected. This is split up into separate
  79. // pieces because it'd be more effort than is necessary right now to confirm
  80. // what the exact filename is.
  81. $this->assertText('Default Database backed up successfully');
  82. $this->assertText('in destination Manual Backups Directory');
  83. $this->assertLink('download');
  84. $this->assertLink('restore');
  85. $this->assertLink('delete');
  86. // Try requesting the backup file.
  87. $xpath = $this
  88. ->xpath('//a[normalize-space(text())=:label]', array(
  89. ':label' => 'download',
  90. ));
  91. $this->verbose($xpath);
  92. $this->assertTrue(isset($xpath[0]['href']));
  93. $this->assertNotNull($xpath[0]['href']);
  94. // @todo This doesn't work on drupalci, so work out how to fix it.
  95. // $this->drupalGet($xpath[0]['href']);
  96. $this->assertResponse(200);
  97. }
  98. /**
  99. * Test the custom validators.
  100. */
  101. public function testFieldValidators() {
  102. // Need this file loaded for the custom validators.
  103. module_load_include('advanced_settings.inc', 'backup_migrate');
  104. $field = 'mock_field';
  105. $element = array(
  106. '#value' => 'a',
  107. '#title' => '',
  108. '#parents' => array($field),
  109. );
  110. // Test the memory limit validator.
  111. $element['#title'] = 'Mock Field (Memory Limit)';
  112. $test_values = array(
  113. // Value to be tested => validity.
  114. // Special meaning: no limit.
  115. '-1' => TRUE,
  116. '50' => TRUE,
  117. // 5 megabytes.
  118. '5M' => TRUE,
  119. // 500 kilobytes.
  120. '.5M' => TRUE,
  121. // 5 gigabytes.
  122. '5G' => TRUE,
  123. '.5G' => TRUE,
  124. '1.5G' => TRUE,
  125. '0' => TRUE,
  126. 'a' => FALSE,
  127. '-2' => FALSE,
  128. // You cannot have half a byte.
  129. '1.5' => FALSE,
  130. '5T' => FALSE,
  131. '5 G' => FALSE,
  132. '.5.5G' => FALSE,
  133. );
  134. foreach ($test_values as $value => $valid) {
  135. $this->assertValidFieldValue('backup_migrate_memory_limit_validate', $element, $value, $valid, 'memory limit value');
  136. }
  137. // Test the positive non-zero integer validator.
  138. $element['#title'] = 'Mock Field (Unsigned Integer)';
  139. $test_values = array(
  140. '0' => TRUE,
  141. '1' => TRUE,
  142. '10000' => TRUE,
  143. // NaN.
  144. 'a' => FALSE,
  145. // Not an integer.
  146. '.5' => FALSE,
  147. // Not positive.
  148. '-1' => FALSE,
  149. // Largest integer.
  150. // @code
  151. // PHP_INT_MAX => TRUE,
  152. // @endcode
  153. // Resolves to a decimal.
  154. // @code
  155. // (PHP_INT_MAX + 1) => FALSE,
  156. // @endcode
  157. );
  158. foreach ($test_values as $value => $valid) {
  159. $this->assertValidFieldValue('backup_migrate_unsigned_integer_validate', $element, $value, $valid, 'zero or a positive integer');
  160. }
  161. // Test the validator for decimals ranging from 0 to 1.
  162. $element['#title'] = 'Mock Field (Decimal between 0 and 1)';
  163. $test_values = array(
  164. '0' => TRUE,
  165. '1' => TRUE,
  166. '0.1' => TRUE,
  167. '.1' => TRUE,
  168. '1.1' => FALSE,
  169. '-1' => FALSE,
  170. '-1.1' => FALSE,
  171. '2' => FALSE,
  172. 'a' => FALSE,
  173. );
  174. foreach ($test_values as $value => $valid) {
  175. $this->assertValidFieldValue('backup_migrate_fraction_validate', $element, $value, $valid, 'decimal between 0 and 1');
  176. }
  177. }
  178. /**
  179. * Asserts that the content of a field passes its associated validation.
  180. *
  181. * @param string $validator
  182. * Drupal validator function callback.
  183. * @param array $element
  184. * Input for error setter.
  185. * @param string $value
  186. * Field value to be tested.
  187. * @param bool $value_is_valid
  188. * Expected outcome.
  189. * @param string $type
  190. * Description of value sub-type.
  191. */
  192. protected function assertValidFieldValue($validator, array $element, $value, $value_is_valid, $type) {
  193. $form = array();
  194. $form_state = array();
  195. $field = 'mock_field';
  196. $element['#value'] = $value;
  197. form_clear_error();
  198. call_user_func_array($validator, array($element, &$form_state, $form));
  199. $errors = form_get_errors();
  200. if ($value_is_valid) {
  201. $args = array(
  202. '%value' => $value,
  203. '%type' => $type,
  204. );
  205. $msg = t('%value is a valid %type.', $args);
  206. $result = $this->assertTrue(empty($errors[$field]), $msg);
  207. }
  208. else {
  209. $args = array(
  210. '%value' => $value,
  211. '%type' => $type,
  212. '%msg' => strip_tags($errors[$field]),
  213. );
  214. $msg = t('%value is not a valid %type. Error message: "%msg".', $args);
  215. $result = $this->assertFalse(empty($errors[$field]), $msg);
  216. }
  217. return $result;
  218. }
  219. /**
  220. * Confirm backup_migrate_to_bytes() works.
  221. */
  222. public function testBackupMigrateToBytes() {
  223. // PHP manual:
  224. // http://php.net/manual/en/ini.core.php#ini.memory-limit
  225. // '-1' is a reserved value meaning 'no limit'.
  226. $input = '-1';
  227. $expected = -1;
  228. $this->assertEqual(backup_migrate_to_bytes($input), $expected);
  229. // PHP would halt before this, but let us test anyway.
  230. $input = 'nonsense';
  231. $expected = 0;
  232. $this->assertEqual(backup_migrate_to_bytes($input), $expected);
  233. // This is 123 bytes, but there is a bug in B&M that turns
  234. // this into 12.5 megabytes.
  235. $input = '123';
  236. $expected = 12582912;
  237. $this->assertEqual(backup_migrate_to_bytes($input), $expected);
  238. // 45 megabytes.
  239. $input = '45M';
  240. $expected = 47185920;
  241. $this->assertEqual(backup_migrate_to_bytes($input), $expected);
  242. // 8 gigabytes.
  243. $input = '8G';
  244. $expected = 8589934592;
  245. $this->assertEqual(backup_migrate_to_bytes($input), $expected);
  246. }
  247. }
  248. // @todo Test permissions.
  249. // @todo Test admin forms.