]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Extensions/TicketListener/GoogleCode.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Extensions / TicketListener / GoogleCode.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @package    PHPUnit
38  * @subpackage Extensions_TicketListener
39  * @author     Jan Sorgalla <jsorgalla@googlemail.com>
40  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42  * @link       http://www.phpunit.de/
43  * @since      File available since Release 3.5.0
44  */
45
46 /**
47  * A ticket listener that interacts with the GoogleCode issue API.
48  *
49  * @package    PHPUnit
50  * @subpackage Extensions_TicketListener
51  * @author     Jan Sorgalla <jsorgalla@googlemail.com>
52  * @copyright  2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
53  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
54  * @version    Release: 3.5.13
55  * @link       http://www.phpunit.de/
56  * @since      Class available since Release 3.5.0
57  */
58 class PHPUnit_Extensions_TicketListener_GoogleCode extends PHPUnit_Extensions_TicketListener
59 {
60     private $email;
61     private $password;
62     private $project;
63
64     private $statusClosed;
65     private $statusReopened;
66
67     private $printTicketStateChanges;
68
69     private $authUrl    = 'https://www.google.com/accounts/ClientLogin';
70     private $apiBaseUrl = 'http://code.google.com/feeds/issues/p/%s/issues';
71     private $authToken;
72
73     /**
74      * @param string $email          The email associated with the Google account.
75      * @param string $password       The password associated with the Google account.
76      * @param string $project        The project name of the system under test (SUT) on Google Code.
77      * @param string $printTicketChanges Boolean flag to print the ticket state changes in the test result.
78      * @param string $statusClosed   The status name of the closed state.
79      * @param string $statusReopened The status name of the reopened state.
80      * @throws RuntimeException
81      */
82     public function __construct($email, $password, $project, $printTicketStateChanges = FALSE, $statusClosed = 'Fixed', $statusReopened = 'Started')
83     {
84         if (!extension_loaded('curl')) {
85             throw new RuntimeException('ext/curl is not available');
86         }
87
88         if (!extension_loaded('simplexml')) {
89             throw new RuntimeException('ext/simplexml is not available');
90         }
91
92         $this->email                   = $email;
93         $this->password                = $password;
94         $this->project                 = $project;
95         $this->statusClosed            = $statusClosed;
96         $this->statusReopened          = $statusReopened;
97         $this->printTicketStateChanges = $printTicketStateChanges;
98         $this->apiBaseUrl              = sprintf($this->apiBaseUrl, $project);
99     }
100
101     /**
102      * @param  integer $ticketId
103      * @return array
104      * @throws RuntimeException
105      */
106     public function getTicketInfo($ticketId = NULL)
107     {
108         if (!is_numeric($ticketId)) {
109             return array('status' => 'invalid_ticket_id');
110         }
111
112         $url    = $this->apiBaseUrl . '/full/' . $ticketId;
113         $header = array(
114           'Authorization: GoogleLogin auth=' . $this->getAuthToken()
115         );
116
117         list($status, $response) = $this->callGoogleCode($url, $header);
118
119         if ($status != 200 || !$response) {
120             return array('state' => 'unknown_ticket');
121         }
122
123         $ticket = new SimpleXMLElement(str_replace("xmlns=", "ns=", $response));
124         $result = $ticket->xpath('//issues:state');
125         $state  = (string)$result[0];
126
127         if ($state === 'open') {
128             return array('status' => 'new');
129         }
130
131         if ($state === 'closed') {
132             return array('status' => 'closed');
133         }
134
135         return array('status' => $state);
136     }
137
138     /**
139      * @param string $ticketId   The ticket number of the ticket under test (TUT).
140      * @param string $statusToBe The status of the TUT after running the associated test.
141      * @param string $message    The additional message for the TUT.
142      * @param string $resolution The resolution for the TUT.
143      * @throws RuntimeException
144      */
145     protected function updateTicket($ticketId, $statusToBe, $message, $resolution)
146     {
147         $url = $this->apiBaseUrl . '/' . $ticketId . '/comments/full';
148
149         $header = array(
150           'Authorization: GoogleLogin auth=' . $this->getAuthToken(),
151           'Content-Type: application/atom+xml'
152         );
153
154         if ($statusToBe == 'closed') {
155             $ticketStatus = $this->statusClosed;
156         } else {
157             $ticketStatus = $this->statusReopened;
158         }
159
160         list($author,) = explode('@', $this->email);
161
162         $post = '<?xml version="1.0" encoding="UTF-8"?>' .
163                 '<entry xmlns="http://www.w3.org/2005/Atom" ' .
164                 '       xmlns:issues="http://schemas.google.com/projecthosting/issues/2009">' .
165                 '  <content type="html">' . htmlspecialchars($message, ENT_COMPAT, 'UTF-8') . '</content>' .
166                 '  <author>' .
167                 '    <name>' . htmlspecialchars($author, ENT_COMPAT, 'UTF-8') . '</name>' .
168                 '  </author>' .
169                 '  <issues:updates>' .
170                 '    <issues:status>' . htmlspecialchars($ticketStatus, ENT_COMPAT, 'UTF-8') . '</issues:status>' .
171                 '  </issues:updates>' .
172                 '</entry>';
173
174         list($status, $response) = $this->callGoogleCode($url, $header, $post);
175
176         if ($status != 201) {
177             throw new RuntimeException('Updating GoogleCode issue failed with status code ' . $status);
178         }
179
180         if ($this->printTicketStateChanges) {
181             printf(
182                 "\nUpdating GoogleCode issue #%d, status: %s\n",
183                 $ticketId,
184                 $statusToBe
185             );
186         }
187     }
188
189     /**
190      * @return string The auth token
191      * @throws RuntimeException
192      */
193     private function getAuthToken()
194     {
195         if (NULL !== $this->authToken) {
196             return $this->authToken;
197         }
198
199         $header = array(
200             'Content-Type: application/x-www-form-urlencoded',
201         );
202
203         $post = array(
204             'accountType' => 'GOOGLE',
205             'Email'       => $this->email,
206             'Passwd'      => $this->password,
207             'service'     => 'code',
208             'source'      => 'PHPUnit-TicketListener_GoogleCode-' . PHPUnit_Runner_Version::id(),
209         );
210
211         list($status, $response) = $this->callGoogleCode(
212             $this->authUrl,
213             $header,
214             http_build_query($post, NULL, '&')
215         );
216
217         if ($status != 200) {
218             throw new RuntimeException('Google account authentication failed');
219         }
220
221         foreach (explode("\n", $response) as $line) {
222             if (strpos(trim($line), 'Auth') === 0) {
223                 list($name, $token) = explode('=', $line);
224                 $this->authToken    = trim($token);
225                 break;
226             }
227         }
228
229         if (NULL === $this->authToken) {
230             throw new RuntimeException('Could not detect auth token in response');
231         }
232
233         return $this->authToken;
234     }
235
236     /**
237      * @param string  $url URL to call
238      * @param array   $header Header
239      * @param string  $post Post data
240      * @return array
241      */
242     private function callGoogleCode($url, array $header = NULL, $post = NULL)
243     {
244         $curlHandle = curl_init();
245
246         curl_setopt($curlHandle, CURLOPT_URL, $url);
247         curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, TRUE);
248         curl_setopt($curlHandle, CURLOPT_FAILONERROR, TRUE);
249         curl_setopt($curlHandle, CURLOPT_FRESH_CONNECT, TRUE);
250         curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, TRUE);
251         curl_setopt($curlHandle, CURLOPT_HTTPPROXYTUNNEL, TRUE);
252         curl_setopt($curlHandle, CURLOPT_USERAGENT, __CLASS__);
253         curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, FALSE);
254
255         if (NULL !== $header) {
256             curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $header);
257         }
258
259         if (NULL !== $post) {
260             curl_setopt($curlHandle, CURLOPT_POST, TRUE);
261             curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $post);
262         }
263
264         $response = curl_exec($curlHandle);
265         $status   = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
266
267         if (!$response) {
268             throw new RuntimeException(curl_error($curlHandle));
269         }
270
271         curl_close($curlHandle);
272
273         return array($status, $response);
274     }
275 }