diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2016-05-12 12:07:06 +0200 |
---|---|---|
committer | Thomas Müller <DeepDiver1975@users.noreply.github.com> | 2016-05-12 12:07:06 +0200 |
commit | b34bacd0718fa24c67a8ef0aa6f3b824a9b525bb (patch) | |
tree | 904bda1263850905c2c8164f4f1367d8c7bc9d46 /apps/files/tests/BackgroundJob | |
parent | eea98f1d74daf2a20c6b08b9df743f0478c48103 (diff) | |
download | nextcloud-server-b34bacd0718fa24c67a8ef0aa6f3b824a9b525bb.tar.gz nextcloud-server-b34bacd0718fa24c67a8ef0aa6f3b824a9b525bb.zip |
Move Files app to PSR-4 (#24569)
* Move lib/ of Files app to PSR-4
* Move tests to PSR-4
Diffstat (limited to 'apps/files/tests/BackgroundJob')
-rw-r--r-- | apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php | 259 | ||||
-rw-r--r-- | apps/files/tests/BackgroundJob/ScanFilesTest.php | 133 |
2 files changed, 392 insertions, 0 deletions
diff --git a/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php b/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php new file mode 100644 index 00000000000..e802a248a9b --- /dev/null +++ b/apps/files/tests/BackgroundJob/DeleteOrphanedItemsJobTest.php @@ -0,0 +1,259 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Vincent Petry <pvince81@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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\Tests\BackgroundJob; + +use OCA\Files\BackgroundJob\DeleteOrphanedItems; +use OCP\DB\QueryBuilder\IQueryBuilder; + +/** + * Class DeleteOrphanedItemsJobTest + * + * @group DB + * + * @package Test\BackgroundJob + */ +class DeleteOrphanedItemsJobTest extends \Test\TestCase { + + /** @var \OCP\IDBConnection */ + protected $connection; + + protected function setup() { + parent::setUp(); + $this->connection = \OC::$server->getDatabaseConnection(); + } + + protected function cleanMapping($table) { + $query = $this->connection->getQueryBuilder(); + $query->delete($table)->execute(); + } + + protected function getMappings($table) { + $query = $this->connection->getQueryBuilder(); + $query->select('*') + ->from($table); + $result = $query->execute(); + $mapping = $result->fetchAll(); + $result->closeCursor(); + + return $mapping; + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearSystemTagMappings() { + $this->cleanMapping('systemtag_object_mapping'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('systemtag_object_mapping') + ->values([ + 'objectid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'objecttype' => $query->createNamedParameter('files'), + 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('systemtag_object_mapping') + ->values([ + 'objectid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'objecttype' => $query->createNamedParameter('files'), + 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + $mapping = $this->getMappings('systemtag_object_mapping'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanSystemTags'); + + $mapping = $this->getMappings('systemtag_object_mapping'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('systemtag_object_mapping'); + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearUserTagMappings() { + $this->cleanMapping('vcategory_to_object'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('vcategory_to_object') + ->values([ + 'objid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'type' => $query->createNamedParameter('files'), + 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('vcategory_to_object') + ->values([ + 'objid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'type' => $query->createNamedParameter('files'), + 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + ])->execute(); + + $mapping = $this->getMappings('vcategory_to_object'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanUserTags'); + + $mapping = $this->getMappings('vcategory_to_object'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('vcategory_to_object'); + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearComments() { + $this->cleanMapping('comments'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments') + ->values([ + 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + 'actor_type' => $query->createNamedParameter('users'), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments') + ->values([ + 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + 'actor_type' => $query->createNamedParameter('users'), + ])->execute(); + + $mapping = $this->getMappings('comments'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanComments'); + + $mapping = $this->getMappings('comments'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('comments'); + } + + /** + * Test clearing orphaned system tag mappings + */ + public function testClearCommentReadMarks() { + $this->cleanMapping('comments_read_markers'); + + $query = $this->connection->getQueryBuilder(); + $query->insert('filecache') + ->values([ + 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT), + 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'), + 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')), + ])->execute(); + $fileId = $query->getLastInsertId(); + + // Existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments_read_markers') + ->values([ + 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + ])->execute(); + + // Non-existing file + $query = $this->connection->getQueryBuilder(); + $query->insert('comments_read_markers') + ->values([ + 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT), + 'object_type' => $query->createNamedParameter('files'), + 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT), + ])->execute(); + + $mapping = $this->getMappings('comments_read_markers'); + $this->assertCount(2, $mapping); + + $job = new DeleteOrphanedItems(); + $this->invokePrivate($job, 'cleanCommentMarkers'); + + $mapping = $this->getMappings('comments_read_markers'); + $this->assertCount(1, $mapping); + + $query = $this->connection->getQueryBuilder(); + $query->delete('filecache') + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->execute(); + $this->cleanMapping('comments_read_markers'); + } + +} diff --git a/apps/files/tests/BackgroundJob/ScanFilesTest.php b/apps/files/tests/BackgroundJob/ScanFilesTest.php new file mode 100644 index 00000000000..eab28071b70 --- /dev/null +++ b/apps/files/tests/BackgroundJob/ScanFilesTest.php @@ -0,0 +1,133 @@ +<?php +/** + * @author Lukas Reschke <lukas@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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\Tests\BackgroundJob; + +use Test\TestCase; +use OCP\IConfig; +use OCP\IUserManager; +use OCA\Files\BackgroundJob\ScanFiles; + +/** + * Class ScanFilesTest + * + * @package OCA\Files\Tests\BackgroundJob + */ +class ScanFilesTest extends TestCase { + /** @var IConfig */ + private $config; + /** @var IUserManager */ + private $userManager; + /** @var ScanFiles */ + private $scanFiles; + + public function setUp() { + parent::setUp(); + + $this->config = $this->getMock('\OCP\IConfig'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + + $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles') + ->setConstructorArgs([ + $this->config, + $this->userManager, + ]) + ->setMethods(['runScanner']) + ->getMock(); + } + + public function testRunWithoutUsers() { + $this->config + ->expects($this->at(0)) + ->method('getAppValue') + ->with('files', 'cronjob_scan_files', 0) + ->will($this->returnValue(50)); + $this->userManager + ->expects($this->at(0)) + ->method('search') + ->with('', 500, 50) + ->will($this->returnValue([])); + $this->userManager + ->expects($this->at(1)) + ->method('search') + ->with('', 500) + ->will($this->returnValue([])); + $this->config + ->expects($this->at(1)) + ->method('setAppValue') + ->with('files', 'cronjob_scan_files', 500); + + $this->invokePrivate($this->scanFiles, 'run', [[]]); + } + + public function testRunWithUsers() { + $fakeUser = $this->getMock('\OCP\IUser'); + $this->config + ->expects($this->at(0)) + ->method('getAppValue') + ->with('files', 'cronjob_scan_files', 0) + ->will($this->returnValue(50)); + $this->userManager + ->expects($this->at(0)) + ->method('search') + ->with('', 500, 50) + ->will($this->returnValue([ + $fakeUser + ])); + $this->config + ->expects($this->at(1)) + ->method('setAppValue') + ->with('files', 'cronjob_scan_files', 550); + $this->scanFiles + ->expects($this->once()) + ->method('runScanner') + ->with($fakeUser); + + $this->invokePrivate($this->scanFiles, 'run', [[]]); + } + + public function testRunWithUsersAndOffsetAtEndOfUserList() { + $this->config + ->expects($this->at(0)) + ->method('getAppValue') + ->with('files', 'cronjob_scan_files', 0) + ->will($this->returnValue(50)); + $this->userManager + ->expects($this->at(0)) + ->method('search') + ->with('', 500, 50) + ->will($this->returnValue([])); + $this->userManager + ->expects($this->at(1)) + ->method('search') + ->with('', 500) + ->will($this->returnValue([])); + $this->config + ->expects($this->at(1)) + ->method('setAppValue') + ->with('files', 'cronjob_scan_files', 500); + $this->scanFiles + ->expects($this->never()) + ->method('runScanner'); + + $this->invokePrivate($this->scanFiles, 'run', [[]]); + } + +} |