source: moodle/trunk/fuentes/admin/tool/behat/tests/manager_test.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 6.2 KB
Line 
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/**
18 * Unit tests for behat manager.
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;
28require_once($CFG->dirroot . '/' . $CFG->admin .'/tool/behat/locallib.php');
29require_once($CFG->libdir . '/behat/classes/util.php');
30require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
31
32/**
33 * Behat manager tests.
34 *
35 * @package    tool_behat
36 * @copyright  2012 David Monllaó
37 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class tool_behat_manager_testcase extends advanced_testcase {
40
41    /**
42     * behat_config_manager tests.
43     */
44    public function test_merge_configs() {
45
46        // Simple default config.
47        $array1 = array(
48            'the' => 'same',
49            'simple' => 'value',
50            'array' => array(
51                'one' => 'arrayvalue1',
52                'two' => 'arrayvalue2'
53            )
54        );
55
56        // Simple override.
57        $array2 = array(
58            'simple' => 'OVERRIDDEN1',
59            'array' => array(
60                'one' => 'OVERRIDDEN2'
61            ),
62            'newprofile' => array(
63                'anotherlevel' => array(
64                    'andanotherone' => array(
65                        'list1',
66                        'list2'
67                    )
68                )
69            )
70        );
71
72        $array = testable_behat_config_manager::merge_config($array1, $array2);
73
74        // Overrides are applied.
75        $this->assertEquals('OVERRIDDEN1', $array['simple']);
76        $this->assertEquals('OVERRIDDEN2', $array['array']['one']);
77
78        // Other values are respected.
79        $this->assertNotEmpty($array['array']['two']);
80
81        // Completely new nodes are added.
82        $this->assertNotEmpty($array['newprofile']);
83        $this->assertNotEmpty($array['newprofile']['anotherlevel']['andanotherone']);
84        $this->assertEquals('list1', $array['newprofile']['anotherlevel']['andanotherone'][0]);
85        $this->assertEquals('list2', $array['newprofile']['anotherlevel']['andanotherone'][1]);
86
87        // Complex override changing vectors to scalars and scalars to vectors.
88        $array2 = array(
89            'simple' => array(
90                'simple' => 'should',
91                'be' => 'overridden',
92                'by' => 'this-array'
93            ),
94            'array' => 'one'
95        );
96
97        $array = testable_behat_config_manager::merge_config($array1, $array2);
98
99        // Overrides applied.
100        $this->assertNotEmpty($array['simple']);
101        $this->assertNotEmpty($array['array']);
102        $this->assertTrue(is_array($array['simple']));
103        $this->assertFalse(is_array($array['array']));
104
105        // Other values are maintained.
106        $this->assertEquals('same', $array['the']);
107    }
108
109    /**
110     * behat_config_manager tests.
111     */
112    public function test_config_file_contents() {
113        global $CFG;
114
115        // Skip tests if behat is not installed.
116        $vendorpath = $CFG->dirroot . '/vendor';
117        if (!file_exists($vendorpath . '/autoload.php') || !is_dir($vendorpath . '/behat')) {
118            $this->markTestSkipped('Behat not installed.');
119        }
120
121        // Add some fake test url.
122        $CFG->behat_wwwroot = 'http://example.com/behat';
123
124        // To avoid user value at config.php level.
125        unset($CFG->behat_config);
126
127        // List.
128        $features = array(
129            'feature1',
130            'feature2',
131            'feature3'
132        );
133
134        // Associative array.
135        $stepsdefinitions = array(
136            'micarro' => '/me/lo/robaron',
137            'anoche' => '/cuando/yo/dormia'
138        );
139
140        $contents = testable_behat_config_manager::get_config_file_contents($features, $stepsdefinitions);
141
142        // YAML decides when is is necessary to wrap strings between single quotes, so not controlled
143        // values like paths should not be asserted including the key name as they would depend on the
144        // directories values.
145        $this->assertContains($CFG->dirroot . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'features', $contents);
146
147        // Not quoted strings.
148        $this->assertContains('micarro: /me/lo/robaron', $contents);
149        $this->assertContains('class: behat_init_context', $contents);
150
151        // YAML uses single quotes to wrap URL strings.
152        $this->assertContains("base_url: '" . $CFG->behat_wwwroot . "'", $contents);
153
154        // Lists.
155        $this->assertContains('- feature1', $contents);
156        $this->assertContains('- feature3', $contents);
157
158        unset($CFG->behat_wwwroot);
159    }
160
161}
162
163/**
164 * Allows access to internal methods without exposing them.
165 *
166 * @package    tool_behat
167 * @copyright  2012 David Monllaó
168 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
169 */
170class testable_behat_config_manager extends behat_config_manager {
171
172    /**
173     * Allow access to protected method
174     * @see parent::merge_config()
175     * @param mixed $config
176     * @param mixed $localconfig
177     * @return mixed
178     */
179    public static function merge_config($config, $localconfig) {
180        return parent::merge_config($config, $localconfig);
181    }
182
183    /**
184     * Allow access to protected method
185     * @see parent::get_config_file_contents()
186     * @param array $features
187     * @param array $stepsdefinitions
188     * @return string
189     */
190    public static function get_config_file_contents($features, $stepsdefinitions) {
191        return parent::get_config_file_contents($features, $stepsdefinitions);
192    }
193}
Note: See TracBrowser for help on using the repository browser.