SystemTestController.php 6.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\system_test\Controller\SystemTestController.
 */

namespace Drupal\system_test\Controller;

use Drupal\Core\Controller\ControllerBase;
11 12
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
13 14
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
15 16
use Drupal\Core\Lock\LockBackendInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
17 18 19 20 21 22

/**
 * Controller routines for system_test routes.
 */
class SystemTestController extends ControllerBase {

23 24 25 26 27 28 29
  /**
   * The lock service.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

30 31 32 33 34 35 36 37 38 39
  /**
   * The persistent lock service.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $persistentLock;

  /**
   * Constructs the SystemTestController.
   *
40 41
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock service.
42 43 44
   * @param \Drupal\Core\Lock\LockBackendInterface $persistent_lock
   *   The persistent lock service.
   */
45 46
  public function __construct(LockBackendInterface $lock, LockBackendInterface $persistent_lock) {
    $this->lock = $lock;
47 48 49 50 51 52 53
    $this->persistentLock = $persistent_lock;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
54
    return new static($container->get('lock'), $container->get('lock.persistent'));
55 56
  }

57 58 59 60 61 62 63
  /**
   * Tests main content fallback.
   *
   * @return string
   *   The text to display.
   */
  public function mainContentFallback() {
64
    return ['#markup' => $this->t('Content to test main content fallback')];
65 66
  }

67 68 69 70 71 72 73 74 75 76 77 78 79
  /**
   * Tests setting messages and removing one before it is displayed.
   *
   * @return string
   *   Empty string, we just test the setting of messages.
   */
  public function drupalSetMessageTest() {
    // Set two messages.
    drupal_set_message('First message (removed).');
    drupal_set_message('Second message (not removed).');

    // Remove the first.
    unset($_SESSION['messages']['status'][0]);
80 81 82 83 84 85 86

    // Duplicate message check.
    drupal_set_message('Non Duplicated message', 'status', FALSE);
    drupal_set_message('Non Duplicated message', 'status', FALSE);

    drupal_set_message('Duplicated message', 'status', TRUE);
    drupal_set_message('Duplicated message', 'status', TRUE);
87
    return [];
88 89
  }

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  /**
   * Controller to return $_GET['destination'] for testing.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response.
   */
  public function getDestination(Request $request) {
    $response = new Response($request->query->get('destination'));
    return $response;
  }

  /**
   * Controller to return $_REQUEST['destination'] for testing.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response.
   */
  public function requestDestination(Request $request) {
    $response = new Response($request->request->get('destination'));
    return $response;
  }

118
  /**
119
   * Try to acquire a named lock and report the outcome.
120 121
   */
  public function lockAcquire() {
122 123 124 125 126 127 128
    if ($this->lock->acquire('system_test_lock_acquire')) {
      $this->lock->release('system_test_lock_acquire');
      return ['#markup' => 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()'];
    }
    else {
      return ['#markup' => 'FALSE: Lock not acquired in \Drupal\system_test\Controller\SystemTestController::lockAcquire()'];
    }
129 130 131
  }

  /**
132
   * Try to acquire a specific lock, and then exit.
133 134
   */
  public function lockExit() {
135 136 137 138 139 140 141 142
    if ($this->lock->acquire('system_test_lock_exit', 900)) {
      echo 'TRUE: Lock successfully acquired in \Drupal\system_test\Controller\SystemTestController::lockExit()';
      // The shut-down function should release the lock.
      exit();
    }
    else {
      return ['#markup' => 'FALSE: Lock not acquired in system_test_lock_exit()'];
    }
143 144
  }

145 146 147 148 149 150 151 152 153 154 155
  /**
   * Creates a lock that will persist across requests.
   *
   * @param string $lock_name
   *   The name of the persistent lock to acquire.
   *
   * @return string
   *   The text to display.
   */
  public function lockPersist($lock_name) {
    if ($this->persistentLock->acquire($lock_name)) {
156
      return ['#markup' => 'TRUE: Lock successfully acquired in SystemTestController::lockPersist()'];
157 158
    }
    else {
159
      return ['#markup' => 'FALSE: Lock not acquired in SystemTestController::lockPersist()'];
160 161 162
    }
  }

163 164 165
  /**
   * Set cache tag on on the returned render array.
   */
166
  public function system_test_cache_tags_page() {
167
    $build['main'] = array(
168
      '#cache' => array('tags' => array('system_test_cache_tags_page')),
169 170 171 172 173 174
      '#pre_render' => array(
        '\Drupal\system_test\Controller\SystemTestController::preRenderCacheTags',
      ),
      'message' => array(
        '#markup' => 'Cache tags page example',
      ),
175 176 177 178
    );
    return $build;
  }

179 180 181 182
  /**
   * Sets a cache tag on an element to help test #pre_render and cache tags.
   */
  public static function preRenderCacheTags($elements) {
183
    $elements['#cache']['tags'][] = 'pre_render';
184 185 186
    return $elements;
  }

187
  /**
188 189 190
   * Initialize authorize.php during testing.
   *
   * @see system_authorized_init().
191 192
   */
  public function authorizeInit($page_title) {
193 194 195
    $authorize_url = Url::fromUri('base:core/authorize.php', array('absolute' => TRUE))->toString();
    system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
    return new RedirectResponse($authorize_url);
196 197 198
  }

  /**
199
   * Sets a header.
200
   */
201 202 203 204 205 206 207
  public function setHeader(Request $request) {
    $query = $request->query->all();
    $response = new Response();
    $response->headers->set($query['name'], $query['value']);
    $response->setContent($this->t('The following header was set: %name: %value', array('%name' => $query['name'], '%value' => $query['value'])));

    return $response;
208 209 210
  }

  /**
211
   * A simple page callback which adds a register shutdown function.
212 213
   */
  public function shutdownFunctions($arg1, $arg2) {
214
    drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
215 216 217 218 219 220
    // If using PHP-FPM then fastcgi_finish_request() will have been fired
    // preventing further output to the browser which means that the escaping of
    // the exception message can not be tested.
    // @see _drupal_shutdown_function()
    // @see \Drupal\system\Tests\System\ShutdownFunctionsTest
    if (function_exists('fastcgi_finish_request')) {
221
      return ['#markup' => 'The function fastcgi_finish_request exists when serving the request.'];
222
    }
223
    return [];
224 225
  }

226 227 228 229 230 231 232 233 234 235 236 237
  /**
   * Returns the title for system_test.info.yml's configure route.
   *
   * @param string $foo
   *   Any string for the {foo} slug.
   *
   * @return string
   */
  public function configureTitle($foo) {
    return 'Bar.' . $foo;
  }

238
}