MDL-37046 behat: Added to standard plugins list
[moodle.git] / admin / tool / behat / tests / tool_behat_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Unit tests for admin/tool/behat
19  *
20  * @package   tool_behat
21  * @copyright  2012 David MonllaĆ³
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot.'/' . $CFG->admin .'/tool/behat/locallib.php');
30 /**
31  * Allows access to internal methods without exposing them
32  */
33 class testable_tool_behat extends tool_behat {
35     /**
36      * Allow access to protected method
37      * @see parent::merge_config()
38      * @param mixed $config
39      * @param mixed $localconfig
40      * @return mixed
41      */
42     public static function merge_config($config, $localconfig) {
43         return parent::merge_config($config, $localconfig);
44     }
46     /**
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
52      * @return string
53      */
54     public static function get_config_file_contents($prefix, $features, $stepsdefinitions) {
55         return parent::get_config_file_contents($prefix, $features, $stepsdefinitions);
56     }
57 }
59 /**
60  * Tool behat tests
61  */
62 class tool_behat_testcase extends advanced_testcase {
64     /**
65      * test_switch_environment
66      */
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());
91         }
92     }
94     /**
95      * test_merge_configs
96      */
97     public function test_merge_configs() {
99         // Simple default config.
100         $array1 = array(
101             'the' => 'same',
102             'simple' => 'value',
103             'array' => array(
104                 'one' => 'arrayvalue1',
105                 'two' => 'arrayvalue2'
106             )
107         );
109         // Simple override.
110         $array2 = array(
111             'simple' => 'OVERRIDDEN1',
112             'array' => array(
113                 'one' => 'OVERRIDDEN2'
114             ),
115             'newprofile' => array(
116                 'anotherlevel' => array(
117                     'andanotherone' => array(
118                         'list1',
119                         'list2'
120                     )
121                 )
122             )
123         );
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.
141         $array2 = array(
142             'simple' => array(
143                 'simple' => 'should',
144                 'be' => 'overridden',
145                 'by' => 'this-array'
146             ),
147             'array' => 'one'
148         );
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']);
160     }
162     /**
163      * test_config_file_contents
164      */
165     public function test_config_file_contents() {
166         global $CFG;
168         unset($CFG->behat_config);
170         // List.
171         $features = array(
172             'feature1',
173             'feature2',
174             'feature3'
175         );
177         // Associative array.
178         $stepsdefinitions = array(
179             'micarro' => '/me/lo/robaron',
180             'anoche' => '/cuando/yo/dormia'
181         );
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);
191     }