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