From b8908efdf72fcfd72afd4b323f451869acd224c9 Mon Sep 17 00:00:00 2001 From: Jeremy Thomerson Date: Mon, 17 Jun 2013 20:29:02 +0000 Subject: [PATCH] ENHANCEMENT: allow sites to override meta generator tag --- code/model/SiteTree.php | 13 ++++++++++++- tests/model/SiteTreeTest.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/code/model/SiteTree.php b/code/model/SiteTree.php index 440cb1f4..73c16409 100644 --- a/code/model/SiteTree.php +++ b/code/model/SiteTree.php @@ -203,6 +203,14 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid */ private static $enforce_strict_hierarchy = true; + /** + * The value used for the meta generator tag. Leave blank to omit the tag. + * + * @config + * @var string + */ + private static $meta_generator = 'SilverStripe - http://silverstripe.org'; + protected $_cache_statusFlags = null; /** @@ -1298,7 +1306,10 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid $tags .= "" . $this->Title . "\n"; } - $tags .= "\n"; + $generator = trim(Config::inst()->get('SiteTree', 'meta_generator')); + if (!empty($generator)) { + $tags .= "\n"; + } $charset = Config::inst()->get('ContentNegotiator', 'encoding'); $tags .= "\n"; diff --git a/tests/model/SiteTreeTest.php b/tests/model/SiteTreeTest.php index 863e290b..b8ebc8d1 100644 --- a/tests/model/SiteTreeTest.php +++ b/tests/model/SiteTreeTest.php @@ -889,6 +889,37 @@ class SiteTreeTest extends SapphireTest { $page = SiteTree::get()->byID($page->ID); $this->assertEquals(null, $page->getField('MenuTitle')); } + + public function testMetaTagGeneratorDisabling() { + $generator = Config::inst()->get('SiteTree', 'meta_generator'); + + $page = new SiteTreeTest_PageNode(); + + $meta = $page->MetaTags(); + $this->assertEquals( + 1, + preg_match('/.*meta name="generator" content="SilverStripe - http:\/\/silverstripe.org".*/', $meta), + 'test default functionality - uses value from Config'); + + // test proper escaping of quotes in attribute value + Config::inst()->update('SiteTree', 'meta_generator', 'Generator with "quotes" in it'); + $meta = $page->MetaTags(); + $this->assertEquals( + 1, + preg_match('/.*meta name="generator" content="Generator with "quotes" in it".*/', $meta), + 'test proper escaping of values from Config'); + + // test empty generator - no tag should appear at all + Config::inst()->update('SiteTree', 'meta_generator', ''); + $meta = $page->MetaTags(); + $this->assertEquals( + 0, + preg_match('/.*meta name=.generator..*/', $meta), + 'test blank value means no tag generated'); + + // reset original value + Config::inst()->update('SiteTree', 'meta_generator', $generator); + } }