2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for admin/tool/behat
21 * @copyright 2012 David Monllaó
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot.'/' . $CFG->admin .'/tool/behat/locallib.php');
31 * Allows access to internal methods without exposing them
33 class testable_tool_behat extends tool_behat {
36 * Allow access to protected method
37 * @see parent::merge_config()
38 * @param mixed $config
39 * @param mixed $localconfig
42 public static function merge_config($config, $localconfig) {
43 return parent::merge_config($config, $localconfig);
47 * Allow access to protected method
48 * @see parent::get_config_file_contents()
49 * @param string $prefix
50 * @param array $features
51 * @param array $stepsdefinitions
54 public static function get_config_file_contents($prefix, $features, $stepsdefinitions) {
55 return parent::get_config_file_contents($prefix, $features, $stepsdefinitions);
62 class tool_behat_testcase extends advanced_testcase {
65 * test_switch_environment
67 public function test_switch_environment() {
69 // Only run the tests if behat dependencies are installed.
70 // We don't need to pre-check PHPUnit initialisation because we are running on it.
71 if (version_compare(PHP_VERSION, '5.4.0', '>=') && tool_behat::are_behat_dependencies_installed()) {
72 tool_behat::switchenvironment('enable');
73 $this->assertTrue(tool_behat::is_test_mode_enabled());
74 $this->assertFalse(tool_behat::is_test_environment_running());
76 // We trigger a debugging() if it's already enabled.
77 tool_behat::switchenvironment('enable');
78 $this->assertDebuggingCalled();
80 tool_behat::switchenvironment('disable');
81 $this->assertFalse(tool_behat::is_test_mode_enabled());
82 $this->assertFalse(tool_behat::is_test_environment_running());
84 // We trigger a debugging() if it's already enabled.
85 tool_behat::switchenvironment('disable');
86 $this->assertDebuggingCalled();
88 // Ensure all continues disabled.
89 $this->assertFalse(tool_behat::is_test_mode_enabled());
90 $this->assertFalse(tool_behat::is_test_environment_running());
97 public function test_merge_configs() {
99 // Simple default config.
104 'one' => 'arrayvalue1',
105 'two' => 'arrayvalue2'
111 'simple' => 'OVERRIDDEN1',
113 'one' => 'OVERRIDDEN2'
115 'newprofile' => array(
116 'anotherlevel' => array(
117 'andanotherone' => array(
125 $array = testable_tool_behat::merge_config($array1, $array2);
127 // Overriddes are applied.
128 $this->assertEquals('OVERRIDDEN1', $array['simple']);
129 $this->assertEquals('OVERRIDDEN2', $array['array']['one']);
131 // Other values are respected.
132 $this->assertNotEmpty($array['array']['two']);
134 // Completely new nodes are added.
135 $this->assertNotEmpty($array['newprofile']);
136 $this->assertNotEmpty($array['newprofile']['anotherlevel']['andanotherone']);
137 $this->assertEquals('list1', $array['newprofile']['anotherlevel']['andanotherone'][0]);
138 $this->assertEquals('list2', $array['newprofile']['anotherlevel']['andanotherone'][1]);
140 // Complex override changing vectors to scalars and scalars to vectors.
143 'simple' => 'should',
144 'be' => 'overridden',
150 $array = testable_tool_behat::merge_config($array1, $array2);
152 // Overrides applied.
153 $this->assertNotEmpty($array['simple']);
154 $this->assertNotEmpty($array['array']);
155 $this->assertTrue(is_array($array['simple']));
156 $this->assertFalse(is_array($array['array']));
158 // Other values are maintained.
159 $this->assertEquals('same', $array['the']);
163 * test_config_file_contents
165 public function test_config_file_contents() {
168 unset($CFG->behat_config);
177 // Associative array.
178 $stepsdefinitions = array(
179 'micarro' => '/me/lo/robaron',
180 'anoche' => '/cuando/yo/dormia'
183 $contents = testable_tool_behat::get_config_file_contents('/i/am/a/prefix/', $features, $stepsdefinitions);
185 $this->assertContains('features: /i/am/a/prefix/lib/behat/features', $contents);
186 $this->assertContains('micarro: /me/lo/robaron', $contents);
187 $this->assertContains('base_url: \'' . $CFG->behat_wwwroot . '\'', $contents);
188 $this->assertContains('class: behat_init_context', $contents);
189 $this->assertContains('- feature1', $contents);
190 $this->assertContains('- feature3', $contents);