|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ONGR\SettingsBundle\Tests\Functional\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase; |
|
15
|
|
|
use ONGR\SettingsBundle\Tests\Fixtures\Security\LoginTestHelper; |
|
16
|
|
|
use ONGR\SettingsBundle\Tests\Functional\PreparePersonalData; |
|
17
|
|
|
use ONGR\SettingsBundle\Tests\Functional\PrepareAdminData; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Tests for SettingsController. |
|
22
|
|
|
*/ |
|
23
|
|
|
class PersonalSettingsControllerTest extends AbstractElasticsearchTestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Client. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function setUp() |
|
34
|
|
|
{ |
|
35
|
|
|
parent::setUp(); |
|
36
|
|
|
$this->client = new LoginTestHelper(static::createClient()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Test settings page ability to set values to cookie. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testSettingsAction() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->getManager(); |
|
45
|
|
|
|
|
46
|
|
|
/** @var Client $client */ |
|
47
|
|
|
$client = $this->client->loginAction('test', 'test'); |
|
48
|
|
|
|
|
49
|
|
|
// Visit settings page. |
|
50
|
|
|
$crawler = $client->request('GET', '/settings/settings'); |
|
51
|
|
|
|
|
52
|
|
|
// Assert categories are rendered. |
|
53
|
|
|
/** @var array $categories */ |
|
54
|
|
|
$categories = $client->getContainer()->getParameter('ongr_settings.settings.categories'); |
|
55
|
|
|
$content = $client->getResponse()->getContent(); |
|
56
|
|
|
|
|
57
|
|
|
// Print $content. |
|
58
|
|
|
foreach ($categories as $category) { |
|
59
|
|
|
$this->assertContains($category['name'], $content); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Submit settings form. |
|
63
|
|
|
$buttonNode = $crawler->selectButton('settings_submit'); |
|
64
|
|
|
$form = $buttonNode->form(); |
|
65
|
|
|
|
|
66
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
67
|
|
|
$form['settings[foo_setting_1]']->tick(); |
|
68
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
69
|
|
|
$form['settings[foo_setting_2]']->untick(); |
|
70
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
71
|
|
|
$form['settings[foo_setting_3]']->tick(); |
|
72
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
73
|
|
|
$form['settings[ongr_settings_profile_Acme2]']->tick(); |
|
74
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
75
|
|
|
$form['settings[ongr_settings_profile_Acme1]']->tick(); |
|
76
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
77
|
|
|
$form['settings[ongr_settings_live_settings]']->untick(); |
|
78
|
|
|
$client->submit($form); |
|
79
|
|
|
|
|
80
|
|
|
// Assert successful redirect. |
|
81
|
|
|
$this->assertStringEndsWith( |
|
82
|
|
|
'settings', |
|
83
|
|
|
$client->getRequest()->getUri(), |
|
84
|
|
|
'response must be a correct redirect' |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
// Assert cookie values updated. |
|
88
|
|
|
$cookieValue = $client |
|
89
|
|
|
->getCookieJar() |
|
90
|
|
|
->get($client->getContainer()->getParameter('ongr_settings.settings.settings_cookie.name')) |
|
91
|
|
|
->getValue(); |
|
92
|
|
|
|
|
93
|
|
|
$expectedValue = [ |
|
94
|
|
|
'foo_setting_1' => true, |
|
95
|
|
|
'foo_setting_2' => false, |
|
96
|
|
|
'foo_setting_3' => true, |
|
97
|
|
|
'ongr_settings_profile_Acme2' => true, |
|
98
|
|
|
'ongr_settings_profile_Acme1' => true, |
|
99
|
|
|
'ongr_settings_live_settings' => false, |
|
100
|
|
|
]; |
|
101
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expectedValue), $cookieValue); |
|
102
|
|
|
|
|
103
|
|
|
// Try to change value through change setting action. |
|
104
|
|
|
$data[0] = ['foo_setting_1', false]; |
|
|
|
|
|
|
105
|
|
|
$data[1] = ['foo_setting_non_existent', false ]; |
|
106
|
|
|
|
|
107
|
|
|
foreach ($data as $case) { |
|
108
|
|
|
$client->request('get', '/admin/setting/change/' . base64_encode($case[0])); |
|
109
|
|
|
|
|
110
|
|
|
// Assert cookie values updated. |
|
111
|
|
|
$cookieValue = $client |
|
112
|
|
|
->getCookieJar() |
|
113
|
|
|
->get($client->getContainer()->getParameter('ongr_settings.settings.settings_cookie.name')) |
|
114
|
|
|
->getValue(); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expectedValue), $cookieValue); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Settings pages should not be allowed to access non-authorized users, redirect should be initiated. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testActionsWhenNotLoggedInNoRedirect() |
|
124
|
|
|
{ |
|
125
|
|
|
$client = $this->client->getClient(); |
|
126
|
|
|
$client->followRedirects(false); |
|
127
|
|
|
|
|
128
|
|
|
// Visit settings page. |
|
129
|
|
|
$client->request('GET', '/settings/settings'); |
|
130
|
|
|
|
|
131
|
|
|
// Assert access is redirected. |
|
132
|
|
|
$this->assertSame(302, $client->getResponse()->getStatusCode()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Settings pages should not be allowed to access non-authorized users, user should be redirected this time. |
|
137
|
|
|
*/ |
|
138
|
|
|
public function testActionsWhenNotLoggedInRedirectToLogin() |
|
139
|
|
|
{ |
|
140
|
|
|
$client = $this->client->getClient(); |
|
141
|
|
|
$client->followRedirects(true); |
|
142
|
|
|
$client->request('GET', '/settings/logout'); |
|
143
|
|
|
|
|
144
|
|
|
// Visit settings page. |
|
145
|
|
|
$client->request('GET', '/settings/settings'); |
|
146
|
|
|
|
|
147
|
|
|
// Assert successful redirect when not logged in. |
|
148
|
|
|
$this->assertStringEndsWith( |
|
149
|
|
|
'login', |
|
150
|
|
|
$client->getRequest()->getUri(), |
|
151
|
|
|
'response must be a correct redirect' |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getDataArray() |
|
159
|
|
|
{ |
|
160
|
|
|
return [ |
|
161
|
|
|
'default' => [ |
|
162
|
|
|
'setting' => [ |
|
163
|
|
|
[ |
|
164
|
|
|
'name' => 'Acme1', |
|
165
|
|
|
'profile' => 'Acme1', |
|
166
|
|
|
'description' => 'Acme1', |
|
167
|
|
|
'type' => 'Acme1', |
|
168
|
|
|
'data' => 'Acme1', |
|
169
|
|
|
], |
|
170
|
|
|
[ |
|
171
|
|
|
'name' => 'Acme2', |
|
172
|
|
|
'profile' => 'Acme2', |
|
173
|
|
|
'description' => 'Acme2', |
|
174
|
|
|
'type' => 'Acme2', |
|
175
|
|
|
'data' => 'Acme2', |
|
176
|
|
|
], |
|
177
|
|
|
], |
|
178
|
|
|
], |
|
179
|
|
|
]; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.