Merge pull request #12280 from eileenmcnaughton/line_endings
[civicrm-core.git] / tests / phpunit / CRM / Utils / SystemTest.php
1 <?php
2
3 use Psr\Http\Message\UriInterface;
4
5 /**
6 * Class CRM_Utils_SystemTest
7 * @group headless
8 */
9 class CRM_Utils_SystemTest extends CiviUnitTestCase {
10
11 public function setUp() {
12 parent::setUp();
13 }
14
15 public function testUrlQueryString() {
16 $config = CRM_Core_Config::singleton();
17 $this->assertTrue($config->userSystem instanceof CRM_Utils_System_UnitTests);
18 $expected = '/index.php?q=civicrm/foo/bar&foo=ab&bar=cd%26ef';
19 $actual = CRM_Utils_System::url('civicrm/foo/bar', 'foo=ab&bar=cd%26ef', FALSE, NULL, FALSE);
20 $this->assertEquals($expected, $actual);
21 }
22
23 public function testUrlQueryArray() {
24 $config = CRM_Core_Config::singleton();
25 $this->assertTrue($config->userSystem instanceof CRM_Utils_System_UnitTests);
26 $expected = '/index.php?q=civicrm/foo/bar&foo=ab&bar=cd%26ef';
27 $actual = CRM_Utils_System::url('civicrm/foo/bar', array(
28 'foo' => 'ab',
29 'bar' => 'cd&ef',
30 ), FALSE, NULL, FALSE);
31 $this->assertEquals($expected, $actual);
32 }
33
34 public function testEvalUrl() {
35 $this->assertEquals(FALSE, CRM_Utils_System::evalUrl(FALSE));
36 $this->assertEquals('http://example.com/', CRM_Utils_System::evalUrl('http://example.com/'));
37 $this->assertEquals('http://example.com/?cms=UnitTests', CRM_Utils_System::evalUrl('http://example.com/?cms={uf}'));
38 }
39
40 /**
41 * Test the redirect hook.
42 *
43 * @param string $url
44 * @param array $parsedUrl
45 *
46 * @dataProvider getURLs
47 */
48 public function testRedirectHook($url, $parsedUrl) {
49 $this->hookClass->setHook('civicrm_alterRedirect', array($this, 'hook_civicrm_alterRedirect'));
50 try {
51 CRM_Utils_System::redirect($url, [
52 'expected' => $parsedUrl,
53 'original' => $url
54 ]);
55 }
56 catch (CRM_Core_Exception $e) {
57 $this->assertEquals(ts('hook called'), $e->getMessage());
58 return;
59 }
60 $this->fail('Exception should have been thrown if hook was called');
61 }
62
63 /**
64 * Hook for alterRedirect.
65 *
66 * We do some checks here.
67 *
68 * @param UriInterface $urlQuery
69 * @param array $context
70 *
71 * @throws \CRM_Core_Exception
72 */
73 public function hook_civicrm_alterRedirect($urlQuery, $context) {
74 $this->assertEquals(CRM_Utils_Array::value('scheme', $context['expected']), $urlQuery->getScheme());
75 $this->assertEquals(CRM_Utils_Array::value('host', $context['expected']), $urlQuery->getHost());
76 $this->assertEquals(CRM_Utils_Array::value('query', $context['expected']), $urlQuery->getQuery());
77 $this->assertEquals($context['original'], CRM_Utils_Url::unparseUrl($urlQuery));
78
79 throw new CRM_Core_Exception(ts('hook called'));
80 }
81
82 /**
83 * Get urls for testing.
84 *
85 * @return array
86 */
87 public function getURLs() {
88 return [
89 ['https://example.com?ab=cd', [
90 'scheme' => 'https',
91 'host' => 'example.com',
92 'query' => 'ab=cd',
93 ]],
94 ['http://myuser:mypass@foo.bar:123/whiz?a=b&c=d', [
95 'scheme' => 'http',
96 'host' => 'foo.bar',
97 'port' => 123,
98 'user' => 'myuser',
99 'pass' => 'mypass',
100 'path' => '/whiz',
101 'query' => 'a=b&c=d',
102 ]],
103 ['/foo/bar', [
104 'path' => '/foo/bar'
105 ]],
106 ];
107 }
108
109 }