Skip to content

Commit

Permalink
Make meta() accept 'block' option in single argument case.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Mar 4, 2017
1 parent 06bd830 commit 0112270
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/View/Helper/HtmlHelper.php
Expand Up @@ -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 `<link />` element.
* @return string|null A completed `<link />` 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],
Expand Down Expand Up @@ -269,7 +268,7 @@ public function meta($type, $content = null, array $options = [])
}
}

$options += $type;
$options += $type + ['block' => null];
$out = null;

if (isset($options['link'])) {
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/View/Helper/HtmlHelperTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 0112270

Please sign in to comment.