Merge in 5.16
[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() {
6a488035
TO
18 list ($ext, $suite) = $this->getRequestExtAndSuite();
19 if (empty($ext) || empty($suite)) {
20 throw new CRM_Core_Exception("FIXME: Not implemented: QUnit browser");
21 }
22
23 if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $suite) || strpos($suite, '..') !== FALSE) {
24 throw new CRM_Core_Exception("Malformed suite name");
25 }
26
27 $path = CRM_Extension_System::singleton()->getMapper()->keyToBasePath($ext);
28 if (!is_dir("$path/tests/qunit/$suite")) {
29 throw new CRM_Core_Exception("Failed to locate test suite");
30 }
31
32 // Load the test suite -- including any PHP, TPL, or JS content
33 if (file_exists("$path/tests/qunit/$suite/test.php")) {
34 // e.g. load resources
35 require_once "$path/tests/qunit/$suite/test.php";
36 }
37 if (file_exists("$path/tests/qunit/$suite/test.tpl")) {
38 // e.g. setup markup and/or load resources
39 CRM_Core_Smarty::singleton()->addTemplateDir("$path/tests");
40 $this->assign('qunitTpl', "qunit/$suite/test.tpl");
41 }
42 if (file_exists("$path/tests/qunit/$suite/test.js")) {
96ed17aa 43 CRM_Core_Resources::singleton()->addScriptFile($ext, "tests/qunit/$suite/test.js", 1000, 'html-header');
6a488035
TO
44 }
45
be2fb01f 46 CRM_Utils_System::setTitle(ts('QUnit: %2 (%1)', [1 => $ext, 2 => $suite]));
6a488035 47 CRM_Core_Resources::singleton()
887b931d
TO
48 ->addScriptFile('civicrm', 'bower_components/qunit/qunit/qunit.js', 1, 'html-header')
49 ->addStyleFile('civicrm', 'bower_components/qunit/qunit/qunit.css', 1, 'html-header');
6a488035
TO
50 parent::run();
51 }
52
53 /**
e8e8f3ad 54 * Extract the extension and suite from the request path.
6a488035
TO
55 *
56 * @return array
57 */
00be9182 58 public function getRequestExtAndSuite() {
6a488035
TO
59 $config = CRM_Core_Config::singleton();
60 $arg = explode('/', $_GET[$config->userFrameworkURLVar]);
61
62 if ($arg[1] == 'dev'
63 && CRM_Utils_Array::value(2, $arg) == 'qunit'
64 && isset($arg[3])
65 && isset($arg[4])
66 ) {
be2fb01f 67 return [
6a488035
TO
68 trim(CRM_Utils_Type::escape($arg[3], 'String'), '/'),
69 trim(CRM_Utils_Type::escape($arg[4], 'String'), '/'),
be2fb01f 70 ];
6a488035
TO
71 }
72 else {
be2fb01f 73 return [NULL, NULL];
6a488035
TO
74 }
75 }
96025800 76
6a488035 77}