3 final class HeraldTestManagementWorkflow
4 extends HeraldManagementWorkflow
{
6 protected function didConstruct() {
9 ->setExamples('**test** --object __object__ --type __type__')
12 'Test content rules for an object. Executes a dry run, like the '.
13 'web UI test console.'))
19 'help' => pht('Run rules on this object.'),
24 'help' => pht('Run rules for this content type.'),
29 public function execute(PhutilArgumentParser
$args) {
30 $viewer = $this->getViewer();
32 $object_name = $args->getArg('object');
33 if (!strlen($object_name)) {
34 throw new PhutilArgumentUsageException(
35 pht('Specify an object to test rules for with "--object".'));
38 $objects = id(new PhabricatorObjectQuery())
40 ->withNames(array($object_name))
43 throw new PhutilArgumentUsageException(
45 'Unable to load specified object ("%s").',
48 $object = head($objects);
50 $adapters = HeraldAdapter
::getAllAdapters();
52 $can_select = array();
53 $display_adapters = array();
54 foreach ($adapters as $key => $adapter) {
55 if (!$adapter->isTestAdapterForObject($object)) {
59 if (!$adapter->isAvailableToUser($viewer)) {
63 $display_adapters[$key] = $adapter;
65 if ($adapter->canCreateTestAdapterForObject($object)) {
66 $can_select[$key] = $adapter;
71 $content_type = $args->getArg('type');
72 if (!strlen($content_type)) {
73 throw new PhutilArgumentUsageException(
75 'Specify a content type to run rules for. For this object, valid '.
76 'content types are: %s.',
77 implode(', ', array_keys($can_select))));
80 if (!isset($can_select[$content_type])) {
81 if (!isset($display_adapters[$content_type])) {
82 throw new PhutilArgumentUsageException(
84 'Specify a content type to run rules for. The specified content '.
85 'type ("%s") is not valid. For this object, valid content types '.
88 implode(', ', array_keys($can_select))));
90 throw new PhutilArgumentUsageException(
92 'The specified content type ("%s") does not support dry runs. '.
93 'Choose a testable content type. For this object, valid content '.
96 implode(', ', array_keys($can_select))));
100 $adapter = $can_select[$content_type]->newTestAdapter(
104 $content_source = $this->newContentSource();
107 ->setContentSource($content_source)
108 ->setIsNewObject(false)
109 ->setViewer($viewer);
111 $rules = id(new HeraldRuleQuery())
113 ->withContentTypes(array($adapter->getAdapterContentType()))
114 ->withDisabled(false)
115 ->needConditionsAndActions(true)
116 ->needAppliedToPHIDs(array($object->getPHID()))
117 ->needValidateAuthors(true)
120 $engine = id(new HeraldEngine())
123 $effects = $engine->applyRules($rules, $adapter);
124 $engine->applyEffects($effects, $adapter, $rules);
126 $xscript = $engine->getTranscript();
128 $uri = '/herald/transcript/'.$xscript->getID().'/';
129 $uri = PhabricatorEnv
::getProductionURI($uri);
133 pht('Test run complete. Transcript:'),