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