diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c403ab3c87..3be37c2e578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # [2.0.12](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.12) (????-??-??) +- Fixed regression changes for `Phalcon\Mvc\View\Engine\Volt::callMacro` [#11745](https://github.com/phalcon/cphalcon/issues/11745) # [2.0.11](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.11) (2016-05-04) - Fix Model magic set functionality to maintain variable visibility and utilize setter methods.[#11286](https://github.com/phalcon/cphalcon/issues/11286) diff --git a/phalcon/mvc/view/engine/volt.zep b/phalcon/mvc/view/engine/volt.zep index a42d7f6bbea..6bf49d5f649 100644 --- a/phalcon/mvc/view/engine/volt.zep +++ b/phalcon/mvc/view/engine/volt.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -121,7 +121,6 @@ class Volt extends Engine implements EngineInterface if mustClean { this->_view->setContent(ob_get_contents()); - //ob_clean(); } } @@ -285,7 +284,7 @@ class Volt extends Engine implements EngineInterface /** * Checks if a macro is defined and calls it */ - public function callMacro(string! name, array arguments) + public function callMacro(string! name, array arguments = []) -> var { var macro; @@ -293,6 +292,6 @@ class Volt extends Engine implements EngineInterface throw new Exception("Macro '" . name . "' does not exist"); } - return call_user_func_array(macro, arguments); + return call_user_func(macro, arguments); } } diff --git a/unit-tests/TODO.txt b/unit-tests/TODO.txt index 3b6ae6fd4da..06e6013fd19 100644 --- a/unit-tests/TODO.txt +++ b/unit-tests/TODO.txt @@ -1,4 +1,3 @@ - Tests for foreign keys cascade in the ORM - Tests for many-to-many relations -- Tests for macros in Volt - Tests for +=, -=, *=, /=, ++, -- in Volt diff --git a/unit-tests/ViewEnginesVoltTest.php b/unit-tests/ViewEnginesVoltTest.php index 4238deccbfa..834d8483bef 100644 --- a/unit-tests/ViewEnginesVoltTest.php +++ b/unit-tests/ViewEnginesVoltTest.php @@ -4,7 +4,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2014 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -19,6 +19,12 @@ */ use Phalcon\Mvc\View\Engine\Volt\Compiler; +use Phalcon\Mvc\View\Engine\Volt; +use Phalcon\Mvc\View; +use Phalcon\Escaper; +use Phalcon\Mvc\Url; +use Phalcon\Tag; +use Phalcon\Di; class SomeObject implements Iterator, Countable { @@ -1120,12 +1126,17 @@ public function testVoltUsersFunctions() return 'str_shuffle(' . $arguments . ')'; }); + $volt->addFunction('strtotime', 'strtotime'); + $compilation = $volt->compileString('{{ random() }}'); $this->assertEquals($compilation, ''); $compilation = $volt->compileString('{{ shuffle("hello") }}'); $this->assertEquals($compilation, ''); + $compilation = $volt->compileString('{{ strtotime("now") }}'); + $this->assertEquals("", $compilation); + } public function testVoltUsersFilters() @@ -1379,4 +1390,105 @@ public function testVoltEngineBuiltInFunctions() $this->assertEquals($view->getContent(), 'Length Array: 4Length Object: 4Length String: 5Length No String: 4Slice Array: 1,2,3,4Slice Array: 2,3Slice Array: 1,2,3Slice Object: 2,3,4Slice Object: 2,3Slice Object: 1,2Slice String: helSlice String: elSlice String: lloSlice No String: 123Slice No String: 23Slice No String: 34'); } + + public function testVoltMacros() + { + $this->removeFiles([ + 'unit-tests/views/macro/hello.volt.php', + 'unit-tests/views/macro/conditionaldate.volt.php', + 'unit-tests/views/macro/my_input.volt.php', + 'unit-tests/views/macro/error_messages.volt.php', + 'unit-tests/views/macro/related_links.volt.php', + 'unit-tests/views/macro/strtotime.volt.php', + ]); + + Di::reset(); + + $view = new View; + $di = new Di; + + $di->set('escaper', function() { return new Escaper; }); + $di->set('tag', function() { return new Tag; }); + $di->set('url', function() { return (new Url)->setBaseUri('/'); }); + + $view->setDI($di); + $view->setViewsDir('unit-tests/views/'); + $view->registerEngines(array( + '.volt' => function ($view, $di) { + $volt = new Volt($view, $di); + $compiler = $volt->getCompiler(); + $compiler->addFunction('strtotime', 'strtotime'); + + return $volt; + } + )); + + $view->start(); + $view->render('macro', 'hello'); + $view->finish(); + + $this->assertEquals('Hello World', $view->getContent()); + + $view->start(); + $view->render('macro', 'conditionaldate'); + $view->finish(); + + $this->assertEquals(sprintf('from
%s, %s UTC', date('Y-m-d'), date('H:i')), $view->getContent()); + + $view->start(); + $view->render('macro', 'my_input'); + $view->finish(); + + $this->assertEquals('

