This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
FormElementManagerFactoryTest.php
119 lines (102 loc) · 4.29 KB
/
FormElementManagerFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Form;
use ArrayObject;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Mvc\Service\FormElementManagerFactory;
use Zend\Mvc\Service\DiFactory;
use Zend\Mvc\Service\DiAbstractServiceFactoryFactory;
use Zend\Mvc\Service\DiServiceInitializerFactory;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;
use Zend\Form\FormElementManager;
use Zend\Session\Container as SessionContainer;
use Zend\Validator\Csrf;
class FormElementManagerFactoryTest extends TestCase
{
/**
* @var ServiceManager
*/
protected $services;
/**
* @var \Zend\Mvc\Controller\ControllerManager
*/
protected $loader;
public function setUp()
{
$formElementManagerFactory = new FormElementManagerFactory();
$config = new ArrayObject(array('di' => array()));
$services = $this->services = new ServiceManager();
$services->setService('Zend\ServiceManager\ServiceLocatorInterface', $services);
$services->setFactory('FormElementManager', $formElementManagerFactory);
$services->setService('Config', $config);
$services->setFactory('Di', new DiFactory());
$services->setFactory('DiAbstractServiceFactory', new DiAbstractServiceFactoryFactory());
$services->setFactory('DiServiceInitializer', new DiServiceInitializerFactory());
$this->manager = $services->get('FormElementManager');
$this->standaloneManager = new FormElementManager();
}
public function tearDown()
{
$ref = new \ReflectionClass('Zend\Validator\Csrf');
$hashCache = $ref->getProperty('hashCache');
$hashCache->setAccessible(true);
$hashCache->setValue(new Csrf, array());
SessionContainer::setDefaultManager(null);
}
public function testWillInstantiateFormFromInvokable()
{
$form = $this->manager->get('form');
$this->assertInstanceof('Zend\Form\Form', $form);
}
public function testWillInstantiateFormFromDiAbstractFactory()
{
//without DiAbstractFactory
$this->assertFalse($this->standaloneManager->has('ZendTest\Form\TestAsset\CustomForm'));
//with DiAbstractFactory
$this->assertTrue($this->manager->has('ZendTest\Form\TestAsset\CustomForm'));
$form = $this->manager->get('ZendTest\Form\TestAsset\CustomForm');
$this->assertInstanceof('ZendTest\Form\TestAsset\CustomForm', $form);
}
public function testNoWrapFieldName()
{
$form = $this->manager->get('ZendTest\Form\TestAsset\CustomForm');
$this->assertFalse($form->wrapElements(), 'ensure wrapElements option');
$this->assertTrue($form->has('email'), 'ensure the form has email element');
$emailElement = $form->get('email');
$this->assertEquals('email', $emailElement->getName());
}
public function testCsrfCompatibility()
{
$_SESSION = array();
$formClass = 'ZendTest\Form\TestAsset\CustomForm';
$ref = new \ReflectionClass('Zend\Validator\Csrf');
$hashPropRef = $ref->getProperty('hash');
$hashPropRef->setAccessible(true);
//check bare born
$preForm = new $formClass;
$csrf = $preForm->get('csrf')->getCsrfValidator();
$this->assertNull($hashPropRef->getValue($csrf), 'Test "new Form" has no hash');
//check FormElementManager
$postForm = $this->manager->get($formClass);
$postCsrf = $postForm->get('csrf')->getCsrfValidator();
$this->assertNull($hashPropRef->getValue($postCsrf), 'Test "form from FormElementManager" has no hash');
}
public function testCsrfWorkFlow()
{
$_SESSION = array();
$formClass = 'ZendTest\Form\TestAsset\CustomForm';
$preForm = new $formClass;
$preForm->prepare();
$requestHash = $preForm->get('csrf')->getValue();
$postForm = $this->manager->get($formClass);
$postCsrf = $postForm->get('csrf')->getCsrfValidator();
$this->assertTrue($postCsrf->isValid($requestHash), 'Test csrf validation');
}
}