Child issue of #1929270: [meta] Drupal-agnostic components should not be calling Drupal functions

The issue might be fixed, but adding an automated test is a nice way to avoid regressions

CommentFileSizeAuthor
#40 interdiff-2282673-38-40.txt588 bytesAnonymous (not verified)
#40 drupal-no_core_in_component_test-2282673-40.patch8.15 KBAnonymous (not verified)
#38 interdiff-2282673-36-38.patch1.28 KBAnonymous (not verified)
#38 drupal-no_core_in_component_test-2282673-38.patch8.18 KBAnonymous (not verified)
#36 drupal-no_core_in_component_test-2282673-36.patch7.13 KBAnonymous (not verified)
#31 drupal-no_core_in_component_test-2282673-31.patch7.15 KBAnonymous (not verified)
#29 interdiff-2282673-24-29.txt1.88 KBAnonymous (not verified)
#29 drupal-no_core_in_component_test-2282673-29.patch7.15 KBAnonymous (not verified)
#24 interdiff-2282673-20-24.txt898 bytesAnonymous (not verified)
#24 drupal-no_core_in_component_test-2282673-24.patch5.27 KBAnonymous (not verified)
#20 interdiff-2282673-17-20.txt1.4 KBAnonymous (not verified)
#20 drupal-no_core_in_component_test-2282673-20.patch5.27 KBAnonymous (not verified)
#17 drupal-no_core_in_component_test-2282673-17.patch4.92 KBAnonymous (not verified)
#14 drupal-no_core_in_component_test-2282673-13.patch4.92 KBAnonymous (not verified)
#12 interdiff-8-11.txt1.39 KBAnonymous (not verified)
#11 drupal-no_core_in_component_test-2282673-11.patch4.93 KBAnonymous (not verified)
#8 drupal-no_core_in_component_test-2282673-8.patch4.8 KBParisLiakos
#5 drupal-no_core_in_component_test-2282673-5.patch2.69 KBmgifford
#2 interdiff.txt893 bytesParisLiakos
#2 drupal-no_core_in_component_test-2282673-2.patch5.02 KBParisLiakos
#1 drupal-no_core_in_component_test-2282673.patch5.09 KBParisLiakos
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ParisLiakos’s picture

Status: Active » Needs review
FileSize
5.09 KB
ParisLiakos’s picture

mgifford’s picture

Status: Postponed » Needs review
Related issues: +#1825952: Turn on twig autoescape by default

This shouldn't still be postponed I think.

Status: Needs review » Needs work

The last submitted patch, 2: drupal-no_core_in_component_test-2282673-2.patch, failed testing.

mgifford’s picture

Status: Needs work » Needs review
FileSize
2.69 KB

Reroll of #2.

tim.plunkett’s picture

Status: Needs review » Needs work

Those are just cosmetic fixes, and missing the entire test.

The last submitted patch, 5: drupal-no_core_in_component_test-2282673-5.patch, failed testing.

ParisLiakos’s picture

Status: Needs work » Needs review
FileSize
4.8 KB

thanks for reminding me this

jhedstrom’s picture

I agree this seems like a sound way to avoid regressions. Test looks good. Can somebody confirm though that all the cleanup here is still relevant/intended?

znerol’s picture

Status: Needs review » Needs work

