diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index cf786138..110d241a 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -1,5 +1,6 @@ define(App\User::class, function (Faker $faker) { +$factory->define(App\Models\User::class, function (Faker $faker) { static $password; + return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, + 'name' => mb_strtolower($faker->firstName), 'password' => $password ?: $password = bcrypt('secret'), - 'remember_token' => str_random(10), + 'remember_token' => Str::random(10), ]; }); diff --git a/tests/Feature/Admin/AdminHomeControllerTest.php b/tests/Feature/Admin/AdminHomeControllerTest.php index e6366d08..f5140932 100644 --- a/tests/Feature/Admin/AdminHomeControllerTest.php +++ b/tests/Feature/Admin/AdminHomeControllerTest.php @@ -3,13 +3,20 @@ namespace Tests\Feature\Admin; use Tests\TestCase; +use App\Models\User; +use Illuminate\Foundation\Testing\DatabaseTransactions; class AdminHomeControllerTest extends TestCase { + use DatabaseTransactions; + public function test_admin_homepage() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin'); + $response->assertViewIs('admin.welcome'); } } diff --git a/tests/Feature/Admin/ArticlesTest.php b/tests/Feature/Admin/ArticlesTest.php index 94e52139..c483aa23 100644 --- a/tests/Feature/Admin/ArticlesTest.php +++ b/tests/Feature/Admin/ArticlesTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Admin; use Tests\TestCase; +use App\Models\User; use Illuminate\Http\UploadedFile; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -12,21 +13,27 @@ class ArticlesTest extends TestCase public function test_index_page() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/blog'); $response->assertSeeText('Select article to edit:'); } public function test_create_page() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/blog/create'); $response->assertSeeText('Title (URL)'); } public function test_create_new_article() { - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/blog', [ 'title' => 'Test Title', 'main' => 'Article content' @@ -36,6 +43,7 @@ class ArticlesTest extends TestCase public function test_create_new_article_with_upload() { + $user = factory(User::class)->create(); $faker = \Faker\Factory::create(); $text = $faker->text; if ($fh = fopen(sys_get_temp_dir() . '/article.md', 'w')) { @@ -45,7 +53,7 @@ class ArticlesTest extends TestCase $path = sys_get_temp_dir() . '/article.md'; $file = new UploadedFile($path, 'article.md', 'text/plain', filesize($path), null, true); - $this->withSession(['loggedin' => true]) + $this->actingAs($user) ->post('/admin/blog', [ 'title' => 'Uploaded Article', 'article' => $file, @@ -59,14 +67,18 @@ class ArticlesTest extends TestCase public function test_see_edit_form() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/blog/1/edit'); $response->assertSeeText('This is *my* new blog. It uses `Markdown`.'); } public function test_edit_article() { - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/blog/1', [ '_method' => 'PUT', 'title' => 'My New Blog', @@ -80,7 +92,9 @@ class ArticlesTest extends TestCase public function test_delete_article() { - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/blog/1', [ '_method' => 'DELETE', ]); diff --git a/tests/Feature/Admin/ClientsTest.php b/tests/Feature/Admin/ClientsTest.php index 74208927..bce2729a 100644 --- a/tests/Feature/Admin/ClientsTest.php +++ b/tests/Feature/Admin/ClientsTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Admin; use Tests\TestCase; +use App\Models\User; use Illuminate\Foundation\Testing\DatabaseTransactions; class ClientsTest extends TestCase @@ -11,21 +12,27 @@ class ClientsTest extends TestCase public function test_index_page() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/clients'); $response->assertSeeText('Clients'); } public function test_create_page() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/clients/create'); $response->assertSeeText('New Client'); } public function test_create_new_client() { - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/clients', [ 'client_name' => 'Micropublish', 'client_url' => 'https://micropublish.net' @@ -38,14 +45,18 @@ class ClientsTest extends TestCase public function test_see_edit_form() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/clients/1/edit'); $response->assertSee('https://jbl5.dev/notes/new'); } public function test_edit_client() { - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/clients/1', [ '_method' => 'PUT', 'client_url' => 'https://jbl5.dev/notes/new', @@ -59,7 +70,9 @@ class ClientsTest extends TestCase public function test_delete_client() { - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/clients/1', [ '_method' => 'DELETE', ]); diff --git a/tests/Feature/Admin/ContactsTest.php b/tests/Feature/Admin/ContactsTest.php index 7c13ab12..87d64ae6 100644 --- a/tests/Feature/Admin/ContactsTest.php +++ b/tests/Feature/Admin/ContactsTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Admin; use Tests\TestCase; +use App\Models\User; use GuzzleHttp\Client; use App\Models\Contact; use GuzzleHttp\HandlerStack; @@ -26,25 +27,25 @@ class ContactsTest extends TestCase public function test_index_page() { - $response = $this->withSession([ - 'loggedin' => true - ])->get('/admin/contacts'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/contacts'); $response->assertViewIs('admin.contacts.index'); } public function test_create_page() { - $response = $this->withSession([ - 'loggedin' => true - ])->get('/admin/contacts/create'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/contacts/create'); $response->assertViewIs('admin.contacts.create'); } public function test_create_new_contact() { - $this->withSession([ - 'loggedin' => true - ])->post('/admin/contacts', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/contacts', [ 'name' => 'Fred Bloggs', 'nick' => 'fred', 'homepage' => 'https://fred.blog/gs', @@ -58,17 +59,17 @@ class ContactsTest extends TestCase public function test_see_edit_form() { - $response = $this->withSession([ - 'loggedin' => true - ])->get('/admin/contacts/1/edit'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->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', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/contacts/1', [ '_method' => 'PUT', 'name' => 'Tantek Celik', 'nick' => 'tantek', @@ -86,9 +87,9 @@ class ContactsTest extends TestCase 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', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/contacts/1', [ '_method' => 'PUT', 'name' => 'Tantek Celik', 'nick' => 'tantek', @@ -104,9 +105,9 @@ class ContactsTest extends TestCase public function test_delete_contact() { - $this->withSession([ - 'loggedin' => true - ])->post('/admin/contacts/1', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/contacts/1', [ '_method' => 'DELETE', ]); $this->assertDatabaseMissing('contacts', [ @@ -129,10 +130,9 @@ HTML; $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); $this->app->instance(Client::class, $client); + $user = factory(User::class)->create(); - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/contacts/1/getavatar'); + $this->actingAs($user)->get('/admin/contacts/1/getavatar'); $this->assertFileEquals( __DIR__ . '/../../aaron.png', @@ -148,10 +148,9 @@ HTML; $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); $this->app->instance(Client::class, $client); + $user = factory(User::class)->create(); - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/contacts/1/getavatar'); + $response = $this->actingAs($user)->get('/admin/contacts/1/getavatar'); $response->assertRedirect('/admin/contacts/1/edit'); } @@ -170,10 +169,9 @@ HTML; $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); $this->app->instance(Client::class, $client); + $user = factory(User::class)->create(); - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/contacts/1/getavatar'); + $response = $this->actingAs($user)->get('/admin/contacts/1/getavatar'); $response->assertRedirect('/admin/contacts/1/edit'); } @@ -184,10 +182,9 @@ HTML; 'nick' => 'fred', 'name' => 'Fred Bloggs', ]); + $user = factory(User::class)->create(); - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/contacts/' . $contact->id . '/getavatar'); + $response = $this->actingAs($user)->get('/admin/contacts/' . $contact->id . '/getavatar'); $response->assertRedirect('/admin/contacts/' . $contact->id . '/edit'); } diff --git a/tests/Feature/Admin/LikesTest.php b/tests/Feature/Admin/LikesTest.php index 83271072..5f9eefeb 100644 --- a/tests/Feature/Admin/LikesTest.php +++ b/tests/Feature/Admin/LikesTest.php @@ -2,11 +2,11 @@ namespace Tests\Feature\Admin; +use App\Models\User; use Tests\TestCase; use App\Models\Like; use App\Jobs\ProcessLike; use Illuminate\Support\Facades\Queue; -use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\DatabaseTransactions; class LikesTest extends TestCase @@ -15,14 +15,18 @@ class LikesTest extends TestCase public function test_index_page() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/likes'); $response->assertSeeText('Likes'); } public function test_create_page() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/likes/create'); $response->assertSeeText('New Like'); } @@ -30,7 +34,9 @@ class LikesTest extends TestCase public function test_create_new_like() { Queue::fake(); - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/likes', [ 'like_url' => 'https://example.com' ]); @@ -42,7 +48,9 @@ class LikesTest extends TestCase public function test_see_edit_form() { - $response = $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) ->get('/admin/likes/1/edit'); $response->assertSee('Edit Like'); } @@ -50,7 +58,9 @@ class LikesTest extends TestCase public function test_edit_like() { Queue::fake(); - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/likes/1', [ '_method' => 'PUT', 'like_url' => 'https://example.com', @@ -65,7 +75,9 @@ class LikesTest extends TestCase { $like = Like::find(1); $url = $like->url; - $this->withSession(['loggedin' => true]) + $user = factory(User::class)->create(); + + $this->actingAs($user) ->post('/admin/likes/1', [ '_method' => 'DELETE', ]); diff --git a/tests/Feature/Admin/NotesTest.php b/tests/Feature/Admin/NotesTest.php index b7b77757..4fc3d68d 100644 --- a/tests/Feature/Admin/NotesTest.php +++ b/tests/Feature/Admin/NotesTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Admin; +use App\Models\User; use Tests\TestCase; use App\Jobs\SendWebMentions; use Illuminate\Support\Facades\Queue; @@ -13,25 +14,25 @@ class NotesTest extends TestCase public function test_index_page() { - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/notes'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/notes'); $response->assertViewIs('admin.notes.index'); } public function test_create_page() { - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/notes/create'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/notes/create'); $response->assertViewIs('admin.notes.create'); } public function test_create_a_new_note() { - $this->withSession([ - 'loggedin' => true, - ])->post('/admin/notes', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/notes', [ 'content' => 'A new test note', ]); $this->assertDatabaseHas('notes', [ @@ -41,19 +42,18 @@ class NotesTest extends TestCase public function test_edit_page() { - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/notes/1/edit'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/notes/1/edit'); $response->assertViewIs('admin.notes.edit'); } public function test_edit_a_note() { Queue::fake(); + $user = factory(User::class)->create(); - $this->withSession([ - 'loggedin' => true, - ])->post('/admin/notes/1', [ + $this->actingAs($user)->post('/admin/notes/1', [ '_method' => 'PUT', 'content' => 'An edited note', 'webmentions' => true, @@ -67,9 +67,9 @@ class NotesTest extends TestCase public function test_delete_note() { - $this->withSession([ - 'loggedin' => true, - ])->post('/admin/notes/1', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/notes/1', [ '_method' => 'DELETE', ]); $this->assertSoftDeleted('notes', [ diff --git a/tests/Feature/Admin/PlacesTest.php b/tests/Feature/Admin/PlacesTest.php index 27c7c6d3..32de32b6 100644 --- a/tests/Feature/Admin/PlacesTest.php +++ b/tests/Feature/Admin/PlacesTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Admin; +use App\Models\User; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -11,25 +12,25 @@ class PlacesTest extends TestCase public function test_index_page() { - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/places'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/places'); $response->assertViewIs('admin.places.index'); } public function test_create_page() { - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/places/create'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/places/create'); $response->assertViewIs('admin.places.create'); } public function test_create_new_place() { - $this->withSession([ - 'loggedin' => true, - ])->post('/admin/places', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/places', [ 'name' => 'Test Place', 'description' => 'A dummy place for feature tests', 'latitude' => '1.23', @@ -43,17 +44,17 @@ class PlacesTest extends TestCase public function test_edit_page() { - $response = $this->withSession([ - 'loggedin' => true, - ])->get('/admin/places/1/edit'); + $user = factory(User::class)->create(); + + $response = $this->actingAs($user)->get('/admin/places/1/edit'); $response->assertViewIs('admin.places.edit'); } public function test_updating_a_place() { - $this->withSession([ - 'loggedin' => true, - ])->post('/admin/places/1', [ + $user = factory(User::class)->create(); + + $this->actingAs($user)->post('/admin/places/1', [ '_method' => 'PUT', 'name' => 'The Bridgewater', 'description' => 'Who uses “Pub” anyway',