MDL-55986 behat: Fix unit tests to use fixtures
authorRajesh Taneja <rajesh@moodle.com>
Fri, 30 Sep 2016 05:31:41 +0000 (13:31 +0800)
committerRajesh Taneja <rajesh@moodle.com>
Mon, 3 Oct 2016 07:38:24 +0000 (15:38 +0800)
16 files changed:
admin/tool/behat/tests/fixtures/core/behat_test_context_1.php [new file with mode: 0644]
admin/tool/behat/tests/fixtures/core/behat_test_context_2.php [new file with mode: 0644]
admin/tool/behat/tests/fixtures/core/test_1.feature [new file with mode: 0644]
admin/tool/behat/tests/fixtures/core/test_2.feature [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/nofeatures/behat_theme_nofeatures_behat_test_context_2.php [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/nofeatures/behat_theme_nofeatures_test_context_1.php [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/withfeatures/behat_theme_withfeatures_behat_test_context_1.php [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/withfeatures/behat_theme_withfeatures_test_context_2.php [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_1.feature [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_2.feature [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_3.feature [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_4.feature [new file with mode: 0644]
admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_5.feature [new file with mode: 0644]
admin/tool/behat/tests/manager_test.php
admin/tool/behat/tests/manager_util_test.php
lib/behat/classes/behat_config_util.php

diff --git a/admin/tool/behat/tests/fixtures/core/behat_test_context_1.php b/admin/tool/behat/tests/fixtures/core/behat_test_context_1.php
new file mode 100644 (file)
index 0000000..01c0396
--- /dev/null
@@ -0,0 +1,38 @@
+<?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/>.
+
+/**
+ * Test context 1
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
+
+require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
+
+/**
+ * Test context 1
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class behat_test_context_1 extends behat_base {
+
+}
\ No newline at end of file
diff --git a/admin/tool/behat/tests/fixtures/core/behat_test_context_2.php b/admin/tool/behat/tests/fixtures/core/behat_test_context_2.php
new file mode 100644 (file)
index 0000000..0dbdbeb
--- /dev/null
@@ -0,0 +1,38 @@
+<?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/>.
+
+/**
+ * Test context 2
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
+
+require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
+
+/**
+ * Test context 2
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class behat_test_context_2 extends behat_base {
+
+}
\ No newline at end of file
diff --git a/admin/tool/behat/tests/fixtures/core/test_1.feature b/admin/tool/behat/tests/fixtures/core/test_1.feature
new file mode 100644 (file)
index 0000000..984f6e7
--- /dev/null
@@ -0,0 +1,9 @@
+@behat_test @test1
+Feature: Test feature 1
+  In order to test behat.yml in phpunit
+  As an user
+  I need to be able to include this feature
+
+  @javascript
+  Scenario: I should not be included in normal execution.
+    Given I pause scenario execution
diff --git a/admin/tool/behat/tests/fixtures/core/test_2.feature b/admin/tool/behat/tests/fixtures/core/test_2.feature
new file mode 100644 (file)
index 0000000..091781e
--- /dev/null
@@ -0,0 +1,8 @@
+@behat_test @test2
+Feature: Test feature 2
+  In order to test behat.yml in phpunit
+  As an user
+  I need to be able to include this feature
+
+  Scenario: I should not be included in normal execution.
+    Given I pause scenario execution
diff --git a/admin/tool/behat/tests/fixtures/theme/nofeatures/behat_theme_nofeatures_behat_test_context_2.php b/admin/tool/behat/tests/fixtures/theme/nofeatures/behat_theme_nofeatures_behat_test_context_2.php
new file mode 100644 (file)
index 0000000..710da0a
--- /dev/null
@@ -0,0 +1,38 @@
+<?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/>.
+
+/**
+ * Theme test context 1 overriding test_1
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
+
+require_once(__DIR__ . '/../behat_test_context_1.php');
+
+/**
+ * Theme test context 1
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class behat_theme_nofeatures_behat_test_context_1 extends behat_test_context_1 {
+
+}
\ No newline at end of file
diff --git a/admin/tool/behat/tests/fixtures/theme/nofeatures/behat_theme_nofeatures_test_context_1.php b/admin/tool/behat/tests/fixtures/theme/nofeatures/behat_theme_nofeatures_test_context_1.php
new file mode 100644 (file)
index 0000000..87fe40c
--- /dev/null
@@ -0,0 +1,38 @@
+<?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/>.
+
+/**
+ * Theme test context 2
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
+
+require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
+
+/**
+ * Theme test context 2
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class behat_theme_nofeatures_test_context_2 extends behat_base {
+
+}
\ No newline at end of file
diff --git a/admin/tool/behat/tests/fixtures/theme/withfeatures/behat_theme_withfeatures_behat_test_context_1.php b/admin/tool/behat/tests/fixtures/theme/withfeatures/behat_theme_withfeatures_behat_test_context_1.php
new file mode 100644 (file)
index 0000000..3941071
--- /dev/null
@@ -0,0 +1,38 @@
+<?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/>.
+
+/**
+ * Theme test context 1 overriding test_1
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
+
+require_once(__DIR__ . '/../behat_test_context_1.php');
+
+/**
+ * Theme test context 1
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class behat_theme_withfeatures_behat_test_context_1 extends behat_test_context_1 {
+
+}
\ No newline at end of file
diff --git a/admin/tool/behat/tests/fixtures/theme/withfeatures/behat_theme_withfeatures_test_context_2.php b/admin/tool/behat/tests/fixtures/theme/withfeatures/behat_theme_withfeatures_test_context_2.php
new file mode 100644 (file)
index 0000000..23eedff
--- /dev/null
@@ -0,0 +1,38 @@
+<?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/>.
+
+/**
+ * Theme test context 2
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
+
+require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
+
+/**
+ * Theme test context 2
+ *
+ * @package    tool_behat
+ * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class behat_theme_withfeatures_test_context_2 extends behat_base {
+
+}
\ No newline at end of file
diff --git a/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_1.feature b/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_1.feature
new file mode 100644 (file)
index 0000000..ff72285
--- /dev/null
@@ -0,0 +1,9 @@
+@behat_test @test1 @testtheme @commontag
+Feature: Test feature 1
+  In order to test behat.yml in phpunit
+  As an user
+  I need to be able to include this feature
+
+  @javascript
+  Scenario: I should not be included in normal execution.
+    Given I pause scenario execution
diff --git a/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_2.feature b/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_2.feature
new file mode 100644 (file)
index 0000000..7e913c6
--- /dev/null
@@ -0,0 +1,9 @@
+@behat_test @test2 @testtheme @commontag
+Feature: Test feature 2
+  In order to test behat.yml in phpunit
+  As an user
+  I need to be able to include this feature
+
+  @javascript
+  Scenario: I should not be included in normal execution.
+    Given I pause scenario execution
diff --git a/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_3.feature b/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_3.feature
new file mode 100644 (file)
index 0000000..d3ea3d9
--- /dev/null
@@ -0,0 +1,9 @@
+@behat_test @test3 @testtheme
+Feature: Test feature 3
+  In order to test behat.yml in phpunit
+  As an user
+  I need to be able to include this feature
+
+  @javascript
+  Scenario: I should not be included in normal execution.
+    Given I pause scenario execution
diff --git a/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_4.feature b/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_4.feature
new file mode 100644 (file)
index 0000000..3825f9f
--- /dev/null
@@ -0,0 +1,9 @@
+@behat_test @test4 @testtheme
+Feature: Test feature 4
+  In order to test behat.yml in phpunit
+  As an user
+  I need to be able to include this feature
+
+  @javascript
+  Scenario: I should not be included in normal execution.
+    Given I pause scenario execution
diff --git a/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_5.feature b/admin/tool/behat/tests/fixtures/theme/withfeatures/theme_test_5.feature
new file mode 100644 (file)
index 0000000..7476252
--- /dev/null
@@ -0,0 +1,9 @@
+@behat_test @test5 @testtheme
+Feature: Test feature 5
+  In order to test behat.yml in phpunit
+  As an user
+  I need to be able to include this feature
+
+  @javascript
+  Scenario: I should not be included in normal execution.
+    Given I pause scenario execution
index ae052e5..7b88c80 100644 (file)
@@ -114,6 +114,8 @@ class tool_behat_manager_testcase extends advanced_testcase {
     public function test_config_file_contents() {
         global $CFG;
 
+        $this->resetAfterTest(true);
+
         // Skip tests if behat is not installed.
         $vendorpath = $CFG->dirroot . '/vendor';
         if (!file_exists($vendorpath . '/autoload.php') || !is_dir($vendorpath . '/behat')) {
@@ -145,17 +147,19 @@ class tool_behat_manager_testcase extends advanced_testcase {
         // YAML decides when is is necessary to wrap strings between single quotes, so not controlled
         // values like paths should not be asserted including the key name as they would depend on the
         // directories values.
-        $this->assertContains($CFG->dirroot, $contents);
+        $this->assertContains($CFG->dirroot,
+            $contents['default']['extensions']['Moodle\BehatExtension']['moodledirroot']);
 
         // Not quoted strings.
-        $this->assertContains('micarro: /me/lo/robaron', $contents);
+        $this->assertEquals('/me/lo/robaron',
+            $contents['default']['extensions']['Moodle\BehatExtension']['steps_definitions']['micarro']);
 
         // YAML uses single quotes to wrap URL strings.
-        $this->assertContains("base_url: '" . $CFG->behat_wwwroot . "'", $contents);
+        $this->assertEquals($CFG->behat_wwwroot, $contents['default']['extensions']['Behat\MinkExtension']['base_url']);
 
         // Lists.
-        $this->assertContains('- feature1', $contents);
-        $this->assertContains('- feature3', $contents);
+        $this->assertEquals('feature1', $contents['default']['suites']['default']['paths'][0]);
+        $this->assertEquals('feature3', $contents['default']['suites']['default']['paths'][2]);
 
         unset($CFG->behat_wwwroot);
     }
index 28182c3..5723743 100644 (file)
@@ -41,254 +41,385 @@ require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
  */
 class tool_behat_manager_util_testcase extends advanced_testcase {
 
-    private function get_config($behatconfigutil) {
+    /** @var array Fixtures features which are available. */
+    private $featurepaths = array(
+        'default' => array(
+            'test_1.feature',
+            'test_2.feature',
+        ),
+        'withfeatures' => array(
+            'theme_test_1.feature',
+            'theme_test_2.feature',
+            'theme_test_3.feature',
+            'theme_test_4.feature',
+            'theme_test_5.feature',
+        ),
+        'nofeatures' => array()
+    );
+
+    /** @var array Fixture contexts which are available */
+    private $contextspath = array(
+        'default' => array(
+            'behat_test_context_1',
+            'behat_test_context_2'
+        ),
+        'withfeatures' => array(
+            'behat_test_context_2',
+            'behat_theme_withfeatures_test_context_2',
+            'behat_theme_withfeatures_behat_test_context_1'
+        ),
+        'nofeatures' => array(
+            'behat_test_context_1',
+            'behat_theme_nofeatures_test_context_1',
+            'behat_theme_nofeatures_behat_test_context_2'
+        ),
+    );
+
+    private $corefatures = array('test_1' => __DIR__.'/fixtures/core/test_1.feature',
+                                 'test_2' => __DIR__.'/fixtures/core/test_2.feature');
+
+    private $corecontexts = array('behat_test_context_1' => __DIR__.'/fixtures/core/behat_test_context_1.php',
+                                  'behat_test_context_2' => __DIR__.'/fixtures/core/behat_test_context_2.php');
+
+    /**
+     * Setup test.
+     */
+    public function setup() {
+        global $CFG;
+
+        $this->resetAfterTest();
+        $CFG->behat_wwwroot = 'http://example.com/behat';
+    }
+
+    /**
+     * Utility function to build mock object.
+     *
+     * @param  behat_config_util $behatconfigutil
+     * @param bool $notheme
+     * @return mixed
+     */
+    private function get_behat_config_util($behatconfigutil, $notheme = false) {
+        // Create a map of arguments to return values.
+        $map = array(
+            array('withfeatures', __DIR__.'/fixtures/theme/withfeatures'),
+            array('nofeatures', __DIR__.'/fixtures/theme/nofeatures')
+        );
+
         // List of themes is const for test.
+        if ($notheme) {
+            $themelist = array();
+        } else {
+            $themelist = array('withfeatures', 'nofeatures');
+        }
+
         $behatconfigutil->expects($this->any())
             ->method('get_list_of_themes')
-            ->will($this->returnValue(array('testtheme')));
+            ->will($this->returnValue($themelist));
 
         // Theme directory for testing.
         $behatconfigutil->expects($this->any())
-            ->method('get_test_directories_overridden_for_theme')
-            ->with($this->equalTo('testtheme'))
-            ->will($this->returnValue(array(
-                __DIR__ . '/fixtures/testtheme/tests/behat'
-            )));
-
-        // Core components list for testing.
-        $behatconfigutil->expects($this->any())
-            ->method('get_components_with_tests')
-            ->will($this->returnValue(array('testtheme' => __DIR__.'/fixtures/core')));
-
-        return $behatconfigutil->get_config_file_contents();
+            ->method('get_theme_test_directory')
+            ->will($this->returnValueMap($map));
 
+        return $behatconfigutil;
     }
 
     /**
      * Behat config for single run.
-     *
      */
     public function test_get_config_file_contents_with_single_run() {
-        global $CFG;
-
-        $this->resetAfterTest();
-        $CFG->behat_wwwroot = 'http://example.com/behat';
 
         $mockbuilder = $this->getMockBuilder('behat_config_util');
-        $mockbuilder->setMethods(array('get_test_directories_overridden_for_theme', 'get_list_of_themes',
-            'get_components_with_tests'));
+        $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
 
         $behatconfigutil = $mockbuilder->getMock();
 
-        $config = $this->get_config($behatconfigutil);
+        $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
+        $config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
 
-var_dump($config);
-        // Contains core features and contexts.
+        // Two suites should be present.
         $suites = $config['default']['suites'];
-        $this->assertContains('test_1.feature', $suites['default']['paths'][0]);
-        $this->assertContains('test_2.feature', $suites['default']['paths'][1]);
-        $this->assertContains('behat_test_context_1', $suites['default']['contexts'][0]);
-        $this->assertContains('behat_test_context_2', $suites['default']['contexts'][1]);
-
-        // Contains theme specific features and contexts.
-        $this->assertContains('theme_test_1.feature', $suites['testtheme']['paths'][0]);
-        $this->assertContains('theme_test_2.feature', $suites['testtheme']['paths'][1]);
+        $this->assertCount(3, $suites);
+
+        // Check features.
+        foreach ($this->featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+
+        // Check contexts.
+        foreach ($this->contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+
+        // There are 6 step definitions.
+        $this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
     }
 
     /**
-     * Behat config for parallel run.
+     * Behat config for single run with no theme installed.
      */
-    public function test_get_config_file_contents_with_parallel_run() {
-        global $CFG;
+    public function test_get_config_file_contents_with_single_run_no_theme() {
 
-        $CFG->behat_wwwroot = 'http://example.com/behat';
-        $behatconfigutil = $this->behatconfigutil;
+        $mockbuilder = $this->getMockBuilder('behat_config_util');
+        $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
 
-        // No theme feature exists.
-        $behatconfigutil->expects($this->any())
-            ->method('get_behat_features_for_theme')
-            ->with($this->anything())
-            ->will($this->returnValue(array(
-                'blacklistfeatures' => array(),
-                'features' => array()))
-            );
-
-        $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts, '', 3, 1);
-
-        // First run.
-        $this->assertContains('/test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_test1.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle\\mod\\assign\\feedback\\file\\tests\\behat\\behat_test2.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle/login/tests/behat/behat_test3.feature',
-            $config);
-
-        // Second run.
-        $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts, '', 3, 2);
-
-        $this->assertNotContains('/test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_test1.feature',
-            $config);
-        $this->assertContains('C:\\test\\moodle\\mod\\assign\\feedback\\file\\tests\\behat\\behat_test2.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle/login/tests/behat/behat_test3.feature',
-            $config);
-
-        $config = $behatconfigutil->get_config_file_contents($this->corefeatures, $this->corecontexts, '', 3, 3);
-
-        $this->assertNotContains('/test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_test1.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle\\mod\\assign\\feedback\\file\\tests\\behat\\behat_test2.feature',
-            $config);
-        $this->assertContains('C:\\test\\moodle/login/tests/behat/behat_test3.feature',
-            $config);
+        $behatconfigutil = $mockbuilder->getMock();
+
+        $behatconfigutil = $this->get_behat_config_util($behatconfigutil, true);
+        $config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts);
+
+        // Two suites should be present.
+        $suites = $config['default']['suites'];
+        $this->assertCount(1, $suites);
+
+        $featurepaths = array(
+            'default' => array(
+                'test_1.feature',
+                'test_2.feature',
+            )
+        );
+
+        $contextspath = array(
+            'default' => array(
+                'behat_test_context_1',
+                'behat_test_context_2'
+            )
+        );
+
+        // Check features.
+        foreach ($featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+
+        // Check contexts.
+        foreach ($contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+
+        // There are 6 step definitions.
+        $this->assertCount(2, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
     }
 
     /**
-     * Behat config with theme features.
+     * Behat config for parallel run.
      */
-    public function test_get_config_file_contents_with_theme_features() {
-        global $CFG;
+    public function test_get_config_file_contents_with_parallel_run() {
 
-        $behatconfigutil = $this->behatconfigutil;
-
-        $suitefeatures = array_merge($this->corefeatures, $this->themefeatures);
-        $themefeatures = $this->themefeatures;
-        $behatconfigutil->expects($this->once())
-            ->method('get_behat_features_for_theme')
-            ->with($this->equalTo('testtheme'))
-            ->will($this->returnValue(array(
-                'blacklistfeatures' => array(),
-                'features' => $this->themefeatures))
-            );
-
-        $behatconfigutil->expects($this->once())
-            ->method('get_behat_contexts_for_theme')
-            ->with($this->equalTo('testtheme'))
-            ->will($this->returnValue(array(
-                'contexts' => $this->themecontexts,
-                'suitecontexts' => $this->themecontexts
-            )));
-
-        $behatconfigutil->expects($this->once())
-            ->method('get_overridden_theme_contexts')
-            ->will($this->returnValue($this->themecontexts));
-        $behatconfigutil->set_theme_suite_to_include_core_features(true);
+        $mockbuilder = $this->getMockBuilder('behat_config_util');
+        $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
 
-        $CFG->behat_wwwroot = 'http://example.com/behat';
-        $config = $behatconfigutil->get_config_file_contents($suitefeatures, $this->corecontexts);
-
-        $expectedconfigwithfeatures = "default:
-  formatters:
-    moodle_progress:
-      output_styles:
-        comment:
-          - magenta
-  suites:
-    default:
-      paths:
-        - /test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_test1.feature
-        - 'C:\\test\\moodle\\mod\\assign\\feedback\\file\\tests\\behat\\behat_test2.feature'
-        - 'C:\\test\\moodle/login/tests/behat/behat_test3.feature'
-      contexts:
-        - behat_context1
-        - behat_context2
-        - behat_context3
-    testtheme:
-      paths:
-        - /test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_test1.feature
-        - 'C:\\test\\moodle\\mod\\assign\\feedback\\file\\tests\\behat\\behat_test2.feature'
-        - 'C:\\test\\moodle/login/tests/behat/behat_test3.feature'
-        - /test/moodle/theme/testtheme/tests/behat/core/behat_themetest1.feature
-        - 'C:\\test\\moodle\\theme\\testtheme\\tests\\behat\\mod_assign\\behat_themetest2.feature'
-        - 'C:\\test\\moodle/theme/testtheme/tests/behat/behat_themetest3.feature'
-      contexts:
-        - behat_theme_testtheme_behat_context1
-        - behat_theme_testtheme_behat_context2
-        - behat_theme_testtheme_behat_context3
-  extensions:
-    Behat\\MinkExtension:
-      base_url: 'http://example.com/behat'
-      goutte: null
-      selenium2:
-        wd_host: 'http://localhost:4444/wd/hub'
-";
-        $this->assertContains($expectedconfigwithfeatures, $config);
-
-        $expectedstepdefinitions = "steps_definitions:
-        behat_context1: /test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_context1.php
-        behat_context2: 'C:\\test\\moodle\\blocks\\comments\\tests\\behat\\behat_context2.php'
-        behat_context3: 'C:\\test\\moodle/lib/editor/atto/tests/behat/behat_context3.php'
-        behat_theme_testtheme_behat_context1: /test/moodle/theme/testtheme/tests/behat/mod_assign/behat_theme_testtheme_behat_context1.php
-        behat_theme_testtheme_behat_context2: 'C:\\test\\moodle\\theme\\testtheme\\tests\\behat\\block_comments\\behat_theme_testtheme_behat_context2.php'
-        behat_theme_testtheme_behat_context3: 'C:\\test\\moodle/theme/testtheme/tests/behat/editor_atto/behat_theme_testtheme_behat_context3.php'";
-
-        $this->assertContains($expectedstepdefinitions, $config);
+        $behatconfigutil = $mockbuilder->getMock();
+
+        $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
+
+        // Test first run out of 3.
+        $config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts, '', 3, 1);
+        // Three suites should be present.
+        $suites = $config['default']['suites'];
+        $this->assertCount(3, $suites);
+        // There is first feature file in first run.
+        $featurepaths = array(
+            'default' => array('test_1.feature'),
+            'withfeatures' => array('theme_test_1.feature', 'theme_test_2.feature'),
+            'nofeatures' => array()
+        );
+        // Check features.
+        foreach ($featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+        // Check contexts.
+        foreach ($this->contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+        // There are 6 step definitions.
+        $this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
+
+        // Test second run out of 3.
+        $config = $behatconfigutil->get_config_file_contents('', '', '', 3, 2);
+        // Three suites should be present.
+        $suites = $config['default']['suites'];
+        $this->assertCount(3, $suites);
+        // There is second feature file in first run.
+        $featurepaths = array(
+            'default' => array('test_2.feature'),
+            'withfeatures' => array('theme_test_3.feature', 'theme_test_4.feature'),
+            'nofeatures' => array()
+        );
+        // Check features.
+        foreach ($featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+        // Check contexts.
+        foreach ($this->contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+        // There are 6 step definitions.
+        $this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
+
+        // Test third run out of 3.
+        $config = $behatconfigutil->get_config_file_contents('', '', '', 3, 3);
+        $suites = $config['default']['suites'];
+        $this->assertCount(3, $suites);
+        // There is second feature file in first run.
+        $featurepaths = array(
+            'default' => array(),
+            'withfeatures' => array('theme_test_5.feature'),
+            'nofeatures' => array()
+        );
+        // Check features.
+        foreach ($featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+        // Check contexts.
+        foreach ($this->contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+        // There are 6 step definitions.
+        $this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
     }
 
     /**
      * Behat config for parallel run.
      */
-    public function test_get_config_file_contents_with_theme_and_parallel_run() {
-        global $CFG;
+    public function test_get_config_file_contents_with_parallel_run_optimize_tags() {
 
-        $CFG->behat_wwwroot = 'http://example.com/behat';
+        $mockbuilder = $this->getMockBuilder('behat_config_util');
+        $mockbuilder->setMethods(array('get_theme_test_directory', 'get_list_of_themes'));
+
+        $behatconfigutil = $mockbuilder->getMock();
 
-        $behatconfigutil = $this->behatconfigutil;
-
-        $features = array_merge($this->corefeatures, $this->themefeatures);
-        $themefeatures = $this->themefeatures;
-        $behatconfigutil->expects($this->atLeastOnce())
-            ->method('get_behat_features_for_theme')
-            ->with($this->equalTo('testtheme'))
-            ->will($this->returnValue(array(
-                'blacklistfeatures' => array(),
-                'features' => $themefeatures))
-            );
-
-        $behatconfigutil->expects($this->atLeastOnce())
-            ->method('get_behat_contexts_for_theme')
-            ->with($this->equalTo('testtheme'))
-            ->will($this->returnValue(array(
-                'contexts' => $this->themecontexts,
-                'suitecontexts' => $this->themecontexts
-            )));
+        $behatconfigutil = $this->get_behat_config_util($behatconfigutil);
 
-        $CFG->behat_wwwroot = 'http://example.com/behat';
+        // Test first run out of 3.
+        $config = $behatconfigutil->get_config_file_contents($this->corefatures, $this->corecontexts, '@commontag', 3, 1);
 
-        $behatconfigutil->set_theme_suite_to_include_core_features(false);
-
-        $config = $behatconfigutil->get_config_file_contents($features, $this->themecontexts, '', 3, 1);
-
-        // First run.
-        $this->assertContains('/test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_test1.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle\\mod\\assign\\feedback\\file\\tests\\behat\\behat_test2.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle/login/tests/behat/behat_test3.feature',
-            $config);
-        // Theme suite features.
-        $this->assertContains('/test/moodle/theme/testtheme/tests/behat/core/behat_themetest1.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle\\theme\\testtheme\\tests\\behat\\mod_assign\\behat_themetest2.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle/theme/testtheme/tests/behat/behat_themetest3.feature',
-            $config);
-
-        // Second run.
-        $config = $behatconfigutil->get_config_file_contents($features, $this->themecontexts, '', 3, 2);
-        $this->assertNotContains('/test/moodle/mod/assign/feedback/editpdf/tests/behat/behat_test1.feature',
-            $config);
-        $this->assertContains('C:\\test\\moodle\\mod\\assign\\feedback\\file\\tests\\behat\\behat_test2.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle/login/tests/behat/behat_test3.feature',
-            $config);
-        // Theme suite features.
-        $this->assertNotContains('/test/moodle/theme/testtheme/tests/behat/core/behat_themetest1.feature',
-            $config);
-        $this->assertContains('C:\\test\\moodle\\theme\\testtheme\\tests\\behat\\mod_assign\\behat_themetest2.feature',
-            $config);
-        $this->assertNotContains('C:\\test\\moodle/theme/testtheme/tests/behat/behat_themetest3.feature',
-            $config);
+        // Three suites should be present.
+        $suites = $config['default']['suites'];
+        $this->assertCount(3, $suites);
+        // There is first feature file in first run.
+        $featurepaths = array(
+            'default' => array('test_1.feature'),
+            'withfeatures' => array('theme_test_1.feature', 'theme_test_3.feature'),
+            'nofeatures' => array()
+        );
+        // Check features.
+        foreach ($featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+        // Check contexts.
+        foreach ($this->contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+        // There are 6 step definitions.
+        $this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
+
+        // Test second run out of 3.
+        $config = $behatconfigutil->get_config_file_contents('', '', '@commontag', 3, 2);
+
+        // Three suites should be present.
+        $suites = $config['default']['suites'];
+        $this->assertCount(3, $suites);
+        // There is second feature file in first run.
+        $featurepaths = array(
+            'default' => array('test_2.feature'),
+            'withfeatures' => array('theme_test_2.feature', 'theme_test_4.feature'),
+            'nofeatures' => array()
+        );
+        // Check features.
+        foreach ($featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+        // Check contexts.
+        foreach ($this->contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+        // There are 6 step definitions.
+        $this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
+
+        // Test third run out of 3.
+        $config = $behatconfigutil->get_config_file_contents('', '', '', 3, 3);
+        $suites = $config['default']['suites'];
+        $this->assertCount(3, $suites);
+        // There is second feature file in first run.
+        $featurepaths = array(
+            'default' => array(),
+            'withfeatures' => array('theme_test_5.feature'),
+            'nofeatures' => array()
+        );
+        // Check features.
+        foreach ($featurepaths as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['paths']);
+
+            foreach ($paths as $key => $feature) {
+                $this->assertContains($feature, $suites[$themename]['paths'][$key]);
+            }
+        }
+        // Check contexts.
+        foreach ($this->contextspath as $themename => $paths) {
+            $this->assertCount(count($paths), $suites[$themename]['contexts']);
+
+            foreach ($paths as $key => $context) {
+                $this->assertTrue(in_array($context, $suites[$themename]['contexts']));
+            }
+        }
+        // There are 6 step definitions.
+        $this->assertCount(6, $config['default']['extensions']['Moodle\BehatExtension']['steps_definitions']);
     }
 
     /**
@@ -303,7 +434,8 @@ var_dump($config);
         $oldroot = $CFG->dirroot;
         $CFG->dirroot = 'C:';
 
-        $behatconfigutil = $this->behatconfigutil;
+        $behatconfigutil = new behat_config_util();
+
         // Fix expected directory path for OS.
         $cleanfeaturepath = testing_cli_fix_directory_separator($cleanfeaturepath);
 
index acf1f57..e07b512 100644 (file)
@@ -363,7 +363,7 @@ class behat_config_util {
         $config = $this->merge_behat_profiles($config);
 
         // Return config array for phpunit, so it can be tested.
-        if (defined('PHPUNIT_TEST')) {
+        if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
             return $config;
         }
 
@@ -1087,7 +1087,7 @@ class behat_config_util {
      * @param string $themename
      * @return string theme directory
      */
-    protected function get_theme_directory($themename) {
+    protected function get_theme_test_directory($themename) {
         global $CFG;
 
         $themetestdir = "/theme/" . $themename;
@@ -1109,7 +1109,7 @@ class behat_config_util {
             'contexts' => '|behat_.*\.php$|',
             'features' => '|.*\.feature$|',
         );
-        $themetestdirfullpath = $this->get_theme_directory($theme) . '/tests/behat';
+        $themetestdirfullpath = $this->get_theme_test_directory($theme);
 
         // If test directory doesn't exist then return.
         if (!is_dir($themetestdirfullpath)) {