function DirectWriteTest::testSiteNotSandboxedIfDirectWriteGloballyEnabled

Tests direct-write mode when globally enabled.

File

core/modules/package_manager/tests/src/Kernel/DirectWriteTest.php, line 124

Class

DirectWriteTest
@covers \Drupal\package_manager\EventSubscriber\DirectWriteSubscriber @covers \Drupal\package_manager\SandboxManagerBase::isDirectWrite

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testSiteNotSandboxedIfDirectWriteGloballyEnabled() : void {
    $mock_beginner = $this->createMock(BeginnerInterface::class);
    $mock_beginner->expects($this->never())
        ->method('begin')
        ->withAnyParameters();
    $this->container
        ->set(BeginnerInterface::class, $mock_beginner);
    $mock_committer = $this->createMock(CommitterInterface::class);
    $mock_committer->expects($this->never())
        ->method('commit')
        ->withAnyParameters();
    $this->container
        ->set(CommitterInterface::class, $mock_committer);
    $this->setSetting('package_manager_allow_direct_write', TRUE);
    $sandbox_manager = $this->createStage(TestDirectWriteSandboxManager::class);
    $logger = new TestLogger();
    $sandbox_manager->setLogger($logger);
    $this->assertTrue($sandbox_manager->isDirectWrite());
    // A status check should flag a warning about running in direct-write mode.
    $expected_results = [
        ValidationResult::createWarning([
            $this->t('Direct-write mode is enabled, which means that changes will be made without sandboxing them first. This can be risky and is not recommended for production environments. For safety, your site will be put into maintenance mode while dependencies are updated.'),
        ]),
    ];
    $actual_results = $this->runStatusCheck($sandbox_manager);
    $this->assertValidationResultsEqual($expected_results, $actual_results);
    $sandbox_manager->create();
    // In direct-write mode, the active and sandbox directories are the same.
    $this->assertTrue($sandbox_manager->sandboxDirectoryExists());
    $this->assertSame($this->container
        ->get(PathLocator::class)
        ->getProjectRoot(), $sandbox_manager->getSandboxDirectory());
    // Do a require operation so we can assert that we are kicked into, and out
    // of, maintenance mode.
    $sandbox_manager->require([
        'ext-json:*',
    ]);
    $this->assertTrue($this->preRequireMaintenanceMode);
    $this->assertFalse($this->postRequireMaintenanceMode);
    $sandbox_manager->apply();
    $sandbox_manager->postApply();
    // Destroying the sandbox should not populate the clean-up queue.
    $sandbox_manager->destroy();
    
    /** @var \Drupal\Core\Queue\QueueInterface $queue */
    $queue = $this->container
        ->get(QueueFactory::class)
        ->get('package_manager_cleanup');
    $this->assertSame(0, $queue->numberOfItems());
    $records = $logger->recordsByLevel['info'];
    $this->assertCount(2, $records);
    $this->assertSame('Direct-write is enabled. Skipping sandboxing.', (string) $records[0]['message']);
    $this->assertSame('Direct-write is enabled. Changes have been made to the running code base.', (string) $records[1]['message']);
    // A sandbox manager that doesn't support direct-write should not be
    // influenced by the setting.
    $this->assertFalse($this->createStage()
        ->isDirectWrite());
}

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