/** * import employee data from csv file * * @param Zend_Console_Getopt $opts * @return integer */ public function importEmployee($opts) { $args = $opts->getRemainingArgs(); array_push($args, 'definition=' . 'hr_employee_import_csv'); if ($opts->d) { array_push($args, '--dry'); } if ($opts->v) { array_push($args, '--verbose'); } $opts->setArguments($args); $result = $this->_import($opts); if (empty($result)) { return 2; } foreach ($result as $filename => $importResult) { $importedEmployee = $this->_getImportedEmployees($importResult); foreach ($importedEmployee as $employee) { $this->_sanitizeEmployee($opts, $employee); $currentEmployee = $this->_getCurrentEmployee($employee); if ($currentEmployee) { $employee = $this->_updateImportedEmployee($opts, $employee, $currentEmployee); } else { $employee = $this->_createImportedEmployee($opts, $employee); } if ($opts->v) { print_r($employee->toArray()); } if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($employee->toArray(), TRUE)); } } } return 0; }
/** * call handle cli function with params * * @param array $_params */ protected function _cliHelper($command, $_params) { $opts = new Zend_Console_Getopt(array($command => $command)); $opts->setArguments($_params); ob_start(); $this->_cli->handle($opts, false); $out = ob_get_clean(); return $out; }
/** * import helper */ protected function _doImport($checkOutput = TRUE) { $opts = new Zend_Console_Getopt(array('verbose|v' => 'Output messages', 'dry|d' => "Dry run - don't change anything")); $filename = dirname(__FILE__) . '/files/employee.csv'; $args = array($filename, 'feast_calendar_id=' . $this->_getFeastCalendar()->getId(), 'working_time_model_id=' . $this->_getWorkingTime()->getId(), 'vacation_days=30'); $opts->setArguments($args); ob_start(); $result = $this->_cli->importEmployee($opts); $out = ob_get_clean(); $this->assertEquals(0, $result, 'import failed: ' . $out); if ($checkOutput) { $this->assertContains("Imported 2 records.", $out); } }
/** * Takes the config from config.inc.php and creates an account with the associated lines * @param Zend_Console_Getopt $_opts */ public function take_config($_opts) { // resolve arguments $args = $this->_parseArgs($_opts, array()); $type = in_array('shared', $args['other']) ? 'shared' : 'private'; if (@isset(Tinebase_Core::getConfig()->sipgate)) { $conf = Tinebase_Core::getConfig()->sipgate; if (@isset($conf->api_username) && @isset($conf->api_password)) { echo 'Validate configuration from config.inc.php...' . PHP_EOL; $accountData = array('data' => array('accounttype' => @isset($conf->api_url) && $conf->api_url != 'https://samurai.sipgate.net/RPC2' ? 'team' : 'plus', 'description' => 'Created by update', 'username' => $conf->api_username, 'password' => $conf->api_password, 'type' => $type)); if (Sipgate_Controller_Account::getInstance()->validateAccount($accountData)) { echo 'Data from config.inc.php could be validated, creating account...' . PHP_EOL; try { $account = Sipgate_Controller_Account::getInstance()->create(new Sipgate_Model_Account($accountData['data'])); } catch (Tinebase_Exception_Duplicate $e) { echo 'An account with this credentials exists already! Did you use this script twice?' . PHP_EOL; die; } if ($account) { echo 'Account created. Trying to synchronize the lines..' . PHP_EOL; if (Sipgate_Controller_Line::getInstance()->syncAccount($account->getId())) { $opts = new Zend_Console_Getopt('abp:'); $args = array('initial', 'verbose'); if ($type == 'shared') { $args[] = 'shared'; } $opts->setArguments($args); echo 'Lines have been synchronized. Now syncing connections from the last two months, day per day. This could take some time.' . PHP_EOL; $this->sync_connections($opts); echo 'Connections has been synchronized. Now assign users to the line(s) to allow them to use the line(s)' . PHP_EOL; echo 'READY!' . PHP_EOL; } else { echo 'The lines for the account could not be created!' . PHP_EOL; } } else { echo 'The account could not be created!' . PHP_EOL; } } else { echo 'The credentials found in config.inc.php could not be validated!' . PHP_EOL; } } else { echo 'No username or password could be found in config.php.inc!' . PHP_EOL; } } else { echo 'No sipgate config could be found in config.php.inc!' . PHP_EOL; } }
/** * Sets up the fixture. * This method is called before a test is executed. * * @access protected */ protected function setUp() { parent::setUp(); $fe = new Tinebase_Frontend_Cli(); $opts = new Zend_Console_Getopt('abp:'); $path = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/tine20/tine20/HumanResources/Export/definitions/hr_default_ods.xml'; $opts->setArguments(array($path)); ob_start(); $fe->updateImportExportDefinition($opts); $output = ob_get_clean(); $this->assertContains('hr_default_ods.xml successfully.', $output); $filter = new Tinebase_Model_ImportExportDefinitionFilter(array(array('field' => 'name', 'operator' => 'equals', 'value' => 'hr_default_ods'))); $backend = new Tinebase_Backend_Sql(array('modelName' => 'Tinebase_Model_ImportExportDefinition', 'tableName' => 'importexport_definition'), NULL); $this->_importDefinition = $backend->search($filter)->getFirstRecord(); $filter = new HumanResources_Model_EmployeeFilter(array()); $options = array('definitionId' => $this->_importDefinition->getId()); $this->_instance = Tinebase_Export::factory($filter, $options); }
<?php $paths = explode(DIRECTORY_SEPARATOR, __DIR__); $paths = array_slice($paths, 0, -5); $path = implode(DIRECTORY_SEPARATOR, $paths) . DIRECTORY_SEPARATOR . 'Mage.php'; require_once $path; Mage::app(); $opts = new Zend_Console_Getopt(array('all-events' => 'Listen for all events', 'enable-blocks' => 'Enable block events (overrides all-events)', 'enable-models' => 'Enable model events (overrides all-events)', 'events=s' => 'Comma-separated list of events to listen for (overides any settings)', 'resolve-classes' => 'Look up the class based off of group class names', 'hide-event-data' => 'Hides the event data when events are turned on', 'hide-event-observers' => 'Hides the event observers when events are turned on', 'delay-messages=i' => 'Delay messages by X milliseconds (default 75ms) - Makes it easier to follow the log', 'no-color' => 'Turns off color coding of elapsed times', 'show-request-id' => 'Show the unique ID for each request', 'show-sql-s' => 'Show the SQL in real time. Optionally add a preg_match() filter', 'show-sql-summary' => 'Show the SQL summary at the end of the request', 'help' => 'This help page')); try { $opts->parse(); } catch (Zend_Console_Getopt_Exception $e) { echo "\n" . $e->getMessage() . "\n\n"; $opts->setArguments(array()); } if ($opts->getOption('help')) { echo "\nThis script is used to connect to the front end of the Eschrade_PubSubLogger utility.\n"; echo "\nNote that if you are using the File cache backend you may need to clear it from /tmp if "; echo "the user you are running this script under does not have permission in {magento}/var/cache.\n\n"; if (!extension_loaded('pcntl')) { echo "Note: that the pcntl extension is not loaded and so the script will not send a logging"; echo " shutdown command if you hit CTRL-C. Install the pcntl extension if you would like this behavior (preferred).\n\n"; } echo $opts->getUsageMessage(); echo "\n"; return; } $config = array(Eschrade_PubSubLogger_Model_Observer::CONFIG_LISTENER_EXISTS => 1, Eschrade_PubSubLogger_Model_Observer::CONFIG_EVENT_LOG_ALL => (int) $opts->getOption('all-events'), Eschrade_PubSubLogger_Model_Observer::CONFIG_EVENT_IGNORE_BLOCKS => (int) (!$opts->getOption('enable-blocks')), Eschrade_PubSubLogger_Model_Observer::CONFIG_EVENT_IGNORE_MODELS => (int) (!$opts->getOption('enable-models')), Eschrade_PubSubLogger_Model_Observer::CONFIG_PROFILE_SQL => (int) $opts->getOption('show-sql'), Eschrade_PubSubLogger_Model_Observer::CONFIG_SQL_SUMMARY => 0); $handler = new Logger_Handler($config, $opts); if (extension_loaded('pcntl')) { $cb = array('Logger_Handler', 'shutdown'); pcntl_signal(SIGTERM, $cb);
public function testGetoptCheckParameterType() { $opts = new Zend_Console_Getopt(array('apple|a=i' => 'apple with integer', 'banana|b=w' => 'banana with word', 'pear|p=s' => 'pear with string', 'orange|o-i' => 'orange with optional integer', 'lemon|l-w' => 'lemon with optional word', 'kumquat|k-s' => 'kumquat with optional string')); $opts->setArguments(array('-a', 327)); $opts->parse(); $this->assertEquals(327, $opts->a); $opts->setArguments(array('-a', 'noninteger')); try { $opts->parse(); $this->fail('Expected to catch Zend_Console_Getopt_Exception'); } catch (Zend_Exception $e) { $this->assertType('Zend_Console_Getopt_Exception', $e, 'Expected Zend_Console_Getopt_Exception, got ' . get_class($e)); $this->assertEquals($e->getMessage(), 'Option "apple" requires an integer parameter, but was given "noninteger".'); } $opts->setArguments(array('-b', 'word')); $this->assertEquals('word', $opts->b); $opts->setArguments(array('-b', 'two words')); try { $opts->parse(); $this->fail('Expected to catch Zend_Console_Getopt_Exception'); } catch (Zend_Exception $e) { $this->assertType('Zend_Console_Getopt_Exception', $e, 'Expected Zend_Console_Getopt_Exception, got ' . get_class($e)); $this->assertEquals($e->getMessage(), 'Option "banana" requires a single-word parameter, but was given "two words".'); } $opts->setArguments(array('-p', 'string')); $this->assertEquals('string', $opts->p); $opts->setArguments(array('-o', 327)); $this->assertEquals(327, $opts->o); $opts->setArguments(array('-o')); $this->assertTrue($opts->o); $opts->setArguments(array('-l', 'word')); $this->assertEquals('word', $opts->l); $opts->setArguments(array('-k', 'string')); $this->assertEquals('string', $opts->k); }
/** * test trigger events */ public function testTriggerAsyncEvents() { $opts = new Zend_Console_Getopt('abp:'); $opts->setArguments(array()); $this->_usernamesToDelete[] = 'cronuser'; $this->_releaseDBLockIds[] = 'Tinebase_Frontend_Cli::triggerAsyncEvents'; ob_start(); $this->_cli->triggerAsyncEvents($opts); $out = ob_get_clean(); $userPlugins = Tinebase_User::getInstance()->getPlugins(); $this->assertEquals(0, count($userPlugins), 'got user plugins: ' . print_r($userPlugins, true)); $cronuserId = Tinebase_Config::getInstance()->get(Tinebase_Config::CRONUSERID); $this->assertTrue(!empty($cronuserId), 'got empty cronuser id'); $cronuser = Tinebase_User::getInstance()->getFullUserById($cronuserId); $this->assertEquals('cronuser', $cronuser->accountLoginName); $adminGroup = Tinebase_Group::getInstance()->getDefaultAdminGroup(); $this->assertEquals($adminGroup->getId(), $cronuser->accountPrimaryGroup); $this->assertContains('Tine 2.0 scheduler run', $out, $out); }
/** * tests if import with members from csv works correctly */ public function testImportGroups() { $opts = new Zend_Console_Getopt('abp:'); $opts->setArguments(array(dirname(__FILE__) . '/files/import_groups.csv', 'definition=admin_group_import_csv')); // start import (dry run) ob_start(); $this->_cli->importGroups($opts); $out = ob_get_clean(); $this->assertStringStartsWith('Imported 4 records.', $out); $expected = array('men' => 3, 'women' => 2, 'highperformers' => 2, 'lowperformers' => 3); $this->_testImportGroupsHelper($expected); $opts->setArguments(array(dirname(__FILE__) . '/files/import_groups_update.csv', 'definition=admin_group_import_csv')); ob_start(); $this->_cli->importGroups($opts); $out = ob_get_clean(); $this->assertStringStartsWith('Imported 0 records.', $out); $expected = array('men' => 3, 'women' => 2, 'lowperformers' => 2, 'highperformers' => 3); $this->_testImportGroupsHelper($expected); }
} if ((bool) $job->getParam('purge')) { $arguments[] = '--purge'; } if ((bool) $job->getParam('onlyNewConcepts')) { $arguments[] = '--onlyNewConcepts'; } if ((bool) $job->getParam('useUriAsIdentifier')) { $arguments[] = '--useUriAsIdentifier'; } $arguments[] = '--commit'; $duplicateConceptSchemes = array(); $notImportedNotations = array(); foreach ($importFiles as $filePath) { $parserOpts = new Zend_Console_Getopt(OpenSKOS_Rdf_Parser::$get_opts); $parserOpts->setArguments(array_merge($arguments, array($filePath))); // The last argument must be the file path. try { $parser = OpenSKOS_Rdf_Parser::factory($parserOpts); $parser->process($job['user']); $duplicateConceptSchemes = array_merge($duplicateConceptSchemes, $parser->getDuplicateConceptSchemes()); $notImportedNotations = array_merge($notImportedNotations, $parser->getNotImportedNotations()); } catch (Exception $e) { $model = new OpenSKOS_Db_Table_Jobs(); // Gets new DB object to prevent connection time out. $job = $model->find($job->id)->current(); // Gets new DB object to prevent connection time out. fwrite(STDERR, $job->id . ': ' . $e->getMessage() . "\n"); $job->error($e->getMessage())->finish()->save(); exit($e->getCode()); }
/** * call setContainerGrants cli function with params * * @param array $_params * @return string */ protected function _cliHelper($_params, $_numberExpected = 1) { $opts = new Zend_Console_Getopt('abp:'); $opts->setArguments($_params); ob_start(); $this->_cli->setContainerGrants($opts); $out = ob_get_clean(); $this->assertContains("Updated {$_numberExpected} container(s)", $out, 'Text not found in: ' . $out); return $out; }
/** * testRemoveAutogeneratedContacts * * @see 0010257: add cli function for deleting autogenerated contacts */ public function testRemoveAutogeneratedContacts() { if (!Tinebase_Application::getInstance()->isInstalled('Calendar')) { $this->markTestSkipped('only works with Calendar app'); } $attenderEmail = '*****@*****.**'; $attenderData = array('email' => $attenderEmail); Calendar_Model_Attender::resolveEmailToContact($attenderData); $opts = new Zend_Console_Getopt('abp:'); $opts->setArguments(array()); $this->_cli->removeAutogeneratedContacts($opts); $filter = new Addressbook_Model_ContactFilter(array(array('field' => 'email', 'operator' => 'equals', 'value' => $attenderEmail))); $result = Addressbook_Controller_Contact::getInstance()->search($filter); $this->assertEquals(0, count($result), 'should not find autogenerated contact any more: ' . print_r($result->toArray(), true)); }
/** * test trigger events */ public function testTriggerAsyncEvents() { $opts = new Zend_Console_Getopt('abp:'); $opts->setArguments(array()); ob_start(); $this->_cli->triggerAsyncEvents($opts); $out = ob_get_clean(); $cronuserId = Tinebase_Config::getInstance()->getConfig(Tinebase_Config::CRONUSERID)->value; $cronuser = Tinebase_User::getInstance()->getFullUserById($cronuserId); $this->assertEquals('cronuser', $cronuser->accountLoginName); $adminGroup = Tinebase_Group::getInstance()->getDefaultAdminGroup(); $this->assertEquals($adminGroup->getId(), $cronuser->accountPrimaryGroup); $this->assertContains('Tine 2.0 scheduler run', $out, $out); }
/** * import users * * @param string $_config xml config */ protected function _importUsers($_config, $_filename, $_definition) { // create definition / check if exists try { $definition = Tinebase_ImportExportDefinition::getInstance()->getByName($_definition); $definition->plugin_options = $_config; } catch (Tinebase_Exception_NotFound $e) { $definition = Tinebase_ImportExportDefinition::getInstance()->create(new Tinebase_Model_ImportExportDefinition(array('application_id' => Tinebase_Application::getInstance()->getApplicationByName('Admin')->getId(), 'name' => $_definition, 'type' => 'import', 'model' => 'Tinebase_Model_FullUser', 'plugin' => 'Admin_Import_Csv', 'plugin_options' => $_config))); } $opts = new Zend_Console_Getopt('abp:'); $opts->setArguments(array($_filename, 'definition=' . $_definition)); ob_start(); $this->_cli->importUser($opts); $out = ob_get_clean(); // check output $this->assertEquals("Imported 3 records. Import failed for 0 records. \n", $out); }