Completed
Push — master ( 3f773c...840f27 )
by Sam
15:02 queued 13s
created

FormFactoryTest::testVersionedForm()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
use SilverStripe\Control\Controller;
4
use SilverStripe\Core\Convert;
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\Forms\DefaultFormFactory;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\HiddenField;
12
use SilverStripe\Forms\LiteralField;
13
use SilverStripe\Forms\TextField;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\ORM\Versioning\Versioned;
16
17
class FormFactoryTest extends SapphireTest
18
{
19
	protected $extraDataObjects = [
20
		FormFactoryTest_TestObject::class,
21
	];
22
23
	protected static $fixture_file = 'FormFactoryTest.yml';
24
25
	/**
26
	 * Test versioned form
27
	 */
28
	public function testVersionedForm() {
29
		$controller = new FormFactoryTest_TestController();
30
		$form = $controller->Form();
31
32
		// Check formfields
33
		$this->assertInstanceOf(TextField::class, $form->Fields()->fieldByName('Title'));
34
		$this->assertInstanceOf(HiddenField::class, $form->Fields()->fieldByName('ID'));
35
		$this->assertInstanceOf(HiddenField::class, $form->Fields()->fieldByName('SecurityID'));
36
37
38
		// Check preview link
39
		/** @var LiteralField $previewLink */
40
		$previewLink = $form->Fields()->fieldByName('PreviewLink');
41
		$this->assertInstanceOf(LiteralField::class, $previewLink);
42
		$this->assertEquals(
43
			'<a href="FormFactoryTest_TestController/preview/" rel="external" target="_blank">Preview</a>',
44
			$previewLink->getContent()
45
		);
46
47
		// Check actions
48
		$this->assertInstanceOf(FormAction::class, $form->Actions()->fieldByName('action_save'));
49
		$this->assertInstanceOf(FormAction::class, $form->Actions()->fieldByName('action_publish'));
50
		$this->assertTrue($controller->hasAction('publish'));
51
	}
52
53
	/**
54
	 * Removing versioning from an object should result in a simpler form
55
	 */
56
	public function testBasicForm() {
57
		FormFactoryTest_TestObject::remove_extension(Versioned::class);
58
		$controller = new FormFactoryTest_TestController();
59
		$form = $controller->Form();
60
61
		// Check formfields
62
		$this->assertInstanceOf(TextField::class, $form->Fields()->fieldByName('Title'));
63
		$this->assertNull($form->Fields()->fieldByName('PreviewLink'));
64
65
		// Check actions
66
		$this->assertInstanceOf(FormAction::class, $form->Actions()->fieldByName('action_save'));
67
		$this->assertNull($form->Actions()->fieldByName('action_publish'));
68
	}
69
}
70
71
/**
72
 * @mixin Versioned
73
 */
74
class FormFactoryTest_TestObject extends DataObject {
75
	private static $db = [
76
		'Title' => 'Varchar',
77
	];
78
79
	private static $extensions = [
80
		Versioned::class,
81
	];
82
}
83
84
/**
85
 * Edit controller for this form
86
 */
87
class FormFactoryTest_TestController extends Controller {
88
	private static $extensions = [
89
		FormFactoryTest_ControllerExtension::class,
90
	];
91
92
	public function Link($action = null) {
93
		return Controller::join_links('FormFactoryTest_TestController', $action, '/');
94
	}
95
96
	public function Form() {
97
		// Simple example; Just get the first draft record
98
		$record = $this->getRecord();
99
		$factory = new FormFactoryTest_EditFactory();
100
		return $factory->getForm($this, 'Form', ['Record' => $record]);
101
	}
102
103
	public function save($data, Form $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $data 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...
Unused Code introduced by
The parameter $form 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...
104
		// Noop
105
	}
106
107
	/**
108
	 * @return DataObject
109
	 */
110
	protected function getRecord()
111
	{
112
		return Versioned::get_by_stage(FormFactoryTest_TestObject::class, Versioned::DRAFT)->first();
113
	}
114
}
115
116
/**
117
 * Provides versionable extensions to a controller / scaffolder
118
 */
119
class FormFactoryTest_ControllerExtension extends Extension {
120
121
	/**
122
	 * Handlers for extra actions added by this extension
123
	 *
124
	 * @var array
125
	 */
126
	private static $allowed_actions = [
127
		'publish',
128
		'preview',
129
	];
130
131
	/**
132
	 * Adds additional form actions
133
	 *
134
	 * @param FieldList $actions
135
	 * @param Controller $controller
136
	 * @param string $name
137
	 * @param array $context
138
	 */
139
	public function updateFormActions(FieldList &$actions, Controller $controller, $name, $context = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $controller 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...
Unused Code introduced by
The parameter $name 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...
140
		// Add publish button if record is versioned
141
		if (empty($context['Record'])) {
142
			return;
143
		}
144
		$record = $context['Record'];
145
		if ($record->hasExtension(Versioned::class)) {
146
			$actions->push(new FormAction('publish', 'Publish'));
147
		}
148
	}
149
150
	/**
151
	 * Adds extra fields to this form
152
	 *
153
	 * @param FieldList $fields
154
	 * @param Controller $controller
155
	 * @param string $name
156
	 * @param array $context
157
	 */
158
	public function updateFormFields(FieldList &$fields, Controller $controller, $name, $context = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $name 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...
159
		// Add preview link
160
		if (empty($context['Record'])) {
161
			return;
162
		}
163
		$record = $context['Record'];
164
		if ($record->hasExtension(Versioned::class)) {
165
			$link = $controller->Link('preview');
166
			$fields->unshift(new LiteralField(
167
				"PreviewLink",
168
				sprintf('<a href="%s" rel="external" target="_blank">Preview</a>', Convert::raw2att($link))
169
			));
170
		}
171
	}
172
173
	public function publish($data, $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $data 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...
Unused Code introduced by
The parameter $form 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...
174
		// noop
175
	}
176
177
	public function preview() {
178
		// noop
179
	}
180
}
181
182
/**
183
 * Test factory
184
 */
185
class FormFactoryTest_EditFactory extends DefaultFormFactory  {
186
187
	private static $extensions = [
188
		FormFactoryTest_ControllerExtension::class
189
	];
190
191
	protected function getFormFields(Controller $controller, $name, $context = [])
192
	{
193
		$fields = new FieldList(
194
			new HiddenField('ID'),
195
			new TextField('Title')
196
		);
197
		$this->invokeWithExtensions('updateFormFields', $fields, $controller, $name, $context);
198
		return $fields;
199
	}
200
201
	protected function getFormActions(Controller $controller, $name, $context = [])
202
	{
203
		$actions = new FieldList(
204
			new FormAction('save', 'Save')
205
		);
206
		$this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
207
		return $actions;
208
	}
209
}
210