Completed
Push — master ( b537e6...b70c66 )
by Daniel
35:35
created

checkTemplateIsCorrect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 18
rs 9.4285
c 1
b 1
f 0
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package userforms
5
 */
6
7
class UserDefinedFormControllerTest extends FunctionalTest {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
	static $fixture_file = 'UserDefinedFormTest.yml';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $fixture_file.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
10
11
	public function testProcess() {
12
		$form = $this->setupFormFrontend();
13
14
		$controller = new UserDefinedFormControllerTest_Controller($form);
0 ignored issues
show
Unused Code introduced by
$controller is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
15
16
		$this->autoFollowRedirection = false;
17
		$this->clearEmails();
18
19
		// load the form
20
		$this->get($form->URLSegment);
21
22
		$field = $this->objFromFixture('EditableTextField', 'basic-text');
23
24
		$response = $this->submitForm('UserForm_Form', null, array($field->Name => 'Basic Value'));
25
26
		// should have a submitted form field now
27
		$submitted = DataObject::get('SubmittedFormField', "\"Name\" = 'basic-text-name'");
28
		$this->assertDOSAllMatch(array('Name' => 'basic-text-name', 'Value' => 'Basic Value', 'Title' => 'Basic Text Field'), $submitted);
29
30
		// check emails
31
		$this->assertEmailSent('[email protected]', '[email protected]', 'Email Subject');
32
		$email = $this->findEmail('[email protected]', '[email protected]', 'Email Subject');
33
34
		// assert that the email has the field title and the value html email
35
		$parser = new CSSContentParser($email['content']);
36
		$title = $parser->getBySelector('strong');
37
38
		$this->assertEquals('Basic Text Field', (string) $title[0], 'Email contains the field name');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
40
		$value = $parser->getBySelector('dd');
41
		$this->assertEquals('Basic Value', (string) $value[0], 'Email contains the value');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43
		// no html
44
		$this->assertEmailSent('[email protected]', '[email protected]', 'Email Subject');
45
		$nohtml = $this->findEmail('[email protected]', '[email protected]', 'Email Subject');
46
47
		$this->assertContains('Basic Text Field: Basic Value', $nohtml['content'], 'Email contains no html');
48
49
		// no data
50
		$this->assertEmailSent('[email protected]', '[email protected]', 'Email Subject');
51
		$nodata = $this->findEmail('[email protected]', '[email protected]', 'Email Subject');
52
53
		$parser = new CSSContentParser($nodata['content']);
54
		$list = $parser->getBySelector('dl');
55
56
		$this->assertFalse(isset($list[0]), 'Email contains no fields');
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
58
		// check to see if the user was redirected (301)
59
		$this->assertEquals($response->getStatusCode(), 302);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
		$this->assertStringEndsWith('finished#uff', $response->getHeader('Location'));
0 ignored issues
show
Bug introduced by
The method assertStringEndsWith() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
	}
62
63
	public function testValidation() {
64
		$form = $this->setupFormFrontend('email-form');
65
66
		// Post with no fields
67
		$this->get($form->URLSegment);
68
		$response = $this->submitForm('UserForm_Form', null, array());
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
		$this->assertPartialMatchBySelector(
70
			'.field .message',
71
			array('This field is required')
72
		);
73
74
		// Post with all fields, but invalid email
75
		$this->get($form->URLSegment);
76
		$this->submitForm('UserForm_Form', null, array(
77
			'required-email' => 'invalid',
78
			'required-text' => 'bob'
79
		));
80
		$this->assertPartialMatchBySelector(
81
			'.field .message',
82
			array('Please enter an email address')
83
		);
84
85
		// Post with only required
86
		$this->get($form->URLSegment);
87
		$this->submitForm('UserForm_Form', null, array(
88
			'required-text' => 'bob'
89
		));
90
		$this->assertPartialMatchBySelector(
91
			'p',
92
			array("Thanks, we've received your submission.")
93
		);
94
	}
95
96 View Code Duplication
	public function testFinished() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
		$form = $this->setupFormFrontend();
98
99
		// set formProcessed and SecurityID to replicate the form being filled out
100
		$this->session()->inst_set('SecurityID', 1);
101
		$this->session()->inst_set('FormProcessed', 1);
102
103
		$response = $this->get($form->URLSegment.'/finished');
104
105
		$this->assertContains($form->OnCompleteMessage ,$response->getBody());
106
	}
