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

Commit

Permalink
Merge branch 'feature/csrf-view-helper' of https://github.com/EvanDot…
Browse files Browse the repository at this point in the history
…Pro/zf2 into hotfix/view-csrf-helper
  • Loading branch information
weierophinney committed Dec 14, 2011
4 parents 289d19e + c9e475e + 1fa3bfc + 6f899c8 commit 5a55cdf
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 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()));
}
}
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
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 5a55cdf

Please sign in to comment.