Move some tests into their own subfodlers

This commit is contained in:
Jonny Barnes 2017-12-18 17:53:22 +00:00
parent 0dd6adfa0e
commit e1aa814d8c
18 changed files with 29 additions and 31 deletions

View file

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use App\Jobs\AddClientToDatabase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AddClientToDatabaseJobTest extends TestCase
{
use DatabaseTransactions;
public function test_job_adds_client()
{
$job = new AddClientToDatabase('https://example.org/client');
$job->handle();
$this->assertDatabaseHas('clients', [
'client_url' => 'https://example.org/client',
'client_name' => 'https://example.org/client',
]);
}
}

View file

@ -0,0 +1,112 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Jobs\DownloadWebMention;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Foundation\Testing\RefreshDatabase;
class DownloadWebMentionJobTest extends TestCase
{
public function tearDown()
{
$this->delTree(storage_path('HTML/https'));
parent::tearDown();
}
public function test_the_job_saves_html()
{
$this->assertFileNotExists(storage_path('HTML/https'));
$source = 'https://example.org/reply/1';
$html = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
$html = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html);
$mock = new MockHandler([
new Response(200, ['X-Foo' => 'Bar'], $html),
new Response(200, ['X-Foo' => 'Bar'], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$job = new DownloadWebMention($source);
$job->handle($client);
$this->assertFileExists(storage_path('HTML/https'));
$job->handle($client);
$this->assertFileNotExists(storage_path('HTML/https/example.org/reply') . '/1.' . date('Y-m-d') . '.backup');
}
public function test_the_job_saves_html_and_backup()
{
$this->assertFileNotExists(storage_path('HTML/https'));
$source = 'https://example.org/reply/1';
$html = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
$html2 = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
<a class="u-repost-of" href=""></a>
</div>
HTML;
$html = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html);
$html2 = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html2);
$mock = new MockHandler([
new Response(200, ['X-Foo' => 'Bar'], $html),
new Response(200, ['X-Foo' => 'Bar'], $html2),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$job = new DownloadWebMention($source);
$job->handle($client);
$this->assertFileExists(storage_path('HTML/https'));
$job->handle($client);
$this->assertFileExists(storage_path('HTML/https/example.org/reply') . '/1.' . date('Y-m-d') . '.backup');
}
public function test_an_index_html_file()
{
$this->assertFileNotExists(storage_path('HTML/https'));
$source = 'https://example.org/reply-one/';
$html = <<<HTML
<div class="h-entry">
<a class="u-like-of" href=""></a>
</div>
HTML;
$html = str_replace('href=""', 'href="' . config('app.url') . '/notes/A"', $html);
$mock = new MockHandler([
new Response(200, ['X-Foo' => 'Bar'], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$job = new DownloadWebMention($source);
$job->handle($client);
$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

@ -0,0 +1,60 @@
<?php
namespace Tests\Unit\Jobs;
use App\Bookmark;
use Tests\TestCase;
use Ramsey\Uuid\Uuid;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use App\Jobs\ProcessBookmark;
use GuzzleHttp\Psr7\Response;
use App\Services\BookmarkService;
use GuzzleHttp\Handler\MockHandler;
use App\Exceptions\InternetArchiveException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ProcessBookmarkJobTest extends TestCase
{
use DatabaseTransactions;
public function test_screenshot_and_archive_link_are_saved()
{
$bookmark = Bookmark::find(1);
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')
->willReturn($uuid->toString());
$service->method('getArchiveLink')
->willReturn('https://web.archive.org/web/1234');
$this->app->instance(BookmarkService::class, $service);
$job = new ProcessBookmark($bookmark);
$job->handle();
$this->assertDatabaseHas('bookmarks', [
'screenshot' => $uuid->toString(),
'archive' => 'https://web.archive.org/web/1234',
]);
}
public function test_exception_casesu_null_value_for_archive_link()
{
$bookmark = Bookmark::find(1);
$uuid = Uuid::uuid4();
$service = $this->createMock(BookmarkService::class);
$service->method('saveScreenshot')
->willReturn($uuid->toString());
$service->method('getArchiveLink')
->will($this->throwException(new InternetArchiveException));
$this->app->instance(BookmarkService::class, $service);
$job = new ProcessBookmark($bookmark);
$job->handle();
$this->assertDatabaseHas('bookmarks', [
'screenshot' => $uuid->toString(),
'archive' => null,
]);
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace Tests\Unit\Jobs;
use Storage;
use Tests\TestCase;
use App\Jobs\ProcessMedia;
use Intervention\Image\ImageManager;
class ProcessMediaJobTest extends TestCase
{
public function test_job_does_nothing_to_non_image()
{
Storage::fake('s3');
$manager = app()->make(ImageManager::class);
Storage::disk('local')->put('file.txt', 'This is not an image');
$job = new ProcessMedia('file.txt');
$job->handle($manager);
$this->assertFalse(file_exists(storage_path('app') . '/file.txt'));
}
public function test_job_does_nothing_to_small_images()
{
Storage::fake('s3');
$manager = app()->make(ImageManager::class);
Storage::disk('local')->put('aaron.png', file_get_contents(__DIR__.'/../../aaron.png'));
$job = new ProcessMedia('aaron.png');
$job->handle($manager);
$this->assertFalse(file_exists(storage_path('app') . '/aaron.png'));
}
public function test_large_images_have_smaller_files_created()
{
$manager = app()->make(ImageManager::class);
Storage::disk('local')->put('test-image.jpg', file_get_contents(__DIR__.'/../../test-image.jpg'));
Storage::fake('s3');
$job = new ProcessMedia('test-image.jpg');
$job->handle($manager);
Storage::disk('s3')->assertExists('media/test-image-small.jpg');
Storage::disk('s3')->assertExists('media/test-image-medium.jpg');
$this->assertFalse(file_exists(storage_path('app') . '/test-image.jpg'));
}
}

View file

@ -0,0 +1,121 @@
<?php
namespace Tests\Unit\Jobs;
use App\Note;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Jobs\SaveProfileImage;
use App\Jobs\ProcessWebMention;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Support\Facades\Queue;
use Jonnybarnes\WebmentionsParser\Parser;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ProcessWebMentionJobTest extends TestCase
{
use DatabaseTransactions;
public function tearDown()
{
if (file_exists(storage_path() . '/HTML/https/example.org/mention/1/index.html')) {
unlink(storage_path() . '/HTML/https/example.org/mention/1/index.html');
rmdir(storage_path() . '/HTML/https/example.org/mention/1');
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();
}
/**
* @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": {"name": ["In reply to a note \\n Updated reply"], "content": [{"html": "Updated reply", "value": "Updated reply"}], "in-reply-to": ["' . config('app.url') . '/notes/E"]}}]}',
]);
}
}

View file

@ -0,0 +1,103 @@
<?php
namespace Tests\Unit\Jobs;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use App\Jobs\SaveProfileImage;
use GuzzleHttp\Handler\MockHandler;
use Jonnybarnes\WebmentionsParser\Authorship;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException;
class SaveProfileImageJobTest extends TestCase
{
public function tearDown()
{
if (file_exists(public_path() . '/assets/profile-images/example.org/image')) {
unlink(public_path() . '/assets/profile-images/example.org/image');
rmdir(public_path() . '/assets/profile-images/example.org');
}
parent::tearDown();
}
public function test_authorship_algo_exception_return_null()
{
$mf = ['items' => []];
$authorship = $this->createMock(Authorship::class);
$authorship->method('findAuthor')
->will($this->throwException(new AuthorshipParserException));
$job = new SaveProfileImage($mf);
$this->assertNull($job->handle($authorship));
}
public function test_we_dont_process_twitter_images()
{
$mf = ['items' => []];
$author = [
'properties' => [
'photo' => ['https://pbs.twimg.com/abc.jpg'],
'url' => ['https://twitter.com/profile'],
],
];
$authorship = $this->createMock(Authorship::class);
$authorship->method('findAuthor')
->willReturn($author);
$job = new SaveProfileImage($mf);
$this->assertNull($job->handle($authorship));
}
public function test_saving_of_remote_image()
{
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'image/jpeg'], 'fake jpeg image'),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$mf = ['items' => []];
$author = [
'properties' => [
'photo' => ['https://example.org/profile.jpg'],
'url' => ['https://example.org'],
],
];
$authorship = $this->createMock(Authorship::class);
$authorship->method('findAuthor')
->willReturn($author);
$job = new SaveProfileImage($mf);
$job->handle($authorship);
$this->assertFileExists(public_path() . '/assets/profile-images/example.org/image');
}
public function test_copying_of_local_image()
{
$mock = new MockHandler([
new Response(404),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$mf = ['items' => []];
$author = [
'properties' => [
'photo' => ['https://example.org/profile.jpg'],
'url' => ['https://example.org'],
],
];
$authorship = $this->createMock(Authorship::class);
$authorship->method('findAuthor')
->willReturn($author);
$job = new SaveProfileImage($mf);
$job->handle($authorship);
$this->assertFileEquals(
public_path() . '/assets/profile-images/default-image',
public_path() . '/assets/profile-images/example.org/image'
);
}
}

View file

@ -0,0 +1,102 @@
<?php
namespace Tests\Unit\Jobs;
use App\Note;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use App\Jobs\SendWebMentions;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Foundation\Testing\RefreshDatabase;
class SendWebMentionJobTest extends TestCase
{
public function test_dicover_endoint_method_on_self()
{
$note = new Note();
$job = new SendWebMentions($note);
$this->assertNull($job->discoverWebmentionEndpoint(config('app.url')));
$this->assertNull($job->discoverWebmentionEndpoint('/notes/tagged/test'));
}
public function test_discover_endpoint_gets_link_from_headers()
{
$url = 'https://example.org/webmention';
$mock = new MockHandler([
new Response(200, ['Link' => '<' . $url . '>; rel="webmention"']),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$job = new SendWebMentions(new Note());
$this->assertEquals($url, $job->discoverWebmentionEndpoint('https://example.org'));
}
public function test_discover_endpoint_correctly_parses_html()
{
$html = '<link rel="webmention" href="https://example.org/webmention">';
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$job = new SendWebMentions(new Note());
$this->assertEquals(
'https://example.org/webmention',
$job->discoverWebmentionEndpoint('https://example.org')
);
}
public function test_discover_endpoint_correctly_parses_html_legacy()
{
$html = '<link rel="http://webmention.org/" href="https://example.org/webmention">';
$mock = new MockHandler([
new Response(200, [], $html),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$job = new SendWebMentions(new Note());
$this->assertEquals(
'https://example.org/webmention',
$job->discoverWebmentionEndpoint('https://example.org')
);
}
public function test_empty_note_does_nothing()
{
$job = new SendWebMentions(new Note());
$this->assertNull($job->handle());
}
public function test_resolve_uri()
{
$uri = '/blog/post';
$base = 'https://example.org/';
$job = new SendWebMentions(new Note());
$this->assertEquals('https://example.org/blog/post', $job->resolveUri($uri, $base));
}
public function test_the_job()
{
$html = '<link rel="http://webmention.org/" href="https://example.org/webmention">';
$mock = new MockHandler([
new Response(200, [], $html),
new Response(202),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$note = new Note();
$note->note = 'Hi [Aaron](https://aaronparecki.com)';
$job = new SendWebMentions($note);
$this->assertNull($job->handle());
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Jobs;
use App\Bookmark;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use App\Jobs\SyndicateBookmarkToFacebook;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SyndicateBookmarkToFacebookJobTest extends TestCase
{
use DatabaseTransactions;
public function test_the_job()
{
$json = json_encode([
'url' => 'https://facebook.com/123'
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$bookmark = Bookmark::find(1);
$job = new SyndicateBookmarkToFacebook($bookmark);
$job->handle($client);
$this->assertDatabaseHas('bookmarks', [
'id' => 1,
'syndicates' => '{"facebook": "https://facebook.com/123"}',
]);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Jobs;
use App\Bookmark;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use App\Jobs\SyndicateBookmarkToTwitter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SyndicateBookmarkToTwitterJobTest extends TestCase
{
use DatabaseTransactions;
public function test_the_job()
{
$json = json_encode([
'url' => 'https://twitter.com/123'
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$bookmark = Bookmark::find(1);
$job = new SyndicateBookmarkToTwitter($bookmark);
$job->handle($client);
$this->assertDatabaseHas('bookmarks', [
'id' => 1,
'syndicates' => '{"twitter": "https://twitter.com/123"}',
]);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Jobs;
use App\Note;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use App\Jobs\SyndicateNoteToFacebook;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SyndicateNoteToFacebookJobTest extends TestCase
{
use DatabaseTransactions;
public function test_the_job()
{
$json = json_encode([
'url' => 'https://facebook.com/123'
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$job = new SyndicateNoteToFacebook($note);
$job->handle($client);
$this->assertDatabaseHas('notes', [
'id' => 1,
'facebook_url' => 'https://facebook.com/123',
]);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Tests\Unit\Jobs;
use App\Note;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Handler\MockHandler;
use App\Jobs\SyndicateNoteToTwitter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class SyndicateNoteToTwitterJobTest extends TestCase
{
use DatabaseTransactions;
public function test_the_job()
{
$json = json_encode([
'url' => 'https://twitter.com/i/web/status/123'
]);
$mock = new MockHandler([
new Response(201, ['Content-Type' => 'application/json'], $json),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$note = Note::find(1);
$job = new SyndicateNoteToTwitter($note);
$job->handle($client);
$this->assertDatabaseHas('notes', [
'id' => 1,
'tweet_id' => '123',
]);
}
}