Skip to content

Commit

Permalink
Merge pull request #35 from silverstripeltd/feature/silverstripe-5
Browse files Browse the repository at this point in the history
Add support for silverstripe 5
  • Loading branch information
tractorcow committed Apr 13, 2023
2 parents 498db49 + 0a04a66 commit bc5655d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 30 deletions.
30 changes: 26 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"issues": "http://github.com/tractorcow/silverstripe-opengraph/issues"
},
"require": {
"silverstripe/recipe-core": "^1 || ^4"
"silverstripe/recipe-core": "^1 || ^4 || ^5"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"silverstripe/recipe-cms": "^1 || ^4"
"silverstripe/recipe-testing": "^3",
"silverstripe/recipe-cms": "^1 || ^4 || ^5"
},
"extra": {
"installer-name": "opengraph",
Expand All @@ -31,6 +31,21 @@
},
"expose": [
"images"
],
"project-files-installed": [
".htaccess",
"app/.htaccess",
"app/_config/mimevalidator.yml",
"app/_config/mysite.yml",
"app/src/Page.php",
"app/src/PageController.php",
"behat.yml",
"phpunit.xml.dist"
],
"public-files-installed": [
".htaccess",
"index.php",
"web.config"
]
},
"scripts": {
Expand All @@ -43,5 +58,12 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/recipe-plugin": true,
"silverstripe/vendor-plugin": true
}
}
}
8 changes: 4 additions & 4 deletions src/Extensions/OpenGraphObjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class OpenGraphObjectExtension extends DataExtension implements IOGObjectExplici

/**
* Do not escpae HTML
*
* @var string
*
* @var string
*/
private static $casting = [
'OGNS' => 'HTMLFragment',
];

