From 5d69f7411dad575789def59bc8ef0cd8823af809 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 27 Dec 2021 20:02:37 +0100 Subject: [PATCH] Add MessageDirect\Search tests --- .../Api/Friendica/DirectMessages/Search.php | 2 - tests/datasets/mail/mail.fixture.php | 17 ++++ .../Friendica/DirectMessages/SearchTest.php | 84 +++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/datasets/mail/mail.fixture.php create mode 100644 tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php diff --git a/src/Module/Api/Friendica/DirectMessages/Search.php b/src/Module/Api/Friendica/DirectMessages/Search.php index c0322a95e..679accb33 100644 --- a/src/Module/Api/Friendica/DirectMessages/Search.php +++ b/src/Module/Api/Friendica/DirectMessages/Search.php @@ -73,8 +73,6 @@ class Search extends BaseApi // message if nothing was found if (!DBA::isResult($mails)) { - $success = ['success' => false, 'search_results' => 'problem with query']; - } elseif (count($mails) == 0) { $success = ['success' => false, 'search_results' => 'nothing found']; } else { $ret = []; diff --git a/tests/datasets/mail/mail.fixture.php b/tests/datasets/mail/mail.fixture.php new file mode 100644 index 000000000..215356bea --- /dev/null +++ b/tests/datasets/mail/mail.fixture.php @@ -0,0 +1,17 @@ + [ + [ + 'uid' => 42, + 'author-id' => 44, + 'uri-id' => 44, + 'parent-uri-id' => 44, + 'thr-parent-id' => 44, + 'guid' => '123456', + 'from-name' => 'Tester', + 'title' => 'test message', + 'body' => 'this is a test', + ], + ], +]; diff --git a/tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php b/tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php new file mode 100644 index 000000000..c66a1f42e --- /dev/null +++ b/tests/src/Module/Api/Friendica/DirectMessages/SearchTest.php @@ -0,0 +1,84 @@ +. + * + */ + +namespace Friendica\Test\src\Module\Api\Friendica\DirectMessages; + +use Friendica\App\Router; +use Friendica\DI; +use Friendica\Factory\Api\Twitter\DirectMessage; +use Friendica\Module\Api\Friendica\DirectMessages\Search; +use Friendica\Test\src\Module\Api\ApiTest; +use Psr\Log\NullLogger; + +class SearchTest extends ApiTest +{ + public function testEmpty() + { + $directMessage = new DirectMessage(new NullLogger(), DI::dba(), DI::twitterUser()); + + $response = (new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]))->run(); + + $json = $this->toJson($response); + + $assert = new \stdClass(); + $assert->result = 'error'; + $assert->message = 'searchstring not specified'; + + self::assertEquals($assert, $json); + } + + public function testMail() + { + $this->loadFixture(__DIR__ . '/../../../../../datasets/mail/mail.fixture.php', DI::dba()); + + $directMessage = new DirectMessage(new NullLogger(), DI::dba(), DI::twitterUser()); + + $search = new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]); + $response = $search->run(['searchstring' => 'test']); + + $json = $this->toJson($response); + + self::assertTrue($json->success); + + foreach ($json->search_results as $searchResult) { + self::assertIsObject($searchResult->sender); + self::assertIsInt($searchResult->id); + self::assertIsInt($searchResult->sender_id); + self::assertIsObject($searchResult->recipient); + } + } + + public function testNothingFound() + { + $directMessage = new DirectMessage(new NullLogger(), DI::dba(), DI::twitterUser()); + + $search = new Search($directMessage, DI::dba(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::GET]); + $response = $search->run(['searchstring' => 'test']); + + $json = $this->toJson($response); + + $assert = new \stdClass(); + $assert->success = false; + $assert->search_results = 'nothing found'; + + self::assertEquals($assert, $json); + } +}