class ReplaceOpTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php \Drupal\Tests\Composer\Plugin\Scaffold\Integration\ReplaceOpTest
  2. 10 core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php \Drupal\Tests\Composer\Plugin\Scaffold\Integration\ReplaceOpTest
  3. 9 core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php \Drupal\Tests\Composer\Plugin\Scaffold\Integration\ReplaceOpTest
  4. 8.9.x core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php \Drupal\Tests\Composer\Plugin\Scaffold\Integration\ReplaceOpTest

Tests Drupal\Composer\Plugin\Scaffold\Operations\ReplaceOp.

Attributes

#[CoversClass(ReplaceOp::class)] #[Group('Scaffold')]

Hierarchy

  • class \Drupal\Tests\Composer\Plugin\Scaffold\Integration\ReplaceOpTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of ReplaceOpTest

File

core/tests/Drupal/Tests/Composer/Plugin/Scaffold/Integration/ReplaceOpTest.php, line 17

Namespace

Drupal\Tests\Composer\Plugin\Scaffold\Integration
View source
class ReplaceOpTest extends TestCase {
  
  /**
   * Tests process.
   */
  public function testProcess() : void {
    $fixtures = new Fixtures();
    $destination = $fixtures->destinationPath('[web-root]/robots.txt');
    $source = $fixtures->sourcePath('drupal-assets-fixture', 'robots.txt');
    $options = ScaffoldOptions::create([]);
    $sut = new ReplaceOp($source, TRUE);
    // Assert that there is no target file before we run our test.
    $this->assertFileDoesNotExist($destination->fullPath());
    // Test the system under test.
    $sut->process($destination, $fixtures->io(), $options);
    // Assert that the target file was created.
    $this->assertFileExists($destination->fullPath());
    // Assert the target contained the contents from the correct scaffold file.
    $contents = trim(file_get_contents($destination->fullPath()));
    $this->assertEquals('# Test version of robots.txt from drupal/core.', $contents);
    // Confirm that expected output was written to our io fixture.
    $output = $fixtures->getOutput();
    $this->assertStringContainsString('Copy [web-root]/robots.txt from assets/robots.txt', $output);
    // Test when the target file is already present and not writable.
    $this->assertDirectoryIsWritable(dirname($destination->fullPath()));
    file_put_contents($destination->fullPath(), 'This is a test');
    chmod($destination->fullPath(), 0444);
    $this->assertFileIsNotWritable($destination->fullPath());
    chmod(dirname($destination->fullPath()), 0555);
    $this->assertDirectoryIsNotWritable(dirname($destination->fullPath()));
    $sut = new ReplaceOp($source, TRUE);
    $this->assertFileExists($destination->fullPath());
    // Test the system under test.
    $sut->process($destination, $fixtures->io(TRUE), $options);
    // Assert the target contained the contents from the correct scaffold file.
    $contents = trim(file_get_contents($destination->fullPath()));
    $this->assertEquals('# Test version of robots.txt from drupal/core.', $contents);
    // Assert the target is still not writable.
    $this->assertFileIsNotWritable($destination->fullPath());
    $this->assertDirectoryIsNotWritable(dirname($destination->fullPath()));
    // Confirm that expected output was written to our io fixture.
    $output = $fixtures->getOutput();
    $this->assertStringContainsString('Copy [web-root]/robots.txt from assets/robots.txt', $output);
    // Make it a symlink and ensure that the permissions code is not executed.
    $source_perms = fileperms($source->fullPath());
    $this->assertNotEquals($source_perms, fileperms($destination->fullPath()));
    $options = ScaffoldOptions::create([
      'drupal-scaffold' => [
        'symlink' => TRUE,
      ],
    ]);
    $sut->process($destination, $fixtures->io(TRUE), $options);
    $this->assertTrue(is_link($destination->fullPath()));
    // Confirm that expected output was written to our io fixture.
    $output = $fixtures->getOutput();
    $this->assertStringContainsString('Link [web-root]/robots.txt', $output);
    $this->assertEquals($source_perms, fileperms($destination->fullPath()));
    $this->assertEquals($source_perms, fileperms($source->fullPath()));
  }
  
  /**
   * Tests empty file.
   *
   * @legacy-covers ::process
   */
  public function testEmptyFile() : void {
    $fixtures = new Fixtures();
    $destination = $fixtures->destinationPath('[web-root]/empty_file.txt');
    $source = $fixtures->sourcePath('empty-file', 'empty_file.txt');
    $options = ScaffoldOptions::create([]);
    $sut = new ReplaceOp($source, TRUE);
    // Assert that there is no target file before we run our test.
    $this->assertFileDoesNotExist($destination->fullPath());
    // Test the system under test.
    $sut->process($destination, $fixtures->io(), $options);
    // Assert that the target file was created.
    $this->assertFileExists($destination->fullPath());
    // Assert the target contained the contents from the correct scaffold file.
    $this->assertSame('', file_get_contents($destination->fullPath()));
    // Confirm that expected output was written to our io fixture.
    $output = $fixtures->getOutput();
    $this->assertStringContainsString('Copy [web-root]/empty_file.txt from assets/empty_file.txt', $output);
  }

}

Members

Title Sort descending Modifiers Object type Summary
ReplaceOpTest::testEmptyFile public function Tests empty file.
ReplaceOpTest::testProcess public function Tests process.

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