Skip to content

Instantly share code, notes, and snippets.

@tmotyl
Last active December 1, 2016 22:50
Show Gist options
  • Save tmotyl/db3238451616f2c22680f081ec0571d0 to your computer and use it in GitHub Desktop.
Save tmotyl/db3238451616f2c22680f081ec0571d0 to your computer and use it in GitHub Desktop.
Add new column to TYPO3 functional tests CSV dataset
<?php
/**
* @cli
*/
public function addl18noriguidToDatasetsCommand()
{
$column = 'l10n_origuid';
$startPath = 'typo3/sysext/core/Tests/Functional/DataHandling/Regular/';
$absStartPath = PATH_site . $startPath;
$Directory = new \RecursiveDirectoryIterator($absStartPath);
$Iterator = new \RecursiveIteratorIterator($Directory);
$Regex = new \RegexIterator($Iterator, '/^.+\.csv$/i', \RecursiveRegexIterator::GET_MATCH);
$tablesToProcess = [
// 'pages' => 'deleted',
'tt_content' => 'l18n_parent',
// 'sys_category' => 'l10n_parent',
// 'tx_irretutorial_1ncsv_hotel' => 'l18n_parent',
// 'tx_irretutorial_1ncsv_offer' => 'l18n_parent',
// 'tx_irretutorial_1ncsv_price' => 'l18n_parent',
// 'tx_irretutorial_1nff_hotel' => 'l18n_parent',
// 'tx_irretutorial_1nff_offer' => 'l18n_parent',
// 'tx_irretutorial_1nff_price' => 'l18n_parent',
// 'tx_testdatahandler_element' => 'l10n_parent',
// 'sys_file_reference' => 'l10n_parent',
];
foreach ($Regex as $file) {
$filePath = $file[0];
$dataSet = DataSet::read($filePath);
foreach ($tablesToProcess as $table => $columnAfter) {
if (isset($dataSet->data[$table]['fields'])) {
//do not add the column twice
if (isset($dataSet->data[$table]['fields'][$column])) {
continue;
}
$dataSet->data[$table]['fields'] = $this->insertAfterValue($dataSet->data[$table]['fields'], $columnAfter, $column);
foreach ($dataSet->data[$table]['elements'] as $uid => $row) {
$l10nOrigUidValue = isset($row['l10n_parent']) && intval($row['l10n_parent']) > 0 ? $row['l10n_parent'] : "0";
$l10nOrigUidValue = $l10nOrigUidValue > 0 && isset($row['l18n_parent']) && intval($row['l18n_parent']) > 0 ? $row['l18n_parent'] : "0";
$dataSet->data[$table]['elements'][$uid] = $this->insertAfterKey($row, $columnAfter, $column, $l10nOrigUidValue);
}
}
}
$dataSet->persist($filePath);
}
}
function insertAfterValue($array, $after, $val)
{
$new = array();
foreach ($array as $item) {
$new[] = $item;
if ($item == $after) {
$new[] = $val;
}
}
return $new;
}
function insertAfterKey($array, $after, $newKey, $val)
{
$new = array();
foreach ($array as $key => $item) {
$new[$key] = $item;
if ($key == $after) {
$new[$newKey] = $val;
}
}
return $new;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment