Clean and repair many phpunit tests (+ fix implied configuration)
[lhc/web/wiklou.git] / tests / phpunit / includes / SeleniumConfigurationTest.php
1 <?php
2
3 class SeleniumConfigurationTest extends MediaWikiTestCase {
4
5 /**
6 * The file where the test temporarity stores the selenium config.
7 * This should be cleaned up as part of teardown.
8 */
9 private $tempFileName;
10
11 /**
12 * String containing the a sample selenium settings
13 */
14 private $testConfig0 =
15 '
16 [SeleniumSettings]
17 browsers[firefox] = "*firefox"
18 browsers[iexplorer] = "*iexploreproxy"
19 browsers[chrome] = "*chrome"
20 host = "localhost"
21 port = "foobarr"
22 wikiUrl = "http://localhost/deployment"
23 username = "xxxxxxx"
24 userPassword = ""
25 testBrowser = "chrome"
26 startserver =
27 stopserver =
28 jUnitLogFile =
29 runAgainstGrid = false
30
31 [SeleniumTests]
32 testSuite[SimpleSeleniumTestSuite] = "tests/selenium/SimpleSeleniumTestSuite.php"
33 testSuite[TestSuiteName] = "testSuitePath"
34 ';
35 /**
36 * Array of expected browsers from $testConfig0
37 */
38 private $testBrowsers0 = array( 'firefox' => '*firefox',
39 'iexplorer' => '*iexploreproxy',
40 'chrome' => '*chrome'
41 );
42 /**
43 * Array of expected selenium settings from $testConfig0
44 */
45 private $testSettings0 = array(
46 'host' => 'localhost',
47 'port' => 'foobarr',
48 'wikiUrl' => 'http://localhost/deployment',
49 'username' => 'xxxxxxx',
50 'userPassword' => '',
51 'testBrowser' => 'chrome',
52 'startserver' => null,
53 'stopserver' => null,
54 'seleniumserverexecpath' => null,
55 'jUnitLogFile' => null,
56 'runAgainstGrid' => null
57 );
58 /**
59 * Array of expected testSuites from $testConfig0
60 */
61 private $testSuites0 = array(
62 'SimpleSeleniumTestSuite' => 'tests/selenium/SimpleSeleniumTestSuite.php',
63 'TestSuiteName' => 'testSuitePath'
64 );
65
66
67 /**
68 * Another sample selenium settings file contents
69 */
70 private $testConfig1 =
71 '
72 [SeleniumSettings]
73 host = "localhost"
74 testBrowser = "firefox"
75 ';
76 /**
77 * Expected browsers from $testConfig1
78 */
79 private $testBrowsers1 = null;
80 /**
81 * Expected selenium settings from $testConfig1
82 */
83 private $testSettings1 = array(
84 'host' => 'localhost',
85 'port' => null,
86 'wikiUrl' => null,
87 'username' => null,
88 'userPassword' => null,
89 'testBrowser' => 'firefox',
90 'startserver' => null,
91 'stopserver' => null,
92 'seleniumserverexecpath' => null,
93 'jUnitLogFile' => null,
94 'runAgainstGrid' => null
95 );
96 /**
97 * Expected test suites from $testConfig1
98 */
99 private $testSuites1 = null;
100
101
102 protected function setUp() {
103 parent::setUp();
104 if ( !defined( 'SELENIUMTEST' ) ) {
105 define( 'SELENIUMTEST', true );
106 }
107 }
108
109 /**
110 * Clean up the temporary file used to store the selenium settings.
111 */
112 protected function tearDown() {
113 if ( strlen( $this->tempFileName ) > 0 ) {
114 unlink( $this->tempFileName );
115 unset( $this->tempFileName );
116 }
117 parent::tearDown();
118 }
119
120 /**
121 * @expectedException MWException
122 * @group SeleniumFramework
123 */
124 public function testErrorOnIncorrectConfigFile() {
125 $seleniumSettings = array();
126 $seleniumBrowsers = array();
127 $seleniumTestSuites = array();
128
129 SeleniumConfig::getSeleniumSettings($seleniumSettings,
130 $seleniumBrowsers,
131 $seleniumTestSuites,
132 "Some_fake_settings_file.ini" );
133
134 }
135
136 /**
137 * @expectedException MWException
138 * @group SeleniumFramework
139 */
140 public function testErrorOnMissingConfigFile() {
141 $seleniumSettings = array();
142 $seleniumBrowsers = array();
143 $seleniumTestSuites = array();
144 global $wgSeleniumConfigFile;
145 $wgSeleniumConfigFile = '';
146 SeleniumConfig::getSeleniumSettings($seleniumSettings,
147 $seleniumBrowsers,
148 $seleniumTestSuites);
149 }
150
151 /**
152 * @group SeleniumFramework
153 */
154 public function testUsesGlobalVarForConfigFile() {
155 $seleniumSettings = array();
156 $seleniumBrowsers = array();
157 $seleniumTestSuites = array();
158 global $wgSeleniumConfigFile;
159 $this->writeToTempFile( $this->testConfig0 );
160 $wgSeleniumConfigFile = $this->tempFileName;
161 SeleniumConfig::getSeleniumSettings($seleniumSettings,
162 $seleniumBrowsers,
163 $seleniumTestSuites);
164 $this->assertEquals($seleniumSettings, $this->testSettings0 ,
165 'The selenium settings should have been read from the file defined in $wgSeleniumConfigFile'
166 );
167 $this->assertEquals($seleniumBrowsers, $this->testBrowsers0,
168 'The available browsers should have been read from the file defined in $wgSeleniumConfigFile'
169 );
170 $this->assertEquals($seleniumTestSuites, $this->testSuites0,
171 'The test suites should have been read from the file defined in $wgSeleniumConfigFile'
172 );
173 }
174
175 /**
176 * @group SeleniumFramework
177 * @dataProvider sampleConfigs
178 */
179 public function testgetSeleniumSettings($sampleConfig, $expectedSettings, $expectedBrowsers, $expectedSuites ) {
180 $this->writeToTempFile( $sampleConfig );
181 $seleniumSettings = array();
182 $seleniumBrowsers = array();
183 $seleniumTestSuites = null;
184
185 SeleniumConfig::getSeleniumSettings($seleniumSettings,
186 $seleniumBrowsers,
187 $seleniumTestSuites,
188 $this->tempFileName );
189
190 $this->assertEquals($seleniumSettings, $expectedSettings,
191 "The selenium settings for the following test configuration was not retrieved correctly" . $sampleConfig
192 );
193 $this->assertEquals($seleniumBrowsers, $expectedBrowsers,
194 "The available browsers for the following test configuration was not retrieved correctly" . $sampleConfig
195 );
196 $this->assertEquals($seleniumTestSuites, $expectedSuites,
197 "The test suites for the following test configuration was not retrieved correctly" . $sampleConfig
198 );
199
200
201 }
202
203 /**
204 * create a temp file and write text to it.
205 * @param $testToWrite the text to write to the temp file
206 */
207 private function writeToTempFile($textToWrite) {
208 $this->tempFileName = tempnam(sys_get_temp_dir(), 'test_settings.');
209 $tempFile = fopen( $this->tempFileName, "w" );
210 fwrite($tempFile , $textToWrite);
211 fclose($tempFile);
212 }
213
214 /**
215 * Returns an array containing:
216 * The contents of the selenium cingiguration ini file
217 * The expected selenium configuration array that getSeleniumSettings should return
218 * The expected available browsers array that getSeleniumSettings should return
219 * The expected test suites arrya that getSeleniumSettings should return
220 */
221 public function sampleConfigs() {
222 return array(
223 array($this->testConfig0, $this->testSettings0, $this->testBrowsers0, $this->testSuites0 ),
224 array($this->testConfig1, $this->testSettings1, $this->testBrowsers1, $this->testSuites1 )
225 );
226 }
227
228
229 }