12 * Check that various paths and URLs are generated correctly.
14 class PathUrlTest
extends \CiviEndToEndTestCase
{
17 * `CRM_Utils_System::url()` should generate working URLs.
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));
25 * `Civi::paths()->getUrl()` should generate working URLs.
27 public function testPaths_getUrl() {
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'));
39 * `Civi::paths()->getPath()` should generate working paths.
41 public function testPaths_getPath() {
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'));
53 * `Civi::paths()->getVariable()` should generate working paths+URLs.
55 public function testPaths_getVariable() {
56 $pathAndUrl = ['cms.root', 'civicrm.root', 'civicrm.packages', 'civicrm.files'];
57 $pathOnly = ['civicrm.private', 'civicrm.log', 'civicrm.compile'];
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.");
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.");
72 * @param string $expectContentRegex
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);
82 * @param string $expectContentRegex
85 private function assertFileContentRegex($expectContentRegex, $file) {
86 $this->assertFileExists($file);
87 $content = file_get_contents($file);
88 $this->assertRegexp($expectContentRegex, $content);
92 * @link https://lab.civicrm.org/dev/core/issues/1637
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.');
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.
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);
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']);
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']);