[TESTS] Fix CommandInterpreterTest

Also corrected a bad refactoring that affected Xmpp plugin test
This commit is contained in:
Diogo Cordeiro 2020-06-24 12:42:05 +01:00
parent 236929a166
commit aa7aff3f6b
4 changed files with 65 additions and 41 deletions

View File

@ -53,27 +53,39 @@ class DirectMessagePlugin extends Plugin
public function onRouterInitialized(URLMapper $m) public function onRouterInitialized(URLMapper $m)
{ {
// web front-end actions // web front-end actions
$m->connect('message/new', $m->connect(
['action' => 'newmessage']); 'message/new',
$m->connect('message/new?to=:to', ['action' => 'newmessage']
);
$m->connect(
'message/new?to=:to',
['action' => 'newmessage'], ['action' => 'newmessage'],
['to' => '[0-9]+']); ['to' => '[0-9]+']
);
$m->connect('message/:message', $m->connect(
'message/:message',
['action' => 'showmessage'], ['action' => 'showmessage'],
['message' => '[0-9]+']); ['message' => '[0-9]+']
);
// direct messages // direct messages
$m->connect('api/direct_messages.:format', $m->connect(
'api/direct_messages.:format',
['action' => 'ApiDirectMessage'], ['action' => 'ApiDirectMessage'],
['format' => '(xml|json|rss|atom)']); ['format' => '(xml|json|rss|atom)']
$m->connect('api/direct_messages/sent.:format', );
$m->connect(
'api/direct_messages/sent.:format',
['action' => 'ApiDirectMessage', ['action' => 'ApiDirectMessage',
'sent' => true], 'sent' => true],
['format' => '(xml|json|rss|atom)']); ['format' => '(xml|json|rss|atom)']
$m->connect('api/direct_messages/new.:format', );
$m->connect(
'api/direct_messages/new.:format',
['action' => 'ApiDirectMessageNew'], ['action' => 'ApiDirectMessageNew'],
['format' => '(xml|json)']); ['format' => '(xml|json)']
);
return true; return true;
} }
@ -97,7 +109,7 @@ class DirectMessagePlugin extends Plugin
* @param string $cmd Command being run * @param string $cmd Command being run
* @param string $arg Rest of the message (including address) * @param string $arg Rest of the message (including address)
* @param User $user User sending the message * @param User $user User sending the message
* @param Command|bool &$result The resulting command object to be run. * @param MessageCommand|bool &$result The resulting command object to be run.
* @return bool hook value * @return bool hook value
*/ */
public function onStartInterpretCommand(string $cmd, ?string $arg, User $user, &$result) : bool public function onStartInterpretCommand(string $cmd, ?string $arg, User $user, &$result) : bool
@ -123,18 +135,20 @@ class DirectMessagePlugin extends Plugin
* @param Profile $target * @param Profile $target
* @param Profile $scoped * @param Profile $scoped
* @return void * @return void
* @throws ServerException
*/ */
public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped = null) public function onEndPersonalGroupNav(Menu $menu, Profile $target, Profile $scoped = null)
{ {
if ($scoped instanceof Profile && $scoped->id == $target->id if ($scoped instanceof Profile && $scoped->id == $target->id
&& !common_config('singleuser', 'enabled')) { && !common_config('singleuser', 'enabled')) {
$menu->out->menuItem(
$menu->out->menuItem(common_local_url('inbox', ['nickname' => $target->getNickname()]), common_local_url('inbox', ['nickname' => $target->getNickname()]),
// TRANS: Menu item in personal group navigation menu. // TRANS: Menu item in personal group navigation menu.
_m('MENU', 'Messages'), _m('MENU', 'Messages'),
// TRANS: Menu item title in personal group navigation menu. // TRANS: Menu item title in personal group navigation menu.
_('Your incoming messages'), _('Your incoming messages'),
$scoped->id === $target->id && $menu->actionName =='inbox'); $scoped->id === $target->id && $menu->actionName =='inbox'
);
} }
} }
@ -159,12 +173,14 @@ class DirectMessagePlugin extends Plugin
if (!$profile->hasBlocked($scoped)) { if (!$profile->hasBlocked($scoped)) {
$out->elementStart('li', 'entity_send-a-message'); $out->elementStart('li', 'entity_send-a-message');
$out->element('a', $out->element(
'a',
['href' => common_local_url('newmessage', ['to' => $profile->getID()]), ['href' => common_local_url('newmessage', ['to' => $profile->getID()]),
// TRANS: Link title for link on user profile. // TRANS: Link title for link on user profile.
'title' => _('Send a direct message to this user.')], 'title' => _('Send a direct message to this user.')],
// TRANS: Link text for link on user profile. // TRANS: Link text for link on user profile.
_m('BUTTON','Message')); _m('BUTTON', 'Message')
);
$out->elementEnd('li'); $out->elementEnd('li');
} }
@ -197,10 +213,12 @@ class DirectMessagePlugin extends Plugin
$msg = Message::getKV('id', $message->id); $msg = Message::getKV('id', $message->id);
$act = $msg->asActivity(); $act = $msg->asActivity();
Notice::saveActivity($act, Notice::saveActivity(
$act,
$msg->getFrom(), $msg->getFrom(),
['source' => 'web', ['source' => 'web',
'scope' => NOTICE::MESSAGE_SCOPE]); 'scope' => NOTICE::MESSAGE_SCOPE]
);
} }
} }

