From c7fdf00e8497af9804b0cfd4fa081940bf53bc96 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Fri, 21 Jun 2013 14:24:52 +0200 Subject: add unit tests for preview lib to make @DeepDiver1975 happy --- tests/lib/preview.php | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/lib/preview.php (limited to 'tests') diff --git a/tests/lib/preview.php b/tests/lib/preview.php new file mode 100644 index 00000000000..2599da400c8 --- /dev/null +++ b/tests/lib/preview.php @@ -0,0 +1,108 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test; + +class Preview extends \PHPUnit_Framework_TestCase { + + public function testIsPreviewDeleted() { + $user = $this->initFS(); + + $rootView = new \OC\Files\View(''); + $rootView->mkdir('/'.$user); + $rootView->mkdir('/'.$user.'/files'); + + $samplefile = '/'.$user.'/files/test.txt'; + + $rootView->file_put_contents($samplefile, 'dummy file data'); + + $x = 50; + $y = 50; + + $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y); + $preview->getPreview(); + + $fileinfo = $rootView->getFileInfo($samplefile); + $fileid = $fileinfo['fileid']; + + $thumbcachefile = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $x . '-' . $y . '.png'; + + $this->assertEquals($rootView->file_exists($thumbcachefile), true); + + $preview->deletePreview(); + + $this->assertEquals($rootView->file_exists($thumbcachefile), false); + } + + public function testAreAllPreviewsDeleted() { + $user = $this->initFS(); + + $rootView = new \OC\Files\View(''); + $rootView->mkdir('/'.$user); + $rootView->mkdir('/'.$user.'/files'); + + $samplefile = '/'.$user.'/files/test.txt'; + + $rootView->file_put_contents($samplefile, 'dummy file data'); + + $x = 50; + $y = 50; + + $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y); + $preview->getPreview(); + + $fileinfo = $rootView->getFileInfo($samplefile); + $fileid = $fileinfo['fileid']; + + $thumbcachefolder = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/'; + + $this->assertEquals($rootView->is_dir($thumbcachefolder), true); + + $preview->deleteAllPreviews(); + + $this->assertEquals($rootView->is_dir($thumbcachefolder), false); + } + + public function testIsMaxSizeWorking() { + $user = $this->initFS(); + + $maxX = 250; + $maxY = 250; + + \OC_Config::getValue('preview_max_x', $maxX); + \OC_Config::getValue('preview_max_y', $maxY); + + $rootView = new \OC\Files\View(''); + $rootView->mkdir('/'.$user); + $rootView->mkdir('/'.$user.'/files'); + + $samplefile = '/'.$user.'/files/test.txt'; + + $rootView->file_put_contents($samplefile, 'dummy file data'); + + $preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000); + $image = $preview->getPreview(); + + $this->assertEquals($image->width(), $maxX); + $this->assertEquals($image->height(), $maxY); + } + + private function initFS() { + if(\OC\Files\Filesystem::getView()){ + $user = \OC_User::getUser(); + }else{ + $user=uniqid(); + \OC_User::setUserId($user); + \OC\Files\Filesystem::init($user, '/'.$user.'/files'); + } + + \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/'); + + return $user; + } +} \ No newline at end of file -- cgit v1.2.3 From 89554bd917f9cbc7d16cc31b754a47d9f942b0b1 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 11 Jul 2013 13:39:10 +0200 Subject: it's setValue not getValue, damn type --- tests/lib/preview.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/lib/preview.php b/tests/lib/preview.php index 2599da400c8..c4894f848f6 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -74,8 +74,8 @@ class Preview extends \PHPUnit_Framework_TestCase { $maxX = 250; $maxY = 250; - \OC_Config::getValue('preview_max_x', $maxX); - \OC_Config::getValue('preview_max_y', $maxY); + \OC_Config::setValue('preview_max_x', $maxX); + \OC_Config::setValue('preview_max_y', $maxY); $rootView = new \OC\Files\View(''); $rootView->mkdir('/'.$user); @@ -87,7 +87,10 @@ class Preview extends \PHPUnit_Framework_TestCase { $preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000); $image = $preview->getPreview(); - + + var_dump($image->width()); + var_dump($image->height()); + $this->assertEquals($image->width(), $maxX); $this->assertEquals($image->height(), $maxY); } -- cgit v1.2.3 From 7f3dbb6936cded830cbbf135f887a84ebd50b77c Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 11 Jul 2013 13:41:09 +0200 Subject: remove debug code ... --- tests/lib/preview.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/lib/preview.php b/tests/lib/preview.php index c4894f848f6..bebdc12b500 100644 --- a/tests/lib/preview.php +++ b/tests/lib/preview.php @@ -87,10 +87,7 @@ class Preview extends \PHPUnit_Framework_TestCase { $preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000); $image = $preview->getPreview(); - - var_dump($image->width()); - var_dump($image->height()); - + $this->assertEquals($image->width(), $maxX); $this->assertEquals($image->height(), $maxY); } -- cgit v1.2.3 From 670242c731ac8d832773ac0fe86813061dcf0768 Mon Sep 17 00:00:00 2001 From: kondou Date: Fri, 2 Aug 2013 11:46:47 +0200 Subject: Add \OC_Appconfig Unittest --- tests/lib/appconfig.php | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/lib/appconfig.php (limited to 'tests') diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php new file mode 100644 index 00000000000..c73e528a277 --- /dev/null +++ b/tests/lib/appconfig.php @@ -0,0 +1,126 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_Appconfig extends PHPUnit_Framework_TestCase { + private function fillDb() { + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)'); + + $query->execute(array('testapp', 'enabled', 'true')); + $query->execute(array('testapp', 'installed_version', '1.2.3')); + $query->execute(array('testapp', 'depends_on', 'someapp')); + $query->execute(array('testapp', 'deletethis', 'deletethis')); + $query->execute(array('testapp', 'key', 'value')); + + $query->execute(array('someapp', 'key', 'value')); + $query->execute(array('someapp', 'otherkey', 'othervalue')); + + $query->execute(array('123456', 'key', 'value')); + $query->execute(array('123456', 'enabled', 'false')); + + $query->execute(array('anotherapp', 'key', 'value')); + $query->execute(array('anotherapp', 'enabled', 'false')); + } + + public function testGetApps() { + $this->fillDb(); + + $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig`'); + $result = $query->execute(); + $expected = array(); + while ($row = $result->fetchRow()) { + $expected[] = $row['appid']; + } + $apps = \OC_Appconfig::getApps(); + $this->assertEquals($expected, $apps); + } + + public function testGetKeys() { + $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $result = $query->execute(array('testapp')); + $expected = array(); + while($row = $result->fetchRow()) { + $expected[] = $row["configkey"]; + } + $keys = \OC_Appconfig::getKeys('testapp'); + $this->assertEquals($expected, $keys); + } + + public function testGetValue() { + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('testapp', 'installed_version')); + $expected = $result->fetchRow(); + $value = \OC_Appconfig::getValue('testapp', 'installed_version'); + $this->assertEquals($expected['configvalue'], $value); + + $value = \OC_Appconfig::getValue('testapp', 'nonexistant'); + $this->assertNull($value); + + $value = \OC_Appconfig::getValue('testapp', 'nonexistant', 'default'); + $this->assertEquals('default', $value); + } + + public function testHasKey() { + $value = \OC_Appconfig::hasKey('testapp', 'installed_version'); + $this->assertTrue($value); + + $value = \OC_Appconfig::hasKey('nonexistant', 'nonexistant'); + $this->assertFalse($value); + } + + public function testSetValue() { + \OC_Appconfig::setValue('testapp', 'installed_version', '1.33.7'); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('testapp', 'installed_version')); + $value = $result->fetchRow(); + $this->assertEquals('1.33.7', $value['configvalue']); + + \OC_Appconfig::setValue('someapp', 'somekey', 'somevalue'); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $result = $query->execute(array('someapp', 'somekey')); + $value = $result->fetchRow(); + $this->assertEquals('somevalue', $value['configvalue']); + } + + public function testDeleteKey() { + \OC_Appconfig::deleteKey('testapp', 'deletethis'); + $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); + $query->execute(array('testapp', 'deletethis')); + $result = (bool)$query->fetchRow(); + $this->assertFalse($result); + } + + public function testDeleteApp() { + \OC_Appconfig::deleteApp('someapp'); + $query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('someapp')); + $result = (bool)$query->fetchRow(); + $this->assertFalse($result); + } + + public function testGetValues() { + $this->assertFalse(\OC_Appconfig::getValues('testapp', 'enabled')); + + $query = \OC_DB::prepare('SELECT `configkey`, `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('testapp')); + $expected = array(); + while ($row = $query->fetchRow()) { + $expected[$row['configkey']] = $row['configvalue']; + } + $values = \OC_Appconfig::getValues('testapp', false); + $this->assertEquals($expected, $values); + + $query = \OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = ?'); + $query->execute(array('enabled')); + $expected = array(); + while ($row = $query->fetchRow()) { + $expected[$row['appid']] = $row['configvalue']; + } + $values = \OC_Appconfig::getValues(false, 'enabled'); + $this->assertEquals($expected, $values); + } +} -- cgit v1.2.3 From c74f3d0b90227f95c1b6d21bbb7ee28561b67446 Mon Sep 17 00:00:00 2001 From: kondou Date: Fri, 2 Aug 2013 15:59:33 +0200 Subject: Add null and emptystring tests to check NOT NULL --- tests/lib/appconfig.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'tests') diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index c73e528a277..ae08877bd7f 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -86,6 +86,20 @@ class Test_Appconfig extends PHPUnit_Framework_TestCase { $this->assertEquals('somevalue', $value['configvalue']); } + /** + * @expectedException \Doctrine\DBAL\DBALException + */ + public function testSetValueNull() { + \OC_Appconfig::setValue('testapp', 'installed_version', null); + } + + /** + * @expectedException \Doctrine\DBAL\DBALException + */ + public function testSetValueEmptyString() { + \OC_Appconfig::setValue('testapp', '', '1.33.7'); + } + public function testDeleteKey() { \OC_Appconfig::deleteKey('testapp', 'deletethis'); $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); -- cgit v1.2.3 From 56549dafce474d74f32d33f6bf510ec7514283e1 Mon Sep 17 00:00:00 2001 From: kondou Date: Fri, 2 Aug 2013 21:27:33 +0200 Subject: Revert "Add null and emptystring tests to check NOT NULL" This reverts commit c74f3d0b90227f95c1b6d21bbb7ee28561b67446. --- tests/lib/appconfig.php | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'tests') diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index ae08877bd7f..c73e528a277 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -86,20 +86,6 @@ class Test_Appconfig extends PHPUnit_Framework_TestCase { $this->assertEquals('somevalue', $value['configvalue']); } - /** - * @expectedException \Doctrine\DBAL\DBALException - */ - public function testSetValueNull() { - \OC_Appconfig::setValue('testapp', 'installed_version', null); - } - - /** - * @expectedException \Doctrine\DBAL\DBALException - */ - public function testSetValueEmptyString() { - \OC_Appconfig::setValue('testapp', '', '1.33.7'); - } - public function testDeleteKey() { \OC_Appconfig::deleteKey('testapp', 'deletethis'); $query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?'); -- cgit v1.2.3 From d70a4a960da243a490c9b3211fb91f59864f5ba5 Mon Sep 17 00:00:00 2001 From: kondou Date: Tue, 6 Aug 2013 17:30:58 +0200 Subject: Use setUpBeforeClass() and tearDownAfterClass() --- tests/lib/appconfig.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/lib/appconfig.php b/tests/lib/appconfig.php index c73e528a277..4d82cd5ba7b 100644 --- a/tests/lib/appconfig.php +++ b/tests/lib/appconfig.php @@ -7,7 +7,7 @@ */ class Test_Appconfig extends PHPUnit_Framework_TestCase { - private function fillDb() { + public static function setUpBeforeClass() { $query = \OC_DB::prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)'); $query->execute(array('testapp', 'enabled', 'true')); @@ -26,9 +26,15 @@ class Test_Appconfig extends PHPUnit_Framework_TestCase { $query->execute(array('anotherapp', 'enabled', 'false')); } - public function testGetApps() { - $this->fillDb(); + public static function tearDownAfterClass() { + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?'); + $query->execute(array('testapp')); + $query->execute(array('someapp')); + $query->execute(array('123456')); + $query->execute(array('anotherapp')); + } + public function testGetApps() { $query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig`'); $result = $query->execute(); $expected = array(); -- cgit v1.2.3 From d3a69bf4c6baba563beceb225b9192a4e22c9c59 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Thu, 8 Aug 2013 11:04:40 +0200 Subject: adding unit tests to determine length --- lib/connector/sabre/quotaplugin.php | 2 +- tests/lib/connector/sabre/quotaplugin.php | 47 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/lib/connector/sabre/quotaplugin.php (limited to 'tests') diff --git a/lib/connector/sabre/quotaplugin.php b/lib/connector/sabre/quotaplugin.php index 0f428b75b19..730a86666be 100644 --- a/lib/connector/sabre/quotaplugin.php +++ b/lib/connector/sabre/quotaplugin.php @@ -57,7 +57,7 @@ class OC_Connector_Sabre_QuotaPlugin extends Sabre_DAV_ServerPlugin { return true; } - private function getLength() + public function getLength() { $expected = $this->server->httpRequest->getHeader('X-Expected-Entity-Length'); if ($expected) diff --git a/tests/lib/connector/sabre/quotaplugin.php b/tests/lib/connector/sabre/quotaplugin.php new file mode 100644 index 00000000000..9582af6ec4e --- /dev/null +++ b/tests/lib/connector/sabre/quotaplugin.php @@ -0,0 +1,47 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_OC_Connector_Sabre_QuotaPlugin extends PHPUnit_Framework_TestCase { + + /** + * @var Sabre_DAV_Server + */ + private $server; + + /** + * @var OC_Connector_Sabre_QuotaPlugin + */ + private $plugin; + + public function setUp() { + $this->server = new Sabre_DAV_Server(); + $this->plugin = new OC_Connector_Sabre_QuotaPlugin(); + $this->plugin->initialize($this->server); + } + + /** + * @dataProvider lengthProvider + */ + public function testLength($expected, $headers) + { + $this->server->httpRequest = new Sabre_HTTP_Request($headers); + $length = $this->plugin->getLength(); + $this->assertEquals($expected, $length); + } + + public function lengthProvider() + { + return array( + array(null, array()), + array(1024, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), + array(512, array('HTTP_CONTENT_LENGTH' => '512')), + array(2048, array('HTTP_OC_TOTAL_LENGTH' => '2048', 'HTTP_CONTENT_LENGTH' => '1024')), + ); + } + +} -- cgit v1.2.3 From fed1792510ff11941765783653573f45fadc4c70 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Thu, 8 Aug 2013 13:33:00 +0200 Subject: adding unit tests for quota checks --- lib/connector/sabre/quotaplugin.php | 75 ++++++++++++++++++------------- tests/lib/connector/sabre/quotaplugin.php | 56 ++++++++++++++++++++++- 2 files changed, 98 insertions(+), 33 deletions(-) (limited to 'tests') diff --git a/lib/connector/sabre/quotaplugin.php b/lib/connector/sabre/quotaplugin.php index 730a86666be..eb95a839b86 100644 --- a/lib/connector/sabre/quotaplugin.php +++ b/lib/connector/sabre/quotaplugin.php @@ -3,45 +3,55 @@ /** * This plugin check user quota and deny creating files when they exceeds the quota. * - * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved. * @author Sergio Cambra + * @copyright Copyright (C) 2012 entreCables S.L. All rights reserved. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License */ class OC_Connector_Sabre_QuotaPlugin extends Sabre_DAV_ServerPlugin { /** - * Reference to main server object - * - * @var Sabre_DAV_Server - */ + * Reference to main server object + * + * @var Sabre_DAV_Server + */ private $server; /** - * This initializes the plugin. - * - * This function is called by Sabre_DAV_Server, after - * addPlugin is called. - * - * This method should set up the requires event subscriptions. - * - * @param Sabre_DAV_Server $server - * @return void - */ + * is kept public to allow overwrite for unit testing + * + * @var \OC\Files\View + */ + public $fileView; + + /** + * This initializes the plugin. + * + * This function is called by Sabre_DAV_Server, after + * addPlugin is called. + * + * This method should set up the requires event subscriptions. + * + * @param Sabre_DAV_Server $server + * @return void + */ public function initialize(Sabre_DAV_Server $server) { - $this->server = $server; - $this->server->subscribeEvent('beforeWriteContent', array($this, 'checkQuota'), 10); - $this->server->subscribeEvent('beforeCreateFile', array($this, 'checkQuota'), 10); + $this->server = $server; + $server->subscribeEvent('beforeWriteContent', array($this, 'checkQuota'), 10); + $server->subscribeEvent('beforeCreateFile', array($this, 'checkQuota'), 10); + + // initialize fileView + $this->fileView = \OC\Files\Filesystem::getView(); } /** - * This method is called before any HTTP method and forces users to be authenticated - * - * @param string $method - * @throws Sabre_DAV_Exception - * @return bool - */ + * This method is called before any HTTP method and validates there is enough free space to store the file + * + * @param string $method + * @throws Sabre_DAV_Exception + * @return bool + */ public function checkQuota($uri, $data = null) { $length = $this->getLength(); if ($length) { @@ -49,7 +59,7 @@ class OC_Connector_Sabre_QuotaPlugin extends Sabre_DAV_ServerPlugin { $uri='/'.$uri; } list($parentUri, $newName) = Sabre_DAV_URLUtil::splitPath($uri); - $freeSpace = \OC\Files\Filesystem::free_space($parentUri); + $freeSpace = $this->fileView->free_space($parentUri); if ($freeSpace !== \OC\Files\FREE_SPACE_UNKNOWN && $length > $freeSpace) { throw new Sabre_DAV_Exception_InsufficientStorage(); } @@ -59,15 +69,16 @@ class OC_Connector_Sabre_QuotaPlugin extends Sabre_DAV_ServerPlugin { public function getLength() { - $expected = $this->server->httpRequest->getHeader('X-Expected-Entity-Length'); - if ($expected) - return $expected; - - $length = $this->server->httpRequest->getHeader('Content-Length'); - $ocLength = $this->server->httpRequest->getHeader('OC-Total-Length'); + $req = $this->server->httpRequest; + $length = $req->getHeader('X-Expected-Entity-Length'); + if (!$length) { + $length = $req->getHeader('Content-Length'); + } - if ($length && $ocLength) + $ocLength = $req->getHeader('OC-Total-Length'); + if ($length && $ocLength) { return max($length, $ocLength); + } return $length; } diff --git a/tests/lib/connector/sabre/quotaplugin.php b/tests/lib/connector/sabre/quotaplugin.php index 9582af6ec4e..1186de28742 100644 --- a/tests/lib/connector/sabre/quotaplugin.php +++ b/tests/lib/connector/sabre/quotaplugin.php @@ -34,14 +34,68 @@ class Test_OC_Connector_Sabre_QuotaPlugin extends PHPUnit_Framework_TestCase { $this->assertEquals($expected, $length); } - public function lengthProvider() + /** + * @dataProvider quotaOkayProvider + */ + public function testCheckQuota($quota, $headers) { + $this->plugin->fileView = $this->buildFileViewMock($quota); + + $this->server->httpRequest = new Sabre_HTTP_Request($headers); + $result = $this->plugin->checkQuota(''); + $this->assertTrue($result); + } + + /** + * @expectedException Sabre_DAV_Exception_InsufficientStorage + * @dataProvider quotaExceededProvider + */ + public function testCheckExceededQuota($quota, $headers) + { + $this->plugin->fileView = $this->buildFileViewMock($quota); + + $this->server->httpRequest = new Sabre_HTTP_Request($headers); + $this->plugin->checkQuota(''); + } + + public function quotaOkayProvider() { + return array( + array(1024, array()), + array(1024, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), + array(1024, array('HTTP_CONTENT_LENGTH' => '512')), + array(1024, array('HTTP_OC_TOTAL_LENGTH' => '1024', 'HTTP_CONTENT_LENGTH' => '512')), + // OC\Files\FREE_SPACE_UNKNOWN = -2 + array(-2, array()), + array(-2, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), + array(-2, array('HTTP_CONTENT_LENGTH' => '512')), + array(-2, array('HTTP_OC_TOTAL_LENGTH' => '1024', 'HTTP_CONTENT_LENGTH' => '512')), + ); + } + + public function quotaExceededProvider() { + return array( + array(1023, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), + array(511, array('HTTP_CONTENT_LENGTH' => '512')), + array(2047, array('HTTP_OC_TOTAL_LENGTH' => '2048', 'HTTP_CONTENT_LENGTH' => '1024')), + ); + } + + public function lengthProvider() { return array( array(null, array()), array(1024, array('HTTP_X_EXPECTED_ENTITY_LENGTH' => '1024')), array(512, array('HTTP_CONTENT_LENGTH' => '512')), array(2048, array('HTTP_OC_TOTAL_LENGTH' => '2048', 'HTTP_CONTENT_LENGTH' => '1024')), + array(4096, array('HTTP_OC_TOTAL_LENGTH' => '2048', 'HTTP_X_EXPECTED_ENTITY_LENGTH' => '4096')), ); } + private function buildFileViewMock($quota) { + // mock filesysten + $view = $this->getMock('\OC\Files\View', array('free_space'), array(), '', FALSE); + $view->expects($this->any())->method('free_space')->withAnyParameters()->will($this->returnValue($quota)); + + return $view; + } + } -- cgit v1.2.3 From 1f5a55ddff8c5339b849d91c24722b3a3e367a2c Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Mon, 26 Aug 2013 17:46:31 +0200 Subject: consolidate validity check for users in group class --- lib/group/group.php | 47 ++++++++++++++++++++++++----------------------- tests/lib/group/group.php | 10 +++++----- 2 files changed, 29 insertions(+), 28 deletions(-) (limited to 'tests') diff --git a/lib/group/group.php b/lib/group/group.php index bb1537b5c66..bcd2419b309 100644 --- a/lib/group/group.php +++ b/lib/group/group.php @@ -62,7 +62,6 @@ class Group { return $this->users; } - $users = array(); $userIds = array(); foreach ($this->backends as $backend) { $diff = array_diff( @@ -74,14 +73,8 @@ class Group { } } - foreach ($userIds as $userId) { - $user = $this->userManager->get($userId); - if(!is_null($user)) { - $users[] = $user; - } - } - $this->users = $users; - return $users; + $this->users = $this->getVerifiedUsers($userIds); + return $this->users; } /** @@ -116,7 +109,7 @@ class Group { if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) { $backend->addToGroup($user->getUID(), $this->gid); if ($this->users) { - $this->users[] = $user; + $this->users[$user->getUID()] = $user; } if ($this->emitter) { $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); @@ -175,12 +168,7 @@ class Group { if (!is_null($offset)) { $offset -= count($userIds); } - foreach ($userIds as $userId) { - $user = $this->userManager->get($userId); - if(!is_null($user)) { - $users[$userId] = $user; - } - } + $users += $this->getVerifiedUsers($userIds); if (!is_null($limit) and $limit <= 0) { return array_values($users); } @@ -197,7 +185,6 @@ class Group { * @return \OC\User\User[] */ public function searchDisplayName($search, $limit = null, $offset = null) { - $users = array(); foreach ($this->backends as $backend) { if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) { $userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset)); @@ -210,12 +197,7 @@ class Group { if (!is_null($offset)) { $offset -= count($userIds); } - foreach ($userIds as $userId) { - $user = $this->userManager->get($userId); - if(!is_null($user)) { - $users[$userId] = $user; - } - } + $users = $this->getVerifiedUsers($userIds); if (!is_null($limit) and $limit <= 0) { return array_values($users); } @@ -244,4 +226,23 @@ class Group { } return $result; } + + /** + * @brief returns all the Users from an array that really exists + * @param $userIds an array containing user IDs + * @return an Array with the userId as Key and \OC\User\User as value + */ + private function getVerifiedUsers($userIds) { + if(!is_array($userIds)) { + return array(); + } + $users = array(); + foreach ($userIds as $userId) { + $user = $this->userManager->get($userId); + if(!is_null($user)) { + $users[$userId] = $user; + } + } + return $users; + } } diff --git a/tests/lib/group/group.php b/tests/lib/group/group.php index 75e975d9e65..f1fda3b9288 100644 --- a/tests/lib/group/group.php +++ b/tests/lib/group/group.php @@ -43,8 +43,8 @@ class Group extends \PHPUnit_Framework_TestCase { $users = $group->getUsers(); $this->assertEquals(2, count($users)); - $user1 = $users[0]; - $user2 = $users[1]; + $user1 = $users['user1']; + $user2 = $users['user2']; $this->assertEquals('user1', $user1->getUID()); $this->assertEquals('user2', $user2->getUID()); } @@ -68,9 +68,9 @@ class Group extends \PHPUnit_Framework_TestCase { $users = $group->getUsers(); $this->assertEquals(3, count($users)); - $user1 = $users[0]; - $user2 = $users[1]; - $user3 = $users[2]; + $user1 = $users['user1']; + $user2 = $users['user2']; + $user3 = $users['user3']; $this->assertEquals('user1', $user1->getUID()); $this->assertEquals('user2', $user2->getUID()); $this->assertEquals('user3', $user3->getUID()); -- cgit v1.2.3 From 0869f9b655ddd0b285629256521d7eafde2f5d8a Mon Sep 17 00:00:00 2001 From: kondou Date: Tue, 6 Aug 2013 16:56:50 +0200 Subject: Fix #4258, clean up \OC_Image and improve its unittest --- lib/image.php | 301 ++++++++++++++++++++++++++++------------------------ tests/lib/image.php | 33 ++++-- 2 files changed, 189 insertions(+), 145 deletions(-) (limited to 'tests') diff --git a/lib/image.php b/lib/image.php index 4bc38e20e56..fcb13a1fa14 100644 --- a/lib/image.php +++ b/lib/image.php @@ -25,24 +25,27 @@ */ class OC_Image { protected $resource = false; // tmp resource. - protected $imagetype = IMAGETYPE_PNG; // Default to png if file type isn't evident. - protected $bit_depth = 24; - protected $filepath = null; + protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. + protected $mimeType = "image/png"; // Default to png + protected $bitDepth = 24; + protected $filePath = null; + + private $fileInfo; /** * @brief Get mime type for an image file. * @param $filepath The path to a local image file. * @returns string The mime type if the it could be determined, otherwise an empty string. */ - static public function getMimeTypeForFile($filepath) { + static public function getMimeTypeForFile($filePath) { // exif_imagetype throws "read error!" if file is less than 12 byte - if (filesize($filepath) > 11) { - $imagetype = exif_imagetype($filepath); + if (filesize($filePath) > 11) { + $imageType = exif_imagetype($filePath); } else { - $imagetype = false; + $imageType = false; } - return $imagetype ? image_type_to_mime_type($imagetype) : ''; + return $imageType ? image_type_to_mime_type($imageType) : ''; } /** @@ -50,14 +53,19 @@ class OC_Image { * @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function. * @returns bool False on error */ - public function __construct($imageref = null) { + public function __construct($imageRef = null) { //OC_Log::write('core',__METHOD__.'(): start', OC_Log::DEBUG); if(!extension_loaded('gd') || !function_exists('gd_info')) { OC_Log::write('core', __METHOD__.'(): GD module not installed', OC_Log::ERROR); return false; } - if(!is_null($imageref)) { - $this->load($imageref); + + if (\OC_Util::fileInfoLoaded()) { + $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); + } + + if(!is_null($imageRef)) { + $this->load($imageRef); } } @@ -74,7 +82,7 @@ class OC_Image { * @returns int */ public function mimeType() { - return $this->valid() ? image_type_to_mime_type($this->imagetype) : ''; + return $this->valid() ? $this->mimeType : ''; } /** @@ -157,30 +165,30 @@ class OC_Image { * @returns bool */ - public function save($filepath=null) { - if($filepath === null && $this->filepath === null) { + public function save($filePath=null) { + if($filePath === null && $this->filePath === null) { OC_Log::write('core', __METHOD__.'(): called with no path.', OC_Log::ERROR); return false; - } elseif($filepath === null && $this->filepath !== null) { - $filepath = $this->filepath; + } elseif($filePath === null && $this->filePath !== null) { + $filePath = $this->filePath; } - return $this->_output($filepath); + return $this->_output($filePath); } /** * @brief Outputs/saves the image. */ - private function _output($filepath=null) { - if($filepath) { - if (!file_exists(dirname($filepath))) - mkdir(dirname($filepath), 0777, true); - if(!is_writable(dirname($filepath))) { + private function _output($filePath=null) { + if($filePath) { + if (!file_exists(dirname($filePath))) + mkdir(dirname($filePath), 0777, true); + if(!is_writable(dirname($filePath))) { OC_Log::write('core', - __METHOD__.'(): Directory \''.dirname($filepath).'\' is not writable.', + __METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.', OC_Log::ERROR); return false; - } elseif(is_writable(dirname($filepath)) && file_exists($filepath) && !is_writable($filepath)) { - OC_Log::write('core', __METHOD__.'(): File \''.$filepath.'\' is not writable.', OC_Log::ERROR); + } elseif(is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) { + OC_Log::write('core', __METHOD__.'(): File \''.$filePath.'\' is not writable.', OC_Log::ERROR); return false; } } @@ -188,30 +196,30 @@ class OC_Image { return false; } - $retval = false; - switch($this->imagetype) { + $retVal = false; + switch($this->imageType) { case IMAGETYPE_GIF: - $retval = imagegif($this->resource, $filepath); + $retVal = imagegif($this->resource, $filePath); break; case IMAGETYPE_JPEG: - $retval = imagejpeg($this->resource, $filepath); + $retVal = imagejpeg($this->resource, $filePath); break; case IMAGETYPE_PNG: - $retval = imagepng($this->resource, $filepath); + $retVal = imagepng($this->resource, $filePath); break; case IMAGETYPE_XBM: - $retval = imagexbm($this->resource, $filepath); + $retVal = imagexbm($this->resource, $filePath); break; case IMAGETYPE_WBMP: - $retval = imagewbmp($this->resource, $filepath); + $retVal = imagewbmp($this->resource, $filePath); break; case IMAGETYPE_BMP: - $retval = imagebmp($this->resource, $filepath, $this->bit_depth); + $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); break; default: - $retval = imagepng($this->resource, $filepath); + $retVal = imagepng($this->resource, $filePath); } - return $retval; + return $retVal; } /** @@ -233,7 +241,21 @@ class OC_Image { */ function data() { ob_start(); - $res = imagepng($this->resource); + switch ($this->mimeType) { + case "image/png": + $res = imagepng($this->resource); + break; + case "image/jpeg": + $res = imagejpeg($this->resource); + break; + case "image/gif": + $res = imagegif($this->resource); + break; + default: + $res = imagepng($this->resource); + OC_Log::write('core', 'OC_Image->data. Couldn\'t guess mimetype, defaulting to png', OC_Log::INFO); + break; + } if (!$res) { OC_Log::write('core', 'OC_Image->data. Error getting image data.', OC_Log::ERROR); } @@ -261,11 +283,11 @@ class OC_Image { OC_Log::write('core', 'OC_Image->fixOrientation() No image loaded.', OC_Log::DEBUG); return -1; } - if(is_null($this->filepath) || !is_readable($this->filepath)) { + if(is_null($this->filePath) || !is_readable($this->filePath)) { OC_Log::write('core', 'OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG); return -1; } - $exif = @exif_read_data($this->filepath, 'IFD0'); + $exif = @exif_read_data($this->filePath, 'IFD0'); if(!$exif) { return -1; } @@ -351,19 +373,19 @@ class OC_Image { * @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function or a file resource (file handle ). * @returns An image resource or false on error */ - public function load($imageref) { - if(is_resource($imageref)) { - if(get_resource_type($imageref) == 'gd') { - $this->resource = $imageref; + public function load($imageRef) { + if(is_resource($imageRef)) { + if(get_resource_type($imageRef) == 'gd') { + $this->resource = $imageRef; return $this->resource; - } elseif(in_array(get_resource_type($imageref), array('file', 'stream'))) { - return $this->loadFromFileHandle($imageref); + } elseif(in_array(get_resource_type($imageRef), array('file', 'stream'))) { + return $this->loadFromFileHandle($imageRef); } - } elseif($this->loadFromFile($imageref) !== false) { + } elseif($this->loadFromFile($imageRef) !== false) { return $this->resource; - } elseif($this->loadFromBase64($imageref) !== false) { + } elseif($this->loadFromBase64($imageRef) !== false) { return $this->resource; - } elseif($this->loadFromData($imageref) !== false) { + } elseif($this->loadFromData($imageRef) !== false) { return $this->resource; } else { OC_Log::write('core', __METHOD__.'(): couldn\'t load anything. Giving up!', OC_Log::DEBUG); @@ -390,62 +412,62 @@ class OC_Image { * @param $imageref The path to a local file. * @returns An image resource or false on error */ - public function loadFromFile($imagepath=false) { + public function loadFromFile($imagePath=false) { // exif_imagetype throws "read error!" if file is less than 12 byte - if(!@is_file($imagepath) || !file_exists($imagepath) || filesize($imagepath) < 12 || !is_readable($imagepath)) { + if(!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { // Debug output disabled because this method is tried before loadFromBase64? - OC_Log::write('core', 'OC_Image->loadFromFile, couldn\'t load: '.$imagepath, OC_Log::DEBUG); + OC_Log::write('core', 'OC_Image->loadFromFile, couldn\'t load: '.$imagePath, OC_Log::DEBUG); return false; } - $itype = exif_imagetype($imagepath); - switch($itype) { + $iType = exif_imagetype($imagePath); + switch ($iType) { case IMAGETYPE_GIF: if (imagetypes() & IMG_GIF) { - $this->resource = imagecreatefromgif($imagepath); + $this->resource = imagecreatefromgif($imagePath); } else { OC_Log::write('core', - 'OC_Image->loadFromFile, GIF images not supported: '.$imagepath, + 'OC_Image->loadFromFile, GIF images not supported: '.$imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_JPEG: if (imagetypes() & IMG_JPG) { - $this->resource = imagecreatefromjpeg($imagepath); + $this->resource = imagecreatefromjpeg($imagePath); } else { OC_Log::write('core', - 'OC_Image->loadFromFile, JPG images not supported: '.$imagepath, + 'OC_Image->loadFromFile, JPG images not supported: '.$imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_PNG: if (imagetypes() & IMG_PNG) { - $this->resource = imagecreatefrompng($imagepath); + $this->resource = imagecreatefrompng($imagePath); } else { OC_Log::write('core', - 'OC_Image->loadFromFile, PNG images not supported: '.$imagepath, + 'OC_Image->loadFromFile, PNG images not supported: '.$imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_XBM: if (imagetypes() & IMG_XPM) { - $this->resource = imagecreatefromxbm($imagepath); + $this->resource = imagecreatefromxbm($imagePath); } else { OC_Log::write('core', - 'OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagepath, + 'OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_WBMP: if (imagetypes() & IMG_WBMP) { - $this->resource = imagecreatefromwbmp($imagepath); + $this->resource = imagecreatefromwbmp($imagePath); } else { OC_Log::write('core', - 'OC_Image->loadFromFile, WBMP images not supported: '.$imagepath, + 'OC_Image->loadFromFile, WBMP images not supported: '.$imagePath, OC_Log::DEBUG); } break; case IMAGETYPE_BMP: - $this->resource = $this->imagecreatefrombmp($imagepath); + $this->resource = $this->imagecreatefrombmp($imagePath); break; /* case IMAGETYPE_TIFF_II: // (intel byte order) @@ -474,14 +496,15 @@ class OC_Image { default: // this is mostly file created from encrypted file - $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagepath))); - $itype = IMAGETYPE_PNG; + $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); + $iType = IMAGETYPE_PNG; OC_Log::write('core', 'OC_Image->loadFromFile, Default', OC_Log::DEBUG); break; } if($this->valid()) { - $this->imagetype = $itype; - $this->filepath = $imagepath; + $this->imageType = $iType; + $this->mimeType = image_type_to_mime_type($iType); + $this->filePath = $imagePath; } return $this->resource; } @@ -496,6 +519,9 @@ class OC_Image { return false; } $this->resource = @imagecreatefromstring($str); + if (\OC_Util::fileInfoLoaded()) { + $this->mimeType = $this->fileInfo->buffer($str); + } if(!$this->resource) { OC_Log::write('core', 'OC_Image->loadFromData, couldn\'t load', OC_Log::DEBUG); return false; @@ -515,6 +541,9 @@ class OC_Image { $data = base64_decode($str); if($data) { // try to load from string data $this->resource = @imagecreatefromstring($data); + if (\OC_Util::fileInfoLoaded()) { + $this->mimeType = $this->fileInfo->buffer($data); + } if(!$this->resource) { OC_Log::write('core', 'OC_Image->loadFromBase64, couldn\'t load', OC_Log::DEBUG); return false; @@ -534,16 +563,16 @@ class OC_Image { *

* @return resource an image resource identifier on success, FALSE on errors. */ - private function imagecreatefrombmp($filename) { - if (!($fh = fopen($filename, 'rb'))) { - trigger_error('imagecreatefrombmp: Can not open ' . $filename, E_USER_WARNING); + private function imagecreatefrombmp($fileName) { + if (!($fh = fopen($fileName, 'rb'))) { + trigger_error('imagecreatefrombmp: Can not open ' . $fileName, E_USER_WARNING); return false; } // read file header $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14)); // check for bitmap if ($meta['type'] != 19778) { - trigger_error('imagecreatefrombmp: ' . $filename . ' is not a bitmap!', E_USER_WARNING); + trigger_error('imagecreatefrombmp: ' . $fileName . ' is not a bitmap!', E_USER_WARNING); return false; } // read image header @@ -554,7 +583,7 @@ class OC_Image { } // set bytes and padding $meta['bytes'] = $meta['bits'] / 8; - $this->bit_depth = $meta['bits']; //remember the bit depth for the imagebmp call + $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4)- floor($meta['width'] * $meta['bytes'] / 4))); if ($meta['decal'] == 4) { $meta['decal'] = 0; @@ -590,7 +619,7 @@ class OC_Image { $p = 0; $vide = chr(0); $y = $meta['height'] - 1; - $error = 'imagecreatefrombmp: ' . $filename . ' has not enough data!'; + $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; // loop through the image data beginning with the lower left corner while ($y >= 0) { $x = 0; @@ -653,7 +682,7 @@ class OC_Image { break; default: trigger_error('imagecreatefrombmp: ' - . $filename . ' has ' . $meta['bits'] . ' bits and this is not supported!', + . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', E_USER_WARNING); return false; } @@ -673,24 +702,24 @@ class OC_Image { * @param $maxsize The maximum size of either the width or height. * @returns bool */ - public function resize($maxsize) { + public function resize($maxSize) { if(!$this->valid()) { OC_Log::write('core', __METHOD__.'(): No image loaded', OC_Log::ERROR); return false; } - $width_orig=imageSX($this->resource); - $height_orig=imageSY($this->resource); - $ratio_orig = $width_orig/$height_orig; + $widthOrig=imageSX($this->resource); + $heightOrig=imageSY($this->resource); + $ratioOrig = $widthOrig/$heightOrig; - if ($ratio_orig > 1) { - $new_height = round($maxsize/$ratio_orig); - $new_width = $maxsize; + if ($ratioOrig > 1) { + $newHeight = round($maxSize/$ratioOrig); + $newWidth = $maxSize; } else { - $new_width = round($maxsize*$ratio_orig); - $new_height = $maxsize; + $newWidth = round($maxSize*$ratioOrig); + $newHeight = $maxSize; } - $this->preciseResize(round($new_width), round($new_height)); + $this->preciseResize(round($newWidth), round($newHeight)); return true; } @@ -699,8 +728,8 @@ class OC_Image { OC_Log::write('core', __METHOD__.'(): No image loaded', OC_Log::ERROR); return false; } - $width_orig=imageSX($this->resource); - $height_orig=imageSY($this->resource); + $widthOrig=imageSX($this->resource); + $heightOrig=imageSY($this->resource); $process = imagecreatetruecolor($width, $height); if ($process == false) { @@ -710,13 +739,13 @@ class OC_Image { } // preserve transparency - if($this->imagetype == IMAGETYPE_GIF or $this->imagetype == IMAGETYPE_PNG) { + if($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); imagealphablending($process, false); imagesavealpha($process, true); } - imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); + imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); if ($process == false) { OC_Log::write('core', __METHOD__.'(): Error resampling process image '.$width.'x'.$height, OC_Log::ERROR); imagedestroy($process); @@ -737,19 +766,19 @@ class OC_Image { OC_Log::write('core', 'OC_Image->centerCrop, No image loaded', OC_Log::ERROR); return false; } - $width_orig=imageSX($this->resource); - $height_orig=imageSY($this->resource); - if($width_orig === $height_orig and $size==0) { + $widthOrig=imageSX($this->resource); + $heightOrig=imageSY($this->resource); + if($widthOrig === $heightOrig and $size==0) { return true; } - $ratio_orig = $width_orig/$height_orig; - $width = $height = min($width_orig, $height_orig); + $ratioOrig = $widthOrig/$heightOrig; + $width = $height = min($widthOrig, $heightOrig); - if ($ratio_orig > 1) { - $x = ($width_orig/2) - ($width/2); + if ($ratioOrig > 1) { + $x = ($widthOrig/2) - ($width/2); $y = 0; } else { - $y = ($height_orig/2) - ($height/2); + $y = ($heightOrig/2) - ($height/2); $x = 0; } if($size>0) { @@ -767,7 +796,7 @@ class OC_Image { } // preserve transparency - if($this->imagetype == IMAGETYPE_GIF or $this->imagetype == IMAGETYPE_PNG) { + if($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); imagealphablending($process, false); imagesavealpha($process, true); @@ -827,9 +856,9 @@ class OC_Image { OC_Log::write('core', __METHOD__.'(): No image loaded', OC_Log::ERROR); return false; } - $width_orig=imageSX($this->resource); - $height_orig=imageSY($this->resource); - $ratio = $width_orig/$height_orig; + $widthOrig=imageSX($this->resource); + $heightOrig=imageSY($this->resource); + $ratio = $widthOrig/$heightOrig; $newWidth = min($maxWidth, $ratio*$maxHeight); $newHeight = min($maxHeight, $maxWidth/$ratio); @@ -863,7 +892,7 @@ if ( ! function_exists( 'imagebmp') ) { * @param int $compression [optional] * @return bool TRUE on success or FALSE on failure. */ - function imagebmp($im, $filename='', $bit=24, $compression=0) { + function imagebmp($im, $fileName='', $bit=24, $compression=0) { if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) { $bit = 24; } @@ -874,14 +903,14 @@ if ( ! function_exists( 'imagebmp') ) { imagetruecolortopalette($im, true, $bits); $width = imagesx($im); $height = imagesy($im); - $colors_num = imagecolorstotal($im); - $rgb_quad = ''; + $colorsNum = imagecolorstotal($im); + $rgbQuad = ''; if ($bit <= 8) { - for ($i = 0; $i < $colors_num; $i++) { + for ($i = 0; $i < $colorsNum; $i++) { $colors = imagecolorsforindex($im, $i); - $rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; + $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; } - $bmp_data = ''; + $bmpData = ''; if ($compression == 0 || $bit < 8) { $compression = 0; $extra = ''; @@ -899,35 +928,35 @@ if ( ! function_exists( 'imagebmp') ) { $bin |= $index << $k; $i++; } - $bmp_data .= chr($bin); + $bmpData .= chr($bin); } - $bmp_data .= $extra; + $bmpData .= $extra; } } // RLE8 else if ($compression == 1 && $bit == 8) { for ($j = $height - 1; $j >= 0; $j--) { - $last_index = "\0"; - $same_num = 0; + $lastIndex = "\0"; + $sameNum = 0; for ($i = 0; $i <= $width; $i++) { $index = imagecolorat($im, $i, $j); - if ($index !== $last_index || $same_num > 255) { - if ($same_num != 0) { - $bmp_data .= chr($same_num) . chr($last_index); + if ($index !== $lastIndex || $sameNum > 255) { + if ($sameNum != 0) { + $bmpData .= chr($same_num) . chr($lastIndex); } - $last_index = $index; - $same_num = 1; + $lastIndex = $index; + $sameNum = 1; } else { - $same_num++; + $sameNum++; } } - $bmp_data .= "\0\0"; + $bmpData .= "\0\0"; } - $bmp_data .= "\0\1"; + $bmpData .= "\0\1"; } - $size_quad = strlen($rgb_quad); - $size_data = strlen($bmp_data); + $sizeQuad = strlen($rgbQuad); + $sizeData = strlen($bmpData); } else { $extra = ''; @@ -935,7 +964,7 @@ if ( ! function_exists( 'imagebmp') ) { if ($padding % 4 != 0) { $extra = str_repeat("\0", $padding); } - $bmp_data = ''; + $bmpData = ''; for ($j = $height - 1; $j >= 0; $j--) { for ($i = 0; $i < $width; $i++) { $index = imagecolorat($im, $i, $j); @@ -945,27 +974,27 @@ if ( ! function_exists( 'imagebmp') ) { $bin |= ($colors['red'] >> 3) << 10; $bin |= ($colors['green'] >> 3) << 5; $bin |= $colors['blue'] >> 3; - $bmp_data .= pack("v", $bin); + $bmpData .= pack("v", $bin); } else { - $bmp_data .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); + $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); } } - $bmp_data .= $extra; + $bmpData .= $extra; } - $size_quad = 0; - $size_data = strlen($bmp_data); - $colors_num = 0; - } - $file_header = 'BM' . pack('V3', 54 + $size_quad + $size_data, 0, 54 + $size_quad); - $info_header = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0); - if ($filename != '') { - $fp = fopen($filename, 'wb'); - fwrite($fp, $file_header . $info_header . $rgb_quad . $bmp_data); + $sizeQuad = 0; + $sizeData = strlen($bmpData); + $colorsNum = 0; + } + $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); + $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); + if ($fileName != '') { + $fp = fopen($fileName, 'wb'); + fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); fclose($fp); return true; } - echo $file_header . $info_header. $rgb_quad . $bmp_data; + echo $fileHeader . $infoHeader. $rgbQuad . $bmpData; return true; } } @@ -977,8 +1006,8 @@ if ( ! function_exists( 'exif_imagetype' ) ) { * @param string $filename * @return string|boolean */ - function exif_imagetype ( $filename ) { - if ( ( $info = getimagesize( $filename ) ) !== false ) { + function exif_imagetype ( $fileName ) { + if ( ( $info = getimagesize( $fileName ) ) !== false ) { return $info[2]; } return false; diff --git a/tests/lib/image.php b/tests/lib/image.php index 0583c300075..b3db89cf5b8 100644 --- a/tests/lib/image.php +++ b/tests/lib/image.php @@ -7,6 +7,10 @@ */ class Test_Image extends PHPUnit_Framework_TestCase { + public static function tearDownAfterClass() { + unlink(OC::$SERVERROOT.'/tests/data/testimage2.png'); + unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg'); + } public function testGetMimeTypeForFile() { $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.png'); @@ -55,7 +59,6 @@ class Test_Image extends PHPUnit_Framework_TestCase { } public function testMimeType() { - $this->markTestSkipped("When loading from data or base64, imagetype is always image/png, see #4258."); $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); $this->assertEquals('image/png', $img->mimeType()); @@ -102,35 +105,47 @@ class Test_Image extends PHPUnit_Framework_TestCase { $img->resize(16); $img->save(OC::$SERVERROOT.'/tests/data/testimage2.png'); $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.png'), $img->data()); + + $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg'); + $img->resize(128); + $img->save(OC::$SERVERROOT.'/tests/data/testimage2.jpg'); + $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.jpg'), $img->data()); } public function testData() { - $this->markTestSkipped("\OC_Image->data() converts to png before outputting data, see #4258."); $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); - $expected = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'); + $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png')); + ob_start(); + imagepng($raw); + $expected = ob_get_clean(); $this->assertEquals($expected, $img->data()); $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg'); - $expected = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'); + $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + ob_start(); + imagejpeg($raw); + $expected = ob_get_clean(); $this->assertEquals($expected, $img->data()); $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif'); - $expected = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'); + $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')); + ob_start(); + imagegif($raw); + $expected = ob_get_clean(); $this->assertEquals($expected, $img->data()); } public function testToString() { - $this->markTestSkipped("\OC_Image->data() converts to png before outputting data, see #4258."); $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); - $expected = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png')); + $expected = base64_encode($img->data()); $this->assertEquals($expected, (string)$img); $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); - $expected = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg')); + $expected = base64_encode($img->data()); $this->assertEquals($expected, (string)$img); $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif'); - $expected = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')); + $expected = base64_encode($img->data()); $this->assertEquals($expected, (string)$img); } -- cgit v1.2.3 From 2d6a400381ffe9d13047ecf7273c550f335e5225 Mon Sep 17 00:00:00 2001 From: kondou Date: Sun, 1 Sep 2013 15:50:58 +0200 Subject: Check for $this->fileInfo and @depend on testData() --- lib/image.php | 4 ++-- tests/lib/image.php | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/lib/image.php b/lib/image.php index badc30ab9a0..7761a3c7737 100644 --- a/lib/image.php +++ b/lib/image.php @@ -519,7 +519,7 @@ class OC_Image { return false; } $this->resource = @imagecreatefromstring($str); - if (\OC_Util::fileInfoLoaded()) { + if ($this->fileInfo) { $this->mimeType = $this->fileInfo->buffer($str); } if(is_resource($this->resource)) { @@ -546,7 +546,7 @@ class OC_Image { $data = base64_decode($str); if($data) { // try to load from string data $this->resource = @imagecreatefromstring($data); - if (\OC_Util::fileInfoLoaded()) { + if ($this->fileInfo) { $this->mimeType = $this->fileInfo->buffer($data); } if(!$this->resource) { diff --git a/tests/lib/image.php b/tests/lib/image.php index b3db89cf5b8..4aba1b0bc61 100644 --- a/tests/lib/image.php +++ b/tests/lib/image.php @@ -135,6 +135,9 @@ class Test_Image extends PHPUnit_Framework_TestCase { $this->assertEquals($expected, $img->data()); } + /** + * @depends testData + */ public function testToString() { $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png'); $expected = base64_encode($img->data()); -- cgit v1.2.3