From 476c2be5a62579d755157a27de67d217bea23593 Mon Sep 17 00:00:00 2001 From: Maximilian Walter Date: Mon, 9 Sep 2024 22:54:33 +0200 Subject: [PATCH 1/7] Add XML for OpenSearch --- app/App/MetaController.php | 10 ++++++++++ resources/views/layouts/base.blade.php | 3 +++ resources/views/misc/opensearch.blade.php | 11 +++++++++++ routes/web.php | 1 + 4 files changed, 25 insertions(+) create mode 100644 resources/views/misc/opensearch.blade.php diff --git a/app/App/MetaController.php b/app/App/MetaController.php index 1515b4f7e..a94334c58 100644 --- a/app/App/MetaController.php +++ b/app/App/MetaController.php @@ -64,4 +64,14 @@ class MetaController extends Controller 'jsLibData' => file_get_contents(base_path('dev/licensing/js-library-licenses.txt')), ]); } + + /** + * Show the view for /opensearch.xml. + */ + public function opensearch() + { + return response() + ->view('misc.opensearch') + ->header('Content-Type', 'application/opensearchdescription+xml'); + } } diff --git a/resources/views/layouts/base.blade.php b/resources/views/layouts/base.blade.php index 4d4d07dc2..ddecb8e76 100644 --- a/resources/views/layouts/base.blade.php +++ b/resources/views/layouts/base.blade.php @@ -32,6 +32,9 @@ + + + @yield('head') diff --git a/resources/views/misc/opensearch.blade.php b/resources/views/misc/opensearch.blade.php new file mode 100644 index 000000000..cfcca8e41 --- /dev/null +++ b/resources/views/misc/opensearch.blade.php @@ -0,0 +1,11 @@ + + {{ setting('app-name') }} + Search {{ setting('app-name') }} + {{ setting('app-icon') ?: url('/icon.png') }} + {{ setting('app-icon-180') ?: url('/icon-180.png') }} + {{ setting('app-icon-128') ?: url('/icon-128.png') }} + {{ setting('app-icon-64') ?: url('/icon-64.png') }} + {{ setting('app-icon-32') ?: url('/icon-32.png') }} + + + diff --git a/routes/web.php b/routes/web.php index 58b8f4e54..81b938f32 100644 --- a/routes/web.php +++ b/routes/web.php @@ -23,6 +23,7 @@ Route::get('/robots.txt', [MetaController::class, 'robots']); Route::get('/favicon.ico', [MetaController::class, 'favicon']); Route::get('/manifest.json', [MetaController::class, 'pwaManifest']); Route::get('/licenses', [MetaController::class, 'licenses']); +Route::get('/opensearch.xml', [MetaController::class, 'opensearch']); // Authenticated routes... Route::middleware('auth')->group(function () { From c110a97d8a871b3f2e56af728c4460096399cbf3 Mon Sep 17 00:00:00 2001 From: Maximilian Walter Date: Sat, 14 Sep 2024 15:24:42 +0200 Subject: [PATCH 2/7] Remove unofficial method-attribute from OpenSearch-XML --- resources/views/misc/opensearch.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/misc/opensearch.blade.php b/resources/views/misc/opensearch.blade.php index cfcca8e41..2b5a6c143 100644 --- a/resources/views/misc/opensearch.blade.php +++ b/resources/views/misc/opensearch.blade.php @@ -6,6 +6,6 @@ {{ setting('app-icon-128') ?: url('/icon-128.png') }} {{ setting('app-icon-64') ?: url('/icon-64.png') }} {{ setting('app-icon-32') ?: url('/icon-32.png') }} - + From 4f890c431c6fb5f83e047eebcea7f078cb1c9d5c Mon Sep 17 00:00:00 2001 From: Maximilian Walter Date: Sat, 14 Sep 2024 15:31:56 +0200 Subject: [PATCH 3/7] Limit short-name for OpenSearch XML to 16 characters The specification does not allow more than 16 characters. --- resources/views/misc/opensearch.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/misc/opensearch.blade.php b/resources/views/misc/opensearch.blade.php index 2b5a6c143..e83e83c12 100644 --- a/resources/views/misc/opensearch.blade.php +++ b/resources/views/misc/opensearch.blade.php @@ -1,5 +1,5 @@ - {{ setting('app-name') }} + {{ mb_strimwidth(setting('app-name'), 0, 16) }} Search {{ setting('app-name') }} {{ setting('app-icon') ?: url('/icon.png') }} {{ setting('app-icon-180') ?: url('/icon-180.png') }} From a5b031f9060141364ed9152eacdb9b494cfa76a7 Mon Sep 17 00:00:00 2001 From: Maximilian Walter Date: Sat, 14 Sep 2024 15:43:46 +0200 Subject: [PATCH 4/7] Translatable description for OpenSearch XML --- lang/en/common.php | 3 +++ resources/views/misc/opensearch.blade.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lang/en/common.php b/lang/en/common.php index 266174eed..b05169bb2 100644 --- a/lang/en/common.php +++ b/lang/en/common.php @@ -107,4 +107,7 @@ return [ // Not directly used but available for convenience to users. 'privacy_policy' => 'Privacy Policy', 'terms_of_service' => 'Terms of Service', + + // OpenSearch + 'opensearch_description' => 'Search :appName', ]; diff --git a/resources/views/misc/opensearch.blade.php b/resources/views/misc/opensearch.blade.php index e83e83c12..366c237b2 100644 --- a/resources/views/misc/opensearch.blade.php +++ b/resources/views/misc/opensearch.blade.php @@ -1,6 +1,6 @@ {{ mb_strimwidth(setting('app-name'), 0, 16) }} - Search {{ setting('app-name') }} + {{ trans('common.opensearch_description', ['appName' => setting('app-name')]) }} {{ setting('app-icon') ?: url('/icon.png') }} {{ setting('app-icon-180') ?: url('/icon-180.png') }} {{ setting('app-icon-128') ?: url('/icon-128.png') }} From 1302e3c959158be0e5a2a3b34343f9cd828544a3 Mon Sep 17 00:00:00 2001 From: Maximilian Walter Date: Sat, 14 Sep 2024 16:13:17 +0200 Subject: [PATCH 5/7] Add missing XML declaration to OpenSearch endpoint --- resources/views/misc/opensearch.blade.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/views/misc/opensearch.blade.php b/resources/views/misc/opensearch.blade.php index 366c237b2..5c1503fc4 100644 --- a/resources/views/misc/opensearch.blade.php +++ b/resources/views/misc/opensearch.blade.php @@ -1,3 +1,4 @@ + {{ mb_strimwidth(setting('app-name'), 0, 16) }} {{ trans('common.opensearch_description', ['appName' => setting('app-name')]) }} From 2f74cfb42c90785f9f1735319af28727cdec407a Mon Sep 17 00:00:00 2001 From: Maximilian Walter Date: Sat, 14 Sep 2024 16:21:01 +0200 Subject: [PATCH 6/7] Add test for OpenSearch endpoint --- tests/OpensearchTest.php | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/OpensearchTest.php diff --git a/tests/OpensearchTest.php b/tests/OpensearchTest.php new file mode 100644 index 000000000..a68076b43 --- /dev/null +++ b/tests/OpensearchTest.php @@ -0,0 +1,42 @@ +get('/opensearch.xml'); + $resp->assertOk(); + + $html = $this->withHtml($resp); + + $html->assertElementExists('OpenSearchDescription > ShortName'); + $html->assertElementContains('OpenSearchDescription > ShortName', mb_strimwidth($appName, 0, 16)); + + $html->assertElementExists('OpenSearchDescription > Description'); + $html->assertElementContains('OpenSearchDescription > Description', trans('common.opensearch_description', [ + 'appName' => $appName, + ])); + + $html->assertElementExists('OpenSearchDescription > Image'); + + $html->assertElementExists('OpenSearchDescription > Url[rel="results"][template="' . htmlspecialchars($resultUrl) . '"]'); + $html->assertElementExists('OpenSearchDescription > Url[rel="self"][template="' . htmlspecialchars($selfUrl) . '"]'); + } + + public function test_opensearch_linked_to_from_home() + { + $appName = setting('app-name'); + $endpointUrl = url('/opensearch.xml'); + + $resp = $this->asViewer()->get('/'); + $html = $this->withHtml($resp); + + $html->assertElementExists('head > link[rel="search"][type="application/opensearchdescription+xml"][title="' . htmlspecialchars($appName) . '"][href="' . htmlspecialchars($endpointUrl) . '"]'); + } +} From 514db60617780de889645513ad613d8ff0ba3108 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Mon, 30 Sep 2024 17:07:53 +0100 Subject: [PATCH 7/7] Tests: Categorised up meta tests Extracted robots.txt tests into its own file to fit into new folder. Also tweaked open search tests a tad to specifically check long app names. --- tests/{ => Meta}/HelpTest.php | 4 +++- tests/{ => Meta}/LicensesTest.php | 4 +++- tests/{ => Meta}/OpenGraphTest.php | 3 ++- tests/{ => Meta}/OpensearchTest.php | 14 +++++------ tests/{ => Meta}/PwaManifestTest.php | 4 +++- tests/Meta/RobotsTest.php | 35 ++++++++++++++++++++++++++++ tests/PublicActionTest.php | 27 --------------------- 7 files changed, 53 insertions(+), 38 deletions(-) rename tests/{ => Meta}/HelpTest.php (94%) rename tests/{ => Meta}/LicensesTest.php (94%) rename tests/{ => Meta}/OpenGraphTest.php (98%) rename tests/{ => Meta}/OpensearchTest.php (84%) rename tests/{ => Meta}/PwaManifestTest.php (98%) create mode 100644 tests/Meta/RobotsTest.php diff --git a/tests/HelpTest.php b/tests/Meta/HelpTest.php similarity index 94% rename from tests/HelpTest.php rename to tests/Meta/HelpTest.php index 9cf80717f..e1de96bc8 100644 --- a/tests/HelpTest.php +++ b/tests/Meta/HelpTest.php @@ -1,6 +1,8 @@ put('app-name', $appName); $resultUrl = url('/search') . '?term={searchTerms}'; $selfUrl = url('/opensearch.xml'); @@ -17,14 +20,11 @@ class OpensearchTest extends TestCase $html->assertElementExists('OpenSearchDescription > ShortName'); $html->assertElementContains('OpenSearchDescription > ShortName', mb_strimwidth($appName, 0, 16)); + $html->assertElementNotContains('OpenSearchDescription > ShortName', $appName); $html->assertElementExists('OpenSearchDescription > Description'); - $html->assertElementContains('OpenSearchDescription > Description', trans('common.opensearch_description', [ - 'appName' => $appName, - ])); - + $html->assertElementContains('OpenSearchDescription > Description', "Search {$appName}"); $html->assertElementExists('OpenSearchDescription > Image'); - $html->assertElementExists('OpenSearchDescription > Url[rel="results"][template="' . htmlspecialchars($resultUrl) . '"]'); $html->assertElementExists('OpenSearchDescription > Url[rel="self"][template="' . htmlspecialchars($selfUrl) . '"]'); } diff --git a/tests/PwaManifestTest.php b/tests/Meta/PwaManifestTest.php similarity index 98% rename from tests/PwaManifestTest.php rename to tests/Meta/PwaManifestTest.php index c66f8b360..fc6d19b54 100644 --- a/tests/PwaManifestTest.php +++ b/tests/Meta/PwaManifestTest.php @@ -1,6 +1,8 @@ get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); + + $this->setSettings(['app-public' => 'true']); + + $resp = $this->get('/robots.txt'); + $resp->assertSee("User-agent: *\nDisallow:"); + $resp->assertDontSee('Disallow: /'); + } + + public function test_robots_effected_by_setting() + { + $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); + + config()->set('app.allow_robots', true); + + $resp = $this->get('/robots.txt'); + $resp->assertSee("User-agent: *\nDisallow:"); + $resp->assertDontSee('Disallow: /'); + + // Check config overrides app-public setting + config()->set('app.allow_robots', false); + $this->setSettings(['app-public' => 'true']); + $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); + } +} diff --git a/tests/PublicActionTest.php b/tests/PublicActionTest.php index 875b279a8..76745aaac 100644 --- a/tests/PublicActionTest.php +++ b/tests/PublicActionTest.php @@ -128,33 +128,6 @@ class PublicActionTest extends TestCase $resp->assertDontSee($page->name); } - public function test_robots_effected_by_public_status() - { - $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); - - $this->setSettings(['app-public' => 'true']); - - $resp = $this->get('/robots.txt'); - $resp->assertSee("User-agent: *\nDisallow:"); - $resp->assertDontSee('Disallow: /'); - } - - public function test_robots_effected_by_setting() - { - $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); - - config()->set('app.allow_robots', true); - - $resp = $this->get('/robots.txt'); - $resp->assertSee("User-agent: *\nDisallow:"); - $resp->assertDontSee('Disallow: /'); - - // Check config overrides app-public setting - config()->set('app.allow_robots', false); - $this->setSettings(['app-public' => 'true']); - $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); - } - public function test_default_favicon_file_created_upon_access() { $faviconPath = public_path('favicon.ico');