function setUp() { parent::setUp(); TestDataController::$data_dir = 'testdata/tests'; $this->envType = Director::get_environment_type(); Director::set_environment_type('test'); TestDataController::$quiet = true; }
/** * Processes the form and builds the output */ function export($data, $form) { increase_time_limit_to(600); $ymlDest = BASE_PATH . '/' . TestDataController::get_data_dir(); // If we are reexporting, override all the other settings in the form from the file. if (isset($data['Reexport']) && $data['Reexport']) { $data['Reexport'] = preg_replace('/[^a-z0-9\\-_\\.]/', '', $data['Reexport']); $params = $this->extractParams(file_get_contents($ymlDest . $data['Reexport'])); $data = array_merge($data, $params); } // Simple validation if (!isset($data['FileName']) || !$data['FileName']) { echo "Specify file name."; exit; } if (!isset($data['Class'])) { echo "Pick some classes."; exit; } $this->currentFixtureFile = $data['FileName']; // Here we will collect all the output. $output = ''; // We want to work off Draft, because that's what's visible in the CMS. Versioned::reading_stage('Stage'); // Prepare the filesystem. @mkdir($ymlDest); if (isset($data['IncludeFiles']) && $data['IncludeFiles']) { $fileDest = BASE_PATH . '/' . TestDataController::get_data_dir() . 'files'; @mkdir($fileDest); } // Variables: // Queue for outstanding records to process. $queue = array(); // Buckets for sorting the resulting records by class name. $buckets = array(); // Populate the queue of DataObjects to be exported foreach ($data['Class'] as $dataObjectName => $checked) { $class = singleton($dataObjectName); if (!$class) { continue; } // Apply the ID filter, if present. if (isset($data['Range']) && isset($data['Range'][$dataObjectName]) && $data['Range'][$dataObjectName]) { $where = $this->generateIDs($data['Range'][$dataObjectName], $dataObjectName); } $objects = $class::get(); if (isset($where)) { $objects = $objects->where($where); } foreach ($objects as $object) { array_push($queue, $object); } } // Collect all the requested objects (and related objects) into buckets. while ($object = array_shift($queue)) { // Generate and attach the unique YML tag $this->assureHasTag($object); // Add the object to the relevant bucket (e.g. "Page") by ID $buckets[$object->ClassName][$object->YMLTag] = $object; // If traversing has been enabled if (isset($data['TraverseRelations']) && $data['TraverseRelations']) { $this->traverseRelations($object, $buckets, $queue); } // If files should be included if (isset($data['IncludeFiles']) && $data['IncludeFiles']) { if ($object instanceof File && $object->ClassName != 'Folder') { $source = $object->getFullPath(); if ($source) { @copy($source, "{$fileDest}/{$object->Name}"); echo "Processing {$source} to {$fileDest}/{$object->Name}.<br>\n"; } } } } // Sort the buckets so the records can be updated in place ksort($buckets, SORT_STRING); foreach ($buckets as $key => $bucket) { ksort($bucket, SORT_STRING); $buckets[$key] = $bucket; } // Write objects. foreach ($buckets as $name => $bucket) { // Write the bucket YML heading (object class) $output .= "{$name}:\n"; foreach ($bucket as $dataObject) { $output .= $this->generateYML($dataObject, $buckets); } } // Dump metadata global $database; $output .= "\n#db={$database}"; $output .= "\n#time=" . date('Y-m-d H:i:s'); $output .= "\n#params=" . json_encode($data); // Output the written data into a file specified in the form. $file = $ymlDest . preg_replace('/[^a-z0-9\\-_\\.]/', '', $this->currentFixtureFile); file_put_contents($file, $output); echo "File {$file} written.<br>\n"; }