Welcome to mirror list, hosted at ThFree Co, Russian Federation.

testPageServicesServices.php « services « selenium « tests « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3032ae8a1cd259e5dbea6f18f49dc7fbca9472b (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
<?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 dirname(__FILE__).'/../../include/CWebTest.php';
require_once dirname(__FILE__).'/../traits/TableTrait.php';
require_once dirname(__FILE__).'/../behaviors/CMessageBehavior.php';

/**
 * @backup services
 *
 * @dataSource Services
 */
class testPageServicesServices extends CWebTest {

	use TableTrait;

	const EDIT = true;

	const SERVICE_COUNT = 16;

	const LAYOUT_PARENT = 'Parent for 2 levels of child services';
	const LAYOUT_CHILD = 'Child service with child service';
	const LAYOUT_CHILD2 = 'Child service of child service';

	const ROOTCAUSE_PARENT = 'Test order';
	const ROOTCAUSE_CHILD1 = '1';
	const ROOTCAUSE_CHILD2 = '2';
	const ROOTCAUSE_CHILD3 = '3';

	const BREADCRUMB_SELECTOR = 'xpath://ul[@class="breadcrumbs"]';
	const TABLE_SELECTOR = 'id:service-list';

	/**
	 * Attach MessageBehavior to the test.
	 *
	 * @return array
	 */
	public function getBehaviors() {
		return [CMessageBehavior::class];
	}


	/**
	 * Set parent and child services to Problem status and link the child services to the corresponding problem events.
	 */
	public static function prepareServiceProblemsData() {
		// Statuses: Warning = 2, Average = 3, High = 4.
		$service_statuses = [
			self::ROOTCAUSE_PARENT => 4,
			self::ROOTCAUSE_CHILD1 => 2,
			self::ROOTCAUSE_CHILD2 => 3,
			self::ROOTCAUSE_CHILD3 => 4
		];

		$service_ids = [];
		$i = 1;

		foreach ($service_statuses as $service => $status) {
			$service_ids[$service] = CDBHelper::getValue('SELECT serviceid FROM services WHERE name='.zbx_dbstr($service));

			// Change service status to Problem with the corresponding severity.
			DBexecute('UPDATE services SET status='.zbx_dbstr($status).' WHERE serviceid='.zbx_dbstr($service_ids[$service]));

			// Link child services to the corresponding problem events.
			if ($service !== self::ROOTCAUSE_PARENT) {
				// Corresponding trigger problem events have ids starting from 9001 in data_test.sql, so "9000 + $i" is used.
				DBexecute('INSERT into service_problem (service_problemid, eventid, serviceid, severity) '.
						'VALUES ('.$i.', '.(9000 + $i).', '.$service_ids[$service].', '.$status.')'
				);

				$i++;
			}
		}
	}

	public function testPageServicesServices_LayoutView() {
		$this->page->login()->open('zabbix.php?action=service.list');
		$this->page->assertTitle('Services');
		$this->page->assertHeader('Services');

		// Labels on columns at services list.
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();
		$this->assertSame(['Name', 'Status', 'Root cause', 'Created at', 'Tags'], $table->getHeadersText());
		$this->assertTableStats(self::SERVICE_COUNT);

		// Check that service buttons are not present in the table row.
		// Row count starts with 0, so here real service count - 1.
		$this->checkServiceButtons($table->getRow(rand(0, self::SERVICE_COUNT - 1)), false);

		// Check that "Edit elements" are not present in View mode.
		$elements = [
			'button:Create service',
			'id:filter_without_children',       // "Only services without children" checkbox in Filter.
			'id:filter_without_problem_tags',   // "Only services without problem tags" checkbox in Filter.
			'id:filter_tag_source_1',           // "Tag source" radiobutton in Filter.
			'id:all_services',                  // "Select all" checkbox in table.
			'button:Mass update',
			'button:Delete'
		];
		foreach ($elements as $element) {
			$this->assertFalse($table->query($element)->exists());
		}

		// Check parent-child service layout.
		$this->checkParentChildLayout($table, self::LAYOUT_PARENT, self::LAYOUT_CHILD);

		// Check Kiosk mode.
		$this->query('xpath://button[@title="Kiosk mode"]')->one()->click();
		$this->page->waitUntilReady();

		// Check that Header and Filter disappeared.
		$this->query('xpath://h1[@id="page-title-general"]')->waitUntilNotVisible();
		$this->assertFalse($this->query('xpath://div[@aria-label="Filter"]')->exists());
		$this->assertTrue($this->query('id:service-list')->exists());

		$this->query('xpath://button[@title="Normal view"]')->waitUntilPresent()->one()->click(true);
		$this->page->waitUntilReady();

		// Check that Header and Filter are visible again.
		$this->query('xpath://h1[@id="page-title-general"]')->waitUntilVisible();
		$this->assertTrue($this->query('xpath://div[@aria-label="Filter"]')->exists());
		$this->assertTrue($this->query('id:service-list')->exists());
	}

	public function testPageServicesServices_LayoutEdit() {
		$this->page->login()->open('zabbix.php?action=service.list');
		$this->query('id:list_mode')->asSegmentedRadio()->one()->waitUntilClickable()->select('Edit');
		$this->page->waitUntilReady();

		$this->page->assertTitle('Services');
		$this->page->assertHeader('Services');

		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();
		$this->assertSame(['', 'Name', 'Status', 'Root cause', 'Created at', 'Tags', ''], $table->getHeadersText());
		$this->assertTableStats(self::SERVICE_COUNT);

		// Check that action buttons below the table are not enabled .
		$this->checkActionButtons(false);

		// Row count starts with 0, so here real service count - 1.
		$this->checkServiceButtons($table->getRow(rand(0, self::SERVICE_COUNT - 1)), true);
		$this->checkParentChildLayout($table, self::LAYOUT_PARENT, self::LAYOUT_CHILD, self::EDIT);
		$this->checkServiceButtons($table->findRow('Name', self::LAYOUT_CHILD2, true), true);

		// Check there is no Kiosk mode button.
		$this->assertFalse($this->query('xpath://button[@title="Kiosk mode"]')->exists());

		// Check Info/Filter tabs switching.
		foreach (['Filter' => 'tab_1', 'Info' => 'tab_info'] as $tab => $id) {
			$this->query("xpath://li[@role='tab']//a[text()=".CXpathHelper::escapeQuotes($tab)."]")
					->waitUntilClickable()->one()->click();
			$this->assertTrue($this->query('id', $id)->waitUntilReady()->one()->isVisible());
		}
	}

	/**
	 * Function for checking layout of Parent-Child chains layout.
	 *
	 * @param CTableElement    $table    table where to select particular service
	 * @param string           $parent   name of parent service
	 * @param string           $child    name of child service
	 * @param boolean          $edit     true if it is edit scenario, false otherwise
	 */
	private function checkParentChildLayout($table, $parent, $child, $edit = false) {
		$this->checkSeviceInfoLayout($table, $parent, $edit);
		$this->assertTableDataColumn([$child.' 1'], 'Name', 'xpath://form[@name="service_list"]//table');
		$this->checkSeviceInfoLayout($table, $child, $edit);
		$this->assertTableDataColumn([self::LAYOUT_CHILD2], 'Name', 'xpath://form[@name="service_list"]//table');

		$breadcrumbs = $this->query(self::BREADCRUMB_SELECTOR)->one();
		// Check breadcrumbs on last child page.
		foreach (['All services', $parent, $child] as $breadcrumb) {
			$this->assertTrue($breadcrumbs->query('link', $breadcrumb)->one()->isClickable());
		}

		// Check selected breadcrumb.
		$this->assertTrue($breadcrumbs->query("xpath:.//span[@class='selected']//a[text()=".
				CXPathHelper::escapeQuotes($child)."]")->one()->isValid()
		);
	}

	/**
	 * Function for checking layout of Service info card.
	 *
	 * @param CTableElement    $table    table where to select particular service
	 * @param string           $service  name of service to be checked
	 * @param boolean          $edit     true if it is edit scenario, false otherwise
	 */
	private function checkSeviceInfoLayout($table, $service, $edit = false) {
		$table->findRow('Name', $service, true)->query('link', $service)->waitUntilClickable()->one()->click();
		$this->page->waitUntilReady();

		foreach (['Info', 'Filter'] as $tab) {
			$this->assertTrue($this->query("xpath://li[@role='tab']//a[text()=".CXPathHelper::escapeQuotes($tab).
					"]")->one()->isClickable());
		}

		$info_card = $this->query('id:tab_info')->waitUntilReady()->one();
		foreach ([$service, 'Parent services', 'Status', 'Tags'] as $text) {
			$this->assertTrue($info_card->query("xpath:.//div[@class='service-info-grid']//div[text()=".
					CXPathHelper::escapeQuotes($text)."]")->one()->isVisible()
			);
		}

		$edit_button = $info_card->query('xpath://button['.CXPathHelper::fromClass('btn-edit').']');
		$this->assertEquals($edit, $edit_button->one(false)->isClickable());
		$table->invalidate();
		$this->assertTableStats(1);
	}

	/**
	 * Function for checking service action buttons in a table row.
	 *
	 * @param CTableRowElement    $row       row where buttons to be found
	 * @param boolean             $exists    true if buttons should exist, false otherwise
	 */
	private function checkServiceButtons($row, $exists) {
		foreach (['Add child service', 'Edit', 'Delete'] as $button) {
			// Here checking only visible, not clickable, because in some cases button can be disabled.
			$this->assertEquals($exists, $row->query("xpath:.//button[@title=".CXPathHelper::escapeQuotes($button).
					"]")->one(false)->isVisible()
			);
		}
	}

	/**
	 * Function for checking service action buttons in a table row.
	 *
	 * @param boolean    $enabled    true if buttons should be enabled, false otherwise
	 */
	private function checkActionButtons($enabled) {
		foreach (['Mass update', 'Delete'] as $button) {
			$this->assertTrue($this->query('button', $button)->one()->isEnabled($enabled));
		}
	}

	public static function getFilterEditData() {
		return [
			// Tags source: Service.
			[
				[
					'Tags' => [
						'Source' => 'Service',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Contains',
								'value' => 'true'
							]
						]
					],
					'result' => [
						'Service with multiple service tags'
					]
				]
			],
			// Tag problem Equals false.
			[
				[
					'Tags' => [
						'Source' => 'Service',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Equals',
								'value' => 'false'
							]
						]
					],
					'result' => [
						'Simple actions service'
					]
				]
			],
			// Tags source: Service and Evaluation: Or.
			[
				[
					'Tags' => [
						'Source' => 'Service',
						'Evaluation' => 'Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Equals',
								'value' => 'false'
							],
							[
								'tag' => 'test',
								'operator' => 'Exists'
							]
						]
					],
					'result' => [
						'Parent for 2 levels of child services 1',
						'Service with multiple service tags',
						'Simple actions service'
					]
				]
			],
			// Only services without children checkbox.
			[
				[
					'filter' => [
						'Name' => 'parent',
						'Only services without children' => true
					],
					'result' => [
						'Parent for child creation'
					]
				]
			],
			// Only services without problem tags checkbox.
			[
				[
					'filter' => [
						'Name' => 'parent',
						'Only services without problem tags' => true
					],
					'result' => [
						'Parent for 2 levels of child services 1',
						'Parent for deletion from row 1',
						'Parent for child deletion from row 1',
						'Clone parent 3',
						'Parent for child creation'
					]
				]
			]
		];
	}

	public static function getFilterCommonData() {
		return [
			// Only Name filtering.
			[
				[
					'filter' => [
						'Name' => 'parent'
					],
					'result' => [
						'Parent for 2 levels of child services 1',
						'Parent for deletion from row 1',
						'Parent for child deletion from row 1',
						'Clone parent 3',
						'Parent for child creation'
					],
					'check_breadcrumbs' => true
				]
			],
			// Name and Status: Any.
			[
				[
					'filter' => [
						'Name' => 'with problem',
						'Status' => 'Any'
					],
					'result' => [
						'Service with problem tags',
						'Service with problem'
					]
				]
			],
			// Name and Status: OK.
			[
				[
					'filter' => [
						'Name' => 'with problem',
						'Status' => 'OK'
					],
					'result' => [
						'Service with problem tags'
					]
				]
			],
			// Name and Status: Problem fields.
			[
				[
					'filter' => [
						'Name' => 'with problem',
						'Status' => 'Problem'
					],
					'result' => [
						'Service with problem'
					]
				]
			],
			// Evaluation: And/Or.
			[
				[
					'Tags' => [
						'Evaluation' => 'And/Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Contains',
								'value' => 'true'
							],
							[
								'tag' => 'test',
								'operator' => 'Exists'
							]
						]
					],
					'result' => [
						'Service with multiple service tags'
					]
				]
			],
			// Evaluation: Or, Operators: Contains, Exists.
			[
				[
					'Tags' => [
						'Evaluation' => 'Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Contains',
								'value' => 'true'
							],
							[
								'tag' => 'test',
								'operator' => 'Exists'
							]
						]
					],
					'result' => [
						'Parent for 2 levels of child services 1',
						'Service with multiple service tags',
						'Simple actions service'
					]
				]
			],
			// Operator: Equals.
			[
				[
					'Tags' => [
						'evaluation_type' => 'And/Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Equals',
								'value' => 'false'
							]
						]
					],
					'result' => [
						'Name' => 'Simple actions service'
					]
				]
			],
			// Operator: Does not exist and Contains.
			[
				[
					'Tags' => [
						'evaluation_type' => 'And/Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'test',
								'operator' => 'Contains',
								'value' => 'test'
							],
							[
								'tag' => 'problem',
								'operator' => 'Does not exist'
							]
						]
					],
					'result' => [
						'Parent for 2 levels of child services 1'
					]
				]
			],
			// Operator: Does not equal and Exists.
			[
				[
					'Tags' => [
						'evaluation_type' => 'And/Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Does not equal',
								'value' => 'false'
							],
							[
								'tag' => 'test',
								'operator' => 'Exists'
							]
						]
					],
					'result' => [
						'Parent for 2 levels of child services 1',
						'Service with multiple service tags'
					]
				]
			],
			// Operator: Does not contain and Exists.
			[
				[
					'Tags' => [
						'evaluation_type' => 'And/Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'problem',
								'operator' => 'Exists'
							],
							[
								'tag' => 'test',
								'operator' => 'Does not contain',
								'value' => '456'
							]
						]
					],
					'result' => [
						'Simple actions service'
					]
				]
			],
			// Empty table.
			[
				[
					'Tags' => [
						'evaluation_type' => 'And/Or',
						'tags' => [
							[
								'index' => 0,
								'action' => USER_ACTION_UPDATE,
								'tag' => 'test',
								'operator' => 'Contains',
								'value' => 'test9'
							]
						]
					],
					'result' => [],
					'check_breadcrumbs' => true
				]
			]
		];
	}

	/**
	 * @dataProvider getFilterCommonData
	 */
	public function testPageServicesServices_FilterView($data) {
		$this->checkFiltering($data);
	}

	/**
	 * @dataProvider getFilterEditData
	 * @dataProvider getFilterCommonData
	 */
	public function testPageServicesServices_FilterEdit($data) {
		$this->checkFiltering($data, self::EDIT);
	}

	/**
	 * Function for checking filtering on Services->Service page.
	 *
	 * @param array      $data      data provider
	 * @param boolean    $edit      true if is edit scenario, false otherwise
	 */
	private function checkFiltering($data, $edit = false) {
		$this->page->login()->open(($edit === false) ? 'zabbix.php?action=service.list' :
				'zabbix.php?action=service.list.edit'
		);
		$filter_form = $this->query('name:zbx_filter')->waitUntilPresent()->asForm()->one();

		// Reset filter in case if some filtering remained before ongoing test case.
		$filter_form->query('button:Reset')->one()->click();

		// Fill filter form with data.
		$filter_form->fill(CTestArrayHelper::get($data, 'filter'));

		// If data contains Tags fill them separataly, because tags form is more complicated.
		if (CTestArrayHelper::get($data, 'Tags')) {
			if (CTestArrayHelper::get($data['Tags'], 'Source')) {
				$filter_form->getField('id:filter_tag_source')->asSegmentedRadio()->fill($data['Tags']['Source']);
			}

			if (CTestArrayHelper::get($data['Tags'], 'Evaluation')) {
				$filter_form->getField('id:filter_evaltype')->asSegmentedRadio()->fill($data['Tags']['Evaluation']);
			}

			$filter_form->getField('id:filter-tags')->asMultifieldTable()->fill(CTestArrayHelper::get($data, 'Tags.tags'));
		}

		$filter_form->submit();
		$this->page->waitUntilReady();

		// Check filtered result.
		$this->assertTableDataColumn($data['result'], 'Name');

		// Check breadcrumbs and table headers.
		$selector = $this->query(self::BREADCRUMB_SELECTOR);
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();
		if (CTestArrayHelper::get($data, 'check_breadcrumbs')) {
			$this->assertTrue($selector->one()->query('link:All services')->one()->isClickable());
			$this->assertTrue($selector->query('xpath:.//span[@class="selected" and text()="Filter results"]')->exists());

			$headers = ($edit)
					? ['', 'Parent services', 'Name', 'Status', 'Root cause', 'Created at', 'Tags', '']
					: ['Parent services', 'Name', 'Status', 'Root cause', 'Created at', 'Tags'];
			$this->assertSame($headers, $table->getHeadersText());
		}

		// Reset filter not to impact the results of next tests.
		$filter_form->query('button:Reset')->one()->click();

		// Check breadcrumbs and "Parent services" headers disappeared.
		if (CTestArrayHelper::get($data, 'check_breadcrumbs')) {
			$this->assertFalse($selector->query('link:All services')->exists());
			$this->assertFalse($selector->query('xpath:.//span[@class="selected" and text()="Filter results"]')->exists());
			$table->invalidate();

			$headers = ($edit)
					? ['', 'Name', 'Status', 'Root cause', 'Created at', 'Tags', '']
					: ['Name', 'Status', 'Root cause', 'Created at', 'Tags'];
			$this->assertSame($headers, $table->getHeadersText());
		}
	}

	public function testPageServicesServices_ResetButton() {
		$this->page->login()->open('zabbix.php?action=service.list');

		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();
		$form = $this->query('name:zbx_filter')->asForm()->one();

		// Check table contents before filtering.
		$start_rows_count = $table->getRows()->count();
		$this->assertTableStats($start_rows_count);
		$start_contents = $this->getTableColumnData('Name');

		// Filling fields with needed services info.
		$form->fill(['id:filter_name' => 'Parent for 2 levels of child services']);
		$form->submit();

		// Check that filtered count matches expected.
		$this->assertEquals(1, $table->getRows()->count());
		$this->assertTableStats(1);

		// Checking that filtered service matches expected.
		$this->assertTableDataColumn(['Parent for 2 levels of child services 1']);

		// After pressing reset button, check that previous services are displayed again.
		$form->query('button:Reset')->one()->click();

		$reset_count =  $table->getRows()->count();
		$this->assertEquals($start_rows_count, $reset_count);
		$this->assertTableStats($reset_count);
		$this->assertEquals($start_contents, $this->getTableColumnData('Name'));
	}

	public function testPageServicesServices_AddChild() {
		$parent = 'Service with problem';
		$child_name = 'Added child for Server with problem';

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query('class:list-table')->asTable()->waitUntilVisible()->one();

		$before_rows_count = $table->getRows()->count();
		$this->assertTableStats($before_rows_count);

		$table->findRow('Name', $parent)->query('xpath:.//button[@title="Add child service"]')->waitUntilClickable()
				->one()->click();

		COverlayDialogElement::find()->one()->waitUntilReady();
		$form = $this->query('id:service-form')->asForm()->one()->waitUntilReady();
		$form->fill(['Name' => $child_name]);
		$form->submit();
		$this->page->waitUntilReady();
		$this->assertMessage(TEST_GOOD, 'Service created');

		// Check that row count is not changed.
		$this->assertTableStats($before_rows_count);

		// Check DB.
		$childid = CDBHelper::getValue('SELECT serviceid FROM services WHERE name='.zbx_dbstr($child_name));
		$parentid = CDBHelper::getValue('SELECT serviceid FROM services WHERE name='.zbx_dbstr($parent));

		// Check parent-child linking in DB.
		$this->assertEquals(1, CDBHelper::getCount('SELECT * FROM services_links WHERE serviceupid='.
				zbx_dbstr($parentid).' AND servicedownid ='.zbx_dbstr($childid))
		);
	}

	public function testPageServicesServices_CancelDeleteFromRow() {
		$this->cancelDelete();
	}

	public function testPageServicesServices_CancelMassDelete() {
		$this->cancelDelete(true);
	}

	/**
	 * Function for checking cancelling of Delete action.
	 *
	 * @param boolean    $mass    true if is mass delete scenario, false otherwise
	 */
	private function cancelDelete($mass = false) {
		$name = 'Service for delete by checkbox';
		$sql = 'SELECT * FROM services ORDER BY serviceid';
		$old_hash = CDBHelper::getHash($sql);

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();

		if ($mass) {
			$this->selectTableRows([$name], 'Name');
			$this->query('button:Delete')->one()->click();
		}
		else {
			$table->findRow('Name', $name)->query('xpath:.//button[contains(@class, "btn-remove")]')->one()
				->waitUntilClickable()->click();
		}

		$this->page->dismissAlert();
		$this->page->waitUntilReady();

		// Check service not disappeared from frontend.
			$this->assertTrue($table->query("xpath:.//td/a[text()=".CXPathHelper::escapeQuotes($name)."]")->exists()
		);

		// Check database.
		$this->assertEquals($old_hash, CDBHelper::getHash($sql));
	}

	public function testPageServicesServices_SimpleServiceDeleteFromRow() {
		$name = 'Service for delete';

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();

		$before_rows_count = $table->getRows()->count();
		$this->assertTableStats($before_rows_count);

		// Delete service pressing cross button.
		$table->findRow('Name', $name)->query('xpath:.//button[contains(@class, "btn-remove")]')->one()
				->waitUntilClickable()->click();
		$this->page->acceptAlert();
		$this->page->waitUntilReady();
		$this->assertMessage(TEST_GOOD, 'Service deleted');

		// Check service disappeared from frontend.
		$this->assertTableStats($before_rows_count-1);
		$this->assertFalse($table->query("xpath:.//td/a[text()=".CXPathHelper::escapeQuotes($name)."]")->exists());

		// Check database.
		$this->assertEquals(0, CDBHelper::getCount('SELECT * FROM services WHERE name='.zbx_dbstr($name)));
	}

	public function testPageServicesServices_DeleteChildFromRow() {
		$parent = 'Parent for child deletion from row';
		$name = 'Child 1';

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();

		// Open parent service info.
		$table->findRow('Name', $parent, true)->query('link', $parent)->waitUntilClickable()->one()->click();
		$this->page->waitUntilReady();

		// Delete child service pressing cross button.
		$table->invalidate();
		$table->findRow('Name', $name)->query('xpath:.//button[contains(@class, "btn-remove")]')->one()
				->waitUntilClickable()->click();

		$this->page->acceptAlert();
		$this->page->waitUntilReady();
		$this->assertMessage(TEST_GOOD, 'Service deleted');

		// Check service disappeared from frontend.
		$this->assertTableData();
		$this->assertFalse($table->query("xpath:.//td/a[text()=".CXPathHelper::escapeQuotes($name)."]")->exists());

		// Check database.
		$this->assertEquals(0, CDBHelper::getCount('SELECT * FROM services WHERE name='.zbx_dbstr($name)));
		$this->assertEquals(1, CDBHelper::getCount('SELECT * FROM services WHERE name='.zbx_dbstr($parent)));

	}

	public function testPageServicesServices_DeleteParentFromRow() {
		$name = 'Parent for deletion from row';
		$child = 'Child 2';

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();

		$before_rows_count = $table->getRows()->count();
		$this->assertTableStats($before_rows_count);

		// Check that parent link exists.
		$this->assertTrue($table->query('link', $name)->exists());

		// Check that child service is not present in global service table.
		$this->assertFalse($table->query("xpath://td/a[text()=".CXPathHelper::escapeQuotes($child)."]")->exists());

		// Delete parent service.
		$table->findRow('Name', $name, true)->query('xpath:.//button[contains(@class, "btn-remove")]')->one()
				->waitUntilClickable()->click();

		$this->page->acceptAlert();
		$this->page->waitUntilReady();
		$this->assertMessage(TEST_GOOD, 'Service deleted');

		// Rows count remains unchanged because child takes parent's place.
		$this->assertTableStats($before_rows_count);

		// Parent disappeared from table.
		$this->assertFalse($table->query('link', $name)->exists());

		// Child now presents in table.
		$this->assertTrue($table->query("xpath:.//td/a[text()=".CXPathHelper::escapeQuotes($child)."]")->exists());

		// Check database.
		$this->assertEquals(0, CDBHelper::getCount('SELECT * FROM services WHERE name='.zbx_dbstr($name)));
		$this->assertEquals(1, CDBHelper::getCount('SELECT * FROM services WHERE name='.zbx_dbstr($child)));
	}

	/**
	 * @depends testPageServicesServices_SimpleServiceDeleteFromRow
	 */
	public function testPageServicesServices_SimpleServicesMassDelete() {
		$names = [
			'Service for delete by checkbox',
			'Service for delete 2'
		];

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();
		$before_rows_count = $table->getRows()->count();
		$this->assertTableStats($before_rows_count);

		$this->selectTableRows($names);
		$this->assertEquals(count($names).' selected',
				$this->query('id:selected_count')->waitUntilVisible()->one()->getText()
		);

		$this->query('button:Delete')->one()->click();
		$this->page->acceptAlert();
		$this->assertMessage(TEST_GOOD, 'Services deleted');

		$this->assertTableStats($before_rows_count - count($names));

		// Services disappeared from frontend.
		foreach ($names as $name) {
			$this->assertFalse($table->query("xpath:.//td/a[text()=".CXPathHelper::escapeQuotes($name)."]")->exists());
		}

		// Check database.
		$this->assertEquals(0, CDBHelper::getCount('SELECT * FROM services WHERE name LIKE '.zbx_dbstr('%delete%')));
	}

	public function testPageServicesServices_ChildrenMassDelete() {
		$parent = 'Clone parent';
		$names = [
			'Clone child 1',
			'Clone child 2'
		];
		$remained = 'Clone child 3';

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();

		// Open parent service info.
		$table->findRow('Name', $parent, true)->query('link', $parent)->waitUntilClickable()->one()->click();
		$this->page->waitUntilReady();
		$children_count = $table->getRows()->count();

		$this->selectTableRows($names, 'Name');
		$this->query('button:Delete')->one()->click();
		$this->page->acceptAlert();
		$this->assertMessage(TEST_GOOD, 'Services deleted');

		// Services disappeared from frontend.
		$this->assertEquals($children_count - count($names), $table->getRows()->count());
		foreach ($names as $name) {
			$this->assertFalse($table->query("xpath:.//td/a[text()=".CXPathHelper::escapeQuotes($name)."]")->exists());
		}

		// Last child is not deleted.
		$this->assertTrue($table->query("xpath:.//td/a[text()=".CXPathHelper::escapeQuotes($remained)."]")->exists());

		// Check database.
		foreach ($names as $name) {
			$this->assertEquals(0, CDBHelper::getCount('SELECT * FROM services WHERE name = '.zbx_dbstr($name)));
		}

		//  Last child remained in DB.
		$this->assertEquals(1, CDBHelper::getCount('SELECT * FROM services WHERE name = '.zbx_dbstr($remained)));
	}

	/**
	 * Test for checking services ordering.
	 */
	public function testPageServicesServices_TestOrder() {
		$parent = 'Test order';
		$children = ['1' => 2, '2' => 3, '3' => 1];

		$this->page->login()->open('zabbix.php?action=service.list.edit');
		$table = $this->query('class:list-table')->waitUntilVisible()->asTable()->one();
		$table->findRow('Name', $parent, true)->query('link', $parent)->waitUntilClickable()->one()->click();
		$this->assertTableDataColumn(['1', '2', '3']);

		foreach ($children as $child => $order) {
			$table->findRow('Name', $child)->query('xpath:.//button[@title="Edit"]')->waitUntilClickable()->one()->click();
			$form = COverlayDialogElement::find()->waitUntilReady()->asForm()->one();
			$form->fill(['Sort order (0->999)' => $order]);
			$form->submit();
			$this->page->waitUntilReady();
			$this->assertMessage(TEST_GOOD, 'Service updated');
		}

		$this->assertTableDataColumn(['3', '1', '2']);
	}

	public static function getRootCauseData() {
		return [
			// All children have problems without any advanced configuration on parent or on child services.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Most critical if all children have problems'
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average, 1_trigger_Warning'
				]
			],
			// Most critical of child services without any advanced configuration on parent or on child services.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Most critical of child services'
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average, 1_trigger_Warning'
				]
			],
			// Set status to OK any advanced configuration on parent or on child services.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK'
					]
				]
			],
			// Warning - If at least 1 child service has High status or above.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If at least N child services have Status status or above',
								'name:limit_value' => 1,
								'Status' => 'High'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High'
				]
			],
			// Warning - If at least 2 child service has High status or above.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If at least N child services have Status status or above',
								'name:limit_value' => 2,
								'Status' => 'High'
							]
						]
					]
				]
			],
			// Warning - If at least 50% of child services have Average status or above.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If at least N% of child services have Status status or above',
								'name:limit_value' => 50,
								'Status' => 'Average'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average'
				]
			],
			// Warning - If at least 90% of child services have Average status or above.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If at least N% of child services have Status status or above',
								'name:limit_value' => 90,
								'Status' => 'Average'
							]
						]
					]
				]
			],
			// Warning - If less than 3 child services have Average status or below.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If less than N child services have Status status or below',
								'name:limit_value' => 3,
								'Status' => 'Average'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High'
				]
			],
			// Warning - If less than 2 child services have Average status or below.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If less than N child services have Status status or below',
								'name:limit_value' => 2,
								'Status' => 'Average'
							]
						]
					]
				]
			],
			// Warning - If less than 35% of child services have Warning status or below.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If less than N% of child services have Status status or below',
								'name:limit_value' => 35,
								'Status' => 'Warning'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average'
				]
			],
			// Warning - If less than 30% of child services have Warning status or below.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If less than N% of child services have Status status or below',
								'name:limit_value' => 30,
								'Status' => 'Warning'
							]
						]
					]
				]
			],
			// Warning - If weight of child services with Average status or above is at least 30.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or above is at least W',
								'name:limit_value' => 20,
								'Status' => 'Average'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average'
				]
			],
			// Warning - If weight of child services with Average status or above is at least 25.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or above is at least W',
								'name:limit_value' => 25,
								'Status' => 'Average'
							]
						]
					]
				]
			],
			// Warning - If weight of child services with Average status or above is at least 66%.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or above is at least N%',
								'name:limit_value' => 66,
								'Status' => 'Average'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average'
				]
			],
			// Warning - If weight of child services with Average status or above is at least 67%.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or above is at least N%',
								'name:limit_value' => 67,
								'Status' => 'Average'
							]
						]
					]
				]
			],
			// Warning - If weight of child services with Average status or below is less than 21.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or below is less than W',
								'name:limit_value' => 21,
								'Status' => 'Average'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High'
				]
			],
			// Warning - If weight of child services with Average status or below is less than 20.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or below is less than W',
								'name:limit_value' => 20,
								'Status' => 'Average'
							]
						]
					]
				]
			],
			// Warning - If weight of child services with Average status or below is less than 67%.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or below is less than N%',
								'name:limit_value' => 67,
								'Status' => 'Average'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High'
				]
			],
			// Warning - If weight of child services with Average status or below is less than 66%.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or below is less than N%',
								'name:limit_value' => 66,
								'Status' => 'Average'
							]
						]
					]
				]
			],
			// Multiple additional rules.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Information',
								'Condition' => 'If weight of child services with Status status or below is less than N%',
								'name:limit_value' => 67,
								'Status' => 'Average'
							],
							[
								'Set status to' => 'Warning',
								'Condition' => 'If weight of child services with Status status or above is at least N%',
								'name:limit_value' => 66,
								'Status' => 'Average'
							],
							[
								'Set status to' => 'High',
								'Condition' => 'If less than N% of child services have Status status or below',
								'name:limit_value' => 30,
								'Status' => 'Warning'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average'
				]
			],
			// Child status propagation rule - Increase by.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Average',
								'Condition' => 'If at least N child services have Status status or above',
								'name:limit_value' => 2,
								'Status' => 'High'
							]
						]
					],
					'child' => [
						[
							'name' => self::ROOTCAUSE_CHILD1,
							'fields' => [
								'Status propagation rule' => 'Increase by',
								'id:propagation_value_number' => 3
							]
						]
					],
					'child_rootcause' => [
						'1' => '1_trigger_Warning',
						'2' => '1_trigger_Average',
						'3' => '1_trigger_High'
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Warning'
				]
			],
			// Child status propagation rule - Decrease by.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Average',
								'Condition' => 'If at least N child services have Status status or above',
								'name:limit_value' => 2,
								'Status' => 'Average'
							]
						]
					],
					'child' => [
						[
							'name' => self::ROOTCAUSE_CHILD1,
							'fields' => [
								'Status propagation rule' => 'As is'
							]
						],
						[
							'name' => self::ROOTCAUSE_CHILD3,
							'fields' => [
								'Status propagation rule' => 'Decrease by',
								'id:propagation_value_number' => 2
							]
						]
					]
				]
			],
			// Child status propagation rule - Ignore this service.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'Average',
								'Condition' => 'If less than N% of child services have Status status or below',
								'name:limit_value' => 51,
								'Status' => 'Average'
							]
						]
					],
					'child' => [
						[
							'name' => self::ROOTCAUSE_CHILD3,
							'fields' => [
								'Status propagation rule' => 'As is'
							]
						],
						[
							'name' => self::ROOTCAUSE_CHILD2,
							'fields' => [
								'Status propagation rule' => 'Ignore this service'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High'
				]
			],
			// Child status propagation rule - Fixed status.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Set status to OK',
						'Additional rules' => [
							[
								'Set status to' => 'High',
								'Condition' => 'If weight of child services with Status status or above is at least W',
								'name:limit_value' => 20,
								'Status' => 'High'
							]
						]
					],
					'child' => [
						[
							'name' => self::ROOTCAUSE_CHILD2,
							'fields' => [
								'Status propagation rule' => 'As is'
							]
						],
						[
							'name' => self::ROOTCAUSE_CHILD1,
							'fields' => [
								'Status propagation rule' => 'Fixed status',
								'id:propagation_value_status' => 'Disaster'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Warning'
				]
			],
			// All three child services use status propagation modifications. Should not be possible to decrease to OK.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Most critical of child services'
					],
					'child' => [
						[
							'name' => self::ROOTCAUSE_CHILD1,
							'fields' => [
								'Status propagation rule' => 'Decrease by',
								'id:propagation_value_number' => 5
							]
						],
						[
							'name' => self::ROOTCAUSE_CHILD2,
							'fields' => [
								'Status propagation rule' => 'Fixed status',
								'id:propagation_value_status' => 'OK'
							]
						],
						[
							'name' => self::ROOTCAUSE_CHILD3,
							'fields' => [
								'Status propagation rule' => 'Ignore this service'
							]
						]
					],
					'parent_rootcause' => '1_trigger_Warning'
				]
			],
			// All three child services use status propagation modifications. Increase all to High and above.
			[
				[
					'parent' => [
						'Status calculation rule' => 'Most critical if all children have problems',
						'Additional rules' => [
							[
								'Set status to' => 'High',
								'Condition' => 'If weight of child services with Status status or below is less than W',
								'name:limit_value' => 1,
								'Status' => 'Average'
							]
						]
					],
					'child' => [
						[
							'name' => self::ROOTCAUSE_CHILD1,
							'fields' => [
								'Status propagation rule' => 'Increase by',
								'id:propagation_value_number' => 5
							]
						],
						[
							'name' => self::ROOTCAUSE_CHILD2,
							'fields' => [
								'Status propagation rule' => 'Fixed status',
								'id:propagation_value_status' => 'Disaster'
							]
						],
						[
							'name' => self::ROOTCAUSE_CHILD3,
							'fields' => [
								'Status propagation rule' => 'As is'
							]
						]
					],
					'parent_rootcause' => '1_trigger_High, 1_trigger_Average, 1_trigger_Warning'
				]
			]
		];
	}

	/**
	 * @onBeforeOnce prepareServiceProblemsData
	 *
	 * @dataProvider getRootCauseData
	 */
	public function testPageServicesServices_RootCause($data) {
		$this->page->login()->open('zabbix.php?action=service.list.edit');

		$table = $this->query(self::TABLE_SELECTOR)->asTable()->one();
		$row = $table->findRow('Name', self::ROOTCAUSE_PARENT, true);
		$row->query('xpath:.//button[@title="Edit"]')->waitUntilClickable()->one()->click();

		COverlayDialogElement::find()->one()->waitUntilReady();
		$form = $this->query('id:service-form')->asForm()->one();
		$form->getField('Status calculation rule')->fill($data['parent']['Status calculation rule']);

		if (array_key_exists('Additional rules', $data['parent'])) {
			$form->query('id:advanced_configuration')->asCheckbox()->one()->set(true);

			// Remove the additional rules from previous test cases.
			$form->getFieldContainer('Additional rules')->query('button:Remove')->all(false)->click();

			// Fill in configuration of each Additional rule separately.
			foreach ($data['parent']['Additional rules'] as $rule_fields) {
				$form->getFieldContainer('Additional rules')->query('button:Add')->waitUntilClickable()->one()->click();
				$rules_form = COverlayDialogElement::find()->all()->last()->waitUntilReady()->asForm();
				$rules_form->fill($rule_fields);
				$rules_form->submit();
				$rules_form->waitUntilNotVisible();
			}
		}

		$form->submit();
		$table->waitUntilReloaded();

		// Fill in the Advanced configuration fields for child services, that impact the root cause on the parent service.
		if (array_key_exists('child', $data)) {
			$this->query('link', self::ROOTCAUSE_PARENT)->one()->waitUntilClickable()->click();
			$this->page->waitUntilReady();

			$child_table = $this->query(self::TABLE_SELECTOR)->asTable()->one();

			// Check the root causes for the child services if corresponding data exists in the data provider.
			if (array_key_exists('child_rootcause', $data)) {
				foreach ($data['child_rootcause'] as $service_name => $child_rootcause) {
					$this->assertEquals($data['child_rootcause'][$service_name], $child_table->findRow('Name', $service_name)
							->getColumn('Root cause')->getText()
					);
				}
			}

			// Fill in configuration of each child service separately.
			foreach ($data['child'] as $child_service) {
				$child_table->findRow('Name', $child_service['name'])->query('xpath:.//button[@title="Edit"]')->one()->click();

				COverlayDialogElement::find()->one()->waitUntilReady();
				$form = $this->query('id:service-form')->asForm()->one();

				$form->fill($child_service['fields']);
				$form->submit();

				$child_table->waitUntilReloaded();
			}

			// Exit to the default services view, where parent root cause can be seen.
			$this->query('link:All services')->one()->waitUntilClickable()->click();
			$this->page->waitUntilReady();
		}

		// Check root cause of the parent service.
		$row->invalidate();
		$this->assertEquals(CTestArrayHelper::get($data, 'parent_rootcause', ''), $row->getColumn('Root cause')->getText());
	}
}