HtmlOutputPrinterTrait.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Drupal\Tests\Listeners;
  3. use Drupal\Component\Utility\Html;
  4. /**
  5. * Defines a class for providing html output results for functional tests.
  6. *
  7. * @internal
  8. */
  9. trait HtmlOutputPrinterTrait {
  10. /**
  11. * File to write html links to.
  12. *
  13. * @var string
  14. */
  15. protected $browserOutputFile;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function __construct($out = NULL, $verbose = FALSE, $colors = self::COLOR_DEFAULT, $debug = FALSE, $numberOfColumns = 80, $reverse = FALSE) {
  20. parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns, $reverse);
  21. $this->setUpHtmlOutput();
  22. }
  23. /**
  24. * Creates the file to list the HTML output created during the test.
  25. *
  26. * @see \Drupal\Tests\BrowserTestBase::initBrowserOutputFile()
  27. */
  28. protected function setUpHtmlOutput() {
  29. if ($html_output_directory = getenv('BROWSERTEST_OUTPUT_DIRECTORY')) {
  30. // Initialize html output debugging.
  31. $html_output_directory = rtrim($html_output_directory, '/');
  32. // Check if directory exists.
  33. if (!is_dir($html_output_directory) || !is_writable($html_output_directory)) {
  34. $this->writeWithColor('bg-red, fg-black', "HTML output directory $html_output_directory is not a writable directory.");
  35. }
  36. else {
  37. // Convert to a canonicalized absolute pathname just in case the current
  38. // working directory is changed.
  39. $html_output_directory = realpath($html_output_directory);
  40. $this->browserOutputFile = tempnam($html_output_directory, 'browser_output_');
  41. if ($this->browserOutputFile) {
  42. touch($this->browserOutputFile);
  43. }
  44. else {
  45. $this->writeWithColor('bg-red, fg-black', "Unable to create a temporary file in $html_output_directory.");
  46. }
  47. }
  48. }
  49. if ($this->browserOutputFile) {
  50. putenv('BROWSERTEST_OUTPUT_FILE=' . $this->browserOutputFile);
  51. }
  52. else {
  53. // Remove any environment variable.
  54. putenv('BROWSERTEST_OUTPUT_FILE');
  55. }
  56. }
  57. /**
  58. * Prints the list of HTML output generated during the test.
  59. */
  60. protected function printHtmlOutput() {
  61. if ($this->browserOutputFile) {
  62. $contents = file_get_contents($this->browserOutputFile);
  63. if ($contents) {
  64. $this->writeNewLine();
  65. $this->writeWithColor('bg-yellow, fg-black', 'HTML output was generated');
  66. $this->write($contents);
  67. }
  68. // No need to keep the file around any more.
  69. unlink($this->browserOutputFile);
  70. }
  71. }
  72. /**
  73. * Prints HTML output links for the Simpletest UI.
  74. */
  75. public function simpletestUiWrite($buffer) {
  76. $buffer = Html::escape($buffer);
  77. // Turn HTML output URLs into clickable link <a> tags.
  78. $url_pattern = '@https?://[^\s]+@';
  79. $buffer = preg_replace($url_pattern, '<a href="$0" target="_blank" title="$0">$0</a>', $buffer);
  80. // Make the output readable in HTML by breaking up lines properly.
  81. $buffer = nl2br($buffer);
  82. print $buffer;
  83. }
  84. }