diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/appframework/http/RequestTest.php | 138 | ||||
-rw-r--r-- | tests/lib/files/node/hookconnector.php | 176 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/encryption.php | 23 | ||||
-rw-r--r-- | tests/lib/files/view.php | 131 | ||||
-rw-r--r-- | tests/lib/l10n.php | 6 | ||||
-rw-r--r-- | tests/lib/preview.php | 119 | ||||
-rw-r--r-- | tests/lib/repair/repairinvalidsharestest.php | 123 | ||||
-rw-r--r-- | tests/lib/repair/repairmimetypes.php | 72 | ||||
-rw-r--r-- | tests/lib/share/share.php | 70 | ||||
-rw-r--r-- | tests/lib/testcase.php | 35 | ||||
-rw-r--r-- | tests/lib/util.php | 2 | ||||
-rw-r--r-- | tests/settings/controller/CheckSetupControllerTest.php | 1 |
12 files changed, 797 insertions, 99 deletions
diff --git a/tests/lib/appframework/http/RequestTest.php b/tests/lib/appframework/http/RequestTest.php index deb28909869..f75e4578546 100644 --- a/tests/lib/appframework/http/RequestTest.php +++ b/tests/lib/appframework/http/RequestTest.php @@ -60,18 +60,18 @@ class RequestTest extends \Test\TestCase { ); // Countable - $this->assertEquals(2, count($request)); + $this->assertSame(2, count($request)); // Array access - $this->assertEquals('Joey', $request['nickname']); + $this->assertSame('Joey', $request['nickname']); // "Magic" accessors - $this->assertEquals('Joey', $request->{'nickname'}); + $this->assertSame('Joey', $request->{'nickname'}); $this->assertTrue(isset($request['nickname'])); $this->assertTrue(isset($request->{'nickname'})); - $this->assertEquals(false, isset($request->{'flickname'})); + $this->assertFalse(isset($request->{'flickname'})); // Only testing 'get', but same approach for post, files etc. - $this->assertEquals('Joey', $request->get['nickname']); + $this->assertSame('Joey', $request->get['nickname']); // Always returns null if variable not set. - $this->assertEquals(null, $request->{'flickname'}); + $this->assertSame(null, $request->{'flickname'}); } @@ -92,9 +92,9 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals(3, count($request)); - $this->assertEquals('Janey', $request->{'nickname'}); - $this->assertEquals('Johnny Weissmüller', $request->{'name'}); + $this->assertSame(3, count($request)); + $this->assertSame('Janey', $request->{'nickname'}); + $this->assertSame('Johnny Weissmüller', $request->{'name'}); } @@ -172,10 +172,10 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('GET', $request->method); + $this->assertSame('GET', $request->method); $result = $request->get; - $this->assertEquals('John Q. Public', $result['name']); - $this->assertEquals('Joey', $result['nickname']); + $this->assertSame('John Q. Public', $result['name']); + $this->assertSame('Joey', $result['nickname']); } public function testJsonPost() { @@ -194,12 +194,12 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('POST', $request->method); + $this->assertSame('POST', $request->method); $result = $request->post; - $this->assertEquals('John Q. Public', $result['name']); - $this->assertEquals('Joey', $result['nickname']); - $this->assertEquals('Joey', $request->params['nickname']); - $this->assertEquals('Joey', $request['nickname']); + $this->assertSame('John Q. Public', $result['name']); + $this->assertSame('Joey', $result['nickname']); + $this->assertSame('Joey', $request->params['nickname']); + $this->assertSame('Joey', $request['nickname']); } public function testPatch() { @@ -219,11 +219,11 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('PATCH', $request->method); + $this->assertSame('PATCH', $request->method); $result = $request->patch; - $this->assertEquals('John Q. Public', $result['name']); - $this->assertEquals('Joey', $result['nickname']); + $this->assertSame('John Q. Public', $result['name']); + $this->assertSame('Joey', $result['nickname']); } public function testJsonPatchAndPut() { @@ -244,11 +244,11 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('PUT', $request->method); + $this->assertSame('PUT', $request->method); $result = $request->put; - $this->assertEquals('John Q. Public', $result['name']); - $this->assertEquals('Joey', $result['nickname']); + $this->assertSame('John Q. Public', $result['name']); + $this->assertSame('Joey', $result['nickname']); // PATCH content $data = '{"name": "John Q. Public", "nickname": null}'; @@ -265,11 +265,11 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('PATCH', $request->method); + $this->assertSame('PATCH', $request->method); $result = $request->patch; - $this->assertEquals('John Q. Public', $result['name']); - $this->assertEquals(null, $result['nickname']); + $this->assertSame('John Q. Public', $result['name']); + $this->assertSame(null, $result['nickname']); } public function testPutStream() { @@ -290,10 +290,10 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('PUT', $request->method); + $this->assertSame('PUT', $request->method); $resource = $request->put; $contents = stream_get_contents($resource); - $this->assertEquals($data, $contents); + $this->assertSame($data, $contents); try { $resource = $request->put; @@ -322,7 +322,7 @@ class RequestTest extends \Test\TestCase { $newParams = array('id' => '3', 'test' => 'test2'); $request->setUrlParameters($newParams); - $this->assertEquals('test2', $request->getParam('test')); + $this->assertSame('test2', $request->getParam('test')); $this->assertEquals('3', $request->getParam('id')); $this->assertEquals('3', $request->getParams()['id']); } @@ -497,6 +497,57 @@ class RequestTest extends \Test\TestCase { $this->assertSame('192.168.0.233', $request->getRemoteAddress()); } + /** + * @return array + */ + public function httpProtocolProvider() { + return [ + // Valid HTTP 1.0 + ['HTTP/1.0', 'HTTP/1.0'], + ['http/1.0', 'HTTP/1.0'], + ['HTTp/1.0', 'HTTP/1.0'], + + // Valid HTTP 1.1 + ['HTTP/1.1', 'HTTP/1.1'], + ['http/1.1', 'HTTP/1.1'], + ['HTTp/1.1', 'HTTP/1.1'], + + // Valid HTTP 2.0 + ['HTTP/2', 'HTTP/2'], + ['http/2', 'HTTP/2'], + ['HTTp/2', 'HTTP/2'], + + // Invalid + ['HTTp/394', 'HTTP/1.1'], + ['InvalidProvider/1.1', 'HTTP/1.1'], + [null, 'HTTP/1.1'], + ['', 'HTTP/1.1'], + + ]; + } + + /** + * @dataProvider httpProtocolProvider + * + * @param mixed $input + * @param string $expected + */ + public function testGetHttpProtocol($input, $expected) { + $request = new Request( + [ + 'server' => [ + 'SERVER_PROTOCOL' => $input, + ], + ], + $this->secureRandom, + $this->getMock('\OCP\Security\ICrypto'), + $this->config, + $this->stream + ); + + $this->assertSame($expected, $request->getHttpProtocol()); + } + public function testGetServerProtocolWithOverride() { $this->config ->expects($this->at(0)) @@ -660,7 +711,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals($matches, $request->isUserAgent($userAgent)); + $this->assertSame($matches, $request->isUserAgent($userAgent)); } /** @@ -749,7 +800,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('from.server.name:8080', $request->getInsecureServerHost()); + $this->assertSame('from.server.name:8080', $request->getInsecureServerHost()); } public function testInsecureServerHostHttpHostHeader() { @@ -766,7 +817,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('from.host.header:8080', $request->getInsecureServerHost()); + $this->assertSame('from.host.header:8080', $request->getInsecureServerHost()); } public function testInsecureServerHostHttpFromForwardedHeaderSingle() { @@ -784,7 +835,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('from.forwarded.host:8080', $request->getInsecureServerHost()); + $this->assertSame('from.forwarded.host:8080', $request->getInsecureServerHost()); } public function testInsecureServerHostHttpFromForwardedHeaderStacked() { @@ -802,7 +853,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('from.forwarded.host2:8080', $request->getInsecureServerHost()); + $this->assertSame('from.forwarded.host2:8080', $request->getInsecureServerHost()); } public function testGetServerHostWithOverwriteHost() { @@ -830,7 +881,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('my.overwritten.host', $request->getServerHost()); + $this->assertSame('my.overwritten.host', $request->getServerHost()); } public function testGetServerHostWithTrustedDomain() { @@ -852,7 +903,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('my.trusted.host', $request->getServerHost()); + $this->assertSame('my.trusted.host', $request->getServerHost()); } public function testGetServerHostWithUntrustedDomain() { @@ -879,7 +930,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('my.trusted.host', $request->getServerHost()); + $this->assertSame('my.trusted.host', $request->getServerHost()); } public function testGetServerHostWithNoTrustedDomain() { @@ -906,7 +957,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('', $request->getServerHost()); + $this->assertSame('', $request->getServerHost()); } public function testGetOverwriteHostDefaultNull() { @@ -967,7 +1018,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals('apps/files/', $request->getPathInfo()); + $this->assertSame('apps/files/', $request->getPathInfo()); } /** @@ -1032,7 +1083,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals($expected, $request->getPathInfo()); + $this->assertSame($expected, $request->getPathInfo()); } /** @@ -1055,7 +1106,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals($expected, $request->getRawPathInfo()); + $this->assertSame($expected, $request->getRawPathInfo()); } /** @@ -1078,7 +1129,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals($expected, $request->getRawPathInfo()); + $this->assertSame($expected, $request->getRawPathInfo()); } /** @@ -1101,7 +1152,7 @@ class RequestTest extends \Test\TestCase { $this->stream ); - $this->assertEquals($expected, $request->getPathInfo()); + $this->assertSame($expected, $request->getPathInfo()); } /** @@ -1109,6 +1160,7 @@ class RequestTest extends \Test\TestCase { */ public function genericPathInfoProvider() { return [ + ['/core/index.php?XDEBUG_SESSION_START=14600', '/core/index.php', ''], ['/index.php/apps/files/', 'index.php', '/apps/files/'], ['/index.php/apps/files/../&/&?someQueryParameter=QueryParam', 'index.php', '/apps/files/../&/&'], ['/remote.php/漢字編碼方法 / 汉字编码方法', 'remote.php', '/漢字編碼方法 / 汉字编码方法'], diff --git a/tests/lib/files/node/hookconnector.php b/tests/lib/files/node/hookconnector.php new file mode 100644 index 00000000000..10566cf5fb1 --- /dev/null +++ b/tests/lib/files/node/hookconnector.php @@ -0,0 +1,176 @@ +<?php +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Node; + +use OC\Files\Filesystem; +use OC\Files\Node\Root; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use OCP\Files\Node; +use Test\TestCase; +use Test\Traits\MountProviderTrait; +use Test\Traits\UserTrait; + +class HookConnector extends TestCase { + use UserTrait; + use MountProviderTrait; + + /** + * @var View + */ + private $view; + + /** + * @var Root + */ + private $root; + + /** + * @var string + */ + private $userId; + + public function setUp() { + parent::setUp(); + $this->userId = $this->getUniqueID(); + $this->createUser($this->userId, 'pass'); + $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/'); + \OC_Util::setupFS($this->userId); + $this->view = new View(); + $this->root = new Root(Filesystem::getMountManager(), $this->view, \OC::$server->getUserManager()->get($this->userId)); + } + + public function tearDown() { + parent::tearDown(); + \OC_Hook::clear('OC_Filesystem'); + \OC_Util::tearDownFS(); + } + + public function viewToNodeProvider() { + return [ + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'preWrite'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'postWrite'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'preCreate'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'postCreate'], + [function () { + Filesystem::mkdir('test.txt'); + }, 'preCreate'], + [function () { + Filesystem::mkdir('test.txt'); + }, 'postCreate'], + [function () { + Filesystem::touch('test.txt'); + }, 'preTouch'], + [function () { + Filesystem::touch('test.txt'); + }, 'postTouch'], + [function () { + Filesystem::touch('test.txt'); + }, 'preCreate'], + [function () { + Filesystem::touch('test.txt'); + }, 'postCreate'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + Filesystem::unlink('test.txt'); + }, 'preDelete'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + Filesystem::unlink('test.txt'); + }, 'postDelete'], + [function () { + Filesystem::mkdir('test.txt'); + Filesystem::rmdir('test.txt'); + }, 'preDelete'], + [function () { + Filesystem::mkdir('test.txt'); + Filesystem::rmdir('test.txt'); + }, 'postDelete'], + ]; + } + + /** + * @param callable $operation + * @param string $expectedHook + * @dataProvider viewToNodeProvider + */ + public function testViewToNode(callable $operation, $expectedHook) { + $connector = new \OC\Files\Node\HookConnector($this->root, $this->view); + $connector->viewToNode(); + $hookCalled = false; + /** @var Node $hookNode */ + $hookNode = null; + + $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) { + $hookCalled = true; + $hookNode = $node; + }); + + $operation(); + + $this->assertTrue($hookCalled); + $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath()); + } + + public function viewToNodeProviderCopyRename() { + return [ + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::rename('source', 'target'); + }, 'preRename'], + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::rename('source', 'target'); + }, 'postRename'], + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::copy('source', 'target'); + }, 'preCopy'], + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::copy('source', 'target'); + }, 'postCopy'], + ]; + } + + /** + * @param callable $operation + * @param string $expectedHook + * @dataProvider viewToNodeProviderCopyRename + */ + public function testViewToNodeCopyRename(callable $operation, $expectedHook) { + $connector = new \OC\Files\Node\HookConnector($this->root, $this->view); + $connector->viewToNode(); + $hookCalled = false; + /** @var Node $hookSourceNode */ + $hookSourceNode = null; + /** @var Node $hookTargetNode */ + $hookTargetNode = null; + + $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) { + $hookCalled = true; + $hookSourceNode = $sourceNode; + $hookTargetNode = $targetNode; + }); + + $operation(); + + $this->assertTrue($hookCalled); + $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath()); + $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath()); + } +} diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php index 44e910b901f..095405462df 100644 --- a/tests/lib/files/storage/wrapper/encryption.php +++ b/tests/lib/files/storage/wrapper/encryption.php @@ -558,4 +558,27 @@ class Encryption extends \Test\Files\Storage\Storage { $this->assertFalse(false); } + + /** + * @dataProvider dataTestIsVersion + * @param string $path + * @param bool $expected + */ + public function testIsVersion($path, $expected) { + $this->assertSame($expected, + $this->invokePrivate($this->instance, 'isVersion', [$path]) + ); + } + + public function dataTestIsVersion() { + return [ + ['files_versions/foo', true], + ['/files_versions/foo', true], + ['//files_versions/foo', true], + ['files/versions/foo', false], + ['files/files_versions/foo', false], + ['files_versions_test/foo', false], + ]; + } + } diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index ceeb9ba7a94..94f9e209152 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -104,6 +104,9 @@ class View extends \Test\TestCase { $this->userObject->delete(); $this->groupObject->delete(); + $mountProviderCollection = \OC::$server->getMountProviderCollection(); + \Test\TestCase::invokePrivate($mountProviderCollection, 'providers', [[]]); + parent::tearDown(); } @@ -1486,6 +1489,112 @@ class View extends \Test\TestCase { $this->assertEquals($shouldEmit, $result); } + /** + * Create test movable mount points + * + * @param array $mountPoints array of mount point locations + * @return array array of MountPoint objects + */ + private function createTestMovableMountPoints($mountPoints) { + $mounts = []; + foreach ($mountPoints as $mountPoint) { + $storage = $this->getMockBuilder('\OC\Files\Storage\Temporary') + ->setMethods([]) + ->getMock(); + + $mounts[] = $this->getMock( + '\Test\TestMoveableMountPoint', + ['moveMount'], + [$storage, $mountPoint] + ); + } + + $mountProvider = $this->getMock('\OCP\Files\Config\IMountProvider'); + $mountProvider->expects($this->any()) + ->method('getMountsForUser') + ->will($this->returnValue($mounts)); + + $mountProviderCollection = \OC::$server->getMountProviderCollection(); + $mountProviderCollection->registerProvider($mountProvider); + + return $mounts; + } + + /** + * Test mount point move + */ + public function testMountPointMove() { + $this->loginAsUser($this->user); + + list($mount1, $mount2) = $this->createTestMovableMountPoints([ + $this->user . '/files/mount1', + $this->user . '/files/mount2', + ]); + $mount1->expects($this->once()) + ->method('moveMount') + ->will($this->returnValue(true)); + + $mount2->expects($this->once()) + ->method('moveMount') + ->will($this->returnValue(true)); + + $view = new \OC\Files\View('/' . $this->user . '/files/'); + $view->mkdir('sub'); + + $this->assertTrue($view->rename('mount1', 'renamed_mount'), 'Can rename mount point'); + $this->assertTrue($view->rename('mount2', 'sub/moved_mount'), 'Can move a mount point into a subdirectory'); + } + /** + * Test that moving a mount point into another is forbidden + */ + public function testMoveMountPointIntoAnother() { + $this->loginAsUser($this->user); + + list($mount1, $mount2) = $this->createTestMovableMountPoints([ + $this->user . '/files/mount1', + $this->user . '/files/mount2', + ]); + + $mount1->expects($this->never()) + ->method('moveMount'); + + $mount2->expects($this->never()) + ->method('moveMount'); + + $view = new \OC\Files\View('/' . $this->user . '/files/'); + + $this->assertFalse($view->rename('mount1', 'mount2'), 'Cannot overwrite another mount point'); + $this->assertFalse($view->rename('mount1', 'mount2/sub'), 'Cannot move a mount point into another'); + } + /** + * Test that moving a mount point into a shared folder is forbidden + */ + public function testMoveMountPointIntoSharedFolder() { + $this->loginAsUser($this->user); + + list($mount1) = $this->createTestMovableMountPoints([ + $this->user . '/files/mount1', + ]); + + $mount1->expects($this->never()) + ->method('moveMount'); + + $view = new \OC\Files\View('/' . $this->user . '/files/'); + $view->mkdir('shareddir'); + $view->mkdir('shareddir/sub'); + $view->mkdir('shareddir/sub2'); + + $fileId = $view->getFileInfo('shareddir')->getId(); + $userObject = \OC::$server->getUserManager()->createUser('test2', 'IHateNonMockableStaticClasses'); + $this->assertTrue(\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, 'test2', \OCP\Constants::PERMISSION_READ)); + + $this->assertFalse($view->rename('mount1', 'shareddir'), 'Cannot overwrite shared folder'); + $this->assertFalse($view->rename('mount1', 'shareddir/sub'), 'Cannot move mount point into shared folder'); + $this->assertFalse($view->rename('mount1', 'shareddir/sub/sub2'), 'Cannot move mount point into shared subfolder'); + + $this->assertTrue(\OCP\Share::unshare('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, 'test2')); + $userObject->delete(); + } public function basicOperationProviderForLocks() { return [ @@ -2042,23 +2151,9 @@ class View extends \Test\TestCase { public function testLockMoveMountPoint() { $this->loginAsUser('test'); - $subStorage = $this->getMockBuilder('\OC\Files\Storage\Temporary') - ->setMethods([]) - ->getMock(); - - $mount = $this->getMock( - '\Test\TestMoveableMountPoint', - ['moveMount'], - [$subStorage, $this->user . '/files/substorage'] - ); - - $mountProvider = $this->getMock('\OCP\Files\Config\IMountProvider'); - $mountProvider->expects($this->once()) - ->method('getMountsForUser') - ->will($this->returnValue([$mount])); - - $mountProviderCollection = \OC::$server->getMountProviderCollection(); - $mountProviderCollection->registerProvider($mountProvider); + list($mount) = $this->createTestMovableMountPoints([ + $this->user . '/files/substorage', + ]); $view = new \OC\Files\View('/' . $this->user . '/files/'); $view->mkdir('subdir'); @@ -2109,8 +2204,6 @@ class View extends \Test\TestCase { $this->assertNull($this->getFileLockType($view, $sourcePath, false), 'Shared storage root not locked after operation'); $this->assertNull($this->getFileLockType($view, $sourcePath, true), 'Source path not locked after operation'); $this->assertNull($this->getFileLockType($view, $targetPath, true), 'Target path not locked after operation'); - - \Test\TestCase::invokePrivate($mountProviderCollection, 'providers', [[]]); } /** diff --git a/tests/lib/l10n.php b/tests/lib/l10n.php index d5f9a5ca3fa..cb2f4179c4d 100644 --- a/tests/lib/l10n.php +++ b/tests/lib/l10n.php @@ -92,8 +92,7 @@ class Test_L10n extends \Test\TestCase { * @dataProvider localizationDataProvider */ public function testNumericStringLocalization($expectedDate, $lang, $type, $value) { - $l = new OC_L10N('test'); - $l->forceLanguage($lang); + $l = new OC_L10N('test', $lang); $this->assertSame($expectedDate, $l->l($type, $value)); } @@ -110,8 +109,7 @@ class Test_L10n extends \Test\TestCase { * @param $lang */ public function testFirstWeekDay($expected, $lang) { - $l = new OC_L10N('test'); - $l->forceLanguage($lang); + $l = new OC_L10N('test', $lang); $this->assertSame($expected, $l->l('firstday', 'firstday')); } diff --git a/tests/lib/preview.php b/tests/lib/preview.php index 9e118014bac..e4c599c66cf 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -22,7 +22,15 @@ namespace Test; +use OC\Files\FileInfo; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use Test\Traits\MountProviderTrait; +use Test\Traits\UserTrait; + class Preview extends TestCase { + use UserTrait; + use MountProviderTrait; const TEST_PREVIEW_USER1 = "test-preview-user1"; @@ -59,11 +67,7 @@ class Preview extends TestCase { protected function setUp() { parent::setUp(); - $userManager = \OC::$server->getUserManager(); - $userManager->clearBackends(); - $backend = new \Test\Util\User\Dummy(); - $userManager->registerBackend($backend); - $backend->createUser(self::TEST_PREVIEW_USER1, self::TEST_PREVIEW_USER1); + $this->createUser(self::TEST_PREVIEW_USER1, self::TEST_PREVIEW_USER1); $this->loginAsUser(self::TEST_PREVIEW_USER1); $storage = new \OC\Files\Storage\Temporary([]); @@ -75,13 +79,13 @@ class Preview extends TestCase { // We simulate the max dimension set in the config \OC::$server->getConfig() - ->setSystemValue('preview_max_x', $this->configMaxWidth); + ->setSystemValue('preview_max_x', $this->configMaxWidth); \OC::$server->getConfig() - ->setSystemValue('preview_max_y', $this->configMaxHeight); + ->setSystemValue('preview_max_y', $this->configMaxHeight); // Used to test upscaling $this->maxScaleFactor = 2; \OC::$server->getConfig() - ->setSystemValue('preview_max_scale_factor', $this->maxScaleFactor); + ->setSystemValue('preview_max_scale_factor', $this->maxScaleFactor); // We need to enable the providers we're going to use in the tests $providers = [ @@ -92,7 +96,7 @@ class Preview extends TestCase { 'OC\\Preview\\Postscript' ]; \OC::$server->getConfig() - ->setSystemValue('enabledPreviewProviders', $providers); + ->setSystemValue('enabledPreviewProviders', $providers); // Sample is 1680x1050 JPEG $this->prepareSample('testimage.jpg', 1680, 1050); @@ -161,7 +165,7 @@ class Preview extends TestCase { $fileId = $fileInfo['fileid']; $thumbCacheFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . - '/' . $fileId . '/'; + '/' . $fileId . '/'; $this->assertSame(true, $this->rootView->is_dir($thumbCacheFolder), "$thumbCacheFolder \n"); @@ -318,7 +322,7 @@ class Preview extends TestCase { // There should be no cached thumbnails $thumbnailFolder = '/' . self::TEST_PREVIEW_USER1 . '/' . \OC\Preview::THUMBNAILS_FOLDER . - '/' . $sampleFileId; + '/' . $sampleFileId; $this->assertSame(false, $this->rootView->is_dir($thumbnailFolder)); $image = $preview->getPreview(); @@ -534,10 +538,10 @@ class Preview extends TestCase { // Small thumbnails are always cropped $this->keepAspect = false; // Smaller previews should be based on the previous, larger preview, with the correct aspect ratio - $this->createThumbnailFromBiggerCachedPreview($fileId, 36, 36); + $this->createThumbnailFromBiggerCachedPreview($fileId, 32, 32); // 2nd cache query should indicate that we have a cached copy of the exact dimension - $this->getCachedSmallThumbnail($fileId, 36, 36); + $this->getCachedSmallThumbnail($fileId, 32, 32); // We create a preview in order to be able to delete the cache $preview = $this->createPreview(rand(), rand()); @@ -611,7 +615,7 @@ class Preview extends TestCase { // Need to take care of special postfix added to the dimensions $postfix = ''; $isMaxPreview = ($width === $this->maxPreviewWidth - && $height === $this->maxPreviewHeight) ? true : false; + && $height === $this->maxPreviewHeight) ? true : false; if ($isMaxPreview) { $postfix = '-max'; } @@ -731,7 +735,7 @@ class Preview extends TestCase { } return $userPath . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId - . '/' . $width . '-' . $height . $postfix . '.png'; + . '/' . $width . '-' . $height . $postfix . '.png'; } /** @@ -752,11 +756,11 @@ class Preview extends TestCase { $this->samples[] = [ - 'sampleFileId' => $fileInfo['fileid'], - 'sampleFileName' => $fileName, - 'sampleWidth' => $sampleWidth, - 'sampleHeight' => $sampleHeight, - 'maxPreviewWidth' => $maxPreviewWidth, + 'sampleFileId' => $fileInfo['fileid'], + 'sampleFileName' => $fileName, + 'sampleWidth' => $sampleWidth, + 'sampleHeight' => $sampleHeight, + 'maxPreviewWidth' => $maxPreviewWidth, 'maxPreviewHeight' => $maxPreviewHeight ]; } @@ -874,4 +878,79 @@ class Preview extends TestCase { return [(int)$askedWidth, (int)$askedHeight]; } + + public function testKeepAspectRatio() { + $originalWidth = 1680; + $originalHeight = 1050; + $originalAspectRation = $originalWidth / $originalHeight; + + $preview = new \OC\Preview( + self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', + 150, + 150 + ); + $preview->setKeepAspect(true); + $image = $preview->getPreview(); + + $aspectRatio = $image->width() / $image->height(); + $this->assertEquals(round($originalAspectRation, 2), round($aspectRatio, 2)); + + $this->assertLessThanOrEqual(150, $image->width()); + $this->assertLessThanOrEqual(150, $image->height()); + } + + public function testKeepAspectRatioCover() { + $originalWidth = 1680; + $originalHeight = 1050; + $originalAspectRation = $originalWidth / $originalHeight; + + $preview = new \OC\Preview( + self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', + 150, + 150 + ); + $preview->setKeepAspect(true); + $preview->setMode(\OC\Preview::MODE_COVER); + $image = $preview->getPreview(); + + $aspectRatio = $image->width() / $image->height(); + $this->assertEquals(round($originalAspectRation, 2), round($aspectRatio, 2)); + + $this->assertGreaterThanOrEqual(150, $image->width()); + $this->assertGreaterThanOrEqual(150, $image->height()); + } + + public function testSetFileWithInfo() { + $info = new FileInfo('/foo', null, '/foo', ['mimetype' => 'foo/bar'], null); + $preview = new \OC\Preview(); + $preview->setFile('/foo', $info); + $this->assertEquals($info, $this->invokePrivate($preview, 'getFileInfo')); + } + + public function testIsCached() { + $sourceFile = __DIR__ . '/../data/testimage.png'; + $userId = $this->getUniqueID(); + $this->createUser($userId, 'pass'); + + $storage = new Temporary(); + $storage->mkdir('files'); + $this->registerMount($userId, $storage, '/' . $userId); + + \OC_Util::tearDownFS(); + \OC_Util::setupFS($userId); + $preview = new \OC\Preview($userId, 'files'); + $view = new View('/' . $userId . '/files'); + $view->file_put_contents('test.png', file_get_contents($sourceFile)); + $info = $view->getFileInfo('test.png'); + $preview->setFile('test.png', $info); + + $preview->setMaxX(64); + $preview->setMaxY(64); + + $this->assertFalse($preview->isCached($info->getId())); + + $preview->getPreview(); + + $this->assertEquals('thumbnails/' . $info->getId() . '/64-64.png', $preview->isCached($info->getId())); + } } diff --git a/tests/lib/repair/repairinvalidsharestest.php b/tests/lib/repair/repairinvalidsharestest.php new file mode 100644 index 00000000000..89a5ba470e1 --- /dev/null +++ b/tests/lib/repair/repairinvalidsharestest.php @@ -0,0 +1,123 @@ +<?php +/** + * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Repair; + + +use OC\Repair\RepairInvalidShares; +use OC\Share\Constants; +use Test\TestCase; + +/** + * Tests for repairing invalid shares + * + * @see \OC\Repair\RepairInvalidShares + */ +class RepairInvalidSharesTest extends TestCase { + + /** @var \OC\RepairStep */ + private $repair; + + /** @var \OCP\IDBConnection */ + private $connection; + + protected function setUp() { + parent::setUp(); + + $config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->any()) + ->method('getSystemValue') + ->with('version') + ->will($this->returnValue('8.0.0.0')); + + $this->connection = \OC::$server->getDatabaseConnection(); + $this->deleteAllShares(); + + /** @var \OCP\IConfig $config */ + $this->repair = new RepairInvalidShares($config, $this->connection); + } + + protected function tearDown() { + $this->deleteAllShares(); + + parent::tearDown(); + } + + protected function deleteAllShares() { + $qb = $this->connection->getQueryBuilder(); + $qb->delete('share')->execute(); + } + + /** + * Test remove expiration date for non-link shares + */ + public function testRemoveExpirationDateForNonLinkShares() { + // user share with bogus expiration date + $qb = $this->connection->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(Constants::SHARE_TYPE_USER), + 'share_with' => $qb->expr()->literal('recipientuser1'), + 'uid_owner' => $qb->expr()->literal('user1'), + 'item_type' => $qb->expr()->literal('folder'), + 'item_source' => $qb->expr()->literal(123), + 'item_target' => $qb->expr()->literal('/123'), + 'file_source' => $qb->expr()->literal(123), + 'file_target' => $qb->expr()->literal('/test'), + 'permissions' => $qb->expr()->literal(1), + 'stime' => $qb->expr()->literal(time()), + 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00') + ]) + ->execute(); + + // select because lastInsertId does not work with OCI + $results = $this->connection->getQueryBuilder() + ->select('id') + ->from('share') + ->execute() + ->fetchAll(); + $bogusShareId = $results[0]['id']; + + // link share with expiration date + $qb = $this->connection->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(Constants::SHARE_TYPE_LINK), + 'uid_owner' => $qb->expr()->literal('user1'), + 'item_type' => $qb->expr()->literal('folder'), + 'item_source' => $qb->expr()->literal(123), + 'item_target' => $qb->expr()->literal('/123'), + 'file_source' => $qb->expr()->literal(123), + 'file_target' => $qb->expr()->literal('/test'), + 'permissions' => $qb->expr()->literal(1), + 'stime' => $qb->expr()->literal(time()), + 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00'), + 'token' => $qb->expr()->literal('abcdefg') + ])->execute(); + + $this->repair->run(); + + $results = $this->connection->getQueryBuilder() + ->select('*') + ->from('share') + ->orderBy('share_type', 'ASC') + ->execute() + ->fetchAll(); + + $this->assertCount(2, $results); + + $userShare = $results[0]; + $linkShare = $results[1]; + $this->assertEquals($bogusShareId, $userShare['id'], 'sanity check'); + $this->assertNull($userShare['expiration'], 'bogus expiration date was removed'); + $this->assertNotNull($linkShare['expiration'], 'valid link share expiration date still there'); + } +} + diff --git a/tests/lib/repair/repairmimetypes.php b/tests/lib/repair/repairmimetypes.php index 3c100b808cf..0288a476ede 100644 --- a/tests/lib/repair/repairmimetypes.php +++ b/tests/lib/repair/repairmimetypes.php @@ -283,6 +283,68 @@ class RepairMimeTypes extends \Test\TestCase { } /** + * Test renaming the java mime types + */ + public function testRenameJavaMimeType() { + $currentMimeTypes = [ + ['test.java', 'application/octet-stream'], + ['test.class', 'application/octet-stream'], + ]; + + $fixedMimeTypes = [ + ['test.java', 'text/x-java-source'], + ['test.class', 'application/java'], + ]; + + $this->renameMimeTypes($currentMimeTypes, $fixedMimeTypes); + } + + /** + * Test renaming the hpp mime type + */ + public function testRenameHppMimeType() { + $currentMimeTypes = [ + ['test.hpp', 'application/octet-stream'], + ]; + + $fixedMimeTypes = [ + ['test.hpp', 'text/x-h'], + ]; + + $this->renameMimeTypes($currentMimeTypes, $fixedMimeTypes); + } + + /** + * Test renaming the rss mime type + */ + public function testRenameRssMimeType() { + $currentMimeTypes = [ + ['test.rss', 'application/octet-stream'], + ]; + + $fixedMimeTypes = [ + ['test.rss', 'application/rss+xml'], + ]; + + $this->renameMimeTypes($currentMimeTypes, $fixedMimeTypes); + } + + /** + * Test renaming the hpp mime type + */ + public function testRenameRtfMimeType() { + $currentMimeTypes = [ + ['test.rtf', 'application/octet-stream'], + ]; + + $fixedMimeTypes = [ + ['test.rtf', 'text/rtf'], + ]; + + $this->renameMimeTypes($currentMimeTypes, $fixedMimeTypes); + } + + /** * Test renaming and splitting old office mime types when * new ones already exist */ @@ -399,6 +461,11 @@ class RepairMimeTypes extends \Test\TestCase { ['test.cnf', 'text/plain'], ['test.yaml', 'application/yaml'], ['test.yml', 'application/yaml'], + ['test.java', 'text/x-java-source'], + ['test.class', 'application/java'], + ['test.hpp', 'text/x-h'], + ['test.rss', 'application/rss+xml'], + ['test.rtf', 'text/rtf'], ]; $fixedMimeTypes = [ @@ -438,6 +505,11 @@ class RepairMimeTypes extends \Test\TestCase { ['test.cnf', 'text/plain'], ['test.yaml', 'application/yaml'], ['test.yml', 'application/yaml'], + ['test.java', 'text/x-java-source'], + ['test.class', 'application/java'], + ['test.hpp', 'text/x-h'], + ['test.rss', 'application/rss+xml'], + ['test.rtf', 'text/rtf'], ]; $this->renameMimeTypes($currentMimeTypes, $fixedMimeTypes); diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index 58a76470afa..2ca54390e65 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -533,6 +533,39 @@ class Test_Share extends \Test\TestCase { $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result['permissions']); } + public function testSharingAFileInsideAFolderThatIsAlreadyShared() { + OC_User::setUserId($this->user1); + $view = new \OC\Files\View('/' . $this->user1 . '/'); + $view->mkdir('files/test'); + $view->mkdir('files/test/sub1'); + $view->file_put_contents('files/test/sub1/file.txt', 'abc'); + + $folderInfo = $view->getFileInfo('files/test/sub1'); + $folderId = $folderInfo->getId(); + + $fileInfo = $view->getFileInfo('files/test/sub1/file.txt'); + $fileId = $fileInfo->getId(); + + $this->assertTrue( + OCP\Share::shareItem('folder', $folderId, OCP\Share::SHARE_TYPE_GROUP, $this->group2, \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_UPDATE), + 'Failed asserting that user 1 successfully shared "test/sub1" with group 2.' + ); + + $this->assertTrue( + OCP\Share::shareItem('file', $fileId, OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ), + 'Failed asserting that user 1 successfully shared "test/sub1/file.txt" with user 2.' + ); + + $result = \OCP\Share::getItemsSharedWithUser('file', $this->user2); + $this->assertCount(2, $result); + + foreach ($result as $share) { + $itemName = substr($share['path'], strrpos($share['path'], '/')); + $this->assertSame($itemName, $share['file_target'], 'Asserting that the file_target is the last segment of the path'); + $this->assertSame($share['item_target'], '/' . $share['item_source'], 'Asserting that the item is the item that was shared'); + } + } + protected function shareUserOneTestFileWithGroupOne() { OC_User::setUserId($this->user1); $this->assertTrue( @@ -898,6 +931,43 @@ class Test_Share extends \Test\TestCase { $this->assertEmpty($expected, 'did not found all expected values'); } + public function testGetShareSubItemsWhenUserNotInGroup() { + OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ); + + $result = \OCP\Share::getItemsSharedWithUser('test', $this->user2); + $this->assertCount(1, $result); + + $groupShareId = array_keys($result)[0]; + + // remove user from group + $userObject = \OC::$server->getUserManager()->get($this->user2); + \OC::$server->getGroupManager()->get($this->group1)->removeUser($userObject); + + $result = \OCP\Share::getItemsSharedWithUser('test', $this->user2); + $this->assertCount(0, $result); + + // test with buggy data + $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $qb->insert('share') + ->values([ + 'share_type' => $qb->expr()->literal(2), // group sub-share + 'share_with' => $qb->expr()->literal($this->user2), + 'parent' => $qb->expr()->literal($groupShareId), + 'uid_owner' => $qb->expr()->literal($this->user1), + 'item_type' => $qb->expr()->literal('test'), + 'item_source' => $qb->expr()->literal('test.txt'), + 'item_target' => $qb->expr()->literal('test.txt'), + 'file_target' => $qb->expr()->literal('test2.txt'), + 'permissions' => $qb->expr()->literal(1), + 'stime' => $qb->expr()->literal(time()), + ])->execute(); + + $result = \OCP\Share::getItemsSharedWithUser('test', $this->user2); + $this->assertCount(0, $result); + + $qb->delete('share')->execute(); + } + public function testShareItemWithLink() { OC_User::setUserId($this->user1); $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ); diff --git a/tests/lib/testcase.php b/tests/lib/testcase.php index 7e4890a46c2..8cfa2a72598 100644 --- a/tests/lib/testcase.php +++ b/tests/lib/testcase.php @@ -24,6 +24,7 @@ namespace Test; use OC\Command\QueueBus; use OC\Files\Filesystem; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Security\ISecureRandom; abstract class TestCase extends \PHPUnit_Framework_TestCase { @@ -129,9 +130,11 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { public static function tearDownAfterClass() { $dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest'); + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - self::tearDownAfterClassCleanStorages(); - self::tearDownAfterClassCleanFileCache(); + self::tearDownAfterClassCleanShares($queryBuilder); + self::tearDownAfterClassCleanStorages($queryBuilder); + self::tearDownAfterClassCleanFileCache($queryBuilder); self::tearDownAfterClassCleanStrayDataFiles($dataDir); self::tearDownAfterClassCleanStrayHooks(); self::tearDownAfterClassCleanStrayLocks(); @@ -140,25 +143,33 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { } /** + * Remove all entries from the share table + * + * @param IQueryBuilder $queryBuilder + */ + static protected function tearDownAfterClassCleanShares(IQueryBuilder $queryBuilder) { + $queryBuilder->delete('share') + ->execute(); + } + + /** * Remove all entries from the storages table * - * @throws \OC\DatabaseException + * @param IQueryBuilder $queryBuilder */ - static protected function tearDownAfterClassCleanStorages() { - $sql = 'DELETE FROM `*PREFIX*storages`'; - $query = \OC_DB::prepare($sql); - $query->execute(); + static protected function tearDownAfterClassCleanStorages(IQueryBuilder $queryBuilder) { + $queryBuilder->delete('storages') + ->execute(); } /** * Remove all entries from the filecache table * - * @throws \OC\DatabaseException + * @param IQueryBuilder $queryBuilder */ - static protected function tearDownAfterClassCleanFileCache() { - $sql = 'DELETE FROM `*PREFIX*filecache`'; - $query = \OC_DB::prepare($sql); - $query->execute(); + static protected function tearDownAfterClassCleanFileCache(IQueryBuilder $queryBuilder) { + $queryBuilder->delete('filecache') + ->execute(); } /** diff --git a/tests/lib/util.php b/tests/lib/util.php index 49579b3b6bd..eaa3d0795e3 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -303,7 +303,7 @@ class Test_Util extends \Test\TestCase { } $appConfig = \OC::$server->getAppConfig(); - $appConfig->setValue('core', 'shareapi_exclude_groups_list', implode(',', $excludedGroups)); + $appConfig->setValue('core', 'shareapi_exclude_groups_list', json_encode($excludedGroups)); $appConfig->setValue('core', 'shareapi_exclude_groups', 'yes'); $result = \OCP\Util::isSharingDisabledForUser(); diff --git a/tests/settings/controller/CheckSetupControllerTest.php b/tests/settings/controller/CheckSetupControllerTest.php index 2a8a6548ebb..c453822464f 100644 --- a/tests/settings/controller/CheckSetupControllerTest.php +++ b/tests/settings/controller/CheckSetupControllerTest.php @@ -346,6 +346,7 @@ class CheckSetupControllerTest extends TestCase { ], 'forwardedForHeadersWorking' => true, 'reverseProxyDocs' => 'reverse-proxy-doc-link', + 'isCorrectMemcachedPHPModuleInstalled' => true, ] ); $this->assertEquals($expected, $this->checkSetupController->check()); |