From 06ba1a8a02f08e066ad97cdd192534c296f3a640 Mon Sep 17 00:00:00 2001 From: Kyle Fazzari Date: Tue, 7 Nov 2017 14:54:21 -0800 Subject: JSResourceLocator: account for symlinks in app path Signed-off-by: Kyle Fazzari --- tests/lib/Template/CSSResourceLocatorTest.php | 148 ++++++++++++++++++++++++++ tests/lib/Template/JSResourceLocatorTest.php | 139 ++++++++++++++++++++++++ 2 files changed, 287 insertions(+) create mode 100644 tests/lib/Template/CSSResourceLocatorTest.php create mode 100644 tests/lib/Template/JSResourceLocatorTest.php (limited to 'tests') diff --git a/tests/lib/Template/CSSResourceLocatorTest.php b/tests/lib/Template/CSSResourceLocatorTest.php new file mode 100644 index 00000000000..bd13487282e --- /dev/null +++ b/tests/lib/Template/CSSResourceLocatorTest.php @@ -0,0 +1,148 @@ + + * + * @author Kyle Fazzari + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace Test\Template; + +use OC\Files\AppData\Factory; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\IConfig; +use OCA\Theming\ThemingDefaults; +use OCP\ICache; +use OC\Template\SCSSCacher; +use OC\Template\CSSResourceLocator; + +function rrmdir($directory) { + $files = array_diff(scandir($directory), array('.','..')); + foreach ($files as $file) { + if (is_dir($directory . '/' . $file)) { + rrmdir($directory . '/' . $file); + } else { + unlink($directory . '/' . $file); + } + } + return rmdir($directory); +} + +function randomString() { + return sha1(uniqid(mt_rand(), true)); +} + +class CSSResourceLocatorTest extends \Test\TestCase { + /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ + protected $appData; + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + protected $urlGenerator; + /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */ + protected $themingDefaults; + /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */ + protected $depsCache; + /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ + protected $logger; + + protected function setUp() { + parent::setUp(); + + $this->logger = $this->createMock(ILogger::class); + $this->appData = $this->createMock(IAppData::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->config = $this->createMock(IConfig::class); + $this->depsCache = $this->createMock(ICache::class); + $this->themingDefaults = $this->createMock(ThemingDefaults::class); + } + + private function cssResourceLocator() { + /** @var Factory|\PHPUnit_Framework_MockObject_MockObject $factory */ + $factory = $this->createMock(Factory::class); + $factory->method('get')->with('css')->willReturn($this->appData); + $scssCacher = new SCSSCacher( + $this->logger, + $factory, + $this->urlGenerator, + $this->config, + $this->themingDefaults, + \OC::$SERVERROOT, + $this->depsCache + ); + return new CSSResourceLocator( + $this->logger, + 'theme', + array('core'=>'map'), + array('3rd'=>'party'), + $scssCacher + ); + } + + public function testConstructor() { + $locator = $this->cssResourceLocator(); + $this->assertAttributeEquals('theme', 'theme', $locator); + $this->assertAttributeEquals('core', 'serverroot', $locator); + $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator); + $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator); + $this->assertAttributeEquals('map', 'webroot', $locator); + $this->assertAttributeEquals(array(), 'resources', $locator); + } + + public function testFindWithAppPathSymlink() { + // First create new apps path, and a symlink to it + $apps_dirname = randomString(); + $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname; + $new_apps_path_symlink = $new_apps_path . '_link'; + mkdir($new_apps_path); + symlink($apps_dirname, $new_apps_path_symlink); + + // Create an app within that path + mkdir($new_apps_path . '/' . 'test-app'); + + // Use the symlink as the app path + \OC::$APPSROOTS[] = [ + 'path' => $new_apps_path_symlink, + 'url' => '/apps-test', + 'writable' => false, + ]; + + $locator = $this->cssResourceLocator(); + $locator->find(array('test-app/test-file')); + + $resources = $locator->getResources(); + $this->assertCount(1, $resources); + $resource = $resources[0]; + $this->assertCount(3, $resource); + $root = $resource[0]; + $webRoot = $resource[1]; + $file = $resource[2]; + + $expectedRoot = $new_apps_path . '/test-app'; + $expectedWebRoot = '/apps-test/test-app'; + $expectedFile = 'test-file.css'; + + $this->assertEquals($expectedRoot, $root, + 'Ensure the app path symlink is resolved into the real path'); + $this->assertEquals($expectedWebRoot, $webRoot); + $this->assertEquals($expectedFile, $file); + + rrmdir($new_apps_path); + } +} diff --git a/tests/lib/Template/JSResourceLocatorTest.php b/tests/lib/Template/JSResourceLocatorTest.php new file mode 100644 index 00000000000..49f0fb67f48 --- /dev/null +++ b/tests/lib/Template/JSResourceLocatorTest.php @@ -0,0 +1,139 @@ + + * + * @author Kyle Fazzari + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace Test\Template; + +use OC\Template\JSCombiner; +use OCP\Files\IAppData; +use OCP\IURLGenerator; +use OCP\ICache; +use OC\SystemConfig; +use OCP\ILogger; +use OC\Template\JSResourceLocator; + +function rrmdir($directory) { + $files = array_diff(scandir($directory), array('.','..')); + foreach ($files as $file) { + if (is_dir($directory . '/' . $file)) { + rrmdir($directory . '/' . $file); + } else { + unlink($directory . '/' . $file); + } + } + return rmdir($directory); +} + +function randomString() { + return sha1(uniqid(mt_rand(), true)); +} + +class JSResourceLocatorTest extends \Test\TestCase { + /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ + protected $appData; + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + protected $urlGenerator; + /** @var SystemConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + /** @var ICache|\PHPUnit_Framework_MockObject_MockObject */ + protected $depsCache; + /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */ + protected $logger; + + protected function setUp() { + parent::setUp(); + + $this->appData = $this->createMock(IAppData::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->config = $this->createMock(SystemConfig::class); + $this->depsCache = $this->createMock(ICache::class); + $this->logger = $this->createMock(ILogger::class); + } + + private function jsResourceLocator() { + $jsCombiner = new JSCombiner( + $this->appData, + $this->urlGenerator, + $this->depsCache, + $this->config, + $this->logger + ); + return new JSResourceLocator( + $this->logger, + 'theme', + array('core'=>'map'), + array('3rd'=>'party'), + $jsCombiner + ); + } + + public function testConstructor() { + $locator = $this->jsResourceLocator(); + $this->assertAttributeEquals('theme', 'theme', $locator); + $this->assertAttributeEquals('core', 'serverroot', $locator); + $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator); + $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator); + $this->assertAttributeEquals('map', 'webroot', $locator); + $this->assertAttributeEquals(array(), 'resources', $locator); + } + + public function testFindWithAppPathSymlink() { + // First create new apps path, and a symlink to it + $apps_dirname = randomString(); + $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname; + $new_apps_path_symlink = $new_apps_path . '_link'; + mkdir($new_apps_path); + symlink($apps_dirname, $new_apps_path_symlink); + + // Create an app within that path + mkdir($new_apps_path . '/' . 'test-app'); + + // Use the symlink as the app path + \OC::$APPSROOTS[] = [ + 'path' => $new_apps_path_symlink, + 'url' => '/apps-test', + 'writable' => false, + ]; + + $locator = $this->jsResourceLocator(); + $locator->find(array('test-app/test-file')); + + $resources = $locator->getResources(); + $this->assertCount(1, $resources); + $resource = $resources[0]; + $this->assertCount(3, $resource); + $root = $resource[0]; + $webRoot = $resource[1]; + $file = $resource[2]; + + $expectedRoot = $new_apps_path . '/test-app'; + $expectedWebRoot = '/apps-test/test-app'; + $expectedFile = 'test-file.js'; + + $this->assertEquals($expectedRoot, $root, + 'Ensure the app path symlink is resolved into the real path'); + $this->assertEquals($expectedWebRoot, $webRoot); + $this->assertEquals($expectedFile, $file); + + rrmdir($new_apps_path); + } +} -- cgit v1.2.3 From 9c24b7be2d4c85ac175af4657843a4c630cb31e9 Mon Sep 17 00:00:00 2001 From: Kyle Fazzari Date: Wed, 8 Nov 2017 08:31:04 -0800 Subject: tests: change helper scope This seems to be the only way to have the same helpers used between tests in a manner that works for both standalone phpunit and autotest.sh. Signed-off-by: Kyle Fazzari --- tests/lib/Template/CSSResourceLocatorTest.php | 48 +++++++++++++------------- tests/lib/Template/JSResourceLocatorTest.php | 49 ++++++++++++++------------- 2 files changed, 51 insertions(+), 46 deletions(-) (limited to 'tests') diff --git a/tests/lib/Template/CSSResourceLocatorTest.php b/tests/lib/Template/CSSResourceLocatorTest.php index bd13487282e..a16cc18cb0a 100644 --- a/tests/lib/Template/CSSResourceLocatorTest.php +++ b/tests/lib/Template/CSSResourceLocatorTest.php @@ -32,22 +32,6 @@ use OCP\ICache; use OC\Template\SCSSCacher; use OC\Template\CSSResourceLocator; -function rrmdir($directory) { - $files = array_diff(scandir($directory), array('.','..')); - foreach ($files as $file) { - if (is_dir($directory . '/' . $file)) { - rrmdir($directory . '/' . $file); - } else { - unlink($directory . '/' . $file); - } - } - return rmdir($directory); -} - -function randomString() { - return sha1(uniqid(mt_rand(), true)); -} - class CSSResourceLocatorTest extends \Test\TestCase { /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ protected $appData; @@ -95,6 +79,22 @@ class CSSResourceLocatorTest extends \Test\TestCase { ); } + private function rrmdir($directory) { + $files = array_diff(scandir($directory), array('.','..')); + foreach ($files as $file) { + if (is_dir($directory . '/' . $file)) { + $this->rrmdir($directory . '/' . $file); + } else { + unlink($directory . '/' . $file); + } + } + return rmdir($directory); + } + + private function randomString() { + return sha1(uniqid(mt_rand(), true)); + } + public function testConstructor() { $locator = $this->cssResourceLocator(); $this->assertAttributeEquals('theme', 'theme', $locator); @@ -107,24 +107,24 @@ class CSSResourceLocatorTest extends \Test\TestCase { public function testFindWithAppPathSymlink() { // First create new apps path, and a symlink to it - $apps_dirname = randomString(); + $apps_dirname = $this->randomString(); $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname; $new_apps_path_symlink = $new_apps_path . '_link'; mkdir($new_apps_path); symlink($apps_dirname, $new_apps_path_symlink); // Create an app within that path - mkdir($new_apps_path . '/' . 'test-app'); + mkdir($new_apps_path . '/' . 'test-css-app'); // Use the symlink as the app path \OC::$APPSROOTS[] = [ 'path' => $new_apps_path_symlink, - 'url' => '/apps-test', + 'url' => '/css-apps-test', 'writable' => false, ]; $locator = $this->cssResourceLocator(); - $locator->find(array('test-app/test-file')); + $locator->find(array('test-css-app/test-file')); $resources = $locator->getResources(); $this->assertCount(1, $resources); @@ -134,8 +134,8 @@ class CSSResourceLocatorTest extends \Test\TestCase { $webRoot = $resource[1]; $file = $resource[2]; - $expectedRoot = $new_apps_path . '/test-app'; - $expectedWebRoot = '/apps-test/test-app'; + $expectedRoot = $new_apps_path . '/test-css-app'; + $expectedWebRoot = \OC::$WEBROOT . '/css-apps-test/test-css-app'; $expectedFile = 'test-file.css'; $this->assertEquals($expectedRoot, $root, @@ -143,6 +143,8 @@ class CSSResourceLocatorTest extends \Test\TestCase { $this->assertEquals($expectedWebRoot, $webRoot); $this->assertEquals($expectedFile, $file); - rrmdir($new_apps_path); + array_pop(\OC::$APPSROOTS); + unlink($new_apps_path_symlink); + $this->rrmdir($new_apps_path); } } diff --git a/tests/lib/Template/JSResourceLocatorTest.php b/tests/lib/Template/JSResourceLocatorTest.php index 49f0fb67f48..6841ee2fee3 100644 --- a/tests/lib/Template/JSResourceLocatorTest.php +++ b/tests/lib/Template/JSResourceLocatorTest.php @@ -31,22 +31,6 @@ use OC\SystemConfig; use OCP\ILogger; use OC\Template\JSResourceLocator; -function rrmdir($directory) { - $files = array_diff(scandir($directory), array('.','..')); - foreach ($files as $file) { - if (is_dir($directory . '/' . $file)) { - rrmdir($directory . '/' . $file); - } else { - unlink($directory . '/' . $file); - } - } - return rmdir($directory); -} - -function randomString() { - return sha1(uniqid(mt_rand(), true)); -} - class JSResourceLocatorTest extends \Test\TestCase { /** @var IAppData|\PHPUnit_Framework_MockObject_MockObject */ protected $appData; @@ -86,6 +70,23 @@ class JSResourceLocatorTest extends \Test\TestCase { ); } + private function rrmdir($directory) { + $files = array_diff(scandir($directory), array('.','..')); + foreach ($files as $file) { + if (is_dir($directory . '/' . $file)) { + $this->rrmdir($directory . '/' . $file); + } else { + unlink($directory . '/' . $file); + } + } + return rmdir($directory); + } + + private function randomString() { + return sha1(uniqid(mt_rand(), true)); + } + + public function testConstructor() { $locator = $this->jsResourceLocator(); $this->assertAttributeEquals('theme', 'theme', $locator); @@ -98,24 +99,24 @@ class JSResourceLocatorTest extends \Test\TestCase { public function testFindWithAppPathSymlink() { // First create new apps path, and a symlink to it - $apps_dirname = randomString(); + $apps_dirname = $this->randomString(); $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname; $new_apps_path_symlink = $new_apps_path . '_link'; mkdir($new_apps_path); symlink($apps_dirname, $new_apps_path_symlink); // Create an app within that path - mkdir($new_apps_path . '/' . 'test-app'); + mkdir($new_apps_path . '/' . 'test-js-app'); // Use the symlink as the app path \OC::$APPSROOTS[] = [ 'path' => $new_apps_path_symlink, - 'url' => '/apps-test', + 'url' => '/js-apps-test', 'writable' => false, ]; $locator = $this->jsResourceLocator(); - $locator->find(array('test-app/test-file')); + $locator->find(array('test-js-app/test-file')); $resources = $locator->getResources(); $this->assertCount(1, $resources); @@ -125,8 +126,8 @@ class JSResourceLocatorTest extends \Test\TestCase { $webRoot = $resource[1]; $file = $resource[2]; - $expectedRoot = $new_apps_path . '/test-app'; - $expectedWebRoot = '/apps-test/test-app'; + $expectedRoot = $new_apps_path . '/test-js-app'; + $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app'; $expectedFile = 'test-file.js'; $this->assertEquals($expectedRoot, $root, @@ -134,6 +135,8 @@ class JSResourceLocatorTest extends \Test\TestCase { $this->assertEquals($expectedWebRoot, $webRoot); $this->assertEquals($expectedFile, $file); - rrmdir($new_apps_path); + array_pop(\OC::$APPSROOTS); + unlink($new_apps_path_symlink); + $this->rrmdir($new_apps_path); } } -- cgit v1.2.3