Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Utility / VariableTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Component\Utility\VariableTest.
6  */
7
8 namespace Drupal\Tests\Component\Utility;
9
10 use Drupal\Component\Utility\Variable;
11 use PHPUnit\Framework\TestCase;
12
13 /**
14  * Test variable export functionality in Variable component.
15  *
16  * @group Variable
17  * @group Utility
18  *
19  * @coversDefaultClass \Drupal\Component\Utility\Variable
20  */
21 class VariableTest extends TestCase {
22
23   /**
24    * Data provider for testExport().
25    *
26    * @return array
27    *   An array containing:
28    *     - The expected export string.
29    *     - The variable to export.
30    */
31   public function providerTestExport() {
32     return [
33       // Array.
34       [
35         'array()',
36         [],
37       ],
38       [
39         // non-associative.
40         "array(\n  1,\n  2,\n  3,\n  4,\n)",
41         [1, 2, 3, 4],
42       ],
43       [
44         // associative.
45         "array(\n  'a' => 1,\n)",
46         ['a' => 1],
47       ],
48       // Bool.
49       [
50         'TRUE',
51         TRUE,
52       ],
53       [
54         'FALSE',
55         FALSE,
56       ],
57       // Strings.
58       [
59         "'string'",
60         'string',
61       ],
62       [
63         '"\n\r\t"',
64         "\n\r\t",
65       ],
66       [
67         // 2 backslashes. \\
68         "'\\'",
69         '\\',
70       ],
71       [
72         // Double-quote "
73         "'\"'",
74         "\"",
75       ],
76       [
77         // Single-quote '
78         '"\'"',
79         "'",
80       ],
81       [
82         // Quotes with $ symbols.
83         '"\$settings[\'foo\']"',
84         '$settings[\'foo\']',
85       ],
86       // Object.
87       [
88         // A stdClass object.
89         '(object) array()',
90         new \stdClass(),
91       ],
92       [
93         // A not-stdClass object.
94         "Drupal\Tests\Component\Utility\StubVariableTestClass::__set_state(array(\n))",
95         new StubVariableTestClass(),
96       ],
97     ];
98   }
99
100   /**
101    * Tests exporting variables.
102    *
103    * @dataProvider providerTestExport
104    * @covers ::export
105    *
106    * @param string $expected
107    *   The expected exported variable.
108    * @param mixed $variable
109    *   The variable to be exported.
110    */
111   public function testExport($expected, $variable) {
112     $this->assertEquals($expected, Variable::export($variable));
113   }
114
115 }
116
117 /**
118  * No-op test class for VariableTest::testExport().
119  *
120  * @see Drupal\Tests\Component\Utility\VariableTest::testExport()
121  * @see Drupal\Tests\Component\Utility\VariableTest::providerTestExport()
122  */
123 class StubVariableTestClass {
124
125 }