Skip to content
Draft
27 changes: 22 additions & 5 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,31 @@ class UserFactory extends Factory {

/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
public function definition(): array {
return [
'password' => $this->faker->password(),
'email' => $this->faker->unique()->safeEmail(),
'verified' => $this->faker->boolean(),
'password' => $this->faker->password(),
'verified' => true,
'is_admin' => 0,
];
}

/**
* Indicate that the user's email address should be unverified.
*/
public function unverified(): static {
return $this->state(fn (array $attributes) => [
'verified' => false,
]);
}

/**
* Indicate that the user is an admin.
*/
public function admin(int $value = 1): static {
return $this->state(fn (array $attributes) => [
'is_admin' => $value,
]);
}
}
1 change: 0 additions & 1 deletion database/seeds/WikisSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public function run() {
$user = User::create([
'email' => $email,
'password' => Hash::make($email),
'verified' => true,
]);

for ($id = 0; $id < 50; $id++) {
Expand Down
5 changes: 3 additions & 2 deletions tests/Jobs/CirrusSearch/ElasticSearchAliasInitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use App\Wiki;
use App\WikiDb;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class ElasticSearchAliasInitTest extends TestCase {
use DatabaseTransactions;

private $wikiId;

private $esHost;
Expand All @@ -29,8 +32,6 @@ protected function setUp(): void {

protected function tearDown(): void {
putenv('ELASTICSEARCH_SHARED_INDEX_PREFIX');
Wiki::query()->delete();
WikiDb::query()->delete();
parent::tearDown();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/CirrusSearch/ForceSearchIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ForceSearchIndexTest extends TestCase {
protected function setUp(): void {
parent::setUp();

$this->user = User::factory()->create(['verified' => true]);
$this->user = User::factory()->create();
$this->wiki = Wiki::factory()->create();
WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]);
WikiSetting::factory()->create(
Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/CirrusSearch/QueueSearchIndexBatchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class QueueSearchIndexBatchesTest extends TestCase {
protected function setUp(): void {
parent::setUp();

$this->user = User::factory()->create(['verified' => true]);
$this->user = User::factory()->create();
$this->wiki = Wiki::factory()->create();
WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]);
WikiSetting::factory()->create(
Expand Down
4 changes: 2 additions & 2 deletions tests/Jobs/DeleteQueryserviceNamespaceJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DeleteQueryserviceNamespaceJobTest extends TestCase {
use DatabaseTransactions;

public function testDeleteNamespace() {
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->subDays(30)->timestamp]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down Expand Up @@ -83,7 +83,7 @@ public function testNoWiki() {

public function testNoNamespace() {

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->subDays(30)->timestamp]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/DeleteWikiDispatcherJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp(): void {
}

private function getWiki($daysSinceDelete = 30): Wiki {
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(
[
'deleted_at' => $daysSinceDelete > 0 ? Carbon::now()->subDays($daysSinceDelete)->timestamp : null,
Expand Down
8 changes: 4 additions & 4 deletions tests/Jobs/DeleteWikiFinalizeJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void {
}

public function testDeleteWiki() {
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->timestamp]);
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => 'asdf', 'value' => false]);
Expand All @@ -41,7 +41,7 @@ public function testDeleteWiki() {
}

public function testDoesNotDeleteWhenResourcesStillExist() {
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->timestamp]);
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => 'asdf', 'value' => false]);
Expand Down Expand Up @@ -81,7 +81,7 @@ public function testDoesNotDeleteWhenResourcesStillExist() {
}

public function testDoesNotDeleteNonDeletedWikis() {
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => null]);
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => 'asdf', 'value' => false]);
Expand All @@ -100,7 +100,7 @@ public function testDoesNotDeleteNonDeletedWikis() {

public function testDeletesFiles() {

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->timestamp]);
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => WikiSetting::wgFavicon, 'value' => false]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/DeleteWikiJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testDeletesWiki() {
// transaction that refresh database has started ue to the DROP statement
Carbon::setTestNow(Carbon::create(2021, 9, 13, 12));

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$this->wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->timestamp]);
WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $user->id]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/ElasticSearch/ElasticSearchIndexDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ElasticSearchIndexDeleteTest extends TestCase {
protected function setUp(): void {
parent::setUp();

$this->user = User::factory()->create(['verified' => true]);
$this->user = User::factory()->create();
$this->wiki = Wiki::factory()->create();
WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]);
WikiSetting::factory()->create(
Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/Integration/ElasticSearchIndexDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testDeletion() {
$response = $this->makeRequest("http://$ELASTICSEARCH_HOST/test_db_name_content_first?pretty", 'PUT');
$this->assertTrue($response['acknowledged']);

$this->user = User::factory()->create(['verified' => true]);
$this->user = User::factory()->create();
$this->wiki = Wiki::factory()->create();
WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]);
WikiSetting::factory()->create(
Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/Integration/QueryserviceNamespaceJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testIntegrationCreate() {
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->never())->method('fail');

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->subDays(30)->timestamp]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/KubernetesIngressCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class KubernetesIngressCreateTest extends TestCase {

public function testCreateIngressJobDoesNotFail() {

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => null]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/KubernetesIngressDeleteJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class KubernetesIngressDeleteJobTest extends TestCase {

public function testDoesNotDeleteNonDeletedWikis() {

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => null]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down
8 changes: 5 additions & 3 deletions tests/Jobs/PlatformStatsSummaryJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ protected function setUp(): void {
);
}

// TODO: Improve PlatformStatsSummaryJob and this test so that it isn't required to wipe whole database tables
protected function tearDown(): void {
Wiki::query()->delete();
WikiDb::query()->delete();
// Wikis are soft-deleted, leaving the database dirty, unless `->forceDelete()` is used
Wiki::query()->forceDelete();
User::query()->delete();
WikiManager::query()->delete();
WikiDb::query()->delete();
parent::tearDown();
}

private function seedWikis() {
$manager = $this->app->make('db');
for ($n = 0; $n < $this->numWikis; $n++) {

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['deleted_at' => null]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down
10 changes: 5 additions & 5 deletions tests/Jobs/SendEmptyWikiNotificationsJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testEmptyWikiNotificationsSendNotification() {
)->toDateTimeString();

Notification::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$wiki->wikiLifecycleEvents()->updateOrCreate(['first_edited' => null]);
Expand All @@ -58,7 +58,7 @@ public function testEmptyWikiNotificationsNotificationNotSent() {
->toDateTimeString();

Notification::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$wiki->wikiLifecycleEvents()->updateOrCreate(['first_edited' => null]);
Expand All @@ -74,7 +74,7 @@ public function testEmptyWikiNotificationsFreshWiki() {
$now = Carbon::now()->toDateTimeString();

Notification::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['created_at' => $now]);
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand All @@ -100,7 +100,7 @@ public function testEmptyWikiNotificationsActiveWiki() {
$now = Carbon::now()->toDateTimeString();

Notification::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]);
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand All @@ -122,7 +122,7 @@ public function testEmptyWikiNotificationsEmptyNotificationReceived() {
)->toDateTimeString();

Notification::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]);
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/SetWikiLogoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testSetLogoFails($wikiKey, $wikiValue, $logoPath) {
*/
public function testSetLogoSucceeds($wikiKey, $wikiValue, $logoPath) {
// create user and wiki for this test
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$wiki = Wiki::factory('nodb')->create([$wikiKey => $wikiValue]);
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Jobs/SiteStatsUpdateJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SiteStatsUpdateJobTest extends TestCase {

protected function setUp(): void {
parent::setUp();
$this->user = User::factory()->create(['verified' => true]);
$this->user = User::factory()->create();
$this->wiki = Wiki::factory()->create();
$this->manager = WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]);
$this->mwBackendHost = 'mediawiki.localhost';
Expand Down
4 changes: 3 additions & 1 deletion tests/Metrics/WikiMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedProp

(new WikiMetrics())->saveMetrics($wiki);

// clean up after the test
// clean up test data after metrics have been saved
// TODO: Why does the `RefreshDatabase` trait not handle this?
$wikiDb = $wikiDb->delete();
$wiki->forceDelete();

$this->assertDatabaseHas('wiki_daily_metrics', [
Expand Down
4 changes: 2 additions & 2 deletions tests/Middleware/LimitWikiAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function tearDown(): void {

private function createWikiAndUser(): array {
$wiki = Wiki::factory()->create();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

return [$wiki, $user];
Expand Down Expand Up @@ -60,7 +60,7 @@ public function testSuccess(): void {
public function testFailOnWrongWikiManager(): void {
$userWiki = Wiki::factory()->create();
$otherWiki = Wiki::factory()->create();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]);

foreach ($this->generateRequestUriAndBody($otherWiki) as $name => [$uri, $body]) {
Expand Down
18 changes: 9 additions & 9 deletions tests/Routes/Wiki/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testWikiCreateDispatchesSomeJobs($elasticSearchConfig) {

Queue::fake();

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
Queue::assertNothingPushed();

$response = $this->actingAs($user, 'api')
Expand Down Expand Up @@ -167,7 +167,7 @@ public function testCreateWikiLimitsNumWikisPerUser() {

Queue::fake();

$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
Queue::assertNothingPushed();

// This shouldn't stop first create since it's deleted
Expand Down Expand Up @@ -238,7 +238,7 @@ private function createSQLandQSDBs(): void {
public function testCreateWikiHandlesRangeOfPostValues($data, $expectedStatus): void {
$this->createSQLandQSDBs();
Queue::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$response = $this->actingAs($user, 'api')
->json(
'POST',
Expand Down Expand Up @@ -299,7 +299,7 @@ public static function createWikiHandlesRangeOfPostValuesProvider(): array {
public function testCreateWithProfileCreatesProfiles(): void {
$this->createSQLandQSDBs();
Queue::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$response = $this->actingAs($user, 'api')
->json(
'POST',
Expand All @@ -314,7 +314,7 @@ public function testCreateWithProfileCreatesProfiles(): void {
public function testCreateWithKERCreatesProfiles(): void {
$this->createSQLandQSDBs();
Queue::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$response = $this->actingAs($user, 'api')
->json(
'POST',
Expand All @@ -332,7 +332,7 @@ public function testCreateWithKERCreatesProfiles(): void {
public function testCreateWithKERRejectsIfSelectedOptionIsInvalid(): void {
$this->createSQLandQSDBs();
Queue::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$response = $this->actingAs($user, 'api')
->json(
'POST',
Expand All @@ -351,7 +351,7 @@ public function testCreateWithKERRejectsIfSelectedOptionIsInvalid(): void {
public function testCreateWithKERCreatesIf3000FreeTextResponse(): void {
$this->createSQLandQSDBs();
Queue::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$response = $this->actingAs($user, 'api')
->json(
'POST',
Expand All @@ -369,7 +369,7 @@ public function testCreateWithKERCreatesIf3000FreeTextResponse(): void {
public function testCreateWithKERErrorsIf3001FreeTextResponse(): void {
$this->createSQLandQSDBs();
Queue::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();
$response = $this->actingAs($user, 'api')
->json(
'POST',
Expand All @@ -389,7 +389,7 @@ public function testCreateWithKERErrorsIf3001FreeTextResponse(): void {
public function testCreateWikiErrorsIfKerIsInvalid(): void {
$this->createSQLandQSDBs();
Queue::fake();
$user = User::factory()->create(['verified' => true]);
$user = User::factory()->create();

$response = $this->actingAs($user, 'api')->json(
'POST',
Expand Down
Loading
Loading