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