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