bdc406e61b02d570eebb2840a2626c53fec30d56
[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      * @see parent::merge_config()
37      * @param mixed $config
38      * @param mixed $localconfig
39      * @return mixed
40      */
41     public static function merge_config($config, $localconfig) {
42         return parent::merge_config($config, $localconfig);
43     }
45     /**
46      * @see parent::get_config_file_contents()
47      * @param string $prefix
48      * @param array $features
49      * @param array $stepsdefinitions
50      * @return string
51      */
52     public static function get_config_file_contents($prefix, $features, $stepsdefinitions) {
53         return parent::get_config_file_contents($prefix, $features, $stepsdefinitions);
54     }
55 }
57 /**
58  * Tool behat tests
59  */
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());
87          }
88      }
90      public function test_merge_configs() {
92          // Simple default config.
93          $array1 = array(
94              'the' => 'same',
95              'simple' => 'value',
96              'array' => array(
97                  'one' => 'arrayvalue1',
98                  'two' => 'arrayvalue2'
99              )
100          );
102          // Simple override.
103          $array2 = array(
104              'simple' => 'OVERRIDDEN1',
105              'array' => array(
106                  'one' => 'OVERRIDDEN2'
107              ),
108              'newprofile' => array(
109                  'anotherlevel' => array(
110                      'andanotherone' => array(
111                          'list1',
112                          'list2'
113                      )
114                  )
115              )
116          );
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.
134          $array2 = array(
135              'simple' => array(
136                  'simple' => 'should',
137                  'be' => 'overridden',
138                  'by' => 'this-array'
139              ),
140              'array' => 'one'
141          );
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']);
153      }
155      public function test_config_file_contents() {
156          global $CFG;
158          unset($CFG->behat_config);
160          // List.
161          $features = array(
162              'feature1',
163              'feature2',
164              'feature3'
165          );
167          // Associative array.
168          $stepsdefinitions = array(
169              'micarro' => '/me/lo/robaron',
170              'anoche' => '/cuando/yo/dormia'
171          );
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);
182      }