3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
27 require_once 'CiviTest/CiviUnitTestCase.php';
28 require_once 'CiviTest/CiviSeleniumSettings.php';
31 * Include configuration
33 define('CIVICRM_SETTINGS_PATH', __DIR__
. '/civicrm.settings.dist.php');
34 define('CIVICRM_SETTINGS_LOCAL_PATH', __DIR__
. '/civicrm.settings.local.php');
35 define('CIVICRM_WEBTEST', 1);
37 if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH
)) {
38 require_once CIVICRM_SETTINGS_LOCAL_PATH
;
40 require_once CIVICRM_SETTINGS_PATH
;
44 * Check that we handle redirects appropriately.
46 class WebTest_Utils_RedirectTest
extends CiviUnitTestCase
{
51 * @param string|null $name
53 public function __construct($name = NULL) {
54 parent
::__construct($name);
56 $this->settings
= new CiviSeleniumSettings();
57 if (property_exists($this->settings
, 'serverStartupTimeOut') && $this->settings
->serverStartupTimeOut
) {
58 global $CiviSeleniumTestCase_polled;
59 if (!$CiviSeleniumTestCase_polled) {
60 $CiviSeleniumTestCase_polled = TRUE;
61 CRM_Utils_Network
::waitForServiceStartup(
62 $this->drivers
[0]->getHost(),
63 $this->drivers
[0]->getPort(),
64 $this->settings
->serverStartupTimeOut
70 protected function setUp() {
72 //URL should eventually be adapted for multisite
73 $this->url
= $this->settings
->sandboxURL
;
75 $this->ch
= curl_init();
76 curl_setopt($this->ch
, CURLOPT_HEADER
, FALSE);
77 curl_setopt($this->ch
, CURLOPT_FOLLOWLOCATION
, FALSE);
78 // curl_setopt($this->ch, CURLOPT_ENCODING, 'gzip');
79 // curl_setopt($this->ch, CURLOPT_VERBOSE, 0);
85 private function tryRedirect($input_url, $expected_url) {
86 // file_put_contents('php://stderr', $input_url . "\n", FILE_APPEND);
87 $url = $this->url
. '/' . $input_url;
88 $expected_url = $this->url
. '/' . $expected_url;
89 curl_setopt($this->ch
, CURLOPT_URL
, $url);
90 $req = curl_exec($this->ch
);
91 $this->assertEquals(0, curl_errno($this->ch
), 'cURL error: ' . curl_error($this->ch
));
92 if (!curl_errno($this->ch
)) {
93 $info = curl_getinfo($this->ch
);
94 // file_put_contents('php://stderr', print_r($info,1), FILE_APPEND);
95 $this->assertEquals($expected_url, $info['redirect_url']);
96 $this->assertEquals('302', $info['http_code']);
101 * Handle onsite redirects with absolute URL.
103 public function testAbsoluteOnsiteRedirect() {
104 $this->tryRedirect("civicrm/contribute/transact?qfKey=xxx&entryURL={$this->url}/civicrm/contribute/transact%3Fid%3D1", 'civicrm/contribute/transact?id=1');
108 * Handle onsite redirects with slash prefix and query params.
110 public function testOnsiteRedirectWithSlashPrefixAndQueryParams() {
111 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=/civicrm/contribute/transact%3Fid%3D1', 'civicrm/contribute/transact?id=1');
115 * Handle onsite redirects with non-CiviCRM paths.
117 public function testOtherpathRedirect() {
118 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=asdf', 'asdf');
122 * Handle offsite redirects without path as onsite redirects.
124 public function testOffsiteRedirectNoPath() {
125 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/', '');
129 * Handle offsite redirects with paths as onsite redirects.
131 public function testOffsiteRedirectWithPath() {
132 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/civicrm', 'civicrm');