View File

@ -31,6 +31,7 @@ if (!defined('STATUSNET')) { // Compatibility
use CommandInterpreter; use CommandInterpreter;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use User;
require_once INSTALLDIR . '/lib/util/common.php'; require_once INSTALLDIR . '/lib/util/common.php';
@ -47,7 +48,7 @@ final class CommandInterpreterTest extends TestCase
{ {
$inter = new CommandInterpreter(); $inter = new CommandInterpreter();
$cmd = $inter->handle_command(null, $input); $cmd = $inter->handle_command(User::getById(1), $input);
$type = $cmd ? get_class($cmd) : null; $type = $cmd ? get_class($cmd) : null;
$this->assertEquals(strtolower($expectedType), strtolower($type), $comment); $this->assertEquals(strtolower($expectedType), strtolower($type), $comment);

View File

@ -71,12 +71,16 @@ final class NicknameTest extends TestCase
} else { } else {
$stuff = var_export($exception, true); $stuff = var_export($exception, true);
} }
$this->assertTrue($exception && $exception instanceof $expectedException, $this->assertTrue(
$exception && $exception instanceof $expectedException,
"invalid input '$input' expected to fail with $expectedException, " . "invalid input '$input' expected to fail with $expectedException, " .
"got $stuff"); "got $stuff"
);
} else { } else {
$this->assertTrue($normalized == false, $this->assertTrue(
"invalid input '$input' expected to fail"); $normalized == false,
"invalid input '$input' expected to fail"
);
} }
} else { } else {
$msg = "normalized input nickname '$input' expected to normalize to '$expected', got "; $msg = "normalized input nickname '$input' expected to normalize to '$expected', got ";
@ -113,12 +117,12 @@ final class NicknameTest extends TestCase
} else { } else {
$text = "@{$input} awesome! :)"; $text = "@{$input} awesome! :)";
$matches = common_find_mentions_raw($text); $matches = common_find_mentions_raw($text);
$this->assertEquals(1, count($matches)); $this->assertCount(1, $matches);
$this->assertEquals($expected, Nickname::normalize($matches[0][0])); $this->assertEquals($expected, Nickname::normalize($matches[0][0]));
} }
} }
static public function provider() public static function provider()
{ {
return array( return array(
array('evan', 'evan'), array('evan', 'evan'),

View File

@ -31,16 +31,17 @@ if (!defined('STATUSNET')) { // Compatibility
use GNUsocial; use GNUsocial;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PluginList;
use XmppPlugin; use XmppPlugin;
require_once INSTALLDIR . '/lib/util/common.php'; require_once INSTALLDIR . '/lib/util/common.php';
require_once INSTALLDIR . '/plugins/Xmpp/XmppModule.php'; require_once INSTALLDIR . '/plugins/Xmpp/XmppPlugin.php';
final class XmppValidateTest extends TestCase final class XmppValidateTest extends TestCase
{ {
public function setUp(): void public function setUp(): void
{ {
if (!array_key_exists('Xmpp', GNUsocial::getActiveModules())) { if (!PluginList::isPluginActive('Xmpp')) {
$this->markTestSkipped('XmppPlugin is not enabled.'); $this->markTestSkipped('XmppPlugin is not enabled.');
} }
} }