Path: blob/master/src/applications/herald/management/HeraldTestManagementWorkflow.php
12256 views
<?php12final class HeraldTestManagementWorkflow3extends HeraldManagementWorkflow {45protected function didConstruct() {6$this7->setName('test')8->setExamples('**test** --object __object__ --type __type__')9->setSynopsis(10pht(11'Test content rules for an object. Executes a dry run, like the '.12'web UI test console.'))13->setArguments(14array(15array(16'name' => 'object',17'param' => 'object',18'help' => pht('Run rules on this object.'),19),20array(21'name' => 'type',22'param' => 'type',23'help' => pht('Run rules for this content type.'),24),25));26}2728public function execute(PhutilArgumentParser $args) {29$viewer = $this->getViewer();3031$object_name = $args->getArg('object');32if (!strlen($object_name)) {33throw new PhutilArgumentUsageException(34pht('Specify an object to test rules for with "--object".'));35}3637$objects = id(new PhabricatorObjectQuery())38->setViewer($viewer)39->withNames(array($object_name))40->execute();41if (!$objects) {42throw new PhutilArgumentUsageException(43pht(44'Unable to load specified object ("%s").',45$object_name));46}47$object = head($objects);4849$adapters = HeraldAdapter::getAllAdapters();5051$can_select = array();52$display_adapters = array();53foreach ($adapters as $key => $adapter) {54if (!$adapter->isTestAdapterForObject($object)) {55continue;56}5758if (!$adapter->isAvailableToUser($viewer)) {59continue;60}6162$display_adapters[$key] = $adapter;6364if ($adapter->canCreateTestAdapterForObject($object)) {65$can_select[$key] = $adapter;66}67}686970$content_type = $args->getArg('type');71if (!strlen($content_type)) {72throw new PhutilArgumentUsageException(73pht(74'Specify a content type to run rules for. For this object, valid '.75'content types are: %s.',76implode(', ', array_keys($can_select))));77}7879if (!isset($can_select[$content_type])) {80if (!isset($display_adapters[$content_type])) {81throw new PhutilArgumentUsageException(82pht(83'Specify a content type to run rules for. The specified content '.84'type ("%s") is not valid. For this object, valid content types '.85'are: %s.',86$content_type,87implode(', ', array_keys($can_select))));88} else {89throw new PhutilArgumentUsageException(90pht(91'The specified content type ("%s") does not support dry runs. '.92'Choose a testable content type. For this object, valid content '.93'types are: %s.',94$content_type,95implode(', ', array_keys($can_select))));96}97}9899$adapter = $can_select[$content_type]->newTestAdapter(100$viewer,101$object);102103$content_source = $this->newContentSource();104105$adapter106->setContentSource($content_source)107->setIsNewObject(false)108->setViewer($viewer);109110$rules = id(new HeraldRuleQuery())111->setViewer($viewer)112->withContentTypes(array($adapter->getAdapterContentType()))113->withDisabled(false)114->needConditionsAndActions(true)115->needAppliedToPHIDs(array($object->getPHID()))116->needValidateAuthors(true)117->execute();118119$engine = id(new HeraldEngine())120->setDryRun(true);121122$effects = $engine->applyRules($rules, $adapter);123$engine->applyEffects($effects, $adapter, $rules);124125$xscript = $engine->getTranscript();126127$uri = '/herald/transcript/'.$xscript->getID().'/';128$uri = PhabricatorEnv::getProductionURI($uri);129130echo tsprintf(131"%s\n\n __%s__\n\n",132pht('Test run complete. Transcript:'),133$uri);134135return 0;136}137138}139140141