You are here

public function BuildTestTest::testCopyCodebaseExclude in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/BuildTests/Framework/Tests/BuildTestTest.php \Drupal\BuildTests\Framework\Tests\BuildTestTest::testCopyCodebaseExclude()

Ensure we're not copying directories we wish to exclude.

@covers ::copyCodebase

File

core/tests/Drupal/BuildTests/Framework/Tests/BuildTestTest.php, line 59

Class

BuildTestTest
@coversDefaultClass \Drupal\BuildTests\Framework\BuildTestBase @group Build

Namespace

Drupal\BuildTests\Framework\Tests

Code

public function testCopyCodebaseExclude() {

  // Create a virtual file system containing items that should be
  // excluded. Exception being modules directory.
  vfsStream::setup('drupal', NULL, [
    'sites' => [
      'default' => [
        'files' => [
          'a_file.txt' => 'some file.',
        ],
        'settings.php' => '<?php $settings = stuff;',
        'settings.local.php' => '<?php $settings = override;',
      ],
      'simpletest' => [
        'simpletest_hash' => [
          'some_results.xml' => '<xml/>',
        ],
      ],
    ],
    'vendor' => [
      'composer' => [
        'composer' => [
          'installed.json' => '"items": {"things"}',
        ],
      ],
    ],
    'modules' => [
      'my_module' => [
        'vendor' => [
          'my_vendor' => [
            'composer.json' => "{\n}",
          ],
        ],
      ],
    ],
  ]);

  // Mock BuildTestBase so that it thinks our VFS is the Drupal root.

  /** @var \PHPUnit\Framework\MockObject\MockBuilder|\Drupal\BuildTests\Framework\BuildTestBase $base */
  $base = $this
    ->getMockBuilder(BuildTestBase::class)
    ->setMethods([
    'getDrupalRoot',
  ])
    ->getMockForAbstractClass();
  $base
    ->expects($this
    ->exactly(2))
    ->method('getDrupalRoot')
    ->willReturn(vfsStream::url('drupal'));
  $base
    ->setUp();

  // Perform the copy.
  $test_directory = 'copied_codebase';
  $base
    ->copyCodebase(NULL, $test_directory);
  $full_path = $base
    ->getWorkspaceDirectory() . '/' . $test_directory;
  $this
    ->assertDirectoryExists($full_path);

  // Verify nested vendor directory was not excluded. Then remove it for next
  // validation.
  $this
    ->assertFileExists($full_path . DIRECTORY_SEPARATOR . 'modules/my_module/vendor/my_vendor/composer.json');
  $file_system = new Filesystem();
  $file_system
    ->remove($full_path . DIRECTORY_SEPARATOR . 'modules');

  // Use scandir() to determine if our target directory is empty. It should
  // only contain the system dot directories.
  $this
    ->assertTrue(($files = @scandir($full_path)) && count($files) <= 2, 'Directory is not empty: ' . implode(', ', $files));
  $base
    ->tearDown();
}