TYPO3 CMS  TYPO3_6-2
FunctionalTestCase.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Tests;
3 
17 use \TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\Response;
18 
49 abstract class FunctionalTestCase extends BaseTestCase {
67  protected $coreExtensionsToLoad = array();
68 
89  protected $testExtensionsToLoad = array();
90 
119  protected $pathsToLinkInTestInstance = array();
120 
128 
153  protected $additionalFoldersToCreate = array();
154 
160  private $bootstrapUtility = NULL;
161 
168  protected function getInstanceIdentifier() {
170  }
171 
177  protected function getInstancePath() {
179  }
180 
188  public function setUp() {
189  if (!defined('ORIGINAL_ROOT')) {
190  $this->markTestSkipped('Functional tests must be called through phpunit on CLI');
191  }
192  $this->bootstrapUtility = new FunctionalTestCaseBootstrapUtility();
193  $this->bootstrapUtility->setUp(
194  get_class($this),
195  $this->coreExtensionsToLoad,
196  $this->testExtensionsToLoad,
197  $this->pathsToLinkInTestInstance,
198  $this->configurationToUseInTestInstance,
199  $this->additionalFoldersToCreate
200  );
201  }
202 
211  protected function getDatabaseConnection() {
212  return $GLOBALS['TYPO3_DB'];
213  }
214 
222  protected function setUpBackendUserFromFixture($userUid) {
223  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/be_users.xml');
224  $database = $this->getDatabaseConnection();
225  $userRow = $database->exec_SELECTgetSingleRow('*', 'be_users', 'uid = ' . (int)$userUid);
226 
228  $backendUser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
229  $sessionId = $backendUser->createSessionId();
230  $_COOKIE['be_typo_user'] = $sessionId;
231  $backendUser->id = $sessionId;
232  $backendUser->sendNoCacheHeaders = FALSE;
233  $backendUser->dontSetCookie = TRUE;
234  $backendUser->createUserSession($userRow);
235 
236  $GLOBALS['BE_USER'] = $backendUser;
237  $GLOBALS['BE_USER']->start();
238  if (!is_array($GLOBALS['BE_USER']->user) || !$GLOBALS['BE_USER']->user['uid']) {
239  throw new Exception(
240  'Can not initialize backend user',
241  1377095807
242  );
243  }
244  $GLOBALS['BE_USER']->backendCheckLogin();
245 
246  return $backendUser;
247  }
248 
256  protected function importDataSet($path) {
257  if (!is_file($path)) {
258  throw new Exception(
259  'Fixture file ' . $path . ' not found',
260  1376746261
261  );
262  }
263 
264  $database = $this->getDatabaseConnection();
265 
266  $fileContent = file_get_contents($path);
267  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
268  $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
269  $xml = simplexml_load_string($fileContent);
270  libxml_disable_entity_loader($previousValueOfEntityLoader);
271  $foreignKeys = array();
272 
274  foreach ($xml->children() as $table) {
275  $insertArray = array();
276 
278  foreach ($table->children() as $column) {
279  $columnName = $column->getName();
280  $columnValue = NULL;
281 
282  if (isset($column['ref'])) {
283  list($tableName, $elementId) = explode('#', $column['ref']);
284  $columnValue = $foreignKeys[$tableName][$elementId];
285  } elseif (isset($column['is-NULL']) && ($column['is-NULL'] === 'yes')) {
286  $columnValue = NULL;
287  } else {
288  $columnValue = (string) $table->$columnName;
289  }
290 
291  $insertArray[$columnName] = $columnValue;
292  }
293 
294  $tableName = $table->getName();
295  $result = $database->exec_INSERTquery($tableName, $insertArray);
296  if ($result === FALSE) {
297  throw new Exception(
298  'Error when processing fixture file: ' . $path . ' Can not insert data to table ' . $tableName,
299  1376746262
300  );
301  }
302  if (isset($table['id'])) {
303  $elementId = (string) $table['id'];
304  $foreignKeys[$tableName][$elementId] = $database->sql_insert_id();
305  }
306  }
307  }
308 
313  protected function setUpFrontendRootPage($pageId, array $typoScriptFiles = array()) {
314  $pageId = (int)$pageId;
315  $page = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'pages', 'uid=' . $pageId);
316 
317  if (empty($page)) {
318  $this->fail('Cannot set up frontend root page "' . $pageId . '"');
319  }
320 
321  $pagesFields = array(
322  'is_siteroot' => 1
323  );
324 
325  $this->getDatabaseConnection()->exec_UPDATEquery('pages', 'uid=' . $pageId, $pagesFields);
326 
327  $templateFields = array(
328  'pid' => $pageId,
329  'title' => '',
330  'config' => '',
331  'clear' => 3,
332  'root' => 1,
333  );
334 
335  foreach ($typoScriptFiles as $typoScriptFile) {
336  $templateFields['config'] .= '<INCLUDE_TYPOSCRIPT: source="FILE:' . $typoScriptFile . '">' . LF;
337  }
338 
339  $this->getDatabaseConnection()->exec_INSERTquery('sys_template', $templateFields);
340  }
341 
350  protected function getFrontendResponse($pageId, $languageId = 0, $backendUserId = 0, $workspaceId = 0, $failOnFailure = TRUE) {
351  $pageId = (int)$pageId;
352  $languageId = (int)$languageId;
353 
354  $additionalParameter = '';
355  if (!empty($backendUserId)) {
356  $additionalParameter .= '&backendUserId=' . (int)$backendUserId;
357  }
358  if (!empty($workspaceId)) {
359  $additionalParameter .= '&workspaceId=' . (int)$workspaceId;
360  }
361 
362  $arguments = array(
363  'documentRoot' => $this->getInstancePath(),
364  'requestUrl' => 'http://localhost/?id=' . $pageId . '&L=' . $languageId . $additionalParameter,
365  );
366 
367  $template = new \Text_Template(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/request.tpl');
368  $template->setVar(
369  array(
370  'arguments' => var_export($arguments, TRUE),
371  'originalRoot' => ORIGINAL_ROOT,
372  )
373  );
374 
375  $php = \PHPUnit_Util_PHP::factory();
376  $response = $php->runJob($template->render());
377  $result = json_decode($response['stdout'], TRUE);
378 
379  if ($result === NULL) {
380  $this->fail('Frontend Response is empty');
381  }
382 
383  if ($failOnFailure && $result['status'] === Response::STATUS_Failure) {
384  $this->fail('Frontend Response has failure:' . LF . $result['error']);
385  }
386 
387  $response = new Response($result['status'], $result['content'], $result['error']);
388  return $response;
389  }
390 
391 }
setUpFrontendRootPage($pageId, array $typoScriptFiles=array())
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
$database
Definition: server.php:38
getFrontendResponse($pageId, $languageId=0, $backendUserId=0, $workspaceId=0, $failOnFailure=TRUE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]