Updated to Drupal 8.5. Core Media not yet in use.
[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     if (method_exists($this, 'expectException')) {
63       $this->expectException(\InvalidArgumentException::class);
64       $this->expectExceptionMessage('Required prefix configuration is missing');
65     }
66     else {
67       $this->setExpectedException(\InvalidArgumentException::class, 'Required prefix configuration is missing');
68     }
69     FileCacheFactory::get('test_foo_settings', []);
70   }
71
72   /**
73    * @covers ::get
74    */
75   public function testGetDisabledFileCache() {
76     // Ensure the returned FileCache is an instance of FileCache::class.
77     $file_cache = FileCacheFactory::get('test_foo_settings', []);
78     $this->assertInstanceOf(FileCache::class, $file_cache);
79
80     $configuration = FileCacheFactory::getConfiguration();
81     $configuration[FileCacheFactory::DISABLE_CACHE] = TRUE;
82     FileCacheFactory::setConfiguration($configuration);
83
84     // Ensure the returned FileCache is now an instance of NullFileCache::class.
85     $file_cache = FileCacheFactory::get('test_foo_settings', []);
86     $this->assertInstanceOf(NullFileCache::class, $file_cache);
87   }
88
89   /**
90    * @covers ::get
91    *
92    * @dataProvider configurationDataProvider
93    */
94   public function testGetConfigurationOverrides($configuration, $arguments, $class) {
95     FileCacheFactory::setConfiguration($configuration);
96
97     $file_cache = FileCacheFactory::get('test_foo_settings', $arguments);
98     $this->assertInstanceOf($class, $file_cache);
99   }
100
101   /**
102    * Data provider for testGetConfigurationOverrides().
103    */
104   public function configurationDataProvider() {
105     $data = [];
106
107     // Get a unique FileCache class.
108     $file_cache = $this->getMockBuilder(FileCache::class)
109       ->disableOriginalConstructor()
110       ->getMock();
111     $class = get_class($file_cache);
112
113     // Test fallback configuration.
114     $data['fallback-configuration'] = [
115       [],
116       [],
117       FileCache::class,
118     ];
119
120     // Test default configuration.
121     $data['default-configuration'] = [
122       ['default' => ['class' => $class]],
123       [],
124       $class,
125     ];
126
127     // Test specific per collection setting.
128     $data['collection-setting'] = [
129       ['test_foo_settings' => ['class' => $class]],
130       [],
131       $class,
132     ];
133
134     // Test default configuration plus specific per collection setting.
135     $data['default-plus-collection-setting'] = [
136       [
137         'default' => ['class' => '\stdClass'],
138         'test_foo_settings' => ['class' => $class],
139       ],
140       [],
141       $class,
142     ];
143
144     // Test default configuration plus class specific override.
145     $data['default-plus-class-override'] = [
146       ['default' => ['class' => '\stdClass']],
147       ['class' => $class],
148       $class,
149     ];
150
151     // Test default configuration plus class specific override plus specific
152     // per collection setting.
153     $data['default-plus-class-plus-collection-setting'] = [
154       [
155         'default' => ['class' => '\stdClass'],
156         'test_foo_settings' => ['class' => $class],
157       ],
158       ['class' => '\stdClass'],
159       $class,
160   ];
161
162     return $data;
163   }
164
165   /**
166    * @covers ::getConfiguration
167    * @covers ::setConfiguration
168    */
169   public function testGetSetConfiguration() {
170     $configuration = FileCacheFactory::getConfiguration();
171     $configuration['test_foo_bar'] = ['bar' => 'llama'];
172     FileCacheFactory::setConfiguration($configuration);
173     $configuration = FileCacheFactory::getConfiguration();
174     $this->assertEquals(['bar' => 'llama'], $configuration['test_foo_bar']);
175   }
176
177   /**
178    * @covers ::getPrefix
179    * @covers ::setPrefix
180    */
181   public function testGetSetPrefix() {
182     // Random generator.
183     $random = new Random();
184
185     $prefix = $random->name(8, TRUE);
186     FileCacheFactory::setPrefix($prefix);
187     $this->assertEquals($prefix, FileCacheFactory::getPrefix());
188   }
189
190 }