Merge pull request #22852 from civicrm/5.47
[civicrm-core.git] / CRM / Core / Page / QUnit.php
CommitLineData
6a488035
TO
1<?php
2
3require_once 'CRM/Core/Page.php';
4
5/**
6 * Accept requests for "civicrm/dev/qunit/$ext/$suite"; locate the qunit
7 * test-suite ($suite) in an extension ($ext) and render it.
8 */
9class CRM_Core_Page_QUnit extends CRM_Core_Page {
10 protected $tplFile = NULL;
11
e8e8f3ad 12 /**
13 * Run.
14 *
15 * @throws \CRM_Core_Exception
16 */
00be9182 17 public function run() {
e351d35e
TO
18 $qunitJsFile = Civi::paths()->getPath('[civicrm.bower]/qunit/qunit/qunit.js');
19 $qunitJsUrl = Civi::paths()->getUrl('[civicrm.bower]/qunit/qunit/qunit.js');
20 $qunitCssUrl = Civi::paths()->getUrl('[civicrm.bower]/qunit/qunit/qunit.css');
21 if (!file_exists($qunitJsFile)) {
22 throw new \CRM_Core_Exception("QUnit is not available. Please install it in [civicrm.bower]/qunit.");
23 }
24
6a488035
TO
25 list ($ext, $suite) = $this->getRequestExtAndSuite();
26 if (empty($ext) || empty($suite)) {
27 throw new CRM_Core_Exception("FIXME: Not implemented: QUnit browser");
28 }
29
30 if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $suite) || strpos($suite, '..') !== FALSE) {
31 throw new CRM_Core_Exception("Malformed suite name");
32 }
33
34 $path = CRM_Extension_System::singleton()->getMapper()->keyToBasePath($ext);
35 if (!is_dir("$path/tests/qunit/$suite")) {
36 throw new CRM_Core_Exception("Failed to locate test suite");
37 }
38
39 // Load the test suite -- including any PHP, TPL, or JS content
40 if (file_exists("$path/tests/qunit/$suite/test.php")) {
41 // e.g. load resources
42 require_once "$path/tests/qunit/$suite/test.php";
43 }
44 if (file_exists("$path/tests/qunit/$suite/test.tpl")) {
45 // e.g. setup markup and/or load resources
46 CRM_Core_Smarty::singleton()->addTemplateDir("$path/tests");
47 $this->assign('qunitTpl', "qunit/$suite/test.tpl");
48 }
49 if (file_exists("$path/tests/qunit/$suite/test.js")) {
96ed17aa 50 CRM_Core_Resources::singleton()->addScriptFile($ext, "tests/qunit/$suite/test.js", 1000, 'html-header');
6a488035
TO
51 }
52
be2fb01f 53 CRM_Utils_System::setTitle(ts('QUnit: %2 (%1)', [1 => $ext, 2 => $suite]));
6a488035 54 CRM_Core_Resources::singleton()
e351d35e
TO
55 ->addScriptUrl($qunitJsUrl, 1, 'html-header')
56 ->addStyleUrl($qunitCssUrl, 1, 'html-header');
6a488035
TO
57 parent::run();
58 }
59
60 /**
e8e8f3ad 61 * Extract the extension and suite from the request path.
6a488035
TO
62 *
63 * @return array
64 */
00be9182 65 public function getRequestExtAndSuite() {
13594d55 66 $arg = explode('/', CRM_Utils_System::currentPath());
6a488035
TO
67
68 if ($arg[1] == 'dev'
69 && CRM_Utils_Array::value(2, $arg) == 'qunit'
70 && isset($arg[3])
71 && isset($arg[4])
72 ) {
be2fb01f 73 return [
6a488035
TO
74 trim(CRM_Utils_Type::escape($arg[3], 'String'), '/'),
75 trim(CRM_Utils_Type::escape($arg[4], 'String'), '/'),
be2fb01f 76 ];
6a488035
TO
77 }
78 else {
be2fb01f 79 return [NULL, NULL];
6a488035
TO
80 }
81 }
96025800 82
6a488035 83}