comment fixes
[civicrm-core.git] / tests / phpunit / WebTest / Utils / RedirectTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 * Try redirect.
84 *
85 * @param string $input_url
86 * @param string $expected_url
87 */
88 private function tryRedirect($input_url, $expected_url) {
89 // file_put_contents('php://stderr', $input_url . "\n", FILE_APPEND);
90 $url = $this->url . '/' . $input_url;
91 $expected_url = $this->url . '/' . $expected_url;
92 curl_setopt($this->ch, CURLOPT_URL, $url);
93 $req = curl_exec($this->ch);
94 $this->assertEquals(0, curl_errno($this->ch), 'cURL error: ' . curl_error($this->ch));
95 if (!curl_errno($this->ch)) {
96 $info = curl_getinfo($this->ch);
97 // file_put_contents('php://stderr', print_r($info,1), FILE_APPEND);
98 $this->assertEquals($expected_url, $info['redirect_url']);
99 $this->assertEquals('302', $info['http_code']);
100 }
101 }
102
103 /**
104 * Handle onsite redirects with absolute URL.
105 */
106 public function testAbsoluteOnsiteRedirect() {
107 $this->tryRedirect("civicrm/contribute/transact?qfKey=xxx&entryURL={$this->url}/civicrm/contribute/transact%3Fid%3D1", 'civicrm/contribute/transact?id=1');
108 }
109
110 /**
111 * Handle onsite redirects with slash prefix and query params.
112 */
113 public function testOnsiteRedirectWithSlashPrefixAndQueryParams() {
114 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=/civicrm/contribute/transact%3Fid%3D1', 'civicrm/contribute/transact?id=1');
115 }
116
117 /**
118 * Handle onsite redirects with non-CiviCRM paths.
119 */
120 public function testOtherpathRedirect() {
121 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=asdf', 'asdf');
122 }
123
124 /**
125 * Handle offsite redirects without path as onsite redirects.
126 */
127 public function testOffsiteRedirectNoPath() {
128 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/', '');
129 }
130
131 /**
132 * Handle offsite redirects with paths as onsite redirects.
133 */
134 public function testOffsiteRedirectWithPath() {
135 $this->tryRedirect('civicrm/contribute/transact?qfKey=xxx&entryURL=http://evil.example.com/civicrm', 'civicrm');
136 }
137
138 }