Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / herald / management / HeraldTestManagementWorkflow.php
blobd58e7c4190d6e8157a8f73d56dcc71cc33203c7b
1 <?php
3 final class HeraldTestManagementWorkflow
4 extends HeraldManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('test')
9 ->setExamples('**test** --object __object__ --type __type__')
10 ->setSynopsis(
11 pht(
12 'Test content rules for an object. Executes a dry run, like the '.
13 'web UI test console.'))
14 ->setArguments(
15 array(
16 array(
17 'name' => 'object',
18 'param' => 'object',
19 'help' => pht('Run rules on this object.'),
21 array(
22 'name' => 'type',
23 'param' => 'type',
24 'help' => pht('Run rules for this content type.'),
26 ));
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())
39 ->setViewer($viewer)
40 ->withNames(array($object_name))
41 ->execute();
42 if (!$objects) {
43 throw new PhutilArgumentUsageException(
44 pht(
45 'Unable to load specified object ("%s").',
46 $object_name));
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)) {
56 continue;
59 if (!$adapter->isAvailableToUser($viewer)) {
60 continue;
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(
74 pht(
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(
83 pht(
84 'Specify a content type to run rules for. The specified content '.
85 'type ("%s") is not valid. For this object, valid content types '.
86 'are: %s.',
87 $content_type,
88 implode(', ', array_keys($can_select))));
89 } else {
90 throw new PhutilArgumentUsageException(
91 pht(
92 'The specified content type ("%s") does not support dry runs. '.
93 'Choose a testable content type. For this object, valid content '.
94 'types are: %s.',
95 $content_type,
96 implode(', ', array_keys($can_select))));
100 $adapter = $can_select[$content_type]->newTestAdapter(
101 $viewer,
102 $object);
104 $content_source = $this->newContentSource();
106 $adapter
107 ->setContentSource($content_source)
108 ->setIsNewObject(false)
109 ->setViewer($viewer);
111 $rules = id(new HeraldRuleQuery())
112 ->setViewer($viewer)
113 ->withContentTypes(array($adapter->getAdapterContentType()))
114 ->withDisabled(false)
115 ->needConditionsAndActions(true)
116 ->needAppliedToPHIDs(array($object->getPHID()))
117 ->needValidateAuthors(true)
118 ->execute();
120 $engine = id(new HeraldEngine())
121 ->setDryRun(true);
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);
131 echo tsprintf(
132 "%s\n\n __%s__\n\n",
133 pht('Test run complete. Transcript:'),
134 $uri);
136 return 0;