commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tests / phpunit / WebTest / Utils / RedirectTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
25 */
26
27 require_once 'CiviTest/CiviUnitTestCase.php';
28 require_once 'CiviTest/CiviSeleniumSettings.php';
29
30 /**
31 * Include configuration
32 */
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);
36
37 if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH)) {
38 require_once CIVICRM_SETTINGS_LOCAL_PATH;
39 }
40 require_once CIVICRM_SETTINGS_PATH;
41
42
43 /**
44 * Check that we handle redirects appropriately.
45 */
46 class WebTest_Utils_RedirectTest extends CiviUnitTestCase {
47 protected $url;
48 protected $ch;
49
50 /**
51 * @param string|null $name
52 */
53 public function __construct($name = NULL) {
54 parent::__construct($name);
55
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
65 );
66 }
67 }
68 }
69
70 protected function setUp() {
71 parent::setUp();
72 //URL should eventually be adapted for multisite
73 $this->url = $this->settings->sandboxURL;
74
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);
80 }
81
82 /**
83 *
84 */
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']);
97 }
98 }
99
100 /**
101 * Handle onsite redirects with absolute URL.
102 */
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');
105 }
106
107 /**
108 * Handle onsite redirects with slash prefix and query params.
109 */
110 public function testOnsiteRedirectWithSlashPrefixAndQueryParams() {
111 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=/civicrm/contribute/transact%3Fid%3D1', 'civicrm/contribute/transact?id=1');
112 }
113
114 /**
115 * Handle onsite redirects with non-CiviCRM paths.
116 */
117 public function testOtherpathRedirect() {
118 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=asdf', 'asdf');
119 }
120
121 /**
122 * Handle offsite redirects without path as onsite redirects.
123 */
124 public function testOffsiteRedirectNoPath() {
125 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/', '');
126 }
127
128 /**
129 * Handle offsite redirects with paths as onsite redirects.
130 */
131 public function testOffsiteRedirectWithPath() {
132 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/civicrm', 'civicrm');
133 }
134
135 }