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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
require_once 'vendor/autoload.php';
require_once dirname(__FILE__).'/../../include/CWebTest.php';
require_once dirname(__FILE__).'/../behaviors/CMessageBehavior.php';
/**
* Base class for "Hosts and Problems filter save" function tests.
*/
class testFormFilter extends CWebTest {
// URL to page with filters: Hosts or Problems.
public $url;
/**
* Attach MessageBehavior to the test.
*
* @return array
*/
public function getBehaviors() {
return [CMessageBehavior::class];
}
/**
* Check created filter.
*
* @param array $data given data provider
*/
public function checkFilters($data) {
$this->page->waitUntilReady();
$filter_container = $this->query('xpath://ul[@class="ui-sortable-container ui-sortable"]')->asFilterTab()->one();
switch ($data['expected']) {
case TEST_GOOD:
$table = $this->query('class:list-table')->asTable()->waitUntilReady()->one();
$rows = $table->getRows();
$filtered_rows_count = ($rows->count() === 1 && $rows->asText() === ['No data found.'])
? 0
: $rows->count();
// Checking that data exists after saving filter.
if (array_key_exists('filter_form', $data)) {
$form = $this->query('id:tabfilter_'.$data['tab_id'])->asForm()->one();
$form->checkValue($data['filter_form']);
}
// Filter default name is Untitled.
if (!array_key_exists('Name', $data['filter'])) {
$data['filter']['Name'] = 'Untitled';
}
$this->checkName($data['filter']['Name']);
// Checking that hosts/problems amount displayed near name in filter tab.
if (array_key_exists('Show number of records', $data['filter'])) {
$this->query('xpath://a[@class="icon-home tabfilter-item-link"]')->one()->click();
$this->assertEquals($filtered_rows_count, $this->query('xpath://li[@data-target="tabfilter_'.
$data['tab_id'].'"]/a')->one()->getAttribute('data-counter'));
}
// Checking that dropdown/popup tab works.
$dropdown = $this->query('class:btn-widget-expand')->asPopupButton()->waitUntilClickable()->one();
$dropdown->fill($data['filter']['Name']);
$this->assertEquals($data['filter']['Name'], $filter_container->getSelectedTabName());
break;
case TEST_BAD:
$this->assertMessage(TEST_BAD, null, $data['error_message']);
$this->page->refresh()->waitUntilReady();
$this->assertEquals($this->query('xpath://li/ul[@class="ui-sortable-container ui-sortable"]/li')->count(), 1);
break;
}
}
/**
* Change data in filter form.
*
* @param string $user test user with saved filters
*/
public function updateFilterForm($user) {
$this->page->userLogin($user, 'zabbix');
$this->page->open($this->url)->waitUntilReady();
// Changing filter data.
$filter_container = $this->query('xpath://ul[@class="ui-sortable-container ui-sortable"]')->asFilterTab()->one();
$filter_container->selectTab('update_tab');
$form = $this->query('id:tabfilter_1')->asForm()->waitUntilReady()->one();
$result_before = $this->getTableResults();
for ($i = 0; $i < 2; ++$i) {
$form->fill(['Host groups' => ['Zabbix servers']]);
if ($i === 0) {
$this->query('name:filter_apply')->one()->click();
$this->assertFalse($result_before === $this->getTableResults());
}
$this->query('xpath://li[@data-target="tabfilter_0"]/a')->one()->click();
$this->page->waitUntilReady();
$this->assertEquals('italic', $this->query('xpath://li[@data-target="tabfilter_1"]/a[@class="tabfilter-item-link"]')
->one()->getCSSValue('font-style'));
$filter_container->selectTab('update_tab');
if ($i === 0) {
$this->query('button:Reset')->one()->click();
}
else {
$this->assertTrue($result_before === $this->getTableResults());
$this->query('button:Update')->one()->click();
}
}
// This time needed for filter to update table with results.
sleep(1);
// Getting changed host/problem result and then comparing it with displayed result from dropdown.
$result = $this->getTableResults();
$this->query('xpath://li[@data-target="tabfilter_0"]/a')->one()->click();
$this->query('xpath://button[@data-action="toggleTabsList"]')->one()->click();
$this->page->waitUntilReady();
$this->assertEquals($result, $this->query('xpath://a[@aria-label="update_tab"]')
->one()->getAttribute('data-counter'));
// Checking that hosts/problems amount in filter displayed near name at the tab changed.
$this->assertEquals($result, $this->query('xpath://li[@data-target="tabfilter_1"]/a')
->one()->getAttribute('data-counter'));
}
/**
* Update filter properties.
*
* @param string $user test user with saved filters
*/
public function updateFilterProperties($user) {
$this->page->userLogin($user, 'zabbix');
$this->page->open($this->url)->waitUntilReady();
$filter_container = $this->query('xpath://ul[@class="ui-sortable-container ui-sortable"]')->asFilterTab()->one();
// Checking that filter result amount displayed.
$this->assertTrue($this->query('xpath://li[@data-target="tabfilter_1"]/a[@data-counter]')->exists());
$filter_container->selectTab('update_tab');
// Changing filter name to empty space.
$filter_container->editProperties();
$this->page->waitUntilReady();
$dialog = COverlayDialogElement::find()->asForm()->all()->last()->waitUntilReady();
$dialog->fill(['Name' => '']);
$dialog->submit();
$this->assertMessage(TEST_BAD, null, 'Incorrect value for field "filter_name": cannot be empty.');
// Changing filter name and disabling Show number of records.
$dialog->fill(['Name' => 'updated_filter_name', 'Show number of records' => false]);
$dialog->submit();
$this->page->waitUntilReady();
// Checking that filter name changed, and result amount not displayed.
$this->checkName('updated_filter_name');
$this->query('xpath://li[@data-target="tabfilter_0"]/a')->one()->click();
$this->assertFalse($this->query('xpath://li[@data-target="tabfilter_1"]/a[@data-counter]')->exists());
}
/**
* Delete existing filters.
*
* @param string $user test user with saved filters
*/
public function deleteFilter($user) {
$this->page->userLogin($user, 'zabbix');
$this->page->open($this->url)->waitUntilReady();
$filter_container = $this->query('xpath://ul[@class="ui-sortable-container ui-sortable"]')->asFilterTab()->one();
$filters = $filter_container->getTitles();
foreach ($filters as $filter) {
$filter_container->editProperties($filter);
$dialog = COverlayDialogElement::find()->all()->last()->waitUntilReady();
$dialog->query('button:Delete')->one()->click();
$this->page->acceptAlert();
$this->page->waitUntilReady();
array_shift($filters);
// Checking that deleted filter doesn't exists in filters tab list.
if ($filters !== []) {
$this->assertEquals($filters, $filter_container->getTitles());
}
else {
$this->assertEquals(null, $filter_container->getTitles());
}
// Checking that deleted filter doesn't exists in filters dropdown list.
$this->assertEquals($filters, $this->getDropdownFilterNames());
}
}
/**
* Create filter.
*
* @param array $data given data provider
* @param string $user test user with saved filters
*/
public function createFilter($data, $user = null) {
$this->page->userLogin($user, 'zabbix');
$this->page->open($this->url)->waitUntilReady();
// Checking if home tab is selected.
$xpath = 'xpath://li[@data-target="tabfilter_0"]';
if ($this->query($xpath)->one()->getAttribute('class') === 'tabfilter-item-label') {
$this->query($xpath.'/a')->waitUntilClickable()->one()->click();
}
$this->page->waitUntilReady();
if (array_key_exists('filter_form', $data)) {
$home_form = $this->query('xpath://div[@id="tabfilter_0"]/form')->asForm()->one();
$home_form->fill($data['filter_form']);
}
$this->query('button:Save as')->one()->click();
$dialog = COverlayDialogElement::find()->asForm()->all()->last()->waitUntilReady();
$dialog->fill($data['filter']);
$dialog->submit();
$this->page->waitUntilReady();
}
/**
* Return result amount from table.
*/
public function getTableResults() {
$table = $this->query('class:list-table')->asTable()->waitUntilReady()->one();
$text = $this->query('xpath://table[@class="list-table"]/tbody/tr/td')->one()->getText();
$result = ($text === 'No data found.') ? 0 : $table->getRows()->count();
return $result;
}
/**
* Return filter names from droplist.
*/
public function getDropdownFilterNames() {
$this->query('xpath://button[@data-action="toggleTabsList"]')->one()->click();
$dropdown_filters = CPopupMenuElement::find()->waitUntilVisible()->one()->getItems()->asText();
array_shift($dropdown_filters);
return $dropdown_filters;
}
/**
* Checking filters name in 3 different places: tab list, droplist, options.
*
* @param string $filter_name filter name, that need to be checked in properties, droplist and tab list
*/
public function checkName($filter_name) {
$filter_container = $this->query('xpath://ul[@class="ui-sortable-container ui-sortable"]')->asFilterTab()->one();
// Checking that name of filter displayed on the tab.
$this->assertEquals($filter_name, $filter_container->getSelectedTabName());
// Checking that names displayed on the filter tabs same as in drop down list.
$filters = $filter_container->getTitles();
$this->assertEquals($filters, $this->getDropdownFilterNames());
// Checking that name displayed in filter properties.
$filter_container->editProperties();
$dialog = COverlayDialogElement::find()->asForm()->all()->last()->waitUntilReady();
$dialog->checkValue(['Name' => $filter_name]);
$this->query('button:Cancel')->one()->click();
COverlayDialogElement::ensureNotPresent();
}
}
|