/**
* Property for retrieving the opengraph namespace html tag(s).
* This should be inserted into your Page.SS template as: "<html $OGNS>"
Expand Down Expand Up @@ -195,7 +195,7 @@ public function getOGDescription()
{
// Check MetaDescription has given content
if ($this->owner->hasField('MetaDescription')) {
if($this->owner->MetaDescription != null || $this->owner->MetaDescription != ''){
if ($this->owner->MetaDescription != null || $this->owner->MetaDescription != '') {
$description = trim($this->owner->MetaDescription);
if (!empty($description)) {
return $description;
Expand Down
4 changes: 2 additions & 2 deletions tests/Extensions/OpenGraphObjectExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OpenGraphObjectExtensionTest extends SapphireTest
TestProfile::class
];

public function testOGNS()
public function testOGNS(): void
{
$page = $this->objFromFixture(TestPage::class, 'page1');
$this->assertEquals(
Expand All @@ -24,7 +24,7 @@ public function testOGNS()
);
}

public function testOGType()
public function testOGType(): void
{
$page = $this->objFromFixture(TestPage::class, 'page1');
$this->assertEquals('website', $page->getOGType());
Expand Down
18 changes: 9 additions & 9 deletions tests/ObjectBuilders/OGProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ class OGProfileTest extends SapphireTest
TestProfile::class
];

public function testBuildTags()
public function testBuildTags(): void
{
$builder = OGProfile::create();
$tags = '';
$cfg = SiteConfig::current_site_config();
$builder->BuildTags($tags, $this->objFromFixture(TestProfile::class, 'tractorcow'), $cfg);

$this->assertContains('<meta property="og:title" content="Damian Mooyman" />', $tags);
$this->assertContains('<meta property="og:type" content="profile" />', $tags);
$this->assertContains('<meta property="og:url" content="http://example.com/profile/tractorcow" />', $tags);
$this->assertContains('<meta property="og:image" content="http://example.com/pic/tractorcow.jpg" />', $tags);
$this->assertContains('<meta property="profile:first_name" content="Damian" />', $tags);
$this->assertContains('<meta property="profile:last_name" content="Mooyman" />', $tags);
$this->assertContains('<meta property="profile:username" content="TractorCow" />', $tags);
$this->assertContains('<meta property="profile:gender" content="male" />', $tags);
$this->assertStringContainsString('<meta property="og:title" content="Damian Mooyman" />', $tags);
$this->assertStringContainsString('<meta property="og:type" content="profile" />', $tags);
$this->assertStringContainsString('<meta property="og:url" content="http://example.com/profile/tractorcow" />', $tags);
$this->assertStringContainsString('<meta property="og:image" content="http://example.com/pic/tractorcow.jpg" />', $tags);
$this->assertStringContainsString('<meta property="profile:first_name" content="Damian" />', $tags);
$this->assertStringContainsString('<meta property="profile:last_name" content="Mooyman" />', $tags);
$this->assertStringContainsString('<meta property="profile:username" content="TractorCow" />', $tags);
$this->assertStringContainsString('<meta property="profile:gender" content="male" />', $tags);
}
}
18 changes: 10 additions & 8 deletions tests/ObjectBuilders/OpenGraphBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TractorCow\OpenGraph\Tests\ObjectBuilders;

use PHPUnit\Framework\Constraint\RegularExpression;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\i18n;
Expand All @@ -19,25 +20,26 @@ class OpenGraphBuilderTest extends SapphireTest
TestProfile::class
];

public function setUp()
public function setUp(): void
{
parent::setUp();
Config::modify()->set(i18n::class, 'default_locale', 'en_US');
i18n::set_locale('en_US');
}

public function testBuildTags()
public function testBuildTags(): void
{
$builder = OpenGraphBuilder::create();
$tags = '';
$cfg = SiteConfig::current_site_config();
$builder->BuildTags($tags, $this->objFromFixture(TestPage::class, 'page1'), $cfg);

$this->assertContains('<meta property="og:title" content="Testpage" />', $tags);
$this->assertContains('<meta property="og:type" content="website" />', $tags);
$this->assertRegExp('{<meta property="og:url" content=".*?" />}', $tags);
$this->assertRegExp('{<meta property="og:image" content=".*?/logo.gif.*?" />}', $tags);
$this->assertContains('<meta property="og:site_name" content="Test Website" />', $tags);
$this->assertContains('<meta property="og:locale" content="en_US" />', $tags);

$this->assertStringContainsString('<meta property="og:title" content="Testpage" />', $tags);
$this->assertStringContainsString('<meta property="og:type" content="website" />', $tags);
static::assertThat($tags, new RegularExpression('{<meta property="og:url" content=".*?" />}'));
static::assertThat($tags, new RegularExpression('{<meta property="og:image" content=".*?/logo.gif.*?" />}'));
$this->assertStringContainsString('<meta property="og:site_name" content="Test Website" />', $tags);
$this->assertStringContainsString('<meta property="og:locale" content="en_US" />', $tags);
}
}
6 changes: 3 additions & 3 deletions tests/OpenGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class OpenGraphTest extends SapphireTest
{
protected function setUp()
protected function setUp(): void
{
parent::setUp();
Config::modify()
Expand All @@ -18,15 +18,15 @@ protected function setUp()
->set(OpenGraph::class, 'default_tagbuilder', 'TractorCow\OpenGraph\ObjectBuilders\OpenGraphBuilder');
}

public function testConfig()
public function testConfig(): void
{
$this->assertEquals('SiteConfig', OpenGraph::get_config('application_id'));
$this->assertEquals('SiteConfig', OpenGraph::get_config('admin_id'));
$this->assertEquals('en_US', OpenGraph::get_default_locale());
$this->assertEquals('TractorCow\OpenGraph\ObjectBuilders\OpenGraphBuilder', OpenGraph::get_default_tagbuilder());
}

public function testLocaleValid()
public function testLocaleValid(): void
{
$this->assertTrue(OpenGraph::is_locale_valid('en_US'));
$this->assertFalse(OpenGraph::is_locale_valid('en_EN'));
Expand Down

0 comments on commit bc5655d

Please sign in to comment.