[TASK] Add framework for frontend rendering functional tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Framework / Frontend / RenderElement.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 use \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
28
29 /**
30 * Model of rendered content elements
31 */
32 class RenderElement {
33
34 /**
35 * @var array
36 */
37 protected $recordData;
38
39 /**
40 * @var string
41 */
42 protected $recordIdentifier;
43
44 /**
45 * @var string
46 */
47 protected $recordTableName;
48
49 /**
50 * @var array|RenderLevel[]
51 */
52 protected $levels;
53
54 /**
55 * @var array
56 */
57 protected $expectedTableNames = array();
58
59 /**
60 * @var array
61 */
62 protected $queries = array();
63
64 /**
65 * @param ContentObjectRenderer $contentObjectRenderer
66 * @return RenderElement
67 */
68 public static function create(ContentObjectRenderer $contentObjectRenderer) {
69 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
70 'TYPO3\\CMS\\Core\\Tests\\Functional\\Framework\\Frontend\\RenderElement',
71 $contentObjectRenderer
72 );
73 }
74
75 /**
76 * @param ContentObjectRenderer $contentObjectRenderer
77 */
78 public function __construct(ContentObjectRenderer $contentObjectRenderer) {
79 $this->recordIdentifier = $contentObjectRenderer->currentRecord;
80 list($this->recordTableName) = explode(':', $this->recordIdentifier);
81 $this->recordData = $contentObjectRenderer->data;
82 }
83
84 /**
85 * @param ContentObjectRenderer $contentObjectRenderer
86 * @return RenderLevel
87 */
88 public function add(ContentObjectRenderer $contentObjectRenderer) {
89 $level = RenderLevel::create($contentObjectRenderer);
90 $level->setParentRecordIdentifier($this->recordIdentifier);
91 $this->levels[] = $level;
92 return $level;
93 }
94
95 /**
96 * @return string
97 */
98 public function getRecordIdentifier() {
99 return $this->recordIdentifier;
100 }
101
102 /**
103 * @param string $expectedTableName
104 */
105 public function addExpectedTableName($expectedTableName) {
106 if (!$this->hasExpectedTableName($expectedTableName)) {
107 $this->expectedTableNames[] = $expectedTableName;
108 }
109 }
110
111 /**
112 * @param string $tableName
113 * @return bool
114 */
115 public function hasExpectedTableName($tableName) {
116 if (in_array($tableName, $this->expectedTableNames)) {
117 return TRUE;
118 }
119 // Handling JOIN constructions
120 // e.g. "sys_category JOIN sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid"
121 foreach ($this->getExpectedTableNames() as $expectedTableName) {
122 if (strpos($tableName, $expectedTableName . ' ') === 0) {
123 return TRUE;
124 }
125 }
126 return FALSE;
127 }
128
129 /**
130 * @return array
131 */
132 public function getExpectedTableNames() {
133 return $this->expectedTableNames;
134 }
135
136 /**
137 * @param string $query
138 * @param string $fromTable
139 */
140 public function addQuery($query, $fromTable) {
141 if (empty($this->expectedTableNames) || $this->hasExpectedTableName($fromTable)) {
142 $this->queries[] = $query;
143 }
144 }
145
146 /**
147 * @param ContentObjectRenderer $contentObjectRenderer
148 * @return NULL|RenderLevel
149 */
150 public function findRenderLevel(ContentObjectRenderer $contentObjectRenderer) {
151 if (empty($this->levels)) {
152 return NULL;
153 }
154
155 foreach ($this->levels as $level) {
156 $result = $level->findRenderLevel($contentObjectRenderer);
157 if ($result !== NULL) {
158 return $result;
159 }
160 }
161
162 return NULL;
163 }
164
165 /**
166 * @param NULL|array $tableFields
167 * @return array
168 */
169 public function getRecordData(array $tableFields = NULL) {
170 $recordData = $this->recordData;
171
172 if (!empty($tableFields[$this->recordTableName])) {
173 $recordData = array_intersect_key(
174 $recordData,
175 array_flip($tableFields[$this->recordTableName])
176 );
177 }
178
179 return $recordData;
180 }
181
182 /**
183 * @param NULL|array $tableFields
184 * @return array
185 */
186 public function structureData(array $tableFields = NULL) {
187 $data = array(
188 $this->recordIdentifier => $this->getRecordData($tableFields)
189 );
190
191 foreach ($this->levels as $level) {
192 $parentRecordIdentifier = $level->getParentRecordIdentifier();
193 $parentRecordField = $level->getParentRecordField();
194
195 foreach ($level->getElements() as $element) {
196 if (empty($parentRecordIdentifier) || empty($parentRecordField) || !isset($data[$parentRecordIdentifier])) {
197 $data = array_merge($data, $element->structureData($tableFields));
198 continue;
199 }
200
201 if (!isset($data[$parentRecordIdentifier][$parentRecordField]) || !is_array($data[$parentRecordIdentifier][$parentRecordField])) {
202 $data[$parentRecordIdentifier][$parentRecordField] = array();
203 }
204
205 $data[$parentRecordIdentifier][$parentRecordField] = array_merge(
206 $data[$parentRecordIdentifier][$parentRecordField],
207 $element->structureData($tableFields)
208 );
209 }
210 }
211
212 return $data;
213 }
214
215 /**
216 * @param NULL|array $tableFields
217 * @return array
218 */
219 public function mergeData(array $tableFields = NULL) {
220 $data = array(
221 $this->recordIdentifier => $this->getRecordData($tableFields),
222 );
223 foreach ($this->levels as $level) {
224 $data = array_merge($data, $level->mergeData($tableFields));
225 }
226 return $data;
227 }
228
229 /**
230 * @return array
231 */
232 public function mergeQueries() {
233 $queries = $this->queries;
234 foreach ($this->levels as $level) {
235 $queries = array_merge($queries, $level->mergeQueries());
236 }
237 return $queries;
238 }
239
240 }