jonnybarnes.uk/tests/Unit/Jobs/ProcessWebMentionJobTest.php
Jonny Barnes ffa9756cb7 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
2019-01-20 17:35:58 +00:00

222 lines
6.5 KiB
PHP

<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Models\Note;
use GuzzleHttp\Client;
use App\Models\WebMention;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Jobs\SaveProfileImage;
use App\Jobs\ProcessWebMention;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\FileSystem\FileSystem;
use Illuminate\Support\Facades\Queue;
use Jonnybarnes\WebmentionsParser\Parser;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ProcessWebMentionJobTest extends TestCase
{
use DatabaseTransactions;
public function tearDown()
{
$fs = new FileSystem();
if ($fs->exists(storage_path() . '/HTML/https')) {
$fs->deleteDirectory(storage_path() . '/HTML/https');
}
parent::tearDown();
}
/**
* @expectedException \App\Exceptions\RemoteContentNotFoundException
*/
public function test_for_exception_from_failure_to_get_webmention()
{
$parser = new Parser();
$mock = new MockHandler([
new Response(404),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$source = 'https://example.org/mention/1/';
$job = new ProcessWebMention($note, $source);
$job->handle($parser, $client);
}
public function test_a_new_webmention_gets_saved()
{
Queue::fake();
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
I liked <a class="u-like-of" href="/notes/1">a note</a>.
</div>
HTML;
$html = str_replace('href="', 'href="' . config('app.url'), $html);
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$source = 'https://example.org/mention/1/';
$job = new ProcessWebMention($note, $source);
$job->handle($parser, $client);
Queue::assertPushed(SaveProfileImage::class);
$this->assertDatabaseHas('webmentions', [
'source' => $source,
'type' => 'like-of',
]);
}
public function test_existing_webmention_gets_updated()
{
Queue::fake();
$parser = new Parser();
$html = <<<HTML
<div class="h-entry">
<p>In reply to <a class="u-in-reply-to" href="/notes/E">a note</a></p>
<div class="e-content">Updated reply</div>
</div>
HTML;
$html = str_replace('href="', 'href="' . config('app.url'), $html);
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(14);
$source = 'https://aaronpk.localhost/reply/1';
$job = new ProcessWebMention($note, $source);
$job->handle($parser, $client);
Queue::assertPushed(SaveProfileImage::class);
$this->assertDatabaseHas('webmentions', [
'source' => $source,
'type' => 'in-reply-to',
'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,
]);
}
}