MDL-64557 tool_behat: fix broken test
[moodle.git] / admin / tool / behat / tests / manager_util_test.php
CommitLineData
5aa9b5ce
RT
1<?php
2// @codingStandardsIgnoreFile
3// @codeCoverageIgnoreStart
4
5// This file is part of Moodle - http://moodle.org/
6//
7// Moodle is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Moodle is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19
20/**
21 * Unit tests for behat manager.
22 *
23 * @package tool_behat
24 * @copyright 2016 Rajesh Taneja
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->dirroot . '/' . $CFG->admin .'/tool/behat/locallib.php');
32require_once($CFG->libdir . '/behat/classes/util.php');
33require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
34
35/**
36 * Behat manager tests.
37 *
38 * @package tool_behat
39 * @copyright 2016 Rajesh Taneja
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class tool_behat_manager_util_testcase extends advanced_testcase {
43
a1d92ef2
RT
44 /** @var array Fixtures features which are available. */
45 private $featurepaths = array(
46 'default' => array(
47 'test_1.feature',
48 'test_2.feature',
49 ),
50 'withfeatures' => array(
51 'theme_test_1.feature',
52 'theme_test_2.feature',
53 'theme_test_3.feature',
54 'theme_test_4.feature',
55 'theme_test_5.feature',
56 ),
57 'nofeatures' => array()
58 );
59
60 /** @var array Fixture contexts which are available */
61 private $contextspath = array(
62 'default' => array(
63 'behat_test_context_1',
4422ffc5
RT
64 'behat_test_context_2',
65 'behat_theme_defaulttheme_test_context_1'
a1d92ef2
RT
66 ),
67 'withfeatures' => array(
68 'behat_test_context_2',
69 'behat_theme_withfeatures_test_context_2',
70 'behat_theme_withfeatures_behat_test_context_1'
71 ),
72 'nofeatures' => array(
73 'behat_test_context_1',
74 'behat_theme_nofeatures_test_context_1',
75 'behat_theme_nofeatures_behat_test_context_2'
76 ),
77 );
78
95e1d5e5 79 /** @var array List of core features. */
4422ffc5 80 private $corefeatures = array('test_1_core_fixtures_tests_behat_tool' => __DIR__.'/fixtures/core/test_1.feature',
95e1d5e5 81 'test_2_core_fixtures_tests_behat_tool' => __DIR__.'/fixtures/core/test_2.feature');
a1d92ef2 82
95e1d5e5 83 /** @var array List of core contexts. */
a1d92ef2
RT
84 private $corecontexts = array('behat_test_context_1' => __DIR__.'/fixtures/core/behat_test_context_1.php',
85 'behat_test_context_2' => __DIR__.'/fixtures/core/behat_test_context_2.php');
86
87 /**
88 * Setup test.
89 */
90 public function setup() {
91 global $CFG;
92
93 $this->resetAfterTest();
94 $CFG->behat_wwwroot = 'http://example.com/behat';
95 }
96
97 /**
98 * Utility function to build mock object.
99 *
100 * @param behat_config_util $behatconfigutil
101 * @param bool $notheme
102 * @return mixed
103 */
104 private function get_behat_config_util($behatconfigutil, $notheme = false) {
105 // Create a map of arguments to return values.
106 $map = array(
107 array('withfeatures', __DIR__.'/fixtures/theme/withfeatures'),
4422ffc5
RT
108 array('nofeatures', __DIR__.'/fixtures/theme/nofeatures'),
109 array('defaulttheme', __DIR__.'/fixtures/theme/defaulttheme'),
a1d92ef2
RT
110 );
111
5aa9b5ce 112 // List of themes is const for test.
a1d92ef2 113 if ($notheme) {
4422ffc5 114 $themelist = array('defaulttheme');
a1d92ef2 115 } else {
4422ffc5 116 $themelist = array('withfeatures', 'nofeatures', 'defaulttheme');
a1d92ef2
RT
117 }
118
b04ca8c8 119 $behatconfigutil->expects($this->any())
5aa9b5ce 120 ->method('get_list_of_themes')
a1d92ef2 121 ->will($this->returnValue($themelist));
5aa9b5ce 122
b04ca8c8
RT
123 // Theme directory for testing.
124 $behatconfigutil->expects($this->any())
a1d92ef2
RT
125 ->method('get_theme_test_directory')
126 ->will($this->returnValueMap($map));
b04ca8c8 127
4422ffc5
RT
128 $behatconfigutil->expects($this->any())
129 ->method('get_default_theme')
130 ->will($this->returnValue('defaulttheme'));
131
a1d92ef2 132 return $behatconfigutil;
5aa9b5ce
RT
133 }
134
135 /**
136 * Behat config for single run.
5aa9b5ce
RT
137 */
138 public function test_get_config_file_contents_with_single_run() {
5aa9b5ce 139
b04ca8c8 140 $mockbuilder = $this->getMockBuilder('behat_config_util');
4422ffc5 141 $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
5aa9b5ce 142
b04ca8c8
RT
143 $behatconfigutil = $mockbuilder->getMock();
144
a1d92ef2 145 $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
4422ffc5 146 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
b04ca8c8 147
a1d92ef2 148 // Two suites should be present.
b04ca8c8 149 $suites = $config['default']['suites'];
a1d92ef2
RT
150 $this->assertCount(3, $suites);
151
152 // Check features.
153 foreach ($this->featurepaths as $themename => $paths) {
154 $this->assertCount(count($paths), $suites[$themename]['paths']);
155
156 foreach ($paths as $key => $feature) {
157 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
158 }
159 }
160
161 // Check contexts.
162 foreach ($this->contextspath as $themename => $paths) {
163 $this->assertCount(count($paths), $suites[$themename]['contexts']);
164
165 foreach ($paths as $key => $context) {
166 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
167 }
168 }
169
4422ffc5
RT
170 // There are 7 step definitions.
171 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
5aa9b5ce
RT
172 }
173
174 /**
a1d92ef2 175 * Behat config for single run with no theme installed.
5aa9b5ce 176 */
a1d92ef2 177 public function test_get_config_file_contents_with_single_run_no_theme() {
5aa9b5ce 178
a1d92ef2 179 $mockbuilder = $this->getMockBuilder('behat_config_util');
4422ffc5 180 $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
5aa9b5ce 181
a1d92ef2
RT
182 $behatconfigutil = $mockbuilder->getMock();
183
184 $behatconfigutil = $this->get_behat_config_util($behatconfigutil, true);
4422ffc5 185 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
a1d92ef2
RT
186
187 // Two suites should be present.
188 $suites = $config['default']['suites'];
189 $this->assertCount(1, $suites);
190
191 $featurepaths = array(
192 'default' => array(
193 'test_1.feature',
194 'test_2.feature',
195 )
196 );
197
198 $contextspath = array(
199 'default' => array(
200 'behat_test_context_1',
4422ffc5
RT
201 'behat_test_context_2',
202 'behat_theme_defaulttheme_test_context_1',
a1d92ef2
RT
203 )
204 );
205
206 // Check features.
207 foreach ($featurepaths as $themename => $paths) {
208 $this->assertCount(count($paths), $suites[$themename]['paths']);
209
210 foreach ($paths as $key => $feature) {
211 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
212 }
213 }
214
215 // Check contexts.
216 foreach ($contextspath as $themename => $paths) {
217 $this->assertCount(count($paths), $suites[$themename]['contexts']);
218
219 foreach ($paths as $key => $context) {
220 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
221 }
222 }
223
4422ffc5
RT
224 // There are 3 step definitions.
225 $this->assertCount(3, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
5aa9b5ce
RT
226 }
227
228 /**
a1d92ef2 229 * Behat config for parallel run.
5aa9b5ce 230 */
a1d92ef2 231 public function test_get_config_file_contents_with_parallel_run() {
5aa9b5ce 232
a1d92ef2 233 $mockbuilder = $this->getMockBuilder('behat_config_util');
4422ffc5 234 $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
5aa9b5ce 235
a1d92ef2
RT
236 $behatconfigutil = $mockbuilder->getMock();
237
238 $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
239
240 // Test first run out of 3.
4422ffc5 241 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts, '', 3, 1);
a1d92ef2
RT
242 // Three suites should be present.
243 $suites = $config['default']['suites'];
244 $this->assertCount(3, $suites);
245 // There is first feature file in first run.
246 $featurepaths = array(
247 'default' => array('test_1.feature'),
248 'withfeatures' => array('theme_test_1.feature', 'theme_test_2.feature'),
249 'nofeatures' => array()
250 );
251 // Check features.
252 foreach ($featurepaths as $themename => $paths) {
253 $this->assertCount(count($paths), $suites[$themename]['paths']);
254
255 foreach ($paths as $key => $feature) {
256 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
257 }
258 }
4422ffc5 259
a1d92ef2
RT
260 // Check contexts.
261 foreach ($this->contextspath as $themename => $paths) {
262 $this->assertCount(count($paths), $suites[$themename]['contexts']);
263
264 foreach ($paths as $key => $context) {
265 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
266 }
267 }
4422ffc5
RT
268 // There are 7 step definitions.
269 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
a1d92ef2
RT
270
271 // Test second run out of 3.
272 $config = $behatconfigutil->get_config_file_contents('', '', '', 3, 2);
273 // Three suites should be present.
274 $suites = $config['default']['suites'];
275 $this->assertCount(3, $suites);
276 // There is second feature file in first run.
277 $featurepaths = array(
278 'default' => array('test_2.feature'),
279 'withfeatures' => array('theme_test_3.feature', 'theme_test_4.feature'),
280 'nofeatures' => array()
281 );
282 // Check features.
283 foreach ($featurepaths as $themename => $paths) {
284 $this->assertCount(count($paths), $suites[$themename]['paths']);
285
286 foreach ($paths as $key => $feature) {
287 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
288 }
289 }
290 // Check contexts.
291 foreach ($this->contextspath as $themename => $paths) {
292 $this->assertCount(count($paths), $suites[$themename]['contexts']);
293
294 foreach ($paths as $key => $context) {
295 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
296 }
297 }
4422ffc5
RT
298 // There are 7 step definitions.
299 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
a1d92ef2
RT
300
301 // Test third run out of 3.
302 $config = $behatconfigutil->get_config_file_contents('', '', '', 3, 3);
303 $suites = $config['default']['suites'];
304 $this->assertCount(3, $suites);
305 // There is second feature file in first run.
306 $featurepaths = array(
307 'default' => array(),
308 'withfeatures' => array('theme_test_5.feature'),
309 'nofeatures' => array()
310 );
311 // Check features.
312 foreach ($featurepaths as $themename => $paths) {
313 $this->assertCount(count($paths), $suites[$themename]['paths']);
314
315 foreach ($paths as $key => $feature) {
316 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
317 }
318 }
319 // Check contexts.
320 foreach ($this->contextspath as $themename => $paths) {
321 $this->assertCount(count($paths), $suites[$themename]['contexts']);
322
323 foreach ($paths as $key => $context) {
324 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
325 }
326 }
4422ffc5
RT
327 // There are 7 step definitions.
328 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
5aa9b5ce
RT
329 }
330
331 /**
332 * Behat config for parallel run.
333 */
a1d92ef2 334 public function test_get_config_file_contents_with_parallel_run_optimize_tags() {
5aa9b5ce 335
a1d92ef2 336 $mockbuilder = $this->getMockBuilder('behat_config_util');
4422ffc5 337 $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
5aa9b5ce 338
a1d92ef2 339 $behatconfigutil = $mockbuilder->getMock();
5aa9b5ce 340
a1d92ef2 341 $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
5aa9b5ce 342
a1d92ef2 343 // Test first run out of 3.
4422ffc5 344 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts, '@commontag', 3, 1);
a1d92ef2
RT
345
346 // Three suites should be present.
347 $suites = $config['default']['suites'];
348 $this->assertCount(3, $suites);
349 // There is first feature file in first run.
350 $featurepaths = array(
351 'default' => array('test_1.feature'),
352 'withfeatures' => array('theme_test_1.feature', 'theme_test_3.feature'),
353 'nofeatures' => array()
354 );
355 // Check features.
356 foreach ($featurepaths as $themename => $paths) {
357 $this->assertCount(count($paths), $suites[$themename]['paths']);
358
359 foreach ($paths as $key => $feature) {
360 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
361 }
362 }
363 // Check contexts.
364 foreach ($this->contextspath as $themename => $paths) {
365 $this->assertCount(count($paths), $suites[$themename]['contexts']);
366
367 foreach ($paths as $key => $context) {
368 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
369 }
370 }
4422ffc5
RT
371 // There are 7step definitions.
372 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
a1d92ef2
RT
373
374 // Test second run out of 3.
375 $config = $behatconfigutil->get_config_file_contents('', '', '@commontag', 3, 2);
376
377 // Three suites should be present.
378 $suites = $config['default']['suites'];
379 $this->assertCount(3, $suites);
380 // There is second feature file in first run.
381 $featurepaths = array(
382 'default' => array('test_2.feature'),
383 'withfeatures' => array('theme_test_2.feature', 'theme_test_4.feature'),
384 'nofeatures' => array()
385 );
386 // Check features.
387 foreach ($featurepaths as $themename => $paths) {
388 $this->assertCount(count($paths), $suites[$themename]['paths']);
389
390 foreach ($paths as $key => $feature) {
391 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
392 }
393 }
394 // Check contexts.
395 foreach ($this->contextspath as $themename => $paths) {
396 $this->assertCount(count($paths), $suites[$themename]['contexts']);
397
398 foreach ($paths as $key => $context) {
399 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
400 }
401 }
4422ffc5
RT
402 // There are 7 step definitions.
403 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
a1d92ef2
RT
404
405 // Test third run out of 3.
406 $config = $behatconfigutil->get_config_file_contents('', '', '', 3, 3);
407 $suites = $config['default']['suites'];
408 $this->assertCount(3, $suites);
409 // There is second feature file in first run.
410 $featurepaths = array(
411 'default' => array(),
412 'withfeatures' => array('theme_test_5.feature'),
413 'nofeatures' => array()
414 );
415 // Check features.
416 foreach ($featurepaths as $themename => $paths) {
417 $this->assertCount(count($paths), $suites[$themename]['paths']);
418
419 foreach ($paths as $key => $feature) {
420 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
421 }
422 }
423 // Check contexts.
424 foreach ($this->contextspath as $themename => $paths) {
425 $this->assertCount(count($paths), $suites[$themename]['contexts']);
426
427 foreach ($paths as $key => $context) {
428 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
429 }
430 }
4422ffc5
RT
431 // There are 7 step definitions.
432 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
5aa9b5ce
RT
433 }
434
435 /**
436 * Test if clean features key and path is returned.
437 * @dataProvider clean_features_path_list
438 */
439 public function test_get_clean_feature_key_and_path($featurepath, $key, $cleanfeaturepath) {
b4bc4286
RT
440 global $CFG;
441
442 // This is a hack so directory name is correctly detected in tests.
05fd5c4d
DP
443 //FIXME: MDL-55722 work out why this is necessary..
444 $oldroot = $CFG->dirroot;
b4bc4286 445 $CFG->dirroot = 'C:';
5aa9b5ce 446
a1d92ef2
RT
447 $behatconfigutil = new behat_config_util();
448
5aa9b5ce 449 // Fix expected directory path for OS.
1b319d3d 450 $cleanfeaturepath = testing_cli_fix_directory_separator($cleanfeaturepath);
5aa9b5ce
RT
451
452 list($retkey, $retcleanfeaturepath) = $behatconfigutil->get_clean_feature_key_and_path($featurepath);
453
454 $this->assertEquals($key, $retkey);
455 $this->assertEquals($cleanfeaturepath, $retcleanfeaturepath);
05fd5c4d
DP
456 //FIXME: MDL-55722 work out why this is necessary..
457 $CFG->dirroot = $oldroot;
5aa9b5ce
RT
458 }
459
460 public function clean_features_path_list() {
461 return array(
462 ['/home/test/this/that/test/behat/mod_assign.feature', 'mod_assign_behat_test_that_this_test', '/home/test/this/that/test/behat/mod_assign.feature'],
463 ['/home/this/that/test/behat/mod_assign.feature', 'mod_assign_behat_test_that_this_home', '/home/this/that/test/behat/mod_assign.feature'],
464 ['/home/that/test/behat/mod_assign.feature', 'mod_assign_behat_test_that_home', '/home/that/test/behat/mod_assign.feature'],
465 ['/home/test/behat/mod_assign.feature', 'mod_assign_behat_test_home', '/home/test/behat/mod_assign.feature'],
466 ['mod_assign.feature', 'mod_assign', 'mod_assign.feature'],
467 ['C:\test\this\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that_this_test', 'C:\test\this\that\test\behat\mod_assign.feature'],
b4bc4286
RT
468 ['C:\this\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that_this', 'C:\this\that\test\behat\mod_assign.feature'],
469 ['C:\that\test\behat\mod_assign.feature', 'mod_assign_behat_test_that', 'C:\that\test\behat\mod_assign.feature'],
470 ['C:\test\behat\mod_assign.feature', 'mod_assign_behat_test', 'C:\test\behat\mod_assign.feature'],
471 ['C:\mod_assign.feature', 'mod_assign', 'C:\mod_assign.feature'],
5aa9b5ce
RT
472 );
473 }
95e1d5e5
RT
474
475 /**
476 * Behat config for blacklisted tags.
477 */
478 public function test_get_config_file_contents_with_blacklisted_tags() {
479
480 $mockbuilder = $this->getMockBuilder('behat_config_util');
4422ffc5
RT
481 $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_blacklisted_tests_for_theme',
482 'get_default_theme'));
95e1d5e5
RT
483
484 $behatconfigutil = $mockbuilder->getMock();
485
486 $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
487
488 // Blacklisted tags.
489 $map = array(
490 array('withfeatures', 'tags', array('@test1')),
491 array('nofeatures', 'tags', array('@test2')),
4422ffc5 492 array('defaulttheme', 'tags', array()),
95e1d5e5
RT
493 array('withfeatures', 'features', array()),
494 array('nofeatures', 'features', array()),
4422ffc5 495 array('defaulttheme', 'features', array()),
95e1d5e5 496 array('withfeatures', 'contexts', array()),
4422ffc5
RT
497 array('nofeatures', 'contexts', array()),
498 array('defaulttheme', 'contexts', array())
95e1d5e5
RT
499 );
500
501 $behatconfigutil->expects($this->any())
502 ->method('get_blacklisted_tests_for_theme')
503 ->will($this->returnValueMap($map));
504
505 $behatconfigutil->set_theme_suite_to_include_core_features(true);
4422ffc5 506 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
95e1d5e5
RT
507
508 // Three suites should be present.
509 $suites = $config['default']['suites'];
510 $this->assertCount(3, $suites);
511
512 $featurepaths = array(
513 'default' => array('test_1.feature', 'test_2.feature'),
514 'withfeatures' => array('test_2.feature', 'theme_test_1.feature', 'theme_test_2.feature', 'theme_test_3.feature',
515 'theme_test_4.feature', 'theme_test_5.feature'),
516 'nofeatures' => array('test_1.feature')
517 );
518
519 // Check features.
520 foreach ($featurepaths as $themename => $paths) {
521 $this->assertCount(count($paths), $suites[$themename]['paths']);
522
523 foreach ($paths as $key => $feature) {
524 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
525 }
526 }
527 // Check contexts.
528 foreach ($this->contextspath as $themename => $paths) {
529 $this->assertCount(count($paths), $suites[$themename]['contexts']);
530
531 foreach ($paths as $key => $context) {
532 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
533 }
534 }
4422ffc5
RT
535 // There are 7 step definitions.
536 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
95e1d5e5
RT
537 }
538
539 /**
540 * Behat config for blacklisted features.
541 */
542 public function test_get_config_file_contents_with_blacklisted_features_contexts() {
543
544 $mockbuilder = $this->getMockBuilder('behat_config_util');
4422ffc5
RT
545 $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_blacklisted_tests_for_theme',
546 'get_default_theme'));
95e1d5e5
RT
547
548 $behatconfigutil = $mockbuilder->getMock();
549
550 $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
551
552 // Blacklisted features and contexts.
553 $map = array(
554 array('withfeatures', 'tags', array()),
555 array('nofeatures', 'tags', array()),
4422ffc5 556 array('defaulttheme', 'tags', array()),
95e1d5e5
RT
557 array('withfeatures', 'features', array('admin/tool/behat/tests/fixtures/core/test_1.feature')),
558 array('nofeatures', 'features', array('admin/tool/behat/tests/fixtures/core/test_2.feature')),
4422ffc5 559 array('defaulttheme', 'features', array()),
95e1d5e5 560 array('withfeatures', 'contexts', array('admin/tool/behat/tests/fixtures/core/behat_test_context_2.php')),
4422ffc5
RT
561 array('nofeatures', 'contexts', array('admin/tool/behat/tests/fixtures/core/behat_test_context_1.php')),
562 array('defaulttheme', 'contexts', array()),
95e1d5e5
RT
563 );
564
565 $behatconfigutil->expects($this->any())
566 ->method('get_blacklisted_tests_for_theme')
567 ->will($this->returnValueMap($map));
568
569 $behatconfigutil->set_theme_suite_to_include_core_features(true);
4422ffc5 570 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
95e1d5e5
RT
571
572 // Three suites should be present.
573 $suites = $config['default']['suites'];
574 $this->assertCount(3, $suites);
575
576 $featurepaths = array(
577 'default' => array('test_1.feature', 'test_2.feature'),
578 'withfeatures' => array('test_2.feature', 'theme_test_1.feature', 'theme_test_2.feature', 'theme_test_3.feature',
579 'theme_test_4.feature', 'theme_test_5.feature'),
580 'nofeatures' => array('test_1.feature')
581 );
582 $contextspath = array(
583 'default' => array(
584 'behat_test_context_1',
4422ffc5
RT
585 'behat_test_context_2',
586 'behat_theme_defaulttheme_test_context_1',
95e1d5e5
RT
587 ),
588 'withfeatures' => array(
589 'behat_theme_withfeatures_test_context_2',
590 'behat_theme_withfeatures_behat_test_context_1'
591 ),
592 'nofeatures' => array(
593 'behat_theme_nofeatures_test_context_1',
594 'behat_theme_nofeatures_behat_test_context_2'
595 ),
596 );
597
598 // Check features.
599 foreach ($featurepaths as $themename => $paths) {
600 $this->assertCount(count($paths), $suites[$themename]['paths']);
601
602 foreach ($paths as $key => $feature) {
603 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
604 }
605 }
606 // Check contexts.
607 foreach ($contextspath as $themename => $paths) {
608 $this->assertCount(count($paths), $suites[$themename]['contexts']);
609
610 foreach ($paths as $key => $context) {
611 $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
612 }
613 }
4422ffc5
RT
614 // There are 7 step definitions.
615 $this->assertCount(7, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
95e1d5e5 616 }
e1ad3a95
RT
617
618 /**
619 * Behat config for blacklisted tags.
620 */
621 public function test_core_features_to_include_in_specified_theme() {
622
623 $mockbuilder = $this->getMockBuilder('behat_config_util');
4422ffc5 624 $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes', 'get_default_theme'));
e1ad3a95
RT
625
626 $behatconfigutil = $mockbuilder->getMock();
627
628 $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
629
630 // Check features when, no theme is specified.
631 $behatconfigutil->set_theme_suite_to_include_core_features('');
4422ffc5 632 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
e1ad3a95
RT
633 $suites = $config['default']['suites'];
634 foreach ($this->featurepaths as $themename => $paths) {
635 $this->assertCount(count($paths), $suites[$themename]['paths']);
636
637 foreach ($paths as $key => $feature) {
638 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
639 }
640 }
641
642 // Check features when all themes are specified.
643 $featurepaths = $this->featurepaths;
644 $featurepaths['withfeatures'] = array_merge ($featurepaths['default'], $featurepaths['withfeatures']);
645 $featurepaths['nofeatures'] = array_merge ($featurepaths['default'], $featurepaths['nofeatures']);
646
647 $behatconfigutil->set_theme_suite_to_include_core_features('ALL');
4422ffc5 648 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
e1ad3a95
RT
649 $suites = $config['default']['suites'];
650 foreach ($featurepaths as $themename => $paths) {
651 $this->assertCount(count($paths), $suites[$themename]['paths']);
652
653 foreach ($paths as $key => $feature) {
654 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
655 }
656 }
657
658 // Check features when all themes are specified.
659 $featurepaths = $this->featurepaths;
660 $featurepaths['withfeatures'] = array_merge ($featurepaths['default'], $featurepaths['withfeatures']);
661 $featurepaths['nofeatures'] = array_merge ($featurepaths['default'], $featurepaths['nofeatures']);
662
663 $behatconfigutil->set_theme_suite_to_include_core_features('withfeatures, nofeatures');
4422ffc5 664 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
e1ad3a95
RT
665 $suites = $config['default']['suites'];
666 foreach ($featurepaths as $themename => $paths) {
667 $this->assertCount(count($paths), $suites[$themename]['paths']);
668
669 foreach ($paths as $key => $feature) {
670 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
671 }
672 }
673
674 // Check features when specified themes are passed..
675 $featurepaths = $this->featurepaths;
676 $featurepaths['nofeatures'] = array_merge ($featurepaths['default'], $featurepaths['nofeatures']);
677
678 $behatconfigutil->set_theme_suite_to_include_core_features('nofeatures');
4422ffc5 679 $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts);
e1ad3a95
RT
680 $suites = $config['default']['suites'];
681 foreach ($featurepaths as $themename => $paths) {
682 $this->assertCount(count($paths), $suites[$themename]['paths']);
683
684 foreach ($paths as $key => $feature) {
685 $this->assertContains($feature, $suites[$themename]['paths'][$key]);
686 }
687 }
688 }
5aa9b5ce
RT
689}
690// @codeCoverageIgnoreEnd