Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Render / FormattableMarkupTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Render;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * Tests the TranslatableMarkup class.
10  *
11  * @coversDefaultClass \Drupal\Component\Render\FormattableMarkup
12  * @group utility
13  */
14 class FormattableMarkupTest extends TestCase {
15
16   /**
17    * The error message of the last error in the error handler.
18    *
19    * @var string
20    */
21   protected $lastErrorMessage;
22
23   /**
24    * The error number of the last error in the error handler.
25    *
26    * @var int
27    */
28   protected $lastErrorNumber;
29
30   /**
31    * @covers ::__toString
32    * @covers ::jsonSerialize
33    */
34   public function testToString() {
35     $string = 'Can I please have a @replacement';
36     $formattable_string = new FormattableMarkup($string, ['@replacement' => 'kitten']);
37     $text = (string) $formattable_string;
38     $this->assertEquals('Can I please have a kitten', $text);
39     $text = $formattable_string->jsonSerialize();
40     $this->assertEquals('Can I please have a kitten', $text);
41   }
42
43   /**
44    * @covers ::count
45    */
46   public function testCount() {
47     $string = 'Can I please have a @replacement';
48     $formattable_string = new FormattableMarkup($string, ['@replacement' => 'kitten']);
49     $this->assertEquals(strlen($string), $formattable_string->count());
50   }
51
52   /**
53    * Custom error handler that saves the last error.
54    *
55    * We need this custom error handler because we cannot rely on the error to
56    * exception conversion as __toString is never allowed to leak any kind of
57    * exception.
58    *
59    * @param int $error_number
60    *   The error number.
61    * @param string $error_message
62    *   The error message.
63    */
64   public function errorHandler($error_number, $error_message) {
65     $this->lastErrorNumber = $error_number;
66     $this->lastErrorMessage = $error_message;
67   }
68
69   /**
70    * @covers ::__toString
71    * @dataProvider providerTestUnexpectedPlaceholder
72    */
73   public function testUnexpectedPlaceholder($string, $arguments, $error_number, $error_message) {
74     // We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487
75     set_error_handler([$this, 'errorHandler']);
76     // We want this to trigger an error.
77     $markup = new FormattableMarkup($string, $arguments);
78     // Cast it to a string which will generate the errors.
79     $output = (string) $markup;
80     restore_error_handler();
81     // The string should not change.
82     $this->assertEquals($string, $output);
83     $this->assertEquals($error_number, $this->lastErrorNumber);
84     $this->assertEquals($error_message, $this->lastErrorMessage);
85   }
86
87   /**
88    * Data provider for FormattableMarkupTest::testUnexpectedPlaceholder().
89    *
90    * @return array
91    */
92   public function providerTestUnexpectedPlaceholder() {
93     return [
94       ['Non alpha starting character: ~placeholder', ['~placeholder' => 'replaced'], E_USER_ERROR, 'Invalid placeholder (~placeholder) in string: Non alpha starting character: ~placeholder'],
95       ['Alpha starting character: placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: Alpha starting character: placeholder'],
96       // Ensure that where the placeholder is located in the the string is
97       // irrelevant.
98       ['placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: placeholder'],
99     ];
100   }
101
102 }