View source
<?php
namespace Drupal\Tests\Component;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
class DrupalComponentTest extends TestCase {
public function testNoCoreInComponent() {
$component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
foreach ($this
->findPhpClasses($component_path) as $class) {
$this
->assertNoCoreUsage($class);
}
}
public function testNoCoreInComponentTests() {
$component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/tests/Drupal/Tests/Component';
foreach ($this
->findPhpClasses($component_path) as $class) {
$this
->assertNoCoreUsage($class);
}
}
public function testComponentLicence($component_path) {
$this
->assertFileExists($component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt');
$this
->assertSame('e84dac1d9fbb5a4a69e38654ce644cea769aa76b', hash_file('sha1', $component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt'));
}
public function getComponents() {
$root_component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
$component_paths = [];
foreach (new \DirectoryIterator($root_component_path) as $file) {
if ($file
->isDir() && !$file
->isDot()) {
$component_paths[$file
->getBasename()] = [
$file
->getPathname(),
];
}
}
return $component_paths;
}
protected function findPhpClasses($dir) {
$classes = [];
foreach (new \DirectoryIterator($dir) as $file) {
if ($file
->isDir() && !$file
->isDot()) {
$classes = array_merge($classes, $this
->findPhpClasses($file
->getPathname()));
}
elseif ($file
->getExtension() == 'php') {
$classes[] = $file
->getPathname();
}
}
return $classes;
}
protected function assertNoCoreUsage($class_path) {
$contents = file_get_contents($class_path);
preg_match_all('/^.*Drupal\\\\Core.*$/m', $contents, $matches);
$matches = array_filter($matches[0], function ($line) {
return strpos($line, '@see') === FALSE;
});
$this
->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in {$class_path}");
}
public function providerAssertNoCoreUsage() {
return [
[
TRUE,
'@see \\Drupal\\Core\\Something',
],
[
FALSE,
'\\Drupal\\Core\\Something',
],
[
FALSE,
"@see \\Drupal\\Core\\Something\n" . '\\Drupal\\Core\\Something',
],
[
FALSE,
"\\Drupal\\Core\\Something\n" . '@see \\Drupal\\Core\\Something',
],
];
}
public function testAssertNoCoreUsage($expected_pass, $file_data) {
$vfs_root = vfsStream::setup('root');
vfsStream::newFile('Test.php')
->at($vfs_root)
->setContent($file_data);
$file_uri = vfsStream::url('root/Test.php');
try {
$pass = TRUE;
$this
->assertNoCoreUsage($file_uri);
} catch (AssertionFailedError $e) {
$pass = FALSE;
}
$this
->assertEquals($expected_pass, $pass, $expected_pass ? 'Test caused a false positive' : 'Test failed to detect Core usage');
}
}