2014-06-25 21:34:39 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the converting of legacy storages to home storages.
|
|
|
|
*
|
|
|
|
* @see \OC\Repair\RepairMimeTypes
|
|
|
|
*/
|
|
|
|
class TestRepairMimeTypes extends PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
/** @var \OC\RepairStep */
|
|
|
|
private $repair;
|
|
|
|
|
|
|
|
private $storage;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
$this->storage = new \OC\Files\Storage\Temporary(array());
|
|
|
|
|
|
|
|
$this->repair = new \OC\Repair\RepairMimeTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
$this->storage->getCache()->clear();
|
2014-06-26 21:24:46 +04:00
|
|
|
$sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?';
|
2014-06-25 21:34:39 +04:00
|
|
|
\OC_DB::executeAudited($sql, array($this->storage->getId()));
|
|
|
|
$this->clearMimeTypes();
|
|
|
|
|
|
|
|
DummyFileCache::clearCachedMimeTypes();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function clearMimeTypes() {
|
|
|
|
$sql = 'DELETE FROM `*PREFIX*mimetypes`';
|
|
|
|
\OC_DB::executeAudited($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addEntries($entries) {
|
|
|
|
// create files for the different extensions, this
|
|
|
|
// will also automatically create the corresponding mime types
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$this->storage->getCache()->put(
|
|
|
|
$entry[0],
|
|
|
|
array(
|
|
|
|
'size' => 0,
|
|
|
|
'mtime' => 0,
|
|
|
|
'mimetype' => $entry[1]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkEntries($entries) {
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$data = $this->storage->getCache()->get($entry[0]);
|
|
|
|
$this->assertEquals($entry[1], $data['mimetype']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:40:12 +04:00
|
|
|
/**
|
|
|
|
* Returns the id of a given mime type or null
|
|
|
|
* if it does not exist.
|
|
|
|
*/
|
|
|
|
private function getMimeTypeIdFromDB($mimeType) {
|
2014-06-26 21:24:46 +04:00
|
|
|
$sql = 'SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?';
|
2014-06-26 18:40:12 +04:00
|
|
|
$results = \OC_DB::executeAudited($sql, array($mimeType));
|
|
|
|
$result = $results->fetchOne();
|
|
|
|
if ($result) {
|
|
|
|
return $result['id'];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-06-25 21:34:39 +04:00
|
|
|
/**
|
|
|
|
* Test renaming and splitting old office mime types
|
|
|
|
*/
|
|
|
|
public function testRenameOfficeMimeTypes() {
|
|
|
|
$this->addEntries(
|
|
|
|
array(
|
|
|
|
array('test.doc', 'application/msword'),
|
|
|
|
array('test.docx', 'application/msword'),
|
|
|
|
array('test.xls', 'application/msexcel'),
|
|
|
|
array('test.xlsx', 'application/msexcel'),
|
|
|
|
array('test.ppt', 'application/mspowerpoint'),
|
|
|
|
array('test.pptx', 'application/mspowerpoint'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
// force mimetype reload
|
2014-06-26 18:40:12 +04:00
|
|
|
DummyFileCache::clearCachedMimeTypes();
|
2014-06-25 21:34:39 +04:00
|
|
|
$this->storage->getCache()->loadMimeTypes();
|
|
|
|
|
|
|
|
$this->checkEntries(
|
|
|
|
array(
|
|
|
|
array('test.doc', 'application/msword'),
|
|
|
|
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
2014-06-26 18:40:12 +04:00
|
|
|
array('test.xls', 'application/vnd.ms-excel'),
|
|
|
|
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
|
|
|
|
array('test.ppt', 'application/vnd.ms-powerpoint'),
|
|
|
|
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
|
2014-06-25 21:34:39 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-06-25 21:35:05 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test renaming and splitting old office mime types when
|
|
|
|
* new ones already exist
|
|
|
|
*/
|
|
|
|
public function testRenameOfficeMimeTypesWhenExist() {
|
|
|
|
$this->addEntries(
|
|
|
|
array(
|
|
|
|
array('test.doc', 'application/msword'),
|
|
|
|
array('test.docx', 'application/msword'),
|
|
|
|
array('test.xls', 'application/msexcel'),
|
|
|
|
array('test.xlsx', 'application/msexcel'),
|
|
|
|
array('test.ppt', 'application/mspowerpoint'),
|
|
|
|
array('test.pptx', 'application/mspowerpoint'),
|
|
|
|
// make it so that the new mimetypes already exist
|
|
|
|
array('bogus.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
|
|
|
array('bogus.xlsx', 'application/vnd.ms-excel'),
|
|
|
|
array('bogus.pptx', 'application/vnd.ms-powerpoint'),
|
|
|
|
array('bogus2.docx', 'application/wrong'),
|
|
|
|
array('bogus2.xlsx', 'application/wrong'),
|
|
|
|
array('bogus2.pptx', 'application/wrong'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
// force mimetype reload
|
2014-06-26 18:40:12 +04:00
|
|
|
DummyFileCache::clearCachedMimeTypes();
|
2014-06-25 21:35:05 +04:00
|
|
|
$this->storage->getCache()->loadMimeTypes();
|
|
|
|
|
|
|
|
$this->checkEntries(
|
|
|
|
array(
|
|
|
|
array('test.doc', 'application/msword'),
|
|
|
|
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
2014-06-26 18:40:12 +04:00
|
|
|
array('test.xls', 'application/vnd.ms-excel'),
|
|
|
|
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
|
|
|
|
array('test.ppt', 'application/vnd.ms-powerpoint'),
|
|
|
|
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
|
2014-06-25 21:35:05 +04:00
|
|
|
array('bogus.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
2014-06-26 18:40:12 +04:00
|
|
|
array('bogus.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
|
|
|
|
array('bogus.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
|
2014-06-25 21:35:05 +04:00
|
|
|
array('bogus2.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
2014-06-26 18:40:12 +04:00
|
|
|
array('bogus2.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
|
|
|
|
array('bogus2.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// wrong mimetypes are gone
|
|
|
|
$this->assertNull($this->getMimeTypeIdFromDB('application/msexcel'));
|
|
|
|
$this->assertNull($this->getMimeTypeIdFromDB('application/mspowerpoint'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that nothing happens and no error happens when all mimetypes are
|
|
|
|
* already correct and no old ones exist..
|
|
|
|
*/
|
|
|
|
public function testDoNothingWhenOnlyNewFiles() {
|
|
|
|
$this->addEntries(
|
|
|
|
array(
|
|
|
|
array('test.doc', 'application/msword'),
|
|
|
|
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
|
|
|
array('test.xls', 'application/vnd.ms-excel'),
|
|
|
|
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
|
|
|
|
array('test.ppt', 'application/vnd.ms-powerpoint'),
|
|
|
|
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
// force mimetype reload
|
|
|
|
DummyFileCache::clearCachedMimeTypes();
|
|
|
|
$this->storage->getCache()->loadMimeTypes();
|
|
|
|
|
|
|
|
$this->checkEntries(
|
|
|
|
array(
|
|
|
|
array('test.doc', 'application/msword'),
|
|
|
|
array('test.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
|
|
|
array('test.xls', 'application/vnd.ms-excel'),
|
|
|
|
array('test.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
|
|
|
|
array('test.ppt', 'application/vnd.ms-powerpoint'),
|
|
|
|
array('test.pptx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'),
|
2014-06-25 21:35:05 +04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-06-25 21:34:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dummy class to access protected members
|
|
|
|
*/
|
|
|
|
class DummyFileCache extends \OC\Files\Cache\Cache {
|
|
|
|
|
|
|
|
public static function clearCachedMimeTypes() {
|
|
|
|
self::$mimetypeIds = array();
|
|
|
|
self::$mimetypes = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|