107
108 View Code Duplication
	public function testAppendingFinished() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
		$form = $this->setupFormFrontend();
110
111
		// replicate finished being added to the end of the form URL without the form being filled out
112
		$this->session()->inst_set('SecurityID', 1);
113
		$this->session()->inst_set('FormProcessed', null);
114
115
		$response = $this->get($form->URLSegment.'/finished');
116
117
		$this->assertNotContains($form->OnCompleteMessage ,$response->getBody());
118
	}
119
120
	public function testForm() {
121
		$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
122
123
		$controller = new UserDefinedFormControllerTest_Controller($form);
124
125
		// test form
126
		$this->assertEquals($controller->Form()->getName(), 'Form', 'The form is referenced as Form');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
Consider using $controller->Form()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
127
		$this->assertEquals($controller->Form()->Fields()->Count(), 1); // disabled SecurityID token fields
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
		$this->assertEquals($controller->Form()->Actions()->Count(), 1);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
		$this->assertEquals(count($controller->Form()->getValidator()->getRequired()), 0);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
131
		$requiredForm = $this->objFromFixture('UserDefinedForm', 'validation-form');
132
		$controller = new UserDefinedFormControllerTest_Controller($requiredForm);
133
134
		$this->assertEquals($controller->Form()->Fields()->Count(), 1); // disabled SecurityID token fields
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
		$this->assertEquals($controller->Form()->Actions()->Count(), 1);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
		$this->assertEquals(count($controller->Form()->getValidator()->getRequired()), 1);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
	}
138
139
	public function testGetFormFields() {
140
		// generating the fieldset of fields
141
		$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
142
143
		$controller = new UserDefinedFormControllerTest_Controller($form);
144
145
		$formSteps = $controller->Form()->getFormFields();
146
		$firstStep = $formSteps->first();
147
148
		$this->assertEquals($formSteps->Count(), 1);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
		$this->assertEquals($firstStep->getChildren()->Count(), 1);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
151
		// custom error message on a form field
152
		$requiredForm = $this->objFromFixture('UserDefinedForm', 'validation-form');
153
		$controller = new UserDefinedFormControllerTest_Controller($requiredForm);
154
155
		UserDefinedForm::config()->required_identifier = "*";
156
157
		$formSteps = $controller->Form()->getFormFields();
158
		$firstStep = $formSteps->first();
159
		$firstField = $firstStep->getChildren()->first();
160
161
		$this->assertEquals('Custom Error Message', $firstField->getCustomValidationMessage());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
		$this->assertEquals($firstField->Title(), 'Required Text Field <span class=\'required-identifier\'>*</span>');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
164
		// test custom right title
165
		$field = $form->Fields()->limit(1, 1)->First();
0 ignored issues
show
Bug introduced by
The method Fields() does not exist on DataObject. Did you maybe mean beforeUpdateCMSFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
166
		$field->RightTitle = 'Right Title';
167
		$field->write();
168
169
		$controller = new UserDefinedFormControllerTest_Controller($form);
170
		$formSteps = $controller->Form()->getFormFields();
171
		$firstStep = $formSteps->first();
172
173
		$this->assertEquals($firstStep->getChildren()->First()->RightTitle(), "Right Title");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
174
175
		// test empty form
176
		$emptyForm = $this->objFromFixture('UserDefinedForm', 'empty-form');
177
		$controller = new UserDefinedFormControllerTest_Controller($emptyForm);
178
179
		$this->assertFalse($controller->Form()->getFormFields()->exists());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
180
	}
181
182
	public function testGetFormActions()
