From 011227010e00568a229b2e5c17a9c33731c91ceb Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 4 Mar 2017 17:39:33 -0500 Subject: [PATCH] Make meta() accept 'block' option in single argument case. Allow the block option to be defined in the custom meta tag attributes array. This allows this usage to be more compact and not require a 2nd and 3rd argument. --- src/View/Helper/HtmlHelper.php | 9 ++++----- tests/TestCase/View/Helper/HtmlHelperTest.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/View/Helper/HtmlHelper.php b/src/View/Helper/HtmlHelper.php index be5051533fc..e7ac877c944 100644 --- a/src/View/Helper/HtmlHelper.php +++ b/src/View/Helper/HtmlHelper.php @@ -222,17 +222,16 @@ public function docType($type = 'html5') * - `block` - Set to true to append output to view block "meta" or provide * custom block name. * - * @param string|array $type The title of the external resource + * @param string|array $type The title of the external resource, Or an array of attributes for a + * custom meta tag. * @param string|array|null $content The address of the external resource or string for content attribute * @param array $options Other attributes for the generated tag. If the type attribute is html, * rss, atom, or icon, the mime-type is returned. - * @return string A completed `` element. + * @return string|null A completed `` element, or null if the element was sent to a block. * @link http://book.cakephp.org/3.0/en/views/helpers/html.html#creating-meta-tags */ public function meta($type, $content = null, array $options = []) { - $options += ['block' => null]; - if (!is_array($type)) { $types = [ 'rss' => ['type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $content], @@ -269,7 +268,7 @@ public function meta($type, $content = null, array $options = []) } } - $options += $type; + $options += $type + ['block' => null]; $out = null; if (isset($options['link'])) { diff --git a/tests/TestCase/View/Helper/HtmlHelperTest.php b/tests/TestCase/View/Helper/HtmlHelperTest.php index 03e9367c47f..4730d3d46d5 100644 --- a/tests/TestCase/View/Helper/HtmlHelperTest.php +++ b/tests/TestCase/View/Helper/HtmlHelperTest.php @@ -1767,6 +1767,25 @@ public function testMetaWithBlocks() $this->assertNull($result); } + /** + * Test meta() with custom tag and block argument + */ + public function testMetaCustomWithBlock() + { + $this->View->expects($this->at(0)) + ->method('append') + ->with('meta', $this->stringContains('og:site_name')); + $this->View->expects($this->at(1)) + ->method('append') + ->with('meta', $this->stringContains('og:description')); + + $result = $this->Html->meta(['property' => 'og:site_name', 'content' => 'CakePHP', 'block' => true]); + $this->assertNull($result, 'compact style should work'); + + $result = $this->Html->meta(['property' => 'og:description', 'content' => 'CakePHP'], null, ['block' => true]); + $this->assertNull($result, 'backwards compat style should work.'); + } + /** * testTableHeaders method *