Completed
Push — master ( eb554a...243cae )
by Damian
02:26
created

MultiFormTest::testCustomGetVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/**
3
 * MultiFormTest
4
 * For testing purposes, we have some test classes:
5
 *
6
 *  - MultiFormTest_Controller (simulation of a real Controller class)
7
 *  - MultiFormTest_Form (subclass of MultiForm)
8
 *  - MultiFormTest_StepOne (subclass of MultiFormStep)
9
 *  - MultiFormTest_StepTwo (subclass of MultiFormStep)
10
 *  - MultiFormTest_StepThree (subclass of MultiFormStep)
11
 *
12
 * The above classes are used to simulate real-world behaviour
13
 * of the multiform module - for example, MultiFormTest_Controller
14
 * is a simulation of a page where MultiFormTest_Form is a simple
15
 * multi-step contact form it belongs to.
16
 *
17
 * @package multiform
18
 * @subpackage tests
19
 */
20
class MultiFormTest 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...
21
22
	public static $fixture_file = 'multiform/tests/MultiFormTest.yml';
23
24
	protected $controller;
25
26
	public function setUp() {
27
		parent::setUp();
28
29
		$this->controller = new MultiFormTest_Controller();
30
		$this->form = $this->controller->Form();
0 ignored issues
show
Bug introduced by
The property form does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
	}
32
33
	public function testInitialisingForm() {
34
		$this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MultiFormTest>.

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...
35
		$this->assertTrue(is_numeric($this->form->getSession()->ID) && ($this->form->getSession()->ID > 0));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MultiFormTest>.

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...
36
		$this->assertEquals('MultiFormTest_StepOne', $this->form->getStartStep());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MultiFormTest>.

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...
37
	}
38
39
	public function testSessionGeneration() {
40
		$this->assertTrue($this->form->session->ID > 0);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<MultiFormTest>.

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...
41
	}
42
43
	public function testMemberLogging() {
44
		// Grab any user to fake being logged in as, and ensure that after a session is written it has
45
		// that user as the submitter.
46
		$userId = Member::get_one("Member")->ID;
47
		$this->session()->inst_set('loggedInAs', $userId);
48
49
		$session = $this->form->session;
50
		$session->write();
51
52
		$this->assertEquals($userId, $session->SubmitterID);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MultiFormTest>.

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...
53
	}
54
55
	public function testSecondStep() {
56
		$this->assertEquals('MultiFormTest_StepTwo', $this->form->getCurrentStep()->getNextStep());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MultiFormTest>.

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
59
	public function testParentForm() {
60
		$currentStep = $this->form->getCurrentStep();
61
		$this->assertEquals($currentStep->getForm()->class, $this->form->class);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MultiFormTest>.

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...
62
	}
63
64
	public function testTotalStepCount() {
65
		$this->assertEquals(3, $this->form->getAllStepsLinear()->Count());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<MultiFormTest>.

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...
66
	}
67
68
	public function testCompletedSession() {
69
		$this->form->setCurrentSessionHash($this->form->session->Hash);
70
		$this->assertInstanceOf('MultiFormSession', $this->form->getCurrentSession());
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<MultiFormTest>.

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...
71
		$this->form->session->markCompleted();
72
		$this->assertNull($this->form->getCurrentSession());
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<MultiFormTest>.

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...
73
	}
74
75
	public function testIncorrectSessionIdentifier() {
76
		$this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up!
77
78
		// A new session is generated, even though we made up the identifier
79
		$this->assertInstanceOf('MultiFormSession', $this->form->session);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<MultiFormTest>.

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...
80
	}
81
82
	function testCustomGetVar() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
		Config::nest();
84
		Config::inst()->update('MultiForm', 'get_var', 'SuperSessionID');
85
86
		$form = $this->controller->Form();
87
		$this->assertContains('SuperSessionID', $form::$ignored_fields, "GET var wasn't added to ignored fields");
88
		$this->assertContains('SuperSessionID', $form->FormAction(), "Form action doesn't contain correct session 
89
			ID parameter");
90
		$this->assertContains('SuperSessionID', $form->getCurrentStep()->Link(), "Form step doesn't contain correct 
91
			session ID parameter");
92
93
		Config::unnest();
94
}
95
	
96
}
97
98
/**
99
 * @package multiform
100
 * @subpackage tests
101
 */
102
class MultiFormTest_Controller extends 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...
103
104
	public function Link() {
105
		return 'MultiFormTest_Controller';
106
	}
107
108
	public function Form($request = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
		$form = new MultiFormTest_Form($this, 'Form');
0 ignored issues
show
Documentation introduced by
$this is of type this<MultiFormTest_Controller>, but the function expects a object<instance>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
		$form->setHTMLID('MultiFormTest_Form');
111
		return $form;
112
	}
113
}
114
115
/**
116
 * @package multiform
117
 * @subpackage tests
118
 */
119
class MultiFormTest_Form extends MultiForm 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...
120
121
	public static $start_step = 'MultiFormTest_StepOne';
122
123
	public function getStartStep() {
124
		return self::$start_step;
125
	}
126
127
}
128
129
/**
130
 * @package multiform
131
 * @subpackage tests
132
 */
133
class MultiFormTest_StepOne extends MultiFormStep 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...
134
135
	public static $next_steps = 'MultiFormTest_StepTwo';
136
137
	public function getFields() {
138
		$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet';
139
		return new $class(
140
			new TextField('FirstName', 'First name'),
141
			new TextField('Surname', 'Surname'),
142
			new EmailField('Email', 'Email address')
143
		);
144
	}
145
}
146
	
147
/**
148
 * @package multiform
149
 * @subpackage tests
150
 */
151 View Code Duplication
class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly {
0 ignored issues
show
Duplication introduced by
This class 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...
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...
152
153
	public static $next_steps = 'MultiFormTest_StepThree';
154
155
	public function getFields() {
156
		$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet';
157
		return new $class(
158
			new TextareaField('Comments', 'Tell us a bit about yourself...')
159
		);
160
	}
161
}
162
	
163
/**
164
 * @package multiform
165
 * @subpackage tests
166
 */
167 View Code Duplication
class MultiFormTest_StepThree extends MultiFormStep implements TestOnly {
0 ignored issues
show
Duplication introduced by
This class 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...
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...
168
169
	public static $is_final_step = true;
170
171
	public function getFields() {
172
		$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet';
173
		return new $class(
174
			new TextField('Test', 'Anything else you\'d like to tell us?')
175
		);
176
	}
177
178
}
179