commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / tests / phpunit / CRM / Utils / HttpClientTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3
4 /**
5 * Class CRM_Utils_HttpClientTest
6 */
7 class CRM_Utils_HttpClientTest extends CiviUnitTestCase {
8
9 const VALID_HTTP_URL = 'http://sandbox.civicrm.org/';
10 const VALID_HTTP_REGEX = '/<html/';
11 const VALID_HTTPS_URL = 'https://civicrm.org/INSTALL.mysql.txt';
12 const VALID_HTTPS_REGEX = '/MySQL/';
13 const SELF_SIGNED_HTTPS_URL = 'https://www-test.civicrm.org:4433/index.html';
14 const SELF_SIGNED_HTTPS_REGEX = '/self-signed/';
15
16 /**
17 * @var string path to which we can store temp file
18 */
19 protected $tmpFile;
20
21 /**
22 * @var CRM_Utils_HttpClient
23 */
24 protected $client;
25
26 public function setUp() {
27 parent::setUp();
28
29 $this->tmpFile = $this->createTempDir() . '/example.txt';
30
31 $result = civicrm_api('Setting', 'create', array(
32 'version' => 3,
33 'verifySSL' => TRUE,
34 ));
35 $this->assertAPISuccess($result);
36 $this->client = new CRM_Utils_HttpClient();
37 }
38
39 public function tearDown() {
40 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'");
41 CRM_Core_Config::singleton(TRUE);
42 parent::tearDown();
43 }
44
45 public function testFetchHttp() {
46 $result = $this->client->fetch(self::VALID_HTTP_URL, $this->tmpFile);
47 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result);
48 $this->assertRegExp(self::VALID_HTTP_REGEX, file_get_contents($this->tmpFile));
49 }
50
51 public function testFetchHttps_valid() {
52 $result = $this->client->fetch(self::VALID_HTTPS_URL, $this->tmpFile);
53 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result);
54 $this->assertRegExp(self::VALID_HTTPS_REGEX, file_get_contents($this->tmpFile));
55 }
56
57 public function testFetchHttps_invalid_verify() {
58 $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile);
59 $this->assertEquals(CRM_Utils_HttpClient::STATUS_DL_ERROR, $result);
60 $this->assertEquals('', file_get_contents($this->tmpFile));
61 }
62
63 public function testFetchHttps_invalid_noVerify() {
64 $result = civicrm_api('Setting', 'create', array(
65 'version' => 3,
66 'verifySSL' => FALSE,
67 ));
68 $this->assertAPISuccess($result);
69
70 $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile);
71 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result);
72 $this->assertRegExp(self::SELF_SIGNED_HTTPS_REGEX, file_get_contents($this->tmpFile));
73 }
74
75 public function testFetchHttp_badOutFile() {
76 $result = $this->client->fetch(self::VALID_HTTP_URL, '/ba/d/path/too/utput');
77 $this->assertEquals(CRM_Utils_HttpClient::STATUS_WRITE_ERROR, $result);
78 }
79
80 public function testGetHttp() {
81 list($status, $data) = $this->client->get(self::VALID_HTTP_URL);
82 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
83 $this->assertRegExp(self::VALID_HTTP_REGEX, $data);
84 }
85
86 public function testGetHttps_valid() {
87 list($status, $data) = $this->client->get(self::VALID_HTTPS_URL);
88 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
89 $this->assertRegExp(self::VALID_HTTPS_REGEX, $data);
90 }
91
92 public function testGetHttps_invalid_verify() {
93 list($status, $data) = $this->client->get(self::SELF_SIGNED_HTTPS_URL);
94 $this->assertEquals(CRM_Utils_HttpClient::STATUS_DL_ERROR, $status);
95 $this->assertEquals('', $data);
96 }
97
98 public function testGetHttps_invalid_noVerify() {
99 $result = civicrm_api('Setting', 'create', array(
100 'version' => 3,
101 'verifySSL' => FALSE,
102 ));
103 $this->assertAPISuccess($result);
104
105 list($status, $data) = $this->client->get(self::SELF_SIGNED_HTTPS_URL);
106 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
107 $this->assertRegExp(self::SELF_SIGNED_HTTPS_REGEX, $data);
108 }
109
110 }