Improve tests feature

Squashed commit of the following:

commit 13ac266b79b496d62291a07f4f5f81940d217ccb
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Jan 20 17:17:25 2019 +0000

    test on 7.2 and 7.3, download phpcs directly from github

commit 692cc7dfe165a7ecaf9dba9498c868193b749aee
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Jan 20 17:10:16 2019 +0000

    Drop code-coverage for now, PHP7.3 and Xdebug aren’t playing ball at the moment

commit 03d262c47af79cfa0ce4937cc8e196e3b6f5877a
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Jan 20 17:09:48 2019 +0000

    Use an excewption when generating twitter content of a note

commit e2df1e5cebebd30473787c924f495a5687145fb8
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Jan 20 17:08:48 2019 +0000

    Updating dependencies

commit 8008eb53854eddefaa4e302f95353c626b3062ef
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sun Nov 25 16:09:32 2018 +0000

    Fix S3 URLs to use config settings

commit 941864b9d6c6cb9d87b4b95385ada67f55de837c
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sat Nov 24 21:28:19 2018 +0000

    Increase code-coverage in the notes unit tests

commit 4a253d3c5c1854dd8ea01d975bb8e709ae697393
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Nov 20 18:26:59 2018 +0000

    Don’t create new notes in null note test

commit 9616cd2b66bc6f26b2d3266f95cf1be894aaed99
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Nov 20 18:23:05 2018 +0000

    Use Laravel’s FileSystem class to better deal with files/folders in the tearDown methods

commit 294f7961ec03d26cc45845632a97b2521a58f403
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Nov 20 18:22:28 2018 +0000

    Add more unit tests for the geocoding method

commit 43328e3ce249fe8df95770f1275cab97f4ca88bc
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Nov 20 18:18:36 2018 +0000

    Improve the revereseGeoCode method

commit 84424e1d8274bfe62bc5f0a7556e5732bf094178
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Tue Nov 20 15:03:14 2018 +0000

    Test that empty notes are saved to the DB as null

commit 77fd87b81323457ce6f578ed7f359ceb6b3ce6b6
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sat Oct 13 20:20:03 2018 +0100

    Increase test coverage to 100% for the WebMention model

commit 4b6da595dc1efc025470279e9012c2a2a90ec3ef
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sat Oct 13 18:49:33 2018 +0100

    Improving test coverage

commit 895061b8dd0ddf4fbc321e4f371ea148d9b3007f
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Sat Oct 13 17:19:35 2018 +0100

    Improvements in Like tests and code, and WebMentions processing tests and code

commit f9a8b96f2c8b1ef22e97d3dc634ee76d97c25cb5
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Oct 12 22:43:49 2018 +0100

    Don’t track the coverage files in git
This commit is contained in:
Jonny Barnes 2019-01-20 17:35:58 +00:00
parent d43386b3b9
commit ffa9756cb7
17 changed files with 1322 additions and 440 deletions

1
.gitignore vendored
View file

@ -13,4 +13,5 @@ yarn-error.log
/public/files /public/files
/public/keybase.txt /public/keybase.txt
/coverage /coverage
/public/coverage
/LegacyTests /LegacyTests

View file

@ -35,6 +35,7 @@ env:
php: php:
- 7.2 - 7.2
- 7.3
before_install: before_install:
- printf "\n" | pecl install imagick - printf "\n" | pecl install imagick
@ -43,7 +44,7 @@ before_install:
- psql -U travis -c 'create database travis_ci_test' - psql -U travis -c 'create database travis_ci_test'
- psql -U travis -d travis_ci_test -c 'create extension postgis' - psql -U travis -d travis_ci_test -c 'create extension postgis'
- travis_retry composer self-update --preview - travis_retry composer self-update --preview
- pear install pear/PHP_CodeSniffer && phpenv rehash - curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
install: install:
- if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-dist; fi - if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-dist; fi
@ -63,7 +64,7 @@ before_script:
#- sleep 5 #- sleep 5
script: script:
- php vendor/bin/phpunit --coverage-text - php vendor/bin/phpunit
- phpcs - php phpcs.phar
#- php artisan dusk #- php artisan dusk
- php vendor/bin/security-checker security:check --end-point=http://security.sensiolabs.org/check_lock - php vendor/bin/security-checker security:check --end-point=http://security.sensiolabs.org/check_lock

