function LegacyScriptsTest::testVendorTestCodeCleanup

@covers ::vendorTestCodeCleanup

File

core/tests/Drupal/BuildTests/Composer/LegacyScriptsTest.php, line 20

Class

LegacyScriptsTest
@group Composer @group legacy @requires externalCommand composer @coversDefaultClass \Drupal\Core\Composer\Composer[[api-linebreak]]

Namespace

Drupal\BuildTests\Composer

Code

public function testVendorTestCodeCleanup() {
  $package_dir = 'composer/Template/RecommendedProject';
  // Create a "Composer"-type repository containing one entry for every
  // package in the vendor directory.
  $vendor_packages_path = $this->getWorkspaceDirectory() . '/vendor_packages/packages.json';
  $this->makeVendorPackage($vendor_packages_path);
  // Make a copy of the code to alter in the workspace directory.
  $this->copyCodebase();
  // Remove the packages.drupal.org entry (and any other custom repository)
  // from the site under test's repositories section. There is no way to do
  // this via `composer config --unset`, so we read and rewrite composer.json.
  $composer_json_path = $this->getWorkspaceDirectory() . "/{$package_dir}/composer.json";
  $composer_json = json_decode(file_get_contents($composer_json_path), TRUE);
  unset($composer_json['repositories']);
  $json = json_encode($composer_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  file_put_contents($composer_json_path, $json);
  // Set up the template to use our path repos. Inclusion of metapackages is
  // reported differently, so we load up a separate set for them.
  $metapackage_path_repos = $this->getPathReposForType($this->getWorkspaceDirectory(), 'Metapackage');
  $this->assertArrayHasKey('drupal/core-recommended', $metapackage_path_repos);
  $path_repos = array_merge($metapackage_path_repos, $this->getPathReposForType($this->getWorkspaceDirectory(), 'Plugin'));
  // Always add drupal/core as a path repo.
  $path_repos['drupal/core'] = $this->getWorkspaceDirectory() . '/core';
  foreach ($path_repos as $name => $path) {
    $this->executeCommand("composer config --no-interaction repositories.{$name} path {$path}", $package_dir);
    $this->assertCommandSuccessful();
  }
  // Add our vendor package repository to our site under test's repositories
  // section. Call it "local" (although the name does not matter).
  $this->executeCommand("composer config --no-interaction repositories.local composer file://" . $vendor_packages_path, $package_dir);
  $this->assertCommandSuccessful();
  // Add the vendorTestCodeCleanup script as a post-install command.
  $this->executeCommand('composer config scripts.post-package-install "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup"');
  $this->assertCommandSuccessful();
  // Attempt to install packages which will trigger the script.
  $this->executeCommand('composer install');
  $this->assertCommandSuccessful();
}

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