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

testFormTags.php « common « selenium « tests « ui - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e951c7efaab65869bf93985f242b9426fe81068d (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
<?php
/*
** Zabbix
** Copyright (C) 2001-2022 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 Tags function tests.
 *
 * @backup profiles
 */
class testFormTags extends CWebTest {

	const EDIT_BUTTON_PATH = 'xpath:.//button[@title="Edit"]';

	public $update_name;
	public $clone_name;
	public $remove_name;
	public $link;
	public $saved_link;
	public $host;
	public $template;

	/**
	 * Flag for problem tags in services.
	 */
	public $problem_tags = false;

	// Tags on host "Host for tags testing".
	const HOST_TAGS = [
		[
			'tag' => 'a:',
			'value' => 'a'
		],
		[
			'tag' => 'action',
			'value' => 'simple'
		],
		[
			'tag' => 'tag',
			'value' => 'HOST'
		],
		[
			'tag' => 'host tag without value',
			'value' => ''
		],
		[
			'tag' => 'common tag on host and element',
			'value' => 'common value'
		]
	];

	// Tags on template "Template for tags testing".
	const TEMPLATE_TAGS = [
		[
			'tag' => 'action',
			'value' => 'simple'
		],
		[
			'tag' => 'tag',
			'value' => 'TEMPLATE'
		],
		[
			'tag' => 'templateTag without value',
			'value' => ''
		],
		[
			'tag' => 'common tag on template and element',
			'value' => 'common value'
		]
	];

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

	public function getCreateData() {
		return [
			[
				[
					'name' => 'With tags',
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => '!@#$%^&*()_+<>,.\/',
							'value' => '!@#$%^&*()_+<>,.\/'
						],
						[
							'tag' => 'tag1',
							'value' => 'value1'
						],
						[
							'tag' => 'tag2'
						],
						[
							'tag' => '{$MACRO:A}',
							'value' => '{$MACRO:A}'
						],
						[
							'tag' => '{$MACRO}',
							'value' => '{$MACRO}'
						],
						[
							'tag' => 'Таг',
							'value' => 'Значение'
						]
					]
				]
			],
			[
				[
					'name' => 'With equal tag names',
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => 'tag3',
							'value' => '3'
						],
						[
							'tag' => 'tag3',
							'value' => '4'
						]
					]
				]
			],
			[
				[
					'name' => 'With equal tag values',
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => 'tag4',
							'value' => '5'
						],
						[
							'tag' => 'tag5',
							'value' => '5'
						]
					]
				]
			],
			[
				[
					'expected' => TEST_BAD,
					'name' => 'With empty tag name',
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'value' => 'value1'
						]
					],
					'error_details' => 'Invalid parameter "/1/tags/1/tag": cannot be empty.'
				]
			],
			[
				[
					'expected' => TEST_BAD,
					'name' => 'With equal tags',
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => 'tag',
							'value' => 'value'
						],
						[
							'tag' => 'tag',
							'value' => 'value'
						]
					],
					'error_details' => 'Invalid parameter "/1/tags/2": value (tag, value)=(tag, value) already exists.'
				]
			],
			[
				[
					'name' => 'With trailing spaces',
					'trim' => true,
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => '    trimmed tag    ',
							'value' => '   trimmed value    '
						]
					]
				]
			],
			[
				[
					'name' => 'Long tag name and value',
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => 'Long tag name. Long tag name. Long tag name. Long tag name. Long tag name.'
									.' Long tag name. Long tag name. Long tag name.',
							'value' => 'Long tag value. Long tag value. Long tag value. Long tag value. Long tag value.'
									.' Long tag value. Long tag value. Long tag value. Long tag value.'
						]
					]
				]
			]
		];
	}

	/**
	 * Check of creating different objects with tags.
	 *
	 * @param array    $data         data provider
	 * @param string   $object       host, template, trigger, item or prototypes
	 * @param string   $expression   trigger or trigger prototype expression
	 */
	public function checkTagsCreate($data, $object, $expression = null) {
		$sql = null;
		$old_hash = null;

		switch ($object) {
			case 'trigger':
			case 'trigger prototype':
				$sql = 'SELECT * FROM triggers ORDER BY triggerid';
				$locator = 'name:triggersForm';
				$fields = ['Name' => $data['name'], 'Expression' => $expression];
				break;

			case 'item':
			case 'item prototype':
				$sql = 'SELECT * FROM items ORDER BY itemid';
				$locator = 'name:itemForm';
				$fields = ['Name' => $data['name'], 'Key' => 'itemtag_'.microtime(true).'[{#KEY}]', 'Type' => 'Zabbix trapper'];
				break;

			case 'web scenario':
				$sql = 'SELECT * FROM httptest ORDER BY httptestid';
				$locator = 'name:httpForm';
				$fields = ['Name' => $data['name'], 'Key' => 'itemtag_'.microtime(true)];
				break;

			case 'service':
				$sql = 'SELECT * FROM services ORDER BY serviceid';
				$locator = 'id:service-form';
				$fields = ['Name' => $data['name']];
				break;

			case 'host':
			case 'host prototype':
			case 'template':
				$sql = 'SELECT * FROM hosts ORDER BY hostid';
				$locator = ($object === 'host prototype') ? 'name:hostPrototypeForm' : 'name:'.$object.'sForm';
				$group_field = ($object === 'template') ? 'Template groups' : 'Host groups';
				$group_name = ($object === 'template') ? 'Templates' : 'Zabbix servers';
				$fields = [ucfirst($object).' name' => $data['name'], $group_field => $group_name];
		}

		if (CTestArrayHelper::get($data, 'expected', TEST_GOOD) === TEST_BAD) {
			$old_hash = CDBHelper::getHash($sql);
		}

		$this->page->login()->open($this->link);

		$this->query('button:Create '.$object)->waitUntilClickable()->one()->click();

		$form = ($object === 'host' || $object === 'service')
			? COverlayDialogElement::find()->asForm()->one()->waitUntilVisible()
			: $this->query($locator)->waitUntilPresent()->asForm()->one();

		if ($object === 'host prototype') {
			$data['name'] = $data['name'].' {#KEY}';
			$form->fill(['Host name' => $data['name']]);
			$form->fill(['Host groups' => 'Zabbix servers']);
		}
		elseif ($object === 'web scenario') {
			$form->fill(['Name' => $data['name']]);
			$form->selectTab('Steps');
			$form->getField('Steps')->query('button:Add')->waitUntilClickable()->one()->click();
			COverlayDialogElement::find()->one()->waitUntilReady();
			$overlay_form = $this->query('id:http_step')->asForm()->one();
			$overlay_form->fill(['Name' => 'zabbix', 'id:url' => 'http://zabbix.com']);
			$overlay_form->submit();
			COverlayDialogElement::ensureNotPresent();
		}
		else {
			$form->fill($fields);
		}

		if (!$this->problem_tags) {
			$form->selectTab('Tags');
			$tags_table = 'class:tags-table';
		}
		else {
			$tags_table = 'id:problem_tags';
		}
		$this->query($tags_table)->asMultifieldTable()->one()->fill($data['tags']);

		// Check screenshots of text area right after filling.
		if ($data['name'] === 'With tags' || $data['name'] === 'Long tag name and value') {
			$this->page->removeFocus();
			$this->page->updateViewport();
			$screenshot_area = $this->query($tags_table)->one();
			$screen_object = ($this->problem_tags) ? 'Service problem tags' : $object;
			$this->assertScreenshot($screenshot_area, $data['name'].' '.$screen_object);
		}

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

		$this->checkResult($data, $object, $form, 'add', $sql, $old_hash);

		return $form;
	}

	public static function getUpdateData() {
		return [
			[
				[
					'expected' => TEST_BAD,
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => '',
							'value' => 'value1'
						]
					],
					'error_details'=>'Invalid parameter "/1/tags/1/tag": cannot be empty.'
				]
			],
			[
				[
					'expected' => TEST_BAD,
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 1,
							'tag' => 'action',
							'value' => 'update'
						]
					],
					'error_details' => 'Invalid parameter "/1/tags/2": value (tag, value)=(action, update) already exists.'
				]
			],
			[
				[
					'expected' => TEST_BAD,
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 2,
							'tag' => 'tag without value',
							'value' => ''
						]
					],
					'error_details' => 'Invalid parameter "/1/tags/3": value (tag, value)=(tag without value, ) already exists.'
				]
			],
			[
				[
					'trim' => true,
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => 'new tag       ',
							'value' => '   trimmed value    '
						],
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 1,
							'tag' => '    trimmed tag    ',
							'value' => '        new value'
						],
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 2,
							'tag' => '    trimmed tag2',
							'value' => 'new value        '
						]
					]
				]
			],
			[
				[
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => '!@#$%^&*()_+<>,.\/',
							'value' => '!@#$%^&*()_+<>,.\/'
						],
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 1,
							'tag' => 'tag1',
							'value' => 'value1'
						],
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 2,
							'tag' => 'tag2'
						],
						[
							'tag' => '{$MACRO:A}',
							'value' => '{$MACRO:A}'
						],
						[
							'tag' => '{$MACRO}',
							'value' => '{$MACRO}'
						],
						[
							'tag' => 'Тег',
							'value' => 'Значение'
						]
					]
				]
			]
		];
	}

	/**
	 * Check updating tags in different objects.
	 *
	 * @param array    $data     data provider
	 * @param string   $object   host, template, trigger, prototype, service etc.
	 */
	public function checkTagsUpdate($data, $object) {
		$sql = null;
		$old_hash = null;

		switch ($object) {
			case 'trigger':
			case 'trigger prototype':
				$sql = 'SELECT * FROM triggers ORDER BY triggerid';
				$locator = 'name:triggersForm';
				break;

			case 'item':
			case 'item prototype':
				$sql = 'SELECT * FROM items ORDER BY itemid';
				$locator = 'name:itemForm';
				break;

			case 'web scenario':
				$sql = 'SELECT * FROM httptest ORDER BY httptestid';
				$locator = 'name:httpForm';
				break;

			case 'service':
				$sql = 'SELECT * FROM services ORDER BY serviceid';
				$locator = 'id:service-form';
				break;

			case 'host':
			case 'host prototype':
			case 'template':
				$sql = 'SELECT * FROM hosts ORDER BY hostid';
				$locator = ($object === 'host prototype') ? 'name:hostPrototypeForm' : 'name:'.$object.'sForm';
		}

		if (CTestArrayHelper::get($data, 'expected', TEST_GOOD) === TEST_BAD) {
			$old_hash = CDBHelper::getHash($sql);
		}

		$data['name'] = $this->update_name;

		$this->page->login()->open($this->link);

		if ($object === 'service') {
			$table = $this->query('class:list-table')->asTable()->one()->waitUntilPresent();
			$table->findRow('Name', $data['name'], true)->query(self::EDIT_BUTTON_PATH)->waitUntilClickable()->one()->click();
		}
		else {
			$this->query('link', $this->update_name)->waitUntilClickable()->one()->click();
		}

		$form = ($object === 'host' || $object === 'service')
			? COverlayDialogElement::find()->waitUntilVisible()->asForm()->one()
			: $this->query($locator)->asForm()->waitUntilPresent()->one();

		if (!$this->problem_tags) {
			$form->selectTab('Tags');
			$tags_table = 'class:tags-table';
		}
		else {
			$tags_table = 'id:problem_tags';
		}

		$this->query($tags_table)->asMultifieldTable()->waitUntilPresent()->one()->fill($data['tags']);
		$form->submit();
		$this->page->waitUntilReady();

		$this->checkResult($data, $object, $form, 'update', $sql, $old_hash);
	}

	/**
	 * Check result after creating or updating object with tags.
	 *
	 * @param array     $data        data provider
	 * @param string    $object      host, template, trigger, item or prototype
	 * @param element   $form        object configuration form
	 * @param string    $action      create or update object
	 * @param string    $sql         selected table from db
	 * @param string    $old_hash    db hash before changes
	 */
	private function checkResult($data, $object, $form, $action, $sql = null, $old_hash = null) {
		if (CTestArrayHelper::get($data, 'expected', TEST_GOOD) === TEST_BAD) {

			$title = ($object === 'service')
				? null
				: (($action === 'add') ? 'Cannot add '.$object : 'Cannot update '.$object);
			$this->assertMessage(TEST_BAD, $title, CTestArrayHelper::get($data, 'error_details'));

			// Check that DB hash is not changed.
			$this->assertEquals($old_hash, CDBHelper::getHash($sql));

			if ($object === 'host' || $object === 'service') {
				COverlayDialogElement::find()->one()->close();
			}
		}
		else {
			switch ($object) {
				case 'host':
				case 'template':
				case 'host prototype':
					$success_sql = 'SELECT NULL FROM hosts WHERE host='.zbx_dbstr($data['name']);
					break;

				case 'trigger':
				case 'trigger prototype':
					$success_sql = 'SELECT NULL FROM triggers WHERE description='.zbx_dbstr($data['name']);
					break;

				case 'item':
				case 'item prototype':
					$success_sql = 'SELECT NULL FROM items WHERE name='.zbx_dbstr($data['name']);
					break;

				case 'web scenario':
					$success_sql = 'SELECT NULL FROM httptest WHERE name='.zbx_dbstr($data['name']);
					break;

				case 'service':
					$success_sql = 'SELECT NULL FROM services WHERE name='.zbx_dbstr($data['name']);
					break;
			}

			$title = ($action === 'add')
				? ($object === 'service') ? ucfirst($object).' created' : ucfirst($object).' added'
				: ucfirst($object).' updated';

			$this->assertMessage(TEST_GOOD, $title);

			// 2 elements for test case "InheritedHostAndTemplateTags"
			$count_elements = (strpos($data['name'], 'Inheritance') !== false) ? 2 : 1;
			$this->assertEquals($count_elements, CDBHelper::getCount($success_sql));

			// Check the results in form.
			$this->checkTagFields($data, $object, $form);
		}
	}

	/**
	 * Test cloning of host, template, item, trigger or prototype with tags
	 *
	 * @param string   $object   host, template, item, trigger or prototype
	 * @param string   $action   clone or full clone
	 */
	public function executeCloning($object, $action) {
		$new_name = (strpos($object, 'prototype') !== false)
			? 'Tags - '.$action.' '.$object.' {#KEY}'
			: '1Tags - '.$action.' '.$object;

		$this->page->login()->open($this->link);

		if ($object === 'service') {
			$table = $this->query('class:list-table')->asTable()->one();
			$table->findRow('Name',  $this->clone_name)->query(self::EDIT_BUTTON_PATH)->waitUntilClickable()->one()->click();
		}
		else {
			$this->query('link', $this->clone_name)->waitUntilClickable()->one()->click();
		}

		switch ($object) {
			case 'trigger':
			case 'trigger prototype':
				$form = $this->query('name:triggersForm')->asForm()->waitUntilPresent()->one();
				$form->fill(['Name' => $new_name]);
				$sql_old_name = 'SELECT NULL FROM triggers WHERE description='.zbx_dbstr($this->clone_name);
				$sql_new_name = 'SELECT NULL FROM triggers WHERE description='.zbx_dbstr($new_name);
				break;

			case 'item':
			case 'item prototype':
				$form = $this->query('name:itemForm')->asForm()->waitUntilPresent()->one();
				$form->fill(['Name' => $new_name, 'Key' => 'newkey_'.microtime(true).'[{#KEY}]']);
				$sql_old_name = 'SELECT NULL FROM items WHERE name='.zbx_dbstr($this->clone_name);
				$sql_new_name = 'SELECT NULL FROM items WHERE name='.zbx_dbstr($new_name);
				break;

			case 'host':
			case 'host prototype':
			case 'discovered host':
				$form_name = ($object === 'host prototype') ? 'name:hostPrototypeForm' : 'name:host-form';
				$form = $this->query($form_name)->asForm()->waitUntilPresent()->one();
				if ($object !== 'discovered host') {
					$form->fill(['Host name' => $new_name]);
				}
				$sql_old_name = 'SELECT NULL FROM hosts WHERE host='.zbx_dbstr($this->clone_name);
				$sql_new_name = 'SELECT NULL FROM hosts WHERE host='.zbx_dbstr($new_name);
				break;

			case 'template':
				$form = $this->query('name:templatesForm')->asForm()->waitUntilPresent()->one();
				$form->fill(['Template name' => $new_name]);
				$sql_old_name = 'SELECT NULL FROM hosts WHERE host='.zbx_dbstr($this->clone_name);
				$sql_new_name = 'SELECT NULL FROM hosts WHERE host='.zbx_dbstr($new_name);
				break;

			case 'web scenario':
				$form = $this->query('name:httpForm')->asForm()->waitUntilPresent()->one();
				$form->fill(['Name' => $new_name]);
				$sql_old_name = 'SELECT NULL FROM httptest WHERE name='.zbx_dbstr($this->clone_name);
				$sql_new_name = 'SELECT NULL FROM httptest WHERE name='.zbx_dbstr($new_name);
				break;

			case 'service':
				$form = COverlayDialogElement::find()->asForm()->one()->waitUntilReady();
				$form->fill(['Name' => $new_name]);
				$sql_old_name = 'SELECT NULL FROM services WHERE name='.zbx_dbstr($this->clone_name);
				$sql_new_name = 'SELECT NULL FROM services WHERE name='.zbx_dbstr($new_name);
				break;

		}

		if (!$this->problem_tags) {
			$form->selectTab('Tags');
			$tags_table = 'class:tags-table';
		}
		else {
			$tags_table = 'id:problem_tags';
		}
		$element = $this->query($tags_table)->asMultifieldTable()->one();
		$tags = $element->getValue();

		// Click Clone or Full Clone button.
		$this->query('button', $action)->one()->click();
		$this->page->waitUntilReady();

		if ($object === 'discovered host') {
			$form->fill(['Host name' => $new_name]);
		}

		// Find form again for cloned host and click Add host.
		$form->invalidate();
		$form->submit();
		$this->page->waitUntilReady();

		if ($object === 'discovered host') {
			$this->assertMessage(TEST_GOOD, ('Host added'));
		}
		else {
			$this->assertMessage(TEST_GOOD, (
					($object === 'service')
						? ucfirst($object).' created'
						: ucfirst($object).' added'
				)
			);
		}

		// Check the results in DB.
		$this->assertEquals(1, CDBHelper::getCount($sql_old_name));
		$this->assertEquals(1, CDBHelper::getCount($sql_new_name));

		// Check created clone.
		if ($object === 'service') {
			$table = $this->query('class:list-table')->asTable()->one()->waitUntilReady();
			$table->findRow('Name',  $new_name)->query(self::EDIT_BUTTON_PATH)->waitUntilClickable()->one()->click();
		}
		else {
			$this->query('link', $new_name)->one()->click();
		}
		$form->invalidate();

		switch ($object) {
			case 'host':
			case 'host prototype':
			case 'discovered host':
				$this->assertEquals($new_name, $form->getField('Host name')->getValue());
				break;

			case 'template':
				$this->assertEquals($new_name, $form->getField('Template name')->getValue());
				break;

			case 'trigger prototype':
			case 'trigger':
			case 'item prototype':
			case 'item':
			case 'web scenario':
			case 'service':
				$this->assertEquals($new_name, $form->getField('Name')->getValue());
				break;
		}

		$form->selectTab('Tags');
		$element->checkValue($tags);

		if ($object === 'host' || $object === 'discovered host') {
			COverlayDialogElement::find()->one()->close();
		}
	}

	/**
	 * Function for checking saved tag fields in form.
	 *
	 * @param array    $data     data provider
	 * @param string   $object   host, template, trigger, item or prototype
	 * @param string   $form     object configuration form
	 */
	private function checkTagFields($data, $object, $form) {
		switch ($object) {
			case 'trigger':
			case 'trigger prototype':
				$id = CDBHelper::getValue('SELECT triggerid FROM triggers WHERE description='.zbx_dbstr($data['name']));
				break;

			case 'item':
			case 'item prototype':
				$id = CDBHelper::getValue('SELECT itemid FROM items WHERE name='.zbx_dbstr($data['name']));
				break;

			case 'web scenario':
				$id = CDBHelper::getValue('SELECT httptestid FROM httptest WHERE name='.zbx_dbstr($data['name']));
				break;

			case 'host':
			case 'host prototype':
			case 'discovered host':
			case 'template':
				$id = CDBHelper::getValue('SELECT hostid FROM hosts WHERE host='.zbx_dbstr($data['name']));
		}

		if ($object === 'service') {
			$this->page->open($this->link);
			$table = $this->query('class:list-table')->asTable()->one()->waitUntilReady();
			$table->findRow('Name', $data['name'])->query(self::EDIT_BUTTON_PATH)->waitUntilClickable()->one()->click();
			$form = COverlayDialogElement::find()->waitUntilReady()->asForm()->one();
		}
		else {
			$this->page->open($this->saved_link.$id);
		}

		if ($object === 'host') {
			$form = $this->query('id:host-form')->waitUntilPresent()->asForm()->one();
		}

		if (!$this->problem_tags) {
			$form->selectTab('Tags');
			$tags_table = 'class:tags-table';
		}
		else {
			$tags_table = 'id:problem_tags';
		}

		$expected = $data['tags'];
		foreach ($expected as &$tag) {
			unset($tag['action'], $tag['index']);

			if (CTestArrayHelper::get($data, 'trim', false) === false) {
				continue;
			}

			// Remove trailing spaces from tag and value.
			foreach ($expected as $i => &$options) {
				foreach (['tag', 'value'] as $parameter) {
					if (array_key_exists($parameter, $options)) {
						$options[$parameter] = trim($options[$parameter]);
					}
				}
			}
			unset($options);
		}
		unset($tag);

		$this->query($tags_table)->asMultifieldTable()->one()->checkValue($expected);

		// Check screenshot of text area after saving.
		if ($data['name'] === 'With tags' || $data['name'] === 'Long tag name and value') {
			$this->page->removeFocus();
			$screenshot_area = $this->query($tags_table)->one();
			$screen_object = ($this->problem_tags) ? 'Service problem tags' : $object;
			$this->assertScreenshot($screenshot_area, $data['name'].' '.$screen_object);
		}
	}

	/**
	 * Test full cloning of host or template with trigger, item, web scenario or prototype that have tags.
	 *
	 * @param string   $object   item, trigger, web scenario or prototype
	 * @param string   $parent   host or template
	 */
	public function executeFullCloning($object, $parent) {
		$new_name = '1Tags - full cloning of '.$parent.' with '.$object;
		$this->page->login()->open($this->link);
		$this->query('link', $this->clone_name)->waitUntilClickable()->one()->click();

		// Get tags of object.
		switch ($object) {
			case 'trigger':
				$form_selector = 'id:triggers-form';
				break;

			case 'item':
				$form_selector = 'id:item-form';
				break;

			case 'web scenario':
				$form_selector = 'id:http-form';
				break;

			case 'host prototype':
				$form_selector = 'id:host-prototype-form';
				break;

			case 'item prototype':
				$form_selector = 'id:item-prototype-form';
				break;

			case 'trigger prototype':
				$form_selector = 'id:triggers-prototype-form';
				break;
			}

		$form = $this->query($form_selector)->asForm()->waitUntilPresent()->one();
		$form->selectTab('Tags');
		$element = $this->query('class:tags-table')->asMultifieldTable()->one();
		$tags = $element->getValue();

		// Navigate to host or template for full cloning.
		$this->query('link', ($parent === 'Host') ? $this->host : $this->template)->waitUntilClickable()->one()->click();
		$host_form = ($object !== 'host prototype' && $parent !== 'Template')
			? COverlayDialogElement::find()->asForm()->one()->waitUntilReady()
			: $this->query('id', ($parent === 'Host') ? 'host-form' : 'templates-form')->asForm()->waitUntilPresent()->one();

		$host_form->fill([$parent.' name' => $new_name]);
		$this->query('button:Full clone')->one()->click();
		$this->query('xpath://div[@class="overlay-dialogue-footer" or contains(@class, "tfoot-buttons")]//button[text()="Add"]')
				->waitUntilClickable()->one()->click();
		$this->page->waitUntilReady();
		$this->assertMessage(TEST_GOOD, $parent.' added');

		if ($parent === 'Host') {
			if ($object !== 'host prototype') {
				$this->query('link:All hosts')->one()->click();
			}
			$this->page->waitUntilReady();
			$this->query('button:Reset')->one()->click();
			$form = $this->query('name:zbx_filter')->asForm()->waitUntilReady()->one();
			$form->fill(['Name' => $new_name]);
			$this->query('button:Apply')->one()->waitUntilClickable()->click();

			switch ($object) {
				case 'trigger':
					$column = 'Triggers';
					break;

				case 'item':
					$column = 'Items';
					break;

				case 'web scenario':
					$column = 'Web';
					break;

				case 'host prototype':
				case 'item prototype':
				case 'trigger prototype':
					$column = 'Discovery';
					break;
			}

			$this->query('xpath://table[@class="list-table"]')->asTable()->one()->findRow('Name', $new_name)
					->getColumn($column)->query('link', $column)->one()->click();
		}
		else {
			// Open cloned host/template.
			$this->query('link', $new_name)->one()->click();
		}

		switch ($object) {
			case 'trigger':
			case 'item':
			case 'web scenario':
				$this->query('link', ucfirst($object).'s')->waitUntilClickable()->one()->click();
				$this->query('link', $this->clone_name)->waitUntilClickable()->one()->click();
				break;

			case 'host prototype':
			case 'item prototype':
			case 'trigger prototype':
				if ($parent !== 'Host') {
					$this->query('link:Discovery rules')->waitUntilClickable()->one()->click();
				}

				$this->query('link', ucfirst($object).'s')->waitUntilClickable()->one()->click();
				$this->query('link', $this->clone_name)->waitUntilClickable()->one()->click();
				break;
		}

		$new_form = $this->query('xpath://main/form')->asForm()->waitUntilPresent()->one();
		$new_form->selectTab('Tags');
		$element->invalidate();
		$element->checkValue($tags);
	}

	/**
	 * Test copy of trigger or item.
	 *
	 * @param string   $object			item or trigger
	 * @param string   $target_type		target type
	 * @param string   $parent			host, host group or template name
	 */
	public function executeCopy($object, $target_type, $parent) {
		$this->page->login()->open($this->link);
		$this->query('link', $this->clone_name)->waitUntilClickable()->one()->click();

		// Get tags of object and return to the list.
		$form = $this->query('xpath://main/form')->asForm()->waitUntilPresent()->one();
		$form->selectTab('Tags');
		$element = $this->query('class:tags-table')->asMultifieldTable()->one();
		$tags = $element->getValue();
		$this->query('button:Cancel')->one()->click();

		// Select object and copy to target.
		$table_name = ($object === 'item') ? 'items' : 'triggersForm';
		$table = $this->query('xpath://form[@name='.CXPathHelper::escapeQuotes($table_name).']/table')
				->asTable()->waitUntilReady()->one();
		$table->findRow('Name', $this->clone_name)->select();
		$this->query('button:Copy')->one()->click();
		$copy_form = $this->query('name:elements_form')->asForm()->waitUntilPresent()->one();
		$copy_form->fill(['Target type' => $target_type.'s', 'Target' => $parent]);
		$copy_form->submit();
		$this->page->waitUntilReady();
		$this->assertMessage(TEST_GOOD, ucfirst($object).' copied');

		// Open host group, host or template and check object tags.
		if ($target_type !== 'Host group') {
			$this->page->open(($target_type === 'Host') ? self::HOST_LIST_PAGE : 'templates.php')->waitUntilReady();

			if ($target_type === 'Host') {
				$this->query('button:Reset')->one()->click();
				$filter = $this->query('name:zbx_filter')->asForm()->waitUntilReady()->one();
				$filter->fill(['Name' => $parent]);
				$this->query('button:Apply')->one()->waitUntilClickable()->click();
				$this->query('xpath://table[@class="list-table"]')->asTable()->one()->findRow('Name', $parent)
						->getColumn(ucfirst($object).'s')->query('link', ucfirst($object).'s')->one()->click();
			}
			else {
				$this->query('link', $parent)->waitUntilClickable()->one()->click();
				$this->query('link', ucfirst($object).'s')->waitUntilClickable()->one()->click();
			}

			$this->query('link', $this->clone_name)->waitUntilClickable()->one()->click();
			$form->invalidate();
			$form->selectTab('Tags');
			$element->checkValue($tags);
		}
		else {
			$filter_form = $this->query('name:zbx_filter')->asForm()->one();
			$filter_form->fill(['Host groups' => $parent, 'Hosts' => '']);
			$result_form = $this->query('xpath://form[@name='.CXPathHelper::escapeQuotes($table_name).']')->one();
			$this->query('button:Apply')->one()->click();
			$this->page->waitUntilReady();
			$result_form->waitUntilReloaded();
			// Find row indices with the cloned entity name.
			$indices = $table->findRows(function ($row) {
				return $row->getColumn('Name')->getText() === $this->clone_name;
			});
			foreach (array_keys($indices->asArray()) as $index) {
				$table->getRow($index)->getColumn('Name')->query('tag:a')->waitUntilClickable()->one()->click();
				$form->invalidate();
				$form->selectTab('Tags');
				$element->checkValue($tags);
				$this->query('button:Cancel')->one()->click();
			}
			$this->query('button:Reset')->one()->click();
		}
	}

	public function getTagsInheritanceData() {
		return [
			[
				[
					'name' => 'Inheritance element',
					'tags' => [
						[
							'action' => USER_ACTION_UPDATE,
							'index' => 0,
							'tag' => 'a',
							'value' => ':a'
						],
						[
							'tag' => 'common tag on host and element',
							'value' => 'common value'
						],
						[
							'tag' => 'common tag on template and element',
							'value' => 'common value'
						],
						[
							'tag' => 'InheritanceEmptyValue',
							'value' => ''
						],
						[
							'tag' => 'InheritanceTag',
							'value' => 'InheritanceValue'
						],
						[
							'tag' => '{$MACRO:A}',
							'value' => '{$MACRO:A}'
						],
						[
							'tag' => '{$MACRO}',
							'value' => '{$MACRO}'
						]
					]
				]
			]
		];
	}

	/**
	 * Check inherited tags from host or template.
	 *
	 * @param type $data			data provider
	 * @param type $object			trigger, item, web scenario or prototype
	 * @param string $parent		test on host or template
	 * @param type $expression		trigger or trigger prototype expression
	 */
	public function checkInheritedTags($data, $object, $parent, $expression = null) {
		// Change name for element due to sql count in checkResult function.
		$data['name'] = ($parent === 'Host') ? 'Inherited '.$object.' tags on '.$parent : 'Inheritance element on '.$parent;
		// Set host or template tags data.
		$parent_tags = ($parent === 'Host') ? self::HOST_TAGS : self::TEMPLATE_TAGS;

		// Create element with tags on host or template.
		$form = $this->checkTagsCreate($data, $object, $expression);

		// Remove index and action key in tags of element.
		unset($data['tags'][0]['action'], $data['tags'][0]['index']);

		// Open created element.
		$this->page->open($this->link);
		$this->query('link', $data['name'])->waitUntilClickable()->one()->click();
		$form->selectTab('Tags');
		$tags_table = $this->query('class:tags-table')->asMultifieldTable()->waitUntilVisible()->one();

		// Check all tags (inherited from host/template and own) on created element.
		if ($object === 'web scenario') {
			$field_name = 'scenario';
		}
		else {
			$field_name = (strpos($object, 'prototype') !== false) ? str_replace(' prototype', '', $object) : $object;
		}
		$form->fill(['id:show_inherited_tags' => 'Inherited and '.$field_name.' tags']);
		$this->page->waitUntilReady();
		$tags_table->checkValue($this->prepareAllTags($data['tags'], $parent_tags));

		// Check disabled inherited tags from host or template on created element.
		$this->assertEquals($this->prepareInheritedTags($data['tags'], $parent_tags), $this->getInheritedTags());
	}

	/**
	 * Check inheritance of tags from host and template on inherited element from template.
	 *
	 * @param array    $data		data provider
	 * @param string   $object		trigger, item, web scenario or prototype
	 * @param string   $host_link	link to host
	 * @param string   $expression  trigger or trigger prototype expression
	 */
	public function checkInheritedElementTags($data, $object, $host_link, $expression = null) {
		// Create element tags on template.
		$form = $this->checkTagsCreate($data, $object, $expression);

		// Remove index and action key in tags of element.
		unset($data['tags'][0]['action'], $data['tags'][0]['index']);

		// Prepare tags that unique only for template (remove host tags from template tags).
		$host_tags = self::HOST_TAGS;
		$unique_template_tags = array_filter(self::TEMPLATE_TAGS, function ($tag) use ($host_tags) {
			foreach ($host_tags as $host_tag) {
				if ($host_tag == $tag) {
					return false;
				}
			}

			return true;
		});
		$unique_template_tags = array_values($unique_template_tags);

		// Open created element.
		$this->page->open($host_link);
		if (strpos($object, 'prototype') !== false) {
			$table = $this->query('class:list-table')->asTable()->waitUntilReady()->one();
			$table->findRow('Name', $this->template, true)->getColumn(ucfirst(str_replace(' prototype', '', $object)).'s')
					->query('tag:a')->one()->click();
		}
		$this->query('link', $data['name'])->waitUntilClickable()->one()->click();
		$form->selectTab('Tags');
		$tags_table = $this->query('class:tags-table')->asMultifieldTable()->waitUntilVisible()->one();

		// Check all tags (inherited from host and template and own) on created element.
		if ($object === 'web scenario') {
			$field_name = 'scenario';
		}
		else {
			$field_name = (strpos($object, 'prototype') !== false) ? str_replace(' prototype', '', $object) : $object;
		}
		$form->fill(['id:show_inherited_tags' => 'Inherited and '.$field_name.' tags']);
		$this->page->waitUntilReady();
		$tags_table->checkValue($this->prepareAllTags($data['tags'], array_merge(self::HOST_TAGS, self::TEMPLATE_TAGS)));

		// Check empty column "Parent templates" except for inherited unique template tags.
		foreach ($tags_table->getRows() as $row) {
			$parent_template = $row->getColumn('Parent templates')->getText();
			$current_tag = [];
			$current_tag['tag'] = $row->getColumn('Name')->getText();
			$current_tag['value'] = $row->getColumn('Value')->getText();

			if (in_array($current_tag, $unique_template_tags)) {
				$this->assertEquals($this->template, $parent_template);
			}
			else {
				$this->assertEquals('', $parent_template);
			}
		}

		// Check disabled inherited tags from host and template on created element.
		$this->assertEquals($this->prepareInheritedTags($data['tags']), $this->getInheritedTags());
	}

	/**
	 * Get inherited tags from element page.
	 *
	 * @return array
	 */
	private function getInheritedTags() {
		$inherited_tags = [];

		$tags_table = $this->query('class:tags-table')->asMultifieldTable()->one();
		$headers = $tags_table->getHeadersText();
		// Find disabled rows of host and/or template tags by disabled Name field.
		$disabled_rows = $tags_table->findRows(function ($row) {
			return $row->getColumn('Name')->children()->one()->detect()->isEnabled() === false;
		});

		foreach ($disabled_rows as $row) {
			// Check other disabled fields.
			$this->assertFalse($row->getColumn('Value')->children()->one()->detect()->isEnabled());
			$this->assertFalse($row->getColumn('')->children()->one()->detect()->isEnabled());

			$values = [];
			// Get disabled row values.
			foreach ($tags_table->getRowControls($row, $headers) as $name => $control) {
				$values[$name] = $control->getValue();
			}
			$inherited_tags[] = $values;
		}

		return $inherited_tags;
	}

	/**
	 * Prepare all tags data (inherited form host and/or template and element tags).
	 *
	 * @param array $tags				element tags data
	 * @param array $parent_tags		host and/or template tags
	 *
	 * @return array
	 */
	private function prepareAllTags($tags, $parent_tags) {
		// Prepare all tags data (inherited form host and/or template, and element tags).
		$all_tags = array_merge($parent_tags, $tags);
		// Sort reference tags array by field "tag".
		usort($all_tags, function($a, $b) {
			return strcasecmp($a['tag'], $b['tag']);
		});
		// Remove duplicated tags and reindex the keys.
		return array_values(array_unique($all_tags, SORT_REGULAR));
	}

	/**
	 * Prepare only unique inherited tags form host and/or template and remove element tags from them.
	 *
	 * @param array $tags			element tags data
	 * @param array $parent_tags	host or template tags
	 *
	 * @return array
	 */
	private function prepareInheritedTags($tags, $parent_tags = false) {
		if (!$parent_tags) {
			$host_template_tags = array_merge(self::HOST_TAGS, self::TEMPLATE_TAGS);
			$parent_tags = array_unique($host_template_tags, SORT_REGULAR);
		}

		$inherited_tags = array_filter($parent_tags, function ($tag) use ($tags) {
			foreach ($tags as $element_tag) {
				if ($element_tag == $tag) {
					return false;
				}
			}

			return true;
		});

		usort($inherited_tags, function($a, $b) {
			return strcasecmp($a['tag'], $b['tag']);
		});

		return array_values($inherited_tags);
	}

	/**
	 * Check removing tags from different objects.
	 *
	 * @param string   $object   host, template, trigger, service etc.
	 */
	public function clearTags($object) {
		$tags = (!$this->problem_tags)
				? [['tag' => '', 'value' => '']]
				: [['tag' => '', 'operator' => 'Equals', 'value' => '']];

		$data = ['name' => $this->remove_name, 'tags' => $tags];
		$this->page->login()->open($this->link);

		if ($object === 'service') {
			$table = $this->query('class:list-table')->asTable()->one()->waitUntilReady();
			$table->findRow('Name', $data['name'], true)->query(self::EDIT_BUTTON_PATH)->waitUntilClickable()->one()->click();
		}
		else {
			$this->query('link', $this->remove_name)->waitUntilPresent()->one()->click();
		}

		$locators = [
			'host' => 'id:host-form',
			'trigger' => 'name:triggersForm',
			'trigger prototype' => 'name:triggersForm',
			'item' => 'name:itemForm',
			'item prototype' => 'name:itemForm',
			'web scenario' => 'name:httpForm',
			'service' => 'id:service-form',
			'host prototype' => 'name:hostPrototypeForm',
			'template' => 'name:templatesForm'
		];

		$form = ($object === 'host' || $object === 'service')
			? COverlayDialogElement::find()->waitUntilVisible()->asForm()->one()
			: $this->query($locators[$object])->asForm()->waitUntilPresent()->one();

		if (!$this->problem_tags) {
			$form->selectTab('Tags');
			$tags_table = 'class:tags-table';
		}
		else {
			$tags_table = 'id:problem_tags';
		}

		$this->query($tags_table)->asMultifieldTable()->waitUntilPresent()->one()->clear();
		$form->submit();
		$this->page->waitUntilReady();

		$this->checkResult($data, $object, $form, 'update');
	}
}