Merge pull request #18745 from seamuslee001/backdrop_session
[civicrm-core.git] / tests / phpunit / E2E / Core / PathUrlTest.php
1 <?php
2
3 namespace E2E\Core;
4
5 use Civi\Core\Url;
6
7 /**
8 * Class PathUrlTest
9 * @package E2E\Core
10 * @group e2e
11 *
12 * Check that various paths and URLs are generated correctly.
13 */
14 class PathUrlTest extends \CiviEndToEndTestCase {
15
16 /**
17 * `CRM_Utils_System::url()` should generate working URLs.
18 */
19 public function testSystemRouter() {
20 $this->assertUrlContentRegex(';class="CRM_Mailing_Form_Subscribe";',
21 \CRM_Utils_System::url('civicrm/mailing/subscribe', 'reset=1', TRUE, NULL, FALSE, TRUE));
22 }
23
24 /**
25 * `Civi::paths()->getUrl()` should generate working URLs.
26 */
27 public function testPaths_getUrl() {
28 $p = \Civi::paths();
29
30 $this->assertUrlContentRegex(';MIT-LICENSE.txt;',
31 $p->getUrl('[civicrm.packages]/jquery/plugins/jquery.timeentry.js', 'absolute'));
32 $this->assertUrlContentRegex(';https://civicrm.org/licensing;',
33 $p->getUrl('[civicrm.root]/js/Common.js', 'absolute'));
34 $this->assertUrlContentRegex(';Copyright jQuery Foundation;',
35 $p->getUrl('[civicrm.bower]/jquery/dist/jquery.js', 'absolute'));
36 }
37
38 /**
39 * `Civi::paths()->getPath()` should generate working paths.
40 */
41 public function testPaths_getPath() {
42 $p = \Civi::paths();
43
44 $this->assertFileContentRegex(';MIT-LICENSE.txt;',
45 $p->getPath('[civicrm.packages]/jquery/plugins/jquery.timeentry.js'));
46 $this->assertFileContentRegex(';https://civicrm.org/licensing;',
47 $p->getPath('[civicrm.root]/js/Common.js'));
48 $this->assertFileContentRegex(';Copyright jQuery Foundation;',
49 $p->getPath('[civicrm.bower]/jquery/dist/jquery.js'));
50 }
51
52 /**
53 * `Civi::paths()->getVariable()` should generate working paths+URLs.
54 */
55 public function testPaths_getVariable() {
56 $pathAndUrl = ['cms.root', 'civicrm.root', 'civicrm.packages', 'civicrm.files'];
57 $pathOnly = ['civicrm.private', 'civicrm.log', 'civicrm.compile'];
58 $urlOnly = [];
59
60 foreach (array_merge($pathOnly, $pathAndUrl) as $var) {
61 $path = \Civi::paths()->getVariable($var, 'path');
62 $this->assertTrue(file_exists($path) && is_dir($path), "The path for $var should be a valid directory.");
63 }
64
65 foreach (array_merge($urlOnly, $pathAndUrl) as $var) {
66 $url = \Civi::paths()->getVariable($var, 'url');
67 $this->assertRegExp(';^https?:;', $url, "The URL for $var should resolve a URL.");
68 }
69 }
70
71 /**
72 * @param string $expectContentRegex
73 * @param string $url
74 */
75 private function assertUrlContentRegex($expectContentRegex, $url) {
76 $this->assertRegexp(';^https?:;', $url, "The URL ($url) should be absolute.");
77 $content = file_get_contents($url);
78 $this->assertRegexp($expectContentRegex, $content);
79 }
80
81 /**
82 * @param string $expectContentRegex
83 * @param string $file
84 */
85 private function assertFileContentRegex($expectContentRegex, $file) {
86 $this->assertFileExists($file);
87 $content = file_get_contents($file);
88 $this->assertRegexp($expectContentRegex, $content);
89 }
90
91 /**
92 * @link https://lab.civicrm.org/dev/core/issues/1637
93 */
94 public function testGetUrl_WpAdmin() {
95 $config = \CRM_Core_Config::singleton();
96 if ($config->userFramework !== 'WordPress') {
97 $this->markTestSkipped('This test only applies to WP sites.');
98 }
99
100 // NOTE: For backend admin forms (eg `civicrm/contribute`) on WP, it doesn't matter
101 // if cleanURL's are enabled. Those are always be dirty URLs.
102
103 // WORKAROUND: There's some issue where the URL gets a diff value in WP E2E env
104 // than in normal WP env. The `cv url` command seems to behave more
105 // representatively, though this technique is harder to inspect with xdebug.
106 $url = cv('url civicrm/contribute?reset=1');
107 // $url = \CRM_Utils_System::url('civicrm/contribute', 'reset=1', TRUE, NULL, FALSE);
108
109 $parts = parse_url($url);
110 parse_str($parts['query'], $queryParts);
111 $this->assertEquals('CiviCRM', $queryParts['page']);
112 $this->assertEquals('civicrm/contribute', $queryParts['q']);
113 $this->assertEquals('1', $queryParts['reset']);
114
115 // As an E2E test for wp-demo, this assertion is specifically valid for wp-demo.
116 $this->assertEquals('/wp-admin/admin.php', $parts['path']);
117 }
118
119 }