CMSFormTest.php
3.13 KB
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
120
121
122
123
124
125
126
127
<?php
/**
 * @package framework
 * @subpackage tests
 */
class CMSFormTest extends FunctionalTest {
	public function testValidationExemptActions() {
		$response = $this->get('CMSFormTest_Controller');
		$response = $this->submitForm(
			'Form_Form',
			'action_doSubmit',
			array(
				'Email' => 'test@test.com'
			)
		);
			
		// Firstly, assert that required fields still work when not using an exempt action
		$this->assertPartialMatchBySelector(
			'#SomeRequiredField span.required',
			array(
				'"Some Required Field" is required'
			),
			'Required fields show a notification on field when left blank'
		);
		// Re-submit the form using validation-exempt button
		$response = $this->submitForm(
			'Form_Form',
			'action_doSubmitValidationExempt',
			array(
				'Email' => 'test@test.com'
			)
		);
		// The required message should be empty if validation was skipped
		$items = $this->cssParser()->getBySelector('#SomeRequiredField span.required');
		$this->assertEmpty($items);
		// And the session message should show up is submitted successfully
		$this->assertPartialMatchBySelector(
			'#Form_Form_error',
			array(
				'Validation skipped'
			),
			'Form->sessionMessage() shows up after reloading the form'
		);
	}
	public function testSetValidationExemptActions() {
		$form = $this->getStubForm();
		$form->setValidationExemptActions(array('exemptaction'));
		$exemptActions = $form->getValidationExemptActions();
		$this->assertEquals('exemptaction', $exemptActions[0]);
	}
	protected function getStubForm() {
		$form = new CMSForm(
			new CMSFormTest_Controller(),
			'CMSForm',
			new FieldList(),
			new FieldList()
		);
		return $form;
	}
}
class CMSFormTest_Controller extends Controller implements TestOnly {
	private static $allowed_actions = array('Form');
	private static $url_handlers = array(
		'$Action//$ID/$OtherID' => "handleAction",
	);
	protected $template = 'BlankPage';
	
	public function Link($action = null) {
		return Controller::join_links('CMSFormTest_Controller', $this->request->latestParam('Action'),
			$this->request->latestParam('ID'), $action);
	}
	
	public function Form() {
		$form = new CMSForm(
			$this,
			'Form',
			new FieldList(
				new EmailField('Email'),
				new TextField('SomeRequiredField'),
				new CheckboxSetField('Boxes', null, array('1'=>'one','2'=>'two'))
			),
			new FieldList(
				new FormAction('doSubmit'),
				new FormAction('doSubmitValidationExempt')
			),
			new RequiredFields(
				'Email',
				'SomeRequiredField'
			)
		);
		$form->setValidationExemptActions(array('doSubmitValidationExempt'));
		$form->setResponseNegotiator('foo'); // We aren't testing AJAX responses, so just set anything
		$form->disableSecurityToken(); // Disable CSRF protection for easier form submission handling
		
		return $form;
	}
	
	public function doSubmit($data, $form, $request) {
		$form->sessionMessage('Test save was successful', 'good');
		return $this->redirectBack();
	}
	public function doSubmitValidationExempt($data, $form, $request) {
		$form->sessionMessage('Validation skipped', 'good');
		return $this->redirectBack();
	}
	public function getViewer($action = null) {
		return new SSViewer('BlankPage');
	}
}