3 require_once 'CRM/Core/Page.php';
6 * Accept requests for "civicrm/dev/qunit/$ext/$suite"; locate the qunit
7 * test-suite ($suite) in an extension ($ext) and render it.
9 class CRM_Core_Page_QUnit
extends CRM_Core_Page
{
10 protected $tplFile = NULL;
15 * @throws \CRM_Core_Exception
17 public function run() {
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.");
25 list ($ext, $suite) = $this->getRequestExtAndSuite();
26 if (empty($ext) ||
empty($suite)) {
27 throw new CRM_Core_Exception("FIXME: Not implemented: QUnit browser");
30 if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $suite) ||
strpos($suite, '..') !== FALSE) {
31 throw new CRM_Core_Exception("Malformed suite name");
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");
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";
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");
49 if (file_exists("$path/tests/qunit/$suite/test.js")) {
50 CRM_Core_Resources
::singleton()->addScriptFile($ext, "tests/qunit/$suite/test.js", 1000, 'html-header');
53 CRM_Utils_System
::setTitle(ts('QUnit: %2 (%1)', [1 => $ext, 2 => $suite]));
54 CRM_Core_Resources
::singleton()
55 ->addScriptUrl($qunitJsUrl, 1, 'html-header')
56 ->addStyleUrl($qunitCssUrl, 1, 'html-header');
61 * Extract the extension and suite from the request path.
65 public function getRequestExtAndSuite() {
66 $config = CRM_Core_Config
::singleton();
67 $arg = explode('/', $_GET[$config->userFrameworkURLVar
]);
70 && CRM_Utils_Array
::value(2, $arg) == 'qunit'
75 trim(CRM_Utils_Type
::escape($arg[3], 'String'), '/'),
76 trim(CRM_Utils_Type
::escape($arg[4], 'String'), '/'),