Commit 8501b7f2 authored by alexpott's avatar alexpott

Issue #2257519 by sun, larowlan, xjm, cs_shadow: Move content assertion...

Issue #2257519 by sun, larowlan, xjm, cs_shadow: Move content assertion methods into a trait, so DUTB can consume it, too.
parent b314a3ea
......@@ -126,8 +126,7 @@ function testFieldItemListView() {
$items = $this->entity->get($this->field_name);
// No display settings: check that default display settings are used.
$output = $items->view();
$this->content = drupal_render($output);
$this->render($items->view());
$settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default');
$setting = $settings['test_formatter_setting'];
$this->assertText($this->label, 'Label was displayed.');
......@@ -144,8 +143,7 @@ function testFieldItemListView() {
'alter' => TRUE,
),
);
$output = $items->view($display);
$this->content = drupal_render($output);
$this->render($items->view($display));
$setting = $display['settings']['test_formatter_setting_multiple'];
$this->assertNoText($this->label, 'Label was not displayed.');
$this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
......@@ -165,8 +163,7 @@ function testFieldItemListView() {
'alter' => TRUE,
),
);
$output = $items->view($display);
$this->content = drupal_render($output);
$this->render($items->view($display));
$setting = $display['settings']['test_formatter_setting_multiple'];
$this->assertRaw('visually-hidden', 'Label was visually hidden.');
$this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
......@@ -185,9 +182,7 @@ function testFieldItemListView() {
'test_formatter_setting_additional' => $this->randomName(),
),
);
$output = $items->view($display);
$view = drupal_render($output);
$this->content = $view;
$this->render($items->view($display));
$setting = $display['settings']['test_formatter_setting_additional'];
$this->assertNoText($this->label, 'Label was not displayed.');
$this->assertNoText('field_test_entity_display_build_alter', 'Alter not fired.');
......@@ -197,8 +192,7 @@ function testFieldItemListView() {
// View mode: check that display settings specified in the display object
// are used.
$output = $items->view('teaser');
$this->content = drupal_render($output);
$this->render($items->view('teaser'));
$setting = $this->display_options['teaser']['settings']['test_formatter_setting'];
$this->assertText($this->label, 'Label was displayed.');
foreach ($this->values as $delta => $value) {
......@@ -207,8 +201,7 @@ function testFieldItemListView() {
// Unknown view mode: check that display settings for 'default' view mode
// are used.
$output = $items->view('unknown_view_mode');
$this->content = drupal_render($output);
$this->render($items->view('unknown_view_mode'));
$setting = $this->display_options['default']['settings']['test_formatter_setting'];
$this->assertText($this->label, 'Label was displayed.');
foreach ($this->values as $delta => $value) {
......@@ -225,8 +218,7 @@ function testFieldItemView() {
$setting = $settings['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[$delta];
$output = $item->view();
$this->content = drupal_render($output);
$this->render($item->view());
$this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -240,8 +232,7 @@ function testFieldItemView() {
$setting = $display['settings']['test_formatter_setting_multiple'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[$delta];
$output = $item->view($display);
$this->content = drupal_render($output);
$this->render($item->view($display));
$this->assertText($setting . '|0:' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -255,8 +246,7 @@ function testFieldItemView() {
$setting = $display['settings']['test_formatter_setting_additional'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[$delta];
$output = $item->view($display);
$this->content = drupal_render($output);
$this->render($item->view($display));
$this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -265,8 +255,7 @@ function testFieldItemView() {
$setting = $this->display_options['teaser']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[$delta];
$output = $item->view('teaser');
$this->content = drupal_render($output);
$this->render($item->view('teaser'));
$this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
......@@ -275,8 +264,7 @@ function testFieldItemView() {
$setting = $this->display_options['default']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->field_name}[$delta];
$output = $item->view('unknown_view_mode');
$this->content = drupal_render($output);
$this->render($item->view('unknown_view_mode'));
$this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
}
}
......@@ -295,9 +283,7 @@ function testFieldEmpty() {
);
// $this->entity is set by the setUp() method and by default contains 4
// numeric values. We only want to test the display of this one field.
$output = $this->entity->get($this->field_name)->view($display);
$view = drupal_render($output);
$this->content = $view;
$this->render($this->entity->get($this->field_name)->view($display));
// The test field by default contains values, so should not display the
// default "empty" text.
$this->assertNoText($display['settings']['test_empty_string']);
......@@ -305,9 +291,7 @@ function testFieldEmpty() {
// Now remove the values from the test field and retest.
$this->entity->{$this->field_name} = array();
$this->entity->save();
$output = $this->entity->get($this->field_name)->view($display);
$view = drupal_render($output);
$this->content = $view;
$this->render($this->entity->get($this->field_name)->view($display));
// This time, as the field values have been removed, we *should* show the
// default "empty" text.
$this->assertText($display['settings']['test_empty_string']);
......
......@@ -85,7 +85,7 @@ function testEntityDisplayBuild() {
// View all fields.
$content = $display->build($entity);
$this->content = drupal_render($content);
$this->render($content);
$this->assertRaw($this->instance->getLabel(), "First field's label is displayed.");
foreach ($values as $delta => $value) {
$this->assertRaw("$formatter_setting|{$value['value']}", "Value $delta is displayed, formatter settings are applied.");
......@@ -100,14 +100,14 @@ function testEntityDisplayBuild() {
$display_options['label'] = 'hidden';
$display->setComponent($this->field->getName(), $display_options);
$content = $display->build($entity);
$this->content = drupal_render($content);
$this->render($content);
$this->assertNoRaw($this->instance->getLabel(), "Hidden label: label is not displayed.");
// Field hidden.
$entity = clone($entity_init);
$display->removeComponent($this->field->getName());
$content = $display->build($entity);
$this->content = drupal_render($content);
$this->render($content);
$this->assertNoRaw($this->instance->getLabel(), "Hidden field: label is not displayed.");
foreach ($values as $delta => $value) {
$this->assertNoRaw("$formatter_setting|{$value['value']}", "Hidden field: value $delta is not displayed.");
......@@ -124,7 +124,7 @@ function testEntityDisplayBuild() {
),
));
$content = $display->build($entity);
$this->content = drupal_render($content);
$this->render($content);
$expected_output = $formatter_setting;
foreach ($values as $delta => $value) {
$expected_output .= "|$delta:{$value['value']}";
......@@ -142,7 +142,7 @@ function testEntityDisplayBuild() {
),
));
$content = $display->build($entity);
$this->content = drupal_render($content);
$this->render($content);
foreach ($values as $delta => $value) {
$expected = $formatter_setting . '|' . $value['value'] . '|' . ($value['value'] + 1);
$this->assertRaw($expected, "Value $delta is displayed, formatter settings are applied.");
......
......@@ -7,7 +7,6 @@
namespace Drupal\field\Tests;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\DrupalUnitTestBase;
......@@ -24,13 +23,6 @@ abstract class FieldUnitTestBase extends DrupalUnitTestBase {
*/
public static $modules = array('user', 'entity', 'system', 'field', 'text', 'entity_test', 'field_test');
/**
* A string for assert raw and text helper methods.
*
* @var string
*/
protected $content;
/**
* Set the default field storage backend for fields created during tests.
*/
......@@ -162,104 +154,4 @@ function assertFieldValues(EntityInterface $entity, $field_name, $expected_value
}
}
/**
* Pass if the raw text IS found in set string.
*
* @param $raw
* Raw (HTML) string to look for.
* @param $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $raw));
}
return $this->assert(strpos($this->content, $raw) !== FALSE, $message, $group);
}
/**
* Pass if the raw text IS NOT found in set string.
*
* @param $raw
* Raw (HTML) string to look for.
* @param $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoRaw($raw, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $raw));
}
return $this->assert(strpos($this->content, $raw) === FALSE, $message, $group);
}
/**
* Pass if the text IS found in set string.
*
* @param $text
* Text to look for.
* @param $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertText($text, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $text));
}
return $this->assert(strpos(Xss::filter($this->content, array()), $text) !== FALSE, $message, $group);
}
/**
* Pass if the text IS NOT found in set string.
*
* @param $text
* Text to look for.
* @param $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return
* TRUE on pass, FALSE on fail.
*/
protected function assertNoText($text, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" not found', array('@raw' => $text));
}
return $this->assert(strpos(Xss::filter($this->content, array()), $text) === FALSE, $message, $group);
}
}
......@@ -431,7 +431,7 @@ function testFieldFormJSAddMore() {
// Press 'add more' button through Ajax, and place the expected HTML result
// as the tested content.
$commands = $this->drupalPostAjaxForm(NULL, $edit, $field_name . '_add_more');
$this->content = $commands[1]['data'];
$this->setRawContent($commands[1]['data']);
for ($delta = 0; $delta <= $delta_range; $delta++) {
$this->assertFieldByName("{$field_name}[$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
......
......@@ -9,7 +9,6 @@
use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\simpletest\WebTestBase;
......@@ -126,27 +125,4 @@ public function testConfigurationRename() {
$this->assertIdentical($staged_type, $content_type->type);
}
/**
* Asserts that a Perl regex pattern is found in the text content.
*
* @param string $pattern
* Perl regex to look for including the regex delimiters.
* @param string $message
* (optional) A message to display with the assertion.
*
* @return bool
* TRUE on pass, FALSE on failure.
*/
protected function assertTextPattern($pattern, $message = NULL) {
// @see WebTestBase::assertTextHelper()
if ($this->plainTextContent === FALSE) {
$this->plainTextContent = Xss::filter($this->drupalGetContent(), array());
}
// @see WebTestBase::assertPattern()
if (!$message) {
$message = String::format('Pattern "@pattern" found', array('@pattern' => $pattern));
}
return $this->assert((bool) preg_match($pattern, $this->plainTextContent), $message);
}
}
This diff is collapsed.
......@@ -36,6 +36,8 @@
*/
abstract class KernelTestBase extends UnitTestBase {
use AssertContentTrait;
/**
* Modules to enable.
*
......@@ -534,4 +536,20 @@ protected function unregisterStreamWrapper($scheme, $type) {
}
}
/**
* Renders a render array.
*
* @param array $elements
* The elements to render.
*
* @return string
* The rendered string output (typically HTML).
*/
protected function render(array $elements) {
$content = drupal_render($elements);
$this->setRawContent($content);
$this->verbose('<pre style="white-space: pre-wrap">' . String::checkPlain($content));
return $content;
}
}
This diff is collapsed.
......@@ -68,11 +68,11 @@ public function testAssertFieldByName($filename, $name, $value, $expected) {
$web_test = $this->getMockBuilder('Drupal\simpletest\WebTestBase')
->disableOriginalConstructor()
->setMethods(array('assertTrue', 'drupalGetContent', 'pass'))
->setMethods(array('getRawContent', 'assertTrue', 'pass'))
->getMock();
$web_test->expects($this->any())
->method('drupalGetContent')
->method('getRawContent')
->will($this->returnValue($content));
$web_test->expects($this->once())
......
......@@ -7,7 +7,6 @@
namespace Drupal\system\Tests\System;
use Drupal\Component\Utility\String;
use Drupal\simpletest\DrupalUnitTestBase;
/**
......@@ -44,7 +43,7 @@ public function testPasswordHashSh() {
);
ob_start();
include DRUPAL_ROOT . '/core/scripts/password-hash.sh';
$this->content = ob_get_contents();
$this->setRawContent(ob_get_contents());
ob_end_clean();
$this->assertRaw('hash: $S$');
}
......@@ -66,22 +65,9 @@ public function testRebuildTokenCalculatorSh() {
);
ob_start();
include DRUPAL_ROOT . '/core/scripts/rebuild_token_calculator.sh';
$this->content = ob_get_contents();
$this->setRawContent(ob_get_contents());
ob_end_clean();
$this->assertRaw('token=');
}
/**
* Asserts that a given string is found in $this->content.
*
* @param string $string
* The raw string to assert.
*/
protected function assertRaw($string) {
return $this->assert(strpos($this->content, $string) !== FALSE, String::format('Raw @value found in @output.', array(
'@value' => var_export($string, TRUE),
'@output' => var_export($this->content, TRUE),
)));
}
}
......@@ -7,7 +7,6 @@
namespace Drupal\system\Tests\Theme;
use Drupal\Component\Utility\String;
use Drupal\simpletest\DrupalUnitTestBase;
/**
......@@ -211,59 +210,4 @@ public function testThemeTableResponsivePriority() {
$this->assertRaw('<td>6</td>', 'Cell 3: no priority classes were applied.');
}
/**
* Renders a given render array.
*
* @param array $elements
* The render array elements to render.
*
* @return string
* The rendered HTML.
*/
protected function render(array $elements) {
$this->content = drupal_render($elements);
$this->verbose('<pre>' . String::checkPlain($this->content));
}
/**
* Removes all white-space between HTML tags from $this->content.
*/
protected function removeWhiteSpace() {
$this->content = preg_replace('@>\s+<@', '><', $this->content);
}
/**
* Asserts that a raw string appears in $this->content.
*
* @param string $value
* The expected string.
* @param string $message
* (optional) A custom assertion message.
*/
protected function assertRaw($value, $message = NULL) {
if (!isset($message)) {
$message = String::format("Raw value @value found.", array(
'@value' => var_export($value, TRUE),
));
}
$this->assert(strpos($this->content, $value) !== FALSE, $message);
}
/**
* Asserts that a raw string does not appear in $this->content.
*
* @param string $value
* The not expected string.
* @param string $message
* (optional) A custom assertion message.
*/
protected function assertNoRaw($value, $message = NULL) {
if (!isset($message)) {
$message = String::format("Raw value @value not found.", array(
'@value' => var_export($value, TRUE),
));
}
$this->assert(strpos($this->content, $value) === FALSE, $message);
}
}
......@@ -29,13 +29,6 @@ class TextPlainUnitTest extends DrupalUnitTestBase {
*/
public static $modules = array('entity', 'field', 'text', 'entity_test', 'system', 'filter', 'user');
/**
* Contains rendered content.
*
* @var string
*/
protected $content;
public static function getInfo() {
return array(
'name' => 'Text field text_plain formatter',
......@@ -123,8 +116,8 @@ protected function createEntity($values = array()) {
*/
protected function renderEntityFields(ContentEntityInterface $entity, EntityViewDisplayInterface $display) {
$content = $display->build($entity);
$this->content = drupal_render($content);
return $this->content;
$content = $this->render($content);
return $content;
}
/**
......
......@@ -7,7 +7,6 @@
namespace Drupal\views\Tests\Plugin;
use Drupal\Component\Utility\Xss;
use Drupal\views\Views;
use Drupal\views\Tests\ViewUnitTestBase;
......@@ -32,13 +31,6 @@ class RowEntityTest extends ViewUnitTestBase {
*/
public static $testViews = array('test_entity_row');
/**
* A string for assert raw and text helper methods.
*
* @var string
*/
protected $content;
public static function getInfo() {
return array(
'name' => 'Row: Entity',
......@@ -68,8 +60,7 @@ public function testEntityRow() {
$term->save();
$view = Views::getView('test_entity_row');
$this->content = $view->preview();
$this->content = drupal_render($this->content);
$this->render($view->preview());
$this->assertText($term->getName(), 'The rendered entity appears as row in the view.');
......@@ -82,28 +73,4 @@ public function testEntityRow() {
$this->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available');
}
/**
* Pass if the text is found in set string.
*
* @param string $text
* Text to look for.
* @param string $message
* (optional) A message to display with the assertion. Do not translate
* messages: use format_string() to embed variables in the message text, not
* t(). If left blank, a default message will be displayed.
* @param string $group
* (optional) The group this message is in, which is displayed in a column
* in test output. Use 'Debug' to indicate this is debugging output. Do not
* translate this string. Defaults to 'Other'; most tests do not override
* this default.
*
* @return bool TRUE on pass, FALSE on fail.
*/
protected function assertText($text, $message = '', $group = 'Other') {
if (!$message) {
$message = t('Raw "@raw" found', array('@raw' => $text));
}
return $this->assert(strpos(Xss::filter($this->content, array()), $text) !== FALSE, $message, $group);
}
}
......@@ -83,7 +83,7 @@ protected function assertGrid(ViewExecutable $view, $alignment, $columns) {
$this->executeView($view);
$output = $view->preview();
$output = drupal_render($output);
$this->drupalSetContent($output, 'internal://test-grid');
$this->drupalSetContent($output);
if (!in_array($alignment, $this->alignmentsTested)) {
$result = $this->xpath('//div[contains(@class, "views-view-grid") and contains(@class, :alignment) and contains(@class, :columns)]', array(':alignment' => $alignment, ':columns' => 'cols-' . $columns));
$this->assertTrue(count($result), ucfirst($alignment) . " grid markup detected.");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment