Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into hotfix/filter-validator-loading
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Dec 19, 2011
5 parents c9e475e + 1fa3bfc + 14c7cd8 + 5a55cdf + 3a78ba4 commit a774960
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 10 deletions.
56 changes: 56 additions & 0 deletions src/Helper/FormCsrf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend\View
* @subpackage Helper
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\View\Helper;

use Zend\Form\Element\Hash as HashElement;

/**
* Helper for rendering CSRF token elements outside of Zend\Form using
* Zend\Form\Element\Hash
*
* @package Zend\View
* @subpackage Helper
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class FormCsrf extends AbstractHelper
{
/**
* @var array
*/
protected $hashElements;

/**
* __invoke
*
* @param string $name
* @return string
*/
public function __invoke($name = 'csrf')
{
if (!isset($this->hashElements[$name])) {
$this->hashElements[$name] = new HashElement($name);
$this->hashElements[$name]->setDecorators(array('ViewHelper'));
}
return trim($this->hashElements[$name]->render($this->getView()));
}
}
2 changes: 1 addition & 1 deletion src/Helper/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function __invoke($name = null, $model = null)
public function cloneView()
{
$view = clone $this->view;
$view->vars()->clear();
$view->setVars(array());
return $view;
}

Expand Down
1 change: 1 addition & 0 deletions src/HelperLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class HelperLoader extends PluginClassLoader
'fieldset' => 'Zend\View\Helper\Fieldset',
'formbutton' => 'Zend\View\Helper\FormButton',
'formcheckbox' => 'Zend\View\Helper\FormCheckbox',
'formcsrf' => 'Zend\View\Helper\FormCsrf',
'formerrors' => 'Zend\View\Helper\FormErrors',
'formfile' => 'Zend\View\Helper\FormFile',
'formhidden' => 'Zend\View\Helper\FormHidden',
Expand Down
59 changes: 50 additions & 9 deletions test/Helper/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
* @namespace
*/
namespace ZendTest\View\Helper;
use Zend\Cache;
use Zend\Currency;
use Zend\View\Helper;

use Zend\Cache\StorageFactory as CacheFactory,
Zend\Cache\Storage\Adapter as CacheAdapter,
Zend\Currency,
Zend\View\Helper;

/**
* Test class for Zend_View_Helper_Currency
Expand Down Expand Up @@ -64,12 +65,30 @@ public function clearRegistry()
public function setUp()
{
$this->clearRegistry();
$this->_cache = Cache\Cache::factory('Core', 'File',
array('lifetime' => 120, 'automatic_serialization' => true),
array('cache_dir' => sys_get_temp_dir())
);
Currency\Currency::setCache($this->_cache);

$this->_cacheDir = sys_get_temp_dir() . '/zend_view_helper_currency';
$this->_removeRecursive($this->_cacheDir);
mkdir($this->_cacheDir);

$this->_cache = CacheFactory::factory(array(
'adapter' => array(
'name' => 'Filesystem',
'options' => array(
'ttl' => 120,
'cache_dir' => $this->_cacheDir,
)
),
'plugins' => array(
array(
'name' => 'serializer',
'options' => array(
'serializer' => 'php_serialize',
),
),
),
));

Currency\Currency::setCache($this->_cache);

$this->helper = new Helper\Currency('de_AT');
}
Expand All @@ -83,10 +102,32 @@ public function setUp()
public function tearDown()
{
unset($this->helper);
$this->_cache->clean(Cache\Cache::CLEANING_MODE_ALL);
$this->_cache->clear(CacheAdapter::MATCH_ALL);
$this->_removeRecursive($this->_cacheDir);
$this->clearRegistry();
}

protected function _removeRecursive($dir)
{
if (file_exists($dir)) {
$dirIt = new \DirectoryIterator($dir);
foreach ($dirIt as $entry) {
$fname = $entry->getFilename();
if ($fname == '.' || $fname == '..') {
continue;
}

if ($entry->isFile()) {
unlink($entry->getPathname());
} else {
$this->_removeRecursive($entry->getPathname());
}
}

rmdir($dir);
}
}

public function testCurrencyObjectPassedToConstructor()
{
$curr = new Currency\Currency('de_AT');
Expand Down
103 changes: 103 additions & 0 deletions test/Helper/FormCsrfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zendview
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace ZendTest\View\Helper;

use PHPUnit_Framework_TestCase as TestCase,
Zend\View\PhpRenderer as View,
Zend\View\Helper\FormCsrf;

/**
* @category Zend
* @package Zendview
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zendview
* @group Zendview_Helper
*/
class FormCsrfTest extends TestCase
{
/**
* @var FormCsrf
*/
protected $helper;

/**
* @var View
*/
protected $view;

/**
* Prepares the environment before running a test.
*/
protected function setUp()
{
$this->helper = new FormCsrf();
$this->view = new View();
$this->view->doctype()->setDoctype(strtoupper("XHTML1_STRICT"));
$this->helper->setView($this->view);

if (isset($_SERVER['HTTPS'])) {
unset ($_SERVER['HTTPS']);
}
}

/**
* Cleans up the environment after running a test.
*/
protected function tearDown()
{
unset($this->helper, $this->view);
}

public function testCsrfXhtmlDoctype()
{
$this->assertRegExp(
'/\/>$/',
$this->helper->__invoke()
);
}

public function testCsrfHtmlDoctype()
{
$object = new FormCsrf();
$view = new View();
$view->doctype()->setDoctype(strtoupper("HTML5"));
$object->setView($view);

$this->assertRegExp(
'/[^\/]>$/',
$object->__invoke()
);
}

public function testReturnInputTag()
{
$this->assertRegExp(
"/^<input\s.+/",
$this->helper->__invoke()
);
}
}

0 comments on commit a774960

Please sign in to comment.