183
	{
184
		// generating the fieldset of actions
185
		$form = $this->objFromFixture('UserDefinedForm', 'basic-form-page');
186
187
		$controller = new UserDefinedFormControllerTest_Controller($form);
188
		$actions = $controller->Form()->getFormActions();
189
190
		// by default will have 1 submit button which links to process
191
		$expected = new FieldList(new FormAction('process', 'Submit'));
192
		$expected->setForm($controller->Form());
193
194
		$this->assertEquals($actions, $expected);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
196
		// the custom popup should have a reset button and a custom text
197
		$custom = $this->objFromFixture('UserDefinedForm', 'form-with-reset-and-custom-action');
198
		$controller = new UserDefinedFormControllerTest_Controller($custom);
199
		$actions = $controller->Form()->getFormActions();
200
201
		$expected = new FieldList(new FormAction('process', 'Custom Button'));
202
		$expected->push(new ResetFormAction("clearForm", "Clear"));
203
		$expected->setForm($controller->Form());
204
205
		$this->assertEquals($actions, $expected);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
206
	}
207
208 View Code Duplication
	public function testRenderingIntoFormTemplate() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
		$form = $this->setupFormFrontend();
210
211
		$form->Content = 'This is some content without a form nested between it';
212
		$form->doPublish();
213
214
		$controller = new UserDefinedFormControllerTest_Controller($form);
215
216
		// check to see if $Form is replaced to inside the content
217
		$index = new ArrayData($controller->index());
218
		$parser = new CSSContentParser($index->renderWith(array('UserDefinedFormControllerTest')));
219
220
		$this->checkTemplateIsCorrect($parser);
221
	}
222
223 View Code Duplication
	public function testRenderingIntoTemplateWithSubstringReplacement() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
		$form = $this->setupFormFrontend();
225
226
		$controller = new UserDefinedFormControllerTest_Controller($form);
227
228
		// check to see if $Form is replaced to inside the content
229
		$index = new ArrayData($controller->index());
230
		$parser = new CSSContentParser($index->renderWith(array('UserDefinedFormControllerTest')));
231
232
		$this->checkTemplateIsCorrect($parser);
233
	}
234
	/**
235
	 * Publish a form for use on the frontend
236
	 *
237
	 * @param string $fixtureName
238
	 * @return UserDefinedForm
239
	 */
240
	protected function setupFormFrontend($fixtureName = 'basic-form-page') {
241
		$form = $this->objFromFixture('UserDefinedForm', $fixtureName);
242
		$this->logInWithPermission('ADMIN');
243
244
		$form->doPublish();
245
246
		$member = Member::currentUser();
247
		$member->logOut();
248
249
		return $form;
250
	}
251
252
	public function checkTemplateIsCorrect($parser) {
253
		$this->assertArrayHasKey(0, $parser->getBySelector('form#UserForm_Form'));
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
254
255
		// check for the input
256
		$this->assertArrayHasKey(0, $parser->getBySelector('input.text'));
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
257
258
		// check for the label and the text
259
		$label = $parser->getBySelector('label.left');
260
		$this->assertArrayHasKey(0, $label);
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
261
262
		$this->assertEquals((string) $label[0][0], "Basic Text Field", "Label contains correct field name");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
263
264
		// check for the action
265
		$action = $parser->getBySelector('input.action');
266
		$this->assertArrayHasKey(0, $action);
0 ignored issues
show
Bug introduced by
The method assertArrayHasKey() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
267
268
		$this->assertEquals((string) $action[0]['value'], "Submit", "Submit button has default text");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<UserDefinedFormControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
269
	}
270
}
271
272
class UserDefinedFormControllerTest_Controller extends UserDefinedForm_Controller implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
273
274
	/**
275
	 * Overloaded to avoid inconsistencies between 2.4.2 and 2.4.3 (disables all security tokens in unit tests by default)
276
	 */
277
	public function Form() {
278
		$form = parent::Form();
279
280
		if($form) $form->disableSecurityToken();
281
282
		return $form;
283
	}
284
285
}
286