diff --git a/.env.github b/.env.github index 6ebe09fb..dbc69b67 100644 --- a/.env.github +++ b/.env.github @@ -7,7 +7,7 @@ APP_LOG_LEVEL=warning DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 -DB_DATABASE=jbuktest +DB_DATABASE=jbukdev_testing DB_USERNAME=postgres DB_PASSWORD=postgres diff --git a/.env.travis b/.env.travis deleted file mode 100644 index 3b70d5d2..00000000 --- a/.env.travis +++ /dev/null @@ -1,17 +0,0 @@ -APP_ENV=testing -APP_DEBUG=true -APP_KEY=base64:6DJhvZLVjE6dD4Cqrteh+6Z5vZlG+v/soCKcDHLOAH0= -APP_URL=http://jonnybarnes.localhost -APP_LONGURL=jonnybarnes.localhost -APP_SHORTURL=jmb.localhost - -DB_CONNECTION=travis - -CACHE_DRIVER=array -SESSION_DRIVER=array -QUEUE_DRIVER=sync - -SCOUT_DRIVER=pgsql - -DISPLAY_NAME='Travis Test' -USER_NAME=travis diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index fa56088f..d568332c 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -11,11 +11,11 @@ jobs: services: postgres: - image: postgres:13.1 + image: postgres:13.2 env: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres - POSTGRES_DB: jbuktest + POSTGRES_DB: jbukdev_testing ports: - 5432:5432 diff --git a/app/Console/Commands/ParseCachedWebMentions.php b/app/Console/Commands/ParseCachedWebMentions.php index 2183cd4a..6bdd1862 100644 --- a/app/Console/Commands/ParseCachedWebMentions.php +++ b/app/Console/Commands/ParseCachedWebMentions.php @@ -24,16 +24,6 @@ class ParseCachedWebMentions extends Command */ protected $description = 'Re-parse the webmention’s cached HTML'; - /** - * Create a new command instance. - * - * @return void - */ - public function __construct() - { - parent::__construct(); - } - /** * Execute the console command. * @@ -41,15 +31,15 @@ class ParseCachedWebMentions extends Command */ public function handle(FileSystem $filesystem) { - $HTMLfiles = $filesystem->allFiles(storage_path() . '/HTML'); - foreach ($HTMLfiles as $file) { - if ($file->getExtension() != 'backup') { //we don’t want to parse.backup files + $htmlFiles = $filesystem->allFiles(storage_path() . '/HTML'); + foreach ($htmlFiles as $file) { + if ($file->getExtension() !== 'backup') { //we don’t want to parse `.backup` files $filepath = $file->getPathname(); $this->info('Loading HTML from: ' . $filepath); $html = $filesystem->get($filepath); - $url = $this->URLFromFilename($filepath); - $microformats = \Mf2\parse($html, $url); + $url = $this->urlFromFilename($filepath); $webmention = WebMention::where('source', $url)->firstOrFail(); + $microformats = \Mf2\parse($html, $url); $webmention->mf2 = json_encode($microformats); $webmention->save(); $this->info('Saved the microformats to the database.'); @@ -63,12 +53,12 @@ class ParseCachedWebMentions extends Command * @param string * @return string */ - private function URLFromFilename(string $filepath): string + private function urlFromFilename(string $filepath): string { $dir = mb_substr($filepath, mb_strlen(storage_path() . '/HTML/')); $url = str_replace(['http/', 'https/'], ['http://', 'https://'], $dir); - if (mb_substr($url, -10) == 'index.html') { - $url = mb_substr($url, 0, mb_strlen($url) - 10); + if (mb_substr($url, -10) === 'index.html') { + $url = mb_substr($url, 0, -10); } return $url; diff --git a/app/Http/Controllers/Admin/ArticlesController.php b/app/Http/Controllers/Admin/ArticlesController.php index 30e5e275..3f390304 100644 --- a/app/Http/Controllers/Admin/ArticlesController.php +++ b/app/Http/Controllers/Admin/ArticlesController.php @@ -7,16 +7,10 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Article; use Illuminate\Http\RedirectResponse; -use Illuminate\Http\Request; use Illuminate\View\View; class ArticlesController extends Controller { - /** - * List the articles that can be edited. - * - * @return \Illuminate\View\View - */ public function index(): View { $posts = Article::select('id', 'title', 'published')->orderBy('id', 'desc')->get(); @@ -24,11 +18,6 @@ class ArticlesController extends Controller return view('admin.articles.index', ['posts' => $posts]); } - /** - * Show the new article form. - * - * @return \Illuminate\View\View - */ public function create(): View { $message = session('message'); @@ -36,11 +25,6 @@ class ArticlesController extends Controller return view('admin.articles.create', ['message' => $message]); } - /** - * Process an incoming request for a new article and save it. - * - * @return \Illuminate\Http\RedirectResponse - */ public function store(): RedirectResponse { //if a `.md` is attached use that for the main content. @@ -49,42 +33,21 @@ class ArticlesController extends Controller $content = $file->fread($file->getSize()); } $main = $content ?? request()->input('main'); - $article = Article::create( - [ - 'url' => request()->input('url'), - 'title' => request()->input('title'), - 'main' => $main, - 'published' => request()->input('published') ?? 0, - ] - ); + Article::create([ + 'url' => request()->input('url'), + 'title' => request()->input('title'), + 'main' => $main, + 'published' => request()->input('published') ?? 0, + ]); return redirect('/admin/blog'); } - /** - * Show the edit form for an existing article. - * - * @param int $articleId - * @return \Illuminate\View\View - */ - public function edit(int $articleId): View + public function edit(Article $article): View { - $post = Article::select( - 'title', - 'main', - 'url', - 'published' - )->where('id', $articleId)->get(); - - return view('admin.articles.edit', ['id' => $articleId, 'post' => $post]); + return view('admin.articles.edit', ['article' => $article]); } - /** - * Process an incoming request to edit an article. - * - * @param int $articleId - * @return \Illuminate\Http\RedirectResponse - */ public function update(int $articleId): RedirectResponse { $article = Article::find($articleId); @@ -97,12 +60,6 @@ class ArticlesController extends Controller return redirect('/admin/blog'); } - /** - * Process a request to delete an aricle. - * - * @param int $articleId - * @return \Illuminate\Http\RedirectResponse - */ public function destroy(int $articleId): RedirectResponse { Article::where('id', $articleId)->delete(); diff --git a/app/Http/Controllers/ArticlesController.php b/app/Http/Controllers/ArticlesController.php index 6d16589a..d70209d7 100644 --- a/app/Http/Controllers/ArticlesController.php +++ b/app/Http/Controllers/ArticlesController.php @@ -59,18 +59,14 @@ class ArticlesController extends Controller * We only have the ID, work out post title, year and month * and redirect to it. * - * @param int $idFromUrl + * @param string $idFromUrl * @return RedirectResponse */ - public function onlyIdInUrl(int $idFromUrl): RedirectResponse + public function onlyIdInUrl(string $idFromUrl): RedirectResponse { - $realId = resolve(Numbers::class)->b60tonum((string) $idFromUrl); + $realId = resolve(Numbers::class)->b60tonum($idFromUrl); - try { - $article = Article::findOrFail($realId); - } catch (ModelNotFoundException $exception) { - abort(404); - } + $article = Article::findOrFail($realId); return redirect($article->link); } diff --git a/app/Models/Article.php b/app/Models/Article.php index a48c1ed9..5c9516c8 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -7,6 +7,7 @@ namespace App\Models; use Cviebrock\EloquentSluggable\Sluggable; use Eloquent; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; @@ -58,6 +59,7 @@ use Spatie\CommonMarkHighlighter\IndentedCodeRenderer; */ class Article extends Model { + use HasFactory; use Sluggable; use SoftDeletes; diff --git a/app/Models/Contact.php b/app/Models/Contact.php index a15a4fc2..30187264 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -6,6 +6,7 @@ namespace App\Models; use Eloquent; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Carbon; @@ -35,6 +36,8 @@ use Illuminate\Support\Carbon; */ class Contact extends Model { + use HasFactory; + /** * The database table used by the model. * diff --git a/app/Models/Media.php b/app/Models/Media.php index df8a4666..e7b0cd88 100644 --- a/app/Models/Media.php +++ b/app/Models/Media.php @@ -6,6 +6,7 @@ namespace App\Models; use Eloquent; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Carbon; @@ -41,6 +42,8 @@ use Illuminate\Support\Str; */ class Media extends Model { + use HasFactory; + /** * The table associated with the model. * @@ -62,7 +65,7 @@ class Media extends Model */ public function note(): BelongsTo { - return $this->belongsTo('App\Models\Note'); + return $this->belongsTo(Note::class); } /** @@ -118,7 +121,7 @@ class Media extends Model $filenameParts = explode('.', $path); array_pop($filenameParts); - return ltrim(array_reduce($filenameParts, function ($carry, $item) { + return ltrim(array_reduce($filenameParts, static function ($carry, $item) { return $carry . '.' . $item; }, ''), '.'); } diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 400bbf7c..d07d8388 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -51,7 +51,7 @@ class Tag extends Model */ public function notes() { - return $this->belongsToMany('App\Models\Note'); + return $this->belongsToMany(Note::class); } /** diff --git a/app/Models/WebMention.php b/app/Models/WebMention.php index 8e175bcb..99178c10 100644 --- a/app/Models/WebMention.php +++ b/app/Models/WebMention.php @@ -8,6 +8,7 @@ use App\Traits\FilterHtml; use Codebird\Codebird; use Eloquent; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Filesystem\Filesystem; @@ -55,6 +56,7 @@ use Jonnybarnes\WebmentionsParser\Exceptions\AuthorshipParserException; class WebMention extends Model { use FilterHtml; + use HasFactory; /** * The database table used by the model. diff --git a/database/factories/ArticleFactory.php b/database/factories/ArticleFactory.php new file mode 100644 index 00000000..2b69f126 --- /dev/null +++ b/database/factories/ArticleFactory.php @@ -0,0 +1,34 @@ + $this->faker->slug(3), + 'title' => $this->faker->words(3, true), + 'main' => $this->faker->paragraphs(4, true), + 'published' => 1, + 'created_at' => Carbon::now()->toDateTimeString(), + 'updated_at' => Carbon::now()->toDateTimeString(), + ]; + } +} diff --git a/database/factories/ContactFactory.php b/database/factories/ContactFactory.php new file mode 100644 index 00000000..175bc280 --- /dev/null +++ b/database/factories/ContactFactory.php @@ -0,0 +1,35 @@ + mb_strtolower($this->faker->firstName), + 'name' => $this->faker->name(), + 'homepage' => $this->faker->url, + 'twitter' => mb_strtolower($this->faker->firstName), + 'facebook' => $this->faker->randomNumber(5), + 'created_at' => Carbon::now()->toDateTimeString(), + 'updated_at' => Carbon::now()->toDateTimeString(), + ]; + } +} diff --git a/database/factories/MediaFactory.php b/database/factories/MediaFactory.php new file mode 100644 index 00000000..b3a44d78 --- /dev/null +++ b/database/factories/MediaFactory.php @@ -0,0 +1,32 @@ + 'media/' . $this->faker->uuid . '.jpg', + 'type' => 'image', + 'created_at' => Carbon::now()->toDateTimeString(), + 'updated_at' => Carbon::now()->toDateTimeString(), + ]; + } +} diff --git a/database/factories/NoteFactory.php b/database/factories/NoteFactory.php index 26c1f488..347ca33a 100644 --- a/database/factories/NoteFactory.php +++ b/database/factories/NoteFactory.php @@ -3,6 +3,7 @@ namespace Database\Factories; use App\Models\Note; +use Exception; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Carbon; @@ -19,10 +20,11 @@ class NoteFactory extends Factory * Define the model's default state. * * @return array + * @throws Exception */ public function definition() { - $now = Carbon::now()->subDays(rand(5, 15)); + $now = Carbon::now()->subDays(random_int(5, 15)); return [ 'note' => $this->faker->paragraph, diff --git a/database/factories/WebMentionFactory.php b/database/factories/WebMentionFactory.php new file mode 100644 index 00000000..bb26f0da --- /dev/null +++ b/database/factories/WebMentionFactory.php @@ -0,0 +1,31 @@ + $this->faker->url, + 'target' => url('notes/1'), + 'type' => 'reply', + 'content' => $this->faker->paragraph, + ]; + } +} diff --git a/phpunit.xml b/phpunit.xml index 3fd13959..9594be5d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,6 +16,7 @@ + diff --git a/resources/views/admin/articles/edit.blade.php b/resources/views/admin/articles/edit.blade.php index d5c5dba5..9c7d5b96 100644 --- a/resources/views/admin/articles/edit.blade.php +++ b/resources/views/admin/articles/edit.blade.php @@ -3,28 +3,28 @@ @section('title')Edit Article « Admin CP « @stop @section('content') -
+ {{ csrf_field() }} {{ method_field('PUT') }}
- - + +
- +
- + published == '1') checked="checked"@endif>

