Skip to content

Commit

Permalink
feature #2279 Deprecate usage of undefined blocks (julienfalque)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

Deprecate usage of undefined blocks

Alternative to #2277.

Commits
-------

75d474a Deprecate usage of undefined blocks
  • Loading branch information
fabpot committed Dec 5, 2016
2 parents 717365d + 75d474a commit 6ab2774
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
14 changes: 9 additions & 5 deletions lib/Twig/Template.php
Expand Up @@ -197,12 +197,16 @@ public function displayBlock($name, array $context, array $blocks = array(), $us
$block = null;
}

if (null !== $template) {
// avoid RCEs when sandbox is enabled
if (!$template instanceof self) {
throw new LogicException('A block must be a method on a Twig_Template instance.');
}
// avoid RCEs when sandbox is enabled
if (null !== $template && !$template instanceof self) {
throw new LogicException('A block must be a method on a Twig_Template instance.');
}

if (!$this->hasBlock($name, $context, $blocks)) {
@trigger_error(sprintf('Displaying undefined block "%s" in template "%s" is deprecated since version 1.29 and will throw an exception in 2.0.', $name, $this->getTemplateName()), E_USER_DEPRECATED);
}

if (null !== $template) {
try {
$template->$block($context, $blocks);
} catch (Twig_Error $e) {
Expand Down
Expand Up @@ -12,7 +12,7 @@
{% block list %}<ul>{{ block('children') }}</ul>{% endblock %}
{% block children %}{% set currentItem = item %}{% for item in currentItem %}{{ block('item') }}{% endfor %}{% set item = currentItem %}{% endblock %}
{% block item %}<li>{% if item is not iterable %}{{ block('label') }}{% else %}{{ block('list') }}{% endif %}</li>{% endblock %}
{% block label %}{{ item }}{{ block('unknown') }}{% endblock %}
{% block label %}{{ item }}{% endblock %}
--TEMPLATE(base.twig)--
{{ block('list') }}
--DATA--
Expand Down
@@ -0,0 +1,12 @@
--TEST--
"block" function with undefined block
--TEMPLATE--
{% extends "base.twig" %}
{% block foo %}{{ parent() }}{{ block('unknown') }}{{ block('bar') }}{% endblock %}
--TEMPLATE(base.twig)--
{% block foo %}Foo{% endblock %}
{% block bar %}Bar{% endblock %}
--DATA--
return array()
--EXPECT--
FooBarBar
28 changes: 24 additions & 4 deletions test/Twig/Tests/TemplateTest.php
Expand Up @@ -214,15 +214,17 @@ public function testGetAttributeWithTemplateAsObjectForDeprecations()
$this->assertNotInstanceof('Twig_Markup', $template->getAttribute($template1, 'empty'));
$this->assertSame('', $template->getAttribute($template1, 'empty'));

$blocks = array('name' => array($template1, 'block_name'));

// trigger some deprecation notice messages to check them with @expectedDeprecation
$template->getAttribute($template, 'renderBlock', array('name', array()));
$template->getAttribute($template, 'displayBlock', array('name', array()));
$template->getAttribute($template, 'renderBlock', array('name', array(), $blocks));
$template->getAttribute($template, 'displayBlock', array('name', array(), $blocks));
$template->getAttribute($template, 'hasBlock', array('name', array()));
$template->getAttribute($template, 'render', array(array()));
$template->getAttribute($template, 'display', array(array()));

$template->getAttribute($template1, 'renderBlock', array('name', array()));
$template->getAttribute($template1, 'displayBlock', array('name', array()));
$template->getAttribute($template1, 'renderBlock', array('name', array(), $blocks));
$template->getAttribute($template1, 'displayBlock', array('name', array(), $blocks));
$template->getAttribute($template1, 'hasBlock', array('name', array()));
$template->getAttribute($template1, 'render', array(array()));
$template->getAttribute($template1, 'display', array(array()));
Expand All @@ -233,6 +235,20 @@ public function testGetAttributeWithTemplateAsObjectForDeprecations()
$this->assertFalse($template->getAttribute($template1, 'displayWithErrorHandling', array(), Twig_Template::METHOD_CALL, true));
}

/**
* @group legacy
* @expectedDeprecation Displaying undefined block "unknown" in template "index.twig" is deprecated since version 1.29 and will throw an exception in 2.0.
* @expectedDeprecation Displaying undefined block "unknown" in template "index.twig" is deprecated since version 1.29 and will throw an exception in 2.0.
*/
public function testRenderBlockWithUndefinedBlock()
{
$twig = new Twig_Environment($this->getMockBuilder('Twig_TemplateTestLoaderInterface')->getMock());

$template = new Twig_TemplateTest($twig, false, 'index.twig');
$template->renderBlock('unknown', array());
$template->displayBlock('unknown', array());
}

/**
* @dataProvider getTestsDependingOnExtensionAvailability
*/
Expand Down Expand Up @@ -534,6 +550,10 @@ public function getAttribute($object, $item, array $arguments = array(), $type =
return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
}
}

public function block_name($context, array $blocks = array())
{
}
}

class Twig_TemplateArrayAccessObject implements ArrayAccess
Expand Down

0 comments on commit 6ab2774

Please sign in to comment.