1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
From 06ba1a8a02f08e066ad97cdd192534c296f3a640 Mon Sep 17 00:00:00 2001
From: Kyle Fazzari <kyrofa@ubuntu.com>
Date: Tue, 7 Nov 2017 14:54:21 -0800
Subject: [PATCH] JSResourceLocator: account for symlinks in app path
Signed-off-by: Kyle Fazzari <kyrofa@ubuntu.com>
---
lib/private/Template/JSResourceLocator.php | 11 +-
tests/lib/Template/CSSResourceLocatorTest.php | 148 ++++++++++++++++++++++++++
tests/lib/Template/JSResourceLocatorTest.php | 139 ++++++++++++++++++++++++
3 files changed, 295 insertions(+), 3 deletions(-)
create mode 100644 tests/lib/Template/CSSResourceLocatorTest.php
create mode 100644 tests/lib/Template/JSResourceLocatorTest.php
diff --git a/lib/private/Template/JSResourceLocator.php b/lib/private/Template/JSResourceLocator.php
index 97a9eacedf5..228fa09e821 100644
--- a/lib/private/Template/JSResourceLocator.php
+++ b/lib/private/Template/JSResourceLocator.php
@@ -75,9 +75,14 @@ public function doFind($script) {
$app_path = \OC_App::getAppPath($app);
$app_url = \OC_App::getAppWebPath($app);
+ // Account for the possibility of having symlinks in app path. Doing
+ // this in a separate variable, because an empty argument to realpath
+ // gets turned into cwd, which makes it hard to see if app_path got set.
+ $real_app_path = realpath($app_path);
+
// missing translations files fill be ignored
if (strpos($script, 'l10n/') === 0) {
- $this->appendIfExist($app_path, $script . '.js', $app_url);
+ $this->appendIfExist($real_app_path, $script . '.js', $app_url);
return;
}
@@ -89,8 +94,8 @@ public function doFind($script) {
return;
}
- if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) {
- $this->append($app_path, $script . '.js', $app_url);
+ if (!$this->cacheAndAppendCombineJsonIfExist($real_app_path, $script.'.json', $app)) {
+ $this->append($real_app_path, $script . '.js', $app_url);
}
}
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 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Kyle Fazzari <kyrofa@ubuntu.com>
+ *
+ * @author Kyle Fazzari <kyrofa@ubuntu.com>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+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 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Kyle Fazzari <kyrofa@ubuntu.com>
+ *
+ * @author Kyle Fazzari <kyrofa@ubuntu.com>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+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);
+ }
+}
|