Skip to content

Commit 421a2ad

Browse files
committed
test improvements, used allowCrossOrigin()
1 parent 432407d commit 421a2ad

33 files changed

+295
-169
lines changed

tests/Forms/Container.values.ArrayHash.phpt

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,26 @@ use Tester\Assert;
1010
require __DIR__ . '/../bootstrap.php';
1111

1212

13-
$_COOKIE[Nette\Http\Helpers::StrictCookieName] = '1';
14-
$_POST = [
15-
'title' => 'sent title',
16-
'first' => [
17-
'age' => '999',
18-
'second' => [
19-
'city' => 'sent city',
13+
setUp(function () {
14+
$_SERVER['REQUEST_METHOD'] = 'POST';
15+
$_POST = [
16+
'title' => 'sent title',
17+
'first' => [
18+
'age' => '999',
19+
'second' => [
20+
'city' => 'sent city',
21+
],
2022
],
21-
],
22-
];
23+
];
24+
ob_start();
25+
Form::initialize(true);
26+
});
2327

2428

2529
function createForm(): Form
2630
{
27-
ob_start();
28-
Form::initialize(true);
29-
3031
$form = new Form;
32+
$form->allowCrossOrigin();
3133
$form->addText('title');
3234

3335
$first = $form->addContainer('first');
@@ -41,6 +43,8 @@ function createForm(): Form
4143

4244

4345
test('setting defaults using ArrayHash', function () {
46+
$_SERVER['REQUEST_METHOD'] = 'GET';
47+
4448
$form = createForm();
4549
Assert::false($form->isSubmitted());
4650

@@ -70,8 +74,6 @@ test('setting defaults using ArrayHash', function () {
7074

7175

7276
test('retrieving POST data as ArrayHash', function () {
73-
$_SERVER['REQUEST_METHOD'] = 'POST';
74-
7577
$form = createForm();
7678
Assert::truthy($form->isSubmitted());
7779
Assert::equal(ArrayHash::from([
@@ -88,8 +90,6 @@ test('retrieving POST data as ArrayHash', function () {
8890

8991

9092
test('resetting form with ArrayHash values', function () {
91-
$_SERVER['REQUEST_METHOD'] = 'POST';
92-
9393
$form = createForm();
9494
Assert::truthy($form->isSubmitted());
9595

@@ -110,8 +110,6 @@ test('resetting form with ArrayHash values', function () {
110110

111111

112112
test('updating values with ArrayHash and erase', function () {
113-
$_SERVER['REQUEST_METHOD'] = 'POST';
114-
115113
$form = createForm();
116114
Assert::truthy($form->isSubmitted());
117115

@@ -155,8 +153,6 @@ test('updating values with ArrayHash and erase', function () {
155153

156154

157155
test('onSuccess event with ArrayHash values', function () {
158-
$_SERVER['REQUEST_METHOD'] = 'POST';
159-
160156
$form = createForm();
161157
$form->onSuccess[] = function (Form $form, array $values) {
162158
Assert::same([

tests/Forms/Container.values.array.phpt

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,26 @@ use Tester\Assert;
1010
require __DIR__ . '/../bootstrap.php';
1111

1212

13-
$_COOKIE[Nette\Http\Helpers::StrictCookieName] = '1';
14-
$_POST = [
15-
'title' => 'sent title',
16-
'first' => [
17-
'age' => '999',
18-
'second' => [
19-
'city' => 'sent city',
13+
setUp(function () {
14+
$_SERVER['REQUEST_METHOD'] = 'POST';
15+
$_POST = [
16+
'title' => 'sent title',
17+
'first' => [
18+
'age' => '999',
19+
'second' => [
20+
'city' => 'sent city',
21+
],
2022
],
21-
],
22-
];
23+
];
24+
ob_start();
25+
Form::initialize(true);
26+
});
2327

2428

2529
function createForm(): Form
2630
{
27-
ob_start();
28-
Form::initialize(true);
29-
3031
$form = new Form;
32+
$form->allowCrossOrigin();
3133
$form->addText('title');
3234

3335
$first = $form->addContainer('first');
@@ -41,6 +43,8 @@ function createForm(): Form
4143

4244

4345
test('setting form defaults and retrieving array values', function () {
46+
$_SERVER['REQUEST_METHOD'] = 'GET';
47+
4448
$form = createForm();
4549
Assert::false($form->isSubmitted());
4650

@@ -70,8 +74,6 @@ test('setting form defaults and retrieving array values', function () {
7074

7175

7276
test('handles POST submission with nested data', function () {
73-
$_SERVER['REQUEST_METHOD'] = 'POST';
74-
7577
$form = createForm();
7678
Assert::truthy($form->isSubmitted());
7779
Assert::equal([
@@ -88,8 +90,6 @@ test('handles POST submission with nested data', function () {
8890

8991

9092
test('resetting form clears submitted values', function () {
91-
$_SERVER['REQUEST_METHOD'] = 'POST';
92-
9393
$form = createForm();
9494
Assert::truthy($form->isSubmitted());
9595

@@ -110,8 +110,6 @@ test('resetting form clears submitted values', function () {
110110

111111

112112
test('setting form values with erase option', function () {
113-
$_SERVER['REQUEST_METHOD'] = 'POST';
114-
115113
$form = createForm();
116114
Assert::truthy($form->isSubmitted());
117115

@@ -155,8 +153,6 @@ test('setting form values with erase option', function () {
155153

156154

157155
test('updating form values without erasing', function () {
158-
$_SERVER['REQUEST_METHOD'] = 'POST';
159-
160156
$form = createForm();
161157

162158
Assert::truthy($form->isSubmitted());
@@ -182,8 +178,6 @@ test('updating form values without erasing', function () {
182178

183179

184180
test('using array as mapped type for form values', function () {
185-
$_SERVER['REQUEST_METHOD'] = 'POST';
186-
187181
$form = createForm();
188182
$form->setMappedType('array');
189183

@@ -210,8 +204,6 @@ test('using array as mapped type for form values', function () {
210204

211205

212206
test('triggering onSuccess with correct value types', function () {
213-
$_SERVER['REQUEST_METHOD'] = 'POST';
214-
215207
$form = createForm();
216208
$form->onSuccess[] = function (Form $form, array $values) {
217209
Assert::same([
@@ -263,7 +255,6 @@ test('triggering onSuccess with correct value types', function () {
263255

264256

265257
test('validation scope limits submitted data', function () {
266-
$_SERVER['REQUEST_METHOD'] = 'POST';
267258
$_POST['send'] = '';
268259

269260
$form = createForm();
@@ -281,7 +272,6 @@ test('validation scope limits submitted data', function () {
281272

282273

283274
test('validation scope applied to container', function () {
284-
$_SERVER['REQUEST_METHOD'] = 'POST';
285275
$_POST['send'] = '';
286276

287277
$form = createForm();
@@ -298,7 +288,6 @@ test('validation scope applied to container', function () {
298288
});
299289

300290
test('validation scope on nested container fields', function () {
301-
$_SERVER['REQUEST_METHOD'] = 'POST';
302291
$_POST['send'] = '';
303292

304293
$form = createForm();

tests/Forms/Container.values.mapping.phpt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class FormSecondLevel
3434

3535

3636
setUp(function () {
37-
$_COOKIE[Nette\Http\Helpers::StrictCookieName] = '1';
3837
$_SERVER['REQUEST_METHOD'] = 'POST';
3938
$_POST = [
4039
'title' => 'sent title',
@@ -45,15 +44,15 @@ setUp(function () {
4544
],
4645
],
4746
];
47+
ob_start();
48+
Form::initialize(true);
4849
});
4950

5051

5152
function createForm(): Form
5253
{
53-
ob_start();
54-
Form::initialize(true);
55-
5654
$form = new Form;
55+
$form->allowCrossOrigin();
5756
$form->addText('title');
5857

5958
$first = $form->addContainer('first');

tests/Forms/Controls.BaseControl.phpt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ require __DIR__ . '/../bootstrap.php';
1515

1616

1717
setUp(function () {
18+
$_SERVER['REQUEST_METHOD'] = 'POST';
19+
$_POST = $_FILES = [];
1820
ob_start();
1921
Form::initialize(true);
2022
});
2123

2224

2325
test('error handling for required text input', function () {
2426
$form = new Form;
27+
$form->allowCrossOrigin();
2528
$input = $form->addText('text')
2629
->setRequired('error');
2730

@@ -42,6 +45,7 @@ test('error handling for required text input', function () {
4245

4346
test('validation methods for text input values', function () {
4447
$form = new Form;
48+
$form->allowCrossOrigin();
4549
$input = $form->addText('text');
4650
$input->setValue(123);
4751

@@ -79,6 +83,7 @@ test('validation methods for text input values', function () {
7983

8084
test('multiSelect validation and length checks', function () {
8185
$form = new Form;
86+
$form->allowCrossOrigin();
8287
$input = $form->addMultiSelect('select', null, ['a', 'b', 'c', 'd']);
8388
$input->setValue([1, 2, 3]);
8489

@@ -102,6 +107,7 @@ test('multiSelect validation and length checks', function () {
102107

103108
test('custom HTML ID for text input', function () {
104109
$form = new Form;
110+
$form->allowCrossOrigin();
105111
$input = $form->addText('text')->setHtmlId('myId');
106112

107113
Assert::same('<input type="text" name="text" id="myId">', (string) $input->getControl());
@@ -110,14 +116,18 @@ test('custom HTML ID for text input', function () {
110116

111117
test('input name conflict resolution', function () {
112118
$form = new Form;
119+
$form->allowCrossOrigin();
113120
$input = $form->addText('submit');
114121

115122
Assert::same('<input type="text" name="_submit" id="frm-submit">', (string) $input->getControl());
116123
});
117124

118125

119126
test('disabled input retains default value', function () {
127+
$_SERVER['REQUEST_METHOD'] = 'GET';
128+
120129
$form = new Form;
130+
$form->allowCrossOrigin();
121131
$form->addText('disabled')
122132
->setDisabled()
123133
->setDefaultValue('default');
@@ -129,11 +139,10 @@ test('disabled input retains default value', function () {
129139

130140

131141
test('disabled inputs ignore POST data', function () {
132-
$_SERVER['REQUEST_METHOD'] = 'POST';
133142
$_POST = ['disabled' => 'submitted value'];
134-
$_COOKIE[Nette\Http\Helpers::StrictCookieName] = '1';
135143

136144
$form = new Form;
145+
$form->allowCrossOrigin();
137146
$form->addText('disabled')
138147
->setDisabled()
139148
->setDefaultValue('default');
@@ -158,6 +167,7 @@ test('disabled inputs ignore POST data', function () {
158167

159168
test('translator integration for labels and errors', function () {
160169
$form = new Form;
170+
$form->allowCrossOrigin();
161171
$form->setTranslator(new class implements Nette\Localization\ITranslator {
162172
public function translate($s, ...$parameters): string
163173
{
@@ -192,7 +202,9 @@ test('translator integration for labels and errors', function () {
192202

193203
test('dynamic HTML name attribute handling', function () {
194204
$_POST = ['b' => '123', 'send' => ''];
205+
195206
$form = new Form;
207+
$form->allowCrossOrigin();
196208
$form->addSubmit('send', 'Send');
197209
$input = $form->addText('a');
198210

tests/Forms/Controls.Button.loadData.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ require __DIR__ . '/../bootstrap.php';
1616
setUp(function () {
1717
$_SERVER['REQUEST_METHOD'] = 'POST';
1818
$_POST = $_FILES = [];
19-
$_COOKIE[Nette\Http\Helpers::StrictCookieName] = '1';
2019
ob_start();
2120
Form::initialize(true);
2221
});
@@ -28,6 +27,7 @@ test('submit button captures POST value', function () {
2827
];
2928

3029
$form = new Form;
30+
$form->allowCrossOrigin();
3131
$input = $form->addSubmit('button');
3232
Assert::true($input->isFilled());
3333
Assert::same('x', $input->getValue());
@@ -41,11 +41,13 @@ test('submit button with empty and zero values', function () {
4141
];
4242

4343
$form = new Form;
44+
$form->allowCrossOrigin();
4445
$input = $form->addSubmit('button1');
4546
Assert::true($input->isFilled());
4647
Assert::same('', $input->getValue());
4748

4849
$form = new Form;
50+
$form->allowCrossOrigin();
4951
$input = $form->addSubmit('button2');
5052
Assert::true($input->isFilled());
5153
Assert::same('0', $input->getValue());
@@ -54,6 +56,7 @@ test('submit button with empty and zero values', function () {
5456

5557
test('unsubmitted button state', function () {
5658
$form = new Form;
59+
$form->allowCrossOrigin();
5760
$input = $form->addSubmit('button');
5861
Assert::false($input->isFilled());
5962
Assert::null($input->getValue());
@@ -66,6 +69,7 @@ test('handling malformed POST data for button', function () {
6669
];
6770

6871
$form = new Form;
72+
$form->allowCrossOrigin();
6973
$input = $form->addSubmit('malformed');
7074
Assert::false($input->isFilled());
7175
Assert::null($input->getValue());

0 commit comments

Comments
 (0)