TestController.php in Automatic Updates 8.2
File
tests/modules/automatic_updates_test/src/TestController.php
View source
<?php
namespace Drupal\automatic_updates_test;
use Drupal\automatic_updates\Exception\UpdateException;
use Drupal\automatic_updates\UpdateRecommender;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\HtmlResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
class TestController extends ControllerBase {
public function update(string $to_version) : Response {
Environment::setTimeLimit(0);
$updater = \Drupal::service('automatic_updates.updater');
try {
$updater
->begin([
'drupal' => $to_version,
]);
$updater
->stage();
$updater
->commit();
$updater
->clean();
$project = (new UpdateRecommender())
->getProjectInfo();
$content = $project['existing_version'];
$status = 200;
} catch (UpdateException $e) {
$messages = [];
foreach ($e
->getValidationResults() as $result) {
if ($summary = $result
->getSummary()) {
$messages[] = $summary;
}
$messages = array_merge($messages, $result
->getMessages());
}
$content = implode('<br />', $messages);
$status = 500;
}
return new HtmlResponse($content, $status);
}
public function metadata($project_name = 'drupal', $version = NULL) : Response {
if ($project_name !== 'drupal') {
return new Response();
}
$xml_map = $this
->config('update_test.settings')
->get('xml_map');
if (isset($xml_map[$project_name])) {
$availability_scenario = $xml_map[$project_name];
}
elseif (isset($xml_map['#all'])) {
$availability_scenario = $xml_map['#all'];
}
else {
$availability_scenario = '#broken#';
}
$file = __DIR__ . "/../../../fixtures/release-history/{$project_name}.{$availability_scenario}.xml";
$headers = [
'Content-Type' => 'text/xml; charset=utf-8',
];
if (!is_file($file)) {
return new Response('', 200, $headers);
}
return new BinaryFileResponse($file, 200, $headers);
}
}