', $view->getContent()); + + $view->start(); + $view->render('macro', 'error_messages'); + $view->finish(); + + $this->assertEquals('
InvalidnameThe name is invalid
', $view->getContent()); + + $view->setVar('links', array((object) array('url' => 'localhost', 'text' => 'Menu item', 'title' => 'Menu title'))); + + $view->start(); + $view->render('macro', 'related_links'); + $view->finish(); + + $this->assertEquals('', $view->getContent()); + + $view->setVar('date', new DateTime()); + + $view->start(); + $view->render('macro', 'strtotime'); + $view->finish(); + + $content = $view->getContent(); + $content = explode('%', $content); + + $this->assertEquals(3, count($content)); + $this->assertEquals($content[0], $content[1]); + $this->assertEquals($content[1], $content[2]); + $this->assertEquals($content[2], $content[0]); + + $this->removeFiles([ + 'unit-tests/views/macro/hello.volt.php', + 'unit-tests/views/macro/conditionaldate.volt.php', + 'unit-tests/views/macro/my_input.volt.php', + 'unit-tests/views/macro/error_messages.volt.php', + 'unit-tests/views/macro/related_links.volt.php', + 'unit-tests/views/macro/strtotime.volt.php', + ]); + } + + protected function removeFiles($files) + { + if (!is_array($files)) { + $files = array($files); + } + + foreach ($files as $file) { + if (file_exists($file) && is_readable($file)) { + @unlink($file); + } + } + } } diff --git a/unit-tests/views/macro/conditionaldate.volt b/unit-tests/views/macro/conditionaldate.volt new file mode 100644 index 00000000000..8948b589d1a --- /dev/null +++ b/unit-tests/views/macro/conditionaldate.volt @@ -0,0 +1,9 @@ +{%- macro conditionaldate(condition, tdate, ttime, tz) -%} + {%- if condition -%} from
{{ tdate }}, {{ ttime }} {{ tz }}{%- endif -%} +{%- endmacro -%} + +{%- set tdate = date('Y-m-d') -%} +{%- set ttime = date('H:i') -%} +{%- set tz = 'UTC' -%} + +{{- conditionaldate(true, tdate, ttime, tz) -}} diff --git a/unit-tests/views/macro/error_messages.volt b/unit-tests/views/macro/error_messages.volt new file mode 100644 index 00000000000..6625eafbf68 --- /dev/null +++ b/unit-tests/views/macro/error_messages.volt @@ -0,0 +1,5 @@ +{%- macro error_messages(message, field, type) -%} +
{{ type }}{{ field }}{{ message }}
+{%- endmacro -%} + +{{- error_messages('type': 'Invalid', 'message': 'The name is invalid', 'field': 'name') -}} diff --git a/unit-tests/views/macro/hello.volt b/unit-tests/views/macro/hello.volt new file mode 100644 index 00000000000..a7f14fab5d5 --- /dev/null +++ b/unit-tests/views/macro/hello.volt @@ -0,0 +1,5 @@ +{%- macro hello(name) -%} + {%- return "Hello " ~ name -%} +{%- endmacro -%} + +{{- hello('World') -}} diff --git a/unit-tests/views/macro/my_input.volt b/unit-tests/views/macro/my_input.volt new file mode 100644 index 00000000000..166e67b79b9 --- /dev/null +++ b/unit-tests/views/macro/my_input.volt @@ -0,0 +1,5 @@ +{%- macro my_input(name, class="input-text") -%} + {%- return text_field(name, 'class': class) -%} +{%- endmacro -%} + +{{- '

' ~ my_input('name') ~ '

' -}} diff --git a/unit-tests/views/macro/related_links.volt b/unit-tests/views/macro/related_links.volt new file mode 100644 index 00000000000..cd5ec9f8f57 --- /dev/null +++ b/unit-tests/views/macro/related_links.volt @@ -0,0 +1,9 @@ +{%- macro related_bar(related_links) -%} + +{%- endmacro -%} + +{{- related_bar(links) -}} diff --git a/unit-tests/views/macro/strtotime.volt b/unit-tests/views/macro/strtotime.volt new file mode 100644 index 00000000000..9f2e8c18b34 --- /dev/null +++ b/unit-tests/views/macro/strtotime.volt @@ -0,0 +1 @@ +{{- date.getTimeStamp() -}} % {{- strtotime('now') -}} % {{- strtotime(date.format('Y-m-d H:i:s')) -}}