Problem/Motivation

Two times `@var \Drupal\Composer\VendorHardening\Config` is used when this is incorrect namespace.

Also, `use Drupal\Composer\VendorHardening\Config` is used once with the incorrect namespace.

The correct namespace is
\Drupal\Composer\Plugin\VendorHardening\Config

Proposed resolution

Correct the namespace

Remaining tasks

patch and review

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tedbow created an issue. See original summary.

tedbow’s picture

#3088730: Include 'composer' directory in phpcs scans as related but this error I don't think is caught by phpcs. I tested with another file in core.

Here is a fix.

tedbow’s picture

Status: Active » Needs review
Kristen Pol’s picture

Status: Needs review » Needs work

Thanks for issue and patch.

1) Patch applies cleanly.

2) Tests pass.

3) See that composer/Plugin/VendorHardening/Config.php has namespace Drupal\Composer\Plugin\VendorHardening which is what is noted in the issue summary.

4) Searching for @var \Drupal\Composer\VendorHardening\Config returns:

./composer/Plugin/VendorHardening/VendorHardeningPlugin.php:   * @var \Drupal\Composer\VendorHardening\Config
./composer/Plugin/ProjectMessage/MessagePlugin.php:   * @var \Drupal\Composer\VendorHardening\Config

which are the items addressed in patch.

5) Searching for other uses of Drupal\Composer\VendorHardening\Config shows this one:

./core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php:use Drupal\Composer\VendorHardening\Config;

So how does this test even work? Shouldn't it error out? Maybe I'm missing something.

Seems to me that VendorHardeningPluginTest.php needs updating so back to needs work. If so, the issue summary needs to be updated as well.

Kristen Pol’s picture

Issue tags: +Bug Smash Initiative
tedbow’s picture

Title: Incorrect Drupal\Composer\VendorHardening namespace is used for @var comments » Incorrect Drupal\Composer\VendorHardening namespace is used instead of Drupal\Composer\Plugin\VendorHardening
Status: Needs work » Needs review
FileSize
1.88 KB
2.95 KB

So how does this test even work? Shouldn't it error out? Maybe I'm missing something.

Interesting I would have thought it would error out too. It is not because.

  1. It seems that you can add "::class" to anything and it will return back a namespace string.

    If you add to the test:
    use Drupal\IamNotReal;
    and then later
    print "class=" . IamNotReal::class;

    It will print
    "class=Drupal\IamNotReal"

    If you don't add the use statement it would print out
    "class=Drupal\Tests\Composer\Plugin\VendorHardening\IamNotReal"
    which just uses the namespace of the current class.

    but in either case if you actually try
    new IamNotReal(); it will error
    Error : Class 'Drupal\Tests\Composer\Plugin\VendorHardening\IamNotReal' not found or the other namespace.

    I guess this because using ::class doesn't actually check the class exists.

  2. Apparently you can pass any string to \PHPUnit\Framework\TestCase::getMockBuilder() until it actually has to check something like if the original class or interface has a constructor or method.

    I tried changing
    \Drupal\Tests\rest\Unit\Plugin\views\style\SerializerTest

    I changed

    -$this->view = $this->getMockBuilder(ViewExecutable::class)
    +    $this->view = $this->getMockBuilder('pancake')

    This worked fine because it the original code is

    $this->view = $this->getMockBuilder('pancake')
          ->disableOriginalConstructor()
          ->getMock();

    So it doesn't do anything with the mock except disableOriginalConstructor()

    In the same class if I try the same with

    $this->displayHandler = $this->getMockBuilder(RestExport::class)
          ->disableOriginalConstructor()
          ->getMock();
    
        $this->displayHandler->expects($this->any())
          ->method('getContentType')
          ->willReturn('json');
    

    It will fail because there is no getContentType() method.

    In our test there is

    $config = $this->getMockBuilder(Config::class)
          ->setMethods(['getPathsForPackage'])
          ->disableOriginalConstructor()
          ->getMock();
        $config->expects($this->once())
          ->method('getPathsForPackage')
          ->willReturn(['tests']);

    getPathsForPackage doesn't exist but setMethods() sets it so it doesn't fail. Commenting out
    ->setMethods(['getPathsForPackage']) make it fail.

    If you fix the class namespace in the use statement you don't need setMethods().

We could fix this in this issue but it seems like a bigger problem with this test.

Anyways I have included it.

Kristen Pol’s picture

Issue summary: View changes

Wow! Super weird. Thanks for playing with that.

1) I updated the issue summary to cover the use Drupal\Composer\VendorHardening\Config; issue.

2) After applying new patch, there are no more Drupal\Composer\VendorHardening\Config references in the code as expected and the 3 places are changed to Drupal\Composer\Plugin\VendorHardening\Config.

3) If this passes tests, this looks good to me other than I'm not sure if the ->setMethods() should be fixed here but I'll let the maintainers decide.

+++ b/core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php
@@ -36,7 +36,6 @@ public function setUp(): void {
     $config = $this->getMockBuilder(Config::class)
-      ->setMethods(['getPathsForPackage'])
+++ b/core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php
@@ -85,7 +84,6 @@ public function testCleanPathsForPackage() {
     $config = $this->getMockBuilder(Config::class)
-      ->setMethods(['getAllCleanupPaths'])
+++ b/core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php
@@ -93,7 +91,6 @@ public function testCleanAllPackages() {
     $package = $this->getMockBuilder(PackageInterface::class)
-      ->setMethods(['getName'])
Kristen Pol’s picture

Status: Needs review » Reviewed & tested by the community

RTBC now that tests are green and based on my last comment.

alexpott’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 82097e2ee3 to 9.1.x and d0b69bf612 to 9.0.x and bf44721fd6 to 8.9.x. Thanks!

Backported to 8.9.x as this is tests and documentation.

  • alexpott committed 82097e2 on 9.1.x
    Issue #3162479 by tedbow, Kristen Pol: Incorrect Drupal\Composer\...

  • alexpott committed d0b69bf on 9.0.x
    Issue #3162479 by tedbow, Kristen Pol: Incorrect Drupal\Composer\...

  • alexpott committed bf44721 on 8.9.x
    Issue #3162479 by tedbow, Kristen Pol: Incorrect Drupal\Composer\...

Status: Fixed » Closed (fixed)

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