3 namespace Drupal\update_test\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\HttpFoundation\BinaryFileResponse;
10 * Provides different routes of the update_test module.
12 class UpdateTestController extends ControllerBase {
15 * Displays an Error 503 (Service unavailable) page.
17 * @return \Symfony\Component\HttpFoundation\Response
18 * Returns the response with a special header.
20 public function updateError() {
21 $response = new Response();
22 $response->setStatusCode(503);
23 $response->headers->set('Status', '503 Service unavailable');
29 * Page callback: Prints mock XML for the Update Manager module.
31 * The specific XML file to print depends on two things: the project we're
32 * trying to fetch data for, and the desired "availability scenario" for that
33 * project which we're trying to test. Before attempting to fetch this data (by
34 * checking for updates on the available updates report), callers need to define
35 * the 'update_test_xml_map' variable as an array, keyed by project name,
36 * indicating which availability scenario to use for that project.
38 * @param string $project_name
39 * The project short name the update manager is trying to fetch data for (the
40 * fetch URLs are of the form: [base_url]/[project_name]/[core_version]).
41 * @param string $version
42 * The version of Drupal core.
44 * @return BinaryFileResponse|Response
45 * A BinaryFileResponse object containing the content of the XML release file
46 * for the specified project if one is available; a Response object with no
49 public function updateTest($project_name, $version) {
50 $xml_map = $this->config('update_test.settings')->get('xml_map');
51 if (isset($xml_map[$project_name])) {
52 $availability_scenario = $xml_map[$project_name];
54 elseif (isset($xml_map['#all'])) {
55 $availability_scenario = $xml_map['#all'];
58 // The test didn't specify (for example, the webroot has other modules and
59 // themes installed but they're disabled by the version of the site
60 // running the test. So, we default to a file we know won't exist, so at
61 // least we'll get an empty xml response instead of a bunch of Drupal page
63 $availability_scenario = '#broken#';
66 $file = __DIR__ . "/../../$project_name.$availability_scenario.xml";
67 $headers = ['Content-Type' => 'text/xml; charset=utf-8'];
68 if (!is_file($file)) {
69 // Return an empty response.
70 return new Response('', 200, $headers);
72 return new BinaryFileResponse($file, 200, $headers);