]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use IAppManager instead of OC_App 840/head
authorJulius Haertl <jus@bitgrid.net>
Fri, 18 Nov 2016 09:49:03 +0000 (10:49 +0100)
committerJulius Haertl <jus@bitgrid.net>
Fri, 18 Nov 2016 10:07:44 +0000 (11:07 +0100)
Signed-off-by: Julius Haertl <jus@bitgrid.net>
apps/theming/lib/IconBuilder.php
apps/theming/lib/Util.php
apps/theming/tests/Controller/ThemingControllerTest.php
apps/theming/tests/IconBuilderTest.php
apps/theming/tests/UtilTest.php

index be3035eb0d8e52887fc00c24dbe644ce4bd64989..d8161051ebbf50b97bfa4e20d6a52d831e68a8bd 100644 (file)
@@ -25,6 +25,7 @@ namespace OCA\Theming;
 
 use Imagick;
 use ImagickPixel;
+use OCP\App\AppPathNotFoundException;
 
 class IconBuilder {
        /** @var ThemingDefaults */
@@ -85,8 +86,13 @@ class IconBuilder {
         * @return Imagick|false
         */
        public function renderAppIcon($app) {
-               $appIcon = $this->util->getAppIcon($app);
-               $appIconContent = file_get_contents($appIcon);
+               try {
+                       $appIcon = $this->util->getAppIcon($app);
+                       $appIconContent = file_get_contents($appIcon);
+               } catch (AppPathNotFoundException $e) {
+                       return false;
+               }
+
                if($appIconContent === false) {
                        return false;
                }
@@ -158,7 +164,11 @@ class IconBuilder {
        }
 
        public function colorSvg($app, $image) {
-               $imageFile = $this->util->getAppImage($app, $image);
+               try {
+                       $imageFile = $this->util->getAppImage($app, $image);
+               } catch (AppPathNotFoundException $e) {
+                       return false;
+               }
                $svg = file_get_contents($imageFile);
                if ($svg !== false) {
                        $color = $this->util->elementColor($this->themingDefaults->getMailHeaderColor());
index 963cf56633b3678c5bf788fd8644454b043583d9..9fea56838adea9855a663ca84c3c2e5d50b70321 100644 (file)
@@ -23,6 +23,8 @@
 
 namespace OCA\Theming;
 
+use OCP\App\AppPathNotFoundException;
+use OCP\App\IAppManager;
 use OCP\IConfig;
 use OCP\Files\IRootFolder;
 
@@ -34,15 +36,20 @@ class Util {
        /** @var IRootFolder */
        private $rootFolder;
 
+       /** @var IAppManager */
+       private $appManager;
+
        /**
         * Util constructor.
         *
         * @param IConfig $config
         * @param IRootFolder $rootFolder
+        * @param IAppManager $appManager
         */
-       public function __construct(IConfig $config, IRootFolder $rootFolder) {
+       public function __construct(IConfig $config, IRootFolder $rootFolder, IAppManager $appManager) {
                $this->config = $config;
                $this->rootFolder = $rootFolder;
+               $this->appManager = $appManager;
        }
 
        /**
@@ -108,8 +115,8 @@ class Util {
         */
        public function getAppIcon($app) {
                $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
-               $appPath = \OC_App::getAppPath($app);
-               if ($appPath !== false) {
+               try {
+                       $appPath = $this->appManager->getAppPath($app);
                        $icon = $appPath . '/img/' . $app . '.svg';
                        if (file_exists($icon)) {
                                return $icon;
@@ -118,7 +125,8 @@ class Util {
                        if (file_exists($icon)) {
                                return $icon;
                        }
-               }
+               } catch (AppPathNotFoundException $e) {}
+
                if($this->config->getAppValue('theming', 'logoMime', '') !== '' && $this->rootFolder->nodeExists('/themedinstancelogo')) {
                        return $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/') . '/themedinstancelogo';
                }
@@ -128,40 +136,45 @@ class Util {
        /**
         * @param $app string app name
         * @param $image string relative path to image in app folder
-        * @return string absolute path to image
+        * @return string|false absolute path to image
         */
        public function getAppImage($app, $image) {
                $app = str_replace(array('\0', '/', '\\', '..'), '', $app);
                $image = str_replace(array('\0', '\\', '..'), '', $image);
-               $appPath = \OC_App::getAppPath($app);
-                       if ($app === "core") {
-                               $icon = \OC::$SERVERROOT . '/core/img/' . $image;
-                               if (file_exists($icon)) {
-                                       return $icon;
-                               }
-                       }
-               if ($appPath !== false) {
-                       $icon = $appPath . '/img/' . $image;
-                       if (file_exists($icon)) {
-                               return $icon;
-                       }
-                       $icon = $appPath . '/img/' . $image . '.svg';
-                       if (file_exists($icon)) {
-                               return $icon;
-                       }
-                       $icon = $appPath . '/img/' . $image . '.png';
-                       if (file_exists($icon)) {
-                               return $icon;
-                       }
-                       $icon = $appPath . '/img/' . $image . '.gif';
-                       if (file_exists($icon)) {
-                               return $icon;
-                       }
-                       $icon = $appPath . '/img/' . $image . '.jpg';
+               if ($app === "core") {
+                       $icon = \OC::$SERVERROOT . '/core/img/' . $image;
                        if (file_exists($icon)) {
                                return $icon;
                        }
                }
+
+               try {
+                       $appPath = $this->appManager->getAppPath($app);
+               } catch (AppPathNotFoundException $e) {
+                       return false;
+               }
+
+               $icon = $appPath . '/img/' . $image;
+               if (file_exists($icon)) {
+                       return $icon;
+               }
+               $icon = $appPath . '/img/' . $image . '.svg';
+               if (file_exists($icon)) {
+                       return $icon;
+               }
+               $icon = $appPath . '/img/' . $image . '.png';
+               if (file_exists($icon)) {
+                       return $icon;
+               }
+               $icon = $appPath . '/img/' . $image . '.gif';
+               if (file_exists($icon)) {
+                       return $icon;
+               }
+               $icon = $appPath . '/img/' . $image . '.jpg';
+               if (file_exists($icon)) {
+                       return $icon;
+               }
+
                return false;
        }
 
index 3bf7709b0def74b0ed95cf86909c85c196aeaa14..9ce4fb86b52c512f056e7e5cf023b80142ad853a 100644 (file)
@@ -26,6 +26,7 @@ namespace OCA\Theming\Tests\Controller;
 
 use OCA\Theming\Controller\ThemingController;
 use OCA\Theming\Util;
+use OCP\App\IAppManager;
 use OCP\AppFramework\Http;
 use OCP\AppFramework\Http\DataResponse;
 use OCP\Files\IRootFolder;
@@ -55,6 +56,8 @@ class ThemingControllerTest extends TestCase {
        private $rootFolder;
        /** @var ITempManager */
        private $tempManager;
+       /** @var IAppManager */
+       private $appManager;
 
        public function setUp() {
                $this->request = $this->getMockBuilder('OCP\IRequest')->getMock();
@@ -66,7 +69,8 @@ class ThemingControllerTest extends TestCase {
                        ->getMock();
                $this->l10n = $this->getMockBuilder('OCP\IL10N')->getMock();
                $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
-               $this->util = new Util($this->config, $this->rootFolder);
+               $this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
+               $this->util = new Util($this->config, $this->rootFolder, $this->appManager);
                $this->timeFactory->expects($this->any())
                        ->method('getTime')
                        ->willReturn(123);
index a13d4b9476d5d181661f3811fc5d12c800aadb3d..54850c8f3c2e3184c7fff5cc3f92a94a5eb76cde 100644 (file)
@@ -25,6 +25,7 @@ namespace OCA\Theming\Tests;
 use OCA\Theming\IconBuilder;
 use OCA\Theming\ThemingDefaults;
 use OCA\Theming\Util;
+use OCP\App\IAppManager;
 use OCP\AppFramework\Http\NotFoundResponse;
 use OCP\Files\IRootFolder;
 use OCP\IConfig;
@@ -42,6 +43,8 @@ class IconBuilderTest extends TestCase {
        protected $util;
        /** @var IconBuilder */
        protected $iconBuilder;
+       /** @var IAppManager */
+       protected $appManager;
 
        protected function setUp() {
                parent::setUp();
@@ -50,7 +53,8 @@ class IconBuilderTest extends TestCase {
                $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
                $this->themingDefaults = $this->getMockBuilder('OCA\Theming\ThemingDefaults')
                        ->disableOriginalConstructor()->getMock();
-               $this->util = new Util($this->config, $this->rootFolder);
+               $this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
+               $this->util = new Util($this->config, $this->rootFolder, $this->appManager);
                $this->iconBuilder = new IconBuilder($this->themingDefaults, $this->util);
        }
 
index d82ec8eeb85272f6e03fd60ac72ec6a2ccd5538f..83895208fea1e79cb1f00fa5e7a1ec8c407b093a 100644 (file)
@@ -23,6 +23,7 @@
 namespace OCA\Theming\Tests;
 
 use OCA\Theming\Util;
+use OCP\App\IAppManager;
 use OCP\IConfig;
 use OCP\Files\IRootFolder;
 use Test\TestCase;
@@ -35,12 +36,15 @@ class UtilTest extends TestCase {
        protected $config;
        /** @var IRootFolder */
        protected $rootFolder;
+       /** @var IAppManager */
+       protected $appManager;
 
        protected function setUp() {
                parent::setUp();
                $this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
                $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')->getMock();
-               $this->util = new Util($this->config, $this->rootFolder);
+               $this->appManager = $this->getMockBuilder('OCP\App\IAppManager')->getMock();
+               $this->util = new Util($this->config, $this->rootFolder, $this->appManager);
        }
 
        public function testInvertTextColorLight() {
@@ -108,6 +112,10 @@ class UtilTest extends TestCase {
         * @dataProvider dataGetAppIcon
         */
        public function testGetAppIcon($app, $expected) {
+               $this->appManager->expects($this->once())
+                       ->method('getAppPath')
+                       ->with($app)
+                       ->willReturn(\OC_App::getAppPath($app));
                $icon = $this->util->getAppIcon($app);
                $this->assertEquals($expected, $icon);
        }
@@ -134,6 +142,12 @@ class UtilTest extends TestCase {
         * @dataProvider dataGetAppImage
         */
        public function testGetAppImage($app, $image, $expected) {
+               if($app !== 'core') {
+                       $this->appManager->expects($this->once())
+                               ->method('getAppPath')
+                               ->with($app)
+                               ->willReturn(\OC_App::getAppPath($app));
+               }
                $this->assertEquals($expected, $this->util->getAppImage($app, $image));
        }