diff --git a/src/Tags/MountUrl.php b/src/Tags/MountUrl.php index c6b6e0741d..be49aa54c0 100644 --- a/src/Tags/MountUrl.php +++ b/src/Tags/MountUrl.php @@ -29,6 +29,10 @@ private function mount($handle) return; } - return $collection->url(Site::current()->handle()); + $site = $this->params->get('site') ?? Site::current()->handle(); + + return $site == Site::current()->handle() + ? $collection->url($site) + : $collection->absoluteUrl($site); } } diff --git a/tests/Tags/MountUrlTagTest.php b/tests/Tags/MountUrlTagTest.php index 861a9f5734..634537ebdd 100644 --- a/tests/Tags/MountUrlTagTest.php +++ b/tests/Tags/MountUrlTagTest.php @@ -13,9 +13,10 @@ class MountUrlTagTest extends TestCase { use PreventSavingStacheItemsToDisk; - /** @test */ - public function it_gets_collection_mount() + public function setUp(): void { + parent::setUp(); + Site::setConfig(['sites' => [ 'english' => ['url' => 'http://localhost/', 'locale' => 'en'], 'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], @@ -25,10 +26,16 @@ public function it_gets_collection_mount() 'english' => 'pages/{slug}', 'french' => 'le-pages/{slug}', ])->save(); + $mountEn = EntryFactory::collection('pages')->slug('blog')->locale('english')->id('blog-en')->create(); $mountFr = EntryFactory::collection('pages')->slug('le-blog')->locale('french')->origin('blog-en')->id('blog-fr')->create(); + Collection::make('blog')->routes('{mount}/{slug}')->mount($mountEn->id())->save(); + } + /** @test */ + public function it_gets_collection_mount() + { $this->assertParseEquals('/pages/blog', '{{ mount_url:blog }}'); $this->assertParseEquals('/pages/blog', '{{ mount_url handle="blog" }}'); @@ -37,6 +44,19 @@ public function it_gets_collection_mount() $this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url handle="blog" }}'); } + /** @test */ + public function it_can_link_to_selected_site() + { + $this->assertParseEquals('/pages/blog', '{{ mount_url:blog }}'); + $this->assertParseEquals('/pages/blog', '{{ mount_url:blog site="english" }}'); + $this->assertParseEquals('http://localhost/fr/le-pages/le-blog', '{{ mount_url:blog site="french" }}'); + + Site::setCurrent('french'); + $this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url:blog }}'); + $this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url:blog site="french" }}'); + $this->assertParseEquals('http://localhost/pages/blog', '{{ mount_url:blog site="english" }}'); + } + private function assertParseEquals($expected, $template, $context = []) { $this->assertEquals($expected, (string) Antlers::parse($template, $context));