<commit_before><?php

require_once( 'Light/Config.php' );
require_once( 'Light/Database/Object/Cache/Factory.php' );

require_once( 'CTM/Test/Param.php' );
require_once( 'CTM/Test/Param/Selector.php' );
require_once( 'CTM/Test/Run.php' );
require_once( 'CTM/Test/Command/Selector.php' );
require_once( 'CTM/Test/Run/BaseUrl.php' );
require_once( 'CTM/Test/Run/BaseUrl/Selector.php' );
require_once( 'CTM/Test/Run/Command.php' );
require_once( 'CTM/Test/Run/Command/Selector.php' );
require_once( 'CTM/Test/Suite/Plan/Selector.php' );

class CTM_Test_Run_Builder {
   private $_plan_type_cache;
   private $_test_suite_cache;
   private $_test_cache;
   private $_param_lib_cache;
   private $_test_run_baseurl_cache;
   private $_selenium_command_cache;

   private $_suite_name;
   private $_suite_dir;
   private $_suite_compressed_file;
   private $_suite_tests;
   private $_suite_test_id;

   function __construct() {

      $this->_plan_type_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Suite_Plan_Type_Cache' );
      $this->_test_suite_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Suite_Cache' );
      $this->_test_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Cache' );
      $this->_param_lib_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Param_Library_Cache' );
      $this->_test_run_baseurl_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Run_BaseUrl_Cache' );
      $this->_selenium_command_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Selenium_Command_Cache' );

      $this->_suite_name = null;
      $this->_suite_dir = null;
      $this->_suite_compressed_file = null;
      $this->_suite_tests = array();
      $this->_suite_test_id = 0;
   }

   public function build( CTM_Test_Run $test_run ) {

      try {

         // clear the existing run plan - if any.
         $this->_clearCurrentPlan( $test_run );
     
         // kick the build off with the parent suite
         $this->_addSuiteToPlan( $test_run, $test_run->test_suite_id );

         return true;

      } catch ( Exception $e ) {
         throw $e;
      }

   }

   public function buildTestSuite( CTM_Test_Run $test_run ) {
         try {
        
            // bring the suite in so we can get the name
            $test_suite = $this->_test_suite_cache->getById( $test_run->test_suite_id );

            $this->_suite_name = $test_suite->name;
            $this->_suite_dir = Light_Config::get('CTM_Config', 'SUITE_DIR' ) . '/' . $test_run->id;
            $this->_suite_compressed_file = $this->_suite_dir . '.zip';
            $this->_suite_tests = array();
            $this->_suite_test_id = 0;

            if ( is_dir( $this->_suite_dir ) ) {
               // cleanup the last try at building this out.
               system( 'rm -rf ' . $this->_suite_dir );
            }
            
            mkdir( $this->_suite_dir, 0755, true );

            if ( ! is_dir( $this->_suite_dir ) ) {
               return false;
            }

            if ( is_file( $this->_suite_compressed_file ) ) {
               unlink( $this->_suite_compressed_file );
            }

            // select the tests off the suite_plan stack.
            $this->_addTestSuiteToSuiteDir( $test_run, $test_run->test_suite_id );

            // write the final suite index.html
            $this->_writeSuiteHtml();

            // compress off the suite and make it easy for the agent to download.
            system( 'cd ' . Light_Config::get( 'CTM_Config', 'SUITE_DIR' ) . ' ; zip -q -r ' . $this->_suite_compressed_file . ' ' . $test_run->id . ' >/dev/null 2> /dev/null' );

         } catch ( Exception $e ) {
            throw $e;
         }

   }

   private function _addTestSuiteToSuiteDir( CTM_Test_Run $test_run, $test_suite_id ) {
      try {

         $sel = new CTM_Test_Suite_Plan_Selector();
         $and_params = array( new Light_Database_Selector_Criteria( 'test_suite_id', '=', $test_suite_id ) );
         $plan_steps = $sel->find( $and_params ); 
         
         if ( count( $plan_steps ) > 0 ) {
            foreach ( $plan_steps as $plan_step ) {
               // is the step a suite or a test?
               $plan_type = $this->_plan_type_cache->getById( $plan_step->test_suite_plan_type_id ); 
               
               // we have to exclude linking back to the parent for the run.. this prevents infinite
               // loops.
               if ( $plan_type->name == 'suite' && $plan_step->linked_id != $test_run->test_suite_id ) {
                  $this->_addTestSuiteToSuiteDir( $test_run, $plan_step->linked_id );
               } else if ( $plan_type->name == 'test' ) {
                  $this->_addTestToSuiteDir( $test_run, $plan_step->linked_id );
               }

            }
         } 
      } catch ( Exception $e ) {
         throw $e;
      }

   }

