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 * @see parent::merge_config()
37 * @param mixed $config
38 * @param mixed $localconfig
41 public static function merge_config($config, $localconfig) {
42 return parent::merge_config($config, $localconfig);
46 * @see parent::get_config_file_contents()
47 * @param string $prefix
48 * @param array $features
49 * @param array $stepsdefinitions
52 public static function get_config_file_contents($prefix, $features, $stepsdefinitions) {
53 return parent::get_config_file_contents($prefix, $features, $stepsdefinitions);
60 class tool_behat_testcase extends advanced_testcase {
63 public function test_switch_environment() {
65 // Only run the tests if behat dependencies are installed.
66 // We don't need to pre-check PHPUnit initialisation because we are running on it.
67 if (version_compare(PHP_VERSION, '5.4.0', '>=') && tool_behat::are_behat_dependencies_installed()) {
68 tool_behat::switchenvironment('enable');
69 $this->assertTrue(tool_behat::is_test_mode_enabled());
70 $this->assertFalse(tool_behat::is_test_environment_running());
72 // We trigger a debugging() if it's already enabled.
73 tool_behat::switchenvironment('enable');
74 $this->assertDebuggingCalled();
76 tool_behat::switchenvironment('disable');
77 $this->assertFalse(tool_behat::is_test_mode_enabled());
78 $this->assertFalse(tool_behat::is_test_environment_running());
80 // We trigger a debugging() if it's already enabled.
81 tool_behat::switchenvironment('disable');
82 $this->assertDebuggingCalled();
84 // Ensure all continues disabled.
85 $this->assertFalse(tool_behat::is_test_mode_enabled());
86 $this->assertFalse(tool_behat::is_test_environment_running());
90 public function test_merge_configs() {
92 // Simple default config.
97 'one' => 'arrayvalue1',
98 'two' => 'arrayvalue2'
104 'simple' => 'OVERRIDDEN1',
106 'one' => 'OVERRIDDEN2'
108 'newprofile' => array(
109 'anotherlevel' => array(
110 'andanotherone' => array(
118 $array = testable_tool_behat::merge_config($array1, $array2);
120 // Overriddes are applied.
121 $this->assertEquals('OVERRIDDEN1', $array['simple']);
122 $this->assertEquals('OVERRIDDEN2', $array['array']['one']);
124 // Other values are respected.
125 $this->assertNotEmpty($array['array']['two']);
127 // Completely new nodes are added.
128 $this->assertNotEmpty($array['newprofile']);
129 $this->assertNotEmpty($array['newprofile']['anotherlevel']['andanotherone']);
130 $this->assertEquals('list1', $array['newprofile']['anotherlevel']['andanotherone'][0]);
131 $this->assertEquals('list2', $array['newprofile']['anotherlevel']['andanotherone'][1]);
133 // Complex override changing vectors to scalars and scalars to vectors.
136 'simple' => 'should',
137 'be' => 'overridden',
143 $array = testable_tool_behat::merge_config($array1, $array2);
145 // Overrides applied.
146 $this->assertNotEmpty($array['simple']);
147 $this->assertNotEmpty($array['array']);
148 $this->assertTrue(is_array($array['simple']));
149 $this->assertFalse(is_array($array['array']));
151 // Other values are maintained.
152 $this->assertEquals('same', $array['the']);
155 public function test_config_file_contents() {
158 unset($CFG->behat_config);
167 // Associative array.
168 $stepsdefinitions = array(
169 'micarro' => '/me/lo/robaron',
170 'anoche' => '/cuando/yo/dormia'
173 $contents = testable_tool_behat::get_config_file_contents('/i/am/a/prefix/', $features, $stepsdefinitions);
175 $this->assertContains('features: /i/am/a/prefix/lib/behat/features', $contents);
176 $this->assertContains('micarro: /me/lo/robaron', $contents);
177 $this->assertContains('base_url: \'' . $CFG->behat_wwwroot . '\'', $contents);
178 $this->assertContains('class: behat_init_context', $contents);
179 $this->assertContains('- feature1', $contents);
180 $this->assertContains('- feature3', $contents);