Great plan. Test passes when patch applied and fails when I deliberately place a file mentioning the core namespace in the component directory.

  1. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,69 @@
    +      elseif ($file->getExtension() == 'php') {
    

    Use ===.

  2. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,69 @@
    +      if ($line && !strpos($line, '@see ')) {
    +        $this->assertFalse(strpos($line, 'Drupal\\Core'));
    

    Use strpos(...) !== FALSE, also the assertion results in a rather unhelpful message. E.g. Failed asserting that 4 is false. Would it be possible to at least mention the path to the offending file? Maybe even the line number?

Anonymous’s picture

Status: Needs work » Needs review
FileSize
4.93 KB
Anonymous’s picture

FileSize
1.39 KB

Forgot interdiff.

znerol’s picture

Great, thanks.

  1. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,71 @@
    +    $line_counter = 0;
    +    foreach (new \SplFileObject($class_path) as $line) {
    +      ++$line_counter;
    

    I think it also would be possible to write foreach (... as $line_number => $line).

  2. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,71 @@
    +        $this->assertFalse(strpos($line, 'Drupal\\Core'), "Found 'Drupal\\Core' class usage at line $line_counter in file $class_path");
    

    This is about the core namespace, not classes. Perhaps something like Illegal reference to 'Drupal\\Core' namespace in $class_path at line $line_number

    Also we need to use assertNotIdentical(FALSE, strpos(...), ...) for the same reasons pointed out in the previous review. Sorry that I did not realize that before.

Anonymous’s picture

"assertNotIdentical" not found using these test bases - are you implying that function needs to be created as it is in other places like views tests or can I link it in from somewhere?

Status: Needs review » Needs work

The last submitted patch, 14: drupal-no_core_in_component_test-2282673-13.patch, failed testing.

dawehner’s picture

Thank you steve!

"assertNotIdentical" not found using these test bases - are you implying that function needs to be created as it is in other places like views tests or can I link it in from somewhere?

I guess he was talking about assertNotSame

+++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
@@ -0,0 +1,69 @@
+/**
+ * General tests for Drupal\Component that can't go anywhere else.
+ *
+ * @group Drupal
+ * @group Component
+ */
+class DrupalComponentTest extends UnitTestCase {

... as I saw in another issue, we should expand this to do the same for the tests itself, so we ensure that also tests don't use anything from the core namespace.

Anonymous’s picture

Trying first bit, will look into test test coverage tomorrow.

znerol’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 17: drupal-no_core_in_component_test-2282673-17.patch, failed testing.

Anonymous’s picture

Status: Needs work » Needs review
FileSize
5.27 KB
1.4 KB

Currently unsure as to whether assertNotSame works - got two halves of a dev machine which I need to sort out to test properly locally but posting this in the meantime. Last test failed cos logic swapped back but now can't get it to fail.

Anonymous’s picture

Status: Needs review » Needs work

...and of course the second I post I realise what the issue is - strpos can return false but it'll return a posn, not TRUE. Putting the coffee on...

znerol’s picture

+++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
@@ -0,0 +1,79 @@
+        $this->assertNotSame(TRUE, strpos($line, 'Drupal\\Core'), "Illegal reference to 'Drupal\\Core' namespace in $class_path at line $line_number");

Should be assertSame(FALSE, strpos(...), ...). Sorry for the confusion.

With this change, the test should detect the following violations in component tests:

$ git grep -n Core core/tests/Drupal/Tests/Component
core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php:11:use Drupal\Core\PhpStorage\PhpStorageFactory;
core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php:10:use Drupal\Core\PhpStorage\PhpStorageFactory;
core/tests/Drupal/Tests/Component/Uuid/UuidTest.php:15:use Drupal\Core\DependencyInjection\ContainerBuilder;
znerol’s picture

Funny, UuidTest does not seem to use the ContainerBuilder at all. I think this use statement is spurious and can be deleted. The same applies for PhpStorageFactory in PluginBaseTest.

Also it seems that PhpStorageTestBase::$storageFactory is never used, neither in the base class nor in any of the subclasses. That means that this property can be removed, along with the setUp() method and the use statement for PhpStorageFactory.

Anonymous’s picture

Status: Needs work » Needs review
FileSize
5.27 KB
898 bytes
Anonymous’s picture

Funny, UuidTest does not seem to use the ContainerBuilder at all. I think this use statement is spurious and can be deleted. The same applies for PhpStorageFactory in PluginBaseTest.

Also it seems that PhpStorageTestBase::$storageFactory is never used, neither in the base class nor in any of the subclasses. That means that this property can be removed, along with the setUp() method and the use statement for PhpStorageFactory.

Is this meant for this issue?

goes to get more coffee...

znerol’s picture

Is this meant for this issue?

As soon as the newly implemented unit test works correctly, it will detect the violations in the component test namespace and will fail because of that (which is good!). So either we need to open a new issue for fixing the offending tests (UuidTest, PluginBaseTest and PhpStorageTestBase) and get that one in before, or we can fix them in this issue. I propose to fix them here if possible and only open a new issue if this turns out to be too complex (i.e. requiring functional changes to the tests).

Anonymous’s picture

OIC - sorry, just jumping in so not fully aware of issue scope (feel like I've been doing that for years ;) Thanks, will take a look.

Status: Needs review » Needs work

The last submitted patch, 24: drupal-no_core_in_component_test-2282673-24.patch, failed testing.

Anonymous’s picture

Status: Needs work » Needs review
FileSize
7.15 KB
1.88 KB
znerol’s picture

Great. From local testing I see there is one little thing left. Ironically, the new test detects a core reference in itself. The problem is the comment in line 64.

+++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
@@ -0,0 +1,79 @@
+   * Asserts that the file is not using any class from Drupal\Core.

Change it to something like Asserts that the file is not using any class from Core namespace and we should be fine.

Anonymous’s picture

D'oh of course! My local dev setup is Yosemite'd at the moment, trying to fix whilst patching manually ;)

text changed

The last submitted patch, 29: drupal-no_core_in_component_test-2282673-29.patch, failed testing.

znerol’s picture

Status: Needs review » Reviewed & tested by the community

Perfect. Thank you for picking that up and good luck with deyosemiting your box.

dawehner’s picture

+++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
@@ -0,0 +1,79 @@
+  /**
+   * 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);
+    }
+  }
+

Thank you!

Wim Leers’s picture

From local testing I see there is one little thing left. Ironically, the new test detects a core reference in itself.

Haha! :D

Great patch, thank you!

RTBC+1

A few nits that can be fixed on commit:

  1. +++ b/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php
    @@ -1,7 +1,7 @@
    + * Definition of Drupal\Component\Plugin\Exception\InvalidDecoratedMethod.
    

    s/Definition of Drupal/Contains \Drupal\/

  2. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,79 @@
    + * General tests for Drupal\Component that can't go anywhere else.
    

    s/Drupal\Component/\Drupal\Component/

  3. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,79 @@
    + * @group Drupal
    

    We don't add @group Drupal anymore.

  4. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,79 @@
    +   * Searches a directory recursively for php classes.
    

    s/php/PHP/

  5. +++ b/core/tests/Drupal/Tests/Component/DrupalComponentTest.php
    @@ -0,0 +1,79 @@
    +   * Asserts that the file is not using any class from Core namespace.
    

    s/file/given class/

Anonymous’s picture

Just on way out to London - dev machine still broken but quickly manually edited the patch according to #35. Presume 'on commit' may mean this not necessary but thought might help.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

SortArray still needs quite a bit of cleaning up.

   * Callback for uasort() within:
   * - system_modules()
   * - theme_simpletest_test_table()
   * Callback for uasort() within system_admin_index().
   * Callback for uasort() used in various functions.

I think all the methods here should just have a line which says (if it is)

   * Callback for uasort().

Or something similar.

I guess someone might argue this is out-of-scope but since we doing:

+++ b/core/lib/Drupal/Component/Utility/SortArray.php
@@ -39,8 +39,6 @@ public static function sortByWeightElement(array $a, array $b) {
-   * Callback for uasort() within \Drupal\Core\Render\Element::children().
-   *

I think not.

Anonymous’s picture

Status: Needs work » Needs review
FileSize
8.18 KB
1.28 KB

No problem, how's this?

znerol’s picture

+++ b/core/lib/Drupal/Component/Utility/SortArray.php
@@ -39,8 +39,6 @@ public static function sortByWeightElement(array $a, array $b) {
-   * Callback for uasort() within \Drupal\Core\Render\Element::children().
-   *

One final nitpick. Instead of removing this line entirely, it could be replaced with the comment now used elsewhere, i.e. Callback for uasort().

Anonymous’s picture

Kicking myself for missing that!

znerol’s picture

Status: Needs review » Reviewed & tested by the community

Thanks.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 914b78d and pushed to 8.0.x. Thanks!

alexpott’s picture

This issue is a unfrozen change (testing) as per #2350615: [policy, no patch] What changes can be accepted during the Drupal 8 beta phase? and it's benefits outweigh any disruption.

  • alexpott committed 914b78d on 8.0.x
    Issue #2282673 by stevepurkiss, ParisLiakos, mgifford: Add a PHPunit...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.