nextcloud/apps/files_sharing/tests/external/managertest.php

216 lines
9.3 KiB
PHP
Raw Normal View History

<?php
/**
2015-03-26 13:44:34 +03:00
* @author Joas Schilling <nickvergessen@owncloud.com>
*
2015-03-26 13:44:34 +03:00
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
2015-03-26 13:44:34 +03:00
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
2015-03-26 13:44:34 +03:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2015-03-26 13:44:34 +03:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
2015-03-26 13:44:34 +03:00
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Sharing\Tests\External;
2015-03-24 19:26:38 +03:00
use OC\Files\Storage\StorageFactory;
2015-04-27 18:14:52 +03:00
use OCA\Files_Sharing\External\Manager;
use OCA\Files_Sharing\Tests\TestCase;
class ManagerTest extends TestCase {
2015-04-27 18:14:52 +03:00
/** @var Manager **/
private $manager;
2015-03-24 19:26:38 +03:00
/** @var \OC\Files\Mount\Manager */
private $mountManager;
2015-04-27 18:14:52 +03:00
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $httpHelper;
private $uid;
protected function setUp() {
parent::setUp();
$this->uid = $this->getUniqueID('user');
2015-03-24 19:26:38 +03:00
$this->mountManager = new \OC\Files\Mount\Manager();
2015-04-27 18:14:52 +03:00
$this->httpHelper = $httpHelper = $this->getMockBuilder('\OC\HTTPHelper')->disableOriginalConstructor()->getMock();
/** @var \OC\HTTPHelper $httpHelper */
$this->manager = new Manager(
\OC::$server->getDatabaseConnection(),
2015-03-24 19:26:38 +03:00
$this->mountManager,
new StorageFactory(),
2015-04-27 18:14:52 +03:00
$httpHelper,
$this->uid
);
}
public function testAddShare() {
2015-04-27 18:14:52 +03:00
$shareData1 = [
2015-03-24 19:26:38 +03:00
'remote' => 'http://localhost',
'token' => 'token1',
'password' => '',
'name' => '/SharedFolder',
'owner' => 'foobar',
'accepted' => false,
'user' => $this->uid,
];
$shareData2 = $shareData1;
$shareData2['token'] = 'token2';
$shareData3 = $shareData1;
$shareData3['token'] = 'token3';
// Add a share for "user"
$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
$openShares = $this->manager->getOpenShares();
$this->assertCount(1, $openShares);
$this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
2015-03-24 19:26:38 +03:00
\Test_Helper::invokePrivate($this->manager, 'setupMounts');
$this->assertNotMount('SharedFolder');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
// Add a second share for "user" with the same name
$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
$openShares = $this->manager->getOpenShares();
$this->assertCount(2, $openShares);
$this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
// New share falls back to "-1" appendix, because the name is already taken
$this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
2015-03-24 19:26:38 +03:00
\Test_Helper::invokePrivate($this->manager, 'setupMounts');
$this->assertNotMount('SharedFolder');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
$this->httpHelper->expects($this->at(0))
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything());
// Accept the first share
$this->manager->acceptShare($openShares[0]['id']);
// Check remaining shares - Accepted
$acceptedShares = \Test_Helper::invokePrivate($this->manager, 'getShares', [true]);
$this->assertCount(1, $acceptedShares);
$shareData1['accepted'] = true;
$this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
// Check remaining shares - Open
$openShares = $this->manager->getOpenShares();
$this->assertCount(1, $openShares);
$this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
2015-03-24 19:26:38 +03:00
\Test_Helper::invokePrivate($this->manager, 'setupMounts');
$this->assertMount($shareData1['name']);
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
// Add another share for "user" with the same name
$this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
$openShares = $this->manager->getOpenShares();
$this->assertCount(2, $openShares);
$this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
// New share falls back to the original name (no "-\d", because the name is not taken)
$this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}');
2015-03-24 19:26:38 +03:00
\Test_Helper::invokePrivate($this->manager, 'setupMounts');
$this->assertMount($shareData1['name']);
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
$this->httpHelper->expects($this->at(0))
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything());
// Decline the third share
$this->manager->declineShare($openShares[1]['id']);
2015-03-24 19:26:38 +03:00
\Test_Helper::invokePrivate($this->manager, 'setupMounts');
$this->assertMount($shareData1['name']);
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
// Check remaining shares - Accepted
$acceptedShares = \Test_Helper::invokePrivate($this->manager, 'getShares', [true]);
$this->assertCount(1, $acceptedShares);
$shareData1['accepted'] = true;
$this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
// Check remaining shares - Open
$openShares = $this->manager->getOpenShares();
$this->assertCount(1, $openShares);
$this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
2015-03-24 19:26:38 +03:00
\Test_Helper::invokePrivate($this->manager, 'setupMounts');
$this->assertMount($shareData1['name']);
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
$this->httpHelper->expects($this->at(0))
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything());
$this->httpHelper->expects($this->at(1))
->method('post')
->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything());
$this->manager->removeUserShares($this->uid);
$this->assertEmpty(\Test_Helper::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
2015-03-24 19:26:38 +03:00
$this->mountManager->clear();
\Test_Helper::invokePrivate($this->manager, 'setupMounts');
$this->assertNotMount($shareData1['name']);
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
$this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
}
/**
* @param array $expected
* @param array $actual
* @param int $share
* @param string $mountPoint
*/
protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint) {
$this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
$this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
$this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
$this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
$this->assertEquals($expected['accepted'], (int) $actual['accepted'], 'Asserting accept of a share #' . $share);
$this->assertEquals($expected['user'], $actual['user'], 'Asserting user of a share #' . $share);
$this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
2015-03-24 19:26:38 +03:00
}
private function assertMount($mountPoint) {
$mountPoint = rtrim($mountPoint, '/');
$mount = $this->mountManager->find($this->getFullPath($mountPoint));
$this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
$this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
$this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
$storage = $mount->getStorage();
$this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
}
private function assertNotMount($mountPoint) {
$mountPoint = rtrim($mountPoint, '/');
$mount = $this->mountManager->find($this->getFullPath($mountPoint));
if ($mount) {
$this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
$this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
} else {
$this->assertNull($mount);
}
}
2015-03-24 19:26:38 +03:00
private function getFullPath($path) {
return '/' . $this->uid . '/files' . $path;
}
}