Move some tests into their own subfodlers

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

View file

@ -0,0 +1,15 @@
<?php
namespace Tests\Feature\Admin;
use Tests\TestCase;
class AdminHomeControllerTest extends TestCase
{
public function test_admin_homepage()
{
$response = $this->withSession(['loggedin' => true])
->get('/admin');
$response->assertViewIs('admin.welcome');
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Tests\Feature\Admin;
use Tests\TestCase;
class AdminTest extends TestCase
{
public function test_admin_page_redirects_to_login()
{
$response = $this->get('/admin');
$response->assertRedirect('/login');
}
public function test_login_page()
{
$response = $this->get('/login');
$response->assertViewIs('login');
}
public function test_attempt_login_with_good_credentials()
{
$response = $this->post('/login', [
'username' => config('admin.user'),
'password' => config('admin.pass'),
]);
$response->assertRedirect('/admin');
}
public function test_attempt_login_with_bad_credentials()
{
$response = $this->post('/login', [
'username' => 'bad',
'password' => 'credentials',
]);
$response->assertRedirect('/login');
}
}

View file

@ -0,0 +1,91 @@
<?php
namespace Tests\Feature\Admin;
use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ArticlesTest extends TestCase
{
use DatabaseTransactions;
public function test_index_page()
{
$response = $this->withSession(['loggedin' => true])
->get('/admin/blog');
$response->assertSeeText('Select article to edit:');
}
public function test_create_page()
{
$response = $this->withSession(['loggedin' => true])
->get('/admin/blog/create');
$response->assertSeeText('Title (URL)');
}
public function test_create_new_article()
{
$this->withSession(['loggedin' => true])
->post('/admin/blog', [
'title' => 'Test Title',
'main' => 'Article content'
]);
$this->assertDatabaseHas('articles', ['title' => 'Test Title']);
}
public function test_create_new_article_with_upload()
{
$faker = \Faker\Factory::create();
$text = $faker->text;
if ($fh = fopen(sys_get_temp_dir() . '/article.md', 'w')) {
fwrite($fh, $text);
fclose($fh);
}
$path = sys_get_temp_dir() . '/article.md';
$file = new UploadedFile($path, 'article.md', 'text/plain', filesize($path), null, true);
$this->withSession(['loggedin' => true])
->post('/admin/blog', [
'title' => 'Uploaded Article',
'article' => $file,
]);
$this->assertDatabaseHas('articles', [
'title' => 'Uploaded Article',
'main' => $text,
]);
}
public function test_see_edit_form()
{
$response = $this->withSession(['loggedin' => true])
->get('/admin/blog/1/edit');
$response->assertSeeText('This is *my* new blog. It uses `Markdown`.');
}
public function test_edit_article()
{
$this->withSession(['loggedin' => true])
->post('/admin/blog/1', [
'_method' => 'PUT',
'title' => 'My New Blog',
'main' => 'This article has been edited',
]);
$this->assertDatabaseHas('articles', [
'title' => 'My New Blog',
'main' => 'This article has been edited',
]);
}
public function test_delete_article()
{
$this->withSession(['loggedin' => true])
->post('/admin/blog/1', [
'_method' => 'DELETE',
]);
$this->assertSoftDeleted('articles', [
'title' => 'My New Blog',
]);
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace Tests\Feature\Admin;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ClientsTest extends TestCase
{
use DatabaseTransactions;
public function test_index_page()
{
$response = $this->withSession(['loggedin' => true])
->get('/admin/clients');
$response->assertSeeText('Clients');
}
public function test_create_page()
{
$response = $this->withSession(['loggedin' => true])
->get('/admin/clients/create');
$response->assertSeeText('New Client');
}
public function test_create_new_client()
{
$this->withSession(['loggedin' => true])
->post('/admin/clients', [
'client_name' => 'Micropublish',
'client_url' => 'https://micropublish.net'
]);
$this->assertDatabaseHas('clients', [
'client_name' => 'Micropublish',
'client_url' => 'https://micropublish.net'
]);
}
public function test_see_edit_form()
{
$response = $this->withSession(['loggedin' => true])
->get('/admin/clients/1/edit');
$response->assertSee('https://jbl5.dev/notes/new');
}
public function test_edit_client()
{
$this->withSession(['loggedin' => true])
->post('/admin/clients/1', [
'_method' => 'PUT',
'client_url' => 'https://jbl5.dev/notes/new',
'client_name' => 'JBL5dev',
]);
$this->assertDatabaseHas('clients', [
'client_url' => 'https://jbl5.dev/notes/new',
'client_name' => 'JBL5dev',
]);
}
public function test_delete_client()
{
$this->withSession(['loggedin' => true])
->post('/admin/clients/1', [
'_method' => 'DELETE',
]);
$this->assertDatabaseMissing('clients', [
'client_url' => 'https://jbl5.dev/notes/new',
]);
}
}

View file

@ -0,0 +1,194 @@
<?php
namespace Tests\Feature\Admin;
use App\Contact;
use Tests\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\UploadedFile;
use GuzzleHttp\Handler\MockHandler;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ContactsTest extends TestCase
{
use DatabaseTransactions;
public function tearDown()
{
if (file_exists(public_path() . '/assets/profile-images/tantek.com/image')) {
unlink(public_path() . '/assets/profile-images/tantek.com/image');
rmdir(public_path() . '/assets/profile-images/tantek.com');
}
parent::tearDown();
}
public function test_index_page()
{
$response = $this->withSession([
'loggedin' => true
])->get('/admin/contacts');
$response->assertViewIs('admin.contacts.index');
}
public function test_create_page()
{
$response = $this->withSession([
'loggedin' => true
])->get('/admin/contacts/create');
$response->assertViewIs('admin.contacts.create');
}
public function test_create_new_contact()
{
$this->withSession([
'loggedin' => true
])->post('/admin/contacts', [
'name' => 'Fred Bloggs',
'nick' => 'fred',
'homepage' => 'https://fred.blog/gs',
]);
$this->assertDatabaseHas('contacts', [
'name' => 'Fred Bloggs',
'nick' => 'fred',
'homepage' => 'https://fred.blog/gs'
]);
}
public function test_see_edit_form()
{
$response = $this->withSession([
'loggedin' => true
])->get('/admin/contacts/1/edit');
$response->assertViewIs('admin.contacts.edit');
}
public function test_update_contact_no_uploaded_avatar()
{
$this->withSession([
'loggedin' => true
])->post('/admin/contacts/1', [
'_method' => 'PUT',
'name' => 'Tantek Celik',
'nick' => 'tantek',
'homepage' => 'https://tantek.com',
'twitter' => 't',
]);
$this->assertDatabaseHas('contacts', [
'name' => 'Tantek Celik',
'homepage' => 'https://tantek.com',
]);
}
public function test_edit_contact_with_uploaded_avatar()
{
copy(__DIR__ . '/../../aaron.png', sys_get_temp_dir() . '/tantek.png');
$path = sys_get_temp_dir() . '/tantek.png';
$file = new UploadedFile($path, 'tantek.png', 'image/png', filesize($path), null, true);
$this->withSession([
'loggedin' => true
])->post('/admin/contacts/1', [
'_method' => 'PUT',
'name' => 'Tantek Celik',
'nick' => 'tantek',
'homepage' => 'https://tantek.com',
'twitter' => 't',
'avatar' => $file,
]);
$this->assertFileEquals(
__DIR__ . '/../../aaron.png',
public_path() . '/assets/profile-images/tantek.com/image'
);
}
public function test_delete_contact()
{
$this->withSession([
'loggedin' => true
])->post('/admin/contacts/1', [
'_method' => 'DELETE',
]);
$this->assertDatabaseMissing('contacts', [
'nick' => 'tantek',
]);
}
public function test_get_avatar_method()
{
$html = <<<HTML
<div class="h-card">
<img class="u-photo" src="http://tantek.com/tantek.png">
</div>
HTML;
$file = fopen(__DIR__ . '/../../aaron.png', 'r');
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'text/html'], $html),
new Response(200, ['Content-Type' => 'iamge/png'], $file),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/contacts/1/getavatar');
$this->assertFileEquals(
__DIR__ . '/../../aaron.png',
public_path() . '/assets/profile-images/tantek.com/image'
);
}
public function test_get_avatar_method_redirects_with_failed_homepage()
{
$mock = new MockHandler([
new Response(404),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/contacts/1/getavatar');
$response->assertRedirect('/admin/contacts/1/edit');
}
public function test_get_avatar_method_redirects_with_failed_avatar_download()
{
$html = <<<HTML
<div class="h-card">
<img class="u-photo" src="http://tantek.com/tantek.png">
</div>
HTML;
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'text/html'], $html),
new Response(404),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$this->app->instance(Client::class, $client);
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/contacts/1/getavatar');
$response->assertRedirect('/admin/contacts/1/edit');
}
public function test_get_avatar_for_contact_with_no_homepage()
{
$contact = Contact::create([
'nick' => 'fred',
'name' => 'Fred Bloggs',
]);
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/contacts/' . $contact->id . '/getavatar');
$response->assertRedirect('/admin/contacts/' . $contact->id . '/edit');
}
}

View file

@ -0,0 +1,79 @@
<?php
namespace Tests\Feature\Admin;
use Tests\TestCase;
use App\Jobs\SendWebMentions;
use Illuminate\Support\Facades\Queue;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class NotesTest extends TestCase
{
use DatabaseTransactions;
public function test_index_page()
{
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/notes');
$response->assertViewIs('admin.notes.index');
}
public function test_create_page()
{
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/notes/create');
$response->assertViewIs('admin.notes.create');
}
public function test_create_a_new_note()
{
$this->withSession([
'loggedin' => true,
])->post('/admin/notes', [
'content' => 'A new test note',
]);
$this->assertDatabaseHas('notes', [
'note' => 'A new test note',
]);
}
public function test_edit_page()
{
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/notes/1/edit');
$response->assertViewIs('admin.notes.edit');
}
public function test_edit_a_note()
{
Queue::fake();
$this->withSession([
'loggedin' => true,
])->post('/admin/notes/1', [
'_method' => 'PUT',
'content' => 'An edited note',
'webmentions' => true,
]);
$this->assertDatabaseHas('notes', [
'note' => 'An edited note',
]);
Queue::assertPushed(SendWebMentions::class);
}
public function test_delete_note()
{
$this->withSession([
'loggedin' => true,
])->post('/admin/notes/1', [
'_method' => 'DELETE',
]);
$this->assertSoftDeleted('notes', [
'id' => '1',
]);
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace Tests\Feature\Admin;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PlacesTest extends TestCase
{
use DatabaseTransactions;
public function test_index_page()
{
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/places');
$response->assertViewIs('admin.places.index');
}
public function test_create_page()
{
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/places/create');
$response->assertViewIs('admin.places.create');
}
public function test_create_new_place()
{
$this->withSession([
'loggedin' => true,
])->post('/admin/places', [
'name' => 'Test Place',
'description' => 'A dummy place for feature tests',
'latitude' => '1.23',
'longitude' => '4.56',
]);
$this->assertDatabaseHas('places', [
'name' => 'Test Place',
'description' => 'A dummy place for feature tests',
]);
}
public function test_edit_page()
{
$response = $this->withSession([
'loggedin' => true,
])->get('/admin/places/1/edit');
$response->assertViewIs('admin.places.edit');
}
public function test_updating_a_place()
{
$this->withSession([
'loggedin' => true,
])->post('/admin/places/1', [
'_method' => 'PUT',
'name' => 'The Bridgewater',
'description' => 'Who uses “Pub” anyway',
'latitude' => '53.4983',
'longitude' => '-2.3805',
]);
$this->assertDatabaseHas('places', [
'name' => 'The Bridgewater',
]);
}
}

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',
]);
}
}