Changeset 11787
- Timestamp:
- 04/25/2022 10:24:42 PM (3 years ago)
- Location:
- sites/trunk/common/includes/slack/props
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/common/includes/slack/props/lib.php
r11786 r11787 2 2 3 3 namespace Dotorg\Slack\Props; 4 use Dotorg\Slack\Send;4 use Exception; 5 5 use function Dotorg\Profiles\{ post as profiles_post }; 6 7 function show_error( $user ) {8 return "Please use `/props SLACK_USERNAME MESSAGE` to give props.\n";9 }10 11 /**12 * Receive `/props` request and send to `#props`.13 *14 * This is being deprecated in favor of `handle_props_message()`.15 *16 * @param array $data17 * @param bool $force_test Send to test channel instead of #props18 *19 * @return string20 */21 function run( $data, $force_test = false ) {22 $sender = $data['user_name'];23 24 if ( $data['command'] !== '/props' ) {25 return "???\n";26 }27 28 if ( empty( $data['text'] ) ) {29 return show_error( $sender );30 }31 32 list( $receiver, $message ) = @preg_split( '/\s+/', trim( $data['text'] ), 2 );33 34 $receiver = ltrim( $receiver, '@' );35 36 if ( ! strlen( $receiver ) || ! strlen( $message ) ) {37 return show_error( $sender );38 }39 40 // TODO: Add WordPress.org username to $text if different than Slack username.41 $text = sprintf( "Props to @%s: %s", $receiver, $message );42 43 $send = new Send( \Dotorg\Slack\Send\WEBHOOK );44 $send->set_username( $sender );45 $send->set_text( $text );46 $send->set_link_names( true ); // We want to the person getting props!47 48 $get_avatar = __NAMESPACE__ . '\\' . 'get_avatar';49 50 if ( function_exists( $get_avatar ) ) {51 $send->set_icon( call_user_func( $get_avatar, $sender, $data['user_id'], $data['team_id'] ) );52 }53 54 if ( $force_test ) {55 $send->testing( true );56 }57 58 $send->send( '#props' );59 60 return sprintf( "Your props to @%s have been sent.\n", $receiver );61 }62 6 63 7 /** -
sites/trunk/common/includes/slack/props/tests/test-lib.php
r11783 r11787 4 4 use wpdbStub; 5 5 use PHPUnit\Framework\TestCase; 6 use function Dotorg\Slack\Props\{ run,is_valid_props, get_recipient_slack_ids, map_slack_users_to_wporg, prepare_message };6 use function Dotorg\Slack\Props\{ is_valid_props, get_recipient_slack_ids, map_slack_users_to_wporg, prepare_message }; 7 7 8 8 /** … … 19 19 20 20 return json_decode( $json ); 21 }22 23 /**24 * @covers ::run25 * @dataProvider data_run26 * @group unit27 */28 public function test_run( array $request, $expected ) : void {29 $actual = run( $request, true );30 31 $this->assertSame( $expected, $actual );32 }33 34 public function data_run() : array {35 $valid_request = array(36 'user_name' => 'iandunn',37 'text' => 'kimparsell Thanks for being so welcoming at WordCamp Columbus',38 'command' => '/props',39 'user_id' => 'U02QCF502',40 'team_id' => '1',41 );42 43 // Can't call show_error() here since data providers run before `setUp()`.44 $usage = "Please use `/props SLACK_USERNAME MESSAGE` to give props.\n";45 $success = "Your props to @kimparsell have been sent.\n";46 47 $cases = array(48 'wrong command' => array(49 'request' => array_merge(50 $valid_request,51 array(52 'command' => '/here',53 )54 ),55 56 'expected' => "???\n",57 ),58 59 'empty text' => array(60 'request' => array_merge(61 $valid_request,62 array(63 'text' => '',64 )65 ),66 67 'expected' => $usage,68 ),69 70 'username but no message, or vice versa' => array(71 'request' => array_merge(72 $valid_request,73 array(74 'text' => 'kimparsell',75 )76 ),77 78 'expected' => $usage,79 ),80 81 'username with @' => array(82 'request' => array_merge(83 $valid_request,84 array(85 'text' => '@' . $valid_request['text'],86 )87 ),88 'expected' => $success,89 ),90 91 'username without @' => array(92 'request' => $valid_request,93 'expected' => $success,94 ),95 );96 97 return $cases;98 21 } 99 22
Note: See TracChangeset
for help on using the changeset viewer.