Merge pull request #19806 from eileenmcnaughton/msg_compat
[civicrm-core.git] / tests / phpunit / CRM / Utils / HttpClientTest.php
CommitLineData
6a488035 1<?php
aba1cd8b
EM
2
3/**
4 * Class CRM_Utils_HttpClientTest
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035
TO
7class CRM_Utils_HttpClientTest extends CiviUnitTestCase {
8
001b60bb
TO
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\./';
6a488035
TO
15
16 /**
1d3260ea
SL
17 * @var string
18 * Path to which we can store temp file
6a488035
TO
19 */
20 protected $tmpFile;
21
3b6f287b
TO
22 /**
23 * @var CRM_Utils_HttpClient
24 */
25 protected $client;
26
6a488035
TO
27 public function setUp() {
28 parent::setUp();
29
30 $this->tmpFile = $this->createTempDir() . '/example.txt';
31
9099cab3 32 $result = civicrm_api('Setting', 'create', [
6a488035
TO
33 'version' => 3,
34 'verifySSL' => TRUE,
9099cab3 35 ]);
6a488035 36 $this->assertAPISuccess($result);
3b6f287b 37 $this->client = new CRM_Utils_HttpClient();
6a488035
TO
38 }
39
dd09ee0c 40 public function tearDown(): void {
6a488035
TO
41 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_setting WHERE name = 'verifySSL'");
42 CRM_Core_Config::singleton(TRUE);
43 parent::tearDown();
44 }
45
00be9182 46 public function testFetchHttp() {
3b6f287b 47 $result = $this->client->fetch(self::VALID_HTTP_URL, $this->tmpFile);
6a488035
TO
48 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result);
49 $this->assertRegExp(self::VALID_HTTP_REGEX, file_get_contents($this->tmpFile));
50 }
51
00be9182 52 public function testFetchHttps_valid() {
3b6f287b 53 $result = $this->client->fetch(self::VALID_HTTPS_URL, $this->tmpFile);
6a488035
TO
54 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result);
55 $this->assertRegExp(self::VALID_HTTPS_REGEX, file_get_contents($this->tmpFile));
56 }
57
00be9182 58 public function testFetchHttps_invalid_verify() {
3b6f287b 59 $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile);
6a488035
TO
60 $this->assertEquals(CRM_Utils_HttpClient::STATUS_DL_ERROR, $result);
61 $this->assertEquals('', file_get_contents($this->tmpFile));
62 }
63
00be9182 64 public function testFetchHttps_invalid_noVerify() {
9099cab3 65 $result = civicrm_api('Setting', 'create', [
6a488035
TO
66 'version' => 3,
67 'verifySSL' => FALSE,
9099cab3 68 ]);
6a488035
TO
69 $this->assertAPISuccess($result);
70
3b6f287b 71 $result = $this->client->fetch(self::SELF_SIGNED_HTTPS_URL, $this->tmpFile);
6a488035
TO
72 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $result);
73 $this->assertRegExp(self::SELF_SIGNED_HTTPS_REGEX, file_get_contents($this->tmpFile));
74 }
75
00be9182 76 public function testFetchHttp_badOutFile() {
3b6f287b 77 $result = $this->client->fetch(self::VALID_HTTP_URL, '/ba/d/path/too/utput');
6a488035
TO
78 $this->assertEquals(CRM_Utils_HttpClient::STATUS_WRITE_ERROR, $result);
79 }
80
00be9182 81 public function testGetHttp() {
afcc9be0
TO
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
00be9182 87 public function testGetHttps_valid() {
afcc9be0
TO
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
00be9182 93 public function testGetHttps_invalid_verify() {
afcc9be0
TO
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
00be9182 99 public function testGetHttps_invalid_noVerify() {
9099cab3 100 $result = civicrm_api('Setting', 'create', [
afcc9be0
TO
101 'version' => 3,
102 'verifySSL' => FALSE,
9099cab3 103 ]);
afcc9be0
TO
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
6a488035 111}