3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 define('CIVICRM_WEBTEST', 1);
30 * Check that we handle redirects appropriately.
32 class WebTest_Utils_RedirectTest
extends PHPUnit_Framework_TestCase
{
37 * @param string|null $name
39 public function __construct($name = NULL) {
40 parent
::__construct($name);
42 // TODO: Just use $GLOBALS['_CV'] and don't bother with CiviSeleniumSettings.
43 $this->settings
= new CiviSeleniumSettings();
44 if (property_exists($this->settings
, 'serverStartupTimeOut') && $this->settings
->serverStartupTimeOut
) {
45 global $CiviSeleniumTestCase_polled;
46 if (!$CiviSeleniumTestCase_polled) {
47 $CiviSeleniumTestCase_polled = TRUE;
48 CRM_Utils_Network
::waitForServiceStartup(
49 $this->drivers
[0]->getHost(),
50 $this->drivers
[0]->getPort(),
51 $this->settings
->serverStartupTimeOut
57 protected function setUp() {
59 //URL should eventually be adapted for multisite
60 $this->url
= $this->settings
->sandboxURL
;
62 $this->ch
= curl_init();
63 curl_setopt($this->ch
, CURLOPT_HEADER
, FALSE);
64 curl_setopt($this->ch
, CURLOPT_FOLLOWLOCATION
, FALSE);
65 // curl_setopt($this->ch, CURLOPT_ENCODING, 'gzip');
66 // curl_setopt($this->ch, CURLOPT_VERBOSE, 0);
72 * @param string $input_url
73 * @param string $expected_url
75 private function tryRedirect($input_url, $expected_url) {
76 // file_put_contents('php://stderr', $input_url . "\n", FILE_APPEND);
77 $url = $this->url
. '/' . $input_url;
78 $expected_url = $this->url
. '/' . $expected_url;
79 curl_setopt($this->ch
, CURLOPT_URL
, $url);
80 $req = curl_exec($this->ch
);
81 $this->assertEquals(0, curl_errno($this->ch
), 'cURL error: ' . curl_error($this->ch
));
82 if (!curl_errno($this->ch
)) {
83 $info = curl_getinfo($this->ch
);
84 // file_put_contents('php://stderr', print_r($info,1), FILE_APPEND);
85 $this->assertEquals($expected_url, $info['redirect_url']);
86 $this->assertEquals('302', $info['http_code']);
91 * Handle onsite redirects with absolute URL.
93 public function testAbsoluteOnsiteRedirect() {
94 $this->tryRedirect("civicrm/contribute/transact?qfKey=xxx&entryURL={$this->url}/civicrm/contribute/transact%3Fid%3D1", 'civicrm/contribute/transact?id=1');
98 * Handle onsite redirects with slash prefix and query params.
100 public function testOnsiteRedirectWithSlashPrefixAndQueryParams() {
101 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=/civicrm/contribute/transact%3Fid%3D1', 'civicrm/contribute/transact?id=1');
105 * Handle onsite redirects with non-CiviCRM paths.
107 public function testOtherpathRedirect() {
108 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=asdf', 'asdf');
112 * Handle offsite redirects without path as onsite redirects.
114 public function testOffsiteRedirectNoPath() {
115 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/', '');
119 * Handle offsite redirects with paths as onsite redirects.
121 public function testOffsiteRedirectWithPath() {
122 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/civicrm', 'civicrm');