Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions core/Controller/SvgController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace OC\Core\Controller;

use OC\Template\IconsCacher;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
Expand Down Expand Up @@ -97,13 +98,13 @@ public function getSvgFromCore(string $folder, string $fileName, string $color =
* @return DataDisplayResponse|NotFoundResponse
*/
public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') {
$appRootPath = $this->appManager->getAppPath($app);
$appPath = substr($appRootPath, strlen($this->serverRoot));

if (!$appPath) {
try {
$appPath = $this->appManager->getAppPath($app);
} catch (AppPathNotFoundException $e) {
return new NotFoundResponse();
}
$path = $this->serverRoot . $appPath ."/img/$fileName.svg";

$path = $appPath . "/img/$fileName.svg";
return $this->getSvg($path, $color, $fileName);
}

Expand Down
65 changes: 61 additions & 4 deletions tests/Core/Controller/SvgControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OC\AppFramework\Http;
use OC\Core\Controller\SvgController;
use OC\Template\IconsCacher;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
Expand All @@ -46,6 +47,9 @@ class SvgControllerTest extends TestCase {
self::TEST_IMAGE_RECT,
];

/** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
private $appManager;

/**
* @var SvgController
*/
Expand Down Expand Up @@ -87,17 +91,20 @@ public static function removeTestImages() {
* @return void
*/
public function setupSvgController() {
/** @var IRequest */
$request = $this->getMockBuilder(IRequest::class)->getMock();
/** @var ITimeFactory $timeFactory */
$timeFactory = $this->getMockBuilder(ITimeFactory::class)->getMock();
$appManager = $this->getMockBuilder(IAppManager::class)->getMock();
/** @var IAppManager */
$this->appManager = $this->getMockBuilder(IAppManager::class)->getMock();
/** @var IconsCacher $iconsCacher */
$iconsCacher = $this->getMockBuilder(IconsCacher::class)->disableOriginalConstructor()->setMethods(['__construct'])->getMock();
$this->svgController = new SvgController('core', $request, $timeFactory, $appManager, $iconsCacher);
$this->svgController = new SvgController('core', $request, $timeFactory, $this->appManager, $iconsCacher);
}

/**
* Checks that requesting an unknown image results in a 404.
*
* @test
* @return void
*/
public function testGetSvgFromCoreNotFound() {
Expand All @@ -120,7 +127,6 @@ public function provideGetSvgFromCoreTestData(): array {
/**
* Tests that retrieving a colored SVG works.
*
* @test
* @dataProvider provideGetSvgFromCoreTestData
* @param string $name The requested svg name
* @param string $color The requested color
Expand All @@ -138,4 +144,55 @@ public function testGetSvgFromCore(string $name, string $color, string $expected

self::assertEquals($expected, $response->getData());
}

/**
* Checks that requesting an unknown image results in a 404.
*/
public function testGetSvgFromAppNotFound(): void {
$this->appManager->expects($this->once())
->method('getAppPath')
->with('invalid_app')
->willThrowException(new AppPathNotFoundException('Could not find path for invalid_app'));

$response = $this->svgController->getSvgFromApp('invalid_app', 'some-icon', '#ff0000');
self::assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus());
}

/**
* Provides svg coloring test data.
*
* @return array
*/
public function provideGetSvgFromAppTestData(): array {
return [
'settings admin' => ['settings', 'admin', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/settings-admin-red.svg')],
'files app' => ['files', 'app', 'f00', file_get_contents(self::TEST_IMAGES_SOURCE_PATH . '/files-app-red.svg')],
];
}

/**
* Tests that retrieving a colored SVG works.
*
* @dataProvider provideGetSvgFromAppTestData
* @param string $appName
* @param string $name The requested svg name
* @param string $color The requested color
* @param string $expected
*/
public function testGetSvgFromApp(string $appName, string $name, string $color, string $expected): void {
$this->appManager->expects($this->once())
->method('getAppPath')
->with($appName)
->willReturn(__DIR__ . '/../../../apps/' . $appName);

$response = $this->svgController->getSvgFromApp($appName, $name, $color);

self::assertEquals(Http::STATUS_OK, $response->getStatus());

$headers = $response->getHeaders();
self::assertArrayHasKey('Content-Type', $headers);
self::assertEquals($headers['Content-Type'], 'image/svg+xml');

self::assertEquals($expected, $response->getData());
}
}
1 change: 1 addition & 0 deletions tests/data/svg/files-app-red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/data/svg/settings-admin-red.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions tests/lib/App/AppManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,35 @@ public function testGetAppPath() {
$this->assertEquals(\OC::$SERVERROOT . '/apps/files', $this->manager->getAppPath('files'));
}

public function testGetAppPathSymlink() {
$fakeAppDirname = sha1(uniqid('test', true));
$fakeAppPath = sys_get_temp_dir() . '/' . $fakeAppDirname;
$fakeAppLink = \OC::$SERVERROOT . '/' . $fakeAppDirname;

mkdir($fakeAppPath);
if (symlink($fakeAppPath, $fakeAppLink) === false) {
$this->markTestSkipped('Failed to create symlink');
}

// Use the symlink as the app path
\OC::$APPSROOTS[] = [
'path' => $fakeAppLink,
'url' => \OC::$WEBROOT . '/' . $fakeAppDirname,
'writable' => false,
];

$fakeTestAppPath = $fakeAppPath . '/' . 'test-test-app';
mkdir($fakeTestAppPath);

$generatedAppPath = $this->manager->getAppPath('test-test-app');

rmdir($fakeTestAppPath);
unlink($fakeAppLink);
rmdir($fakeAppPath);

$this->assertEquals($fakeAppLink . '/test-test-app', $generatedAppPath);
}

public function testGetAppPathFail() {
$this->expectException(AppPathNotFoundException::class);
$this->manager->getAppPath('testnotexisting');
Expand Down