diff --git a/tests/core/command/maintenance/mimetype/updatedbtest.php b/tests/core/command/maintenance/mimetype/updatedbtest.php new file mode 100644 index 0000000000..217301102c --- /dev/null +++ b/tests/core/command/maintenance/mimetype/updatedbtest.php @@ -0,0 +1,184 @@ + + * + * @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 + * + */ + +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 0000000000..7f87afd2f4 --- /dev/null +++ b/tests/lib/files/type/loadertest.php @@ -0,0 +1,93 @@ + + * + * @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 + * + */ + +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); + } + +}