3 * Copyright (C) 2007-2008 Alain Peyrat <aljeux at free dot fr>
4 * Copyright (C) 2009 Alain Peyrat, Alcatel-Lucent
5 * Copyright 2013,2019-2020, Franck Villaume - TrivialDev
6 * Copyright (C) 2015 Inria (Sylvain Beucler)
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * Standard Alcatel-Lucent disclaimer for contributing to open source
28 * "The test suite ("Contribution") has not been tested and/or
29 * validated for release as or in products, combinations with products or
30 * other commercial use. Any use of the Contribution is entirely made at
31 * the user's own responsibility and the user can not rely on any features,
32 * functionalities or performances Alcatel-Lucent has attributed to the
35 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
36 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
37 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
38 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
39 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
40 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
41 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
43 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
48 *Copyright (c) 2010-2013, Sebastian Bergmann <sebastian@phpunit.de>.
49 * All rights reserved.
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
55 * * Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
58 * * Redistributions in binary form must reproduce the above copyright
59 * notice, this list of conditions and the following disclaimer in
60 * the documentation and/or other materials provided with the
63 * * Neither the name of Sebastian Bergmann nor the names of his
64 * contributors may be used to endorse or promote products derived
65 * from this software without specific prior written permission.
67 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
68 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
69 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
70 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
71 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
72 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
73 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
74 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
75 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
77 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
78 * POSSIBILITY OF SUCH DAMAGE.
81 define('FORGE_ADMIN_USERNAME', 'admin');
82 define('FORGE_ADMIN_PASSWORD', 'my_Admin7');
83 define('FORGE_OTHER_PASSWORD', 'toto_Tata8');
85 $config = dirname(__FILE__).'/config.php';
88 if (@include_once '/usr/local/share/php/vendor/autoload.php') {
90 require_once 'PHPUnit/Extensions/Selenium2TestCase.php';
93 class FForge_SeleniumTestCase extends PHPUnit\Extensions\Selenium2TestCase
95 public $logged_in = false ;
96 public $fixture = 'base';
97 public $fixture_loaded = false;
99 public function setUp(): void {
100 $this->configureSelenium();
101 $this->loadCachedFixture();
104 public function configureSelenium() {
105 if (getenv('SELENIUM_RC_DIR') && getenv('SELENIUM_RC_URL')) {
106 $this->captureScreenshotOnFailure = true;
107 $this->screenshotPath = getenv('SELENIUM_RC_DIR');
108 $this->screenshotUrl = getenv('SELENIUM_RC_URL');
111 $this->setBrowser('firefox');
112 $capabilities = array('acceptInsecureCerts' => true);
113 $this->setDesiredCapabilities($capabilities);
114 $this->setBrowserUrl(URL);
115 $this->setHost(SELENIUM_RC_HOST);
117 // Use a sensible default background (instead of Selenium's criminal default to black)
118 // (future-proof - https://github.com/giorgiosironi/phpunit-selenium/commit/07e50f74f3782ce8781527653e6c79aeefd94ada)
119 $this->screenshotBgColor = '#CCFFDD';
123 * Load existing fixture.
124 * Mainly used to load the 'base' fixture in tests that don't call loadAndCacheFixture() yet
126 public function loadCachedFixture() {
127 $this->fixture_loaded = false;
128 $base_cmd = dirname(__FILE__)."/fixtures.sh";
130 passthru("$base_cmd --exists {$this->fixture}", $ret); ob_flush();
132 # wait until the test starts (with a valid Selenium session) to generate the fixture
134 passthru("$base_cmd {$this->fixture}", $ret); ob_flush();
136 die("Error running: $base_cmd {$this->fixture}");
137 $this->fixture_loaded = true;
142 * We can't run the fixture in setUp nor even in assertPreConditions
143 * because the Selenium session isn't started yet >(
145 * Postponing fixture caching after the test is run.
146 * Call this first in your test.
148 * Alternatively we could use SQL-based fixtures (rather than
149 * Selenium-based fixtures)
151 public function loadAndCacheFixture() {
152 if (!$this->fixture_loaded) {
153 $base_cmd = dirname(__FILE__)."/fixtures.sh";
155 passthru("$base_cmd base", $ret); ob_flush();
157 require(dirname(__FILE__)."/fixtures/{$this->fixture}.php");
159 $this->fixture_loaded = true;
161 passthru("$base_cmd --backup {$this->fixture}", $ret); ob_flush();
166 public function changeConfig($config) {
167 $config_path = rtrim(`forge_get_config config_path`);
168 $classname = get_class($this);
171 foreach ($config as $section => $sv) {
172 $contents .= "[$section]\n";
173 foreach ($sv as $variable => $value) {
174 $contents .= "$variable = $value\n";
178 file_put_contents("$config_path/config.ini.d/zzz-buildbot-$classname.ini",
182 public function openWithOneRetry($url) {
186 catch (Exception $e) {
191 public function clickAndWait($link) {
192 if (preg_match('/^jquery#/', $link)) {
193 $elementid = substr($link, 7);
194 $this->execute(array(
195 'script' => "jQuery('a[href=\"#$elementid\"').click()",
199 for ($second = 0; ; $second++) {
200 if ($second >= 30) $this->fail("timeout");
202 if (preg_match('/^link=/', $link)) {
203 $text = substr($link, 5);
204 $myelement = $this->byLinkText($text);
205 } else if (preg_match('/^id=/', $link)) {
206 $id = substr($link, 3);
207 $myelement = $this->byId($id);
208 } else if (preg_match('/^css=/', $link)) {
209 $css = substr($link, 4);
210 $myelement = $this->byCssSelector($css);
211 } else if (preg_match('/^\/\/[a-z]/', $link)) {
212 $myelement = $this->byXPath($link);
215 $myelement = $this->byName($link);
217 if ($myelement->displayed()) break;
218 } catch (Exception $e) {}
224 } catch (Exception $e) {
225 $this->url($myelement->attribute('href'));
231 public function waitForTextPresent($text) {
232 for ($second = 0; ; $second++) {
233 if ($second >= 30) $this->fail("timeout");
235 if ($this->isTextPresent($text)) break;
236 } catch (Exception $e) {}
242 public function runCommand($cmd) {
244 $this->assertEquals(0, $ret);
248 function runCommandTimeout($dir, $command, $env='') {
249 # Disable timeout so we have a chance to gdb the stalled process:
250 #$cmd = "cd $dir && $env timeout 15s $command";
251 $cmd = "cd $dir && $env $command";
253 if ($ret == 124) { # retry once if we get a timeout
256 if ($ret == 124) { # retry a second time if we get a timeout again
259 $this->assertEquals(0, $ret); # Give up
263 public function cron($cmd) {
264 $this->runCommand("forge_run_job $cmd");
267 public function cron_for_plugin($cmd, $plugin) {
268 $this->runCommand("forge_run_plugin_job $plugin $cmd");
272 * Execute pending system tasks
274 public function waitSystasks() {
275 $this->runCommand(dirname(__FILE__).'/../../src/bin/systasks_wait_until_empty.php');
278 public function init() {
279 $this->createAndGoto('ProjectA');
282 public function populateStandardTemplate($what='all') {
283 if ($what == 'all') {
284 $what = array('trackers','tasks','forums');
285 } elseif ($what == 'empty') {
287 } elseif (!is_array($what)) {
288 $what = array($what) ;
290 $this->switchUser (FORGE_ADMIN_USERNAME) ;
292 $this->createProject ('Tmpl');
294 $this->url(ROOT."/admin/");
295 $this->clickAndWait("link=Display Full Project List/Edit Projects");
296 $this->clickAndWait("link=Tmpl");
297 $this->select($this->byXPath("//select[@name='form_template']"))->selectOptionByLabel("Yes");
298 $this->clickAndWait("submit");
300 $this->open( ROOT . '/projects/tmpl') ;
301 $this->waitForPageToLoad();
303 $this->clickAndWait("link=Admin");
304 $this->clickAndWait("link=Tools");
305 $this->check("//input[@name='use_forum']") ;
306 $this->check("//input[@name='use_tracker']") ;
307 $this->check("//input[@name='use_mail']") ;
308 $this->check("//input[@name='use_pm']") ;
309 $this->check("//input[@name='use_docman']") ;
310 $this->check("//input[@name='use_news']") ;
311 $this->check("//input[@name='use_frs']") ;
312 $this->clickAndWait("submit");
314 if (in_array ('trackers', $what)) {
315 $this->clickAndWait("link=Trackers Administration");
316 $this->type("name", "Bugs");
317 $this->type("//input[@name='description']", "Tracker for bug reports");
318 $this->clickAndWait("post_changes");
319 $this->assertTrue($this->isTextPresent("Tracker created successfully"));
320 $this->clickAndWait("link=Bugs");
321 $this->clickAndWait("link=Manage Custom Fields");
322 $this->type("name", "URL");
323 $this->type("alias", "url");
324 $this->clickAndWait("//input[@name='field_type' and @value=4]");
325 $this->clickAndWait("post_changes");
327 $this->clickAndWait("link=Admin");
328 $this->clickAndWait("link=Tools");
329 $this->clickAndWait("link=Trackers Administration");
330 $this->type("name", "Support Requests");
331 $this->type("//input[@name='description']", "Tracker for support requests");
332 $this->clickAndWait("post_changes");
333 $this->assertTrue($this->isTextPresent("Tracker created successfully"));
335 $this->type("name", "Patches");
336 $this->type("//input[@name='description']", "Proposed changes to code");
337 $this->clickAndWait("post_changes");
338 $this->assertTrue($this->isTextPresent("Tracker created successfully"));
340 $this->type("name", "Feature Requests");
341 $this->type("//input[@name='description']", "New features that people want");
342 $this->clickAndWait("post_changes");
343 $this->assertTrue($this->isTextPresent("Tracker created successfully"));
346 if (in_array ('tasks', $what)) {
347 $this->clickAndWait("link=Admin");
348 $this->clickAndWait("link=Tools");
349 $this->clickAndWait("link=Tasks Administration");
350 $this->clickAndWait("link=Add a Subproject");
351 $this->type("project_name", "To Do");
352 $this->type("//input[@name='description']", "Things we have to do");
353 $this->clickAndWait("submit");
354 $this->assertTrue($this->isTextPresent("Subproject Inserted"));
356 $this->type("project_name", "Next Release");
357 $this->type("//input[@name='description']", "Items for our next release");
358 $this->clickAndWait("submit");
359 $this->assertTrue($this->isTextPresent("Subproject Inserted"));
362 if (in_array ('forums', $what)) {
363 $this->clickAndWait("link=Admin");
364 $this->clickAndWait("link=Tools");
365 $this->clickAndWait("link=Forums Administration");
367 $this->clickAndWait("link=Add Forum");
368 $this->type("forum_name", "Open-Discussion");
369 $this->type("//input[@name='description']", "General Discussion");
370 $this->clickAndWait("submit");
371 $this->assertTrue($this->isTextPresent("Forum added successfully"));
373 $this->type("forum_name", "Help");
374 $this->type("//input[@name='description']", "Get Public Help");
375 $this->clickAndWait("submit");
376 $this->assertTrue($this->isTextPresent("Forum added successfully"));
378 $this->type("forum_name", "Developers-Discussion");
379 $this->type("//input[@name='description']", "Project Developer Discussion");
380 $this->clickAndWait("submit");
381 $this->assertTrue($this->isTextPresent("Forum added successfully"));
383 $this->clickAndWait("link=Forums");
384 $this->assertTrue($this->isTextPresent("open-discussion"));
385 $this->assertTrue($this->isTextPresent("Get Public Help"));
386 $this->assertTrue($this->isTextPresent("Project Developer Discussion"));
390 public function login($username) {
392 if ($this->isTextPresent('Log Out')) {
395 $this->clickAndWait("link=Log In");
396 $this->triggeredLogin($username);
399 public function triggeredLogin($username) {
400 if ($username == FORGE_ADMIN_USERNAME) {
401 $password = FORGE_ADMIN_PASSWORD;
403 $password = FORGE_OTHER_PASSWORD;
406 $this->type("form_loginname", $username);
407 $this->type("form_pw", $password);
408 $this->clickAndWait("login");
410 $this->logged_in = $username;
413 public function logout() {
414 $this->open( ROOT ."/account/logout.php" );
415 $this->logged_in = false;
418 public function switchUser($username) {
419 if ($this->logged_in != $username) {
421 $this->login($username);
425 public function isLoginRequired() {
426 return $this->isTextPresent("You've been redirected to this login page") ;
429 public function isPermissionDenied() {
430 return $this->isTextPresent("Permission denied") ;
433 public function registerProject ($name, $user, $scm='scmsvn') {
434 $unix_name = strtolower($name);
436 $saved_user = $this->logged_in ;
437 $this->switchUser ($user) ;
439 $this->url(ROOT."/my/");
440 $this->clickAndWait("link=Register Project");
441 $this->type("full_name", $name);
442 $this->type("purpose", "This is a simple description for $name");
443 $this->type("//textarea[@name='description']", "This is the public description for $name.");
444 $this->type("unix_name", $unix_name);
445 $this->clickAndWait("//input[@name='scm' and @value='$scm']");
448 $this->select($this->byXPath("//select[@name='built_from_template']"))->selectOptionByLabel("Tmpl");
449 } catch (Exception $e) {}
451 $this->clickAndWait("submit");
452 $this->assertTextPresent("Your project has been automatically approved");
454 $this->switchUser ($saved_user) ;
457 public function approveProject ($name, $user) {
458 $unix_name = strtolower($name);
460 $saved_user = $this->logged_in ;
461 $this->switchUser ($user) ;
463 $this->open( ROOT . '/admin/approve-pending.php') ;
464 $this->waitForPageToLoad();
465 $this->clickAndWait("document.forms['approve.$unix_name'].submit");
467 $this->assertTrue($this->isTextPresent("Approving Project: $unix_name"));
469 $this->switchUser ($saved_user) ;
472 public function createProject ($name, $scm='scmsvn') {
473 $unix_name = strtolower($name);
475 $this->switchUser (FORGE_ADMIN_USERNAME) ;
477 // Create a simple project.
478 $this->registerProject($name, FORGE_ADMIN_USERNAME, $scm);
481 public function createAndGoto($project) {
482 $this->createProject($project);
483 $this->gotoProject($project);
486 public function createUser ($login) {
487 $this->switchUser(FORGE_ADMIN_USERNAME);
489 $this->clickAndWait("link=Site Admin");
490 $this->clickAndWait("link=Register a New User");
491 $this->type("unix_name", $login);
492 $this->type("password1", FORGE_OTHER_PASSWORD);
493 $this->type("password2", FORGE_OTHER_PASSWORD);
494 $this->type("firstname", $login);
495 $this->type("lastname", "Lastname");
496 $this->type("email", $login."@debug.log");
497 $this->clickAndWait("submit");
498 $this->clickAndWait("link=Site Admin");
499 $this->clickAndWait("link=Display Full User List/Edit Users");
500 $this->clickAndWait("//table/tbody/tr/td/a[contains(@href,'useredit.php') and contains(.,'($login)')]/../..//a[contains(@href, 'userlist.php?action=activate&user_id=')]");
503 public function activatePlugin($pluginName) {
504 $this->switchUser(FORGE_ADMIN_USERNAME);
505 $this->open( ROOT . '/admin/pluginman.php?update='.$pluginName.'&action=deactivate');
506 $this->waitForPageToLoad();
507 $this->open( ROOT . '/admin/pluginman.php?update='.$pluginName.'&action=activate');
508 $this->waitForPageToLoad();
512 public function gotoProject($project) {
513 $unix_name = strtolower($project);
515 $this->open( ROOT . '/projects/' . $unix_name) ;
516 $this->waitForPageToLoad();
517 $this->assertTrue($this->isTextPresent("This is the public description for $project."));
520 public function uploadSshKey () {
521 // Prepare client config
522 $sshdir = getenv('HOME') . '/.ssh';
523 if (!file_exists($sshdir)) {
525 chmod($sshdir, 0700);
527 $config = $sshdir . '/config';
528 if (!file_exists($config) or
529 // Avoid OpenSSH host fingerprint prompt
530 count(preg_grep('/StrictHostKeyChecking/', file($config))) == 0) {
531 $f = fopen($config, 'a');
532 fwrite($f, 'StrictHostKeyChecking no');
535 chmod($sshdir . '/config', 0600);
537 // Generate user keys
538 $privkey = $sshdir . '/id_rsa';
539 $pubkey = $sshdir . '/id_rsa.pub';
540 if (!file_exists($pubkey)) {
541 system("ssh-keygen -N '' -f $privkey");
544 // Upload keys to the web interface
545 $keys = file($pubkey);
547 $this->assertEquals(1, count($keys));
548 $this->clickAndWait("link=My Account");
549 $this->clickAndWait("link=Edit Keys");
550 $this->type("authorized_key", $k);
551 $this->clickAndWait("submit");
554 public function skip_test($msg) {
555 $this->captureScreenshotOnFailure = false;
556 $this->markTestSkipped($msg);
559 public function skip_on_rpm_installs($msg='Skipping on installations from RPM') {
560 if (INSTALL_METHOD == 'rpm') {
561 $this->skip_test($msg);
565 public function skip_on_deb_installs($msg='Skipping on installations from *.deb') {
566 if (INSTALL_METHOD == 'deb') {
567 $this->skip_test($msg);
571 public function skip_on_src_installs($msg='Skipping on installations from source') {
572 if (INSTALL_METHOD == 'src') {
573 $this->skip_test($msg);
577 public function skip_on_centos($msg='Skipping on CentOS platforms') {
578 if (INSTALL_OS == 'centos') {
579 $this->skip_test($msg);
583 public function skip_on_debian($msg='Skipping on Debian platforms') {
584 if (INSTALL_OS == 'debian') {
585 $this->skip_test($msg);
590 * add PHP wrappers for SeleniumTestCase compatibility
593 function open($url) {
597 function isTextPresent($text) {
598 $elementArray = $this->execute(array(
599 'script' => 'return document.body;',
602 $element = $this->elementFromResponseValue($elementArray);
603 if (strpos($element->text(), $text) === false) {
609 function isElementPresent($element) {
611 if (preg_match('/^\/\/[a-z]/', $element)) {
612 if ($this->byXPath($element) instanceof PHPUnit_Extensions_Selenium2TestCase_Element) {
615 } elseif (preg_match('/^link=/', $element)) {
616 if ($this->byLinkText(substr($element, 5)) instanceof PHPUnit_Extensions_Selenium2TestCase_Element) {
620 } catch (Exception $e) {}
624 function getLocation() {
625 return $this->execute(array(
626 'script' => 'return window.location.href;',
631 function type($name, $value) {
632 if (preg_match('/^\/\/[a-z]/', $name)) {
633 $this->byXPath($name)->clear();
634 $this->byXPath($name)->value($value);
635 } else if (preg_match('/^id=/', $name)) {
636 $this->byId(substr($name, 3))->clear();
637 $this->byId(substr($name, 3))->value($value);
639 $this->byName($name)->clear();
640 $this->byName($name)->value($value);
644 function waitForPageToLoad($integer = 30000) {
645 //do we need to do something???
646 $this->pause($integer);
649 function pause($integer = 10000) {
653 function assertTextPresent($string) {
654 return $this->assertTrue($this->waitForTextPresent($string));
657 function assertTextNotPresent($string) {
658 return $this->assertFalse($this->isTextPresent($string));
661 function check($string) {
662 $myelement = $this->byXPath($string);
663 if (!$myelement->attribute('checked')) {
668 function uncheck($string) {
669 $myelement = $this->byXPath($string);
670 if ($myelement->attribute('checked')) {
676 $this->execute(array(
677 'script' => 'window.history.back();',
682 function getText($string) {
683 if (preg_match('/^\/\/[a-z]/', $string)) {
684 return $this->byXPath($string)->text();
688 function getValue($string) {
689 if (preg_match('/^\/\/[a-z]/', $string)) {
690 return $this->byXPath($string)->attribute('value');
694 function selectFrame($string) {
695 if (preg_match('/^id=/', $string)) {
696 $myelement = $this->byId(substr($string, 3));
700 $this->frame($myelement);
703 function assertElementPresent($string) {
705 if (preg_match('/^\/\/[a-z]/', $string)) {
706 $myelement = $this->byXPath($string);
709 } catch(Exception $e) {}
716 // c-file-style: "bsd"