From 6a30c869c2c10b0281247a4288920889151c724b Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Wed, 28 Sep 2011 23:21:28 +0900 Subject: [PATCH] Issue #1283892 by tim.plunkett, chx: Let Render API fail in a tale-telling way on invalid element. --- includes/common.inc | 13 +++++++++--- modules/menu/menu.admin.inc | 2 +- modules/simpletest/tests/common.test | 17 ++++++++++++++++ modules/simpletest/tests/common_test.module | 22 +++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index dd4a2fd8799b..d64be82515e6 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -6145,9 +6145,16 @@ function element_children(&$elements, $sort = FALSE) { $sortable = FALSE; foreach ($elements as $key => $value) { if ($key === '' || $key[0] !== '#') { - $children[$key] = $value; - if (is_array($value) && isset($value['#weight'])) { - $sortable = TRUE; + if (is_array($value)) { + $children[$key] = $value; + if (isset($value['#weight'])) { + $sortable = TRUE; + } + } + // Only trigger an error if the value is not null. + // @see http://drupal.org/node/1283892 + elseif (isset($value)) { + trigger_error(t('"@key" is an invalid render array key', array('@key' => $key)), E_USER_ERROR); } } } diff --git a/modules/menu/menu.admin.inc b/modules/menu/menu.admin.inc index 91229b497418..8669c33eb213 100644 --- a/modules/menu/menu.admin.inc +++ b/modules/menu/menu.admin.inc @@ -158,7 +158,7 @@ function menu_overview_form_submit($form, &$form_state) { // are sent by $_POST, ensuring parents are saved first, then their children. // See http://drupal.org/node/181126#comment-632270 $order = array_flip(array_keys($form_state['input'])); // Get the $_POST order. - $form = array_merge($order, $form); // Update our original form with the new order. + $form = array_intersect_key(array_merge($order, $form), $form); // Update our original form with the new order. $updated_items = array(); $fields = array('weight', 'plid'); diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index d7c3b055051e..021e7210f4fc 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -1724,6 +1724,23 @@ class DrupalRenderTestCase extends DrupalWebTestCase { )); } + /** + * 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)); diff --git a/modules/simpletest/tests/common_test.module b/modules/simpletest/tests/common_test.module index b8355a9bcc95..a3bb9b0fade9 100644 --- a/modules/simpletest/tests/common_test.module +++ b/modules/simpletest/tests/common_test.module @@ -52,6 +52,12 @@ function common_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['common-test/drupal-render-invalid-keys'] = array( + 'title' => 'Drupal Render', + 'page callback' => 'common_test_drupal_render_invalid_keys', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -100,6 +106,22 @@ function common_test_destination() { print "The destination: " . check_plain($destination['destination']); } +/** + * Render an element with an invalid render array key. + */ +function common_test_drupal_render_invalid_keys() { + define('SIMPLETEST_COLLECT_ERRORS', FALSE); + + // Keys that begin with # may contain a value of any type, otherwise they must + // contain arrays. + $key = 'child'; + $value = 'This should be an array.'; + $element = array( + $key => $value, + ); + return drupal_render($element); +} + /** * Implements hook_TYPE_alter(). */ -- GitLab