Chris@0: findPhpClasses($component_path) as $class) { Chris@0: $this->assertNoCoreUsage($class); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that classes in Component Tests do not use any Core class. Chris@0: */ Chris@0: public function testNoCoreInComponentTests() { Chris@0: $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/tests/Drupal/Tests/Component'; Chris@0: foreach ($this->findPhpClasses($component_path) as $class) { Chris@0: $this->assertNoCoreUsage($class); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@14: * Tests LICENSE.txt is present and has the correct content. Chris@14: * Chris@14: * @param $component_path Chris@14: * The path to the component. Chris@14: * @dataProvider \Drupal\Tests\Component\DrupalComponentTest::getComponents Chris@14: */ Chris@14: public function testComponentLicence($component_path) { Chris@14: $this->assertFileExists($component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt'); Chris@14: $this->assertSame('e84dac1d9fbb5a4a69e38654ce644cea769aa76b', hash_file('sha1', $component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt')); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Data provider. Chris@14: * Chris@14: * @return array Chris@14: */ Chris@14: public function getComponents() { Chris@14: $root_component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component'; Chris@14: $component_paths = []; Chris@14: foreach (new \DirectoryIterator($root_component_path) as $file) { Chris@14: if ($file->isDir() && !$file->isDot()) { Chris@14: $component_paths[$file->getBasename()] = [$file->getPathname()]; Chris@14: } Chris@14: } Chris@14: return $component_paths; Chris@14: } Chris@14: Chris@14: /** Chris@0: * Searches a directory recursively for PHP classes. Chris@0: * Chris@0: * @param string $dir Chris@0: * The full path to the directory that should be checked. Chris@0: * Chris@0: * @return array Chris@0: * An array of class paths. Chris@0: */ Chris@0: protected function findPhpClasses($dir) { Chris@0: $classes = []; Chris@0: foreach (new \DirectoryIterator($dir) as $file) { Chris@0: if ($file->isDir() && !$file->isDot()) { Chris@0: $classes = array_merge($classes, $this->findPhpClasses($file->getPathname())); Chris@0: } Chris@0: elseif ($file->getExtension() == 'php') { Chris@0: $classes[] = $file->getPathname(); Chris@0: } Chris@0: } Chris@0: Chris@0: return $classes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that the given class is not using any class from Core namespace. Chris@0: * Chris@0: * @param string $class_path Chris@0: * The full path to the class that should be checked. Chris@0: */ Chris@0: protected function assertNoCoreUsage($class_path) { Chris@0: $contents = file_get_contents($class_path); Chris@0: preg_match_all('/^.*Drupal\\\Core.*$/m', $contents, $matches); Chris@0: $matches = array_filter($matches[0], function ($line) { Chris@0: // Filter references to @see as they don't really matter. Chris@0: return strpos($line, '@see') === FALSE; Chris@0: }); Chris@0: $this->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in $class_path"); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Data provider for testAssertNoCoreUseage(). Chris@0: * Chris@0: * @return array Chris@0: * Data for testAssertNoCoreUseage() in the form: Chris@0: * - TRUE if the test passes, FALSE otherwise. Chris@0: * - File data as a string. This will be used as a virtual file. Chris@0: */ Chris@0: public function providerAssertNoCoreUseage() { Chris@0: return [ Chris@0: [ Chris@0: TRUE, Chris@0: '@see \\Drupal\\Core\\Something', Chris@0: ], Chris@0: [ Chris@0: FALSE, Chris@0: '\\Drupal\\Core\\Something', Chris@0: ], Chris@0: [ Chris@0: FALSE, Chris@0: "@see \\Drupal\\Core\\Something\n" . Chris@0: '\\Drupal\\Core\\Something', Chris@0: ], Chris@0: [ Chris@0: FALSE, Chris@0: "\\Drupal\\Core\\Something\n" . Chris@0: '@see \\Drupal\\Core\\Something', Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers \Drupal\Tests\Component\DrupalComponentTest::assertNoCoreUsage Chris@0: * @dataProvider providerAssertNoCoreUseage Chris@0: */ Chris@0: public function testAssertNoCoreUseage($expected_pass, $file_data) { Chris@0: // Set up a virtual file to read. Chris@0: $vfs_root = vfsStream::setup('root'); Chris@0: vfsStream::newFile('Test.php')->at($vfs_root)->setContent($file_data); Chris@0: $file_uri = vfsStream::url('root/Test.php'); Chris@0: Chris@0: try { Chris@0: $pass = TRUE; Chris@0: $this->assertNoCoreUsage($file_uri); Chris@0: } Chris@0: catch (\PHPUnit_Framework_AssertionFailedError $e) { Chris@0: $pass = FALSE; Chris@0: } Chris@0: $this->assertEquals($expected_pass, $pass, $expected_pass ? Chris@0: 'Test caused a false positive' : Chris@0: 'Test failed to detect Core usage'); Chris@0: } Chris@0: Chris@0: }