From 965e643c71b22493f79df8fe525adb061c0afb2b Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 2 May 2026 23:19:18 +0200 Subject: [PATCH] tests: Tests for opengraph meta tags Also fixed a minor whitespace issue in include/opengraph.php, and added braces so the logic is clearer. --- include/opengraph.php | 6 ++- tests/unit/includes/OpenGraphTest.php | 72 +++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 tests/unit/includes/OpenGraphTest.php diff --git a/include/opengraph.php b/include/opengraph.php index f62970c5d..dcc6cb4fe 100644 --- a/include/opengraph.php +++ b/include/opengraph.php @@ -62,8 +62,10 @@ $ogimagetype = $channel['xchan_photo_mimetype']; } - if (! isset(App::$page['htmlhead'])) - App::$page['htmlhead'] = ''; + if (! isset(App::$page['htmlhead'])) { + App::$page['htmlhead'] = ''; + } + App::$page['htmlhead'] .= '' . "\r\n"; App::$page['htmlhead'] .= '' . "\r\n"; App::$page['htmlhead'] .= '' . "\r\n"; diff --git a/tests/unit/includes/OpenGraphTest.php b/tests/unit/includes/OpenGraphTest.php new file mode 100644 index 000000000..a6367027d --- /dev/null +++ b/tests/unit/includes/OpenGraphTest.php @@ -0,0 +1,72 @@ + + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\includes; + +use App; +use PHPUnit\Framework\Attributes\BackupStaticProperties; +use Zotlabs\Tests\Unit\UnitTestCase; + +require_once 'include/opengraph.php'; + +#[BackupStaticProperties('App')] +class OpenGraphTest extends UnitTestCase +{ + public function testWithEmptyItem(): void { + App::$profile['about'] = false; + $channel = [ + 'channel_name' => 'test', + 'xchan_photo_l' => z_root() . '/photos/1', + 'xchan_photo_mimetype' => 'image/jpeg', + ]; + + opengraph_add_meta(null, $channel); + + $this->assertMetaOGTag('og:title', 'test'); + $this->assertMetaOGTag('og:image', z_root() . '/photos/1'); + $this->assertMetaOGTag('og:image:type', 'image/jpeg'); + $this->assertMetaOGTag('og:description', 'This is the home page of test.'); + $this->assertMetaOGTag('og:type', 'profile'); + } + + public function testWithPostItem(): void { + App::$profile['about'] = false; + $channel = [ + 'channel_name' => 'test', + 'xchan_photo_l' => z_root() . '/photos/1', + 'xchan_photo_mimetype' => 'image/jpeg', + ]; + + $item = [ + 'title' => 'The post title', + 'body' => '[zmg=' . z_root() . '/some_image.jpg]An image with alt text[/zmg]', + 'summary' => 'A summary of the post', + ]; + + opengraph_add_meta($item, $channel); + + $this->assertMetaOGTag('og:title', 'The post title'); + $this->assertMetaOGTag('og:image', z_root() . '/some_image.jpg'); + + // + // Image type is empty because `guess_image_type()` won't be able + // to locate the image, and we can't mock the function as it's not + // called from a namespace. + // + $this->assertMetaOGTag('og:image:type', ''); + + $this->assertMetaOGTag('og:description', 'A summary of the post'); + $this->assertMetaOGTag('og:type', 'article'); + } + + private function assertMetaOGTag(string $tag, string $content): void { + $this->assertStringContainsString( + "", + App::$page['htmlhead']); + } +}