Skip to content

Commit e7479be

Browse files
committed
Dispatchable Resource Tests
1 parent ac3b288 commit e7479be

29 files changed

+62
-83
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock
File renamed without changes.

resources/css/do-not-modify.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* @do-not-dispatch */
2+
body {
3+
background: yellow url(../img/x.jpg);
4+
}
File renamed without changes.

resources/css/test.css

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
11
body {
22
background: yellow url(../img/x.jpg);
33
}
4+
5+
body.nofile {
6+
background: yellow url();
7+
}
8+
9+
body.localfile {
10+
background: yellow url('css.jpg');
11+
}
12+
13+
14+
body.subfile {
15+
background: yellow url('sub/subimg.jpg');
16+
}
17+
18+
body.externalfile {
19+
background: yellow url('http://www.example.com/background.jpg');
20+
}
21+
22+
body.append {
23+
background: yellow url(../img/x.jpg?x=y&request=b);
24+
}
25+
26+
html {
27+
background: yellow url(../img/missing-file.jpg);
28+
}
29+
30+
#invalid-relative {
31+
background: yellow url(../../../img/missing-file.jpg);
32+
}

src/Manager/ResourceManager.php

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public function getResourceUri($relativeFullPath): ?string
5050
return $relativeFullPath;
5151
}
5252
$hash = $this->getFileHash($this->getFilePath($relativeFullPath));
53+
if(!$hash)
54+
{
55+
return null;
56+
}
5357
return Path::custom('/', array_merge($this->_baseUri, [$hash, $relativeFullPath]));
5458
}
5559

@@ -79,6 +83,10 @@ public function getFilePath($relativePath)
7983

8084
protected function getFileHash($fullPath)
8185
{
86+
if(!file_exists($fullPath))
87+
{
88+
return null;
89+
}
8290
$key = 'pdspfh-' . md5($fullPath) . '-' . filectime($fullPath);
8391

8492
if(function_exists("apcu_fetch"))

src/Resources/AbstractDispatchableResource.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ public function setContent($content)
4646
*/
4747
protected function _processContent()
4848
{
49-
if(strpos($this->_content, '@' . 'do-not-dispatch') !== false)
49+
//Treat as a standard resource if no resource manager has been set.
50+
if(!isset($this->_manager))
5051
{
5152
return;
5253
}
5354

54-
//Treat as a standard resource if no resource manager has been set.
55-
if(!isset($this->_manager))
55+
//Do not modify file content
56+
if(strpos($this->_content, '@' . 'do-not-dispatch') !== false)
5657
{
5758
return;
5859
}
@@ -95,7 +96,7 @@ protected function _dispatchNestedUrl($uri)
9596

9697
if(empty($url))
9798
{
98-
return $uri[0];
99+
return "url('" . ($path ?? $uri[0]) . "')";
99100
}
100101

101102
if(!empty($append))
@@ -115,14 +116,19 @@ protected function _dispatchNestedUrl($uri)
115116
*/
116117
protected function makeFullPath($relativePath, $workingDirectory)
117118
{
119+
if($relativePath == '.')
120+
{
121+
return $workingDirectory;
122+
}
118123
$levelUps = substr_count($relativePath, '../');
119124
if($levelUps > 0)
120125
{
121126
$relativePath = str_replace('../', '', $relativePath);
122127
$workingDirectoryParts = explode('/', $workingDirectory);
123-
if(count($workingDirectoryParts) <= $levelUps)
128+
if($levelUps > count($workingDirectoryParts))
124129
{
125-
return $relativePath;
130+
//Relative to this directory is not allowed
131+
return null;
126132
}
127133
return implode('/', array_slice($workingDirectoryParts, $levelUps)) . $relativePath;
128134
}

tests/Resources/AbstractDispatchableResourceTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@ public function testProcessesContent()
2222
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'test.css')));
2323
$content = $resource->getContent();
2424
$this->assertContains('url(\'r/d41d8cd9/img/x.jpg\')', $content);
25+
$this->assertContains('url(\'r/d41d8cd9/css/css.jpg\')', $content);
26+
$this->assertContains('url(\'http://www.example.com/background.jpg\')', $content);
27+
$this->assertContains('url(\'img/missing-file.jpg\')', $content);
28+
29+
$resource->setProcessingPath('css/do-not-modify.css');
30+
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'do-not-modify.css')));
31+
$this->assertContains('url(../img/x.jpg)', $resource->getContent());
2532
}
2633
}

tests/asset/checksum.txt

-1
This file was deleted.

tests/asset/dispatch.test.js

-12
This file was deleted.

tests/asset/includes/base.scss

-6
This file was deleted.

tests/asset/includes/constants.scss

-2
This file was deleted.

tests/asset/no-parse-test.css

-2
This file was deleted.

tests/asset/test-import.expect.css

-9
This file was deleted.

tests/asset/test.js

-4
This file was deleted.

tests/asset/test.unknown

-1
This file was deleted.

tests/asset/test2.min.js

-4
This file was deleted.

tests/asset/tester.css

-1
This file was deleted.

tests/asset/tests.min.js

-1
This file was deleted.

tests/asset/testurl.js

-4
This file was deleted.

tests/asset2/sub/subsub/tester.css

-1
This file was deleted.

tests/asset2/sub/test.css

-1
This file was deleted.

tests/asset2/sub/tester.css

-1
This file was deleted.

tests/asset2/sub2/x.jpg

Whitespace-only changes.

tests/asset2/test.css

-1
This file was deleted.

tests/asset3/expect.css

-9
This file was deleted.

tests/asset3/includes/base.scss

-6
This file was deleted.

tests/asset3/includes/constants.scss

-2
This file was deleted.

tests/asset3/test.scss

-9
This file was deleted.

0 commit comments

Comments
 (0)