1dd67eb9e9a3e5367594d3049c3865ad2d1e3353
[yaffs-website] / web / core / tests / Drupal / Tests / Listeners / HtmlOutputPrinterTrait.php
1 <?php
2
3 namespace Drupal\Tests\Listeners;
4
5 /**
6  * Defines a class for providing html output results for functional tests.
7  *
8  * @internal
9  */
10 trait HtmlOutputPrinterTrait {
11
12   /**
13    * File to write html links to.
14    *
15    * @var string
16    */
17   protected $browserOutputFile;
18
19   /**
20    * Creates the file to list the HTML output created during the test.
21    *
22    * @see \Drupal\Tests\BrowserTestBase::initBrowserOutputFile()
23    */
24   protected function setUpHtmlOutput() {
25     if ($html_output_directory = getenv('BROWSERTEST_OUTPUT_DIRECTORY')) {
26       // Initialize html output debugging.
27       $html_output_directory = rtrim($html_output_directory, '/');
28
29       // Check if directory exists.
30       if (!is_dir($html_output_directory) || !is_writable($html_output_directory)) {
31         $this->writeWithColor('bg-red, fg-black', "HTML output directory $html_output_directory is not a writable directory.");
32       }
33       else {
34         // Convert to a canonicalized absolute pathname just in case the current
35         // working directory is changed.
36         $html_output_directory = realpath($html_output_directory);
37         $this->browserOutputFile = tempnam($html_output_directory, 'browser_output_');
38         if ($this->browserOutputFile) {
39           touch($this->browserOutputFile);
40         }
41         else {
42           $this->writeWithColor('bg-red, fg-black', "Unable to create a temporary file in $html_output_directory.");
43         }
44       }
45     }
46
47     if ($this->browserOutputFile) {
48       putenv('BROWSERTEST_OUTPUT_FILE=' . $this->browserOutputFile);
49     }
50     else {
51       // Remove any environment variable.
52       putenv('BROWSERTEST_OUTPUT_FILE');
53     }
54   }
55
56   /**
57    * Prints the list of HTML output generated during the test.
58    */
59   protected function printHtmlOutput() {
60     if ($this->browserOutputFile) {
61       $contents = file_get_contents($this->browserOutputFile);
62       if ($contents) {
63         $this->writeNewLine();
64         $this->writeWithColor('bg-yellow, fg-black', 'HTML output was generated');
65         $this->write($contents);
66       }
67       // No need to keep the file around any more.
68       unlink($this->browserOutputFile);
69     }
70   }
71
72 }