migrate.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Tom Needham <tom@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_Migrate extends PHPUnit_Framework_TestCase {
  9. public $users;
  10. public $tmpfiles = array();
  11. /**
  12. * Generates a test user and sets up their file system
  13. * @return string the test users id
  14. */
  15. public function generateUser() {
  16. $username = uniqid();
  17. \OC_User::createUser($username, 'password');
  18. \OC_Util::tearDownFS();
  19. \OC_User::setUserId('');
  20. \OC\Files\Filesystem::tearDown();
  21. \OC_Util::setupFS($username);
  22. $this->users[] = $username;
  23. return $username;
  24. }
  25. /**
  26. * validates an export for a user
  27. * checks for existence of export_info.json and file folder
  28. * @param string $exportedUser the user that was exported
  29. * @param string $path the path to the .zip export
  30. * @param string $exportedBy
  31. */
  32. public function validateUserExport($exportedBy, $exportedUser, $path) {
  33. $this->assertTrue(file_exists($path));
  34. // Extract
  35. $extract = get_temp_dir() . '/oc_import_' . uniqid();
  36. //mkdir($extract);
  37. $this->tmpfiles[] = $extract;
  38. $zip = new ZipArchive;
  39. $zip->open($path);
  40. $zip->extractTo($extract);
  41. $zip->close();
  42. $this->assertTrue(file_exists($extract.'/export_info.json'));
  43. $exportInfo = file_get_contents($extract.'/export_info.json');
  44. $exportInfo = json_decode($exportInfo);
  45. $this->assertNotNull($exportInfo);
  46. $this->assertEquals($exportedUser, $exportInfo->exporteduser);
  47. $this->assertEquals($exportedBy, $exportInfo->exportedby);
  48. $this->assertTrue(file_exists($extract.'/'.$exportedUser.'/files'));
  49. }
  50. public function testUserSelfExport() {
  51. // Create a user
  52. $user = $this->generateUser();
  53. \OC_User::setUserId($user);
  54. $export = \OC_Migrate::export($user);
  55. // Check it succeeded and exists
  56. $this->assertTrue(json_decode($export)->success);
  57. // Validate the export
  58. $this->validateUserExport($user, $user, json_decode($export)->data);
  59. }
  60. public function testUserOtherExport() {
  61. $user = $this->generateUser();
  62. $user2 = $this->generateUser();
  63. \OC_User::setUserId($user2);
  64. $export = \OC_Migrate::export($user);
  65. // Check it succeeded and exists
  66. $this->assertTrue(json_decode($export)->success);
  67. // Validate the export
  68. $this->validateUserExport($user2, $user, json_decode($export)->data);
  69. }
  70. public function tearDown() {
  71. $u = new OC_User();
  72. foreach($this->users as $user) {
  73. $u->deleteUser($user);
  74. }
  75. foreach($this->tmpfiles as $file) {
  76. \OC_Helper::rmdirr($file);
  77. }
  78. }
  79. }