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
17 changes: 11 additions & 6 deletions src/Http/Middleware/AddViewPaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ private function update()

private function updatePaths()
{
$site = $this->site;

$paths = collect($this->paths)->flatMap(function ($path) use ($site) {
$paths = collect($this->paths)->flatMap(function ($path) {
return [
$path.'/'.$site,
$this->sitePath($path),
$path,
];
})->filter()->values()->all();
Expand All @@ -53,15 +51,22 @@ private function updateHints()
foreach ($this->hints as $namespace => $paths) {
$paths = collect($paths)->flatMap(function ($path) {
return [
$path.'/'.$this->site,
$this->sitePath($path),
$path,
];
})->values();
})->filter()->values();

$this->finder->replaceNamespace($namespace, $paths->all());
}
}

private function sitePath($path)
{
$sitePath = $path.'/'.$this->site;

return is_dir($sitePath) ? $sitePath : null;
}

private function restore()
{
$this->finder->setPaths($this->paths);
Expand Down
132 changes: 88 additions & 44 deletions tests/Http/Middleware/AddViewPathsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,41 @@
use Statamic\Facades\Site;
use Statamic\Http\Middleware\AddViewPaths;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;

class AddViewPathsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

mkdir(__DIR__.'/tmp/views/french', 0755, true);
mkdir(__DIR__.'/tmp/other/views/english', 0755, true);
}

public function tearDown(): void
{
app('files')->deleteDirectory(__DIR__.'/tmp');

parent::tearDown();
}

#[Test]
#[DataProvider('viewPathProvider')]
public function adds_view_paths($isAmpEnabled, $requestUrl, $expectedPaths)
public function adds_view_paths($requestUrl, $expectedPaths)
{
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]);

view()->getFinder()->setPaths($originalPaths = [
'/path/to/views',
'/path/to/other/views',
__DIR__.'/tmp/views',
__DIR__.'/tmp/other/views',
]);

config(['statamic.amp.enabled' => $isAmpEnabled]);

$this->setCurrentSiteBasedOnUrl($requestUrl);

$request = $this->createRequest($requestUrl);
Expand All @@ -57,8 +69,8 @@ public function adds_namespaced_view_paths($requestUrl, $expectedPaths)
]);

view()->getFinder()->replaceNamespace('foo', [
'/path/to/views',
'/path/to/other',
__DIR__.'/tmp/views',
__DIR__.'/tmp/other/views',
]);
$originalHints = view()->getFinder()->getHints()['foo'];

Expand All @@ -78,10 +90,58 @@ public function adds_namespaced_view_paths($requestUrl, $expectedPaths)
$this->assertEquals($originalHints, Arr::get(view()->getFinder()->getHints(), 'foo'));
}

#[Test]
public function adds_no_view_paths_on_single_site_when_site_directory_does_not_exist()
{
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
]);

view()->getFinder()->setPaths($originalPaths = [__DIR__.'/tmp/views']);
view()->getFinder()->replaceNamespace('foo', $originalHints = [__DIR__.'/tmp/views']);

$request = $this->createRequest('/test');
$handled = false;

(new AddViewPaths())->handle($request, function () use ($originalPaths, $originalHints, &$handled) {
$this->assertEquals($originalPaths, view()->getFinder()->getPaths());
$this->assertEquals($originalHints, Arr::get(view()->getFinder()->getHints(), 'foo'));
$handled = true;

return new Response;
});

$this->assertTrue($handled);
}

#[Test]
public function adds_view_paths_on_single_site_when_site_directory_exists()
{
$this->setSites([
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
]);

view()->getFinder()->setPaths([__DIR__.'/tmp/other/views']);

$request = $this->createRequest('/test');
$handled = false;

(new AddViewPaths())->handle($request, function () use (&$handled) {
$this->assertEquals([
__DIR__.'/tmp/other/views/english',
__DIR__.'/tmp/other/views',
], view()->getFinder()->getPaths());
$handled = true;

return new Response;
});

$this->assertTrue($handled);
}

private function setCurrentSiteBasedOnUrl($requestUrl)
{
$url = 'http://localhost'.Str::removeLeft($requestUrl, '/amp');
$site = Site::findByUrl($url);
$site = Site::findByUrl('http://localhost'.$requestUrl);
Site::setCurrent($site->handle());
}

Expand All @@ -95,52 +155,36 @@ private function createRequest($url)
public static function viewPathProvider()
{
return [
'amp enabled, amp request' => [true, '/amp/fr/test', [
'/path/to/views/french',
'/path/to/views',
'/path/to/other/views/french',
'/path/to/other/views',
'site directory exists in first path' => ['/fr/test', [
__DIR__.'/tmp/views/french',
__DIR__.'/tmp/views',
__DIR__.'/tmp/other/views',
]],
'amp enabled, non-amp request' => [true, '/fr/test', [
'/path/to/views/french',
'/path/to/views',
'/path/to/other/views/french',
'/path/to/other/views',
]],
'amp disabled, default site' => [false, '/test', [
'/path/to/views/english',
'/path/to/views',
'/path/to/other/views/english',
'/path/to/other/views',
]],
'amp disabled, second site' => [false, '/fr/test', [
'/path/to/views/french',
'/path/to/views',
'/path/to/other/views/french',
'/path/to/other/views',
'site directory exists in second path' => ['/test', [
__DIR__.'/tmp/views',
__DIR__.'/tmp/other/views/english',
__DIR__.'/tmp/other/views',
]],
];
}

public static function namespacedViewPathProvider()
{
return [
'default site' => [
'/test',
'site directory exists in first path' => [
'/fr/test',
[
'/path/to/views/english',
'/path/to/views',
'/path/to/other/english',
'/path/to/other',
__DIR__.'/tmp/views/french',
__DIR__.'/tmp/views',
__DIR__.'/tmp/other/views',
],
],
'second site' => [
'/fr/test',
'site directory exists in second path' => [
'/test',
[
'/path/to/views/french',
'/path/to/views',
'/path/to/other/french',
'/path/to/other',
__DIR__.'/tmp/views',
__DIR__.'/tmp/other/views/english',
__DIR__.'/tmp/other/views',
],
],
];
Expand Down
Loading