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