You are here

public function ComposerHookTest::testComposerHooks in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ComposerHookTest.php \Drupal\Tests\Composer\Plugin\Scaffold\Functional\ComposerHookTest::testComposerHooks()
  2. 10 core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ComposerHookTest.php \Drupal\Tests\Composer\Plugin\Scaffold\Functional\ComposerHookTest::testComposerHooks()

Tests to see if scaffold operation runs at the correct times.

File

core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Functional/ComposerHookTest.php, line 73

Class

ComposerHookTest
Tests Composer Hooks that run scaffold operations.

Namespace

Drupal\Tests\Composer\Plugin\Scaffold\Functional

Code

public function testComposerHooks() {
  $topLevelProjectDir = 'composer-hooks-fixture';
  $sut = $this->fixturesDir . '/' . $topLevelProjectDir;

  // First test: run composer install. This is the same as composer update
  // since there is no lock file. Ensure that scaffold operation ran.
  $this
    ->mustExec("composer install --no-ansi", $sut);
  $this
    ->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'Test version of default.settings.php from drupal/core');

  // Run composer required to add in the scaffold-override-fixture. This
  // project is "allowed" in our main fixture project, but not required.
  // We expect that requiring this library should re-scaffold, resulting
  // in a changed default.settings.php file.
  $stdout = $this
    ->mustExec("composer require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut);
  $this
    ->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');

  // Make sure that the appropriate notice informing us that scaffolding
  // is allowed was printed.
  $this
    ->assertStringContainsString('Package fixtures/scaffold-override-fixture has scaffold operations, and is already allowed in the root-level composer.json file.', $stdout);

  // Delete one scaffold file, just for test purposes, then run
  // 'composer update' and see if the scaffold file is replaced.
  @unlink($sut . '/sites/default/default.settings.php');
  $this
    ->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  $this
    ->mustExec("composer update --no-ansi", $sut);
  $this
    ->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');

  // Delete the same test scaffold file again, then run
  // 'composer drupal:scaffold' and see if the scaffold file is
  // re-scaffolded.
  @unlink($sut . '/sites/default/default.settings.php');
  $this
    ->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  $this
    ->mustExec("composer install --no-ansi", $sut);
  $this
    ->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');

  // Delete the same test scaffold file yet again, then run
  // 'composer install' and see if the scaffold file is re-scaffolded.
  @unlink($sut . '/sites/default/default.settings.php');
  $this
    ->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  $this
    ->mustExec("composer drupal:scaffold --no-ansi", $sut);
  $this
    ->assertScaffoldedFile($sut . '/sites/default/default.settings.php', FALSE, 'scaffolded from the scaffold-override-fixture');

  // Run 'composer create-project' to create a new test project called
  // 'create-project-test', which is a copy of 'fixtures/drupal-drupal'.
  $sut = $this->fixturesDir . '/create-project-test';
  $filesystem = new Filesystem();
  $filesystem
    ->remove($sut);
  $stdout = $this
    ->mustExec("composer create-project --repository=packages.json fixtures/drupal-drupal {$sut}", $this->fixturesDir, [
    'COMPOSER_MIRROR_PATH_REPOS' => 1,
  ]);
  $this
    ->assertDirectoryExists($sut);
  $this
    ->assertStringContainsString('Scaffolding files for fixtures/drupal-drupal', $stdout);
  $this
    ->assertScaffoldedFile($sut . '/index.php', FALSE, 'Test version of index.php from drupal/core');
  $topLevelProjectDir = 'composer-hooks-nothing-allowed-fixture';
  $sut = $this->fixturesDir . '/' . $topLevelProjectDir;

  // Run composer install on an empty project.
  $this
    ->mustExec("composer install --no-ansi", $sut);

  // Require a project that is not allowed to scaffold and confirm that we
  // get a warning, and it does not scaffold.
  $stdout = $this
    ->mustExec("composer require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut);
  $this
    ->assertFileDoesNotExist($sut . '/sites/default/default.settings.php');
  $this
    ->assertStringContainsString("Not scaffolding files for fixtures/scaffold-override-fixture, because it is not listed in the element 'extra.drupal-scaffold.allowed-packages' in the root-level composer.json file.", $stdout);
}