Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / language / tests / language_test / src / Controller / LanguageTestController.php
1 <?php
2
3 namespace Drupal\language_test\Controller;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Drupal\Core\Url;
9 use Drupal\language\ConfigurableLanguageInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpKernel\HttpKernelInterface;
13
14 /**
15  * Controller routines for language_test routes.
16  */
17 class LanguageTestController implements ContainerInjectionInterface {
18
19   use StringTranslationTrait;
20
21   /**
22    * The HTTP kernel service.
23    *
24    * @var \Symfony\Component\HttpKernel\HttpKernelInterface
25    */
26   protected $httpKernel;
27
28   /**
29    * The language manager service.
30    *
31    * @var \Drupal\Core\Language\LanguageManagerInterface
32    */
33   protected $languageManager;
34
35   /**
36    * Constructs a new LanguageTestController object.
37    *
38    * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
39    *   An HTTP kernel.
40    */
41   public function __construct(HttpKernelInterface $httpKernel, LanguageManagerInterface $language_manager) {
42     $this->httpKernel = $httpKernel;
43     $this->languageManager = $language_manager;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function create(ContainerInterface $container) {
50     return new static($container->get('http_kernel'), $container->get('language_manager'));
51   }
52
53   /**
54    * Route entity upcasting test helper.
55    *
56    * @param \Drupal\language\ConfigurableLanguageInterface $configurable_language
57    *   The ConfigurableLanguage object from the route.
58    *
59    * @return string
60    *   Testing feedback based on (translated) entity title.
61    */
62   public function testEntity(ConfigurableLanguageInterface $configurable_language) {
63     return ['#markup' => $this->t('Loaded %label.', ['%label' => $configurable_language->label()])];
64   }
65
66   /**
67    * Returns links to the current page with different langcodes.
68    *
69    * Using #type 'link' causes these links to be rendered with the link
70    * generator.
71    */
72   public function typeLinkActiveClass() {
73     // We assume that 'en' and 'fr' have been configured.
74     $languages = $this->languageManager->getLanguages();
75     return [
76       'no_language' => [
77         '#type' => 'link',
78         '#title' => t('Link to the current path with no langcode provided.'),
79         '#url' => Url::fromRoute('<current>'),
80         '#options' => [
81           'attributes' => [
82             'id' => 'no_lang_link',
83           ],
84           'set_active_class' => TRUE,
85         ],
86       ],
87       'fr' => [
88         '#type' => 'link',
89         '#title' => t('Link to a French version of the current path.'),
90         '#url' => Url::fromRoute('<current>'),
91         '#options' => [
92           'language' => $languages['fr'],
93           'attributes' => [
94             'id' => 'fr_link',
95           ],
96           'set_active_class' => TRUE,
97         ],
98       ],
99       'en' => [
100         '#type' => 'link',
101         '#title' => t('Link to an English version of the current path.'),
102         '#url' => Url::fromRoute('<current>'),
103         '#options' => [
104           'language' => $languages['en'],
105           'attributes' => [
106             'id' => 'en_link',
107           ],
108           'set_active_class' => TRUE,
109         ],
110       ],
111     ];
112   }
113
114   /**
115    * Uses a sub request to retrieve the 'user' page.
116    *
117    * @return \Symfony\Component\HttpFoundation\Response
118    *   The kernels response to the sub request.
119    */
120   public function testSubRequest() {
121     $request = Request::createFromGlobals();
122     $server = $request->server->all();
123     if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
124       // We need this for when the test is executed by run-tests.sh.
125       // @todo Remove this once run-tests.sh has been converted to use a Request
126       //   object.
127       $server['SCRIPT_FILENAME'] = $server['SCRIPT_NAME'];
128       $base_path = ltrim($server['REQUEST_URI'], '/');
129     }
130     else {
131       $base_path = $request->getBasePath();
132     }
133     $sub_request = Request::create($base_path . '/user', 'GET', $request->query->all(), $request->cookies->all(), [], $server);
134     return $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
135   }
136
137 }