'Alter hook functionality', 'description' => 'Tests alteration of arguments passed to drupal_alter().', 'group' => 'Common', ); } function setUp() { parent::setUp('common_test'); } function testDrupalAlter() { // This test depends on Bartik, so make sure that it is always the current // active theme. global $theme, $base_theme_info; $theme = 'bartik'; $base_theme_info = array(); $array = array('foo' => 'bar'); $entity = new stdClass(); $entity->foo = 'bar'; // Verify alteration of a single argument. $array_copy = $array; $array_expected = array('foo' => 'Drupal theme'); drupal_alter('drupal_alter', $array_copy); $this->assertEqual($array_copy, $array_expected, t('Single array was altered.')); $entity_copy = clone $entity; $entity_expected = clone $entity; $entity_expected->foo = 'Drupal theme'; drupal_alter('drupal_alter', $entity_copy); $this->assertEqual($entity_copy, $entity_expected, t('Single object was altered.')); // Verify alteration of multiple arguments. $array_copy = $array; $array_expected = array('foo' => 'Drupal theme'); $entity_copy = clone $entity; $entity_expected = clone $entity; $entity_expected->foo = 'Drupal theme'; $array2_copy = $array; $array2_expected = array('foo' => 'Drupal theme'); drupal_alter('drupal_alter', $array_copy, $entity_copy, $array2_copy); $this->assertEqual($array_copy, $array_expected, t('First argument to drupal_alter() was altered.')); $this->assertEqual($entity_copy, $entity_expected, t('Second argument to drupal_alter() was altered.')); $this->assertEqual($array2_copy, $array2_expected, t('Third argument to drupal_alter() was altered.')); // Verify alteration order when passing an array of types to drupal_alter(). // common_test_module_implements_alter() places 'block' implementation after // other modules. $array_copy = $array; $array_expected = array('foo' => 'Drupal block theme'); drupal_alter(array('drupal_alter', 'drupal_alter_foo'), $array_copy); $this->assertEqual($array_copy, $array_expected, t('hook_TYPE_alter() implementations ran in correct order.')); } } /** * Tests for URL generation functions. * * url() calls module_implements(), which may issue a db query, which requires * inheriting from a web test case rather than a unit test case. */ class CommonURLUnitTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'URL generation tests', 'description' => 'Confirm that url(), drupal_get_query_parameters(), drupal_http_build_query(), and l() work correctly with various input.', 'group' => 'Common', ); } /** * Confirm that invalid text given as $path is filtered. */ function testLXSS() { $text = $this->randomName(); $path = ""; $link = l($text, $path); $sanitized_path = check_url(url($path)); $this->assertTrue(strpos($link, $sanitized_path) !== FALSE, t('XSS attack @path was filtered', array('@path' => $path))); } /* * Tests for active class in l() function. */ function testLActiveClass() { $link = l($this->randomName(), $_GET['q']); $this->assertTrue($this->hasClass($link, 'active'), t('Class @class is present on link to the current page', array('@class' => 'active'))); } /** * Tests for custom class in l() function. */ function testLCustomClass() { $class = $this->randomName(); $link = l($this->randomName(), $_GET['q'], array('attributes' => array('class' => array($class)))); $this->assertTrue($this->hasClass($link, $class), t('Custom class @class is present on link when requested', array('@class' => $class))); $this->assertTrue($this->hasClass($link, 'active'), t('Class @class is present on link to the current page', array('@class' => 'active'))); } private function hasClass($link, $class) { return preg_match('|class="([^\"\s]+\s+)*' . $class . '|', $link); } /** * Test drupal_get_query_parameters(). */ function testDrupalGetQueryParameters() { $original = array( 'a' => 1, 'b' => array( 'd' => 4, 'e' => array( 'f' => 5, ), ), 'c' => 3, 'q' => 'foo/bar', ); // Default arguments. $result = $_GET; unset($result['q']); $this->assertEqual(drupal_get_query_parameters(), $result, t("\$_GET['q'] was removed.")); // Default exclusion. $result = $original; unset($result['q']); $this->assertEqual(drupal_get_query_parameters($original), $result, t("'q' was removed.")); // First-level exclusion. $result = $original; unset($result['b']); $this->assertEqual(drupal_get_query_parameters($original, array('b')), $result, t("'b' was removed.")); // Second-level exclusion. $result = $original; unset($result['b']['d']); $this->assertEqual(drupal_get_query_parameters($original, array('b[d]')), $result, t("'b[d]' was removed.")); // Third-level exclusion. $result = $original; unset($result['b']['e']['f']); $this->assertEqual(drupal_get_query_parameters($original, array('b[e][f]')), $result, t("'b[e][f]' was removed.")); // Multiple exclusions. $result = $original; unset($result['a'], $result['b']['e'], $result['c']); $this->assertEqual(drupal_get_query_parameters($original, array('a', 'b[e]', 'c')), $result, t("'a', 'b[e]', 'c' were removed.")); } /** * Test drupal_http_build_query(). */ function testDrupalHttpBuildQuery() { $this->assertEqual(drupal_http_build_query(array('a' => ' &#//+%20@۞')), 'a=%20%26%23//%2B%2520%40%DB%9E', t('Value was properly encoded.')); $this->assertEqual(drupal_http_build_query(array(' &#//+%20@۞' => 'a')), '%20%26%23%2F%2F%2B%2520%40%DB%9E=a', t('Key was properly encoded.')); $this->assertEqual(drupal_http_build_query(array('a' => '1', 'b' => '2', 'c' => '3')), 'a=1&b=2&c=3', t('Multiple values were properly concatenated.')); $this->assertEqual(drupal_http_build_query(array('a' => array('b' => '2', 'c' => '3'), 'd' => 'foo')), 'a[b]=2&a[c]=3&d=foo', t('Nested array was properly encoded.')); } /** * Test drupal_parse_url(). */ function testDrupalParseUrl() { // Relative URL. $url = 'foo/bar?foo=bar&bar=baz&baz#foo'; $result = array( 'path' => 'foo/bar', 'query' => array('foo' => 'bar', 'bar' => 'baz', 'baz' => ''), 'fragment' => 'foo', ); $this->assertEqual(drupal_parse_url($url), $result, t('Relative URL parsed correctly.')); // Relative URL that is known to confuse parse_url(). $url = 'foo/bar:1'; $result = array( 'path' => 'foo/bar:1', 'query' => array(), 'fragment' => '', ); $this->assertEqual(drupal_parse_url($url), $result, t('Relative URL parsed correctly.')); // Absolute URL. $url = '/foo/bar?foo=bar&bar=baz&baz#foo'; $result = array( 'path' => '/foo/bar', 'query' => array('foo' => 'bar', 'bar' => 'baz', 'baz' => ''), 'fragment' => 'foo', ); $this->assertEqual(drupal_parse_url($url), $result, t('Absolute URL parsed correctly.')); // External URL testing. $url = 'http://drupal.org/foo/bar?foo=bar&bar=baz&baz#foo'; // Test that drupal can recognize an absolute URL. Used to prevent attack vectors. $this->assertTrue(url_is_external($url), t('Correctly identified an external URL.')); // Test the parsing of absolute URLs. $result = array( 'path' => 'http://drupal.org/foo/bar', 'query' => array('foo' => 'bar', 'bar' => 'baz', 'baz' => ''), 'fragment' => 'foo', ); $this->assertEqual(drupal_parse_url($url), $result, t('External URL parsed correctly.')); // Verify proper parsing of URLs when clean URLs are disabled. $result = array( 'path' => 'foo/bar', 'query' => array('bar' => 'baz'), 'fragment' => 'foo', ); // Non-clean URLs #1: Absolute URL generated by url(). $url = $GLOBALS['base_url'] . '/?q=foo/bar&bar=baz#foo'; $this->assertEqual(drupal_parse_url($url), $result, t('Absolute URL with clean URLs disabled parsed correctly.')); // Non-clean URLs #2: Relative URL generated by url(). $url = '?q=foo/bar&bar=baz#foo'; $this->assertEqual(drupal_parse_url($url), $result, t('Relative URL with clean URLs disabled parsed correctly.')); // Non-clean URLs #3: URL generated by url() on non-Apache webserver. $url = 'index.php?q=foo/bar&bar=baz#foo'; $this->assertEqual(drupal_parse_url($url), $result, t('Relative URL on non-Apache webserver with clean URLs disabled parsed correctly.')); // Test that drupal_parse_url() does not allow spoofing a URL to force a malicious redirect. $parts = drupal_parse_url('forged:http://cwe.mitre.org/data/definitions/601.html'); $this->assertFalse(valid_url($parts['path'], TRUE), t('drupal_parse_url() correctly parsed a forged URL.')); } /** * Test url() with/without query, with/without fragment, absolute on/off and * assert all that works when clean URLs are on and off. */ function testUrl() { global $base_url; foreach (array(FALSE, TRUE) as $absolute) { // Get the expected start of the path string. $base = $absolute ? $base_url . '/' : base_path(); $absolute_string = $absolute ? 'absolute' : NULL; // Disable Clean URLs. $GLOBALS['conf']['clean_url'] = 0; $url = $base . '?q=node/123'; $result = url('node/123', array('absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . '?q=node/123#foo'; $result = url('node/123', array('fragment' => 'foo', 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . '?q=node/123&foo'; $result = url('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . '?q=node/123&foo=bar&bar=baz'; $result = url('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . '?q=node/123&foo#bar'; $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . '?q=node/123&foo#0'; $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => '0', 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . '?q=node/123&foo'; $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => '', 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base; $result = url('', array('absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); // Enable Clean URLs. $GLOBALS['conf']['clean_url'] = 1; $url = $base . 'node/123'; $result = url('node/123', array('absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . 'node/123#foo'; $result = url('node/123', array('fragment' => 'foo', 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . 'node/123?foo'; $result = url('node/123', array('query' => array('foo' => NULL), 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . 'node/123?foo=bar&bar=baz'; $result = url('node/123', array('query' => array('foo' => 'bar', 'bar' => 'baz'), 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base . 'node/123?foo#bar'; $result = url('node/123', array('query' => array('foo' => NULL), 'fragment' => 'bar', 'absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); $url = $base; $result = url('', array('absolute' => $absolute)); $this->assertEqual($url, $result, "$url == $result"); } } /** * Test external URL handling. */ function testExternalUrls() { $test_url = 'http://drupal.org/'; // Verify external URL can contain a fragment. $url = $test_url . '#drupal'; $result = url($url); $this->assertEqual($url, $result, t('External URL with fragment works without a fragment in $options.')); // Verify fragment can be overidden in an external URL. $url = $test_url . '#drupal'; $fragment = $this->randomName(10); $result = url($url, array('fragment' => $fragment)); $this->assertEqual($test_url . '#' . $fragment, $result, t('External URL fragment is overidden with a custom fragment in $options.')); // Verify external URL can contain a query string. $url = $test_url . '?drupal=awesome'; $result = url($url); $this->assertEqual($url, $result, t('External URL with query string works without a query string in $options.')); // Verify external URL can be extended with a query string. $url = $test_url; $query = array($this->randomName(5) => $this->randomName(5)); $result = url($url, array('query' => $query)); $this->assertEqual($url . '?' . http_build_query($query, '', '&'), $result, t('External URL can be extended with a query string in $options.')); // Verify query string can be extended in an external URL. $url = $test_url . '?drupal=awesome'; $query = array($this->randomName(5) => $this->randomName(5)); $result = url($url, array('query' => $query)); $this->assertEqual($url . '&' . http_build_query($query, '', '&'), $result, t('External URL query string can be extended with a custom query string in $options.')); } } /** * Tests for check_plain(), filter_xss(), format_string(), and check_url(). */ class CommonXssUnitTestCase extends DrupalUnitTestCase { public static function getInfo() { return array( 'name' => 'String filtering tests', 'description' => 'Confirm that check_plain(), filter_xss(), format_string() and check_url() work correctly, including invalid multi-byte sequences.', 'group' => 'Common', ); } /** * Check that invalid multi-byte sequences are rejected. */ function testInvalidMultiByte() { // Ignore PHP 5.3+ invalid multibyte sequence warning. $text = @check_plain("Foo\xC0barbaz"); $this->assertEqual($text, '', 'check_plain() rejects invalid sequence "Foo\xC0barbaz"'); // Ignore PHP 5.3+ invalid multibyte sequence warning. $text = @check_plain("\xc2\""); $this->assertEqual($text, '', 'check_plain() rejects invalid sequence "\xc2\""'); $text = check_plain("Fooÿñ"); $this->assertEqual($text, "Fooÿñ", 'check_plain() accepts valid sequence "Fooÿñ"'); $text = filter_xss("Foo\xC0barbaz"); $this->assertEqual($text, '', 'filter_xss() rejects invalid sequence "Foo\xC0barbaz"'); $text = filter_xss("Fooÿñ"); $this->assertEqual($text, "Fooÿñ", 'filter_xss() accepts valid sequence Fooÿñ'); } /** * Check that special characters are escaped. */ function testEscaping() { $text = check_plain("' . "\n"; $this->assertTrue(strpos($javascript, $expected_1) > 0, t('Rendered JavaScript within downlevel-hidden conditional comments.')); $this->assertTrue(strpos($javascript, $expected_2) > 0, t('Rendered JavaScript within downlevel-revealed conditional comments.')); } /** * Test JavaScript versioning. */ function testVersionQueryString() { drupal_add_js('core/misc/collapse.js', array('version' => 'foo')); drupal_add_js('core/misc/ajax.js', array('version' => 'bar')); $javascript = drupal_get_js(); $this->assertTrue(strpos($javascript, 'core/misc/collapse.js?v=foo') > 0 && strpos($javascript, 'core/misc/ajax.js?v=bar') > 0 , t('JavaScript version identifiers correctly appended to URLs')); } /** * Test JavaScript grouping and aggregation. */ function testAggregation() { $default_query_string = variable_get('css_js_query_string', '0'); // To optimize aggregation, items with the 'every_page' option are ordered // ahead of ones without. The order of JavaScript execution must be the // same regardless of whether aggregation is enabled, so ensure this // expected order, first with aggregation off. drupal_add_js('core/misc/ajax.js'); drupal_add_js('core/misc/authorize.js', array('every_page' => TRUE)); drupal_add_js('core/misc/autocomplete.js'); drupal_add_js('core/misc/batch.js', array('every_page' => TRUE)); $javascript = drupal_get_js(); $expected = implode("\n", array( '', '', '', '', )); $this->assertTrue(strpos($javascript, $expected) > 0, t('Unaggregated JavaScript is added in the expected group order.')); // Now ensure that with aggregation on, one file is made for the // 'every_page' files, and one file is made for the others. drupal_static_reset('drupal_add_js'); $config = config('system.performance'); $config->set('preprocess_js', 1); $config->save(); drupal_add_js('core/misc/ajax.js'); drupal_add_js('core/misc/authorize.js', array('every_page' => TRUE)); drupal_add_js('core/misc/autocomplete.js'); drupal_add_js('core/misc/batch.js', array('every_page' => TRUE)); $js_items = drupal_add_js(); $javascript = drupal_get_js(); $expected = implode("\n", array( '', '', )); $this->assertTrue(strpos($javascript, $expected) > 0, t('JavaScript is aggregated in the expected groups and order.')); } /** * Test JavaScript ordering. */ function testRenderOrder() { // Add a bunch of JavaScript in strange ordering. drupal_add_js('(function($){alert("Weight 5 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)); drupal_add_js('(function($){alert("Weight 0 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer')); drupal_add_js('(function($){alert("Weight 0 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer')); drupal_add_js('(function($){alert("Weight -8 #1");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); drupal_add_js('(function($){alert("Weight -8 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); drupal_add_js('(function($){alert("Weight -8 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); drupal_add_js('http://example.com/example.js?Weight -5 #1', array('type' => 'external', 'scope' => 'footer', 'weight' => -5)); drupal_add_js('(function($){alert("Weight -8 #4");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => -8)); drupal_add_js('(function($){alert("Weight 5 #2");})(jQuery);', array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)); drupal_add_js('(function($){alert("Weight 0 #3");})(jQuery);', array('type' => 'inline', 'scope' => 'footer')); // Construct the expected result from the regex. $expected = array( "-8 #1", "-8 #2", "-8 #3", "-8 #4", "-5 #1", // The external script. "0 #1", "0 #2", "0 #3", "5 #1", "5 #2", ); // Retrieve the rendered JavaScript and test against the regex. $js = drupal_get_js('footer'); $matches = array(); if (preg_match_all('/Weight\s([-0-9]+\s[#0-9]+)/', $js, $matches)) { $result = $matches[1]; } else { $result = array(); } $this->assertIdentical($result, $expected, t('JavaScript is added in the expected weight order.')); } /** * Test rendering the JavaScript with a file's weight above jQuery's. */ function testRenderDifferentWeight() { // JavaScript files are sorted first by group, then by the 'every_page' // flag, then by weight (see drupal_sort_css_js()), so to test the effect of // weight, we need the other two options to be the same. drupal_add_js('core/misc/collapse.js', array('group' => JS_LIBRARY, 'every_page' => TRUE, 'weight' => -21)); $javascript = drupal_get_js(); $this->assertTrue(strpos($javascript, 'core/misc/collapse.js') < strpos($javascript, 'core/misc/jquery.js'), t('Rendering a JavaScript file above jQuery.')); } /** * Test altering a JavaScript's weight via hook_js_alter(). * * @see simpletest_js_alter() */ function testAlter() { // Add both tableselect.js and simpletest.js, with a larger weight on SimpleTest. drupal_add_js('core/misc/tableselect.js'); drupal_add_js(drupal_get_path('module', 'simpletest') . '/simpletest.js', array('weight' => 9999)); // Render the JavaScript, testing if simpletest.js was altered to be before // tableselect.js. See simpletest_js_alter() to see where this alteration // takes place. $javascript = drupal_get_js(); $this->assertTrue(strpos($javascript, 'simpletest.js') < strpos($javascript, 'core/misc/tableselect.js'), t('Altering JavaScript weight through the alter hook.')); } /** * Adds a library to the page and tests for both its JavaScript and its CSS. */ function testLibraryRender() { $result = drupal_add_library('system', 'farbtastic'); $this->assertTrue($result !== FALSE, t('Library was added without errors.')); $scripts = drupal_get_js(); $styles = drupal_get_css(); $this->assertTrue(strpos($scripts, 'core/misc/farbtastic/farbtastic.js'), t('JavaScript of library was added to the page.')); $this->assertTrue(strpos($styles, 'core/misc/farbtastic/farbtastic.css'), t('Stylesheet of library was added to the page.')); } /** * Adds a JavaScript library to the page and alters it. * * @see common_test_library_info_alter() */ function testLibraryAlter() { // Verify that common_test altered the title of Farbtastic. $library = drupal_get_library('system', 'farbtastic'); $this->assertEqual($library['title'], 'Farbtastic: Altered Library', t('Registered libraries were altered.')); // common_test_library_info_alter() also added a dependency on jQuery Form. drupal_add_library('system', 'farbtastic'); $scripts = drupal_get_js(); $this->assertTrue(strpos($scripts, 'core/misc/jquery.form.js'), t('Altered library dependencies are added to the page.')); } /** * Tests that multiple modules can implement the same library. * * @see common_test_library_info() */ function testLibraryNameConflicts() { $farbtastic = drupal_get_library('common_test', 'farbtastic'); $this->assertEqual($farbtastic['title'], 'Custom Farbtastic Library', t('Alternative libraries can be added to the page.')); } /** * Tests non-existing libraries. */ function testLibraryUnknown() { $result = drupal_get_library('unknown', 'unknown'); $this->assertFalse($result, t('Unknown library returned FALSE.')); drupal_static_reset('drupal_get_library'); $result = drupal_add_library('unknown', 'unknown'); $this->assertFalse($result, t('Unknown library returned FALSE.')); $scripts = drupal_get_js(); $this->assertTrue(strpos($scripts, 'unknown') === FALSE, t('Unknown library was not added to the page.')); } /** * Tests the addition of libraries through the #attached['library'] property. */ function testAttachedLibrary() { $element['#attached']['library'][] = array('system', 'farbtastic'); drupal_render($element); $scripts = drupal_get_js(); $this->assertTrue(strpos($scripts, 'core/misc/farbtastic/farbtastic.js'), t('The attached_library property adds the additional libraries.')); } /** * Tests retrieval of libraries via drupal_get_library(). */ function testGetLibrary() { // Retrieve all libraries registered by a module. $libraries = drupal_get_library('common_test'); $this->assertTrue(isset($libraries['farbtastic']), t('Retrieved all module libraries.')); // Retrieve all libraries for a module not implementing hook_library_info(). // Note: This test installs Locale module. $libraries = drupal_get_library('locale'); $this->assertEqual($libraries, array(), t('Retrieving libraries from a module not implementing hook_library_info() returns an emtpy array.')); // Retrieve a specific library by module and name. $farbtastic = drupal_get_library('common_test', 'farbtastic'); $this->assertEqual($farbtastic['version'], '5.3', t('Retrieved a single library.')); // Retrieve a non-existing library by module and name. $farbtastic = drupal_get_library('common_test', 'foo'); $this->assertIdentical($farbtastic, FALSE, t('Retrieving a non-existing library returns FALSE.')); } /** * Tests that the query string remains intact when adding JavaScript files * that have query string parameters. */ function testAddJsFileWithQueryString() { $this->drupalGet('common-test/query-string'); $query_string = variable_get('css_js_query_string', '0'); $this->assertRaw(drupal_get_path('module', 'node') . '/node.js?' . $query_string, t('Query string was appended correctly to js.')); } } /** * Tests for drupal_render(). */ class CommonDrupalRenderTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'drupal_render()', 'description' => 'Performs functional tests on drupal_render().', 'group' => 'Common', ); } function setUp() { parent::setUp('common_test'); } /** * Test sorting by weight. */ function testDrupalRenderSorting() { $first = $this->randomName(); $second = $this->randomName(); // Build an array with '#weight' set for each element. $elements = array( 'second' => array( '#weight' => 10, '#markup' => $second, ), 'first' => array( '#weight' => 0, '#markup' => $first, ), ); $output = drupal_render($elements); // The lowest weight element should appear last in $output. $this->assertTrue(strpos($output, $second) > strpos($output, $first), t('Elements were sorted correctly by weight.')); // Confirm that the $elements array has '#sorted' set to TRUE. $this->assertTrue($elements['#sorted'], t("'#sorted' => TRUE was added to the array")); // Pass $elements through element_children() and ensure it remains // sorted in the correct order. drupal_render() will return an empty string // if used on the same array in the same request. $children = element_children($elements); $this->assertTrue(array_shift($children) == 'first', t('Child found in the correct order.')); $this->assertTrue(array_shift($children) == 'second', t('Child found in the correct order.')); // The same array structure again, but with #sorted set to TRUE. $elements = array( 'second' => array( '#weight' => 10, '#markup' => $second, ), 'first' => array( '#weight' => 0, '#markup' => $first, ), '#sorted' => TRUE, ); $output = drupal_render($elements); // The elements should appear in output in the same order as the array. $this->assertTrue(strpos($output, $second) < strpos($output, $first), t('Elements were not sorted.')); } /** * Test #attached functionality in children elements. */ function testDrupalRenderChildrenAttached() { // The cache system is turned off for POST requests. $request_method = $_SERVER['REQUEST_METHOD']; $_SERVER['REQUEST_METHOD'] = 'GET'; // Create an element with a child and subchild. Each element loads a // different JavaScript file using #attached. $parent_js = drupal_get_path('module', 'user') . '/user.js'; $child_js = drupal_get_path('module', 'forum') . '/forum.js'; $subchild_js = drupal_get_path('module', 'book') . '/book.js'; $element = array( '#type' => 'fieldset', '#cache' => array( 'keys' => array('simpletest', 'drupal_render', 'children_attached'), ), '#attached' => array('js' => array($parent_js)), '#title' => 'Parent', ); $element['child'] = array( '#type' => 'fieldset', '#attached' => array('js' => array($child_js)), '#title' => 'Child', ); $element['child']['subchild'] = array( '#attached' => array('js' => array($subchild_js)), '#markup' => 'Subchild', ); // Render the element and verify the presence of #attached JavaScript. drupal_render($element); $scripts = drupal_get_js(); $this->assertTrue(strpos($scripts, $parent_js), t('The element #attached JavaScript was included.')); $this->assertTrue(strpos($scripts, $child_js), t('The child #attached JavaScript was included.')); $this->assertTrue(strpos($scripts, $subchild_js), t('The subchild #attached JavaScript was included.')); // Load the element from cache and verify the presence of the #attached // JavaScript. drupal_static_reset('drupal_add_js'); $this->assertTrue(drupal_render_cache_get($element), t('The element was retrieved from cache.')); $scripts = drupal_get_js(); $this->assertTrue(strpos($scripts, $parent_js), t('The element #attached JavaScript was included when loading from cache.')); $this->assertTrue(strpos($scripts, $child_js), t('The child #attached JavaScript was included when loading from cache.')); $this->assertTrue(strpos($scripts, $subchild_js), t('The subchild #attached JavaScript was included when loading from cache.')); $_SERVER['REQUEST_METHOD'] = $request_method; } /** * Test passing arguments to the theme function. */ function testDrupalRenderThemeArguments() { $element = array( '#theme' => 'common_test_foo', ); // Test that defaults work. $this->assertEqual(drupal_render($element), 'foobar', 'Defaults work'); $element = array( '#theme' => 'common_test_foo', '#foo' => $this->randomName(), '#bar' => $this->randomName(), ); // Test that passing arguments to the theme function works. $this->assertEqual(drupal_render($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works'); } /** * Test rendering form elements without passing through form_builder(). */ function testDrupalRenderFormElements() { // Define a series of form elements. $element = array( '#type' => 'button', '#value' => $this->randomName(), ); $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'submit')); $element = array( '#type' => 'textfield', '#title' => $this->randomName(), '#value' => $this->randomName(), ); $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'text')); $element = array( '#type' => 'password', '#title' => $this->randomName(), ); $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'password')); $element = array( '#type' => 'textarea', '#title' => $this->randomName(), '#value' => $this->randomName(), ); $this->assertRenderedElement($element, '//textarea'); $element = array( '#type' => 'radio', '#title' => $this->randomName(), '#value' => FALSE, ); $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'radio')); $element = array( '#type' => 'checkbox', '#title' => $this->randomName(), ); $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'checkbox')); $element = array( '#type' => 'select', '#title' => $this->randomName(), '#options' => array( 0 => $this->randomName(), 1 => $this->randomName(), ), ); $this->assertRenderedElement($element, '//select'); $element = array( '#type' => 'file', '#title' => $this->randomName(), ); $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'file')); $element = array( '#type' => 'item', '#title' => $this->randomName(), '#markup' => $this->randomName(), ); $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', array( ':class' => 'form-type-item', ':markup' => $element['#markup'], ':label' => $element['#title'], )); $element = array( '#type' => 'hidden', '#title' => $this->randomName(), '#value' => $this->randomName(), ); $this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'hidden')); $element = array( '#type' => 'link', '#title' => $this->randomName(), '#href' => $this->randomName(), '#options' => array( 'absolute' => TRUE, ), ); $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', array( ':href' => url($element['#href'], array('absolute' => TRUE)), ':title' => $element['#title'], )); $element = array( '#type' => 'fieldset', '#title' => $this->randomName(), ); $this->assertRenderedElement($element, '//fieldset/legend[contains(., :title)]', array( ':title' => $element['#title'], )); $element['item'] = array( '#type' => 'item', '#title' => $this->randomName(), '#markup' => $this->randomName(), ); $this->assertRenderedElement($element, '//fieldset/div/div[contains(@class, :class) and contains(., :markup)]', array( ':class' => 'form-type-item', ':markup' => $element['item']['#markup'], )); } /** * Test rendering elements with invalid keys. */ function testDrupalRenderInvalidKeys() { $error = array( '%type' => 'User error', '!message' => '"child" is an invalid render array key', '%function' => 'element_children()', ); $message = t('%type: !message in %function (line ', $error); variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL); $this->drupalGet('common-test/drupal-render-invalid-keys'); $this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertRaw($message, t('Found error message: !message.', array('!message' => $message))); } protected function assertRenderedElement(array $element, $xpath, array $xpath_args = array()) { $original_element = $element; $this->drupalSetContent(drupal_render($element)); $this->verbose('
' .  check_plain(var_export($original_element, TRUE)) . '
' . '
' .  check_plain(var_export($element, TRUE)) . '
' . '
' . $this->drupalGetContent() ); // @see DrupalWebTestCase::xpath() $xpath = $this->buildXPathQuery($xpath, $xpath_args); $element += array('#value' => NULL); $this->assertFieldByXPath($xpath, $element['#value'], t('#type @type was properly rendered.', array( '@type' => var_export($element['#type'], TRUE), ))); } /** * Tests caching of an empty render item. */ function testDrupalRenderCache() { // Force a request via GET. $request_method = $_SERVER['REQUEST_METHOD']; $_SERVER['REQUEST_METHOD'] = 'GET'; // Create an empty element. $test_element = array( '#cache' => array( 'cid' => 'render_cache_test', ), '#markup' => '', ); // Render the element and confirm that it goes through the rendering // process (which will set $element['#printed']). $element = $test_element; drupal_render($element); $this->assertTrue(isset($element['#printed']), t('No cache hit')); // Render the element again and confirm that it is retrieved from the cache // instead (so $element['#printed'] will not be set). $element = $test_element; drupal_render($element); $this->assertFalse(isset($element['#printed']), t('Cache hit')); // Restore the previous request method. $_SERVER['REQUEST_METHOD'] = $request_method; } } /** * Tests URL validation by valid_url(). */ class CommonValidUrlUnitTestCase extends DrupalUnitTestCase { public static function getInfo() { return array( 'name' => 'URL validation', 'description' => 'Tests URL validation by valid_url()', 'group' => 'Common', ); } /** * Test valid absolute urls. */ function testValidAbsolute() { $url_schemes = array('http', 'https', 'ftp'); $valid_absolute_urls = array( 'example.com', 'www.example.com', 'ex-ample.com', '3xampl3.com', 'example.com/paren(the)sis', 'example.com/index.html#pagetop', 'example.com:8080', 'subdomain.example.com', 'example.com/index.php?q=node', 'example.com/index.php?q=node¶m=false', 'user@www.example.com', 'user:pass@www.example.com:8080/login.php?do=login&style=%23#pagetop', '127.0.0.1', 'example.org?', 'john%20doe:secret:foo@example.org/', 'example.org/~,$\'*;', 'caf%C3%A9.example.org', '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html', ); foreach ($url_schemes as $scheme) { foreach ($valid_absolute_urls as $url) { $test_url = $scheme . '://' . $url; $valid_url = valid_url($test_url, TRUE); $this->assertTrue($valid_url, t('@url is a valid url.', array('@url' => $test_url))); } } } /** * Test invalid absolute urls. */ function testInvalidAbsolute() { $url_schemes = array('http', 'https', 'ftp'); $invalid_ablosule_urls = array( '', 'ex!ample.com', 'ex%ample.com', ); foreach ($url_schemes as $scheme) { foreach ($invalid_ablosule_urls as $url) { $test_url = $scheme . '://' . $url; $valid_url = valid_url($test_url, TRUE); $this->assertFalse($valid_url, t('@url is NOT a valid url.', array('@url' => $test_url))); } } } /** * Test valid relative urls. */ function testValidRelative() { $valid_relative_urls = array( 'paren(the)sis', 'index.html#pagetop', 'index.php?q=node', 'index.php?q=node¶m=false', 'login.php?do=login&style=%23#pagetop', ); foreach (array('', '/') as $front) { foreach ($valid_relative_urls as $url) { $test_url = $front . $url; $valid_url = valid_url($test_url); $this->assertTrue($valid_url, t('@url is a valid url.', array('@url' => $test_url))); } } } /** * Test invalid relative urls. */ function testInvalidRelative() { $invalid_relative_urls = array( 'ex^mple', 'example<>', 'ex%ample', ); foreach (array('', '/') as $front) { foreach ($invalid_relative_urls as $url) { $test_url = $front . $url; $valid_url = valid_url($test_url); $this->assertFALSE($valid_url, t('@url is NOT a valid url.', array('@url' => $test_url))); } } } } /** * Tests writing of data records with drupal_write_record(). */ class CommonDrupalWriteRecordTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Data record write functionality', 'description' => 'Tests writing of data records with drupal_write_record().', 'group' => 'Common', ); } function setUp() { parent::setUp('database_test'); } /** * Test the drupal_write_record() API function. */ function testDrupalWriteRecord() { // Insert a record with no columns populated. $record = array(); $insert_result = drupal_write_record('test', $record); $this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when an empty record is inserted with drupal_write_record().')); // Insert a record - no columns allow NULL values. $person = new stdClass(); $person->name = 'John'; $person->unknown_column = 123; $insert_result = drupal_write_record('test', $person); $this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when a record is inserted with drupal_write_record() for a table with a single-field primary key.')); $this->assertTrue(isset($person->id), t('Primary key is set on record created with drupal_write_record().')); $this->assertIdentical($person->age, 0, t('Age field set to default value.')); $this->assertIdentical($person->job, 'Undefined', t('Job field set to default value.')); // Verify that the record was inserted. $result = db_query("SELECT * FROM {test} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical($result->name, 'John', t('Name field set.')); $this->assertIdentical($result->age, '0', t('Age field set to default value.')); $this->assertIdentical($result->job, 'Undefined', t('Job field set to default value.')); $this->assertFalse(isset($result->unknown_column), t('Unknown column was ignored.')); // Update the newly created record. $person->name = 'Peter'; $person->age = 27; $person->job = NULL; $update_result = drupal_write_record('test', $person, array('id')); $this->assertTrue($update_result == SAVED_UPDATED, t('Correct value returned when a record updated with drupal_write_record() for table with single-field primary key.')); // Verify that the record was updated. $result = db_query("SELECT * FROM {test} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical($result->name, 'Peter', t('Name field set.')); $this->assertIdentical($result->age, '27', t('Age field set.')); $this->assertIdentical($result->job, '', t('Job field set and cast to string.')); // Try to insert NULL in columns that does not allow this. $person = new stdClass(); $person->name = 'Ringo'; $person->age = NULL; $person->job = NULL; $insert_result = drupal_write_record('test', $person); $this->assertTrue(isset($person->id), t('Primary key is set on record created with drupal_write_record().')); $result = db_query("SELECT * FROM {test} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical($result->name, 'Ringo', t('Name field set.')); $this->assertIdentical($result->age, '0', t('Age field set.')); $this->assertIdentical($result->job, '', t('Job field set.')); // Insert a record - the "age" column allows NULL. $person = new stdClass(); $person->name = 'Paul'; $person->age = NULL; $insert_result = drupal_write_record('test_null', $person); $this->assertTrue(isset($person->id), t('Primary key is set on record created with drupal_write_record().')); $result = db_query("SELECT * FROM {test_null} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical($result->name, 'Paul', t('Name field set.')); $this->assertIdentical($result->age, NULL, t('Age field set.')); // Insert a record - do not specify the value of a column that allows NULL. $person = new stdClass(); $person->name = 'Meredith'; $insert_result = drupal_write_record('test_null', $person); $this->assertTrue(isset($person->id), t('Primary key is set on record created with drupal_write_record().')); $this->assertIdentical($person->age, 0, t('Age field set to default value.')); $result = db_query("SELECT * FROM {test_null} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical($result->name, 'Meredith', t('Name field set.')); $this->assertIdentical($result->age, '0', t('Age field set to default value.')); // Update the newly created record. $person->name = 'Mary'; $person->age = NULL; $update_result = drupal_write_record('test_null', $person, array('id')); $result = db_query("SELECT * FROM {test_null} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical($result->name, 'Mary', t('Name field set.')); $this->assertIdentical($result->age, NULL, t('Age field set.')); // Insert a record - the "data" column should be serialized. $person = new stdClass(); $person->name = 'Dave'; $update_result = drupal_write_record('test_serialized', $person); $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical($result->name, 'Dave', t('Name field set.')); $this->assertIdentical($result->info, NULL, t('Info field set.')); $person->info = array(); $update_result = drupal_write_record('test_serialized', $person, array('id')); $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical(unserialize($result->info), array(), t('Info field updated.')); // Update the serialized record. $data = array('foo' => 'bar', 1 => 2, 'empty' => '', 'null' => NULL); $person->info = $data; $update_result = drupal_write_record('test_serialized', $person, array('id')); $result = db_query("SELECT * FROM {test_serialized} WHERE id = :id", array(':id' => $person->id))->fetchObject(); $this->assertIdentical(unserialize($result->info), $data, t('Info field updated.')); // Run an update query where no field values are changed. The database // layer should return zero for number of affected rows, but // db_write_record() should still return SAVED_UPDATED. $update_result = drupal_write_record('test_null', $person, array('id')); $this->assertTrue($update_result == SAVED_UPDATED, t('Correct value returned when a valid update is run without changing any values.')); // Insert an object record for a table with a multi-field primary key. $node_access = new stdClass(); $node_access->nid = mt_rand(); $node_access->gid = mt_rand(); $node_access->realm = $this->randomName(); $insert_result = drupal_write_record('node_access', $node_access); $this->assertTrue($insert_result == SAVED_NEW, t('Correct value returned when a record is inserted with drupal_write_record() for a table with a multi-field primary key.')); // Update the record. $update_result = drupal_write_record('node_access', $node_access, array('nid', 'gid', 'realm')); $this->assertTrue($update_result == SAVED_UPDATED, t('Correct value returned when a record is updated with drupal_write_record() for a table with a multi-field primary key.')); } } /** * Tests SimpleTest error and exception collector. */ class CommonSimpleTestErrorCollectorTestCase extends DrupalWebTestCase { /** * Errors triggered during the test. * * Errors are intercepted by the overriden implementation * of DrupalWebTestCase::error below. * * @var Array */ protected $collectedErrors = array(); public static function getInfo() { return array( 'name' => 'SimpleTest error collector', 'description' => 'Performs tests on the Simpletest error and exception collector.', 'group' => 'Common', ); } function setUp() { parent::setUp('system_test', 'error_test'); } /** * Test that simpletest collects errors from the tested site. */ function testErrorCollect() { $this->collectedErrors = array(); $this->drupalGet('error-test/generate-warnings-with-report'); $this->assertEqual(count($this->collectedErrors), 3, t('Three errors were collected')); if (count($this->collectedErrors) == 3) { $this->assertError($this->collectedErrors[0], 'Notice', 'error_test_generate_warnings()', 'error_test.module', 'Undefined variable: bananas'); $this->assertError($this->collectedErrors[1], 'Warning', 'error_test_generate_warnings()', 'error_test.module', 'Division by zero'); $this->assertError($this->collectedErrors[2], 'User warning', 'error_test_generate_warnings()', 'error_test.module', 'Drupal is awesome'); } else { // Give back the errors to the log report. foreach ($this->collectedErrors as $error) { parent::error($error['message'], $error['group'], $error['caller']); } } } /** * Error handler that collects errors in an array. * * This test class is trying to verify that simpletest correctly sees errors * and warnings. However, it can't generate errors and warnings that * propagate up to the testing framework itself, or these tests would always * fail. So, this special copy of error() doesn't propagate the errors up * the class hierarchy. It just stuffs them into a protected collectedErrors * array for various assertions to inspect. */ protected function error($message = '', $group = 'Other', array $caller = NULL) { // Due to a WTF elsewhere, simpletest treats debug() and verbose() // messages as if they were an 'error'. But, we don't want to collect // those here. This function just wants to collect the real errors (PHP // notices, PHP fatal errors, etc.), and let all the 'errors' from the // 'User notice' group bubble up to the parent classes to be handled (and // eventually displayed) as normal. if ($group == 'User notice') { parent::error($message, $group, $caller); } // Everything else should be collected but not propagated. else { $this->collectedErrors[] = array( 'message' => $message, 'group' => $group, 'caller' => $caller ); } } /** * Assert that a collected error matches what we are expecting. */ function assertError($error, $group, $function, $file, $message = NULL) { $this->assertEqual($error['group'], $group, t("Group was %group", array('%group' => $group))); $this->assertEqual($error['caller']['function'], $function, t("Function was %function", array('%function' => $function))); $this->assertEqual(drupal_basename($error['caller']['file']), $file, t("File was %file", array('%file' => $file))); if (isset($message)) { $this->assertEqual($error['message'], $message, t("Message was %message", array('%message' => $message))); } } } /** * Tests the drupal_parse_info_file() API function. */ class CommonDrupalParseInfoFileTestCase extends DrupalUnitTestCase { public static function getInfo() { return array( 'name' => 'Parsing .info files', 'description' => 'Tests the drupal_parse_info_file() API function.', 'group' => 'Common', ); } /** * Parse an example .info file an verify the results. */ function testParseInfoFile() { $info_values = drupal_parse_info_file(drupal_get_path('module', 'simpletest') . '/tests/common_test_info.txt'); $this->assertEqual($info_values['simple_string'], 'A simple string', t('Simple string value was parsed correctly.'), t('System')); $this->assertEqual($info_values['simple_constant'], WATCHDOG_INFO, t('Constant value was parsed correctly.'), t('System')); $this->assertEqual($info_values['double_colon'], 'dummyClassName::', t('Value containing double-colon was parsed correctly.'), t('System')); } } /** * Tests scanning system directories in drupal_system_listing(). */ class CommonDrupalSystemListingTestCase extends DrupalWebTestCase { /** * Use the testing profile; this is needed for testDirectoryPrecedence(). */ protected $profile = 'testing'; public static function getInfo() { return array( 'name' => 'Drupal system listing', 'description' => 'Tests scanning system directories in drupal_system_listing().', 'group' => 'Common', ); } /** * Test that files in different directories take precedence as expected. */ function testDirectoryPrecedence() { // Define the module files we will search for, and the directory precedence // we expect. $expected_directories = array( // When the copy of the module in the profile directory is incompatible // with Drupal core, the copy in the core modules directory takes // precedence. 'drupal_system_listing_incompatible_test' => array( 'core/modules/simpletest/tests', 'profiles/testing/modules', ), // When both copies of the module are compatible with Drupal core, the // copy in the profile directory takes precedence. 'drupal_system_listing_compatible_test' => array( 'profiles/testing/modules', 'core/modules/simpletest/tests', ), ); // This test relies on two versions of the same module existing in // different places in the filesystem. Without that, the test has no // meaning, so assert their presence first. foreach ($expected_directories as $module => $directories) { foreach ($directories as $directory) { $filename = "$directory/$module/$module.module"; $this->assertTrue(file_exists(DRUPAL_ROOT . '/' . $filename), t('@filename exists.', array('@filename' => $filename))); } } // Now scan the directories and check that the files take precedence as // expected. $files = drupal_system_listing('/\.module$/', 'modules', 'name', 1); foreach ($expected_directories as $module => $directories) { $expected_directory = array_shift($directories); $expected_filename = "$expected_directory/$module/$module.module"; $this->assertEqual($files[$module]->uri, $expected_filename, t('Module @module was found at @filename.', array('@module' => $module, '@filename' => $expected_filename))); } } } /** * Tests the format_date() function. */ class CommonFormatDateTestCase extends DrupalWebTestCase { /** * Arbitrary langcode for a custom language. */ const LANGCODE = 'xx'; public static function getInfo() { return array( 'name' => 'Format date', 'description' => 'Test the format_date() function.', 'group' => 'Common', ); } function setUp() { parent::setUp('locale'); variable_set('configurable_timezones', 1); variable_set('date_format_long', 'l, j. F Y - G:i'); variable_set('date_format_medium', 'j. F Y - G:i'); variable_set('date_format_short', 'Y M j - g:ia'); variable_set('locale_custom_strings_' . self::LANGCODE, array( '' => array('Sunday' => 'domingo'), 'Long month name' => array('March' => 'marzo'), )); $this->refreshVariables(); } /** * Test admin-defined formats in format_date(). */ function testAdminDefinedFormatDate() { // Create an admin user. $this->admin_user = $this->drupalCreateUser(array('administer site configuration')); $this->drupalLogin($this->admin_user); // Add new date format. $admin_date_format = 'j M y'; $edit = array('date_format' => $admin_date_format); $this->drupalPost('admin/config/regional/date-time/formats/add', $edit, t('Add format')); // Add new date type. $edit = array( 'date_type' => 'Example Style', 'machine_name' => 'example_style', 'date_format' => $admin_date_format, ); $this->drupalPost('admin/config/regional/date-time/types/add', $edit, t('Add date type')); $timestamp = strtotime('2007-03-10T00:00:00+00:00'); $this->assertIdentical(format_date($timestamp, 'example_style', '', 'America/Los_Angeles'), '9 Mar 07', t('Test format_date() using an admin-defined date type.')); $this->assertIdentical(format_date($timestamp, 'undefined_style'), format_date($timestamp, 'medium'), t('Test format_date() defaulting to medium when $type not found.')); } /** * Tests for the format_date() function. */ function testFormatDate() { global $user, $language; $timestamp = strtotime('2007-03-26T00:00:00+00:00'); $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', t('Test all parameters.')); $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'domingo, 25-Mar-07 17:00:00 PDT', t('Test translated format.')); $this->assertIdentical(format_date($timestamp, 'custom', '\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'l, 25-Mar-07 17:00:00 PDT', t('Test an escaped format string.')); $this->assertIdentical(format_date($timestamp, 'custom', '\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\domingo, 25-Mar-07 17:00:00 PDT', t('Test format containing backslash character.')); $this->assertIdentical(format_date($timestamp, 'custom', '\\\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\l, 25-Mar-07 17:00:00 PDT', t('Test format containing backslash followed by escaped format string.')); $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London', 'en'), 'Monday, 26-Mar-07 01:00:00 BST', t('Test a different time zone.')); // Create an admin user and add Spanish language. $admin_user = $this->drupalCreateUser(array('administer languages')); $this->drupalLogin($admin_user); $edit = array( 'predefined_langcode' => 'custom', 'langcode' => self::LANGCODE, 'name' => self::LANGCODE, 'direction' => LANGUAGE_LTR, ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); // Set language prefix. $edit = array('prefix[' . self::LANGCODE . ']' => self::LANGCODE); $this->drupalPost('admin/config/regional/language/configure/url', $edit, t('Save configuration')); // Create a test user to carry out the tests. $test_user = $this->drupalCreateUser(); $this->drupalLogin($test_user); $edit = array('language' => self::LANGCODE, 'mail' => $test_user->mail, 'timezone' => 'America/Los_Angeles'); $this->drupalPost('user/' . $test_user->uid . '/edit', $edit, t('Save')); // Disable session saving as we are about to modify the global $user. drupal_save_session(FALSE); // Save the original user and language and then replace it with the test user and language. $real_user = $user; $user = user_load($test_user->uid, TRUE); $real_language = $language->langcode; $language->langcode = $user->language; // Simulate a Drupal bootstrap with the logged-in user. date_default_timezone_set(drupal_get_user_timezone()); $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', t('Test a different language.')); $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London'), 'Monday, 26-Mar-07 01:00:00 BST', t('Test a different time zone.')); $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T'), 'domingo, 25-Mar-07 17:00:00 PDT', t('Test custom date format.')); $this->assertIdentical(format_date($timestamp, 'long'), 'domingo, 25. marzo 2007 - 17:00', t('Test long date format.')); $this->assertIdentical(format_date($timestamp, 'medium'), '25. marzo 2007 - 17:00', t('Test medium date format.')); $this->assertIdentical(format_date($timestamp, 'short'), '2007 Mar 25 - 5:00pm', t('Test short date format.')); $this->assertIdentical(format_date($timestamp), '25. marzo 2007 - 17:00', t('Test default date format.')); // Test HTML time element formats. $this->assertIdentical(format_date($timestamp, 'html_datetime'), '2007-03-25T17:00:00-0700', t('Test html_datetime date format.')); $this->assertIdentical(format_date($timestamp, 'html_date'), '2007-03-25', t('Test html_date date format.')); $this->assertIdentical(format_date($timestamp, 'html_time'), '17:00:00', t('Test html_time date format.')); $this->assertIdentical(format_date($timestamp, 'html_yearless_date'), '03-25', t('Test html_yearless_date date format.')); $this->assertIdentical(format_date($timestamp, 'html_week'), '2007-W12', t('Test html_week date format.')); $this->assertIdentical(format_date($timestamp, 'html_month'), '2007-03', t('Test html_month date format.')); $this->assertIdentical(format_date($timestamp, 'html_year'), '2007', t('Test html_year date format.')); // Restore the original user and language, and enable session saving. $user = $real_user; $language->langcode = $real_language; // Restore default time zone. date_default_timezone_set(drupal_get_user_timezone()); drupal_save_session(TRUE); } } /** * Tests the drupal_attributes() functionality. */ class CommonDrupalAttributesUnitTestCase extends DrupalUnitTestCase { public static function getInfo() { return array( 'name' => 'HTML Attributes', 'description' => 'Tests the drupal_attributes() functionality.', 'group' => 'Common', ); } /** * Tests that drupal_html_class() cleans the class name properly. */ function testDrupalAttributes() { // Verify that special characters are HTML encoded. $this->assertIdentical(drupal_attributes(array('title' => '&"\'<>')), ' title="&"'<>"', t('HTML encode attribute values.')); // Verify multi-value attributes are concatenated with spaces. $attributes = array('class' => array('first', 'last')); $this->assertIdentical(drupal_attributes(array('class' => array('first', 'last'))), ' class="first last"', t('Concatenate multi-value attributes.')); // Verify empty attribute values are rendered. $this->assertIdentical(drupal_attributes(array('alt' => '')), ' alt=""', t('Empty attribute value #1.')); $this->assertIdentical(drupal_attributes(array('alt' => NULL)), ' alt=""', t('Empty attribute value #2.')); // Verify multiple attributes are rendered. $attributes = array( 'id' => 'id-test', 'class' => array('first', 'last'), 'alt' => 'Alternate', ); $this->assertIdentical(drupal_attributes($attributes), ' id="id-test" class="first last" alt="Alternate"', t('Multiple attributes.')); // Verify empty attributes array is rendered. $this->assertIdentical(drupal_attributes(array()), '', t('Empty attributes array.')); } } /** * Tests the various drupal_array_* helper functions. */ class CommonDrupalArrayUnitTest extends DrupalUnitTestCase { /** * Form array to check. */ protected $form; /** * Array of parents for the nested element. */ protected $parents; public static function getInfo() { return array( 'name' => 'drupal_array_*() tests', 'description' => 'Tests the various drupal_array_* helper functions.', 'group' => 'System', ); } function setUp() { parent::setUp(); // Create a form structure with a nested element. $this->form['fieldset']['element'] = array( '#value' => 'Nested element', ); // Set up parent array. $this->parents = array('fieldset', 'element'); } /** * Tests getting nested array values. */ function testGet() { // Verify getting a value of a nested element. $value = drupal_array_get_nested_value($this->form, $this->parents); $this->assertEqual($value['#value'], 'Nested element', 'Nested element value found.'); // Verify changing a value of a nested element by reference. $value = &drupal_array_get_nested_value($this->form, $this->parents); $value['#value'] = 'New value'; $value = drupal_array_get_nested_value($this->form, $this->parents); $this->assertEqual($value['#value'], 'New value', 'Nested element value was changed by reference.'); $this->assertEqual($this->form['fieldset']['element']['#value'], 'New value', 'Nested element value was changed by reference.'); // Verify that an existing key is reported back. $key_exists = NULL; drupal_array_get_nested_value($this->form, $this->parents, $key_exists); $this->assertIdentical($key_exists, TRUE, 'Existing key found.'); // Verify that a non-existing key is reported back and throws no errors. $key_exists = NULL; $parents = $this->parents; $parents[] = 'foo'; drupal_array_get_nested_value($this->form, $parents, $key_exists); $this->assertIdentical($key_exists, FALSE, 'Non-existing key not found.'); } /** * Tests setting nested array values. */ function testSet() { $new_value = array( '#value' => 'New value', '#required' => TRUE, ); // Verify setting the value of a nested element. drupal_array_set_nested_value($this->form, $this->parents, $new_value); $this->assertEqual($this->form['fieldset']['element']['#value'], 'New value', 'Changed nested element value found.'); $this->assertIdentical($this->form['fieldset']['element']['#required'], TRUE, 'New nested element value found.'); } /** * Tests unsetting nested array values. */ function testUnset() { // Verify unsetting a non-existing nested element throws no errors and the // non-existing key is properly reported. $key_existed = NULL; $parents = $this->parents; $parents[] = 'foo'; drupal_array_unset_nested_value($this->form, $parents, $key_existed); $this->assertTrue(isset($this->form['fieldset']['element']['#value']), 'Outermost nested element key still exists.'); $this->assertIdentical($key_existed, FALSE, 'Non-existing key not found.'); // Verify unsetting a nested element. $key_existed = NULL; drupal_array_unset_nested_value($this->form, $this->parents, $key_existed); $this->assertFalse(isset($this->form['fieldset']['element']), 'Removed nested element not found.'); $this->assertIdentical($key_existed, TRUE, 'Existing key was found.'); } /** * Tests existence of array key. */ function testKeyExists() { // Verify that existing key is found. $this->assertIdentical(drupal_array_nested_key_exists($this->form, $this->parents), TRUE, 'Nested key found.'); // Verify that non-existing keys are not found. $parents = $this->parents; $parents[] = 'foo'; $this->assertIdentical(drupal_array_nested_key_exists($this->form, $parents), FALSE, 'Non-existing nested key not found.'); } } /** * Tests the drupal_json_encode() and drupal_json_decode() functions. */ class CommonJSONUnitTestCase extends DrupalUnitTestCase { public static function getInfo() { return array( 'name' => 'JSON', 'description' => 'Tests the drupal_json_encode() and drupal_json_decode() functions to convert PHP variables to JSON strings and back.', 'group' => 'Common', ); } /** * Tests converting PHP variables to JSON strings and back. */ function testJSON() { // Setup a string with the full ASCII table. // @todo: Add tests for non-ASCII characters and Unicode. $str = ''; for ($i=0; $i < 128; $i++) { $str .= chr($i); } // Characters that must be escaped. // We check for unescaped " separately. $html_unsafe = array('<', '>', '\'', '&'); // The following are the encoded forms of: < > ' & " $html_unsafe_escaped = array('\u003C', '\u003E', '\u0027', '\u0026', '\u0022'); // Verify there aren't character encoding problems with the source string. $this->assertIdentical(strlen($str), 128, t('A string with the full ASCII table has the correct length.')); foreach ($html_unsafe as $char) { $this->assertTrue(strpos($str, $char) > 0, t('A string with the full ASCII table includes @s.', array('@s' => $char))); } // Verify that JSON encoding produces a string with all of the characters. $json = drupal_json_encode($str); $this->assertTrue(strlen($json) > strlen($str), t('A JSON encoded string is larger than the source string.')); // The first and last characters should be ", and no others. $this->assertTrue($json[0] == '"', t('A JSON encoded string begins with ".')); $this->assertTrue($json[strlen($json) - 1] == '"', t('A JSON encoded string ends with ".')); $this->assertTrue(substr_count($json, '"') == 2, t('A JSON encoded string contains exactly two ".')); // Verify that encoding/decoding is reversible. $json_decoded = drupal_json_decode($json); $this->assertIdentical($str, $json_decoded, t('Encoding a string to JSON and decoding back results in the original string.')); // Verify reversibility for structured data. Also verify that necessary // characters are escaped. $source = array(TRUE, FALSE, 0, 1, '0', '1', $str, array('key1' => $str, 'key2' => array('nested' => TRUE))); $json = drupal_json_encode($source); foreach ($html_unsafe as $char) { $this->assertTrue(strpos($json, $char) === FALSE, t('A JSON encoded string does not contain @s.', array('@s' => $char))); } // Verify that JSON encoding escapes the HTML unsafe characters foreach ($html_unsafe_escaped as $char) { $this->assertTrue(strpos($json, $char) > 0, t('A JSON encoded string contains @s.', array('@s' => $char))); } $json_decoded = drupal_json_decode($json); $this->assertNotIdentical($source, $json, t('An array encoded in JSON is not identical to the source.')); $this->assertIdentical($source, $json_decoded, t('Encoding structured data to JSON and decoding back results in the original data.')); } } /** * Basic tests for drupal_add_feed(). */ class CommonDrupalAddFeedTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'drupal_add_feed() tests', 'description' => 'Make sure that drupal_add_feed() works correctly with various constructs.', 'group' => 'Common', ); } /** * Test drupal_add_feed() with paths, URLs, and titles. */ function testBasicFeedAddNoTitle() { $path = $this->randomName(12); $external_url = 'http://' . $this->randomName(12) . '/' . $this->randomName(12); $fully_qualified_local_url = url($this->randomName(12), array('absolute' => TRUE)); $path_for_title = $this->randomName(12); $external_for_title = 'http://' . $this->randomName(12) . '/' . $this->randomName(12); $fully_qualified_for_title = url($this->randomName(12), array('absolute' => TRUE)); // Possible permutations of drupal_add_feed() to test. // - 'input_url': the path passed to drupal_add_feed(), // - 'output_url': the expected URL to be found in the header. // - 'title' == the title of the feed as passed into drupal_add_feed(). $urls = array( 'path without title' => array( 'input_url' => $path, 'output_url' => url($path, array('absolute' => TRUE)), 'title' => '', ), 'external url without title' => array( 'input_url' => $external_url, 'output_url' => $external_url, 'title' => '', ), 'local url without title' => array( 'input_url' => $fully_qualified_local_url, 'output_url' => $fully_qualified_local_url, 'title' => '', ), 'path with title' => array( 'input_url' => $path_for_title, 'output_url' => url($path_for_title, array('absolute' => TRUE)), 'title' => $this->randomName(12), ), 'external url with title' => array( 'input_url' => $external_for_title, 'output_url' => $external_for_title, 'title' => $this->randomName(12), ), 'local url with title' => array( 'input_url' => $fully_qualified_for_title, 'output_url' => $fully_qualified_for_title, 'title' => $this->randomName(12), ), ); foreach ($urls as $description => $feed_info) { drupal_add_feed($feed_info['input_url'], $feed_info['title']); } $this->drupalSetContent(drupal_get_html_head()); foreach ($urls as $description => $feed_info) { $this->assertPattern($this->urlToRSSLinkPattern($feed_info['output_url'], $feed_info['title']), t('Found correct feed header for %description', array('%description' => $description))); } } /** * Create a pattern representing the RSS feed in the page. */ function urlToRSSLinkPattern($url, $title = '') { // Escape any regular expression characters in the url ('?' is the worst). $url = preg_replace('/([+?.*])/', '[$0]', $url); $generated_pattern = '%%'; return $generated_pattern; } }