1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8/*
9 * Parent class of all Selenium test cases.
10 */
11
12
13class TikiSeleniumTestCase extends PHPUnit_Extensions_Selenium2TestCase
14{
15	protected $backupGlobals = false;
16	var $current_test_db;
17	var $user_credentials = [
18			'admin' => 'tiki'
19			];
20
21	function __construct($name = '')
22	{
23		parent::__construct($name);
24		$this->configure();
25	}
26
27	private function configure()
28	{
29		$test_tiki_root_url = null;
30		$config_fpath = './tests_config.php';
31
32		if (! file_exists($config_fpath)) {
33			return false;
34		}
35
36		$lines = file($config_fpath);
37		$source = implode('', $lines);
38		echo "-- TikiSeleniumTestCase.configure: After reading config file: \$source='$source'\n";
39		eval($source);
40		echo "-- TikiSeleniumTestCase.configure: After evaluating config file: \$test_site_url='$test_site_url'\n";
41		if ($test_tiki_root_url == null) {
42			exit("Variable \$test_tiki_root_url MUST be defined in test configuration file: '$config_fpath'");
43		} else {
44			$this->setBrowserUrl($test_tiki_root_url);
45		}
46		if (! preg_match('/^http\:\/\/local/', $test_tiki_root_url)) {
47			exit("Error found in test configuration file '$config_fpath'\n" .
48					"The URL specified by \$test_tiki_root_url should start with http://local, in order to prevent accidentally running tests on a non-local test site.\n" .
49					"Value was: '$test_tiki_root_url'\n");
50		}
51	}
52
53	public function openTikiPage($tikiPage)
54	{
55		$this->open("http://localhost/tiki-trunk/$tikiPage");
56	}
57
58	public function restoreDBforThisTest()
59	{
60		$dbRestorer = new TikiAcceptanceTestDBRestorerSQLDumps();
61		$error_msg = $dbRestorer->restoreDB($this->current_test_db);
62		if ($error_msg != null) {
63			$this->markTestSkipped($error_msg);
64		}
65	}
66
67	public function logInIfNecessaryAs($my_user)
68	{
69		if (! $this->_login_as($my_user)) {
70			die("Couldn't log in as $my_user!");
71		}
72	}
73
74	public function logOutIfNecessary()
75	{
76		if ($this->isElementPresent("link=Logout")) {
77			$this->clickAndWait("link=Logout");
78		}
79	}
80
81	public function assertSelectElementContainsItems($selectElementID, $expItems, $message)
82	{
83		$this->assertElementPresent($selectElementID, "$message\nMarkup element '$selectElementID' did not exist");
84		$selectElementLabels = $this->getSelectOptions($selectElementID);
85		foreach ($expItems as $anItem => $anItemValue) {
86			$this->assertTrue(in_array($anItem, $selectElementLabels), "$message\n$anItem is not in the select element list");
87			$thisItemElementID = "$selectElementID/option[@value='$anItemValue']";
88			$this->assertElementPresent($thisItemElementID);
89		}
90	}
91
92	public function assertSelectElementContainsAllTheItems($selectElementID, $expItems, $message)
93	{
94		$this->assertElementPresent($selectElementID, "$message\nMarkup element '$selectElementID' did not exist");
95		$gotItemsText = $this->getSelectOptions($selectElementID);
96		$expItemsText = array_keys($expItems);
97		$this->assertEquals($gotItemsText, $expItemsText, "$message\nItems in the Select element '$selectElementID' were wrong.");
98		foreach ($expItems as $anItem => $anItemValue) {
99			$thisItemElementID = "$selectElementID/option[@value='$anItemValue']";
100			$this->assertElementPresent($thisItemElementID);
101		}
102	}
103
104	public function assertSelectElementDoesNotContainItems($selectElementID, $expItems, $message)
105	{
106		$this->assertElementPresent($selectElementID, "$message\nMarkup element '$selectElementID' did not exist");
107		$gotItemsText = $this->getSelectOptions($selectElementID);
108		$expItemsText = array_keys($expItems);
109		//        $this->assertEquals($gotItemsText, $expItemsText, "$message\nItems in the Select element '$selectElementID' were wrong.");
110		foreach ($expItems as $anItem => $anItemValue) {
111			$thisItemElementID = "$selectElementID/option[@value='$anItemValue']";
112			$this->assertFalse($this->isElementPresent($thisItemElementID));
113		}
114	}
115
116	private function _login_as($user)
117	{
118		if ($this->isElementPresent("sl-login-user")) {
119			$password = $this->user_credentials[$user];
120			$this->type("sl-login-user", $user);
121			$this->type("sl-login-pass", $password);
122			$this->clickAndWait("login");
123			if ($this->isTextPresent("Invalid username or password")) {
124				return false;
125			}
126		}
127		return true;
128	}
129
130	public function implode_with_key($glue = null, $pieces, $hifen = '=>')
131	{
132		$return = null;
133		foreach ($pieces as $tk => $tv) {
134			$return .= $glue . $tk . $hifen . $tv;
135		}
136		return substr($return, 1);
137	}
138}
139