コード例 #1
0
 /**
  * Generate site data.
  *
  * @param OutputInterface $output output interface for writting.
  * @param string $sitesize Site size.
  * @param bool $force try generating data, even if there is data present.
  * @return int|bool error code, false to show help message, for invalid option passed.
  */
 public function run_testdata(OutputInterface $output, $sitesize, $force = false)
 {
     if (!util::get_moodle_path() || !util::get_data_path()) {
         $output->writeln("<error>Moodelpath and datapath not set, check behatgenerator.json</error> ");
         return 1;
     }
     // Check site status and then perform action.
     $sitestatus = installer::get_site_status($output);
     // If site is not configured then just exit with error.
     if ($sitestatus == installer::SITE_ERROR_CONFIG) {
         $output->writeln("<error>Error: Ensure you set \$CFG->* vars in config.php " . "and you ran vendor/bin/moodle_behat_generator --install</error>");
         return 1;
     }
     if ($sitestatus !== installer::SITE_INSTALLED && !$force) {
         echo "No site installed, so can't generate performance site data." . PHP_EOL;
         echo "Run \n   - vendor/bin/moodle_behat_generator --install" . PHP_EOL;
         return 1;
     }
     $sitesize = $this->check_valid_site_size($sitesize);
     if (empty($this->execute('enable', $sitesize))) {
         // Don't generate site if already installed.
         if ($sitedata = get_config('core', 'performancesitedata')) {
             if ($force) {
                 $this->execute('enable', $sitesize);
                 set_config('performancesitedata', $sitesize . ',' . util::get_tool_version());
             } else {
                 echo "Site data is already generated for site size: " . $sitedata . PHP_EOL;
                 echo "Drop site and try again:\n - vendor/bin/moodle_behat_generator --drop" . PHP_EOL;
                 exit(1);
             }
         } else {
             set_config('performancesitedata', $sitesize . ',' . util::get_tool_version());
         }
         $this->generate();
     } else {
         return 1;
     }
     // Success.
     return 0;
 }
コード例 #2
0
 /**
  * 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;
 }
コード例 #3
0
 /**
  * Enables test mode
  *
  * It uses CFG->dataroot/performance
  *
  * Starts the test mode checking the composer installation and
  * the test environment and updating the available
  * features and steps definitions.
  *
  * Stores a file in dataroot/performance to allow Moodle to switch
  * to the test environment when using cli-server.
  *
  * @param string $sitesize size of site
  * @param array $optionaltestdata (optional), replace default template with this value.
  * @throws performance_exception
  * @return int
  */
 public static function enable_performance_sitemode($sitesize, $optionaltestdata = array())
 {
     global $CFG;
     if (!defined('PERFORMANCE_SITE_GENERATOR')) {
         self::performance_exception('This method can be only used by performance site generator.');
     }
     // Checks the behat set up and the PHP version.
     if ($errorcode = self::check_setup_problem()) {
         return $errorcode;
     }
     // Check that test environment is correctly set up.
     if (self::test_environment_problem() !== self::SITE_INSTALLED) {
         return $errorcode;
     }
     // Make it a performance site, we have already checked for tables.
     if (!self::is_performance_site() && empty(get_config('core', 'perfromancesitehash'))) {
         self::store_versions_hash();
     }
     util::get_performance_dir(true);
     // Add moodle release and tool hash to performancesite.txt.
     $release = null;
     require "{$CFG->dirroot}/version.php";
     $contents = "release=" . $release . PHP_EOL;
     if ($hash = util::get_performance_tool_hash()) {
         $contents .= "hash=" . $hash . PHP_EOL;
     }
     // Add tool version to the file. This will help identify the tool version used to generate site.
     $generatorconfig = util::get_tool_version();
     $contents .= "generatorversion=" . $generatorconfig . PHP_EOL;
     // Add feature data hash.
     $featuresethash = md5(serialize($optionaltestdata));
     $contents .= "featurehash=" . $featuresethash . PHP_EOL;
     // Finally add site size.
     $contents .= "sitesize=" . $sitesize . PHP_EOL;
     $filepath = util::get_tool_dir() . DIRECTORY_SEPARATOR . 'performancesite.txt';
     if (!file_put_contents($filepath, $contents)) {
         echo 'File ' . $filepath . ' can not be created' . PHP_EOL;
         exit(1);
     }
     // Check composer dependencies.
     if (!is_dir($CFG->dirroot . '/vendor')) {
         util::testing_update_composer_dependencies($CFG->dirroot);
     }
     util::create_test_feature($sitesize, $optionaltestdata);
     return 0;
 }