diff --git a/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php b/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php index bcc6dc8..919603d 100644 --- a/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php +++ b/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php @@ -1,10 +1,5 @@ [ - 'default' => [ - 'modules' => [ - 'module_a' => [ - 'module_a.info.yml' => $info_a + [ + 'sites' => [ + 'default' => [ + 'modules' => [ + 'module_a' => [ + 'module_a.info.yml' => $info_a + ], ], - ] - ] + ], + ], ], ]; } /** - * Check that the cache is loaded and used. + * Check that the cache uses different keys for different root directories. * * @covers ::scan * * @dataProvider providerModule */ - public function testScanCache($filesystem) { - vfsStream::setup('test_a'); - vfsStream::create($filesystem); - - $expected = ['test_scanned_directory']; - - // Set up a mocked ExtensionDiscovery object so we can mock the - // scanDirectory() method. - $mock_extension_discovery = $this->getMockBuilder(ExtensionDiscovery::class) - ->setConstructorArgs([vfsStream::url('test_a'), TRUE, ['no/profile'], 'sites/default']) - ->setMethods(['scanDirectory']) - ->getMock(); - // The scanDirectory() method gets called 4 times while traversing the file - // system once, one each for ORIGIN_CORE, ORIGIN_SITE_ALL, etc. - $mock_extension_discovery->expects($this->exactly(4)) - ->method('scanDirectory') - ->willReturn($expected); - // Use up our 4 calls to scanDirectory(). - $mock_extension_discovery->scan('module', FALSE); - - // Subsequent scans should not call scanDirectory(). - $mock_extension_discovery->expects($this->exactly(0)) - ->method('scanDirectory') - ->willReturn($expected); - $mock_extension_discovery->scan('module', FALSE); - } - - /** - * Check that the cache is invalidated when needed. - * - * @covers ::scan - * - * @dataProvider providerModule - */ - public function testScanCacheInvalidate($filesystem) { + public function testScanCacheKeys($filesystem) { vfsStream::setup('root'); // Create the same filesystem in two different directories. vfsStream::create($filesystem, vfsStream::newDirectory('test1')); vfsStream::create($filesystem, vfsStream::newDirectory('test2')); - $expected = ['test_scanned_directory']; - - // Set up a mocked ExtensionDiscovery object so we can mock the - // scanDirectory() method. We'll use the test1 directory first. - $test1_mock_extension_discovery = $this->getMockBuilder(ExtensionDiscovery::class) - ->setConstructorArgs([vfsStream::url('root/test1'), TRUE, ['no/profile'], 'sites/default']) - ->setMethods(['scanDirectory']) - ->getMock(); - // The scanDirectory() method gets called 4 times while traversing the file - // system once, one each for ORIGIN_CORE, ORIGIN_SITE_ALL, etc. - $test1_mock_extension_discovery->expects($this->exactly(4)) - ->method('scanDirectory') - ->willReturn($expected); - - // Use up our 4 calls to scanDirectory(). - $test1_mock_extension_discovery->scan('module', FALSE); - - // Verify that when we mock ExtensionDiscovery again, the static is still - // present. - $test1_mock_extension_discovery = $this->getMockBuilder(ExtensionDiscovery::class) - ->setConstructorArgs([vfsStream::url('root/test1'), TRUE, ['no/profile'], 'sites/default']) - ->setMethods(['scanDirectory']) - ->getMock(); - // scanDirectory() should be called 0 times if the cache is still there. - $test1_mock_extension_discovery->expects($this->exactly(0)) - ->method('scanDirectory') - ->willReturn($expected); - $test1_mock_extension_discovery->scan('module', FALSE); - - // Now use test2 in order to check that the cache was invalidated. - $test2_mock_extension_discovery = $this->getMockBuilder(ExtensionDiscovery::class) - ->setConstructorArgs([vfsStream::url('root/test2'), TRUE, ['no/profile'], 'sites/default']) - ->setMethods(['scanDirectory']) - ->getMock(); - $test2_mock_extension_discovery->expects($this->exactly(4)) - ->method('scanDirectory') - ->willReturn($expected); - $test2_mock_extension_discovery->scan('module', FALSE); + $root_1 = vfsStream::url('root/test1'); + $root_2 = vfsStream::url('root/test2'); + + // Create an ExtensionDiscovery with $root_1. + $extension_discovery = new ExtensionDiscovery($root_1, TRUE, ['no/profile'], 'sites/default'); + + // Assert that the cache does not contain either key we're looking for until + // after we've performed a scan. + $this->assertArrayNotHasKey($root_1, $this->getDiscoveredCache($extension_discovery)); + $this->assertArrayNotHasKey($root_2, $this->getDiscoveredCache($extension_discovery)); + + // Perform the scan. + $extension_discovery->scan('module'); + + // Assert that we have $root_1 as a key, but not $root_2 yet. + $this->assertArrayHasKey($root_1, $this->getDiscoveredCache($extension_discovery)); + $this->assertArrayNotHasKey($root_2, $this->getDiscoveredCache($extension_discovery)); + + // Re-instantiate ExtensionDiscovery with a different root. + $extension_discovery = new ExtensionDiscovery($root_2, TRUE, ['no/profile'], 'sites/default'); + + // Since the cache is static, it will still only have $root_1. + $this->assertArrayHasKey($root_1, $this->getDiscoveredCache($extension_discovery)); + $this->assertArrayNotHasKey($root_2, $this->getDiscoveredCache($extension_discovery)); + + // Perform the scan. + $extension_discovery->scan('module'); + + // Now we have both keys in the cache array. + $this->assertArrayHasKey($root_1, $this->getDiscoveredCache($extension_discovery)); + $this->assertArrayHasKey($root_2, $this->getDiscoveredCache($extension_discovery)); + } + + /** + * Helper method to get the cache array from an ExtensionDiscovery object. + * + * @param ExtensionDiscovery $extension_discovery + * An ExtensionDiscovery object. + * + * @return array[] + * The files array. + */ + protected function getDiscoveredCache(ExtensionDiscovery $extension_discovery) { + $ref_cache = new \ReflectionProperty(ExtensionDiscovery::class, 'files'); + $ref_cache->setAccessible(TRUE); + return $ref_cache->getValue($extension_discovery); } }