Skip to content
Snippets Groups Projects
Commit bcd866b6 authored by Dries Buytaert's avatar Dries Buytaert
Browse files

Issue #2116827 by Wim Leers: Speed up RenderTest: move integration tests into separate test.

parent dda81912
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -7,19 +7,19 @@ ...@@ -7,19 +7,19 @@
namespace Drupal\system\Tests\Common; namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\DrupalUnitTestBase;
/** /**
* Tests drupal_render(). * Tests drupal_render().
*/ */
class RenderTest extends WebTestBase { class RenderTest extends DrupalUnitTestBase {
/** /**
* Modules to enable. * Modules to enable.
* *
* @var array * @var array
*/ */
public static $modules = array('common_test'); public static $modules = array('system', 'common_test');
public static function getInfo() { public static function getInfo() {
return array( return array(
...@@ -327,10 +327,10 @@ function testDrupalRenderSorting() { ...@@ -327,10 +327,10 @@ function testDrupalRenderSorting() {
*/ */
function testDrupalRenderChildrenAttached() { function testDrupalRenderChildrenAttached() {
// The cache system is turned off for POST requests. // The cache system is turned off for POST requests.
$request_method = $_SERVER['REQUEST_METHOD']; $request_method = \Drupal::request()->getMethod();
$_SERVER['REQUEST_METHOD'] = 'GET'; \Drupal::request()->setMethod('GET');
// Create an element with a child and subchild. Each element loads a // Create an element with a child and subchild. Each element loads a
// different JavaScript file using #attached. // different JavaScript file using #attached.
$parent_js = drupal_get_path('module', 'user') . '/user.js'; $parent_js = drupal_get_path('module', 'user') . '/user.js';
$child_js = drupal_get_path('module', 'forum') . '/forum.js'; $child_js = drupal_get_path('module', 'forum') . '/forum.js';
...@@ -369,7 +369,8 @@ function testDrupalRenderChildrenAttached() { ...@@ -369,7 +369,8 @@ function testDrupalRenderChildrenAttached() {
$this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included when loading from cache.'); $this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included when loading from cache.');
$this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included when loading from cache.'); $this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included when loading from cache.');
$_SERVER['REQUEST_METHOD'] = $request_method; // Restore the previous request method.
\Drupal::request()->setMethod($request_method);
} }
/** /**
...@@ -390,165 +391,14 @@ function testDrupalRenderThemeArguments() { ...@@ -390,165 +391,14 @@ function testDrupalRenderThemeArguments() {
$this->assertEqual(drupal_render($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works'); $this->assertEqual(drupal_render($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works');
} }
/**
* Tests 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' => 'details',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//details/summary[contains(., :title)]', array(
':title' => $element['#title'],
));
$element = array(
'#type' => 'details',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//details');
$element['item'] = array(
'#type' => 'item',
'#title' => $this->randomName(),
'#markup' => $this->randomName(),
);
$this->assertRenderedElement($element, '//details/div/div[contains(@class, :class) and contains(., :markup)]', array(
':class' => 'form-type-item',
':markup' => $element['item']['#markup'],
));
}
/**
* Tests 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);
\Drupal::config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
$this->drupalGet('common-test/drupal-render-invalid-keys');
$this->assertResponse(200, 'Received expected HTTP status code.');
$this->assertRaw($message, format_string('Found error message: !message.', array('!message' => $message)));
}
/**
* Tests that elements are rendered properly.
*/
protected function assertRenderedElement(array $element, $xpath, array $xpath_args = array()) {
$original_element = $element;
$this->drupalSetContent(drupal_render($element));
$this->verbose('<pre>' . check_plain(var_export($original_element, TRUE)) . '</pre>'
. '<pre>' . check_plain(var_export($element, TRUE)) . '</pre>'
. '<hr />' . $this->drupalGetContent()
);
// @see \Drupal\simpletest\WebTestBase::xpath()
$xpath = $this->buildXPathQuery($xpath, $xpath_args);
$element += array('#value' => NULL);
$this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', array(
'@type' => var_export($element['#type'], TRUE),
)));
}
/** /**
* Tests caching of an empty render item. * Tests caching of an empty render item.
*/ */
function testDrupalRenderCache() { function testDrupalRenderCache() {
// Force a request via GET. // The cache system is turned off for POST requests.
$request_method = $_SERVER['REQUEST_METHOD']; $request_method = \Drupal::request()->getMethod();
$_SERVER['REQUEST_METHOD'] = 'GET'; \Drupal::request()->setMethod('GET');
// Create an empty element. // Create an empty element.
$test_element = array( $test_element = array(
'#cache' => array( '#cache' => array(
...@@ -587,6 +437,6 @@ function testDrupalRenderCache() { ...@@ -587,6 +437,6 @@ function testDrupalRenderCache() {
$this->assertEqual($expected_tags, $actual_tags, 'Cache tags were collected from the element and its subchild.'); $this->assertEqual($expected_tags, $actual_tags, 'Cache tags were collected from the element and its subchild.');
// Restore the previous request method. // Restore the previous request method.
$_SERVER['REQUEST_METHOD'] = $request_method; \Drupal::request()->setMethod($request_method);
} }
} }
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\RenderWebTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
/**
* Tests drupal_render() in a full environment.
*/
class RenderWebTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('common_test');
public static function getInfo() {
return array(
'name' => 'drupal_render() in a full environment',
'description' => 'Performs integration tests on drupal_render().',
'group' => 'Common',
);
}
/**
* Tests 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' => 'details',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//details/summary[contains(., :title)]', array(
':title' => $element['#title'],
));
$element = array(
'#type' => 'details',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//details');
$element['item'] = array(
'#type' => 'item',
'#title' => $this->randomName(),
'#markup' => $this->randomName(),
);
$this->assertRenderedElement($element, '//details/div/div[contains(@class, :class) and contains(., :markup)]', array(
':class' => 'form-type-item',
':markup' => $element['item']['#markup'],
));
}
/**
* Tests 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);
\Drupal::config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
$this->drupalGet('common-test/drupal-render-invalid-keys');
$this->assertResponse(200, 'Received expected HTTP status code.');
$this->assertRaw($message, format_string('Found error message: !message.', array('!message' => $message)));
}
/**
* Tests that elements are rendered properly.
*/
protected function assertRenderedElement(array $element, $xpath, array $xpath_args = array()) {
$original_element = $element;
$this->drupalSetContent(drupal_render($element));
$this->verbose('<pre>' . check_plain(var_export($original_element, TRUE)) . '</pre>'
. '<pre>' . check_plain(var_export($element, TRUE)) . '</pre>'
. '<hr />' . $this->drupalGetContent()
);
// @see \Drupal\simpletest\WebTestBase::xpath()
$xpath = $this->buildXPathQuery($xpath, $xpath_args);
$element += array('#value' => NULL);
$this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', array(
'@type' => var_export($element['#type'], TRUE),
)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment