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