class VendorHardeningPluginTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php \Drupal\Tests\Composer\Plugin\VendorHardening\VendorHardeningPluginTest
  2. 8.9.x core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php \Drupal\Tests\Composer\Plugin\VendorHardening\VendorHardeningPluginTest
  3. 11.x core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php \Drupal\Tests\Composer\Plugin\VendorHardening\VendorHardeningPluginTest

@coversDefaultClass \Drupal\Composer\Plugin\VendorHardening\VendorHardeningPlugin
@group VendorHardening

Hierarchy

Expanded class hierarchy of VendorHardeningPluginTest

File

core/tests/Drupal/Tests/Composer/Plugin/VendorHardening/VendorHardeningPluginTest.php, line 22

Namespace

Drupal\Tests\Composer\Plugin\VendorHardening
View source
class VendorHardeningPluginTest extends TestCase {
  use PhpUnitWarnings;
  use ProphecyTrait;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    vfsStream::setup('vendor', NULL, [
      'drupal' => [
        'package' => [
          'tests' => [
            'SomeTest.php' => '<?php',
          ],
          'SomeFile.php' => '<?php',
        ],
      ],
    ]);
  }
  
  /**
   * @covers ::cleanPackage
   */
  public function testCleanPackage() : void {
    $config = $this->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config->expects($this->once())
      ->method('getPathsForPackage')
      ->willReturn([
      'tests',
    ]);
    $plugin = $this->getMockBuilder(VendorHardeningPlugin::class)
      ->onlyMethods([
      'getInstallPathForPackage',
    ])
      ->getMock();
    $plugin->expects($this->once())
      ->method('getInstallPathForPackage')
      ->willReturn(vfsStream::url('vendor/drupal/package'));
    $ref_config = new \ReflectionProperty($plugin, 'config');
    $ref_config->setValue($plugin, $config);
    $io = $this->prophesize(IOInterface::class);
    $ref_io = new \ReflectionProperty($plugin, 'io');
    $ref_io->setValue($plugin, $io->reveal());
    $this->assertFileExists(vfsStream::url('vendor/drupal/package/tests/SomeTest.php'));
    $package = $this->prophesize(PackageInterface::class);
    $package->getName()
      ->willReturn('drupal/package');
    $plugin->cleanPackage($package->reveal());
    $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/tests'));
  }
  
  /**
   * @covers ::cleanPathsForPackage
   */
  public function testCleanPathsForPackage() : void {
    $plugin = $this->getMockBuilder(VendorHardeningPlugin::class)
      ->onlyMethods([
      'getInstallPathForPackage',
    ])
      ->getMock();
    $plugin->expects($this->once())
      ->method('getInstallPathForPackage')
      ->willReturn(vfsStream::url('vendor/drupal/package'));
    $io = $this->prophesize(IOInterface::class);
    $ref_io = new \ReflectionProperty($plugin, 'io');
    $ref_io->setValue($plugin, $io->reveal());
    $this->assertFileExists(vfsStream::url('vendor/drupal/package/tests/SomeTest.php'));
    $this->assertFileExists(vfsStream::url('vendor/drupal/package/SomeFile.php'));
    $package = $this->prophesize(PackageInterface::class);
    $package->getName()
      ->willReturn('drupal/package');
    $ref_clean = new \ReflectionMethod($plugin, 'cleanPathsForPackage');
    $ref_clean->invokeArgs($plugin, [
      $package->reveal(),
      [
        'tests',
        'SomeFile.php',
      ],
    ]);
    $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/tests'));
    $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/SomeFile.php'));
  }
  
  /**
   * @covers ::cleanAllPackages
   */
  public function testCleanAllPackages() : void {
    $config = $this->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config->expects($this->once())
      ->method('getAllCleanupPaths')
      ->willReturn([
      'drupal/package' => [
        'tests',
      ],
    ]);
    $package = $this->createMock(PackageInterface::class);
    $package->expects($this->any())
      ->method('getName')
      ->willReturn('drupal/package');
    $plugin = $this->getMockBuilder(VendorHardeningPlugin::class)
      ->onlyMethods([
      'getInstalledPackages',
      'getInstallPathForPackage',
    ])
      ->getMock();
    $plugin->expects($this->once())
      ->method('getInstalledPackages')
      ->willReturn([
      $package,
    ]);
    $plugin->expects($this->once())
      ->method('getInstallPathForPackage')
      ->willReturn(vfsStream::url('vendor/drupal/package'));
    $io = $this->prophesize(IOInterface::class);
    $ref_io = new \ReflectionProperty($plugin, 'io');
    $ref_io->setValue($plugin, $io->reveal());
    $ref_config = new \ReflectionProperty($plugin, 'config');
    $ref_config->setValue($plugin, $config);
    $this->assertFileExists(vfsStream::url('vendor/drupal/package/tests/SomeTest.php'));
    $plugin->cleanAllPackages();
    $this->assertFileDoesNotExist(vfsStream::url('vendor/drupal/package/tests'));
  }
  
  /**
   * @covers ::writeAccessRestrictionFiles
   */
  public function testWriteAccessRestrictionFiles() : void {
    $dir = vfsStream::url('vendor');
    // Set up mocks so that writeAccessRestrictionFiles() can eventually use
    // the IOInterface object.
    $composer = $this->getMockBuilder(Composer::class)
      ->onlyMethods([
      'getPackage',
    ])
      ->getMock();
    $composer->expects($this->once())
      ->method('getPackage')
      ->willReturn($this->prophesize(RootPackageInterface::class)
      ->reveal());
    $plugin = new VendorHardeningPlugin();
    $plugin->activate($composer, $this->prophesize(IOInterface::class)
      ->reveal());
    $this->assertDirectoryExists($dir);
    $this->assertFileDoesNotExist($dir . '/.htaccess');
    $this->assertFileDoesNotExist($dir . '/web.config');
    $plugin->writeAccessRestrictionFiles($dir);
    $this->assertFileExists($dir . '/.htaccess');
    $this->assertFileExists($dir . '/web.config');
  }
  public static function providerFindBinOverlap() {
    return [
      [
        [],
        [
          'bin/script',
        ],
        [
          'tests',
        ],
      ],
      [
        [
          'bin/composer' => 'bin/composer',
        ],
        [
          'bin/composer',
        ],
        [
          'bin',
          'tests',
        ],
      ],
      [
        [
          'bin/composer' => 'bin/composer',
        ],
        [
          'bin/composer',
        ],
        [
          'bin/composer',
        ],
      ],
      [
        [],
        [
          'bin/composer',
        ],
        [
          'bin/something_else',
        ],
      ],
      [
        [],
        [
          'test/script',
        ],
        [
          'test/longer',
        ],
      ],
      [
        [
          'bin/very/long/path/script' => 'bin/very/long/path/script',
        ],
        [
          'bin/very/long/path/script',
        ],
        [
          'bin',
        ],
      ],
      [
        [
          'bin/bin/bin' => 'bin/bin/bin',
        ],
        [
          'bin/bin/bin',
        ],
        [
          'bin/bin',
        ],
      ],
      [
        [],
        [
          'bin/bin',
        ],
        [
          'bin/bin/bin',
        ],
      ],
    ];
  }
  
  /**
   * @covers ::findBinOverlap
   * @dataProvider providerFindBinOverlap
   */
  public function testFindBinOverlap($expected, $binaries, $clean_paths) : void {
    $plugin = $this->getMockBuilder(VendorHardeningPlugin::class)
      ->disableOriginalConstructor()
      ->getMock();
    $ref_find_bin_overlap = new \ReflectionMethod($plugin, 'findBinOverlap');
    $this->assertSame($expected, $ref_find_bin_overlap->invokeArgs($plugin, [
      $binaries,
      $clean_paths,
    ]));
  }

}

Members

Title Sort descending Modifiers Object type Summary
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
VendorHardeningPluginTest::providerFindBinOverlap public static function
VendorHardeningPluginTest::setUp protected function
VendorHardeningPluginTest::testCleanAllPackages public function @covers ::cleanAllPackages[[api-linebreak]]
VendorHardeningPluginTest::testCleanPackage public function @covers ::cleanPackage[[api-linebreak]]
VendorHardeningPluginTest::testCleanPathsForPackage public function @covers ::cleanPathsForPackage[[api-linebreak]]
VendorHardeningPluginTest::testFindBinOverlap public function @covers ::findBinOverlap[[api-linebreak]]
@dataProvider providerFindBinOverlap
VendorHardeningPluginTest::testWriteAccessRestrictionFiles public function @covers ::writeAccessRestrictionFiles[[api-linebreak]]

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.