summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-11-03 10:47:06 (GMT)
committerAlex Pott2014-11-03 10:47:06 (GMT)
commit914b78de24a048f45768a27f1c9d17833131761c (patch)
treeba1f865f1be79825788965ea374a2525c331c09b
parentf98cf1f60cbe8b4e48f58fc8970052e28166d6f3 (diff)
Issue #2282673 by stevepurkiss, ParisLiakos, mgifford: Add a PHPunit test for not using Drupal\Core code in Drupal\Component.
-rw-r--r--core/lib/Drupal/Component/Annotation/PluginID.php2
-rw-r--r--core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php2
-rw-r--r--core/lib/Drupal/Component/Utility/SortArray.php10
-rw-r--r--core/lib/Drupal/Component/Utility/UrlHelper.php5
-rw-r--r--core/tests/Drupal/Tests/Component/DrupalComponentTest.php78
-rw-r--r--core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php17
-rw-r--r--core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php1
-rw-r--r--core/tests/Drupal/Tests/Component/Uuid/UuidTest.php1
8 files changed, 86 insertions, 30 deletions
diff --git a/core/lib/Drupal/Component/Annotation/PluginID.php b/core/lib/Drupal/Component/Annotation/PluginID.php
index fe8e599..ab5e193 100644
--- a/core/lib/Drupal/Component/Annotation/PluginID.php
+++ b/core/lib/Drupal/Component/Annotation/PluginID.php
@@ -24,7 +24,7 @@ class PluginID extends AnnotationBase {
public $value;
/**
- * Implements \Drupal\Core\Annotation\AnnotationInterface::get().
+ * {@inheritdoc}
*/
public function get() {
return array(
diff --git a/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php b/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php
index a810063..57fd835 100644
--- a/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php
+++ b/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php
@@ -1,7 +1,7 @@
<?php
/**
* @file
- * Definition of Drupal\Core\Plugin\Exception\InvalidDecoratedMethod.
+ * Contains \Drupal\Component\Plugin\Exception\InvalidDecoratedMethod.
*/
namespace Drupal\Component\Plugin\Exception;
diff --git a/core/lib/Drupal/Component/Utility/SortArray.php b/core/lib/Drupal/Component/Utility/SortArray.php
index 79688ec..0ed5bbc 100644
--- a/core/lib/Drupal/Component/Utility/SortArray.php
+++ b/core/lib/Drupal/Component/Utility/SortArray.php
@@ -20,7 +20,7 @@ class SortArray {
* Note that the sorting is by the 'weight' array element, not by the render
* element property '#weight'.
*
- * Callback for uasort() used in various functions.
+ * Callback for uasort().
*
* @param array $a
* First item for comparison. The compared items should be associative
@@ -39,7 +39,7 @@ class SortArray {
/**
* Sorts a structured array by '#weight' property.
*
- * Callback for uasort() within \Drupal\Core\Render\Element::children().
+ * Callback for uasort().
*
* @param array $a
* First item for comparison. The compared items should be associative
@@ -57,7 +57,7 @@ class SortArray {
/**
* Sorts a structured array by 'title' key (no # prefix).
*
- * Callback for uasort() within system_admin_index().
+ * Callback for uasort().
*
* @param array $a
* First item for comparison. The compared items should be associative arrays
@@ -75,9 +75,7 @@ class SortArray {
/**
* Sorts a structured array by '#title' property.
*
- * Callback for uasort() within:
- * - system_modules()
- * - theme_simpletest_test_table()
+ * Callback for uasort().
*
* @param array $a
* First item for comparison. The compared items should be associative arrays
diff --git a/core/lib/Drupal/Component/Utility/UrlHelper.php b/core/lib/Drupal/Component/Utility/UrlHelper.php
index 45a8036..691413e 100644
--- a/core/lib/Drupal/Component/Utility/UrlHelper.php
+++ b/core/lib/Drupal/Component/Utility/UrlHelper.php
@@ -288,9 +288,8 @@ class UrlHelper {
* to being output to an HTML attribute value. It is often called as part of
* check_url() or Drupal\Component\Utility\Xss::filter(), but those functions
* return an HTML-encoded string, so this function can be called independently
- * when the output needs to be a plain-text string for passing to t(), _l(),
- * Drupal\Core\Template\Attribute, or another function that will call
- * \Drupal\Component\Utility\String::checkPlain() separately.
+ * when the output needs to be a plain-text string for passing to functions
+ * that will call \Drupal\Component\Utility\String::checkPlain() separately.
*
* @param string $uri
* A plain-text URI that might contain dangerous protocols.
diff --git a/core/tests/Drupal/Tests/Component/DrupalComponentTest.php b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
new file mode 100644
index 0000000..2924074
--- /dev/null
+++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
@@ -0,0 +1,78 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Component\DrupalComponentTest.
+ */
+
+namespace Drupal\Tests\Component;
+
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * General tests for \Drupal\Component that can't go anywhere else.
+ *
+ * @group Component
+ */
+class DrupalComponentTest extends UnitTestCase {
+
+ /**
+ * Tests that classes in Component do not use any Core class.
+ */
+ public function testNoCoreInComponent() {
+ $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
+ foreach ($this->findPhpClasses($component_path) as $class) {
+ $this->assertNoCoreUsage($class);
+ }
+ }
+
+ /**
+ * Tests that classes in Component Tests do not use any Core 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);
+ }
+ }
+
+ /**
+ * Searches a directory recursively for PHP classes.
+ *
+ * @param string $dir
+ * The full path to the directory that should be checked.
+ *
+ * @return array
+ * An array of class paths.
+ */
+ protected function findPhpClasses($dir) {
+ $classes = array();
+ 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;
+ }
+
+ /**
+ * Asserts that the given class is not using any class from Core namespace.
+ *
+ * @param string $class_path
+ * The full path to the class that should be checked.
+ */
+ protected function assertNoCoreUsage($class_path) {
+ foreach (new \SplFileObject($class_path) as $line_number => $line) {
+ // Allow linking to Core files with @see docs. Its harmless and boosts DX
+ // because even outside projects can treat those links as examples.
+ if ($line && (strpos($line, '@see ') === FALSE)) {
+ $this->assertSame(FALSE, strpos($line, 'Drupal\\Core'), "Illegal reference to 'Drupal\\Core' namespace in $class_path at line $line_number");
+ }
+ }
+ }
+
+}
diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
index 2c179a8..aa54598 100644
--- a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
+++ b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
@@ -8,7 +8,6 @@
namespace Drupal\Tests\Component\PhpStorage;
use Drupal\Tests\UnitTestCase;
-use Drupal\Core\PhpStorage\PhpStorageFactory;
/**
* Base test for PHP storages.
@@ -16,22 +15,6 @@ use Drupal\Core\PhpStorage\PhpStorageFactory;
abstract class PhpStorageTestBase extends UnitTestCase {
/**
- * The storage factory object.
- *
- * @var \Drupal\Component\PhpStorage\PhpStorageFactory
- */
- protected $storageFactory;
-
- /**
- * {@inheritdoc}
- */
- protected function setUp() {
- parent::setUp();
-
- $this->storageFactory = new PhpStorageFactory();
- }
-
- /**
* Assert that a PHP storage's load/save/delete operations work.
*/
public function assertCRUD($php) {
diff --git a/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php b/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php
index 97e4275..585bd37 100644
--- a/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php
+++ b/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php
@@ -7,7 +7,6 @@
namespace Drupal\Tests\Component\Plugin;
-use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Tests\UnitTestCase;
/**
diff --git a/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php b/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php
index 1f00819..353c352 100644
--- a/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php
+++ b/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php
@@ -12,7 +12,6 @@ use Drupal\Component\Uuid\UuidInterface;
use Drupal\Component\Uuid\Com;
use Drupal\Component\Uuid\Pecl;
use Drupal\Component\Uuid\Php;
-use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
/**