From f6d62817eea90f604c9c864ab6350e717dde0187 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:26:31 +0200 Subject: [PATCH 01/30] Add ext-simplexml as requirement --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d1fc4239..0a78a4b2 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "laravel-zero/framework": "^9.1", "league/commonmark": "^2.2", "spatie/yaml-front-matter": "^2.0.7", - "torchlight/torchlight-commonmark": "^0.5.5" + "torchlight/torchlight-commonmark": "^0.5.5", + "ext-simplexml": "*" }, "require-dev": { "phpunit/phpunit": "^7.5.15 || ^8.5 || ^9.0" From 1f669282d727042df5074f0182bf5e0563d07a91 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:26:54 +0200 Subject: [PATCH 02/30] Create basic sitemap generator --- src/Services/SitemapService.php | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/Services/SitemapService.php diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php new file mode 100644 index 00000000..45cde212 --- /dev/null +++ b/src/Services/SitemapService.php @@ -0,0 +1,61 @@ +time_start = microtime(true); + + $this->xmlElement = new SimpleXMLElement(''); + $this->xmlElement->addAttribute('generator', 'HydePHP '.Hyde::version()); + } + + public function generate(): self + { + if (Features::hasBladePages()) { + $this->addToSitemap(BladePage::class); + } + + return $this; + } + + public function addToSitemap(string $model): self + { + $collection = CollectionService::getSourceFileListForModel($model); + + foreach ($collection as $page) { + $this->xmlElement->addChild('url')->addChild('loc', Hyde::uriPath(Hyde::pageLink($page . '.html'))); + } + + return $this; + } + + public function getXML(): string + { + $this->xmlElement->addAttribute('processing_time_ms', (string) round((microtime(true) - $this->time_start) * 1000, 2)); + + return $this->xmlElement->asXML(); + } + + public static function generateSitemap(): string + { + return (new static)->generate()->getXML(); + } + + public static function canGenerateSitemap(): bool + { + return (Hyde::uriPath() !== false); + } +} \ No newline at end of file From 1e27de0e45aa9c9c7def72c5fcea41c3fb959ba7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:28:46 +0200 Subject: [PATCH 03/30] Generate the sitemap after running site build --- src/Commands/HydeBuildStaticSiteCommand.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Commands/HydeBuildStaticSiteCommand.php b/src/Commands/HydeBuildStaticSiteCommand.php index 3c0abac2..ef6c61ed 100644 --- a/src/Commands/HydeBuildStaticSiteCommand.php +++ b/src/Commands/HydeBuildStaticSiteCommand.php @@ -13,6 +13,7 @@ use Hyde\Framework\Models\MarkdownPage; use Hyde\Framework\Models\MarkdownPost; use Hyde\Framework\Services\DiscoveryService; +use Hyde\Framework\Services\SitemapService; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\File; use LaravelZero\Framework\Commands\Command; @@ -169,6 +170,11 @@ public function postBuildActions(): void if ($this->option('run-prod')) { $this->runNodeCommand('npm run prod', 'Building frontend assets for production!'); } + + if (SitemapService::canGenerateSitemap()) { + $this->info('Generating sitemap.xml'); + file_put_contents(Hyde::getSiteOutputPath('sitemap.xml'), SitemapService::generateSitemap()); + } } /** @internal */ From ea88427292bf044804b6b46f26a70307a9acda77 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:29:15 +0200 Subject: [PATCH 04/30] Change order of page builders --- src/Commands/HydeBuildStaticSiteCommand.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Commands/HydeBuildStaticSiteCommand.php b/src/Commands/HydeBuildStaticSiteCommand.php index ef6c61ed..65112ed3 100644 --- a/src/Commands/HydeBuildStaticSiteCommand.php +++ b/src/Commands/HydeBuildStaticSiteCommand.php @@ -67,21 +67,22 @@ public function handle(): int $this->transferMediaAssets(); - if (Features::hasBlogPosts()) { - $this->runBuildAction(MarkdownPost::class); + if (Features::hasBladePages()) { + $this->runBuildAction(BladePage::class); } if (Features::hasMarkdownPages()) { $this->runBuildAction(MarkdownPage::class); } + if (Features::hasBlogPosts()) { + $this->runBuildAction(MarkdownPost::class); + } + if (Features::hasDocumentationPages()) { $this->runBuildAction(DocumentationPage::class); } - if (Features::hasBladePages()) { - $this->runBuildAction(BladePage::class); - } $this->postBuildActions(); From 8dbffcfc9e34a5514005a28f93c9758395eb10f1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:31:18 +0200 Subject: [PATCH 05/30] Create the test class --- .../SitemapServiceTest/SitemapServiceTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php diff --git a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php new file mode 100644 index 00000000..9aaef3ca --- /dev/null +++ b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php @@ -0,0 +1,14 @@ + Date: Wed, 18 May 2022 16:31:43 +0200 Subject: [PATCH 06/30] Add newline after sitemap is generated --- src/Commands/HydeBuildStaticSiteCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Commands/HydeBuildStaticSiteCommand.php b/src/Commands/HydeBuildStaticSiteCommand.php index 65112ed3..7274e845 100644 --- a/src/Commands/HydeBuildStaticSiteCommand.php +++ b/src/Commands/HydeBuildStaticSiteCommand.php @@ -175,6 +175,7 @@ public function postBuildActions(): void if (SitemapService::canGenerateSitemap()) { $this->info('Generating sitemap.xml'); file_put_contents(Hyde::getSiteOutputPath('sitemap.xml'), SitemapService::generateSitemap()); + $this->newLine(); } } From aeb1c4a1acdf4fae2209e54754a7c0fe3875cf04 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:36:08 +0200 Subject: [PATCH 07/30] Add lastmod property --- src/Services/SitemapService.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 45cde212..5d6f745d 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -36,7 +36,11 @@ public function addToSitemap(string $model): self $collection = CollectionService::getSourceFileListForModel($model); foreach ($collection as $page) { - $this->xmlElement->addChild('url')->addChild('loc', Hyde::uriPath(Hyde::pageLink($page . '.html'))); + $urlItem = $this->xmlElement->addChild('url'); + $urlItem->addChild('loc', Hyde::uriPath(Hyde::pageLink($page . '.html'))); + $urlItem->addChild('lastmod', date('c', filemtime( + Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') + ))); } return $this; From efeb74673457691bd61d04e2a03f350de6563eb2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:40:39 +0200 Subject: [PATCH 08/30] Add changefreq daily --- src/Services/SitemapService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 5d6f745d..93d47fc6 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -41,6 +41,7 @@ public function addToSitemap(string $model): self $urlItem->addChild('lastmod', date('c', filemtime( Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') ))); + $urlItem->addChild('changefreq', 'daily'); } return $this; From ab01e973adac02f5f8e7aa64670e24b64377da0e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:41:09 +0200 Subject: [PATCH 09/30] Add link to the sitemap protocol --- src/Services/SitemapService.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 93d47fc6..74926a56 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -9,6 +9,8 @@ /** * @see \Tests\Feature\Services\SitemapServiceTest + * + * @see https://www.sitemaps.org/protocol.html */ class SitemapService { From 7feb2175aefc802828f0621d37389557f2ac32dd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:44:45 +0200 Subject: [PATCH 10/30] Add HTML entity encoding to conform with protocol --- src/Services/SitemapService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 74926a56..b97ec0f0 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -39,10 +39,10 @@ public function addToSitemap(string $model): self foreach ($collection as $page) { $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', Hyde::uriPath(Hyde::pageLink($page . '.html'))); - $urlItem->addChild('lastmod', date('c', filemtime( + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); + $urlItem->addChild('lastmod', htmlentities(date('c', filemtime( Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') - ))); + )))); $urlItem->addChild('changefreq', 'daily'); } From e1b64b89791ec1c28e0b85948e58eda7e574f8ef Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:51:57 +0200 Subject: [PATCH 11/30] Extract get lastmod date to helper --- src/Services/SitemapService.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index b97ec0f0..d27d8ab6 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -40,9 +40,9 @@ public function addToSitemap(string $model): self foreach ($collection as $page) { $urlItem = $this->xmlElement->addChild('url'); $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); - $urlItem->addChild('lastmod', htmlentities(date('c', filemtime( + $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') - )))); + ))); $urlItem->addChild('changefreq', 'daily'); } @@ -65,4 +65,11 @@ public static function canGenerateSitemap(): bool { return (Hyde::uriPath() !== false); } + + protected function getLastModDateForFileOrFallback(string $filepath): string + { + return file_exists($filepath) + ? date('c', filemtime($filepath)) + : date('c'); + } } \ No newline at end of file From adcb5a06e9bf7aca4bda25043b320388755ad23d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:55:02 +0200 Subject: [PATCH 12/30] Remove premature abstraction --- src/Services/SitemapService.php | 58 +++++++++++++++------------------ 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index d27d8ab6..74ae1b65 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -8,68 +8,62 @@ use SimpleXMLElement; /** - * @see \Tests\Feature\Services\SitemapServiceTest - * - * @see https://www.sitemaps.org/protocol.html - */ +* @see \Tests\Feature\Services\SitemapServiceTest +* +* @see https://www.sitemaps.org/protocol.html +*/ class SitemapService { public SimpleXMLElement $xmlElement; - + public function __construct() { $this->time_start = microtime(true); - + $this->xmlElement = new SimpleXMLElement(''); $this->xmlElement->addAttribute('generator', 'HydePHP '.Hyde::version()); } - + public function generate(): self { if (Features::hasBladePages()) { - $this->addToSitemap(BladePage::class); + $collection = CollectionService::getSourceFileListForModel(BladePage::class); + + foreach ($collection as $page) { + $urlItem = $this->xmlElement->addChild('url'); + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); + $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') + ))); + $urlItem->addChild('changefreq', 'daily'); + } } - + return $this; } - - public function addToSitemap(string $model): self - { - $collection = CollectionService::getSourceFileListForModel($model); - - foreach ($collection as $page) { - $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( - Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') - ))); - $urlItem->addChild('changefreq', 'daily'); - } - - return $this; - } - + + public function getXML(): string { $this->xmlElement->addAttribute('processing_time_ms', (string) round((microtime(true) - $this->time_start) * 1000, 2)); - + return $this->xmlElement->asXML(); } - + public static function generateSitemap(): string { return (new static)->generate()->getXML(); } - + public static function canGenerateSitemap(): bool { return (Hyde::uriPath() !== false); } - + protected function getLastModDateForFileOrFallback(string $filepath): string { return file_exists($filepath) - ? date('c', filemtime($filepath)) - : date('c'); + ? date('c', filemtime($filepath)) + : date('c'); } } \ No newline at end of file From 2c1c93244f96061c7b75a818547e1f29c87baaa0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 16:56:33 +0200 Subject: [PATCH 13/30] Add support for Markdown pages --- src/Services/SitemapService.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 74ae1b65..0f9f1f94 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -5,6 +5,7 @@ use Hyde\Framework\Helpers\Features; use Hyde\Framework\Hyde; use Hyde\Framework\Models\BladePage; +use Hyde\Framework\Models\MarkdownPage; use SimpleXMLElement; /** @@ -38,6 +39,19 @@ public function generate(): self $urlItem->addChild('changefreq', 'daily'); } } + + if (Features::hasMarkdownPages()) { + $collection = CollectionService::getSourceFileListForModel(MarkdownPage::class); + + foreach ($collection as $page) { + $urlItem = $this->xmlElement->addChild('url'); + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); + $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + Hyde::path(MarkdownPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') + ))); + $urlItem->addChild('changefreq', 'daily'); + } + } return $this; } From 3166e65672ac6c2efe43db4a8d621a6fc3ff109e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:03:57 +0200 Subject: [PATCH 14/30] Add support for blog posts --- src/Services/SitemapService.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 0f9f1f94..31a9ac64 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -6,6 +6,7 @@ use Hyde\Framework\Hyde; use Hyde\Framework\Models\BladePage; use Hyde\Framework\Models\MarkdownPage; +use Hyde\Framework\Models\MarkdownPost; use SimpleXMLElement; /** @@ -52,6 +53,19 @@ public function generate(): self $urlItem->addChild('changefreq', 'daily'); } } + + if (Features::hasBlogPosts()) { + $collection = CollectionService::getSourceFileListForModel(MarkdownPost::class); + + foreach ($collection as $page) { + $urlItem = $this->xmlElement->addChild('url'); + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink('posts/'.$page . '.html')))); + $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + Hyde::path(MarkdownPost::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') + ))); + $urlItem->addChild('changefreq', 'daily'); + } + } return $this; } @@ -78,6 +92,6 @@ protected function getLastModDateForFileOrFallback(string $filepath): string { return file_exists($filepath) ? date('c', filemtime($filepath)) - : date('c'); + : 'null' ; // ?? date('c') } } \ No newline at end of file From 6ab9c3f05798477c5b52ddd2581bd0b17b3b59b5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:11:00 +0200 Subject: [PATCH 15/30] Add support for documentation pages --- src/Services/SitemapService.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 31a9ac64..e716711a 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -5,6 +5,7 @@ use Hyde\Framework\Helpers\Features; use Hyde\Framework\Hyde; use Hyde\Framework\Models\BladePage; +use Hyde\Framework\Models\DocumentationPage; use Hyde\Framework\Models\MarkdownPage; use Hyde\Framework\Models\MarkdownPost; use SimpleXMLElement; @@ -66,6 +67,20 @@ public function generate(): self $urlItem->addChild('changefreq', 'daily'); } } + + + if (Features::hasDocumentationPages()) { + $collection = CollectionService::getSourceFileListForModel(DocumentationPage::class); + + foreach ($collection as $page) { + $urlItem = $this->xmlElement->addChild('url'); + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink(Hyde::docsDirectory().'/'.$page . '.html')))); + $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + Hyde::path(DocumentationPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') + ))); + $urlItem->addChild('changefreq', 'daily'); + } + } return $this; } From 49468a92eaa76e393182f88738925382660be940 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:13:47 +0200 Subject: [PATCH 16/30] Reorder methods and remove test code --- src/Services/SitemapService.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index e716711a..1c115336 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -92,21 +92,21 @@ public function getXML(): string return $this->xmlElement->asXML(); } - + + protected function getLastModDateForFileOrFallback(string $filepath): string + { + return file_exists($filepath) + ? date('c', filemtime($filepath)) + : date('c'); + } + public static function generateSitemap(): string { return (new static)->generate()->getXML(); } - + public static function canGenerateSitemap(): bool { return (Hyde::uriPath() !== false); } - - protected function getLastModDateForFileOrFallback(string $filepath): string - { - return file_exists($filepath) - ? date('c', filemtime($filepath)) - : 'null' ; // ?? date('c') - } } \ No newline at end of file From ce5d8ed089546a8262e637d3ce399bf190672ba0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:29:30 +0200 Subject: [PATCH 17/30] Add SitemapService tests --- .../SitemapServiceTest/SitemapServiceTest.php | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php index 9aaef3ca..ecb5e199 100644 --- a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php +++ b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Services\SitemapServiceTest; +use Hyde\Framework\Hyde; use Hyde\Framework\Services\SitemapService; use Tests\TestCase; @@ -10,5 +11,93 @@ */ class SitemapServiceTest extends TestCase { + // Test service instantiates an XML element + public function testServiceInstantiatesXmlElement() + { + $service = new SitemapService(); + $this->assertInstanceOf('SimpleXMLElement', $service->xmlElement); + } + // Test generate method adds default pages to sitemap XML + public function testGenerateAddsDefaultPagesToXml() + { + $service = new SitemapService(); + $service->generate(); + + // Test runner has an index and 404 page, so we are using that as a baseline + $this->assertCount(2, $service->xmlElement->url); + } + + // Test generate method adds Markdown pages to sitemap XML + public function testGenerateAddsMarkdownPagesToXml() + { + touch(Hyde::path('_pages/foo.md')); + + $service = new SitemapService(); + $service->generate(); + + $this->assertCount(3, $service->xmlElement->url); + + unlink(Hyde::path('_pages/foo.md')); + } + + // Test generate method adds Markdown posts to sitemap XML + public function testGenerateAddsMarkdownPostsToXml() + { + touch(Hyde::path('_posts/foo.md')); + + $service = new SitemapService(); + $service->generate(); + + $this->assertCount(3, $service->xmlElement->url); + + unlink(Hyde::path('_posts/foo.md')); + } + + // Test generate method adds documentation pages to sitemap XML + public function testGenerateAddsDocumentationPagesToXml() + { + touch(Hyde::path('_docs/foo.md')); + + $service = new SitemapService(); + $service->generate(); + + $this->assertCount(3, $service->xmlElement->url); + + unlink(Hyde::path('_docs/foo.md')); + } + + // Test getXML method returns XML string + public function testGetXMLReturnsXMLString() + { + $service = new SitemapService(); + $service->generate(); + $xml = $service->getXML(); + + $this->assertIsString($xml); + $this->assertStringStartsWith('', $xml); + } + + // Test generateSitemap shorthand method returns XML string + public function testGenerateSitemapShorthandMethodReturnsXMLString() + { + $xml = SitemapService::generateSitemap(); + + $this->assertIsString($xml); + $this->assertStringStartsWith('', $xml); + } + + // Test canGenerateSitemap helper returns true if Hyde has a base URL + public function testCanGenerateSitemapHelperReturnsTrueIfHydeHasBaseUrl() + { + $this->app['config']->set('hyde.site_url', 'foo'); + $this->assertTrue(SitemapService::canGenerateSitemap()); + } + + // Test canGenerateSitemap helper returns false if Hyde does not have a base URL + public function testCanGenerateSitemapHelperReturnsFalseIfHydeDoesNotHaveBaseUrl() + { + $this->app['config']->set('hyde.site_url', ''); + $this->assertFalse(SitemapService::canGenerateSitemap()); + } } From 2b08c393bada2f279081a46266dcbd80d2ef3c08 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:42:19 +0200 Subject: [PATCH 18/30] Add tests covering the generated XML values --- .../SitemapServiceTest/SitemapServiceTest.php | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php index ecb5e199..43729bcb 100644 --- a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php +++ b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php @@ -90,14 +90,48 @@ public function testGenerateSitemapShorthandMethodReturnsXMLString() // Test canGenerateSitemap helper returns true if Hyde has a base URL public function testCanGenerateSitemapHelperReturnsTrueIfHydeHasBaseUrl() { - $this->app['config']->set('hyde.site_url', 'foo'); + config(['hyde.site_url' => 'foo']); $this->assertTrue(SitemapService::canGenerateSitemap()); } // Test canGenerateSitemap helper returns false if Hyde does not have a base URL public function testCanGenerateSitemapHelperReturnsFalseIfHydeDoesNotHaveBaseUrl() { - $this->app['config']->set('hyde.site_url', ''); + config(['hyde.site_url' => '']); $this->assertFalse(SitemapService::canGenerateSitemap()); } + + // Test URL item is generated correctly + public function testURLItemIsGeneratedCorrectly() + { + config(['hyde.prettyUrls' => false]); + config(['hyde.site_url' => 'https://example.com']); + touch(Hyde::path('_pages/0-test.blade.php')); + + $service = new SitemapService(); + $service->generate(); + + $url = $service->xmlElement->url[0]; + $this->assertEquals('https://example.com/0-test.html', $url->loc); + $this->assertEquals('daily', $url->changefreq); + $this->assertEquals(date('c'), $url->lastmod); + + unlink(Hyde::path('_pages/0-test.blade.php')); + } + + // Test URL item is generated with pretty URLs if enabled + public function testURLItemIsGeneratedWithPrettyURLsIfEnabled() + { + config(['hyde.prettyUrls' => true]); + config(['hyde.site_url' => 'https://example.com']); + touch(Hyde::path('_pages/0-test.blade.php')); + + $service = new SitemapService(); + $service->generate(); + + $url = $service->xmlElement->url[0]; + $this->assertEquals('https://example.com/0-test', $url->loc); + + unlink(Hyde::path('_pages/0-test.blade.php')); + } } From ad88b5572a1e41f217c3b396c2e242bd8777540d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:45:20 +0200 Subject: [PATCH 19/30] Remove filemtime fallback It should not be possible for a file to disappear in the microsecond since the collection was generated. If a stat fails it is probably because the input parameter was wrong, which would be an error and not something that should have a graceful fallback. --- src/Services/SitemapService.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 1c115336..9cc48ff1 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -35,7 +35,7 @@ public function generate(): self foreach ($collection as $page) { $urlItem = $this->xmlElement->addChild('url'); $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') ))); $urlItem->addChild('changefreq', 'daily'); @@ -48,7 +48,7 @@ public function generate(): self foreach ($collection as $page) { $urlItem = $this->xmlElement->addChild('url'); $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( Hyde::path(MarkdownPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') ))); $urlItem->addChild('changefreq', 'daily'); @@ -61,21 +61,20 @@ public function generate(): self foreach ($collection as $page) { $urlItem = $this->xmlElement->addChild('url'); $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink('posts/'.$page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( Hyde::path(MarkdownPost::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') ))); $urlItem->addChild('changefreq', 'daily'); } } - if (Features::hasDocumentationPages()) { $collection = CollectionService::getSourceFileListForModel(DocumentationPage::class); foreach ($collection as $page) { $urlItem = $this->xmlElement->addChild('url'); $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink(Hyde::docsDirectory().'/'.$page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDateForFileOrFallback( + $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( Hyde::path(DocumentationPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') ))); $urlItem->addChild('changefreq', 'daily'); @@ -85,7 +84,6 @@ public function generate(): self return $this; } - public function getXML(): string { $this->xmlElement->addAttribute('processing_time_ms', (string) round((microtime(true) - $this->time_start) * 1000, 2)); @@ -93,11 +91,9 @@ public function getXML(): string return $this->xmlElement->asXML(); } - protected function getLastModDateForFileOrFallback(string $filepath): string + protected function getLastModDate(string $filepath): string { - return file_exists($filepath) - ? date('c', filemtime($filepath)) - : date('c'); + return date('c', filemtime($filepath)); } public static function generateSitemap(): string From 46f41d6848a5562006f4290aa00df221d25d815a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:56:11 +0200 Subject: [PATCH 20/30] Refactor shared code into new helper Adds a helper that handles shared code to make it easier to manage and maintain. Does not change compiled output. --- src/Services/SitemapService.php | 68 ++++++++++++++------------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 9cc48ff1..d810fd83 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -30,55 +30,29 @@ public function __construct() public function generate(): self { if (Features::hasBladePages()) { - $collection = CollectionService::getSourceFileListForModel(BladePage::class); - - foreach ($collection as $page) { - $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( - Hyde::path(BladePage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.blade.php') - ))); - $urlItem->addChild('changefreq', 'daily'); - } + $this->addPageModelUrls( + BladePage::class + ); } if (Features::hasMarkdownPages()) { - $collection = CollectionService::getSourceFileListForModel(MarkdownPage::class); - - foreach ($collection as $page) { - $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( - Hyde::path(MarkdownPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') - ))); - $urlItem->addChild('changefreq', 'daily'); - } + $this->addPageModelUrls( + MarkdownPage::class + ); } if (Features::hasBlogPosts()) { - $collection = CollectionService::getSourceFileListForModel(MarkdownPost::class); - - foreach ($collection as $page) { - $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink('posts/'.$page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( - Hyde::path(MarkdownPost::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') - ))); - $urlItem->addChild('changefreq', 'daily'); - } + $this->addPageModelUrls( + MarkdownPost::class, + 'posts/' + ); } if (Features::hasDocumentationPages()) { - $collection = CollectionService::getSourceFileListForModel(DocumentationPage::class); - - foreach ($collection as $page) { - $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink(Hyde::docsDirectory().'/'.$page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( - Hyde::path(DocumentationPage::$sourceDirectory.DIRECTORY_SEPARATOR.$page.'.md') - ))); - $urlItem->addChild('changefreq', 'daily'); - } + $this->addPageModelUrls( + DocumentationPage::class, + Hyde::docsDirectory().'/' + ); } return $this; @@ -91,6 +65,20 @@ public function getXML(): string return $this->xmlElement->asXML(); } + public function addPageModelUrls(string $pageClass, string $routePrefix = ''): void + { + $collection = CollectionService::getSourceFileListForModel($pageClass); + + foreach ($collection as $page) { + $urlItem = $this->xmlElement->addChild('url'); + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$page . '.html')))); + $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( + Hyde::path($pageClass::$sourceDirectory.DIRECTORY_SEPARATOR.$page.$pageClass::$fileExtension) + ))); + $urlItem->addChild('changefreq', 'daily'); + } + } + protected function getLastModDate(string $filepath): string { return date('c', filemtime($filepath)); From 62e8a031588a27916135debaaadba140220c585f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:58:32 +0200 Subject: [PATCH 21/30] Handle file path lookup in getLastModDate helper --- src/Services/SitemapService.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index d810fd83..8fe4be42 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -72,16 +72,16 @@ public function addPageModelUrls(string $pageClass, string $routePrefix = ''): v foreach ($collection as $page) { $urlItem = $this->xmlElement->addChild('url'); $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDate( - Hyde::path($pageClass::$sourceDirectory.DIRECTORY_SEPARATOR.$page.$pageClass::$fileExtension) - ))); + $urlItem->addChild('lastmod', htmlentities($this->getLastModDate($pageClass, $page))); $urlItem->addChild('changefreq', 'daily'); } } - protected function getLastModDate(string $filepath): string + protected function getLastModDate(string $pageClass, string $page): string { - return date('c', filemtime($filepath)); + return date('c', filemtime( + Hyde::path($pageClass::$sourceDirectory.DIRECTORY_SEPARATOR.$page.$pageClass::$fileExtension) + )); } public static function generateSitemap(): string From cb9ecc2902abc3b9d8d0ce50f082cbad58c16e33 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 17:59:53 +0200 Subject: [PATCH 22/30] Rename internal variable to match Hyde conventions --- src/Services/SitemapService.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 8fe4be42..9c451b72 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -69,18 +69,18 @@ public function addPageModelUrls(string $pageClass, string $routePrefix = ''): v { $collection = CollectionService::getSourceFileListForModel($pageClass); - foreach ($collection as $page) { + foreach ($collection as $slug) { $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$page . '.html')))); - $urlItem->addChild('lastmod', htmlentities($this->getLastModDate($pageClass, $page))); + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$slug . '.html')))); + $urlItem->addChild('lastmod', htmlentities($this->getLastModDate($pageClass, $slug))); $urlItem->addChild('changefreq', 'daily'); } } - protected function getLastModDate(string $pageClass, string $page): string + protected function getLastModDate(string $pageClass, string $slug): string { return date('c', filemtime( - Hyde::path($pageClass::$sourceDirectory.DIRECTORY_SEPARATOR.$page.$pageClass::$fileExtension) + Hyde::path($pageClass::$sourceDirectory.DIRECTORY_SEPARATOR.$slug.$pageClass::$fileExtension) )); } From 2f67dc6ebc3ed039409947343a27778b5f3e7af0 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 18 May 2022 16:09:30 +0000 Subject: [PATCH 23/30] Apply fixes from StyleCI --- src/Commands/HydeBuildStaticSiteCommand.php | 1 - src/Services/SitemapService.php | 25 ++++++++++----------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Commands/HydeBuildStaticSiteCommand.php b/src/Commands/HydeBuildStaticSiteCommand.php index 7274e845..8735a840 100644 --- a/src/Commands/HydeBuildStaticSiteCommand.php +++ b/src/Commands/HydeBuildStaticSiteCommand.php @@ -83,7 +83,6 @@ public function handle(): int $this->runBuildAction(DocumentationPage::class); } - $this->postBuildActions(); $this->printFinishMessage($time_start); diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 9c451b72..ddf6515f 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -11,22 +11,21 @@ use SimpleXMLElement; /** -* @see \Tests\Feature\Services\SitemapServiceTest -* -* @see https://www.sitemaps.org/protocol.html -*/ + * @see \Tests\Feature\Services\SitemapServiceTest + * @see https://www.sitemaps.org/protocol.html + */ class SitemapService { public SimpleXMLElement $xmlElement; - + public function __construct() { $this->time_start = microtime(true); - + $this->xmlElement = new SimpleXMLElement(''); $this->xmlElement->addAttribute('generator', 'HydePHP '.Hyde::version()); } - + public function generate(): self { if (Features::hasBladePages()) { @@ -54,14 +53,14 @@ public function generate(): self Hyde::docsDirectory().'/' ); } - + return $this; } - + public function getXML(): string { $this->xmlElement->addAttribute('processing_time_ms', (string) round((microtime(true) - $this->time_start) * 1000, 2)); - + return $this->xmlElement->asXML(); } @@ -71,7 +70,7 @@ public function addPageModelUrls(string $pageClass, string $routePrefix = ''): v foreach ($collection as $slug) { $urlItem = $this->xmlElement->addChild('url'); - $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$slug . '.html')))); + $urlItem->addChild('loc', htmlentities(Hyde::uriPath(Hyde::pageLink($routePrefix.$slug.'.html')))); $urlItem->addChild('lastmod', htmlentities($this->getLastModDate($pageClass, $slug))); $urlItem->addChild('changefreq', 'daily'); } @@ -91,6 +90,6 @@ public static function generateSitemap(): string public static function canGenerateSitemap(): bool { - return (Hyde::uriPath() !== false); + return Hyde::uriPath() !== false; } -} \ No newline at end of file +} From 5d870dc185583632fb6620c9fb9b90573380e5ca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 18:12:47 +0200 Subject: [PATCH 24/30] Allow sitemaps to be disabled in config --- src/Services/SitemapService.php | 2 +- .../Services/SitemapServiceTest/SitemapServiceTest.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Services/SitemapService.php b/src/Services/SitemapService.php index 9c451b72..88c49327 100644 --- a/src/Services/SitemapService.php +++ b/src/Services/SitemapService.php @@ -91,6 +91,6 @@ public static function generateSitemap(): string public static function canGenerateSitemap(): bool { - return (Hyde::uriPath() !== false); + return (Hyde::uriPath() !== false) && config('hyde.generateSitemap', true); } } \ No newline at end of file diff --git a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php index 43729bcb..b5c8b619 100644 --- a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php +++ b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php @@ -101,6 +101,14 @@ public function testCanGenerateSitemapHelperReturnsFalseIfHydeDoesNotHaveBaseUrl $this->assertFalse(SitemapService::canGenerateSitemap()); } + // Test canGenerateSitemap helper returns false if sitemaps are disabled in config + public function testCanGenerateSitemapHelperReturnsFalseIfSitemapsAreDisabledInConfig() + { + config(['hyde.site_url' => 'foo']); + config(['hyde.generateSitemap' => false]); + $this->assertFalse(SitemapService::canGenerateSitemap()); + } + // Test URL item is generated correctly public function testURLItemIsGeneratedCorrectly() { From 43bc10fba920162a8d7c26d5bd4f8570139e581a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 18:26:03 +0200 Subject: [PATCH 25/30] Merge "Site URL Configuration" options --- config/hyde.php | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/config/hyde.php b/config/hyde.php index f1ae2f57..a71b6075 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -39,21 +39,27 @@ /* |-------------------------------------------------------------------------- - | Site URL - |-------------------------------------------------------------------------- - | - | If you want, you can set your site's URL here or in the .env file. - | - | The URL will then be used in meta tags to create permalinks. - | If you are serving your site from a subdirectory, you will - | need to include that in the path without a trailing slash. - | - | Example: https://example.org/blog - | + | Site URL Configuration + |-------------------------------------------------------------------------- + | + | Here are some configuration options for URL generation. + | + | `site_url` is used to create canonical URLs and permalinks. + | `prettyUrls` will when enabled create links that do not end in .html. + | `generateSitemap` determines if a sitemap.xml file should be generated. + | + | To see the full documentation, please visit the (temporary link) below. + | https://github.com/hydephp/framework/wiki/Documentation-Page-Drafts + | + | */ 'site_url' => env('SITE_URL', null), + 'prettyUrls' => false, + + 'generateSitemap' => true, + /* |-------------------------------------------------------------------------- | Site Language @@ -280,21 +286,6 @@ 'smoothPageScrolling' => true, ], - /* - |-------------------------------------------------------------------------- - | Pretty URLs (Links that do not end in .html) - |-------------------------------------------------------------------------- - | - | Introduced in v0.25.0, you can now enable "pretty URLs". When the setting - | is enabled, generated links in the compiled HTML site are without the - | `.html` extension. Since this breaks local browsing you can leave - | the setting disabled, and instead add the `--pretty-urls` flag - | when running the `php hyde build` command for deployment. - | - */ - - 'prettyUrls' => false, - /* |-------------------------------------------------------------------------- | Hyde Config Version @HydeConfigVersion 0.1.0 From 86fe874d85fec6e1b9cd97d76164bb491fe8e2e2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 18:35:14 +0200 Subject: [PATCH 26/30] Change test names to use snake_case --- .../SitemapServiceTest/SitemapServiceTest.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php index b5c8b619..55be8359 100644 --- a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php +++ b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php @@ -12,14 +12,14 @@ class SitemapServiceTest extends TestCase { // Test service instantiates an XML element - public function testServiceInstantiatesXmlElement() + public function test_service_instantiates_xml_element() { $service = new SitemapService(); $this->assertInstanceOf('SimpleXMLElement', $service->xmlElement); } // Test generate method adds default pages to sitemap XML - public function testGenerateAddsDefaultPagesToXml() + public function test_generate_adds_default_pages_to_xml() { $service = new SitemapService(); $service->generate(); @@ -29,7 +29,7 @@ public function testGenerateAddsDefaultPagesToXml() } // Test generate method adds Markdown pages to sitemap XML - public function testGenerateAddsMarkdownPagesToXml() + public function test_generate_adds_markdown_pages_to_xml() { touch(Hyde::path('_pages/foo.md')); @@ -42,7 +42,7 @@ public function testGenerateAddsMarkdownPagesToXml() } // Test generate method adds Markdown posts to sitemap XML - public function testGenerateAddsMarkdownPostsToXml() + public function test_generate_adds_markdown_posts_to_xml() { touch(Hyde::path('_posts/foo.md')); @@ -55,7 +55,7 @@ public function testGenerateAddsMarkdownPostsToXml() } // Test generate method adds documentation pages to sitemap XML - public function testGenerateAddsDocumentationPagesToXml() + public function test_generate_adds_documentation_pages_to_xml() { touch(Hyde::path('_docs/foo.md')); @@ -68,7 +68,7 @@ public function testGenerateAddsDocumentationPagesToXml() } // Test getXML method returns XML string - public function testGetXMLReturnsXMLString() + public function test_get_xml_returns_xml_string() { $service = new SitemapService(); $service->generate(); @@ -79,7 +79,7 @@ public function testGetXMLReturnsXMLString() } // Test generateSitemap shorthand method returns XML string - public function testGenerateSitemapShorthandMethodReturnsXMLString() + public function test_generate_sitemap_shorthand_method_returns_xml_string() { $xml = SitemapService::generateSitemap(); @@ -88,21 +88,21 @@ public function testGenerateSitemapShorthandMethodReturnsXMLString() } // Test canGenerateSitemap helper returns true if Hyde has a base URL - public function testCanGenerateSitemapHelperReturnsTrueIfHydeHasBaseUrl() + public function test_can_generate_sitemap_helper_returns_true_if_hyde_has_base_url() { config(['hyde.site_url' => 'foo']); $this->assertTrue(SitemapService::canGenerateSitemap()); } // Test canGenerateSitemap helper returns false if Hyde does not have a base URL - public function testCanGenerateSitemapHelperReturnsFalseIfHydeDoesNotHaveBaseUrl() + public function test_can_generate_sitemap_helper_returns_false_if_hyde_does_not_have_base_url() { config(['hyde.site_url' => '']); $this->assertFalse(SitemapService::canGenerateSitemap()); } // Test canGenerateSitemap helper returns false if sitemaps are disabled in config - public function testCanGenerateSitemapHelperReturnsFalseIfSitemapsAreDisabledInConfig() + public function test_can_generate_sitemap_helper_returns_false_if_sitemaps_are_disabled_in_config() { config(['hyde.site_url' => 'foo']); config(['hyde.generateSitemap' => false]); @@ -110,7 +110,7 @@ public function testCanGenerateSitemapHelperReturnsFalseIfSitemapsAreDisabledInC } // Test URL item is generated correctly - public function testURLItemIsGeneratedCorrectly() + public function test_url_item_is_generated_correctly() { config(['hyde.prettyUrls' => false]); config(['hyde.site_url' => 'https://example.com']); @@ -128,7 +128,7 @@ public function testURLItemIsGeneratedCorrectly() } // Test URL item is generated with pretty URLs if enabled - public function testURLItemIsGeneratedWithPrettyURLsIfEnabled() + public function test_url_item_is_generated_with_pretty_ur_ls_if_enabled() { config(['hyde.prettyUrls' => true]); config(['hyde.site_url' => 'https://example.com']); From 531667428e48da3b77c788fb915d4719ae548ab9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 18:35:42 +0200 Subject: [PATCH 27/30] Remove copilot comments --- .../SitemapServiceTest/SitemapServiceTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php index 55be8359..59c7cd9a 100644 --- a/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php +++ b/tests/Feature/Services/SitemapServiceTest/SitemapServiceTest.php @@ -11,14 +11,12 @@ */ class SitemapServiceTest extends TestCase { - // Test service instantiates an XML element public function test_service_instantiates_xml_element() { $service = new SitemapService(); $this->assertInstanceOf('SimpleXMLElement', $service->xmlElement); } - // Test generate method adds default pages to sitemap XML public function test_generate_adds_default_pages_to_xml() { $service = new SitemapService(); @@ -28,7 +26,6 @@ public function test_generate_adds_default_pages_to_xml() $this->assertCount(2, $service->xmlElement->url); } - // Test generate method adds Markdown pages to sitemap XML public function test_generate_adds_markdown_pages_to_xml() { touch(Hyde::path('_pages/foo.md')); @@ -41,7 +38,6 @@ public function test_generate_adds_markdown_pages_to_xml() unlink(Hyde::path('_pages/foo.md')); } - // Test generate method adds Markdown posts to sitemap XML public function test_generate_adds_markdown_posts_to_xml() { touch(Hyde::path('_posts/foo.md')); @@ -54,7 +50,6 @@ public function test_generate_adds_markdown_posts_to_xml() unlink(Hyde::path('_posts/foo.md')); } - // Test generate method adds documentation pages to sitemap XML public function test_generate_adds_documentation_pages_to_xml() { touch(Hyde::path('_docs/foo.md')); @@ -67,7 +62,6 @@ public function test_generate_adds_documentation_pages_to_xml() unlink(Hyde::path('_docs/foo.md')); } - // Test getXML method returns XML string public function test_get_xml_returns_xml_string() { $service = new SitemapService(); @@ -78,7 +72,6 @@ public function test_get_xml_returns_xml_string() $this->assertStringStartsWith('', $xml); } - // Test generateSitemap shorthand method returns XML string public function test_generate_sitemap_shorthand_method_returns_xml_string() { $xml = SitemapService::generateSitemap(); @@ -87,21 +80,18 @@ public function test_generate_sitemap_shorthand_method_returns_xml_string() $this->assertStringStartsWith('', $xml); } - // Test canGenerateSitemap helper returns true if Hyde has a base URL public function test_can_generate_sitemap_helper_returns_true_if_hyde_has_base_url() { config(['hyde.site_url' => 'foo']); $this->assertTrue(SitemapService::canGenerateSitemap()); } - // Test canGenerateSitemap helper returns false if Hyde does not have a base URL public function test_can_generate_sitemap_helper_returns_false_if_hyde_does_not_have_base_url() { config(['hyde.site_url' => '']); $this->assertFalse(SitemapService::canGenerateSitemap()); } - // Test canGenerateSitemap helper returns false if sitemaps are disabled in config public function test_can_generate_sitemap_helper_returns_false_if_sitemaps_are_disabled_in_config() { config(['hyde.site_url' => 'foo']); @@ -109,7 +99,6 @@ public function test_can_generate_sitemap_helper_returns_false_if_sitemaps_are_d $this->assertFalse(SitemapService::canGenerateSitemap()); } - // Test URL item is generated correctly public function test_url_item_is_generated_correctly() { config(['hyde.prettyUrls' => false]); @@ -127,7 +116,6 @@ public function test_url_item_is_generated_correctly() unlink(Hyde::path('_pages/0-test.blade.php')); } - // Test URL item is generated with pretty URLs if enabled public function test_url_item_is_generated_with_pretty_ur_ls_if_enabled() { config(['hyde.prettyUrls' => true]); From ce54fdcff7f029b40637577d96f83c96d9f15d88 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 18:37:03 +0200 Subject: [PATCH 28/30] Add missing test case for pretty urls option --- tests/Feature/Commands/BuildStaticSiteCommandTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Feature/Commands/BuildStaticSiteCommandTest.php b/tests/Feature/Commands/BuildStaticSiteCommandTest.php index ffef392b..32460a56 100644 --- a/tests/Feature/Commands/BuildStaticSiteCommandTest.php +++ b/tests/Feature/Commands/BuildStaticSiteCommandTest.php @@ -86,6 +86,13 @@ public function test_node_action_outputs() ->assertExitCode(0); } + public function test_pretty_urls_option_output() + { + $this->artisan('build --pretty-urls') + ->expectsOutput('Generating site with pretty URLs') + ->assertExitCode(0); + } + /** * Added for code coverage, deprecated as the pretty flag is deprecated. * From 4e15d0edb14d201311ead9b9fe6920e202341dff Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 18 May 2022 18:46:13 +0200 Subject: [PATCH 29/30] Add tests for sitemap generation --- .../Commands/BuildStaticSiteCommandTest.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Feature/Commands/BuildStaticSiteCommandTest.php b/tests/Feature/Commands/BuildStaticSiteCommandTest.php index 32460a56..9cecfe37 100644 --- a/tests/Feature/Commands/BuildStaticSiteCommandTest.php +++ b/tests/Feature/Commands/BuildStaticSiteCommandTest.php @@ -93,6 +93,31 @@ public function test_pretty_urls_option_output() ->assertExitCode(0); } + public function test_sitemap_is_not_generated_when_conditions_are_not_met() + { + config(['hyde.site_url' => '']); + config(['hyde.generateSitemap' => false]); + + unlinkIfExists(Hyde::path('_site/sitemap.xml')); + $this->artisan('build') + ->assertExitCode(0); + + $this->assertFileDoesNotExist(Hyde::path('_site/sitemap.xml')); + } + + public function test_sitemap_is_generated_when_conditions_are_met() + { + config(['hyde.site_url' => 'https://example.com']); + config(['hyde.generateSitemap' => true]); + + unlinkIfExists(Hyde::path('_site/sitemap.xml')); + $this->artisan('build') + ->expectsOutput('Generating sitemap.xml') + ->assertExitCode(0); + + $this->assertFileExists(Hyde::path('_site/sitemap.xml')); + } + /** * Added for code coverage, deprecated as the pretty flag is deprecated. * From 4ef9b3b2c36959b9198cfd0c20ae9ab5494cc391 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 18 May 2022 16:46:58 +0000 Subject: [PATCH 30/30] Apply fixes from StyleCI --- config/hyde.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config/hyde.php b/config/hyde.php index a71b6075..6c8de035 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -41,17 +41,17 @@ |-------------------------------------------------------------------------- | Site URL Configuration |-------------------------------------------------------------------------- - | - | Here are some configuration options for URL generation. - | + | + | Here are some configuration options for URL generation. + | | `site_url` is used to create canonical URLs and permalinks. | `prettyUrls` will when enabled create links that do not end in .html. | `generateSitemap` determines if a sitemap.xml file should be generated. - | + | | To see the full documentation, please visit the (temporary link) below. | https://github.com/hydephp/framework/wiki/Documentation-Page-Drafts - | - | + | + | */ 'site_url' => env('SITE_URL', null),