Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
f9bdf062 | 3 | use Psr\Http\Message\UriInterface; |
4 | ||
aba1cd8b EM |
5 | /** |
6 | * Class CRM_Utils_SystemTest | |
acb109b7 | 7 | * @group headless |
aba1cd8b | 8 | */ |
6a488035 TO |
9 | class CRM_Utils_SystemTest extends CiviUnitTestCase { |
10 | ||
00be9182 | 11 | public function setUp() { |
6a488035 TO |
12 | parent::setUp(); |
13 | } | |
14 | ||
00be9182 | 15 | public function testUrlQueryString() { |
6a488035 TO |
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'; | |
6c6e6187 | 19 | $actual = CRM_Utils_System::url('civicrm/foo/bar', 'foo=ab&bar=cd%26ef', FALSE, NULL, FALSE); |
6a488035 TO |
20 | $this->assertEquals($expected, $actual); |
21 | } | |
b6708aeb | 22 | |
00be9182 | 23 | public function testUrlQueryArray() { |
6a488035 TO |
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', | |
6c6e6187 | 30 | ), FALSE, NULL, FALSE); |
6a488035 TO |
31 | $this->assertEquals($expected, $actual); |
32 | } | |
efceedd4 TO |
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 | } | |
96025800 | 39 | |
f9bdf062 | 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 | ||
6a488035 | 109 | } |