2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
40 * @brief Try to test download.php for php notices
43 class Bug45896Test extends Sugar_PHPUnit_Framework_TestCase
46 private $sessionName = '';
47 private $sessionId = '';
48 private $backup = array();
52 * @brief Here we create valid session for anonymous user
55 public function setUp()
57 $this->backup['session.use_cookies'] = ini_get('session.use_cookies');
58 ini_set('session.use_cookies', false);
59 $this->backup['session.use_only_cookies'] = ini_get('session.use_only_cookies');
60 ini_set('session.use_only_cookies', false);
62 $this->user = SugarTestUserUtilities::createAnonymousUser();
64 $this->curl = curl_init();
65 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
66 curl_setopt($this->curl, CURLOPT_URL, $GLOBALS['sugar_config']['site_url']);
67 curl_setopt($this->curl, CURLOPT_HEADER, true);
68 curl_setopt($this->curl, CURLOPT_NOBODY, true);
69 $headers = curl_exec($this->curl);
70 $error = curl_errno($this->curl);
73 $this->fail('Curl returns incorrect code');
76 $headers = explode("\r\n", trim($headers));
77 foreach ($headers as $header)
79 $header = explode(': ', $header, 2);
80 if ($header[0] == 'Set-Cookie')
82 $header[1] = explode('; ', $header[1]);
83 $header = reset($header[1]);
84 $header = explode('=', $header, 2);
85 $this->sessionName = $header[0];
86 $this->sessionId = $header[1];
90 session_id($this->sessionId);
92 $_SESSION['authenticated_user_id'] = $this->user->id;
93 $_SESSION['authenticated_user_language'] = $GLOBALS['sugar_config']['default_language'];
94 $_SESSION['unique_key'] = $GLOBALS['sugar_config']['unique_key'];
95 if ($_SESSION['unique_key'] == false)
97 $this->fail('You must set unique_key value in config.php');
99 session_write_close();
103 * @brief query strings for download random file
106 public function getQueryString()
109 array('entryPoint=download&id=643da5f0-513c-0933-5222-4e521fc84036&type=SugarFieldImage&isTempFile=1'),
110 array('entryPoint=download&id=WeShouldTryToDownloadIncorrectFile&type=SugarFieldImage&isTempFile=1'),
111 array('entryPoint=download&id=WeShouldTryToDownloadIncorrectFile&type=SugarFieldImage'),
112 array('entryPoint=download&id=WeShouldTryToDownloadIncorrectFile&type=2&isTempFile=1')
117 * @brief try to download files and to check response for notices
118 * @dataProvider getQueryString
121 * @param array $queryString query string to download any file url
123 public function testDownload($queryString)
125 $this->markTestSkipped('Need mgusev to fix this test');
127 curl_setopt($this->curl, CURLOPT_HEADER, true);
128 curl_setopt($this->curl, CURLOPT_NOBODY, false);
129 curl_setopt($this->curl, CURLOPT_URL, $GLOBALS['sugar_config']['site_url'].'?'.$queryString);
133 'ck_login_id_20='.$this->user->id.'; '.
134 'ck_login_language_20=en_us; '.
135 'ck_login_theme_20=Sugar; '.
136 'globalLinksOpen=true; '.
137 'sugar_theme_gm_current=All; '.
138 'sugar_user_theme=Sugar; '.
139 $this->sessionName.'='.$this->sessionId
141 $content = curl_exec($this->curl);
142 $error = curl_errno($this->curl);
143 $stat = curl_getinfo($this->curl);
144 if ($error != 0) // need only valid curl result
146 $this->fail('Curl returns incorrect code');
148 elseif ($stat['http_code'] != 200) // need only success header
150 $this->fail('Incorrect HTTP code for test');
154 $content = explode("\r\n\r\n", $content, 2);
155 $content[0] = explode("\r\n", $content[0]);
157 foreach ($content[0] as $header)
159 $header = explode(': ', $header, 2);
160 if (count($header) != 2)
164 $headers[strtolower($header[0])] = $header[1];
166 $content = $content[1];
168 // parse for type of content
169 $headers['content-type'] = explode('/', $headers['content-type'], 2);
170 $headers['content-type'] = strtolower(reset($headers['content-type']));
172 // thinking what image and application type is valid, text is our test place, other types are fail
173 switch ($headers['content-type']) {
177 $this->assertNotEmpty($content, 'Content should be not empty');
182 $this->assertContains(
185 'Not a Valid Entry Point',
186 'Error. This type is not valid.',
187 'Invalid File Reference'
195 $this->fail('Received unknown content type');
201 * @brief closing curl connection and restore php.ini parameters
204 public function tearDown()
206 curl_close($this->curl);
207 foreach ($this->backup as $k=>$v)