1. <?php
  2.  
  3. function simpletest_automator_export_test($simpletest_automator) {
  4.   drupal_set_header('Content-Type: application/x-httpd-php');
  5.   drupal_set_header('Content-Disposition: attachment; filename="'. $simpletest_automator->file .'"');
  6.   $export[] = '<?php';
  7.   $export[] = '';
  8.   $export[] = '/**';
  9.   $export[] = ' * ' . $simpletest_automator->description;
  10.   $export[] = ' */';
  11.   $export[] = 'class ' . $simpletest_automator->class . ' extends DrupalWebTestCase {';
  12.   $export[] = '';
  13.   $export = array_merge($export, _simpletest_automator_export_initial($simpletest_automator));
  14.   $export = array_merge($export, _simpletest_automator_export_test($simpletest_automator));
  15.   $export[] = '}';
  16.   drupal_alter('simpletest_automator', $export);
  17.   $code = _simpletest_automator_export_to_code($export);
  18.   exit($code);
  19. }
  20.  
  21. function _simpletest_automator_export_initial($simpletest_automator) {
  22.   $export = array();
  23.   $export[] = 'function getInfo() {';
  24.   $export[] = 'return array(';
  25.   $export[] = array(
  26.     'value' => "'name' => %s,",
  27.     'arguments' => array($simpletest_automator->name),
  28.   );
  29.   $export[] = array(
  30.     'value' => "'description' => %s,",
  31.     'arguments' => array($simpletest_automator->description),
  32.   );
  33.   $export[] = array(
  34.     'value' => "'group' => %s,",
  35.     'arguments' => array($simpletest_automator->test_group),
  36.   );
  37.   $export[] = ');';
  38.   $export[] = '}';
  39.   $export[] = '';
  40.   $export[] = 'function setUp() {';
  41.   $export[] = array(
  42.     'value' =>  'return parent::setUp(' . implode(', ', array_fill(0, count($simpletest_automator->modules), '%s')) . ');',
  43.     'arguments' => $simpletest_automator->modules,
  44.   );
  45.   $export[] = '}';
  46.   $export[] = '';
  47.   return $export;
  48. }
  49.  
  50. function _simpletest_automator_export_test($simpletest_automator) {
  51.   $export = array();
  52.   $export[] = 'function ' . $simpletest_automator->method . '() {';
  53.   $export[] = array(
  54.     'value' => '$user = $this->drupalCreateUser(array(' . implode(', ', array_fill(0, count($simpletest_automator->permissions), '%s')) . '));',
  55.     'arguments' => $simpletest_automator->permissions,
  56.   );
  57.   $export[] = '$this->drupalLogin($user)';
  58.   foreach ($simpletest_automator->actions as $action) {
  59.     $export = array_merge($export, module_invoke_all('simpletest_automator_actions', $action, 'export'));
  60.   }
  61.   $export[] = '}';
  62.   return $export;
  63. }
  64.  
  65. function _simpletest_automator_export_php($simpletest_automator) {
  66.   unset($simpletest_automator->said);
  67.   unset($simpletest_automator->db_prefix);
  68.   foreach ($simpletest_automator->actions as $index => $action) {
  69.     unset($simpletest_automator->actions[$index]->aid);
  70.     unset($simpletest_automator->actions[$index]->said);
  71.   }
  72.   return serialize($simpletest_automator);
  73. }
  74.  
  75. /**
  76.  * Helper function for simpletest_automator_export_test(). Turns a simpletest
  77.  * automator export array into actual code.
  78.  *
  79.  * @param $export
  80.  *   The export array to convert.
  81.  * @return
  82.  *   The actual code.
  83.  */
  84. function _simpletest_automator_export_to_code($export) {
  85.   $code = '';
  86.   $indent = 0;
  87.   foreach ($export as $line) {
  88.     if (is_string($line)) {
  89.       $line = array('value' => $line);
  90.     }
  91.     $data = count_chars($line['value'], 1);
  92.     $increase = ($data[ord('{')] + $data[ord('(')]) - ($data[ord('}')] + $data[ord(')')]);
  93.     if ($increase < 0) {
  94.       $indent += $increase;
  95.     }
  96.     if (!empty($line['value'])) {
  97.       for ($i = 0; $i < $indent; $i++) {
  98.         $code .= '  ';
  99.       }
  100.     }
  101.     if ($increase > 0) {
  102.       $indent += $increase;
  103.     }
  104.     if (isset($line['arguments'])) {
  105.       foreach ($line['arguments'] as $key => $arg) {
  106.         $line['arguments'][$key] = var_export($arg, TRUE);
  107.       }
  108.       $code .= call_user_func_array('sprintf', array_merge(array($line['value']), $line['arguments']));
  109.     }
  110.     else {
  111.       $code .= $line['value'];
  112.     }
  113.     $code .= "\n";
  114.   }
  115.   return rtrim($code);
  116. }