c446972bc2cfbc6cc3c805c800a0e85341297ead
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Tests / Functional / Export / AbstractExportTestCase.php
1 <?php
2 namespace TYPO3\CMS\Impexp\Tests\Functional\Export;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\QueryHelper;
19 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Functional test for the Export
24 */
25 abstract class AbstractExportTestCase extends \TYPO3\CMS\Components\TestingFramework\Core\FunctionalTestCase
26 {
27 /**
28 * @var array
29 */
30 protected $coreExtensionsToLoad = ['impexp'];
31
32 /**
33 * @var \TYPO3\CMS\Impexp\Export
34 */
35 protected $export;
36
37 /**
38 * Set up for set up the backend user, initialize the language object
39 * and creating the Export instance
40 *
41 * @return void
42 */
43 protected function setUp()
44 {
45 parent::setUp();
46
47 $this->setUpBackendUserFromFixture(1);
48
49 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeLanguageObject();
50
51 $this->export = GeneralUtility::makeInstance(\TYPO3\CMS\Impexp\Export::class);
52 $this->export->init(0, 'export');
53 }
54
55 /**
56 * Builds a flat array containing the page tree with the PageTreeView
57 * based on given start pid and depth and set it in the Export object.
58 *
59 * @param int $pidToStart
60 * @param int $depth
61 * @return void
62 */
63 protected function setPageTree($pidToStart, $depth = 1)
64 {
65 $permsClause = $GLOBALS['BE_USER']->getPagePermsClause(1);
66
67 /** @var $tree \TYPO3\CMS\Backend\Tree\View\PageTreeView */
68 $tree = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Tree\View\PageTreeView::class);
69 $tree->init('AND ' . $permsClause);
70 $tree->tree[] = ['row' => $pidToStart];
71 $tree->buffer_idH = [];
72 if ($depth > 0) {
73 $tree->getTree($pidToStart, $depth, '');
74 }
75
76 $idH[$pidToStart]['uid'] = $pidToStart;
77 if (!empty($tree->buffer_idH)) {
78 $idH[$pidToStart]['subrow'] = $tree->buffer_idH;
79 }
80
81 $this->export->setPageTree($idH);
82 }
83
84 /**
85 * Adds records to the export object for a specific page id.
86 *
87 * @param int $pid Page id for which to select records to add
88 * @param array $tables Array of table names to select from
89 * @return void
90 */
91 protected function addRecordsForPid($pid, array $tables)
92 {
93 foreach ($GLOBALS['TCA'] as $table => $value) {
94 if ($table != 'pages' && (in_array($table, $tables) || in_array('_ALL', $tables))) {
95 if ($GLOBALS['BE_USER']->check('tables_select', $table) && !$GLOBALS['TCA'][$table]['ctrl']['is_static']) {
96 $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?: $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
97
98 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
99 ->getQueryBuilderForTable($table);
100
101 $queryBuilder->getRestrictions()
102 ->removeAll()
103 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
104
105 $queryBuilder
106 ->select('*')
107 ->from($table)
108 ->where(
109 $queryBuilder->expr()->eq(
110 'pid',
111 $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
112 )
113 );
114
115 foreach (QueryHelper::parseOrderBy((string)$orderBy) as $orderPair) {
116 list($fieldName, $order) = $orderPair;
117 $queryBuilder->addOrderBy($fieldName, $order);
118 }
119
120 $result = $queryBuilder->execute();
121 while ($row = $result->fetch()) {
122 $this->export->export_addRecord($table, $row);
123 }
124 }
125 }
126 }
127 }
128 }