summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRoeland Douma <rullzer@users.noreply.github.com>2015-09-06 16:56:35 +0200
committerRoeland Douma <rullzer@users.noreply.github.com>2015-09-06 16:56:35 +0200
commit24f5f50b20c4f49bcd602a3c322f2ee2deb0f95b (patch)
treeb5ba1876566dfa1cc39341e8f58e419bc137d722 /tests
parent3642fb701a7fc0caba779de9f5d53bf12c27f5aa (diff)
parentc6314fc699c7316973fa79f75b7d585e620323e9 (diff)
downloadnextcloud-server-24f5f50b20c4f49bcd602a3c322f2ee2deb0f95b.tar.gz
nextcloud-server-24f5f50b20c4f49bcd602a3c322f2ee2deb0f95b.zip
Merge pull request #18742 from owncloud/mimetype-updatedb
Introduce mimetype DB update occ command
Diffstat (limited to 'tests')
-rw-r--r--tests/core/command/maintenance/mimetype/updatedbtest.php184
-rw-r--r--tests/lib/files/type/loadertest.php93
-rw-r--r--tests/lib/repair/repairmimetypes.php29
3 files changed, 291 insertions, 15 deletions
diff --git a/tests/core/command/maintenance/mimetype/updatedbtest.php b/tests/core/command/maintenance/mimetype/updatedbtest.php
new file mode 100644
index 00000000000..217301102c5
--- /dev/null
+++ b/tests/core/command/maintenance/mimetype/updatedbtest.php
@@ -0,0 +1,184 @@
+<?php
+/**
+ * @author Robin McCorkell <rmccorkell@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, 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 Tests\Core\Command\Maintenance\Mimetype;
+
+use OC\Core\Command\Maintenance\Mimetype\UpdateDB;
+use Test\TestCase;
+use OCP\Files\IMimeTypeDetector;
+use OCP\Files\IMimeTypeLoader;
+
+class UpdateDBTest extends TestCase {
+ /** @var IMimeTypeDetector */
+ protected $detector;
+ /** @var IMimeTypeLoader */
+ protected $loader;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $consoleInput;
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $consoleOutput;
+
+ /** @var \Symfony\Component\Console\Command\Command */
+ protected $command;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->detector = $this->getMockBuilder('OC\Files\Type\Detection')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->loader = $this->getMockBuilder('OC\Files\Type\Loader')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
+ $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
+
+ $this->command = new UpdateDB($this->detector, $this->loader);
+ }
+
+ public function testNoop() {
+ $this->consoleInput->method('getOption')
+ ->with('repair-filecache')
+ ->willReturn(false);
+
+ $this->detector->expects($this->once())
+ ->method('getAllMappings')
+ ->willReturn([
+ 'ext' => ['testing/existingmimetype']
+ ]);
+ $this->loader->expects($this->once())
+ ->method('exists')
+ ->with('testing/existingmimetype')
+ ->willReturn(true);
+
+ $this->loader->expects($this->never())
+ ->method('updateFilecache');
+
+ $this->consoleOutput->expects($this->at(0))
+ ->method('writeln')
+ ->with('Added 0 new mimetypes');
+ $this->consoleOutput->expects($this->at(1))
+ ->method('writeln')
+ ->with('Updated 0 filecache rows');
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+ public function testAddMimetype() {
+ $this->consoleInput->method('getOption')
+ ->with('repair-filecache')
+ ->willReturn(false);
+
+ $this->detector->expects($this->once())
+ ->method('getAllMappings')
+ ->willReturn([
+ 'ext' => ['testing/existingmimetype'],
+ 'new' => ['testing/newmimetype']
+ ]);
+ $this->loader->expects($this->exactly(2))
+ ->method('exists')
+ ->will($this->returnValueMap([
+ ['testing/existingmimetype', true],
+ ['testing/newmimetype', false],
+ ]));
+ $this->loader->expects($this->exactly(2))
+ ->method('getId')
+ ->will($this->returnValueMap([
+ ['testing/existingmimetype', 1],
+ ['testing/newmimetype', 2],
+ ]));
+
+ $this->loader->expects($this->once())
+ ->method('updateFilecache')
+ ->with('new', 2)
+ ->willReturn(3);
+
+ $this->consoleOutput->expects($this->at(0))
+ ->method('writeln')
+ ->with('Added mimetype "testing/newmimetype" to database');
+ $this->consoleOutput->expects($this->at(1))
+ ->method('writeln')
+ ->with('Updated 3 filecache rows for mimetype "testing/newmimetype"');
+
+ $this->consoleOutput->expects($this->at(2))
+ ->method('writeln')
+ ->with('Added 1 new mimetypes');
+ $this->consoleOutput->expects($this->at(3))
+ ->method('writeln')
+ ->with('Updated 3 filecache rows');
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+ public function testSkipComments() {
+ $this->detector->expects($this->once())
+ ->method('getAllMappings')
+ ->willReturn([
+ '_comment' => 'some comment in the JSON'
+ ]);
+ $this->loader->expects($this->never())
+ ->method('exists');
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+ public function testRepairFilecache() {
+ $this->consoleInput->method('getOption')
+ ->with('repair-filecache')
+ ->willReturn(true);
+
+ $this->detector->expects($this->once())
+ ->method('getAllMappings')
+ ->willReturn([
+ 'ext' => ['testing/existingmimetype'],
+ ]);
+ $this->loader->expects($this->exactly(1))
+ ->method('exists')
+ ->will($this->returnValueMap([
+ ['testing/existingmimetype', true],
+ ]));
+ $this->loader->expects($this->exactly(1))
+ ->method('getId')
+ ->will($this->returnValueMap([
+ ['testing/existingmimetype', 1],
+ ]));
+
+ $this->loader->expects($this->once())
+ ->method('updateFilecache')
+ ->with('ext', 1)
+ ->willReturn(3);
+
+ $this->consoleOutput->expects($this->at(0))
+ ->method('writeln')
+ ->with('Updated 3 filecache rows for mimetype "testing/existingmimetype"');
+
+ $this->consoleOutput->expects($this->at(1))
+ ->method('writeln')
+ ->with('Added 0 new mimetypes');
+ $this->consoleOutput->expects($this->at(2))
+ ->method('writeln')
+ ->with('Updated 3 filecache rows');
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+}
diff --git a/tests/lib/files/type/loadertest.php b/tests/lib/files/type/loadertest.php
new file mode 100644
index 00000000000..7f87afd2f4d
--- /dev/null
+++ b/tests/lib/files/type/loadertest.php
@@ -0,0 +1,93 @@
+<?php
+/**
+ * @author Robin McCorkell <rmccorkell@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, 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 OC\Files\Type;
+
+use \OC\Files\Type\Loader;
+use \OCP\IDBConnection;
+
+class LoaderTest extends \Test\TestCase {
+ /** @var IDBConnection */
+ protected $db;
+ /** @var Loader */
+ protected $loader;
+
+ protected function setUp() {
+ $this->db = \OC::$server->getDatabaseConnection();
+ $this->loader = new Loader($this->db);
+ }
+
+ protected function tearDown() {
+ $deleteMimetypes = $this->db->getQueryBuilder();
+ $deleteMimetypes->delete('mimetypes')
+ ->where($deleteMimetypes->expr()->like(
+ 'mimetype', $deleteMimetypes->createPositionalParameter('testing/%')
+ ));
+ $deleteMimetypes->execute();
+ }
+
+
+ public function testGetMimetype() {
+ $qb = $this->db->getQueryBuilder();
+ $qb->insert('mimetypes')
+ ->values([
+ 'mimetype' => $qb->createPositionalParameter('testing/mymimetype')
+ ]);
+ $qb->execute();
+
+ $this->assertTrue($this->loader->exists('testing/mymimetype'));
+ $mimetypeId = $this->loader->getId('testing/mymimetype');
+ $this->assertNotNull($mimetypeId);
+
+ $mimetype = $this->loader->getMimetypeById($mimetypeId);
+ $this->assertEquals('testing/mymimetype', $mimetype);
+ }
+
+ public function testGetNonexistentMimetype() {
+ $this->assertFalse($this->loader->exists('testing/nonexistent'));
+ // hopefully this ID doesn't exist
+ $this->assertNull($this->loader->getMimetypeById(12345));
+ }
+
+ public function testStore() {
+ $this->assertFalse($this->loader->exists('testing/mymimetype'));
+ $mimetypeId = $this->loader->getId('testing/mymimetype');
+
+ $qb = $this->db->getQueryBuilder();
+ $qb->select('mimetype')
+ ->from('mimetypes')
+ ->where($qb->expr()->eq('id', $qb->createPositionalParameter($mimetypeId)));
+
+ $mimetype = $qb->execute()->fetch();
+ $this->assertEquals('testing/mymimetype', $mimetype['mimetype']);
+
+ $this->assertEquals('testing/mymimetype', $this->loader->getMimetypeById($mimetypeId));
+ $this->assertEquals($mimetypeId, $this->loader->getId('testing/mymimetype'));
+ }
+
+ public function testStoreExists() {
+ $mimetypeId = $this->loader->getId('testing/mymimetype');
+ $mimetypeId2 = $this->loader->getId('testing/mymimetype');
+
+ $this->assertEquals($mimetypeId, $mimetypeId2);
+ }
+
+}
diff --git a/tests/lib/repair/repairmimetypes.php b/tests/lib/repair/repairmimetypes.php
index 40ffc14612b..76d4d7cc656 100644
--- a/tests/lib/repair/repairmimetypes.php
+++ b/tests/lib/repair/repairmimetypes.php
@@ -22,8 +22,17 @@ class RepairMimeTypes extends \Test\TestCase {
protected function setUp() {
parent::setUp();
- $this->storage = new \OC\Files\Storage\Temporary([]);
+ $this->savedMimetypeLoader = \OC::$server->getMimeTypeLoader();
+ $this->mimetypeLoader = $this->getMockBuilder('\OC\Files\Type\Loader')
+ ->setConstructorArgs([\OC::$server->getDatabaseConnection()])
+ ->setMethods(null)
+ ->getMock();
+ \OC::$server->registerService('MimeTypeLoader', function ($c) {
+ return $this->mimetypeLoader;
+ });
+
+ $this->storage = new \OC\Files\Storage\Temporary([]);
$this->repair = new \OC\Repair\RepairMimeTypes();
}
@@ -33,7 +42,9 @@ class RepairMimeTypes extends \Test\TestCase {
\OC_DB::executeAudited($sql, [$this->storage->getId()]);
$this->clearMimeTypes();
- DummyFileCache::clearCachedMimeTypes();
+ \OC::$server->registerService('MimeTypeLoader', function($c) {
+ return $this->savedMimetypeLoader;
+ });
parent::tearDown();
}
@@ -86,8 +97,7 @@ class RepairMimeTypes extends \Test\TestCase {
$this->repair->run();
// force mimetype reload
- DummyFileCache::clearCachedMimeTypes();
- $this->storage->getCache()->loadMimeTypes();
+ self::invokePrivate($this->mimetypeLoader, 'loadMimetypes');
$this->checkEntries($fixedMimeTypes);
}
@@ -434,14 +444,3 @@ class RepairMimeTypes extends \Test\TestCase {
}
}
-/**
- * Dummy class to access protected members
- */
-class DummyFileCache extends \OC\Files\Cache\Cache {
-
- public static function clearCachedMimeTypes() {
- self::$mimetypeIds = [];
- self::$mimetypes = [];
- }
-}
-