   private function _addTestToSuiteDir( CTM_Test_Run $test_run, $test_id ) {

      try {
         // fetch the test_obj
         $test_obj = $this->_test_cache->getById( $test_id );

         // fetch all the test commands
         $sel = new CTM_Test_Command_Selector();
         $and_params = array( new Light_Database_Selector_Criteria( 'test_id', '=', $test_id ) );
         $or_params = array();
         $order = array( 'id' );
         $test_commands = $sel->find( $and_params, $or_params, $order );

         // increment the _suite_test_id
         $this->_suite_test_id++;

         $this->_suite_tests[] = array(
               'suite_test_id' => $this->_suite_test_id,
               'test_obj' => $test_obj
         );

         $filename = $this->_suite_dir . '/' . $this->_suite_test_id . '.html';

         $fh = fopen( $filename, 'w' );

         if ( ! is_resource( $fh ) ) {
            return false;
         }

         // determine the baseurl for this test.
         $baseurl_obj = $this->_test_run_baseurl_cache->getByCompoundKey( $test_run->id, 0, $test_id );

         // eject the headers.
         fwrite($fh, '<?xml version="1.0" encoding="UTF-8"?>' . "\n" );
         fwrite($fh, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n" );
         fwrite($fh, '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' . "\n" );
         fwrite($fh, '<head profile="http://selenium-ide.openqa.org/profiles/test-case">' . "\n" );
         fwrite($fh, '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . "\n" );
         fwrite($fh, '<link rel="selenium.base" href="' . $baseurl_obj->cleanBaseUrl() . '" />' . "\n" ); 
         fwrite($fh, '<title>' . $this->_escapeVariable( $test_obj->name ) . '</title>' . "\n" );
         fwrite($fh, '</head>' . "\n" );
         fwrite($fh, '<body>' . "\n" );
         fwrite($fh, '<table cellpadding="1" cellspacing="1" border="1">' . "\n" );
         fwrite($fh, '<thead>' . "\n" );
         fwrite($fh, '<tr><td rowspan="1" colspan="3">' . $this->_escapeVariable( $test_obj->name ) . '</td></tr>' . "\n" );
         fwrite($fh, '</thead><tbody>' . "\n" );

         fwrite($fh, '<tr>' . "\n" );<commit_msg>- Removing the faulty meta tag for now. Working on a c# based runner   so I don't need to stay 100% accurate to their spec.<commit_after><?php

require_once( 'Light/Config.php' );
require_once( 'Light/Database/Object/Cache/Factory.php' );

require_once( 'CTM/Test/Param.php' );
require_once( 'CTM/Test/Param/Selector.php' );
require_once( 'CTM/Test/Run.php' );
require_once( 'CTM/Test/Command/Selector.php' );
require_once( 'CTM/Test/Run/BaseUrl.php' );
require_once( 'CTM/Test/Run/BaseUrl/Selector.php' );
require_once( 'CTM/Test/Run/Command.php' );
require_once( 'CTM/Test/Run/Command/Selector.php' );
require_once( 'CTM/Test/Suite/Plan/Selector.php' );

class CTM_Test_Run_Builder {
   private $_plan_type_cache;
   private $_test_suite_cache;
   private $_test_cache;
   private $_param_lib_cache;
   private $_test_run_baseurl_cache;
   private $_selenium_command_cache;

   private $_suite_name;
   private $_suite_dir;
   private $_suite_compressed_file;
   private $_suite_tests;
   private $_suite_test_id;

   function __construct() {

      $this->_plan_type_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Suite_Plan_Type_Cache' );
      $this->_test_suite_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Suite_Cache' );
      $this->_test_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Cache' );
      $this->_param_lib_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Param_Library_Cache' );
      $this->_test_run_baseurl_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Run_BaseUrl_Cache' );
      $this->_selenium_command_cache = Light_Database_Object_Cache_Factory::factory( 'CTM_Test_Selenium_Command_Cache' );

      $this->_suite_name = null;
      $this->_suite_dir = null;
      $this->_suite_compressed_file = null;
      $this->_suite_tests = array();
      $this->_suite_test_id = 0;
   }

   public function build( CTM_Test_Run $test_run ) {

      try {

         // clear the existing run plan - if any.
         $this->_clearCurrentPlan( $test_run );
     
         // kick the build off with the parent suite
         $this->_addSuiteToPlan( $test_run, $test_run->test_suite_id );

         return true;

      } catch ( Exception $e ) {
         throw $e;
      }

   }

   public function buildTestSuite( CTM_Test_Run $test_run ) {
         try {
        
            // bring the suite in so we can get the name
            $test_suite = $this->_test_suite_cache->getById( $test_run->test_suite_id );

            $this->_suite_name = $test_suite->name;
            $this->_suite_dir = Light_Config::get('CTM_Config', 'SUITE_DIR' ) . '/' . $test_run->id;
            $this->_suite_compressed_file = $this->_suite_dir . '.zip';
            $this->_suite_tests = array();
            $this->_suite_test_id = 0;

            if ( is_dir( $this->_suite_dir ) ) {
               // cleanup the last try at building this out.
               system( 'rm -rf ' . $this->_suite_dir );
            }
            
            mkdir( $this->_suite_dir, 0755, true );

            if ( ! is_dir( $this->_suite_dir ) ) {
               return false;
            }

            if ( is_file( $this->_suite_compressed_file ) ) {
               unlink( $this->_suite_compressed_file );
            }

            // select the tests off the suite_plan stack.
            $this->_addTestSuiteToSuiteDir( $test_run, $test_run->test_suite_id );

            // write the final suite index.html
            $this->_writeSuiteHtml();

            // compress off the suite and make it easy for the agent to download.
            system( 'cd ' . Light_Config::get( 'CTM_Config', 'SUITE_DIR' ) . ' ; zip -q -r ' . $this->_suite_compressed_file . ' ' . $test_run->id . ' >/dev/null 2> /dev/null' );

         } catch ( Exception $e ) {
            throw $e;
         }

   }

   private function _addTestSuiteToSuiteDir( CTM_Test_Run $test_run, $test_suite_id ) {
      try {

         $sel = new CTM_Test_Suite_Plan_Selector();
         $and_params = array( new Light_Database_Selector_Criteria( 'test_suite_id', '=', $test_suite_id ) );
         $plan_steps = $sel->find( $and_params ); 
         
         if ( count( $plan_steps ) > 0 ) {
            foreach ( $plan_steps as $plan_step ) {
               // is the step a suite or a test?
               $plan_type = $this->_plan_type_cache->getById( $plan_step->test_suite_plan_type_id ); 
               
               // we have to exclude linking back to the parent for the run.. this prevents infinite
               // loops.
               if ( $plan_type->name == 'suite' && $plan_step->linked_id != $test_run->test_suite_id ) {
                  $this->_addTestSuiteToSuiteDir( $test_run, $plan_step->linked_id );
               } else if ( $plan_type->name == 'test' ) {
                  $this->_addTestToSuiteDir( $test_run, $plan_step->linked_id );
               }

            }
         } 
      } catch ( Exception $e ) {
         throw $e;
      }

   }

   private function _addTestToSuiteDir( CTM_Test_Run $test_run, $test_id ) {

      try {
         // fetch the test_obj
         $test_obj = $this->_test_cache->getById( $test_id );

         // fetch all the test commands
         $sel = new CTM_Test_Command_Selector();
         $and_params = array( new Light_Database_Selector_Criteria( 'test_id', '=', $test_id ) );
         $or_params = array();
         $order = array( 'id' );
         $test_commands = $sel->find( $and_params, $or_params, $order );

         // increment the _suite_test_id
         $this->_suite_test_id++;

         $this->_suite_tests[] = array(
               'suite_test_id' => $this->_suite_test_id,
               'test_obj' => $test_obj
         );

         $filename = $this->_suite_dir . '/' . $this->_suite_test_id . '.html';

         $fh = fopen( $filename, 'w' );

         if ( ! is_resource( $fh ) ) {
            return false;
         }

         // determine the baseurl for this test.
         $baseurl_obj = $this->_test_run_baseurl_cache->getByCompoundKey( $test_run->id, 0, $test_id );

         // eject the headers.
         fwrite($fh, '<?xml version="1.0" encoding="UTF-8"?>' . "\n" );
         fwrite($fh, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n" );
         fwrite($fh, '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' . "\n" );
         fwrite($fh, '<head profile="http://selenium-ide.openqa.org/profiles/test-case">' . "\n" );
        
         // jeo - temporarily removing this piece.
         // fwrite($fh, '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . "\n" );

         fwrite($fh, '<link rel="selenium.base" href="' . $baseurl_obj->cleanBaseUrl() . '" />' . "\n" ); 
         fwrite($fh, '<title>' . $this->_escapeVariable( $test_obj->name ) . '</title>' . "\n" );
         fwrite($fh, '</head>' . "\n" );
         fwrite($fh, '<body>' . "\n" );
         fwrite($fh, '<table cellpadding="1" cellspacing="1" border="1">' . "\n" );
         fwrite($fh, '<thead>' . "\n" );
         fwrite($fh, '<tr><td rowspan="1" colspan="3">' . $this->_escapeVariable( $test_obj->name ) . '</td></tr>' . "\n" );
         fwrite($fh, '</thead><tbody>' . "\n" );

         fwrite($fh, '<tr>' . "\n" );
{'fraction_non_alphanumeric': 0.1509030745898249, 'fraction_numerical': 0.004205156486970909, 'mean_word_length': 2.3480267712900993, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 2, '>': 179, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3801706', 'n_tokens_mistral': 4874, 'n_tokens_neox': 4682, 'n_words': 905}