-
+ {{ csrf_field() }} {{ method_field('DELETE') }}
diff --git a/routes/web.php b/routes/web.php index 6c27e427..e288fee6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -65,7 +65,7 @@ Route::group(['domain' => config('url.longurl')], function () { Route::get('/', [AdminArticlesController::class, 'index']); Route::get('/create', [AdminArticlesController::class, 'create']); Route::post('/', [AdminArticlesController::class, 'store']); - Route::get('/{id}/edit', [AdminArticlesController::class, 'edit']); + Route::get('/{article}/edit', [AdminArticlesController::class, 'edit']); Route::put('/{id}', [AdminArticlesController::class, 'update']); Route::delete('/{id}', [AdminArticlesController::class, 'destroy']); }); diff --git a/tests/Feature/ActivityStreamTest.php b/tests/Feature/ActivityStreamTest.php index e00aa495..d6a8eec8 100644 --- a/tests/Feature/ActivityStreamTest.php +++ b/tests/Feature/ActivityStreamTest.php @@ -25,8 +25,8 @@ class ActivityStreamTest extends TestCase /** @test */ public function requestForNoteIncludesActivityStreamData(): void { - $note = Note::find(11); - $response = $this->get('/notes/B', ['Accept' => 'application/activity+json']); + $note = Note::factory()->create(); + $response = $this->get($note->longurl, ['Accept' => 'application/activity+json']); $response->assertHeader('Content-Type', 'application/activity+json'); $response->assertJson([ '@context' => 'https://www.w3.org/ns/activitystreams', diff --git a/tests/Feature/Admin/ArticlesTest.php b/tests/Feature/Admin/ArticlesTest.php index bdb8d60e..025e4638 100644 --- a/tests/Feature/Admin/ArticlesTest.php +++ b/tests/Feature/Admin/ArticlesTest.php @@ -4,15 +4,16 @@ declare(strict_types=1); namespace Tests\Feature\Admin; +use App\Models\Article; use App\Models\User; use Faker\Factory; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; class ArticlesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function adminArticlesPageLoads(): void @@ -76,9 +77,12 @@ class ArticlesTest extends TestCase public function articleCanLoadFormToEditArticle(): void { $user = User::factory()->make(); + $article = Article::factory()->create([ + 'main' => 'This is *my* new blog. It uses `Markdown`.', + ]); $response = $this->actingAs($user) - ->get('/admin/blog/1/edit'); + ->get('/admin/blog/' . $article->id . '/edit'); $response->assertSeeText('This is *my* new blog. It uses `Markdown`.'); } @@ -86,9 +90,10 @@ class ArticlesTest extends TestCase public function adminCanEditArticle(): void { $user = User::factory()->make(); + $article = Article::factory()->create(); $this->actingAs($user) - ->post('/admin/blog/1', [ + ->post('/admin/blog/' . $article->id, [ '_method' => 'PUT', 'title' => 'My New Blog', 'main' => 'This article has been edited', @@ -103,13 +108,14 @@ class ArticlesTest extends TestCase public function adminCanDeleteArticle(): void { $user = User::factory()->make(); + $article = Article::factory()->create(); $this->actingAs($user) - ->post('/admin/blog/1', [ + ->post('/admin/blog/' . $article->id, [ '_method' => 'DELETE', ]); $this->assertSoftDeleted('articles', [ - 'title' => 'My New Blog', + 'title' => $article->title, ]); } } diff --git a/tests/Feature/Admin/ClientsTest.php b/tests/Feature/Admin/ClientsTest.php index 2ca70b2f..f9b33fd5 100644 --- a/tests/Feature/Admin/ClientsTest.php +++ b/tests/Feature/Admin/ClientsTest.php @@ -4,13 +4,14 @@ declare(strict_types=1); namespace Tests\Feature\Admin; +use App\Models\MicropubClient; use App\Models\User; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class ClientsTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function clientsPageLoads(): void @@ -52,9 +53,12 @@ class ClientsTest extends TestCase public function adminCanLoadEditFormForClient(): void { $user = User::factory()->make(); + $client = MicropubClient::factory()->create([ + 'client_url' => 'https://jbl5.dev/notes/new', + ]); $response = $this->actingAs($user) - ->get('/admin/clients/1/edit'); + ->get('/admin/clients/' . $client->id . '/edit'); $response->assertSee('https://jbl5.dev/notes/new'); } @@ -62,9 +66,10 @@ class ClientsTest extends TestCase public function adminCanEditClient(): void { $user = User::factory()->make(); + $client = MicropubClient::factory()->create(); $this->actingAs($user) - ->post('/admin/clients/1', [ + ->post('/admin/clients/' . $client->id, [ '_method' => 'PUT', 'client_url' => 'https://jbl5.dev/notes/new', 'client_name' => 'JBL5dev', @@ -79,9 +84,12 @@ class ClientsTest extends TestCase public function adminCanDeleteClient(): void { $user = User::factory()->make(); + $client = MicropubClient::factory()->create([ + 'client_url' => 'https://jbl5.dev/notes/new', + ]); $this->actingAs($user) - ->post('/admin/clients/1', [ + ->post('/admin/clients/' . $client->id, [ '_method' => 'DELETE', ]); $this->assertDatabaseMissing('clients', [ diff --git a/tests/Feature/Admin/ContactsTest.php b/tests/Feature/Admin/ContactsTest.php index 65bbe85d..61acb1b7 100644 --- a/tests/Feature/Admin/ContactsTest.php +++ b/tests/Feature/Admin/ContactsTest.php @@ -10,13 +10,13 @@ use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; use Tests\TestCase; class ContactsTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; protected function tearDown(): void { @@ -66,8 +66,9 @@ class ContactsTest extends TestCase public function adminCanSeeFormToEditContact(): void { $user = User::factory()->make(); + $contact = Contact::factory()->create(); - $response = $this->actingAs($user)->get('/admin/contacts/1/edit'); + $response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/edit'); $response->assertViewIs('admin.contacts.edit'); } @@ -75,8 +76,9 @@ class ContactsTest extends TestCase public function adminCanUpdateContact(): void { $user = User::factory()->make(); + $contact = Contact::factory()->create(); - $this->actingAs($user)->post('/admin/contacts/1', [ + $this->actingAs($user)->post('/admin/contacts/' . $contact->id, [ '_method' => 'PUT', 'name' => 'Tantek Celik', 'nick' => 'tantek', @@ -96,8 +98,9 @@ class ContactsTest extends TestCase $path = sys_get_temp_dir() . '/tantek.png'; $file = new UploadedFile($path, 'tantek.png', 'image/png', null, true); $user = User::factory()->make(); + $contact = Contact::factory()->create(); - $this->actingAs($user)->post('/admin/contacts/1', [ + $this->actingAs($user)->post('/admin/contacts/' . $contact->id, [ '_method' => 'PUT', 'name' => 'Tantek Celik', 'nick' => 'tantek', @@ -115,8 +118,13 @@ class ContactsTest extends TestCase public function adminCanDeleteContact(): void { $user = User::factory()->make(); + $contact = Contact::factory()->create(['nick' => 'tantek']); - $this->actingAs($user)->post('/admin/contacts/1', [ + $this->assertDatabaseHas('contacts', [ + 'nick' => 'tantek', + ]); + + $this->actingAs($user)->post('/admin/contacts/' . $contact->id, [ '_method' => 'DELETE', ]); $this->assertDatabaseMissing('contacts', [ @@ -132,7 +140,7 @@ class ContactsTest extends TestCase
HTML; - $file = fopen(__DIR__ . '/../../aaron.png', 'r'); + $file = fopen(__DIR__ . '/../../aaron.png', 'rb'); $mock = new MockHandler([ new Response(200, ['Content-Type' => 'text/html'], $html), new Response(200, ['Content-Type' => 'iamge/png'], $file), @@ -141,8 +149,11 @@ class ContactsTest extends TestCase $client = new Client(['handler' => $handler]); $this->app->instance(Client::class, $client); $user = User::factory()->make(); + $contact = Contact::factory()->create([ + 'homepage' => 'https://tantek.com', + ]); - $this->actingAs($user)->get('/admin/contacts/1/getavatar'); + $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar'); $this->assertFileEquals( __DIR__ . '/../../aaron.png', @@ -160,10 +171,11 @@ class ContactsTest extends TestCase $client = new Client(['handler' => $handler]); $this->app->instance(Client::class, $client); $user = User::factory()->make(); + $contact = Contact::factory()->create(); - $response = $this->actingAs($user)->get('/admin/contacts/1/getavatar'); + $response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar'); - $response->assertRedirect('/admin/contacts/1/edit'); + $response->assertRedirect('/admin/contacts/' . $contact->id . '/edit'); } /** @test */ @@ -182,10 +194,11 @@ class ContactsTest extends TestCase $client = new Client(['handler' => $handler]); $this->app->instance(Client::class, $client); $user = User::factory()->make(); + $contact = Contact::factory()->create(); - $response = $this->actingAs($user)->get('/admin/contacts/1/getavatar'); + $response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar'); - $response->assertRedirect('/admin/contacts/1/edit'); + $response->assertRedirect('/admin/contacts/' . $contact->id . '/edit'); } /** @test */ diff --git a/tests/Feature/Admin/LikesTest.php b/tests/Feature/Admin/LikesTest.php index 8cb57502..462fc2e4 100644 --- a/tests/Feature/Admin/LikesTest.php +++ b/tests/Feature/Admin/LikesTest.php @@ -7,13 +7,13 @@ namespace Tests\Feature\Admin; use App\Jobs\ProcessLike; use App\Models\Like; use App\Models\User; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Queue; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; class LikesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function likesPageLoads(): void @@ -55,9 +55,10 @@ class LikesTest extends TestCase public function likeEditFormLoads(): void { $user = User::factory()->make(); + $like = Like::factory()->create(); $response = $this->actingAs($user) - ->get('/admin/likes/1/edit'); + ->get('/admin/likes/' . $like->id . '/edit'); $response->assertSee('Edit Like'); } @@ -66,9 +67,10 @@ class LikesTest extends TestCase { Queue::fake(); $user = User::factory()->make(); + $like = Like::factory()->create(); $this->actingAs($user) - ->post('/admin/likes/1', [ + ->post('/admin/likes/' . $like->id, [ '_method' => 'PUT', 'like_url' => 'https://example.com', ]); @@ -81,12 +83,12 @@ class LikesTest extends TestCase /** @test */ public function adminCanDeleteLike(): void { - $like = Like::find(1); + $like = Like::factory()->create(); $url = $like->url; $user = User::factory()->make(); $this->actingAs($user) - ->post('/admin/likes/1', [ + ->post('/admin/likes/' . $like->id, [ '_method' => 'DELETE', ]); $this->assertDatabaseMissing('likes', [ diff --git a/tests/Feature/Admin/NotesTest.php b/tests/Feature/Admin/NotesTest.php index 8ce59173..26b9ac0e 100644 --- a/tests/Feature/Admin/NotesTest.php +++ b/tests/Feature/Admin/NotesTest.php @@ -5,14 +5,15 @@ declare(strict_types=1); namespace Tests\Feature\Admin; use App\Jobs\SendWebMentions; +use App\Models\Note; use App\Models\User; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Queue; use Tests\TestCase; class NotesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function notesPageLoads(): void @@ -49,8 +50,9 @@ class NotesTest extends TestCase public function noteEditFormLoads(): void { $user = User::factory()->make(); + $note = Note::factory()->create(); - $response = $this->actingAs($user)->get('/admin/notes/1/edit'); + $response = $this->actingAs($user)->get('/admin/notes/' . $note->id . '/edit'); $response->assertViewIs('admin.notes.edit'); } @@ -59,8 +61,9 @@ class NotesTest extends TestCase { Queue::fake(); $user = User::factory()->make(); + $note = Note::factory()->create(); - $this->actingAs($user)->post('/admin/notes/1', [ + $this->actingAs($user)->post('/admin/notes/' . $note->id, [ '_method' => 'PUT', 'content' => 'An edited note', 'webmentions' => true, @@ -76,12 +79,13 @@ class NotesTest extends TestCase public function adminCanDeleteNote(): void { $user = User::factory()->make(); + $note = Note::factory()->create(); - $this->actingAs($user)->post('/admin/notes/1', [ + $this->actingAs($user)->post('/admin/notes/' . $note->id, [ '_method' => 'DELETE', ]); $this->assertSoftDeleted('notes', [ - 'id' => '1', + 'id' => $note->id, ]); } } diff --git a/tests/Feature/Admin/PlacesTest.php b/tests/Feature/Admin/PlacesTest.php index 910a7fe0..0de5b928 100644 --- a/tests/Feature/Admin/PlacesTest.php +++ b/tests/Feature/Admin/PlacesTest.php @@ -4,13 +4,14 @@ declare(strict_types=1); namespace Tests\Feature\Admin; +use App\Models\Place; use App\Models\User; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class PlacesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function placesPageLoads(): void @@ -51,8 +52,9 @@ class PlacesTest extends TestCase public function editPlacePageLoads(): void { $user = User::factory()->make(); + $place = Place::factory()->create(); - $response = $this->actingAs($user)->get('/admin/places/1/edit'); + $response = $this->actingAs($user)->get('/admin/places/' . $place->id . '/edit'); $response->assertViewIs('admin.places.edit'); } @@ -60,8 +62,11 @@ class PlacesTest extends TestCase public function adminCanUpdatePlace(): void { $user = User::factory()->make(); + $place = Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + ]); - $this->actingAs($user)->post('/admin/places/1', [ + $this->actingAs($user)->post('/admin/places/' . $place->id, [ '_method' => 'PUT', 'name' => 'The Bridgewater', 'description' => 'Who uses “Pub” anyway', diff --git a/tests/Feature/ArticlesTest.php b/tests/Feature/ArticlesTest.php index 96aded01..8586d7ff 100644 --- a/tests/Feature/ArticlesTest.php +++ b/tests/Feature/ArticlesTest.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Tests\Feature; +use App\Models\Article; +use Jonnybarnes\IndieWeb\Numbers; use Tests\TestCase; class ArticlesTest extends TestCase @@ -18,22 +20,26 @@ class ArticlesTest extends TestCase /** @test */ public function singleArticlePageLoads() { - $response = $this->get('/blog/' . date('Y') . '/' . date('m') . '/some-code-i-did'); + $article = Article::factory()->create(); + $response = $this->get($article->link); $response->assertViewIs('articles.show'); } /** @test */ public function wrongDateInUrlRedirectsToCorrectDate() { - $response = $this->get('/blog/1900/01/some-code-i-did'); - $response->assertRedirect('/blog/' . date('Y') . '/' . date('m') . '/some-code-i-did'); + $article = Article::factory()->create(); + $response = $this->get('/blog/1900/01/' . $article->titleurl); + $response->assertRedirect('/blog/' . date('Y') . '/' . date('m') . '/' . $article->titleurl); } /** @test */ public function oldUrlsWithIdAreRedirected() { - $response = $this->get('/blog/s/2'); - $response->assertRedirect('/blog/' . date('Y') . '/' . date('m') . '/some-code-i-did'); + $article = Article::factory()->create(); + $num60Id = resolve(Numbers::class)->numto60($article->id); + $response = $this->get('/blog/s/' . $num60Id); + $response->assertRedirect($article->link); } /** @test */ diff --git a/tests/Feature/BookmarksTest.php b/tests/Feature/BookmarksTest.php index d624563b..8b298dbf 100644 --- a/tests/Feature/BookmarksTest.php +++ b/tests/Feature/BookmarksTest.php @@ -6,14 +6,15 @@ namespace Tests\Feature; use App\Jobs\ProcessBookmark; use App\Jobs\SyndicateBookmarkToTwitter; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use App\Models\Bookmark; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Queue; use Tests\TestCase; use Tests\TestToken; class BookmarksTest extends TestCase { - use DatabaseTransactions, TestToken; + use RefreshDatabase, TestToken; /** @test */ public function bookmarksPageLoadsWithoutError(): void @@ -25,7 +26,8 @@ class BookmarksTest extends TestCase /** @test */ public function singleBookmarkPageLoadsWithoutError(): void { - $response = $this->get('/bookmarks/1'); + $bookmark = Bookmark::factory()->create(); + $response = $this->get('/bookmarks/' . $bookmark->id); $response->assertViewIs('bookmarks.show'); } diff --git a/tests/Feature/BridgyPosseTest.php b/tests/Feature/BridgyPosseTest.php index d9ce2629..08c35d5b 100644 --- a/tests/Feature/BridgyPosseTest.php +++ b/tests/Feature/BridgyPosseTest.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Tests\Feature; +use App\Models\Contact; +use App\Models\Note; use Tests\TestCase; class BridgyPosseTest extends TestCase @@ -11,9 +13,15 @@ class BridgyPosseTest extends TestCase /** @test */ public function notesWeWantCopiedToTwitterShouldHaveNecessaryMarkup(): void { - $response = $this->get('/notes/4'); + Contact::factory()->create([ + 'nick' => 'joe', + 'twitter' => 'joe__', + ]); + $note = Note::factory()->create(['note' => 'Hi @joe']); + + $response = $this->get($note->longurl); $html = $response->content(); - $this->assertTrue(is_string(mb_stristr($html, 'p-bridgy-twitter-content'))); + $this->assertStringContainsString('p-bridgy-twitter-content', $html); } } diff --git a/tests/Feature/ContactsTest.php b/tests/Feature/ContactsTest.php index c4d1f5fa..c06dddc1 100644 --- a/tests/Feature/ContactsTest.php +++ b/tests/Feature/ContactsTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Tests\Feature; +use App\Models\Contact; use Tests\TestCase; class ContactsTest extends TestCase @@ -26,6 +27,9 @@ class ContactsTest extends TestCase */ public function contactPageShouldFallbackToDefaultProfilePic(): void { + Contact::factory()->create([ + 'nick' => 'tantek', + ]); $response = $this->get('/contacts/tantek'); $response->assertViewHas('image', '/assets/profile-images/default-image'); } @@ -37,6 +41,10 @@ class ContactsTest extends TestCase */ public function contactPageShouldUseSpecificProfilePicIfPresent(): void { + Contact::factory()->create([ + 'nick' => 'aaron', + 'homepage' => 'https://aaronparecki.com', + ]); $response = $this->get('/contacts/aaron'); $response->assertViewHas('image', '/assets/profile-images/aaronparecki.com/image'); } diff --git a/tests/Feature/LikesTest.php b/tests/Feature/LikesTest.php index c33c005c..7e56f6b2 100644 --- a/tests/Feature/LikesTest.php +++ b/tests/Feature/LikesTest.php @@ -11,7 +11,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Queue; use Jonnybarnes\WebmentionsParser\Authorship; use Tests\TestCase; @@ -19,7 +19,7 @@ use Tests\TestToken; class LikesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; use TestToken; /** @test */ @@ -32,7 +32,8 @@ class LikesTest extends TestCase /** @test */ public function singleLikePageHasCorrectView(): void { - $response = $this->get('/likes/1'); + $like = Like::factory()->create(); + $response = $this->get('/likes/' . $like->id); $response->assertViewIs('likes.show'); } diff --git a/tests/Feature/MicropubControllerTest.php b/tests/Feature/MicropubControllerTest.php index 5ac472e8..ffdc7660 100644 --- a/tests/Feature/MicropubControllerTest.php +++ b/tests/Feature/MicropubControllerTest.php @@ -5,17 +5,16 @@ declare(strict_types=1); namespace Tests\Feature; use Faker\Factory; +use Illuminate\Foundation\Testing\RefreshDatabase; use App\Jobs\{SendWebMentions, SyndicateNoteToTwitter}; -use App\Models\{Media, Place}; +use App\Models\{Media, Note, Place}; use Carbon\Carbon; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Queue; -use Tests\TestCase; -use Tests\TestToken; +use Tests\{TestCase, TestToken}; class MicropubControllerTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; use TestToken; /** @test */ @@ -57,6 +56,11 @@ class MicropubControllerTest extends TestCase /** @test */ public function micropubClientsCanRequestKnownNearbyPlaces(): void { + Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + 'latitude' => '53.5', + 'longitude' => '-2.38', + ]); $response = $this->get('/api/post?q=geo:53.5,-2.38', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); $response->assertJson(['places' => [['slug' => 'the-bridgewater-pub']]]); } @@ -64,12 +68,12 @@ class MicropubControllerTest extends TestCase /** * @test * @todo Add uncertainty parameter - */ + * public function micropubClientsCanRequestKnownNearbyPlacesWithUncertaintyParameter(): void { $response = $this->get('/api/post?q=geo:53.5,-2.38', ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]); $response->assertJson(['places' => [['slug' => 'the-bridgewater-pub']]]); - } + }*/ /** @test */ public function returnEmptyResultWhenMicropubClientRequestsKnownNearbyPlaces(): void @@ -459,11 +463,12 @@ class MicropubControllerTest extends TestCase /** @test */ public function micropubClientApiRequestUpdatesExistingNote(): void { + $note = Note::factory()->create(); $response = $this->postJson( '/api/post', [ 'action' => 'update', - 'url' => config('app.url') . '/notes/A', + 'url' => $note->longurl, 'replace' => [ 'content' => ['replaced content'], ], @@ -478,11 +483,12 @@ class MicropubControllerTest extends TestCase /** @test */ public function micropubClientApiRequestUpdatesNoteSyndicationLinks(): void { + $note = Note::factory()->create(); $response = $this->postJson( '/api/post', [ 'action' => 'update', - 'url' => config('app.url') . '/notes/A', + 'url' => $note->longurl, 'add' => [ 'syndication' => [ 'https://www.swarmapp.com/checkin/123', @@ -504,11 +510,12 @@ class MicropubControllerTest extends TestCase /** @test */ public function micropubClientApiRequestAddsImageToNote(): void { + $note = Note::factory()->create(); $response = $this->postJson( '/api/post', [ 'action' => 'update', - 'url' => config('app.url') . '/notes/A', + 'url' => $note->longurl, 'add' => [ 'photo' => ['https://example.org/photo.jpg'], ], @@ -564,11 +571,12 @@ class MicropubControllerTest extends TestCase /** @test */ public function micropubClientApiRequestReturnsErrorWhenTryingToUpdateUnsupportedProperty(): void { + $note = Note::factory()->create(); $response = $this->postJson( '/api/post', [ 'action' => 'update', - 'url' => config('app.url') . '/notes/A', + 'url' => $note->longurl, 'morph' => [ // or any other unsupported update type 'syndication' => ['https://www.swarmapp.com/checkin/123'], ], @@ -602,11 +610,12 @@ class MicropubControllerTest extends TestCase /** @test */ public function micropubClientApiRequestCanReplaceNoteSyndicationTargets(): void { + $note = Note::factory()->create(); $response = $this->postJson( '/api/post', [ 'action' => 'update', - 'url' => config('app.url') . '/notes/B', + 'url' => $note->longurl, 'replace' => [ 'syndication' => [ 'https://www.swarmapp.com/checkin/the-id', diff --git a/tests/Feature/NotesControllerTest.php b/tests/Feature/NotesControllerTest.php index 59197e4a..d44c3492 100644 --- a/tests/Feature/NotesControllerTest.php +++ b/tests/Feature/NotesControllerTest.php @@ -4,21 +4,22 @@ declare(strict_types=1); namespace Tests\Feature; +use App\Models\Note; use Tests\TestCase; class NotesControllerTest extends TestCase { - /* + /** * Test the `/notes` page returns 200, this should * mean the database is being hit. * * @test - * + */ public function notesPageLoads(): void { $response = $this->get('/notes'); $response->assertStatus(200); - }*/ + } /** * Test a specific note. @@ -27,10 +28,12 @@ class NotesControllerTest extends TestCase */ public function specificNotePageLoads(): void { - $response = $this->get('/notes/D'); + $note = Note::factory()->create(); + $response = $this->get($note->longurl); $response->assertViewHas('note'); } + /** @todo */ /* @test * public function noteReplyingToTweet(): void { @@ -45,8 +48,9 @@ class NotesControllerTest extends TestCase */ public function oldNoteUrlsRedirect(): void { - $response = $this->get('/note/11'); - $response->assertRedirect(config('app.url') . '/notes/B'); + $note = Note::factory()->create(); + $response = $this->get('/note/' . $note->id); + $response->assertRedirect($note->longurl); } /** diff --git a/tests/Feature/ParseCachedWebMentionsTest.php b/tests/Feature/ParseCachedWebMentionsTest.php index da192d64..6d54e37c 100644 --- a/tests/Feature/ParseCachedWebMentionsTest.php +++ b/tests/Feature/ParseCachedWebMentionsTest.php @@ -6,13 +6,14 @@ namespace Tests\Feature; use App\Models\WebMention; use Illuminate\FileSystem\FileSystem; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Artisan; use Tests\TestCase; class ParseCachedWebMentionsTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; protected function setUp(): void { @@ -27,6 +28,16 @@ class ParseCachedWebMentionsTest extends TestCase /** @test */ public function parseWebmentionHtml(): void { + $webmentionAaron = WebMention::factory()->create([ + 'source' => 'https://aaronpk.localhost/reply/1', + 'created_at' => Carbon::now()->subDays(5), + 'updated_at' => Carbon::now()->subDays(5), + ]); + $webmentionTantek = WebMention::factory()->create([ + 'source' => 'http://tantek.com/', + 'created_at' => Carbon::now()->subDays(5), + 'updated_at' => Carbon::now()->subDays(5), + ]); $this->assertFileExists(storage_path('HTML') . '/https/aaronpk.localhost/reply/1'); $this->assertFileExists(storage_path('HTML') . '/http/tantek.com/index.html'); $htmlAaron = file_get_contents(storage_path('HTML') . '/https/aaronpk.localhost/reply/1'); @@ -40,8 +51,9 @@ class ParseCachedWebMentionsTest extends TestCase Artisan::call('webmentions:parsecached'); - $webmentionAaron = WebMention::find(1); - $webmentionTantek = WebMention::find(2); + $webmentionAaron = WebMention::find($webmentionAaron->id); + $webmentionTantek = WebMention::find($webmentionTantek->id); + $this->assertTrue($webmentionAaron->updated_at->gt($webmentionAaron->created_at)); $this->assertTrue($webmentionTantek->updated_at->gt($webmentionTantek->created_at)); } diff --git a/tests/Feature/PlacesTest.php b/tests/Feature/PlacesTest.php index 56c4ebbd..e6247ff5 100644 --- a/tests/Feature/PlacesTest.php +++ b/tests/Feature/PlacesTest.php @@ -27,8 +27,8 @@ class PlacesTest extends TestCase */ public function singlePlacePageLoads(): void { - $place = Place::where('slug', 'the-bridgewater-pub')->first(); - $response = $this->get('/places/the-bridgewater-pub'); + $place = Place::factory()->create(); + $response = $this->get($place->longurl); $response->assertViewHas('place', $place); } diff --git a/tests/Feature/ReDownloadWebMentionsTest.php b/tests/Feature/ReDownloadWebMentionsTest.php index 174cd6d5..94199cb2 100644 --- a/tests/Feature/ReDownloadWebMentionsTest.php +++ b/tests/Feature/ReDownloadWebMentionsTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Tests\Feature; use App\Jobs\DownloadWebMention; +use App\Models\WebMention; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Queue; use Tests\TestCase; @@ -16,6 +17,8 @@ class ReDownloadWebMentionsTest extends TestCase { Queue::fake(); + WebMention::factory()->create(); + Artisan::call('webmentions:redownload'); Queue::assertPushed(DownloadWebMention::class); diff --git a/tests/Feature/SearchControllerTest.php b/tests/Feature/SearchControllerTest.php index 137e780f..d4ef1436 100644 --- a/tests/Feature/SearchControllerTest.php +++ b/tests/Feature/SearchControllerTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Tests\Feature; +use App\Models\Note; use Tests\TestCase; class SearchControllerTest extends TestCase @@ -11,6 +12,9 @@ class SearchControllerTest extends TestCase /** @test */ public function searchPageReturnsResult(): void { + Note::factory()->create([ + 'note' => 'I love [duckduckgo.com](https://duckduckgo.com)', + ]); $response = $this->get('/search?terms=love'); $response->assertSee('duckduckgo.com'); } diff --git a/tests/Feature/WebMentionsControllerTest.php b/tests/Feature/WebMentionsControllerTest.php index c3917d3e..258f6c77 100644 --- a/tests/Feature/WebMentionsControllerTest.php +++ b/tests/Feature/WebMentionsControllerTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Tests\Feature; use App\Jobs\ProcessWebMention; +use App\Models\Note; use Illuminate\Support\Facades\Queue; use Tests\TestCase; @@ -73,13 +74,15 @@ class WebMentionsControllerTest extends TestCase } /** @test */ - public function legitimateWebmentionTriggersProcesswebmentionJob(): void + public function legitimateWebmentionTriggersProcessWebmentionJob(): void { Queue::fake(); + $note = Note::factory()->create(); + $response = $this->call('POST', '/webmention', [ 'source' => 'https://example.org/post/123', - 'target' => config('app.url') . '/notes/B' + 'target' => $note->longurl, ]); $response->assertStatus(202); diff --git a/tests/Unit/ArticlesTest.php b/tests/Unit/ArticlesTest.php index d16f640c..1412afc3 100644 --- a/tests/Unit/ArticlesTest.php +++ b/tests/Unit/ArticlesTest.php @@ -5,12 +5,13 @@ declare(strict_types=1); namespace Tests\Unit; use App\Models\Article; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Carbon; use Tests\TestCase; class ArticlesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function titleSlugIsGeneratedAutomatically(): void @@ -64,6 +65,12 @@ class ArticlesTest extends TestCase /** @test */ public function dateScopeReturnsExpectedArticles(): void { + Article::factory()->create([ + 'created_at' => Carbon::now()->subYear()->toDateTimeString(), + 'updated_at' => Carbon::now()->subYear()->toDateTimeString(), + ]); + Article::factory()->create(); + $yearAndMonth = Article::date(date('Y'), date('m'))->get(); $this->assertTrue(count($yearAndMonth) === 1); diff --git a/tests/Unit/Jobs/ProcessBookmarkJobTest.php b/tests/Unit/Jobs/ProcessBookmarkJobTest.php index fea471f1..ba1b4e6f 100644 --- a/tests/Unit/Jobs/ProcessBookmarkJobTest.php +++ b/tests/Unit/Jobs/ProcessBookmarkJobTest.php @@ -19,7 +19,7 @@ class ProcessBookmarkJobTest extends TestCase /** @test */ public function screenshotAndArchiveLinkAreSavedByJob(): void { - $bookmark = Bookmark::find(1); + $bookmark = Bookmark::factory()->create(); $uuid = Uuid::uuid4(); $service = $this->createMock(BookmarkService::class); $service->method('saveScreenshot') @@ -40,7 +40,7 @@ class ProcessBookmarkJobTest extends TestCase /** @test */ public function archiveLinkSavedAsNullWhenExceptionThrown(): void { - $bookmark = Bookmark::find(1); + $bookmark = Bookmark::factory()->create(); $uuid = Uuid::uuid4(); $service = $this->createMock(BookmarkService::class); $service->method('saveScreenshot') diff --git a/tests/Unit/Jobs/ProcessWebMentionJobTest.php b/tests/Unit/Jobs/ProcessWebMentionJobTest.php index e8734fbb..f49615b1 100644 --- a/tests/Unit/Jobs/ProcessWebMentionJobTest.php +++ b/tests/Unit/Jobs/ProcessWebMentionJobTest.php @@ -44,7 +44,7 @@ class ProcessWebMentionJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $note = Note::find(1); + $note = Note::factory()->create(); $source = 'https://example.org/mention/1/'; $job = new ProcessWebMention($note, $source); @@ -70,7 +70,7 @@ class ProcessWebMentionJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $note = Note::find(1); + $note = Note::factory()->create(); $source = 'https://example.org/mention/1/'; $job = new ProcessWebMention($note, $source); @@ -103,7 +103,7 @@ class ProcessWebMentionJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $note = Note::find(14); + $note = Note::factory()->create(); $source = 'https://aaronpk.localhost/reply/1'; $job = new ProcessWebMention($note, $source); @@ -135,7 +135,7 @@ class ProcessWebMentionJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $note = Note::find(14); + $note = Note::factory()->create(); $source = 'https://example.org/reply/1'; $webmention = new WebMention(); $webmention->source = $source; @@ -172,7 +172,7 @@ class ProcessWebMentionJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $note = Note::find(14); + $note = Note::factory()->create(); $source = 'https://example.org/reply/1'; $webmention = new WebMention(); $webmention->source = $source; @@ -209,7 +209,7 @@ class ProcessWebMentionJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $note = Note::find(14); + $note = Note::factory()->create(); $source = 'https://example.org/reply/1'; $webmention = new WebMention(); $webmention->source = $source; diff --git a/tests/Unit/Jobs/SyndicateBookmarkToTwitterJobTest.php b/tests/Unit/Jobs/SyndicateBookmarkToTwitterJobTest.php index 9f6c19da..5f7eeb28 100644 --- a/tests/Unit/Jobs/SyndicateBookmarkToTwitterJobTest.php +++ b/tests/Unit/Jobs/SyndicateBookmarkToTwitterJobTest.php @@ -10,18 +10,20 @@ use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class SyndicateBookmarkToTwitterJobTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function weSendBookmarksToTwitter(): void { + $faker = \Faker\Factory::create(); + $randomNumber = $faker->randomNumber(); $json = json_encode([ - 'url' => 'https://twitter.com/123' + 'url' => 'https://twitter.com/' . $randomNumber ]); $mock = new MockHandler([ new Response(201, ['Content-Type' => 'application/json'], $json), @@ -29,13 +31,12 @@ class SyndicateBookmarkToTwitterJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $bookmark = Bookmark::find(1); + $bookmark = Bookmark::factory()->create(); $job = new SyndicateBookmarkToTwitter($bookmark); $job->handle($client); $this->assertDatabaseHas('bookmarks', [ - 'id' => 1, - 'syndicates' => '{"twitter": "https://twitter.com/123"}', + 'syndicates' => '{"twitter": "https://twitter.com/' . $randomNumber . '"}', ]); } } diff --git a/tests/Unit/Jobs/SyndicateNoteToTwitterJobTest.php b/tests/Unit/Jobs/SyndicateNoteToTwitterJobTest.php index 20e2490e..bad6f897 100644 --- a/tests/Unit/Jobs/SyndicateNoteToTwitterJobTest.php +++ b/tests/Unit/Jobs/SyndicateNoteToTwitterJobTest.php @@ -8,18 +8,20 @@ use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class SyndicateNoteToTwitterJobTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function weSyndicateNotesToTwitter(): void { + $faker = \Faker\Factory::create(); + $randomNumber = $faker->randomNumber(); $json = json_encode([ - 'url' => 'https://twitter.com/i/web/status/123' + 'url' => 'https://twitter.com/i/web/status/' . $randomNumber, ]); $mock = new MockHandler([ new Response(201, ['Content-Type' => 'application/json'], $json), @@ -27,13 +29,12 @@ class SyndicateNoteToTwitterJobTest extends TestCase $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); - $note = Note::find(1); + $note = Note::factory()->create(); $job = new SyndicateNoteToTwitter($note); $job->handle($client); $this->assertDatabaseHas('notes', [ - 'id' => 1, - 'tweet_id' => '123', + 'tweet_id' => $randomNumber, ]); } } diff --git a/tests/Unit/MediaTest.php b/tests/Unit/MediaTest.php index a88003da..e4f06d16 100644 --- a/tests/Unit/MediaTest.php +++ b/tests/Unit/MediaTest.php @@ -5,16 +5,20 @@ declare(strict_types=1); namespace Tests\Unit; use App\Models\Media; +use App\Models\Note; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class MediaTest extends TestCase { + use RefreshDatabase; + /** @test */ public function getTheNoteThatMediaInstanceBelongsTo(): void { - $media = Media::find(1); - $note = $media->note; - $this->assertInstanceOf('App\Models\Note', $note); + $media = Media::factory()->for(Note::factory())->create(); + + $this->assertInstanceOf(Note::class, $media->note); } /** @test */ diff --git a/tests/Unit/MicropubClientsTest.php b/tests/Unit/MicropubClientsTest.php index a812341e..26d6dca4 100644 --- a/tests/Unit/MicropubClientsTest.php +++ b/tests/Unit/MicropubClientsTest.php @@ -6,14 +6,18 @@ namespace Tests\Unit; use App\Models\MicropubClient; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class MicropubClientsTest extends TestCase { + use RefreshDatabase; + /** @test */ public function weCanGetNotesRelatingToClient(): void { - $client = MicropubClient::find(1); + $client = MicropubClient::factory()->make(); + $this->assertInstanceOf(Collection::class, $client->notes); } } diff --git a/tests/Unit/NotesTest.php b/tests/Unit/NotesTest.php index 06386479..784f7a59 100644 --- a/tests/Unit/NotesTest.php +++ b/tests/Unit/NotesTest.php @@ -4,18 +4,18 @@ declare(strict_types=1); namespace Tests\Unit; +use App\Models\{Contact, Media, Note, Place, Tag}; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Cache; -use App\Models\{Media, Note, Tag}; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; class NotesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** * Test the getNoteAttribute method. This will then also call the @@ -28,7 +28,9 @@ class NotesTest extends TestCase { // phpcs:ignore $expected = '

Having a at the local. 🍺

' . PHP_EOL; - $note = Note::find(2); + $note = Note::factory([ + 'note' => 'Having a #beer at the local. 🍺', + ])->create(); $this->assertEquals($expected, $note->note); } @@ -42,7 +44,16 @@ class NotesTest extends TestCase { // phpcs:ignore $expected = '

Hi Tantek Çelik t

' . PHP_EOL; - $note = Note::find(4); + Contact::factory()->create([ + 'nick' => 'tantek', + 'name' => 'Tantek Çelik', + 'homepage' => 'http://tantek.com', + 'twitter' => 't', + 'facebook' => null, + ]); + $note = Note::factory()->create([ + 'note' => 'Hi @tantek', + ]); $this->assertEquals($expected, $note->note); } @@ -56,7 +67,16 @@ class NotesTest extends TestCase { // phpcs:ignore $expected = '

Hi Aaron Parecki Facebook

' . PHP_EOL; - $note = Note::find(5); + Contact::factory()->create([ + 'nick' => 'aaron', + 'name' => 'Aaron Parecki', + 'homepage' => 'https://aaronparecki.com', + 'twitter' => null, + 'facebook' => 123456, + ]); + $note = Note::factory()->create([ + 'note' => 'Hi @aaron', + ]); $this->assertEquals($expected, $note->note); } @@ -69,13 +89,16 @@ class NotesTest extends TestCase public function twitterLinkIsCreatedWhenNoContactFound(): void { $expected = '

Hi @bob

' . PHP_EOL; - $note = Note::find(6); + $note = Note::factory()->create([ + 'note' => 'Hi @bob', + ]); $this->assertEquals($expected, $note->note); } /** @test */ public function shorturlMethodReturnsExpectedValue(): void { + Note::factory(14)->create(); $note = Note::find(14); $this->assertEquals(config('app.shorturl') . '/notes/E', $note->shorturl); } @@ -83,7 +106,13 @@ class NotesTest extends TestCase /** @test */ public function weGetLatitudeLongitudeValuesOfAssociatedPlaceOfNote(): void { - $note = Note::find(2); // should be having beer at bridgewater note + $place = Place::factory()->create([ + 'latitude' => '53.4983', + 'longitude' => '-2.3805', + ]); + $note = Note::factory()->create([ + 'place_id' => $place->id, + ]); $this->assertEquals('53.4983', $note->latitude); $this->assertEquals('-2.3805', $note->longitude); } @@ -91,7 +120,7 @@ class NotesTest extends TestCase /** @test */ public function whenNoAssociatedPlaceWeGetNullForLatitudeLongitudeValues(): void { - $note = Note::find(5); + $note = Note::factory()->create(); $this->assertNull($note->latitude); $this->assertNull($note->longitude); } @@ -99,7 +128,14 @@ class NotesTest extends TestCase /** @test */ public function weCanGetAddressAttributeForAssociatedPlace(): void { - $note = Note::find(2); + $place = Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + 'latitude' => '53.4983', + 'longitude' => '-2.3805', + ]); + $note = Note::factory()->create([ + 'place_id' => $place->id, + ]); $this->assertEquals('The Bridgewater Pub', $note->address); } @@ -254,13 +290,13 @@ class NotesTest extends TestCase /** @test */ public function addImageElementToNoteContentWhenMediaAssociated(): void { - $media = new Media([ + $media = Media::factory()->create([ 'type' => 'image', 'path' => 'test.png', ]); - $media->save(); - $note = new Note(['note' => 'A nice image']); - $note->save(); + $note = Note::factory()->create([ + 'note' => 'A nice image', + ]); $note->media()->save($media); $expected = "

A nice image

@@ -271,13 +307,13 @@ class NotesTest extends TestCase /** @test */ public function addVideoElementToNoteContentWhenMediaAssociated(): void { - $media = new Media([ + $media = Media::factory()->create([ 'type' => 'video', 'path' => 'test.mkv', ]); - $media->save(); - $note = new Note(['note' => 'A nice video']); - $note->save(); + $note = Note::factory()->create([ + 'note' => 'A nice video', + ]); $note->media()->save($media); $expected = "

A nice video

@@ -288,13 +324,13 @@ class NotesTest extends TestCase /** @test */ public function addAudioElementToNoteContentWhenMediaAssociated(): void { - $media = new Media([ + $media = Media::factory()->create([ 'type' => 'audio', 'path' => 'test.flac', ]); - $media->save(); - $note = new Note(['note' => 'Some nice audio']); - $note->save(); + $note = Note::factory()->create([ + 'note' => 'Some nice audio', + ]); $note->media()->save($media); $expected = "

Some nice audio

@@ -326,7 +362,7 @@ class NotesTest extends TestCase /** @test */ public function markdownContentGetsConverted(): void { - $note = Note::create([ + $note = Note::factory()->create([ 'note' => 'The best search engine? https://duckduckgo.com', ]); @@ -348,24 +384,21 @@ class NotesTest extends TestCase ]; Cache::put('933662564587855877', $tempContent); - $note = Note::find(1); + $note = Note::factory()->create([ + 'in_reply_to' => 'https://twitter.com/someRando/status/933662564587855877' + ]); $this->assertSame($tempContent, $note->twitter); } /** @test */ - public function latitudeCanBeParsedFromPlainLocation(): void + public function latitudeAndLongitudeCanBeParsedFromPlainLocation(): void { - $note = Note::find(18); + $note = Note::factory()->create([ + 'location' => '1.23,4.56', + ]); $this->assertSame(1.23, $note->latitude); - } - - /** @test */ - public function longitudeCanBeParsedFromPlainLocation(): void - { - $note = Note::find(18); - $this->assertSame(4.56, $note->longitude); } @@ -374,7 +407,9 @@ class NotesTest extends TestCase { Cache::put('1.23,4.56', 'Antarctica'); - $note = Note::find(18); + $note = Note::factory()->create([ + 'location' => '1.23,4.56', + ]); $this->assertSame('Antarctica', $note->address); } diff --git a/tests/Unit/PlacesTest.php b/tests/Unit/PlacesTest.php index d098bf80..cbb7a58c 100644 --- a/tests/Unit/PlacesTest.php +++ b/tests/Unit/PlacesTest.php @@ -4,55 +4,74 @@ declare(strict_types=1); namespace Tests\Unit; +use App\Models\Note; use App\Models\Place; use App\Services\PlaceService; use Illuminate\Database\Eloquent\Collection; -use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\DB; use InvalidArgumentException; use Tests\TestCase; class PlacesTest extends TestCase { - use DatabaseTransactions; + use RefreshDatabase; /** @test */ public function canRetrieveAssociatedNotes(): void { - $place = Place::find(1); + $place = Place::factory()->create(); + Note::factory(5)->create([ + 'place_id' => $place->id, + ]); $this->assertInstanceOf(Collection::class, $place->notes); + $this->assertCount(5, $place->notes); } /** @test */ public function nearMethodReturnsCollection(): void { - $nearby = Place::near((object) ['latitude' => 53.5, 'longitude' => -2.38], 1000)->get(); + Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + 'latitude' => 53.4983, + 'longitude' => -2.3805, + ]); + $nearby = Place::near((object) ['latitude' => 53.5, 'longitude' => -2.38])->get(); $this->assertEquals('the-bridgewater-pub', $nearby[0]->slug); } /** @test */ public function getLongurl(): void { - $place = Place::find(1); + $place = Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + ]); $this->assertEquals(config('app.url') . '/places/the-bridgewater-pub', $place->longurl); } /** @test */ public function getShorturl() { - $place = Place::find(1); + $place = Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + ]); $this->assertEquals(config('app.shorturl') . '/places/the-bridgewater-pub', $place->shorturl); } /** @test */ public function getUri(): void { - $place = Place::find(1); + $place = Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + ]); $this->assertEquals(config('app.url') . '/places/the-bridgewater-pub', $place->uri); } /** @test */ public function placeServiceReturnsExistingPlaceBasedOnExternalUrlsSearch(): void { + Place::factory(10)->create(); + $place = new Place(); $place->name = 'Temp Place'; $place->latitude = 37.422009; @@ -65,8 +84,8 @@ class PlacesTest extends TestCase 'url' => ['https://www.openstreetmap.org/way/1234'], ] ]); - $this->assertInstanceOf('App\Models\Place', $ret); // a place was returned - $this->assertCount(12, Place::all()); // still 12 places + $this->assertInstanceOf('App\Models\Place', $ret); + $this->assertCount(11, Place::all()); } /** @test */ @@ -90,10 +109,23 @@ class PlacesTest extends TestCase } /** @test */ - public function placeServcieCanupdateExternalUrls(): void + public function placeServiceCanUpdateExternalUrls(): void { - $place = Place::find(1); + $place = Place::factory()->create([ + 'name' => 'The Bridgewater Pub', + 'latitude' => 53.4983, + 'longitude' => -2.3805, + 'external_urls' => '', + ]); + $place->external_urls = 'https://www.openstreetmap.org/way/987654'; + $place->external_urls = 'https://foursquare.com/v/123435/the-bridgewater-pub'; + $place->save(); + $place->external_urls = 'https://bridgewater.pub'; - $this->assertEquals('{"osm":"https:\/\/www.openstreetmap.org\/way\/987654","foursquare":"https:\/\/foursquare.com\/v\/123435\/the-bridgewater-pub","default":"https:\/\/bridgewater.pub"}', $place->external_urls); + $this->assertEquals(json_encode([ + 'default' => 'https://bridgewater.pub', + 'osm' => 'https://www.openstreetmap.org/way/987654', + 'foursquare' => 'https://foursquare.com/v/123435/the-bridgewater-pub', + ]), $place->external_urls); } } diff --git a/tests/Unit/TagsTest.php b/tests/Unit/TagsTest.php index 225a54f4..83ffe539 100644 --- a/tests/Unit/TagsTest.php +++ b/tests/Unit/TagsTest.php @@ -4,22 +4,31 @@ declare(strict_types=1); namespace Tests\Unit; +use App\Models\Bookmark; +use App\Models\Note; use App\Models\Tag; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class TagsTest extends TestCase { + use RefreshDatabase; + /** @test */ public function canGetAssociatedNotes(): void { - $tag = Tag::find(1); // should be beer tag + $note = Note::factory()->create(); + $tag = Tag::factory()->create(); + $note->tags()->save($tag); $this->assertCount(1, $tag->notes); } /** @test */ public function canGetAssociatedBookmarks(): void { - $tag = Tag::find(5); //should be first random tag for bookmarks + $bookmark = Bookmark::factory()->create(); + $tag = Tag::factory()->create(); + $bookmark->tags()->save($tag); $this->assertCount(1, $tag->bookmarks); } diff --git a/tests/Unit/WebMentionTest.php b/tests/Unit/WebMentionTest.php index 6839e862..e58ea5a8 100644 --- a/tests/Unit/WebMentionTest.php +++ b/tests/Unit/WebMentionTest.php @@ -4,19 +4,27 @@ declare(strict_types=1); namespace Tests\Unit; +use App\Models\Note; use App\Models\WebMention; use Codebird\Codebird; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; use Tests\TestCase; class WebMentionTest extends TestCase { + use RefreshDatabase; + /** @test */ public function commentableMethodLinksToNotes(): void { - $webmention = WebMention::find(1); - $this->assertInstanceOf('App\Models\Note', $webmention->commentable); + $note = Note::factory()->create(); + $webmention = WebMention::factory()->make([ + 'commentable_id' => $note->id, + 'commentable_type' => Note::class, + ]); + $this->assertInstanceOf(Note::class, $webmention->commentable); } /** @test */ diff --git a/tests/tantek.html b/tests/tantek.html index 352215db..f3c25707 100644 --- a/tests/tantek.html +++ b/tests/tantek.html @@ -6,5 +6,5 @@

Posted by: