HtmlOutputPrinterTrait.php in Drupal 9        
                          
                  
                        
  
  
  
  
File
  core/tests/Drupal/Tests/Listeners/HtmlOutputPrinterTrait.php
  
    View source  
  <?php
namespace Drupal\Tests\Listeners;
use Drupal\Component\Utility\Html;
trait HtmlOutputPrinterTrait {
  
  protected $browserOutputFile;
  
  public function __construct($out = NULL, $verbose = FALSE, $colors = self::COLOR_DEFAULT, $debug = FALSE, $numberOfColumns = 80, $reverse = FALSE) {
    parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns, $reverse);
    $this
      ->setUpHtmlOutput();
  }
  
  protected function setUpHtmlOutput() {
    if ($html_output_directory = getenv('BROWSERTEST_OUTPUT_DIRECTORY')) {
      
      $html_output_directory = rtrim($html_output_directory, '/');
      
      if (!is_dir($html_output_directory) || !is_writable($html_output_directory)) {
        $this
          ->writeWithColor('bg-red, fg-black', "HTML output directory {$html_output_directory} is not a writable directory.");
      }
      else {
        
        $html_output_directory = realpath($html_output_directory);
        $this->browserOutputFile = tempnam($html_output_directory, 'browser_output_');
        if ($this->browserOutputFile) {
          touch($this->browserOutputFile);
        }
        else {
          $this
            ->writeWithColor('bg-red, fg-black', "Unable to create a temporary file in {$html_output_directory}.");
        }
      }
    }
    if ($this->browserOutputFile) {
      putenv('BROWSERTEST_OUTPUT_FILE=' . $this->browserOutputFile);
    }
    else {
      
      putenv('BROWSERTEST_OUTPUT_FILE');
    }
  }
  
  protected function printHtmlOutput() {
    if ($this->browserOutputFile) {
      $contents = file_get_contents($this->browserOutputFile);
      if ($contents) {
        $this
          ->writeNewLine();
        $this
          ->writeWithColor('bg-yellow, fg-black', 'HTML output was generated');
        $this
          ->write($contents);
      }
      
      unlink($this->browserOutputFile);
    }
  }
  
  public function simpletestUiWrite($buffer) {
    $buffer = Html::escape($buffer);
    
    $url_pattern = '@https?://[^\\s]+@';
    $buffer = preg_replace($url_pattern, '<a href="$0" target="_blank" title="$0">$0</a>', $buffer);
    
    $buffer = nl2br($buffer);
    print $buffer;
  }
}