2014-01-07 04:38:27 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Tom Needham <tom@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Test_Migrate extends PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
public $users;
|
2014-02-17 14:58:27 +04:00
|
|
|
public $tmpfiles = array();
|
2014-01-07 04:38:27 +04:00
|
|
|
|
2014-02-17 14:58:27 +04:00
|
|
|
/**
|
2014-02-18 20:23:16 +04:00
|
|
|
* @brief Generates a test user and sets up their file system
|
2014-02-17 14:58:27 +04:00
|
|
|
* @return string the test users id
|
|
|
|
*/
|
|
|
|
public function generateUser() {
|
|
|
|
$username = uniqid();
|
|
|
|
\OC_User::createUser($username, 'password');
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
\OC_Util::setupFS($username);
|
|
|
|
$this->users[] = $username;
|
|
|
|
return $username;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-18 20:23:16 +04:00
|
|
|
* @brief validates an export for a user
|
|
|
|
* @brief checks for existence of export_info.json and file folder
|
2014-02-17 14:58:27 +04:00
|
|
|
* @param string $exportedUser the user that was exported
|
|
|
|
* @param string $path the path to the .zip export
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $exportedBy
|
2014-02-17 14:58:27 +04:00
|
|
|
*/
|
|
|
|
public function validateUserExport($exportedBy, $exportedUser, $path) {
|
|
|
|
$this->assertTrue(file_exists($path));
|
|
|
|
// Extract
|
|
|
|
$extract = get_temp_dir() . '/oc_import_' . uniqid();
|
|
|
|
//mkdir($extract);
|
|
|
|
$this->tmpfiles[] = $extract;
|
|
|
|
$zip = new ZipArchive;
|
|
|
|
$zip->open($path);
|
|
|
|
$zip->extractTo($extract);
|
|
|
|
$zip->close();
|
|
|
|
$this->assertTrue(file_exists($extract.'/export_info.json'));
|
|
|
|
$exportInfo = file_get_contents($extract.'/export_info.json');
|
|
|
|
$exportInfo = json_decode($exportInfo);
|
|
|
|
$this->assertNotNull($exportInfo);
|
|
|
|
$this->assertEquals($exportedUser, $exportInfo->exporteduser);
|
|
|
|
$this->assertEquals($exportedBy, $exportInfo->exportedby);
|
|
|
|
$this->assertTrue(file_exists($extract.'/'.$exportedUser.'/files'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserSelfExport() {
|
2014-01-07 04:38:27 +04:00
|
|
|
// Create a user
|
2014-02-17 14:58:27 +04:00
|
|
|
$user = $this->generateUser();
|
|
|
|
\OC_User::setUserId($user);
|
|
|
|
$export = \OC_Migrate::export($user);
|
|
|
|
// Check it succeeded and exists
|
|
|
|
$this->assertTrue(json_decode($export)->success);
|
|
|
|
// Validate the export
|
|
|
|
$this->validateUserExport($user, $user, json_decode($export)->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserOtherExport() {
|
|
|
|
$user = $this->generateUser();
|
|
|
|
$user2 = $this->generateUser();
|
|
|
|
\OC_User::setUserId($user2);
|
|
|
|
$export = \OC_Migrate::export($user);
|
|
|
|
// Check it succeeded and exists
|
|
|
|
$this->assertTrue(json_decode($export)->success);
|
|
|
|
// Validate the export
|
|
|
|
$this->validateUserExport($user2, $user, json_decode($export)->data);
|
2014-01-07 04:38:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
$u = new OC_User();
|
|
|
|
foreach($this->users as $user) {
|
|
|
|
$u->deleteUser($user);
|
|
|
|
}
|
2014-02-17 14:58:27 +04:00
|
|
|
foreach($this->tmpfiles as $file) {
|
|
|
|
\OC_Helper::rmdirr($file);
|
|
|
|
}
|
2014-01-07 04:38:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|