forked from ezsystems/repository-forms
/
FieldTypeFormContext.php
204 lines (178 loc) · 6.98 KB
/
FieldTypeFormContext.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryForms\Features\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\RawMinkContext;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct;
use PHPUnit\Framework\Assert as Assertion;
final class FieldTypeFormContext extends RawMinkContext implements SnippetAcceptingContext
{
private static $fieldIdentifier = 'field';
private static $fieldTypeIdentifierMap = [
'user' => 'ezuser',
'textline' => 'ezstring',
'selection' => 'ezselection',
];
/** @var \EzSystems\RepositoryForms\Features\Context\ContentType */
private $contentTypeContext;
/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();
$this->contentTypeContext = $environment->getContext('EzSystems\RepositoryForms\Features\Context\ContentType');
}
/**
* @Given a Content Type with a(n) :fieldTypeIdentifier field definition
*/
public function aContentTypeWithAGivenFieldDefinition($fieldTypeIdentifier)
{
if (isset(self::$fieldTypeIdentifierMap[$fieldTypeIdentifier])) {
$fieldTypeIdentifier = self::$fieldTypeIdentifierMap[$fieldTypeIdentifier];
}
$contentTypeCreateStruct = $this->contentTypeContext->newContentTypeCreateStruct();
$contentTypeCreateStruct->addFieldDefinition(
new FieldDefinitionCreateStruct(
[
'identifier' => self::$fieldIdentifier,
'fieldTypeIdentifier' => $fieldTypeIdentifier,
'names' => ['eng-GB' => 'Field'],
]
)
);
$this->contentTypeContext->createContentType($contentTypeCreateStruct);
}
/**
* @When /^I view the edit form for this field$/
*/
public function iEditOrCreateContentOfThisType()
{
$this->visitPath(
sprintf(
'/content/create/nodraft/%s/eng-GB/2',
$this->contentTypeContext->getCurrentContentType()->identifier
)
);
}
/**
* @Then the edit form should contain an identifiable widget for :fieldTypeIdentifier field definition
*/
public function theEditFormShouldContainAFieldsetNamedAfterTheFieldDefinition($fieldTypeIdentifier)
{
$this->assertSession()->elementTextContains(
'css',
sprintf('form[name="ezrepoforms_%s"] label', $this->getFieldTypeSelector($fieldTypeIdentifier)),
'Field'
);
}
/**
* @Given it should contain a :type input field
*/
public function itShouldContainAGivenTypeInputField($inputType)
{
$this->assertSession()->elementExists(
'css',
sprintf(
'form[name="ezrepoforms_content_edit"] '
. 'input[name="ezrepoforms_content_edit[fieldsData][%s][value]"][type=%s]',
self::$fieldIdentifier,
$inputType
)
);
}
/**
* @Given /^it should contain the following set of labels, and input fields of the following types:$/
*/
public function itShouldContainTheFollowingSetOfLabelsAndInputFieldsTypes(TableNode $table)
{
$inputNodeElements = $this->getSession()->getPage()->findAll(
'css',
sprintf(
'form[name="ezrepoforms_user_create"] #ezrepoforms_user_create_fieldsData_%s_value input',
self::$fieldIdentifier
)
);
$actualInputFields = [];
foreach ($inputNodeElements as $inputElement) {
$type = $inputElement->getAttribute('type');
$inputId = $inputElement->getAttribute('id');
$label = $this->getSession()->getPage()->find('css', sprintf('label[for=%s]', $inputId))->getText();
$actualInputFields[] = ['type' => $type, 'label' => $label];
}
foreach ($expectedInputFields = $table->getColumnsHash() as $inputField) {
Assertion::assertContains($inputField, $actualInputFields);
}
}
/**
* @Given /^the field definition is required$/
* @Given /^the field definition is marked as required$/
*/
public function theFieldDefinitionIsMarkedAsRequired()
{
$this->contentTypeContext->updateFieldDefinition(
self::$fieldIdentifier,
new FieldDefinitionUpdateStruct(['isRequired' => true])
);
}
/**
* @Then the value input fields for :fieldIdentifier field should be flagged as required
*/
public function theInputFieldsShouldBeFlaggedAsRequired($fieldTypeIdentifier)
{
$inputNodeElements = $this->getSession()->getPage()->findAll(
'css',
sprintf(
'form[name="ezrepoforms_%1$s"] #ezrepoforms_%1$s_fieldsData_%2$s input',
$this->getFieldTypeSelector($fieldTypeIdentifier),
self::$fieldIdentifier
)
);
Assertion::assertNotEmpty($inputNodeElements, 'The input field is not marked as required');
$exceptions = $this->getRequiredFieldTypeExceptions($fieldTypeIdentifier);
foreach ($inputNodeElements as $inputNodeElement) {
$inputId = $inputNodeElement->getAttribute('id');
$label = $this->getSession()->getPage()->find('css', sprintf('label[for=%s]', $inputId))->getText();
$expectedState = array_key_exists($label, $exceptions) ? $exceptions[$label] : true;
Assertion::assertEquals(
$expectedState,
$inputNodeElement->hasAttribute('required'),
sprintf(
'%s input with id %s is not flagged as required',
$inputNodeElement->getAttribute('type'),
$inputNodeElement->getAttribute('id')
)
);
}
}
/**
* Set a field definition option $option to $value.
*
* @param $option string The field definition option
* @param $value mixed The option value
*/
public function setFieldDefinitionOption($option, $value)
{
$this->contentTypeContext->updateFieldDefinition(
self::$fieldIdentifier,
new FieldDefinitionUpdateStruct(['fieldSettings' => [$option => $value]])
);
}
private function getFieldTypeSelector(string $fieldTypeIdentifier): string
{
if ($fieldTypeIdentifier === 'user') {
return 'user_create';
}
return 'content_edit';
}
private function getRequiredFieldTypeExceptions(string $fieldTypeIdentifier): array
{
if ($fieldTypeIdentifier === 'user') {
return ['Enabled' => false];
}
return [];
}
}