MDL-37657 behat: Adding acceptance tests to the acceptance test tool
[moodle.git] / admin / tool / behat / tests / tool_behat_test.php
CommitLineData
f2f66ea4
DM
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/>.
16
17/**
096858ed 18 * Unit tests for admin/tool/behat.
f2f66ea4
DM
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 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
b5c13009 28require_once($CFG->dirroot . '/' . $CFG->admin .'/tool/behat/locallib.php');
096858ed 29require_once($CFG->libdir . '/behat/classes/util.php');
b5c13009 30require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
f2f66ea4
DM
31
32/**
096858ed 33 * Allows access to internal methods without exposing them.
95167121
DM
34 *
35 * @package tool_behat
36 * @copyright 2012 David Monllaó
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f2f66ea4 38 */
b5c13009 39class testable_behat_config_manager extends behat_config_manager {
f2f66ea4
DM
40
41 /**
ff2ec29b 42 * Allow access to protected method
f2f66ea4
DM
43 * @see parent::merge_config()
44 * @param mixed $config
45 * @param mixed $localconfig
46 * @return mixed
47 */
48 public static function merge_config($config, $localconfig) {
49 return parent::merge_config($config, $localconfig);
50 }
51
52 /**
ff2ec29b 53 * Allow access to protected method
f2f66ea4
DM
54 * @see parent::get_config_file_contents()
55 * @param string $prefix
56 * @param array $features
57 * @param array $stepsdefinitions
58 * @return string
59 */
096858ed
DM
60 public static function get_config_file_contents($features, $stepsdefinitions) {
61 return parent::get_config_file_contents($features, $stepsdefinitions);
f2f66ea4
DM
62 }
63}
64
65/**
096858ed 66 * Tool behat tests.
95167121
DM
67 *
68 * @package tool_behat
69 * @copyright 2012 David Monllaó
70 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f2f66ea4
DM
71 */
72class tool_behat_testcase extends advanced_testcase {
73
ff2ec29b 74 /**
096858ed 75 * behat_config_manager tests.
ff2ec29b
DM
76 */
77 public function test_merge_configs() {
78
79 // Simple default config.
80 $array1 = array(
81 'the' => 'same',
82 'simple' => 'value',
83 'array' => array(
84 'one' => 'arrayvalue1',
85 'two' => 'arrayvalue2'
86 )
87 );
88
89 // Simple override.
90 $array2 = array(
91 'simple' => 'OVERRIDDEN1',
92 'array' => array(
93 'one' => 'OVERRIDDEN2'
94 ),
95 'newprofile' => array(
96 'anotherlevel' => array(
97 'andanotherone' => array(
98 'list1',
99 'list2'
100 )
101 )
102 )
103 );
104
b5c13009 105 $array = testable_behat_config_manager::merge_config($array1, $array2);
ff2ec29b
DM
106
107 // Overriddes are applied.
108 $this->assertEquals('OVERRIDDEN1', $array['simple']);
109 $this->assertEquals('OVERRIDDEN2', $array['array']['one']);
110
111 // Other values are respected.
112 $this->assertNotEmpty($array['array']['two']);
113
114 // Completely new nodes are added.
115 $this->assertNotEmpty($array['newprofile']);
116 $this->assertNotEmpty($array['newprofile']['anotherlevel']['andanotherone']);
117 $this->assertEquals('list1', $array['newprofile']['anotherlevel']['andanotherone'][0]);
118 $this->assertEquals('list2', $array['newprofile']['anotherlevel']['andanotherone'][1]);
119
120 // Complex override changing vectors to scalars and scalars to vectors.
121 $array2 = array(
122 'simple' => array(
123 'simple' => 'should',
124 'be' => 'overridden',
125 'by' => 'this-array'
126 ),
127 'array' => 'one'
128 );
129
b5c13009 130 $array = testable_behat_config_manager::merge_config($array1, $array2);
ff2ec29b
DM
131
132 // Overrides applied.
133 $this->assertNotEmpty($array['simple']);
134 $this->assertNotEmpty($array['array']);
135 $this->assertTrue(is_array($array['simple']));
136 $this->assertFalse(is_array($array['array']));
137
138 // Other values are maintained.
139 $this->assertEquals('same', $array['the']);
140 }
141
142 /**
096858ed 143 * behat_config_manager tests.
ff2ec29b
DM
144 */
145 public function test_config_file_contents() {
146 global $CFG;
147
ec017a34
DM
148 // Skip tests if behat is not installed.
149 $vendorpath = $CFG->dirroot . '/vendor';
150 if (!file_exists($vendorpath . '/autoload.php') || !is_dir($vendorpath . '/behat')) {
151 $this->markTestSkipped('Behat not installed.');
152 }
153
096858ed 154 // To avoid user value at config.php level.
ff2ec29b
DM
155 unset($CFG->behat_config);
156
157 // List.
158 $features = array(
159 'feature1',
160 'feature2',
161 'feature3'
162 );
163
164 // Associative array.
165 $stepsdefinitions = array(
166 'micarro' => '/me/lo/robaron',
167 'anoche' => '/cuando/yo/dormia'
168 );
169
096858ed 170 $contents = testable_behat_config_manager::get_config_file_contents($features, $stepsdefinitions);
ff2ec29b 171
096858ed 172 $this->assertContains('features: ' . $CFG->dirroot . '/lib/behat/features', $contents);
ff2ec29b
DM
173 $this->assertContains('micarro: /me/lo/robaron', $contents);
174 $this->assertContains('base_url: \'' . $CFG->behat_wwwroot . '\'', $contents);
175 $this->assertContains('class: behat_init_context', $contents);
176 $this->assertContains('- feature1', $contents);
177 $this->assertContains('- feature3', $contents);
178 }
179
f2f66ea4
DM
180}
181