3ca46baa3c2448a599e66004b49aebe68e120c5d
[yaffs-website] / web / core / modules / update / tests / modules / update_test / src / Controller / UpdateTestController.php
1 <?php
2
3 namespace Drupal\update_test\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\HttpFoundation\BinaryFileResponse;
8
9 /**
10  * Provides different routes of the update_test module.
11  */
12 class UpdateTestController extends ControllerBase {
13
14   /**
15    * Displays an Error 503 (Service unavailable) page.
16    *
17    * @return \Symfony\Component\HttpFoundation\Response
18    *   Returns the response with a special header.
19    */
20   public function updateError() {
21     $response = new Response();
22     $response->setStatusCode(503);
23     $response->headers->set('Status', '503 Service unavailable');
24
25     return $response;
26   }
27
28   /**
29    * Page callback: Prints mock XML for the Update Manager module.
30    *
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.
37    *
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.
43    *
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
47    *   content otherwise.
48    */
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];
53     }
54     elseif (isset($xml_map['#all'])) {
55       $availability_scenario = $xml_map['#all'];
56     }
57     else {
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
62       // output.
63       $availability_scenario = '#broken#';
64     }
65
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);
71     }
72     return new BinaryFileResponse($file, 200, $headers);
73   }
74
75 }