diff --git a/ctools.info b/ctools.info index 0bced99..ee96822 100644 --- a/ctools.info +++ b/ctools.info @@ -15,3 +15,6 @@ files[] = tests/ctools.plugins.test files[] = tests/math_expression.test files[] = tests/math_expression_stack.test files[] = tests/object_cache.test +files[] = tests/object_cache_unit.test +files[] = tests/page_tokens.test +files[] = tests/uuid.test diff --git a/ctools.module b/ctools.module index 78ac26b..c7bd80a 100644 --- a/ctools.module +++ b/ctools.module @@ -314,23 +314,29 @@ function ctools_break_phrase($str) { * Set a token/value pair to be replaced later in the request, specifically in * ctools_page_token_processing(). * - * @param $token + * @param string $token * The token to be replaced later, during page rendering. This should - * ideally be a string inside of an HTML comment, so that if there is - * no replacement, the token will not render on the page. - * @param $type + * ideally be a string inside of an HTML comment, so that if there is + * no replacement, the token will not render on the page. + * If $token is NULL, the token set is not changed, but is still + * returned. + * @param string $type * The type of the token. Can be either 'variable', which will pull data - * directly from the page variables - * @param $argument - * If $type == 'variable' then argument should be the key to fetch from - * the $variables. If $type == 'callback' then it should either be the - * callback, or an array that will be sent to call_user_func_array(). + * directly from the page variables, or 'callback', which causes a function + * to be called to calculate the value. No other values are supported. + * @param string|array $argument + * For $type of: + * - 'variable': argument should be the key to fetch from the $variables. + * - 'callback': then it should either be the callback function name as a + * string, or an array that will be sent to call_user_func_array(). Argument + * arrays must not use array keys (i.e. $a[0] is the first and $a[1] the + * second element, etc.) * - * @return + * @return array * A array of token/variable names to be replaced. */ function ctools_set_page_token($token = NULL, $type = NULL, $argument = NULL) { - static $tokens = array(); + $tokens = &drupal_static('ctools_set_page_token', array()); if (isset($token)) { $tokens[$token] = array($type, $argument); @@ -339,13 +345,32 @@ function ctools_set_page_token($token = NULL, $type = NULL, $argument = NULL) { } /** - * Easily set a token from the page variables. + * Reset the defined page tokens within this request. + * + * Introduced for simpletest purposes. Normally not needed. + */ +function ctools_reset_page_tokens() { + drupal_static_reset('ctools_set_page_token'); +} + +/** + * Set a replacement token from the containing element's children during #post_render. * * This function can be used like this: - * $token = ctools_set_variable_token('tabs'); + * $token = ctools_set_variable_token('tabs'); * - * $token will then be a simple replacement for the 'tabs' about of the - * variables available in the page template. + * The token "" would then be replaced by the value of + * this element's (sibling) render array key 'tabs' during post-render (or be + * deleted if there was no such key by that point). + * + * @param string $token + * The token string for the page callback, e.g. 'title' + * + * @return string + * The constructed token. + * + * @see ctools_set_callback_token(). + * @see ctools_page_token_processing(). */ function ctools_set_variable_token($token) { $string = ''; @@ -354,10 +379,45 @@ function ctools_set_variable_token($token) { } /** - * Easily set a token from the page variables. + * Set a replacement token from the value of a function during #post_render. * * This function can be used like this: - * $token = ctools_set_variable_token('id', 'mymodule_myfunction'); + * $token = ctools_set_callback_token('id', 'mymodule_myfunction'); + * + * Or this (from its use in ctools_page_title_content_type_render): + * $token = ctools_set_callback_token('title', array( + * 'ctools_page_title_content_type_token', $conf['markup'], $conf['id'], $conf['class'] + * ) + * ); + * + * The token (e.g: "") + * would then be replaced during post-render by the return value of: + * + * ctools_page_title_content_type_token($value_markup, $value_id, $value_class); + * + * @param string $token + * The token string for the page callback, e.g. 'title' + * + * @param string|array $callback + * For callback functions that require no args, the name of the function as a + * string; otherwise an array of two or more elements: the function name + * followed by one or more function arguments. + * + * NB: the value of $callback must be a procedural (non-class) function that + * passes the php function_exists() check. + * + * The callback function itself will be called with args dependent + * on $callback. If: + * - $callback is a string, the function is called with a reference to the + * render array; + * - $callback is an array, the function is called with $callback merged + * with an array containing a reference to the render array. + * + * @return string + * The constructed token. + * + * @see ctools_set_variable_token(). + * @see ctools_page_token_processing(). */ function ctools_set_callback_token($token, $callback) { // If the callback uses arguments they are considered in the token. diff --git a/tests/context.test b/tests/context.test index 2dd4f4d..92f9cd1 100644 --- a/tests/context.test +++ b/tests/context.test @@ -1,6 +1,13 @@ 'Keywords substitution', @@ -9,12 +16,19 @@ public static function getInfo() { ); } - public function setUp() { - parent::setUp('ctools'); + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + parent::setUp($modules); ctools_include('context'); } + /** + * Test the keyword substitution. + */ public function testKeywordsSubstitution() { // Create node context for substitution. $node = $this->drupalCreateNode(); diff --git a/tests/css.test b/tests/css.test index 3ffd42a..aaef842 100644 --- a/tests/css.test +++ b/tests/css.test @@ -1,13 +1,13 @@ 'CSS Tools tests', @@ -16,50 +16,87 @@ public static function getInfo() { ); } - function setUp() { - // Additionally enable contact module. - parent::setUp('ctools'); + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + parent::setUp($modules); + ctools_include('css'); } /** - * Test that cached plugins are loaded correctly. + * Test that Stored CSS snippets can be retrieved, filtered or otherwise. */ - function testCssStuff() { + public function testCssStoreFilterRetrieve() { $css = "#some-id .some-class {\n color: black;\n illegal-key: foo;\n}"; $filtered_css = '#some-id .some-class{color:black;}'; - ctools_include('css'); + $this->assertNull(ctools_css_retrieve('missing-css-test'), 'Missing css snippet is not found'); + $filename1 = ctools_css_store('unfiltered-css-test', $css, FALSE); $filename2 = ctools_css_store('filtered-css-test', $css, TRUE); + $file_contents = file_get_contents($filename1); + $this->assertEqual($css, $file_contents, 'Unfiltered css file contents are correct'); + $this->assertEqual($filename1, ctools_css_retrieve('unfiltered-css-test'), 'Unfiltered css file successfully fetched'); $file_contents = file_get_contents($filename1); $this->assertEqual($css, $file_contents, 'Unfiltered css file contents are correct'); -// $match = $filename1 == ctools_css_retrieve('unfiltered-css-test') ? 'Match' : 'No match'; -// $output .= '
Unfiltered: ' . $filename1 . ' ' . $match . '
'; -// $output .= '
' . file_get_contents($filename1) . '
'; $this->assertEqual($filename2, ctools_css_retrieve('filtered-css-test'), 'Filtered css file succcesfully fetched'); $file_contents = file_get_contents($filename2); $this->assertEqual($filtered_css, $file_contents, 'Filtered css file contents are correct'); - // $match = $filename2 == ctools_css_retrieve('filtered-css-test') ? 'Match' : 'No match'; -// $output .= '
Filtered: ' . $filename2 . ' ' . $match . '
'; -// $output .= '
' . file_get_contents($filename2) . '
'; -// -// drupal_add_css($filename2, array('type' => 'file')); -// return array('#markup' => $output); + } + /** + * @todo Test db-row-exist but file-not-exist case in ctools_css_retrieve(). + */ - // Test that in case that url can be used, the value surives when a colon is in it. + /** + * Test that Stored CSS snippets can be correctly overwritten. + */ + public function testCssStoreOverwrite() { + $css1 = "#some-id .some-class {\n color: black;\n illegal-key: foo;\n}"; + + $css2 = "#other-id .other-class {\n color: blue;\n illegal-key: foo;\n}"; + $filtered_css2 = '#other-id .other-class{color:blue;}'; + + ctools_css_store('unfiltered-css-test', $css1, FALSE); + ctools_css_store('filtered-css-test', $css1, TRUE); + + // Now overwrite the first css with the second version. + + $filename3 = ctools_css_store('unfiltered-css-test', $css2, FALSE); + $filename4 = ctools_css_store('filtered-css-test', $css2, TRUE); + + $file_contents3 = file_get_contents($filename3); + $file_contents4 = file_get_contents($filename4); + + $this->assertEqual($css2, $file_contents3, 'Unfiltered CSS has overwritten earlier contents.'); + $this->assertEqual($filtered_css2, $file_contents4, 'Filtered CSS has overwritten earlier contents.'); + } + + /** + * Test that in case that url is used, the colons survives filtering. + */ + public function testCssFilterURLHandling() { $css = "#some-id {\n background-image: url(http://example.com/example.gif);\n}"; $css_data = ctools_css_disassemble($css); $empty_array = array(); $disallowed_values_regex = '/(expression)/'; - $filtered = ctools_css_assemble(ctools_css_filter_css_data($css_data, $empty_array, $empty_array, '', $disallowed_values_regex)); + + $intermediate = ctools_css_filter_css_data($css_data, $empty_array, $empty_array, '', $disallowed_values_regex); + $filtered = ctools_css_assemble($intermediate); + $url = (strpos($filtered, 'http://example.com/example.gif') !== FALSE); $this->assertTrue($url, 'CSS with multiple colons can survive.'); + } - // Test that in case the CSS has two properties defined are merged. + /** + * Test that when the CSS has two properties defined they are merged. + */ + public function testCssFilterMergeProperties() { $css = "#some-id {\n font-size: 12px;\n}\n#some-id {\n color: blue;\n}"; $filtered = ctools_css_filter($css); $font_size = (strpos($filtered, 'font-size:12px;') !== FALSE); @@ -78,4 +115,5 @@ function testCssStuff() { $color = (strpos($filtered, 'color:red') !== FALSE); $this->assertTrue($font_size && $color, 'Multiple properties are retained.'); } + } diff --git a/tests/css_cache.test b/tests/css_cache.test index 0b3528d..26ae68e 100644 --- a/tests/css_cache.test +++ b/tests/css_cache.test @@ -1,13 +1,9 @@ 'Get plugin info', @@ -16,11 +16,23 @@ public static function getInfo() { ); } - function setUp() { - // Additionally enable contact module. - parent::setUp('ctools', 'ctools_plugin_test'); + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + $modules[] = 'ctools_plugin_test'; + parent::setUp($modules); } + /** + * + * + * @param string $module + * @param string $type + * @param mixed $id + * @param string $function + */ protected function assertPluginFunction($module, $type, $id, $function = 'function') { $func = ctools_plugin_load_function($module, $type, $id, $function); $this->assertTrue(function_exists($func), t('Plugin @plugin of plugin type @module:@type successfully retrieved @retrieved for @function.', array( @@ -32,6 +44,14 @@ protected function assertPluginFunction($module, $type, $id, $function = 'functi ))); } + /** + * + * + * @param string $module + * @param string $type + * @param mixed $id + * @param string $function + */ protected function assertPluginMissingFunction($module, $type, $id, $function = 'function') { $func = ctools_plugin_load_function($module, $type, $id, $function); $this->assertEqual($func, NULL, t('Plugin @plugin of plugin type @module:@type for @function with missing function successfully failed.', array( @@ -53,6 +73,14 @@ protected function assertPluginClass($module, $type, $id, $class = 'handler') { ))); } + /** + * + * + * @param string $module + * @param string $type + * @param mixed $id + * @param string $function + */ protected function assertPluginMissingClass($module, $type, $id, $class = 'handler') { $class_name = ctools_plugin_load_class($module, $type, $id, $class); $this->assertEqual($class_name, NULL, t('Plugin @plugin of plugin type @module:@type for @class with missing class successfully failed.', array( @@ -81,7 +109,7 @@ function testPluginLoading() { $this->assertPluginClass($module, $type, 'plugin_array', 'handler'); $this->assertPluginClass($module, $type, 'plugin_array2', 'handler'); $this->assertPluginMissingClass($module, $type, 'plugin_array_dne', 'handler'); - // TODO Test big hook plugins. + // @todo Test big hook plugins. $type = 'cached'; @@ -95,6 +123,7 @@ function testPluginLoading() { $this->assertPluginClass($module, $type, 'plugin_array', 'handler'); $this->assertPluginClass($module, $type, 'plugin_array2', 'handler'); $this->assertPluginMissingClass($module, $type, 'plugin_array_dne', 'handler'); - // TODO Test big hook plugins. + // @todo Test big hook plugins. } + } diff --git a/tests/ctools_export_test/ctools_export.test b/tests/ctools_export_test/ctools_export.test index 6a062de..baec2cd 100644 --- a/tests/ctools_export_test/ctools_export.test +++ b/tests/ctools_export_test/ctools_export.test @@ -1,15 +1,13 @@ 'CTools export CRUD tests', @@ -18,9 +16,13 @@ public static function getInfo() { ); } - protected function setUp() { - parent::setUp('ctools_export_test'); - $this->resetAll(); + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + $modules[] = 'ctools_export_test'; + parent::setUp($modules); } /** @@ -135,7 +137,8 @@ function testCrudExportRevert() { // Check the exportable is now in_code_only. $this->assertTrue($default_export->in_code_only, 'The loaded exportable is in the database only.'); - // Make sure the default object loaded matches the same overridden one in the database. + // Make sure the default object loaded matches the same overridden one in + // the database. $this->assertEqual($original_export->machine, $default_export->machine, 'The default exportable has been loaded and matches the overridden exportable.'); } @@ -158,6 +161,7 @@ function testCrudExportDelete() { $machine = $database_export->machine; ctools_export_crud_delete('ctools_export_test', $database_export); + // Clear the exportable caches as it's been loaded above. ctools_export_load_object_reset('ctools_export_test'); $result = ctools_export_crud_load('ctools_export_test', $machine); diff --git a/tests/ctools_plugin_test.info b/tests/ctools_plugin_test.info index ca02e26..4798fe2 100644 --- a/tests/ctools_plugin_test.info +++ b/tests/ctools_plugin_test.info @@ -4,10 +4,4 @@ package = Chaos tool suite version = CTOOLS_MODULE_VERSION core = 7.x dependencies[] = ctools -files[] = ctools.plugins.test -files[] = object_cache.test -files[] = css.test -files[] = context.test -files[] = math_expression.test -files[] = math_expression_stack.test hidden = TRUE diff --git a/tests/ctools_plugin_test.module b/tests/ctools_plugin_test.module index 567f6e7..a1c587d 100644 --- a/tests/ctools_plugin_test.module +++ b/tests/ctools_plugin_test.module @@ -1,10 +1,12 @@ array( @@ -50,6 +55,11 @@ function ctools_plugin_test_ctools_plugin_type() { ); } +/** + * Plugin callback. + * + * @see ctools_plugin_test_ctools_plugin_type() + */ function ctools_plugin_test_ctools_plugin_test_big_hook_cached() { return array( 'test1' => array( @@ -59,6 +69,11 @@ function ctools_plugin_test_ctools_plugin_test_big_hook_cached() { ); } +/** + * Plugin callback. + * + * @see ctools_plugin_test_ctools_plugin_type() + */ function ctools_plugin_test_ctools_plugin_test_big_hook_not_cached() { return array( 'test1' => array( @@ -68,5 +83,16 @@ function ctools_plugin_test_ctools_plugin_test_big_hook_not_cached() { ); } +/** + * Callback for the big_hook_cached plugin. + * + * @see ctools_plugin_test_ctools_plugin_test_big_hook_cached + */ function ctools_plugin_test_hook_cached_test() {} + +/** + * Callback for the big_hook_not_cached plugin. + * + * @see ctools_plugin_test_ctools_plugin_test_big_hook_not_cached() + */ function ctools_plugin_test_hook_not_cached_test() {} diff --git a/tests/math_expression.test b/tests/math_expression.test index 8810e8c..143b409 100644 --- a/tests/math_expression.test +++ b/tests/math_expression.test @@ -1,14 +1,13 @@ 'CTools math expression tests', @@ -17,8 +16,13 @@ public static function getInfo() { ); } - public function setUp() { - parent::setUp('ctools', 'ctools_plugin_test'); + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + $modules[] = 'ctools_plugin_test'; + parent::setUp($modules); } /** @@ -35,6 +39,9 @@ protected function assertFloat($first, $second, $delta = 0.0000001, $message = ' return $this->assert(abs($first - $second) <= $delta, $message ? $message : t('Value @first is equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group); } + /** + * Test some arithmetic handling. + */ public function testArithmetic() { $math_expression = new ctools_math_expr(); @@ -66,10 +73,13 @@ public function testArithmetic() { $this->assertEqual(pow($random_number_a, $random_number_b), $math_expression->e("$random_number_a ^ $random_number_b")); } + /** + * Test + */ public function testBuildInFunctions() { $math_expression = new ctools_math_expr(); - // @todo: maybe run this code multiple times to test different values. + // @todo Maybe run this code multiple times to test different values. $random_double = $this->rand01(); $random_int = rand(5, 10); $this->assertFloat(sin($random_double), $math_expression->e("sin($random_double)")); @@ -94,6 +104,9 @@ public function testBuildInFunctions() { // $this->assertFloat(min($random_double_a, $random_double_b), $math_expression->e("min($random_double_a, $random_double_b)")); } + /** + * Test variable handling. + */ public function testVariables() { $math_expression = new ctools_math_expr(); @@ -108,6 +121,9 @@ public function testVariables() { $this->assertEqual($random_number_a / $random_number_b, $math_expression->e("var / $random_number_b")); } + /** + * Test custom function handling. + */ public function testCustomFunctions() { $math_expression = new ctools_math_expr(); @@ -126,4 +142,5 @@ public function testCustomFunctions() { // Use a custom function in another function. $this->assertEqual(($random_number_a * 2 + $random_number_b) * 2, $math_expression->e("f(g($random_number_a, $random_number_b))")); } + } diff --git a/tests/math_expression_stack.test b/tests/math_expression_stack.test index bdd4695..8a06d89 100644 --- a/tests/math_expression_stack.test +++ b/tests/math_expression_stack.test @@ -1,14 +1,13 @@ 'CTools math expression stack tests', @@ -17,10 +16,18 @@ public static function getInfo() { ); } - public function setUp() { - parent::setUp('ctools', 'ctools_plugin_test'); + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + $modules[] = 'ctools_plugin_test'; + parent::setUp($modules); } + /** + * Test the math expression stack system. + */ public function testStack() { $stack = new ctools_math_expr_stack(); @@ -35,13 +42,15 @@ public function testStack() { $this->assertIdentical($value, $stack->pop()); $this->assertNull($stack->pop()); - // Add multiple elements and see whether they are returned in the right order. + // Add multiple elements and see whether they are returned in the right + // order. $values = array($this->randomName(), $this->randomName(), $this->randomName()); foreach ($values as $value) { $stack->push($value); } - // Test the different elements at different positions with the last() method. + // Test the different elements at different positions with the last() + // method. $count = count($values); foreach ($values as $key => $value) { $this->assertEqual($value, $stack->last($count - $key)); @@ -58,6 +67,6 @@ public function testStack() { $this->assertEqual($stack->pop(), $value); } $this->assertNull($stack->pop()); - } + } diff --git a/tests/object_cache.test b/tests/object_cache.test index 2e714d6..a699e1d 100644 --- a/tests/object_cache.test +++ b/tests/object_cache.test @@ -1,13 +1,13 @@ 'Ctools object cache storage', @@ -16,12 +16,20 @@ public static function getInfo() { ); } - public function setUp() { - // Additionally enable ctools module. - parent::setUp('ctools'); + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + parent::setUp($modules); } + /** + * + */ public function testObjectStorage() { + ctools_include('cache'); + $account1 = $this->drupalCreateUser(array()); $this->drupalLogin($account1); diff --git a/tests/object_cache_unit.test b/tests/object_cache_unit.test new file mode 100644 index 0000000..1bb4af0 --- /dev/null +++ b/tests/object_cache_unit.test @@ -0,0 +1,138 @@ + 'Ctools Unit object cache storage', + 'description' => 'Verify that objects are written, readable and lockable.', + 'group' => 'ctools', + ); + } + + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + parent::setUp($modules); + } + + /** + * Check that the supplied array looks like a ctools cache plugin. + * + * @param mixed $p + * The value to check. + * @param string $msg + * Prefix of the assertion message. + */ + public function assertValidCachePlugin($p, $msg) { + //$this->pass(var_export($p, TRUE)); + $this->assertTrue(is_array($p), $msg . ': plugin is an array'); + + $this->assertEqual($p['plugin type'], 'cache', $msg . ': type is cache'); + + $this->assertTrue(array_key_exists('title', $p), $msg . ': title element exists'); + $this->assertTrue(!empty($p['title']) && is_string($p['title']), $msg . ': title is a string'); + + $this->assertTrue(array_key_exists('cache get', $p), $msg . ': has a get function'); + $this->assertTrue(!empty($p['cache get']) && is_callable($p['cache get']), $msg . ': get is executable'); + + $this->assertTrue(array_key_exists('cache set', $p), $msg . ': has a set function'); + $this->assertTrue(!empty($p['cache set']) && is_callable($p['cache set']), $msg . ': set is executable'); + + // @todo Clear is required by the spec (cache.inc:40..48): but export_ui cache doesn't implement it. + $this->assertTrue(array_key_exists('cache clear', $p), $msg . ': has a clear function'); + $this->assertTrue(is_callable($p['cache clear']), $msg . ': clear is executable'); + + // @todo Break is optional acc'd to spec but does anything implement it? + $this->assertTrue(!array_key_exists('cache break', $p) || is_callable($p['cache break']), $msg . ': break is executable'); + + // @todo Finalize is optional so don't fail if not there?? + $this->assertTrue(!array_key_exists('cache finalize', $p) || is_callable($p['cache finalize']), $msg . ': finalize is executable'); + } + + /** + * Check the return value of the ctools_cache_find_plugin function. + * + * @param mixed $p the value to check. + * @param string $msg prefix of the assertion message. + */ + public function assertPluginNotFound($p, $msg) { + //$this->pass(var_export($p, TRUE)); + $this->assertTrue(is_array($p), $msg . ': is an array'); + $plugin = array_shift($p); + $this->assertNull($plugin, $msg . ': no plugin info'); + $data = array_shift($p); + $this->assertTrue(empty($data) || is_string($data), $msg . ': data string-like'); + $this->assertTrue(empty($p), $msg . ': just two elements'); + } + + /** + * Check the return value of the ctools_cache_find_plugin function. + * + * @param mixed $p the value to check. + * @param string $msg prefix of the assertion message. + */ + public function assertPluginFound($p, $msg) { + //$this->pass(var_export($p, TRUE)); + $this->assertTrue(is_array($p), $msg . ': is an array'); + $plugin = array_shift($p); + $this->assertTrue(is_array($plugin), $msg . ': has plugin data'); + $data = array_shift($p); + $this->assertTrue(empty($data) || is_string($data), $msg . ': data is string-like'); + $this->assertTrue(empty($p), $msg . ': just two elements'); + } + + /** + * Test to see that we can find the standard simple plugin. + */ + public function testFindSimpleCachePlugin() { + ctools_include('cache'); + + // The simple plugin. + $plugin = ctools_cache_find_plugin('simple'); + $this->assertPluginFound($plugin, 'The Simple Cache plugin is present'); + $this->assertValidCachePlugin($plugin[0], 'The Simple Cache plugin'); + + // The simple plugin, with ::. + $plugin = ctools_cache_find_plugin('simple::data'); + $this->assertPluginFound($plugin, 'The Simple Cache plugin is present, with data'); + } + + /** + * Test to see that we can find the standard export_ui plugin. + */ + public function testFindExportUICachePlugin() { + ctools_include('cache'); + + // The export plugin. + $plugin = ctools_cache_find_plugin('export_ui'); + $this->assertPluginFound($plugin, 'The Export UI Cache plugin is present'); + $this->assertValidCachePlugin($plugin[0], 'The Export Cache plugin'); + + // The export plugin, with ::. + $plugin = ctools_cache_find_plugin('export_ui::data'); + $this->assertTrue(is_array($plugin), 'The Export UI Cache plugin is present, with data'); + } + + /** + * Test to see that we don't find plugins that aren't there. + */ + public function testFindFoobarbazCachePlugin() { + ctools_include('cache'); + + // An imaginary foobarbaz plugin. + $plugin = ctools_cache_find_plugin('foobarbaz'); + $this->assertPluginNotFound($plugin, 'The Foobarbaz Cache plugin is absent'); + $plugin = ctools_cache_find_plugin('foobarbaz::data'); + $this->assertPluginNotFound($plugin, 'The Foobarbaz Cache plugin is absent, with data'); + } + +} diff --git a/tests/page_tokens.test b/tests/page_tokens.test new file mode 100644 index 0000000..1a60df7 --- /dev/null +++ b/tests/page_tokens.test @@ -0,0 +1,137 @@ + 'Ctools Unit page token processing', + 'description' => 'Verify that page tokens can be set and read.', + 'group' => 'ctools', + ); + } + + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + parent::setUp($modules); + + // Start each test anew. + ctools_reset_page_tokens(); + } + + /** + * Test that we can set page tokens. + */ + public function testSetPageToken() { + ctools_set_page_token('id', 'variable', 'test'); + + $tokens = ctools_set_page_token(); + $this->assertTrue(is_array($tokens) && count($tokens) === 1, 'Page tokens no longer empty.'); + $this->assertEqual($tokens['id'], ['variable', 'test'], 'Page token has correct value.'); + } + + /** + * Test that we can set page tokens. + */ + public function testSetVariableToken() { + $string = ctools_set_variable_token('title'); + $tokens = ctools_set_page_token(); + + $this->assertTrue(is_array($tokens) && count($tokens) === 1, 'Page tokens no longer empty'); + $this->assertEqual($tokens[$string], ['variable', 'title'], 'Page tokens no longer empty'); + } + + /** + * Test that we can set page tokens. + */ + public function testReplaceVariableToken() { + $string = ctools_set_variable_token('title'); + $this->assertEqual($string, '', 'Expected form of token was found'); + + $elements = array( + '#type' => 'container', + '#attributes' => array('class' => array('test')), + 'title' => '

Title

', + 'content' => array( + '#type' => 'markup', + '#markup' => t('This is my test markup'), + '#prefix' => $string, + ), + 'link' => array( + '#type' => 'link', + '#title' => t('My link'), + '#href' => 'node/1', + ), + ); + $markup = '
This is my test markupMy link
'; + + $new_markup = ctools_page_token_processing($markup, $elements); + + $this->assertTrue(is_string($new_markup) && strlen($new_markup) > 1, 'Should return string'); + + $this->assertTrue(strstr($new_markup, '

'), 'Variable Token Markup should contain h2 element'); + $this->assertFalse(strstr($new_markup, 'ctools-page-title'), 'Variable Token Markup should not contain comment element'); + } + + /** + * Test that we can set page tokens. + */ + public function testReplaceCallbackToken() { + $string = ctools_set_callback_token('title', 'test_ctools_page_callback_token'); + $this->assertEqual($string, '', 'Expected form of token was found'); + + $elements = array( + '#type' => 'container', + '#attributes' => array('class' => array('test')), + 'content' => array( + '#type' => 'markup', + '#markup' => t('This is my test markup'), + '#prefix' => $string, + ), + 'link' => array( + '#type' => 'link', + '#title' => t('My link'), + '#href' => 'node/1', + ), + ); + $markup = '
This is my test markupMy link
'; + + $new_markup = ctools_page_token_processing($markup, $elements); + + $this->assertTrue(is_string($new_markup) && strlen($new_markup) > 1, 'Should return a non-empty string'); + + $this->assertTrue(strstr($new_markup, '

'), 'Callback Token Markup should contain h2 element'); + $this->assertFalse(strstr($new_markup, 'ctools-page-title'), 'Callback Token Markup should not contain comment element'); + } + +} + +/** + * + * @param $elements + * + * @return string + */ +function test_ctools_page_callback_token($elements) { + // Check that 'elements' array looks good. + if (isset($elements['content'])) { + return '

Title

'; + } + else { + return ''; + } +} diff --git a/tests/uuid.test b/tests/uuid.test new file mode 100644 index 0000000..2428580 --- /dev/null +++ b/tests/uuid.test @@ -0,0 +1,107 @@ + 'CTools unit tests for mini-UUID', + 'description' => 'Check that the CTools UUID functions behave correctly.', + 'group' => 'ctools', + ); + } + + /** + * {@inheritDoc} + */ + function setUp(array $modules = array()) { + $modules[] = 'ctools'; + parent::setUp($modules); + } + + /** + * Check the define UUID_PATTERN exists. + */ + public function testHaveUUIDpattern() { + ctools_include('uuid'); + $this->assertTrue(is_string(UUID_PATTERN) && strlen(UUID_PATTERN) > 12, 'UUID pattern exists'); + } + + /** + * Check we can get a UUID when . + * + * NB: This test will use one (and only one) of the mechanisms available + * from pear, + */ + public function testMakeNewUUIDctoolsphp() { + ctools_include('uuid'); + $uuid = _ctools_uuid_generate_php(); + $this->assertTrue(ctools_uuid_is_valid($uuid), 'UUID generated (ctoolsphp): ' . $uuid); + } + + /** + * Check we can get a UUID when . + * + * NB: This test will use one (and only one) of the mechanisms available + * from pear, + */ + public function testMakeNewUUIDnomodule() { + // drupal_get_filename will report even if the module is disabled. + if (drupal_get_filename('module', 'uuid') && module_exists('uuid')) { + module_disable(array('uuid')); + } + $uuid = ctools_uuid_generate(); + $this->assertTrue(ctools_uuid_is_valid($uuid), 'UUID generated (ctools): ' . $uuid); + } + + /** + * Check we can get a UUID. + * + * NB: This test will use one (and only one) of the mechanisms available + * from pear, + */ + public function testMakeNewUUIDmodule() { + // drupal_get_filename will report even if the module is disabled. + if (drupal_get_filename('module', 'uuid') && !module_exists('uuid')) { + module_enable(array('uuid')); + } + // if we now have uuid module, use it. + if (module_exists('uuid')) { + $uuid = ctools_uuid_generate(); + $this->assertTrue(ctools_uuid_is_valid($uuid), 'UUID generated (uuid): ' . $uuid); + } + else { + // otherwise say we can't... + $this->verbose('uuid module not present'); + } + } + + /** + * Check we can verify that a string looks like a UUID. + */ + public function testVerifyUUID() { + $checks = [ + NULL => FALSE, + '' => FALSE, + '0' => FALSE, + 'b5827a5cadd311e69a1f936389a27663' => FALSE, // version 1, no dashes + '15ff2566-add3-11e6-b98f' => FALSE, // incomplete + + '15ff2566-add3-11e6-b98f-080027dc4f7a' => TRUE, // version 1 + 'b5827a5c-add3-11e6-9a1f-936389a27663' => TRUE, // version 1 + '02d9e6d5-9467-382e-8f9b-9300a64ac3cd' => TRUE, // version 3 url-based + '5e330afb-50c6-45c2-a292-99c3168696d2' => TRUE, // version 4 uuid + '8f4ca4fd-154e-5063-b6db-aa91af137037' => TRUE, // version 5 + ]; + + foreach($checks as $uuid => $exp) { + $this->assertEqual(ctools_uuid_is_valid($uuid), $exp, 'Is Valid: UUIDs match expectations: ' . $uuid); + } + } + +}