3 namespace Drupal\system_test\Controller;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Cache\CacheableResponse;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Render\RendererInterface;
9 use Drupal\Core\Render\Markup;
10 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\HttpFoundation\RedirectResponse;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\Response;
15 use Drupal\Core\Lock\LockBackendInterface;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
19 * Controller routines for system_test routes.
21 class SystemTestController extends ControllerBase {
26 * @var \Drupal\Core\Lock\LockBackendInterface
31 * The persistent lock service.
33 * @var \Drupal\Core\Lock\LockBackendInterface
35 protected $persistentLock;
40 * @var \Drupal\Core\Session\AccountInterface
42 protected $currentUser;
47 * @var \Drupal\Core\Render\RendererInterface
52 * Constructs the SystemTestController.
54 * @param \Drupal\Core\Lock\LockBackendInterface $lock
56 * @param \Drupal\Core\Lock\LockBackendInterface $persistent_lock
57 * The persistent lock service.
58 * @param \Drupal\Core\Session\AccountInterface $current_user
60 * @param \Drupal\Core\Render\RendererInterface $renderer
63 public function __construct(LockBackendInterface $lock, LockBackendInterface $persistent_lock, AccountInterface $current_user, RendererInterface $renderer) {
65 $this->persistentLock = $persistent_lock;
66 $this->currentUser = $current_user;
67 $this->renderer = $renderer;
73 public static function create(ContainerInterface $container) {
75 $container->get('lock'),
76 $container->get('lock.persistent'),
77 $container->get('current_user'),
78 $container->get('renderer')
83 * Tests main content fallback.
86 * The text to display.
88 public function mainContentFallback() {
89 return ['#markup' => $this->t('Content to test main content fallback')];
93 * Tests setting messages and removing one before it is displayed.
96 * Empty string, we just test the setting of messages.
98 public function drupalSetMessageTest() {
100 drupal_set_message('First message (removed).');
101 drupal_set_message(t('Second message with <em>markup!</em> (not removed).'));
104 unset($_SESSION['messages']['status'][0]);
106 // Duplicate message check.
107 drupal_set_message('Non Duplicated message', 'status', FALSE);
108 drupal_set_message('Non Duplicated message', 'status', FALSE);
110 drupal_set_message('Duplicated message', 'status', TRUE);
111 drupal_set_message('Duplicated message', 'status', TRUE);
113 // Add a Markup message.
114 drupal_set_message(Markup::create('Markup with <em>markup!</em>'));
115 // Test duplicate Markup messages.
116 drupal_set_message(Markup::create('Markup with <em>markup!</em>'));
117 // Ensure that multiple Markup messages work.
118 drupal_set_message(Markup::create('Markup2 with <em>markup!</em>'));
120 // Test mixing of types.
121 drupal_set_message(Markup::create('Non duplicate Markup / string.'));
122 drupal_set_message('Non duplicate Markup / string.');
123 drupal_set_message(Markup::create('Duplicate Markup / string.'), 'status', TRUE);
124 drupal_set_message('Duplicate Markup / string.', 'status', TRUE);
126 // Test auto-escape of non safe strings.
127 drupal_set_message('<em>This<span>markup will be</span> escaped</em>.');
133 * Controller to return $_GET['destination'] for testing.
135 * @param \Symfony\Component\HttpFoundation\Request $request
138 * @return \Symfony\Component\HttpFoundation\Response
141 public function getDestination(Request $request) {
142 $response = new Response($request->query->get('destination'));
147 * Controller to return $_REQUEST['destination'] for testing.
149 * @param \Symfony\Component\HttpFoundation\Request $request
152 * @return \Symfony\Component\HttpFoundation\Response
155 public function requestDestination(Request $request) {
156 $response = new Response($request->request->get('destination'));
161 * Try to acquire a named lock and report the outcome.
163 public function lockAcquire() {
164 if ($this->lock->acquire('system_test_lock_acquire')) {
165 $this->lock->release('system_test_lock_acquire');
166 return ['#markup' => 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()'];
169 return ['#markup' => 'FALSE: Lock not acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()'];
174 * Try to acquire a specific lock, and then exit.
176 public function lockExit() {
177 if ($this->lock->acquire('system_test_lock_exit', 900)) {
178 echo 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockExit()';
179 // The shut-down function should release the lock.
183 return ['#markup' => 'FALSE: Lock not acquired in system_test_lock_exit()'];
188 * Creates a lock that will persist across requests.
190 * @param string $lock_name
191 * The name of the persistent lock to acquire.
194 * The text to display.
196 public function lockPersist($lock_name) {
197 if ($this->persistentLock->acquire($lock_name)) {
198 return ['#markup' => 'TRUE: Lock successfully acquired in SystemTestController::lockPersist()'];
201 return ['#markup' => 'FALSE: Lock not acquired in SystemTestController::lockPersist()'];
206 * Set cache tag on on the returned render array.
208 public function system_test_cache_tags_page() {
210 '#cache' => ['tags' => ['system_test_cache_tags_page']],
212 '\Drupal\system_test\Controller\SystemTestController::preRenderCacheTags',
215 '#markup' => 'Cache tags page example',
222 * Set cache max-age on the returned render array.
224 public function system_test_cache_maxage_page() {
226 '#cache' => ['max-age' => 90],
228 '#markup' => 'Cache max-age page example',
235 * Sets a cache tag on an element to help test #pre_render and cache tags.
237 public static function preRenderCacheTags($elements) {
238 $elements['#cache']['tags'][] = 'pre_render';
243 * Initialize authorize.php during testing.
245 * @see system_authorized_init()
247 public function authorizeInit($page_title) {
248 $authorize_url = Url::fromUri('base:core/authorize.php', ['absolute' => TRUE])->toString();
249 system_authorized_init('system_test_authorize_run', __DIR__ . '/../../system_test.module', [], $page_title);
250 return new RedirectResponse($authorize_url);
256 public function setHeader(Request $request) {
257 $query = $request->query->all();
258 $response = new CacheableResponse();
259 $response->headers->set($query['name'], $query['value']);
260 $response->getCacheableMetadata()->addCacheContexts(['url.query_args:name', 'url.query_args:value']);
261 $response->setContent($this->t('The following header was set: %name: %value', ['%name' => $query['name'], '%value' => $query['value']]));
267 * A simple page callback that uses a plain Symfony response object.
269 public function respondWithReponse(Request $request) {
270 return new Response('test');
274 * A plain Symfony reponse with Cache-Control: public, max-age=60.
276 public function respondWithPublicResponse() {
277 return (new Response('test'))->setPublic()->setMaxAge(60);
281 * A simple page callback that uses a CacheableResponse object.
283 public function respondWithCacheableReponse(Request $request) {
284 return new CacheableResponse('test');
288 * A simple page callback which adds a register shutdown function.
290 public function shutdownFunctions($arg1, $arg2) {
291 drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
292 // If using PHP-FPM then fastcgi_finish_request() will have been fired
293 // preventing further output to the browser which means that the escaping of
294 // the exception message can not be tested.
295 // @see _drupal_shutdown_function()
296 // @see \Drupal\system\Tests\System\ShutdownFunctionsTest
297 if (function_exists('fastcgi_finish_request')) {
298 return ['#markup' => 'The function fastcgi_finish_request exists when serving the request.'];
304 * Returns the title for system_test.info.yml's configure route.
307 * Any string for the {foo} slug.
311 public function configureTitle($foo) {
312 return 'Bar.' . $foo;
316 * Simple argument echo.
318 * @param string $text
319 * Any string for the {text} slug.
324 public function simpleEcho($text) {
326 '#plain_text' => $text,
331 * Shows permission-dependent content.
336 public function permissionDependentContent() {
339 // The content depends on the access result.
340 $access = AccessResult::allowedIfHasPermission($this->currentUser, 'pet llamas');
341 $this->renderer->addCacheableDependency($build, $access);
343 // Build the content.
344 if ($access->isAllowed()) {
345 $build['allowed'] = ['#markup' => 'Permission to pet llamas: yes!'];
348 $build['forbidden'] = ['#markup' => 'Permission to pet llamas: no!'];
355 * Returns the current date.
357 * @return \Symfony\Component\HttpFoundation\Response
358 * A Response object containing the current date.
360 public function getCurrentDate() {
361 // Uses specific time to test that the right timezone is used.
362 $response = new Response(\Drupal::service('date.formatter')->format(1452702549));
367 * Returns a response with a test header set from the request.
369 * @return \Symfony\Component\HttpFoundation\Response
370 * A Response object containing the test header.
372 public function getTestHeader(Request $request) {
373 $response = new Response();
374 $response->headers->set('Test-Header', $request->headers->get('Test-Header'));