Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / FileCache / FileCacheFactoryTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\FileCache;
4
5 use Drupal\Component\FileCache\FileCache;
6 use Drupal\Component\FileCache\NullFileCache;
7 use Drupal\Component\FileCache\FileCacheFactory;
8 use Drupal\Component\Utility\Random;
9 use PHPUnit\Framework\TestCase;
10
11 /**
12  * @coversDefaultClass \Drupal\Component\FileCache\FileCacheFactory
13  * @group FileCache
14  */
15 class FileCacheFactoryTest extends TestCase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function setUp() {
21     parent::setUp();
22
23     $configuration = [
24       'test_foo_settings' => [
25         'collection' => 'test-23',
26         'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
27         'cache_backend_configuration' => [
28           'bin' => 'dog',
29         ],
30       ],
31     ];
32     FileCacheFactory::setConfiguration($configuration);
33     FileCacheFactory::setPrefix('prefix');
34   }
35
36   /**
37    * @covers ::get
38    */
39   public function testGet() {
40     $file_cache = FileCacheFactory::get('test_foo_settings', []);
41
42     // Ensure the right backend and configuration is used.
43     $filename = __DIR__ . '/Fixtures/llama-23.txt';
44     $realpath = realpath($filename);
45     $cid = 'prefix:test-23:' . $realpath;
46
47     $file_cache->set($filename, 23);
48
49     $static_cache = new StaticFileCacheBackend(['bin' => 'dog']);
50     $result = $static_cache->fetch([$cid]);
51     $this->assertNotEmpty($result);
52
53     // Cleanup static caches.
54     $file_cache->delete($filename);
55   }
56
57   /**
58    * @covers ::get
59    */
60   public function testGetNoPrefix() {
61     FileCacheFactory::setPrefix(NULL);
62     $this->setExpectedException(\InvalidArgumentException::class, 'Required prefix configuration is missing');
63     FileCacheFactory::get('test_foo_settings', []);
64   }
65
66   /**
67    * @covers ::get
68    */
69   public function testGetDisabledFileCache() {
70     // Ensure the returned FileCache is an instance of FileCache::class.
71     $file_cache = FileCacheFactory::get('test_foo_settings', []);
72     $this->assertInstanceOf(FileCache::class, $file_cache);
73
74     $configuration = FileCacheFactory::getConfiguration();
75     $configuration[FileCacheFactory::DISABLE_CACHE] = TRUE;
76     FileCacheFactory::setConfiguration($configuration);
77
78     // Ensure the returned FileCache is now an instance of NullFileCache::class.
79     $file_cache = FileCacheFactory::get('test_foo_settings', []);
80     $this->assertInstanceOf(NullFileCache::class, $file_cache);
81   }
82
83   /**
84    * @covers ::get
85    *
86    * @dataProvider configurationDataProvider
87    */
88   public function testGetConfigurationOverrides($configuration, $arguments, $class) {
89     FileCacheFactory::setConfiguration($configuration);
90
91     $file_cache = FileCacheFactory::get('test_foo_settings', $arguments);
92     $this->assertInstanceOf($class, $file_cache);
93   }
94
95   /**
96    * Data provider for testGetConfigurationOverrides().
97    */
98   public function configurationDataProvider() {
99     $data = [];
100
101     // Get a unique FileCache class.
102     $file_cache = $this->getMockBuilder(FileCache::class)
103       ->disableOriginalConstructor()
104       ->getMock();
105     $class = get_class($file_cache);
106
107     // Test fallback configuration.
108     $data['fallback-configuration'] = [
109       [],
110       [],
111       FileCache::class,
112     ];
113
114     // Test default configuration.
115     $data['default-configuration'] = [
116       ['default' => ['class' => $class]],
117       [],
118       $class,
119     ];
120
121     // Test specific per collection setting.
122     $data['collection-setting'] = [
123       ['test_foo_settings' => ['class' => $class]],
124       [],
125       $class,
126     ];
127
128     // Test default configuration plus specific per collection setting.
129     $data['default-plus-collection-setting'] = [
130       [
131         'default' => ['class' => '\stdClass'],
132         'test_foo_settings' => ['class' => $class],
133       ],
134       [],
135       $class,
136     ];
137
138     // Test default configuration plus class specific override.
139     $data['default-plus-class-override'] = [
140       ['default' => ['class' => '\stdClass']],
141       ['class' => $class],
142       $class,
143     ];
144
145     // Test default configuration plus class specific override plus specific
146     // per collection setting.
147     $data['default-plus-class-plus-collection-setting'] = [
148       [
149         'default' => ['class' => '\stdClass'],
150         'test_foo_settings' => ['class' => $class],
151       ],
152       ['class' => '\stdClass'],
153       $class,
154   ];
155
156     return $data;
157   }
158
159   /**
160    * @covers ::getConfiguration
161    * @covers ::setConfiguration
162    */
163   public function testGetSetConfiguration() {
164     $configuration = FileCacheFactory::getConfiguration();
165     $configuration['test_foo_bar'] = ['bar' => 'llama'];
166     FileCacheFactory::setConfiguration($configuration);
167     $configuration = FileCacheFactory::getConfiguration();
168     $this->assertEquals(['bar' => 'llama'], $configuration['test_foo_bar']);
169   }
170
171   /**
172    * @covers ::getPrefix
173    * @covers ::setPrefix
174    */
175   public function testGetSetPrefix() {
176     // Random generator.
177     $random = new Random();
178
179     $prefix = $random->name(8, TRUE);
180     FileCacheFactory::setPrefix($prefix);
181     $this->assertEquals($prefix, FileCacheFactory::getPrefix());
182   }
183
184 }