/**
  * Updates the composer installer and the dependencies.
  *
  * @param string $dirroot root of directory where to check for composer install.
  * @return void exit() if something goes wrong
  */
 public static function testing_update_composer_dependencies($dirroot)
 {
     // To restore the value after finishing.
     $cwd = getcwd();
     $composerpath = $dirroot . DIRECTORY_SEPARATOR . 'composer.phar';
     $composerurl = 'https://getcomposer.org/composer.phar';
     // Switch to Moodle's dirroot for easier path handling.
     chdir($dirroot);
     // Download or update composer.phar. Unfortunately we can't use the curl
     // class in filelib.php as we're running within one of the test platforms.
     if (!file_exists($composerpath)) {
         $file = @fopen($composerpath, 'w');
         if ($file === false) {
             $errordetails = error_get_last();
             util::performance_exception(sprintf("Unable to create composer.phar\nPHP error: %s", $errordetails['message']));
         }
         $curl = curl_init();
         curl_setopt($curl, CURLOPT_URL, $composerurl);
         curl_setopt($curl, CURLOPT_FILE, $file);
         $result = curl_exec($curl);
         $curlerrno = curl_errno($curl);
         $curlerror = curl_error($curl);
         $curlinfo = curl_getinfo($curl);
         curl_close($curl);
         fclose($file);
         if (!$result) {
             $error = sprintf("Unable to download composer.phar\ncURL error (%d): %s", $curlerrno, $curlerror);
             util::performance_exception($error);
         } else {
             if ($curlinfo['http_code'] === 404) {
                 if (file_exists($composerpath)) {
                     // Deleting the resource as it would contain HTML.
                     unlink($composerpath);
                 }
                 $error = sprintf("Unable to download composer.phar\n" . "404 http status code fetching {$composerurl}");
                 util::performance_exception($error);
             }
         }
     } else {
         passthru("php composer.phar self-update", $code);
         if ($code != 0) {
             exit($code);
         }
     }
     // Update composer dependencies.
     passthru("php composer.phar install", $code);
     if ($code != 0) {
         exit($code);
     }
     // Return to our original location.
     chdir($cwd);
 }
 /**
  * Start behat process for generating site contents.
  */
 protected function generate()
 {
     $generatorfeaturepath = util::get_tool_dir();
     // Execute each feature file 1 by one to show the proper progress...
     $generatorconfig = util::get_feature_config();
     if (empty($generatorconfig)) {
         util::performance_exception("Check generator config file.");
     }
     // Create test feature file depending on what is given.
     foreach ($generatorconfig as $featuretoadd => $config) {
         $finalfeaturepath = $generatorfeaturepath . DIRECTORY_SEPARATOR . $featuretoadd . '.feature';
         if (file_exists($finalfeaturepath)) {
             self::log($featuretoadd);
             $status = $this->execute_behat_generator($featuretoadd, $finalfeaturepath);
             // Don't proceed...
             if ($status) {
                 echo "Error: Failed generating data for " . $featuretoadd . PHP_EOL . PHP_EOL;
                 echo "Check running\n";
                 $cmd = "vendor/bin/behat --config " . util::get_tool_dir() . DIRECTORY_SEPARATOR . 'behat.yml ';
                 echo $cmd . $finalfeaturepath . PHP_EOL;
                 exit(1);
             }
             self::end_log();
         }
     }
 }
 /**
  * Execute the command.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // We don't need moodle for getting value.
     if ($value = $input->getOption('value')) {
         switch ($value) {
             case 'version':
                 $output->writeln(\moodlehq\behat_generator\util::get_tool_version());
                 break;
             case 'moodlepath':
                 $output->writeln(util::get_moodle_path());
                 break;
             case 'datapath':
                 $output->writeln(util::get_data_path());
                 break;
             default:
                 $output->writeln('<error>Not a valid option.</error><info> Should be wither version|moodlepath|datapath</info>');
                 return 1;
                 break;
         }
         return 0;
     }
     // Include moodle config.
     define('PERFORMANCE_SITE_GENERATOR', true);
     define('CLI_SCRIPT', true);
     define('NO_OUTPUT_BUFFERING', true);
     define('IGNORE_COMPONENT_CACHE', true);
     // Load moodle config and all classes.
     if (!($moodlepath = util::get_moodle_path())) {
         util::performance_exception("Moodlepath should have been set by now.");
     }
     // Autoload files and ensure we load moodle config, as we will be using moodle code for behat context.
     require_once $moodlepath . '/config.php';
     require_once __DIR__ . '/inc.php';
     raise_memory_limit(MEMORY_HUGE);
     $status = false;
     // Do action.
     $sitedatagenerator = new \moodlehq\behat_generator\generator();
     if ($input->getOption('drop')) {
         // Don't check for any option, just try drop site..
         $force = $input->getOption('force') ? true : false;
         $status = $sitedatagenerator->run_drop($output, $force);
     } else {
         if ($input->getOption('dropsite')) {
             // Don't check for any option, just try drop site..
             $force = $input->getOption('force') ? true : false;
             $status = $sitedatagenerator->run_dropsite($output, $force);
         }
     }
     // Check if site enable/disable is needed.
     if ($input->getOption('enable')) {
         $status = $sitedatagenerator->run_enable($output, $input->getOption('enable'));
     } else {
         if ($input->getOption('disable')) {
             $status = $sitedatagenerator->run_disable($output);
         }
     }
     if ($input->getOption('install')) {
         $status = $sitedatagenerator->run_install($output);
     }
     // Check if testdata needs to be generated.
     if ($input->getOption('testdata')) {
         $status = $sitedatagenerator->run_testdata($output, $input->getOption('testdata'), $input->getOption('force'));
     }
     // Finally, check if backup/restore is needed.
     if ($input->getOption('backup')) {
         $status = $sitedatagenerator->run_backup($output, $input->getOption('backup'));
     } else {
         if ($input->getOption('restore')) {
             $status = $sitedatagenerator->run_restore($output, $input->getOption('restore'));
         }
     }
     if ($status === false) {
         $output->write($this->getHelp(), true);
     }
     return $status;
 }
 /**
  * Restore state of current site. Dataroot and database.
  *
  * @param string $statename
  * @return int 0 on success else error code.
  */
 public static function restore_site_state($statename = "default")
 {
     // Clean up the dataroot folder.
     util::drop_dir(self::get_dataroot() . '/');
     // Restore database and dataroot state, before proceeding.
     echo "Restoring database state" . PHP_EOL;
     if (!self::restore_database_state($statename)) {
         util::performance_exception("Error restoring state db: " . $statename);
     }
     echo "Restoring dataroot state" . PHP_EOL;
     if (!self::restore_dataroot($statename)) {
         util::performance_exception("Error restoring state data: " . $statename);
     }
     echo "Site restored to {$statename} state" . PHP_EOL;
     return 0;
 }