mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
behat_config_util.php
1258 lines (1063 loc) · 44.3 KB
/
behat_config_util.php
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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Utils to set Behat config
*
* @package core
* @copyright 2016 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
require_once(__DIR__ . '/behat_command.php');
require_once(__DIR__ . '/../../testing/classes/tests_finder.php');
/**
* Behat configuration manager
*
* Creates/updates Behat config files getting tests
* and steps from Moodle codebase
*
* @package core
* @copyright 2016 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_config_util {
/**
* @var array list of features in core.
*/
private $features;
/**
* @var array list of contexts in core.
*/
private $contexts;
/**
* @var array list of theme specific contexts.
*/
private $themecontexts;
/**
* @var array list of overridden theme contexts.
*/
private $overriddenthemescontexts;
/**
* @var array list of components with tests.
*/
private $componentswithtests;
/**
* @var bool keep track of theme to return suite with all core features included or not.
*/
private $themesuitewithallfeatures = false;
/**
* @var string filter features which have tags.
*/
private $tags = '';
/**
* @var int number of parallel runs.
*/
private $parallelruns = 0;
/**
* @var int current run.
*/
private $currentrun = 0;
/**
* Set value for theme suite to include all core features. This should be used if your want all core features to be
* run with theme.
*
* @param bool $val
*/
public function set_theme_suite_to_include_core_features($val) {
$this->themesuitewithallfeatures = $val;
}
/**
* Set the value for tags, so features which are returned will be using filtered by this.
*
* @param string $tags
*/
public function set_tag_for_feature_filter($tags) {
$this->tags = $tags;
}
/**
* Set parallel run to be used for generating config.
*
* @param int $parallelruns number of parallel runs.
* @param int $currentrun current run
*/
public function set_parallel_run($parallelruns, $currentrun) {
if ($parallelruns < $currentrun) {
behat_error(BEHAT_EXITCODE_REQUIREMENT,
'Parallel runs('.$parallelruns.') should be more then current run('.$currentrun.')');
}
$this->parallelruns = $parallelruns;
$this->currentrun = $currentrun;
}
/**
* Return parallel runs
*
* @return int number of parallel runs.
*/
public function get_number_of_parallel_run() {
// Get number of parallel runs if not passed.
if (empty($this->parallelruns) && ($this->parallelruns !== false)) {
$this->parallelruns = behat_config_manager::get_parallel_test_runs();
}
return $this->parallelruns;
}
/**
* Return current run
*
* @return int current run.
*/
public function get_current_run() {
global $CFG;
// Get number of parallel runs if not passed.
if (empty($this->currentrun) && ($this->currentrun !== false) && !empty($CFG->behatrunprocess)) {
$this->currentrun = $CFG->behatrunprocess;
}
return $this->currentrun;
}
/**
* Return list of features.
*
* @param string $tags tags.
* @return array
*/
public function get_components_features($tags = '') {
global $CFG;
// If we already have a list created then just return that, as it's up-to-date.
// If tags are passed then it's a new filter of features we need.
if (!empty($this->features) && empty($tags)) {
return $this->features;
}
// Gets all the components with features.
$features = array();
$featurespaths = array();
$components = $this->get_components_with_tests();
if ($components) {
foreach ($components as $componentname => $path) {
$path = $this->clean_path($path) . self::get_behat_tests_path();
if (empty($featurespaths[$path]) && file_exists($path)) {
list($key, $featurepath) = $this->get_clean_feature_key_and_path($path);
$featurespaths[$key] = $featurepath;
}
}
foreach ($featurespaths as $path) {
$additional = glob("$path/*.feature");
$additionalfeatures = array();
foreach ($additional as $featurepath) {
list($key, $path) = $this->get_clean_feature_key_and_path($featurepath);
$additionalfeatures[$key] = $path;
}
$features = array_merge($features, $additionalfeatures);
}
}
// Optionally include features from additional directories.
if (!empty($CFG->behat_additionalfeatures)) {
$additional = array_map("realpath", $CFG->behat_additionalfeatures);
$additionalfeatures = array();
foreach ($additional as $featurepath) {
list($key, $path) = $this->get_clean_feature_key_and_path($featurepath);
$additionalfeatures[$key] = $path;
}
$features = array_merge($features, $additionalfeatures);
}
$this->features = $features;
return $this->filtered_features_with_tags($features, $tags);
}
/**
* Return feature key for featurepath
*
* @param string $featurepath
* @return array key and featurepath.
*/
public function get_clean_feature_key_and_path($featurepath) {
global $CFG;
// Fix directory path.
$featurepath = str_replace('\\', DIRECTORY_SEPARATOR, $featurepath);
$featurepath = str_replace('/', DIRECTORY_SEPARATOR, $featurepath);
if (testing_is_cygwin()) {
$featurepath = str_replace('\\', '/', $featurepath);
}
$key = basename($featurepath, '.feature');
// Get relative path.
$featuredirname = str_replace($CFG->dirroot . DIRECTORY_SEPARATOR , '', $featurepath);
// Get 5 levels of feature path to ensure we have a unique key.
for ($i = 0; $i < 5; $i++) {
if (($featuredirname = dirname($featuredirname)) && $featuredirname !== '.') {
if ($basename = basename($featuredirname)) {
$key .= '_' . $basename;
}
}
}
return array($key, $featurepath);
}
/**
* Get component contexts.
*
* @param string $component component name.
* @return array
*/
private function get_component_contexts($component) {
if (empty($component)) {
return $this->contexts;
}
$componentcontexts = array();
foreach ($this->contexts as $key => $path) {
if ($component == '' || $component === $key) {
$componentcontexts[$key] = $path;
}
}
return $componentcontexts;
}
/**
* Gets the list of Moodle behat contexts
*
* Class name as a key and the filepath as value
*
* Externalized from update_config_file() to use
* it from the steps definitions web interface
*
* @param string $component Restricts the obtained steps definitions to the specified component
* @return array
*/
public function get_components_contexts($component = '') {
// If we already have a list created then just return that, as it's up-to-date.
if (!empty($this->contexts)) {
return $this->get_component_contexts($component);
}
$components = $this->get_components_with_tests();
$this->contexts = array();
foreach ($components as $componentname => $componentpath) {
$componentpath = self::clean_path($componentpath);
if (!file_exists($componentpath . self::get_behat_tests_path())) {
continue;
}
$diriterator = new DirectoryIterator($componentpath . self::get_behat_tests_path());
$regite = new RegexIterator($diriterator, '|behat_.*\.php$|');
// All behat_*.php inside self::get_behat_tests_path() are added as steps definitions files.
foreach ($regite as $file) {
$key = $file->getBasename('.php');
$this->contexts[$key] = $file->getPathname();
}
}
return $this->get_component_contexts($component);
}
/**
* Behat config file specifing the main context class,
* the required Behat extensions and Moodle test wwwroot.
*
* @param array $features The system feature files
* @param array $contexts The system steps definitions
* @param string $tags filter features with specified tags.
* @param int $parallelruns number of parallel runs.
* @param int $currentrun current run for which config file is needed.
* @return string
*/
public function get_config_file_contents($features = '', $contexts = '', $tags = '', $parallelruns = 0, $currentrun = 0) {
global $CFG;
// Set current run and parallel run.
if (!empty($parallelruns) && !empty($currentrun)) {
$this->set_parallel_run($parallelruns, $currentrun);
}
// If tags defined then use them. This is for BC.
if (!empty($tags)) {
$this->set_tag_for_feature_filter($tags);
}
// If features not passed then get it. Empty array means we don't need to include features.
if (empty($features) && !is_array($features)) {
$features = $this->get_components_features();
} else {
$this->features = $features;
}
// If stepdefinitions not passed then get the list.
if (empty($contexts)) {
$this->get_components_contexts();
} else {
$this->contexts = $contexts;
}
// We require here when we are sure behat dependencies are available.
require_once($CFG->dirroot . '/vendor/autoload.php');
$config = $this->build_config();
$config = $this->merge_behat_config($config);
$config = $this->merge_behat_profiles($config);
return Symfony\Component\Yaml\Yaml::dump($config, 10, 2);
}
/**
* Search feature files for set of tags.
*
* @param array $features set of feature files.
* @param string $tags list of tags (currently support && only.)
* @return array filtered list of feature files with tags.
*/
public function filtered_features_with_tags($features = '', $tags = '') {
// This is for BC. Features if not passed then we already have a list in this object.
if (empty($features)) {
$features = $this->features;
}
// If no tags defined then return full list.
if (empty($tags) && empty($this->tags)) {
return $features;
}
// If no tags passed by the caller, then it's already set.
if (empty($tags)) {
$tags = $this->tags;
}
$newfeaturelist = array();
// Split tags in and and or.
$tags = explode('&&', $tags);
$andtags = array();
$ortags = array();
foreach ($tags as $tag) {
// Explode all tags seperated by , and add it to ortags.
$ortags = array_merge($ortags, explode(',', $tag));
// And tags will be the first one before comma(,).
$andtags[] = preg_replace('/,.*/', '', $tag);
}
foreach ($features as $featurefile) {
$contents = file_get_contents($featurefile);
$includefeature = true;
foreach ($andtags as $tag) {
// If negitive tag, then ensure it don't exist.
if (strpos($tag, '~') !== false) {
$tag = substr($tag, 1);
if ($contents && strpos($contents, $tag) !== false) {
$includefeature = false;
break;
}
} else if ($contents && strpos($contents, $tag) === false) {
$includefeature = false;
break;
}
}
// If feature not included then check or tags.
if (!$includefeature && !empty($ortags)) {
foreach ($ortags as $tag) {
if ($contents && (strpos($tag, '~') === false) && (strpos($contents, $tag) !== false)) {
$includefeature = true;
break;
}
}
}
if ($includefeature) {
$newfeaturelist[] = $featurefile;
}
}
return $newfeaturelist;
}
/**
* Build config for behat.yml.
*
* @param int $parallelruns how many parallel runs feature needs to be divided.
* @param int $currentrun current run for which features should be returned.
* @return array
*/
protected function build_config($parallelruns = 0, $currentrun = 0) {
global $CFG;
if (!empty($parallelruns) && !empty($currentrun)) {
$this->set_parallel_run($parallelruns, $currentrun);
} else {
$currentrun = $this->get_current_run();
$parallelruns = $this->get_number_of_parallel_run();
}
$selenium2wdhost = array('wd_host' => 'http://localhost:4444/wd/hub');
// If parallel run, then set wd_host if specified.
if (!empty($currentrun) && !empty($parallelruns)) {
// Set proper selenium2 wd_host if defined.
if (!empty($CFG->behat_parallel_run[$currentrun - 1]['wd_host'])) {
$selenium2wdhost = array('wd_host' => $CFG->behat_parallel_run[$currentrun - 1]['wd_host']);
}
}
// It is possible that it has no value as we don't require a full behat setup to list the step definitions.
if (empty($CFG->behat_wwwroot)) {
$CFG->behat_wwwroot = 'http://itwillnotbeused.com';
}
$suites = $this->get_behat_suites($parallelruns, $currentrun);
$overriddenthemescontexts = $this->get_overridden_theme_contexts();
if (!empty($overriddenthemescontexts)) {
$allcontexts = array_merge($this->contexts, $overriddenthemescontexts);
} else {
$allcontexts = $this->contexts;
}
// Remove selectors from step definitions.
$themes = $this->get_list_of_themes();
foreach ($themes as $theme) {
$selectorclass = self::get_behat_theme_selector_override_classname($theme);
if (isset($allcontexts[$selectorclass])) {
unset($allcontexts[$selectorclass]);
}
}
// Comments use black color, so failure path is not visible. Using color other then black/white is safer.
// https://github.com/Behat/Behat/pull/628.
$config = array(
'default' => array(
'formatters' => array(
'moodle_progress' => array(
'output_styles' => array(
'comment' => array('magenta'))
)
),
'suites' => $suites,
'extensions' => array(
'Behat\MinkExtension' => array(
'base_url' => $CFG->behat_wwwroot,
'goutte' => null,
'selenium2' => $selenium2wdhost
),
'Moodle\BehatExtension' => array(
'moodledirroot' => $CFG->dirroot,
'steps_definitions' => $allcontexts,
)
)
)
);
return $config;
}
/**
* Divide features between the runs and return list.
*
* @param array $features list of features to be divided.
* @param int $parallelruns how many parallel runs feature needs to be divided.
* @param int $currentrun current run for which features should be returned.
* @return array
*/
protected function get_features_for_the_run($features, $parallelruns, $currentrun) {
// If no features are passed then just return.
if (empty($features)) {
return $features;
}
$allocatedfeatures = $features;
// If parallel run, then only divide features.
if (!empty($currentrun) && !empty($parallelruns)) {
// Attempt to split into weighted buckets using timing information, if available.
if ($alloc = $this->profile_guided_allocate($features, max(1, $parallelruns), $currentrun)) {
$allocatedfeatures = $alloc;
} else {
// Divide the list of feature files amongst the parallel runners.
// Pull out the features for just this worker.
if (count($features)) {
$features = array_chunk($features, ceil(count($features) / max(1, $parallelruns)));
// Check if there is any feature file for this process.
if (!empty($features[$currentrun - 1])) {
$allocatedfeatures = $features[$currentrun - 1];
} else {
$allocatedfeatures = array();
}
}
}
}
return $allocatedfeatures;
}
/**
* Parse $CFG->behat_profile and return the array with required config structure for behat.yml.
*
* $CFG->behat_profiles = array(
* 'profile' = array(
* 'browser' => 'firefox',
* 'tags' => '@javascript',
* 'wd_host' => 'http://127.0.0.1:4444/wd/hub',
* 'capabilities' => array(
* 'platform' => 'Linux',
* 'version' => 44
* )
* )
* );
*
* @param string $profile profile name
* @param array $values values for profile.
* @return array
*/
protected function get_behat_profile($profile, $values) {
// Values should be an array.
if (!is_array($values)) {
return array();
}
// Check suite values.
$behatprofilesuites = array();
// Fill tags information.
if (isset($values['tags'])) {
$behatprofilesuites = array(
'suites' => array(
'default' => array(
'filters' => array(
'tags' => $values['tags'],
)
)
)
);
}
// Selenium2 config values.
$behatprofileextension = array();
$seleniumconfig = array();
if (isset($values['browser'])) {
$seleniumconfig['browser'] = $values['browser'];
}
if (isset($values['wd_host'])) {
$seleniumconfig['wd_host'] = $values['wd_host'];
}
if (isset($values['capabilities'])) {
$seleniumconfig['capabilities'] = $values['capabilities'];
}
if (!empty($seleniumconfig)) {
$behatprofileextension = array(
'extensions' => array(
'Behat\MinkExtension' => array(
'selenium2' => $seleniumconfig,
)
)
);
}
return array($profile => array_merge($behatprofilesuites, $behatprofileextension));
}
/**
* Attempt to split feature list into fairish buckets using timing information, if available.
* Simply add each one to lightest buckets until all files allocated.
* PGA = Profile Guided Allocation. I made it up just now.
* CAUTION: workers must agree on allocation, do not be random anywhere!
*
* @param array $features Behat feature files array
* @param int $nbuckets Number of buckets to divide into
* @param int $instance Index number of this instance
* @return array|bool Feature files array, sorted into allocations
*/
public function profile_guided_allocate($features, $nbuckets, $instance) {
$behattimingfile = defined('BEHAT_FEATURE_TIMING_FILE') &&
@filesize(BEHAT_FEATURE_TIMING_FILE) ? BEHAT_FEATURE_TIMING_FILE : false;
if (!$behattimingfile || !$behattimingdata = @json_decode(file_get_contents($behattimingfile), true)) {
// No data available, fall back to relying on steps data.
$stepfile = "";
if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) {
$stepfile = BEHAT_FEATURE_STEP_FILE;
}
// We should never get this. But in case we can't do this then fall back on simple splitting.
if (empty($stepfile) || !$behattimingdata = @json_decode(file_get_contents($stepfile), true)) {
return false;
}
}
arsort($behattimingdata); // Ensure most expensive is first.
$realroot = realpath(__DIR__.'/../../../').'/';
$defaultweight = array_sum($behattimingdata) / count($behattimingdata);
$weights = array_fill(0, $nbuckets, 0);
$buckets = array_fill(0, $nbuckets, array());
$totalweight = 0;
// Re-key the features list to match timing data.
foreach ($features as $k => $file) {
$key = str_replace($realroot, '', $file);
$features[$key] = $file;
unset($features[$k]);
if (!isset($behattimingdata[$key])) {
$behattimingdata[$key] = $defaultweight;
}
}
// Sort features by known weights; largest ones should be allocated first.
$behattimingorder = array();
foreach ($features as $key => $file) {
$behattimingorder[$key] = $behattimingdata[$key];
}
arsort($behattimingorder);
// Finally, add each feature one by one to the lightest bucket.
foreach ($behattimingorder as $key => $weight) {
$file = $features[$key];
$lightbucket = array_search(min($weights), $weights);
$weights[$lightbucket] += $weight;
$buckets[$lightbucket][] = $file;
$totalweight += $weight;
}
if ($totalweight && !defined('BEHAT_DISABLE_HISTOGRAM') && $instance == $nbuckets && !defined('PHPUNIT_TEST')) {
echo "Bucket weightings:\n";
foreach ($weights as $k => $weight) {
echo $k + 1 . ": " . str_repeat('*', 70 * $nbuckets * $weight / $totalweight) . PHP_EOL;
}
}
// Return the features for this worker.
return $buckets[$instance - 1];
}
/**
* Overrides default config with local config values
*
* array_merge does not merge completely the array's values
*
* @param mixed $config The node of the default config
* @param mixed $localconfig The node of the local config
* @return mixed The merge result
*/
public function merge_config($config, $localconfig) {
if (!is_array($config) && !is_array($localconfig)) {
return $localconfig;
}
// Local overrides also deeper default values.
if (is_array($config) && !is_array($localconfig)) {
return $localconfig;
}
foreach ($localconfig as $key => $value) {
// If defaults are not as deep as local values let locals override.
if (!is_array($config)) {
unset($config);
}
// Add the param if it doesn't exists or merge branches.
if (empty($config[$key])) {
$config[$key] = $value;
} else {
$config[$key] = $this->merge_config($config[$key], $localconfig[$key]);
}
}
return $config;
}
/**
* Merges $CFG->behat_config with the one passed.
*
* @param array $config existing config.
* @return array merged config with $CFG->behat_config
*/
public function merge_behat_config($config) {
global $CFG;
// In case user defined overrides respect them over our default ones.
if (!empty($CFG->behat_config)) {
foreach ($CFG->behat_config as $profile => $values) {
$config = $this->merge_config($config, $this->get_behat_config_for_profile($profile, $values));
}
}
return $config;
}
/**
* Parse $CFG->behat_config and return the array with required config structure for behat.yml
*
* @param string $profile profile name
* @param array $values values for profile
* @return array
*/
public function get_behat_config_for_profile($profile, $values) {
// Only add profile which are compatible with Behat 3.x
// Just check if any of Bheat 2.5 config is set. Not checking for 3.x as it might have some other configs
// Like : rerun_cache etc.
if (!isset($values['filters']['tags']) && !isset($values['extensions']['Behat\MinkExtension\Extension'])) {
return array($profile => $values);
}
// Parse 2.5 format and get related values.
$oldconfigvalues = array();
if (isset($values['extensions']['Behat\MinkExtension\Extension'])) {
$extensionvalues = $values['extensions']['Behat\MinkExtension\Extension'];
if (isset($extensionvalues['selenium2']['browser'])) {
$oldconfigvalues['browser'] = $extensionvalues['selenium2']['browser'];
}
if (isset($extensionvalues['selenium2']['wd_host'])) {
$oldconfigvalues['wd_host'] = $extensionvalues['selenium2']['wd_host'];
}
if (isset($extensionvalues['capabilities'])) {
$oldconfigvalues['capabilities'] = $extensionvalues['capabilities'];
}
}
if (isset($values['filters']['tags'])) {
$oldconfigvalues['tags'] = $values['filters']['tags'];
}
if (!empty($oldconfigvalues)) {
behat_config_manager::$autoprofileconversion = true;
return $this->get_behat_profile($profile, $oldconfigvalues);
}
// If nothing set above then return empty array.
return array();
}
/**
* Merges $CFG->behat_profiles with the one passed.
*
* @param array $config existing config.
* @return array merged config with $CFG->behat_profiles
*/
public function merge_behat_profiles($config) {
global $CFG;
// Check for Moodle custom ones.
if (!empty($CFG->behat_profiles) && is_array($CFG->behat_profiles)) {
foreach ($CFG->behat_profiles as $profile => $values) {
$config = $this->merge_config($config, $this->get_behat_profile($profile, $values));
}
}
return $config;
}
/**
* Cleans the path returned by get_components_with_tests() to standarize it
*
* @see tests_finder::get_all_directories_with_tests() it returns the path including /tests/
* @param string $path
* @return string The string without the last /tests part
*/
public final function clean_path($path) {
$path = rtrim($path, DIRECTORY_SEPARATOR);
$parttoremove = DIRECTORY_SEPARATOR . 'tests';
$substr = substr($path, strlen($path) - strlen($parttoremove));
if ($substr == $parttoremove) {
$path = substr($path, 0, strlen($path) - strlen($parttoremove));
}
return rtrim($path, DIRECTORY_SEPARATOR);
}
/**
* The relative path where components stores their behat tests
*
* @return string
*/
public static final function get_behat_tests_path() {
return DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'behat';
}
/**
* Return context name of behat_theme selector to use.
*
* @param string $themename name of the theme.
* @param bool $includeclass if class should be included.
* @return string
*/
public static final function get_behat_theme_selector_override_classname($themename, $includeclass = false) {
global $CFG;
$overridebehatclassname = 'behat_theme_'.$themename.'_behat_selectors';
if ($includeclass) {
$themeoverrideselector = $CFG->dirroot . DIRECTORY_SEPARATOR . 'theme' . DIRECTORY_SEPARATOR . $themename .
self::get_behat_tests_path() . DIRECTORY_SEPARATOR . $overridebehatclassname . '.php';
if (file_exists($themeoverrideselector)) {
require_once($themeoverrideselector);
}
}
return $overridebehatclassname;
}
/**
* List of components which contain behat context or features.
*
* @return array
*/
private function get_components_with_tests() {
if (empty($this->componentswithtests)) {
$this->componentswithtests = tests_finder::get_components_with_tests('behat');
}
return $this->componentswithtests;
}
/**
* Remove list of blacklisted features from the feature list.
*
* @param array $features list of original features.
* @param array|string $blacklist list of features which needs to be removed.
* @return array features - blacklisted features.
*/
protected function remove_blacklisted_features_from_list($features, $blacklist) {
// If no blacklist passed then return.
if (empty($blacklist)) {
return $features;
}
// If there is no feature in suite then just return what was passed.
if (empty($features)) {
return $features;
}
if (!is_array($blacklist)) {
$blacklist = array($blacklist);
}
// Remove blacklisted features.
foreach ($blacklist as $blacklistpath) {
list($key, $featurepath) = $this->get_clean_feature_key_and_path($blacklistpath);
if (isset($features[$key])) {
$features[$key] = null;
unset($features[$key]);
} else if (empty($this->tags)) {
// If tags not set, then ensure we have a blacklisted feature in core. Else, let user know that
// blacklisted feature is invalid.
$featurestocheck = $this->get_components_features();
if (!isset($featurestocheck[$key]) && !defined('PHPUNIT_TEST')) {
behat_error(BEHAT_EXITCODE_REQUIREMENT, 'Blacklisted feature "' . $blacklistpath . '" not found.');
}
}
}
return $features;
}
/**
* Return list of behat suites. Multiple suites are returned if theme
* overrides default step definitions/features.
*
* @param int $parallelruns number of parallel runs
* @param int $currentrun current run.
* @return array list of suites.
*/
protected function get_behat_suites($parallelruns = 0, $currentrun = 0) {
$features = $this->get_components_features();
$contexts = $this->get_components_contexts();
// Get number of parallel runs and current run.
if (!empty($parallelruns) && !empty($currentrun)) {
$this->set_parallel_run($parallelruns, $currentrun);
} else {
$parallelruns = $this->get_number_of_parallel_run();
$currentrun = $this->get_current_run();;
}
$blacklistedfeatures = array();
$themefeatures = array();
$themesuitecontexts = array();
$themecontexts = array();
$themes = $this->get_list_of_themes();
// Create list of theme suite features and contexts.
foreach ($themes as $theme) {
// Get theme features.
list($blacklistedfeatures[$theme], $themefeatures[$theme]) = $this->get_behat_features_for_theme($theme);
list($themecontexts[$theme], $themesuitecontexts[$theme]) = $this->get_behat_contexts_for_theme($theme);
}
// Remove list of theme features for default suite, as default suite should not run theme specific features.
foreach ($themefeatures as $removethemefeatures) {
$features = $this->remove_blacklisted_features_from_list($features, $removethemefeatures);
}
// Remove list of theme features for default suite, as default suite should not run theme specific features.
foreach ($themecontexts as $theme => $themeblacklistcontexts) {
if ($themeblacklistcontexts) {
foreach ($themeblacklistcontexts as $c) {
// Remove theme specific contexts from default contexts.
unset($contexts[$c]);
// Remove theme specific contexts from other themes.
foreach ($themes as $currenttheme) {
if (($currenttheme != $theme) && isset($themesuitecontexts[$currenttheme][$c])) {
unset($themesuitecontexts[$currenttheme][$c]);
}
}
}
}
}
// Return sub-set of features if parallel run.
$featuresforrun = $this->get_features_for_the_run($features, $parallelruns, $currentrun);
// Default suite.
$suites = array(
'default' => array(
'paths' => array_values($featuresforrun),
'contexts' => array_keys($contexts),
)
);
// Set suite for each theme.
foreach ($themes as $theme) {
// Get list of features which will be included in theme.
// If theme suite with all features is set, then we want all core features to be part of theme suite.
if ($this->themesuitewithallfeatures) {
// If there is no theme specific feature. Then it's just core features.
if (empty($themefeatures[$theme])) {
$themesuitefeatures = $features;
} else {
$themesuitefeatures = array_merge($features, $themefeatures[$theme]);
}
} else {
$themesuitefeatures = $themefeatures[$theme];
}
// Remove blacklisted features.
$themesuitefeatures = $this->remove_blacklisted_features_from_list($themesuitefeatures, $blacklistedfeatures[$theme]);
// Return sub-set of features if parallel run.
$themesuitefeatures = $this->get_features_for_the_run($themesuitefeatures, $parallelruns, $currentrun);