View file

@ -0,0 +1,7 @@
<?php
namespace App\Exceptions;
class TwitterContentException extends \Exception
{
}

View file

@ -10,7 +10,7 @@ use Illuminate\Bus\Queueable;
use Thujohn\Twitter\Facades\Twitter; use Thujohn\Twitter\Facades\Twitter;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\RequestException;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Jonnybarnes\WebmentionsParser\Authorship; use Jonnybarnes\WebmentionsParser\Authorship;
@ -60,7 +60,7 @@ class ProcessLike implements ShouldQueue
], ],
] ]
); );
} catch (ClientException $exception) { } catch (RequestException $exception) {
//no biggie //no biggie
} }

View file

@ -67,7 +67,7 @@ class ProcessWebMention implements ShouldQueue
return; return;
} }
if ($webmention->type == 'like-of') { if ($webmention->type == 'like-of') {
if ($parser->checkLikeOf($microformats, $note->longurl) == false) { if ($parser->checkLikeOf($microformats, $this->note->longurl) == false) {
// it doesnt so delete // it doesnt so delete
$webmention->delete(); $webmention->delete();
@ -75,7 +75,7 @@ class ProcessWebMention implements ShouldQueue
} // note we dont need to do anything if it still is a like } // note we dont need to do anything if it still is a like
} }
if ($webmention->type == 'repost-of') { if ($webmention->type == 'repost-of') {
if ($parser->checkRepostOf($microformats, $note->longurl) == false) { if ($parser->checkRepostOf($microformats, $this->note->longurl) == false) {
// it doesnt so delete // it doesnt so delete
$webmention->delete(); $webmention->delete();

View file

@ -17,6 +17,7 @@ use League\CommonMark\HtmlRenderer;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Jonnybarnes\EmojiA11y\EmojiModifier; use Jonnybarnes\EmojiA11y\EmojiModifier;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use App\Exceptions\TwitterContentException;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Jonnybarnes\CommonmarkLinkify\LinkifyExtension; use Jonnybarnes\CommonmarkLinkify\LinkifyExtension;
@ -362,21 +363,20 @@ class Note extends Model
* *
* That is we swap the contacts names for their known Twitter handles. * That is we swap the contacts names for their known Twitter handles.
* *
* @return string|null * @return string
*/ */
public function getTwitterContentAttribute(): ?string public function getTwitterContentAttribute(): string
{ {
if ($this->contacts === null) { // check for contacts
return null; if ($this->contacts === null || count($this->contacts) === 0) {
} throw new TwitterContentException('There are no contacts for this note');
if (count($this->contacts) === 0) {
return null;
} }
// here we check the matched contact from the note corresponds to a contact
// in the database
if (count(array_unique(array_values($this->contacts))) === 1 if (count(array_unique(array_values($this->contacts))) === 1
&& array_unique(array_values($this->contacts))[0] === null) { && array_unique(array_values($this->contacts))[0] === null) {
return null; throw new TwitterContentException('The matched contact is not in the database');
} }
// swap in twitter usernames // swap in twitter usernames
@ -530,7 +530,7 @@ class Note extends Model
$latlng = $latitude . ',' . $longitude; $latlng = $latitude . ',' . $longitude;
return Cache::get($latlng, function () use ($latlng, $latitude, $longitude) { return Cache::get($latlng, function () use ($latlng, $latitude, $longitude) {
$guzzle = new Client(); $guzzle = resolve(Client::class);
$response = $guzzle->request('GET', 'https://nominatim.openstreetmap.org/reverse', [ $response = $guzzle->request('GET', 'https://nominatim.openstreetmap.org/reverse', [
'query' => [ 'query' => [
'format' => 'json', 'format' => 'json',
@ -542,9 +542,13 @@ class Note extends Model
'headers' => ['User-Agent' => 'jonnybarnes.uk via Guzzle, email jonny@jonnybarnes.uk'], 'headers' => ['User-Agent' => 'jonnybarnes.uk via Guzzle, email jonny@jonnybarnes.uk'],
]); ]);
$json = json_decode((string) $response->getBody()); $json = json_decode((string) $response->getBody());
if (isset($json->address->town)) { if (isset($json->address->suburb)) {
$locality = $json->address->suburb;
if (isset($json->address->city)) {
$locality .= ', ' . $json->address->city;
}
$address = '<span class="p-locality">' $address = '<span class="p-locality">'
. $json->address->town . $locality
. '</span>, <span class="p-country-name">' . '</span>, <span class="p-country-name">'
. $json->address->country . $json->address->country
. '</span>'; . '</span>';
@ -553,7 +557,11 @@ class Note extends Model
return $address; return $address;
} }
if (isset($json->address->city)) { if (isset($json->address->city)) {
$address = $json->address->city . ', ' . $json->address->country; $address = '<span class="p-locality">'
. $json->address->city
. '</span>, <span class="p-country-name">'
. $json->address->country
. '</span>';
Cache::forever($latlng, $address); Cache::forever($latlng, $address);
return $address; return $address;

View file

@ -34,10 +34,9 @@
}, },
"require-dev": { "require-dev": {
"barryvdh/laravel-debugbar": "~3.0", "barryvdh/laravel-debugbar": "~3.0",
"codedungeon/phpunit-result-printer": "^0.22.0", "codedungeon/phpunit-result-printer": "^0.24.0",
"filp/whoops": "~2.0", "filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4", "fzaninotto/faker": "~1.4",
"jakub-onderka/php-parallel-lint": "^1.0.0",
"laravel/dusk": "^4.0", "laravel/dusk": "^4.0",
"mockery/mockery": "~1.0", "mockery/mockery": "~1.0",
"nunomaduro/collision": "^2.0", "nunomaduro/collision": "^2.0",

1243
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -15,12 +15,11 @@
@if($media->type == 'download') <p><a class="u-attachment" href="{{ $media->url }}">Download the attached media</a></p>@endif @if($media->type == 'download') <p><a class="u-attachment" href="{{ $media->url }}">Download the attached media</a></p>@endif
@endforeach @endforeach
</div> </div>
@if($note->twitter_content) <div class="p-bridgy-twitter-content"> @php
{!! $note->twitter_content !!} try {
</div>@endif echo '<div class="p-bridgy-twitter-content">' . $note->twitter_content . '</div>';
@if($note->facebook_content) <div class="p-bridgy-facebook-content"> } catch (App\Exceptions\TwitterContentException $exception) {}
{!! $note->facebook_content !!} @endphp
</div>@endif
<div class="note-metadata"> <div class="note-metadata">
<div> <div>
<a class="u-url" href="/notes/{{ $note->nb60id }}"><time class="dt-published" datetime="{{ $note->iso8601 }}" title="{{ $note->iso8601 }}">{{ $note->humandiff }}</time></a>@if($note->client) via <a class="client" href="{{ $note->client->client_url }}">{{ $note->client->client_name }}</a>@endif <a class="u-url" href="/notes/{{ $note->nb60id }}"><time class="dt-published" datetime="{{ $note->iso8601 }}" title="{{ $note->iso8601 }}">{{ $note->humandiff }}</time></a>@if($note->client) via <a class="client" href="{{ $note->client->client_url }}">{{ $note->client->client_name }}</a>@endif

View file

@ -173,4 +173,50 @@ END;
$this->assertNull(Like::find($id)->author_name); $this->assertNull(Like::find($id)->author_name);
} }
public function test_process_like_that_is_tweet()
{
$like = new Like();
$like->url = 'https://twitter.com/jonnybarnes/status/1050823255123251200';
$like->save();
$id = $like->id;
$job = new ProcessLike($like);
$mock = new MockHandler([
new Response(201, [], json_encode([
'url' => 'https://twitter.com/likes/id',
])),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->bind(Client::class, $client);
$authorship = new Authorship();
$job->handle($client, $authorship);
$this->assertEquals('Jonny Barnes', Like::find($id)->author_name);
}
public function test_process_like_that_is_tweet_with_oembed_error()
{
$like = new Like();
$like->url = 'https://twitter.com/jonnybarnes/status/1050823255123251200';
$like->save();
$id = $like->id;
$job = new ProcessLike($like);
$mock = new MockHandler([
new Response(500),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->bind(Client::class, $client);
$authorship = new Authorship();
$job->handle($client, $authorship);
$this->assertEquals('Jonny Barnes', Like::find($id)->author_name);
}
} }

View file

@ -4,6 +4,7 @@ namespace Tests\Feature;
use Tests\TestCase; use Tests\TestCase;
use App\Models\WebMention; use App\Models\WebMention;
use Illuminate\FileSystem\FileSystem;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -44,13 +45,13 @@ class ParseCachedWebMentionsTest extends TestCase
public function tearDown() public function tearDown()
{ {
unlink(storage_path('HTML') . '/https/aaronpk.localhost/reply/1'); $fs = new FileSystem();
rmdir(storage_path('HTML') . '/https/aaronpk.localhost/reply'); if ($fs->exists(storage_path() . '/HTML/https')) {
rmdir(storage_path('HTML') . '/https/aaronpk.localhost'); $fs->deleteDirectory(storage_path() . '/HTML/https');
rmdir(storage_path('HTML') . '/https'); }
unlink(storage_path('HTML') . '/http/tantek.com/index.html'); if ($fs->exists(storage_path() . '/HTML/http')) {
rmdir(storage_path('HTML') . '/http/tantek.com'); $fs->deleteDirectory(storage_path() . '/HTML/http');
rmdir(storage_path('HTML') . '/http'); }
parent::tearDown(); parent::tearDown();
} }

View file

@ -8,13 +8,17 @@ use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use App\Jobs\DownloadWebMention; use App\Jobs\DownloadWebMention;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use Illuminate\FileSystem\FileSystem;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
class DownloadWebMentionJobTest extends TestCase class DownloadWebMentionJobTest extends TestCase
{ {
public function tearDown() public function tearDown()
{ {
$this->delTree(storage_path('HTML/https')); $fs = new FileSystem();
if ($fs->exists(storage_path() . '/HTML/https')) {
$fs->deleteDirectory(storage_path() . '/HTML/https');
}
parent::tearDown(); parent::tearDown();
} }
@ -100,13 +104,4 @@ HTML;
$this->assertFileExists(storage_path('HTML/https/example.org/reply-one/index.html')); $this->assertFileExists(storage_path('HTML/https/example.org/reply-one/index.html'));
} }
private function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
} }

View file

@ -5,11 +5,13 @@ namespace Tests\Unit\Jobs;
use Tests\TestCase; use Tests\TestCase;
use App\Models\Note; use App\Models\Note;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use App\Models\WebMention;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use App\Jobs\SaveProfileImage; use App\Jobs\SaveProfileImage;
use App\Jobs\ProcessWebMention; use App\Jobs\ProcessWebMention;
use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Handler\MockHandler;
use Illuminate\FileSystem\FileSystem;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
use Jonnybarnes\WebmentionsParser\Parser; use Jonnybarnes\WebmentionsParser\Parser;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -20,18 +22,9 @@ class ProcessWebMentionJobTest extends TestCase
public function tearDown() public function tearDown()
{ {
if (file_exists(storage_path() . '/HTML/https/example.org/mention/1/index.html')) { $fs = new FileSystem();
unlink(storage_path() . '/HTML/https/example.org/mention/1/index.html'); if ($fs->exists(storage_path() . '/HTML/https')) {
rmdir(storage_path() . '/HTML/https/example.org/mention/1'); $fs->deleteDirectory(storage_path() . '/HTML/https');
rmdir(storage_path() . '/HTML/https/example.org/mention');
rmdir(storage_path() . '/HTML/https/example.org');
rmdir(storage_path() . '/HTML/https');
}
if (file_exists(storage_path() . '/HTML/https/aaronpk.localhost/reply/1')) {
unlink(storage_path() . '/HTML/https/aaronpk.localhost/reply/1');
rmdir(storage_path() . '/HTML/https/aaronpk.localhost/reply');
rmdir(storage_path() . '/HTML/https/aaronpk.localhost');
rmdir(storage_path() . '/HTML/https');
} }
parent::tearDown(); parent::tearDown();
} }
@ -118,4 +111,112 @@ HTML;
'mf2' => '{"rels": [], "items": [{"type": ["h-entry"], "properties": {"content": [{"html": "Updated reply", "value": "Updated reply"}], "in-reply-to": ["' . config('app.url') . '/notes/E"]}}], "rel-urls": []}', 'mf2' => '{"rels": [], "items": [{"type": ["h-entry"], "properties": {"content": [{"html": "Updated reply", "value": "Updated reply"}], "in-reply-to": ["' . config('app.url') . '/notes/E"]}}], "rel-urls": []}',
]); ]);
} }
public function test_webmention_reply_gets_deleted()
{
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-in-reply-to" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">Replying to someone else</div>
</div>
HTML;
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;
$webmention->target = config('app.url') . '/notes/E';
$webmention->type = 'in-reply-to';
$webmention->save();
$this->assertDatabaseHas('webmentions', [
'source' => $source,
]);
$job = new ProcessWebMention($note, $source);
$job->handle($parser, $client);
$this->assertDatabaseMissing('webmentions', [
'source' => $source,
]);
}
public function test_webmention_like_gets_deleted()
{
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-like-of" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">I like someone else now</div>
</div>
HTML;
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;
$webmention->target = config('app.url') . '/notes/E';
$webmention->type = 'like-of';
$webmention->save();
$this->assertDatabaseHas('webmentions', [
'source' => $source,
]);
$job = new ProcessWebMention($note, $source);
$job->handle($parser, $client);
$this->assertDatabaseMissing('webmentions', [
'source' => $source,
]);
}
public function test_webmention_repost_gets_deleted()
{
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-repost-of" href="https://other.com/notes/E">a note</a></p>
<div class="e-content">Reposting someone else</div>
</div>
HTML;
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$source = 'https://example.org/reply/1';
$webmention = new WebMention();
$webmention->source = $source;
$webmention->target = config('app.url') . '/notes/E';
$webmention->type = 'repost-of';
$webmention->save();
$this->assertDatabaseHas('webmentions', [
'source' => $source,
]);
$job = new ProcessWebMention($note, $source);
$job->handle($parser, $client);
$this->assertDatabaseMissing('webmentions', [
'source' => $source,
]);
}
} }

View file

@ -4,13 +4,49 @@ namespace Tests\Unit;
use Tests\TestCase; use Tests\TestCase;
use App\Models\Like; use App\Models\Like;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LikesTest extends TestCase class LikesTest extends TestCase
{ {
use DatabaseTransactions;
public function test_setting_author_url() public function test_setting_author_url()
{ {
$like = new Like(); $like = new Like();
$like->author_url = 'https://joe.bloggs/'; $like->author_url = 'https://joe.bloggs/';
$this->assertEquals('https://joe.bloggs', $like->author_url); $this->assertEquals('https://joe.bloggs', $like->author_url);
} }
public function test_plaintext_like_content()
{
$like = new Like();
$like->url = 'https://example.org/post/123';
$like->content = 'some plaintext content';
$like->save();
$this->assertEquals('some plaintext content', $like->content);
}
public function test_html_like_content_is_filtered()
{
$htmlEvil = <<<HTML
<div class="h-entry">
<div class="e-content">
<p>Hello</p>
<img src="javascript:evil();" onload="evil();" />
</div>
</div>
HTML;
$htmlFiltered = <<<HTML
<p>Hello</p>
HTML;
$like = new Like();
$like->url = 'https://example.org/post/123';
$like->content = $htmlEvil;
$like->save();
// HTMLPurifer will leave the whitespace before the <img> tag
// trim it, saving whitespace in $htmlFilteres can get removed by text editors
$this->assertEquals($htmlFiltered, trim($like->content));
}
} }

View file

@ -13,4 +13,13 @@ class MediaTest extends TestCase
$note = $media->note; $note = $media->note;
$this->assertInstanceOf('App\Models\Note', $note); $this->assertInstanceOf('App\Models\Note', $note);
} }
public function test_media_absolute_url_returned_unmodified()
{
$absoluteUrl = 'https://instagram-cdn.com/image/uuid';
$media = new Media();
$media->path = $absoluteUrl;
$this->assertEquals($absoluteUrl, $media->url);
}
} }

View file

@ -3,11 +3,18 @@
namespace Tests\Unit; namespace Tests\Unit;
use Tests\TestCase; use Tests\TestCase;
use App\Models\{Note, Tag}; use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Models\{Media, Note, Tag};
use GuzzleHttp\Handler\MockHandler;
use Thujohn\Twitter\Facades\Twitter; use Thujohn\Twitter\Facades\Twitter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NotesTest extends TestCase class NotesTest extends TestCase
{ {
use DatabaseTransactions;
/** /**
* Test the getNoteAttribute method. This will then also call the * Test the getNoteAttribute method. This will then also call the
* relevant sub-methods. * relevant sub-methods.
@ -96,4 +103,190 @@ class NotesTest extends TestCase
'tag_id' => $tag->id, 'tag_id' => $tag->id,
]); ]);
} }
/** @test */
public function blank_note_should_be_saved_as_null()
{
$note = new Note(['note' => '']);
$this->assertNull($note->note);
}
/** @test */
public function reverse_geocode_an_attraction()
{
$json = <<<JSON
{"place_id":"198791063","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"relation","osm_id":"5208404","lat":"51.50084125","lon":"-0.142990166340849","display_name":"Buckingham Palace, Ambassador's Court, St. James's, Victoria, Westminster, London, Greater London, England, SW1E 6LA, United Kingdom","address":{"attraction":"Buckingham Palace","road":"Ambassador's Court","neighbourhood":"St. James's","suburb":"Victoria","city":"London","state_district":"Greater London","state":"England","postcode":"SW1E 6LA","country":"UK","country_code":"gb"},"boundingbox":["51.4997342","51.5019473","-0.143984","-0.1413002"]}
JSON;
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$note = new Note();
$address = $note->reverseGeoCode(51.50084, -0.14264);
$this->assertEquals('<span class="p-locality">Victoria, London</span>, <span class="p-country-name">UK</span>', $address);
}
/** @test */
public function reverse_geocode_a_suburb()
{
$json = <<<JSON
{"place_id":"96518506","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"94107885","lat":"51.0225764535969","lon":"0.906664040464189","display_name":"Melon Lane, Newchurch, Shepway, Kent, South East, England, TN29 0AS, United Kingdom","address":{"road":"Melon Lane","suburb":"Newchurch","city":"Shepway","county":"Kent","state_district":"South East","state":"England","postcode":"TN29 0AS","country":"UK","country_code":"gb"},"boundingbox":["51.0140377","51.0371494","0.8873312","0.9109506"]}
JSON;
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$note = new Note();
$address = $note->reverseGeoCode(51.02, 0.91);
$this->assertEquals('<span class="p-locality">Newchurch, Shepway</span>, <span class="p-country-name">UK</span>', $address);
}
/** @test */
public function reverse_geocode_a_city()
{
// Note Ive modified this JSON response so it only contains the
// city the Uni is in
$json = <<<JSON
{"place_id":"198561071","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"relation","osm_id":"1839026","lat":"53.46600455","lon":"-2.23300880782987","display_name":"University of Manchester - Main Campus, Brunswick Street, Curry Mile, Ardwick, Manchester, Greater Manchester, North West England, England, M13 9NR, United Kingdom","address":{"university":"University of Manchester - Main Campus","city":"Manchester","county":"Greater Manchester","state_district":"North West England","state":"England","postcode":"M13 9NR","country":"UK","country_code":"gb"},"boundingbox":["53.4598667","53.4716848","-2.2390346","-2.2262754"]}
JSON;
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$note = new Note();
$address = $note->reverseGeoCode(53.466277988406, -2.2304474827445);
$this->assertEquals('<span class="p-locality">Manchester</span>, <span class="p-country-name">UK</span>', $address);
}
/** @test */
public function reverse_geocode_a_county()
{
// Note Ive removed everything below county to test for querires where
// thats all that is returned
$json = <<<JSON
{"place_id":"98085404","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"103703318","lat":"51.0997470194065","lon":"0.609897771085209","display_name":"Biddenden, Ashford, Kent, South East, England, TN27 8ET, United Kingdom","address":{"county":"Kent","state_district":"South East","state":"England","postcode":"TN27 8ET","country":"UK","country_code":"gb"},"boundingbox":["51.0986632","51.104459","0.5954434","0.6167775"]}
JSON;
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$note = new Note();
$address = $note->reverseGeoCode(51.1, 0.61);
$this->assertEquals('<span class="p-region">Kent</span>, <span class="p-country-name">UK</span>', $address);
}
/** @test */
public function reverse_geocode_a_country()
{
// Note Ive removed everything below country to test for querires where
// thats all that is returned
$json = <<<JSON
{"place_id":"120553244","licence":"Data © OpenStreetMap contributors, ODbL 1.0. https:\/\/osm.org\/copyright","osm_type":"way","osm_id":"191508282","lat":"54.3004150140189","lon":"-9.39993720828084","display_name":"R314, Doonfeeny Lower, Ballycastle ED, Ballina, County Mayo, Connacht, Ireland","address":{"country":"Ireland","country_code":"ie"},"boundingbox":["54.2964027","54.3045856","-9.4337961","-9.3960403"]}
JSON;
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$note = new Note();
$address = $note->reverseGeoCode(54.3, 9.4);
$this->assertEquals('<span class="p-country-name">Ireland</span>', $address);
}
/** @test */
public function add_image_element_to_note_content()
{
$media = new Media([
'type' => 'image',
'path' => 'test.png']
);
$media->save();
$note = new Note(['note' => 'A nice image']);
$note->save();
$note->media()->save($media);
$expected = "<p>A nice image</p>
<img src=\"" . config('filesystems.disks.s3.url') . "/test.png\" alt=\"\">";
$this->assertEquals($expected, $note->content);
}
/** @test */
public function add_video_element_to_note_content()
{
$media = new Media([
'type' => 'video',
'path' => 'test.mkv']
);
$media->save();
$note = new Note(['note' => 'A nice video']);
$note->save();
$note->media()->save($media);
$expected = "<p>A nice video</p>
<video src=\"" . config('filesystems.disks.s3.url') . "/test.mkv\">";
$this->assertEquals($expected, $note->content);
}
/** @test */
public function add_audio_element_to_note_content()
{
$media = new Media([
'type' => 'audio',
'path' => 'test.flac']
);
$media->save();
$note = new Note(['note' => 'Some nice audio']);
$note->save();
$note->media()->save($media);
$expected = "<p>Some nice audio</p>
<audio src=\"" . config('filesystems.disks.s3.url') . "/test.flac\">";
$this->assertEquals($expected, $note->content);
}
/** @test */
public function blank_note_content()
{
$note = new Note();
$this->assertEquals('A blank note', $note->content);
}
/** @test */
public function twitter_content_is_null_when_oembed_error_occurs()
{
Twitter::shouldReceive('getOembed')
->once()
->andThrow('Exception');
$note = new Note();
$note->in_reply_to = 'https://twitter.com/foo';
$this->assertNull($note->twitter);
}
} }

View file

@ -118,4 +118,11 @@ class WebMentionTest extends TestCase
$webmention = new WebMention(); $webmention = new WebMention();
$this->assertNull($webmention->reply); $this->assertNull($webmention->reply);
} }
public function test_get_reply_attribute_with_mf2_without_html_returns_null()
{
$webmention = new WebMention();
$webmention->mf2 = json_encode(['no_html' => 'found_here']);
$this->assertNull($webmention->reply);
}
} }