c78940173cbf6d05dc84ba735393b9fa5e6946c6
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Framework / Frontend / RequestBootstrap.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\Framework\Frontend;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Bootstrap for direct CLI Request
29 */
30 class RequestBootstrap {
31
32 /**
33 * @return void
34 */
35 static public function setGlobalVariables(array $requestArguments = NULL) {
36 if (empty($requestArguments)) {
37 die('No JSON encoded arguments given');
38 }
39
40 if (empty($requestArguments['documentRoot'])) {
41 die('No documentRoot given');
42 }
43
44 if (empty($requestArguments['requestUrl']) || ($requestUrlParts = parse_url($requestArguments['requestUrl'])) === FALSE) {
45 die('No valid request URL given');
46 }
47
48 // Populating $_GET and $_REQUEST is query part is set:
49 if (isset($requestUrlParts['query'])) {
50 parse_str($requestUrlParts['query'], $_GET);
51 parse_str($requestUrlParts['query'], $_REQUEST);
52 }
53
54 // Populating $_POST
55 $_POST = array();
56 // Populating $_COOKIE
57 $_COOKIE = array();
58
59 // Setting up the server environment
60 $_SERVER = array();
61 $_SERVER['DOCUMENT_ROOT'] = $requestArguments['documentRoot'];
62 $_SERVER['HTTP_USER_AGENT'] = 'TYPO3 Functional Test Request';
63 $_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] = $requestUrlParts['host'];
64 $_SERVER['SERVER_ADDR'] = $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
65 $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = '/index.php';
66 $_SERVER['SCRIPT_FILENAME'] = $_SERVER['_'] = $_SERVER['PATH_TRANSLATED'] = $requestArguments['documentRoot'] . '/index.php';
67 $_SERVER['QUERY_STRING'] = (isset($requestUrlParts['query']) ? $requestUrlParts['query'] : '');
68 $_SERVER['REQUEST_URI'] = $requestUrlParts['path'] . (isset($requestUrlParts['query']) ? '?' . $requestUrlParts['query'] : '');
69 $_SERVER['REQUEST_METHOD'] = 'GET';
70
71 // Define a port if used in the URL:
72 if (isset($requestUrlParts['port'])) {
73 $_SERVER['SERVER_PORT'] = $requestUrlParts['port'];
74 }
75 // Define HTTPS disposal:
76 if ($requestUrlParts['scheme'] === 'https') {
77 $_SERVER['HTTPS'] = 'on';
78 }
79
80 if (!is_dir($_SERVER['DOCUMENT_ROOT'])) {
81 die('Document root directory "' . $_SERVER['SCRIPT_FILENAME'] . '" does not exist');
82 }
83
84 if (!is_file($_SERVER['SCRIPT_FILENAME'])) {
85 die('Script file "' . $_SERVER['SCRIPT_FILENAME'] . '" does not exist');
86 }
87 }
88
89 /**
90 * @return void
91 */
92 static public function executeAndOutput() {
93 global $TT, $TSFE, $TYPO3_CONF_VARS, $BE_USER, $TYPO3_MISC;
94
95 $result = array('status' => 'failure', 'content' => NULL, 'error' => NULL);
96
97 ob_start();
98 try {
99 chdir($_SERVER['DOCUMENT_ROOT']);
100 include($_SERVER['SCRIPT_FILENAME']);
101 $result['status'] = 'success';
102 $result['content'] = ob_get_contents();
103 } catch(\Exception $exception) {
104 $result['error'] = $exception->__toString();
105 }
106 ob_end_clean();
107
108 echo json_encode($result);
109 }
110
111 }