]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/data/Bug45896Test.php
Release 6.5.4
[Github/sugarcrm.git] / tests / data / Bug45896Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37
38
39 /**
40  * @brief Try to test download.php for php notices
41  * @ticket 45896
42  */
43 class Bug45896Test extends Sugar_PHPUnit_Framework_TestCase
44 {
45     private $curl = null;
46     private $sessionName = '';
47     private $sessionId = '';
48     private $backup = array();
49     private $user = null;
50
51     /**
52      * @brief Here we create valid session for anonymous user
53      * @return void
54      */
55     public function setUp()
56     {
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);
61
62         $this->user = SugarTestUserUtilities::createAnonymousUser();
63
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);
71         if ($error != 0)
72         {
73             $this->fail('Curl returns incorrect code');
74         }
75
76         $headers = explode("\r\n", trim($headers));
77         foreach ($headers as $header)
78         {
79             $header = explode(': ', $header, 2);
80             if ($header[0] == 'Set-Cookie')
81             {
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];
87             }
88         }
89
90         session_id($this->sessionId);
91         @session_start();
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)
96         {
97             $this->fail('You must set unique_key value in config.php');
98         }
99         session_write_close();
100     }
101
102     /**
103      * @brief query strings for download random file
104      * @return array
105      */
106     public function getQueryString()
107     {
108         return array(
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')
113         );
114     }
115
116         /**
117      * @brief try to download files and to check response for notices
118          * @dataProvider getQueryString
119      * @group 45896
120      * 
121          * @param array $queryString query string to download any file url
122          */
123         public function testDownload($queryString)
124         {
125         $this->markTestIncomplete('Need mgusev to fix this test');
126         return;
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);
130         curl_setopt(
131             $this->curl,
132             CURLOPT_COOKIE,
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
140         );
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
145         {
146             $this->fail('Curl returns incorrect code');
147         }
148         elseif ($stat['http_code'] != 200) // need only success header
149         {
150             $this->fail('Incorrect HTTP code for test');
151         }
152
153         // getting headers
154         $content = explode("\r\n\r\n", $content, 2);
155         $content[0] = explode("\r\n", $content[0]);
156         $headers = array();
157         foreach ($content[0] as $header)
158         {
159             $header = explode(': ', $header, 2);
160             if (count($header) != 2)
161             {
162                 continue;
163             }
164             $headers[strtolower($header[0])] = $header[1];
165         }
166         $content = $content[1];
167
168         // parse for type of content
169         $headers['content-type'] = explode('/', $headers['content-type'], 2);
170         $headers['content-type'] = strtolower(reset($headers['content-type']));
171
172         // thinking what image and application type is valid, text is our test place, other types are fail
173         switch ($headers['content-type']) {
174             case 'image' :
175             case 'application' :
176                 {
177                     $this->assertNotEmpty($content, 'Content should be not empty');
178                 }
179                 break;
180             case 'text' :
181                 {
182                     $this->assertContains(
183                         $content,
184                         array(
185                              'Not a Valid Entry Point',
186                              'Error. This type is not valid.',
187                              'Invalid File Reference'
188                         ),
189                         'Got php notice'
190                     );
191                 }
192                 break;
193             default :
194                 {
195                     $this->fail('Received unknown content type');
196                 }
197         }
198         }
199
200     /**
201      * @brief closing curl connection and restore php.ini parameters
202      * @return void
203      */
204     public function tearDown()
205     {
206         curl_close($this->curl);
207         foreach ($this->backup as $k=>$v)
208         {
209             ini_set($k, $v);
210         }
211     }
212 }