Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPF-652 improve test_get_drafts_by_type_and_between_months_old() #1572

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\DataProvider;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Tests\TestCase;
Expand Down Expand Up @@ -704,161 +703,226 @@ public function test_it_can_not_save_integration_with_subscription_with_differen
);
}

#[DataProvider('dataProviderForGetDraftsByTypeAndBetweenMonthsOld')]
public function test_get_drafts_by_type_and_between_months_old(
IntegrationType $integrationType,
IntegrationStatus $status,
Carbon $date,
?Carbon $mailAlreadySent,
bool $hasContact,
bool $onHold,
TemplateName $templateName,
int $expectedCount,
): void {
$integrationId = Uuid::uuid4()->toString();
public function test_get_drafts_by_type_and_between_months_old(): void
{
$this->setUpDatabaseForGetDraftsByTypeAndBetweenMonthsOld();

$actual = $this->integrationRepository->getDraftsByTypeAndBetweenMonthsOld(
IntegrationType::SearchApi,
12,
24,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
);

$this->assertEqualsCanonicalizing(
[
'A different type of email has been sent, should be selected',
'Should be selected!',
'Should also be selected!',
],
$actual->map(fn ($item) => $item->name)->toArray()
);
}

public static function setUpDatabaseForGetDraftsByTypeAndBetweenMonthsOld(): void
{
$wrongTypeId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $integrationId,
'type' => $integrationType->value,
'id' => $wrongTypeId,
'type' => IntegrationType::EntryApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Test',
'name' => 'Should not be selected: wrong type',
'description' => 'test',
'status' => $status,
'created_at' => $date,
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(14),
]);
DB::table('contacts')->insert([
JonasVHG marked this conversation as resolved.
Show resolved Hide resolved
'id' => Uuid::uuid4()->toString(),
'integration_id' => $wrongTypeId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);

if ($mailAlreadySent) {
DB::table('integrations_mails')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $integrationId,
'template_name' => TemplateName::INTEGRATION_ACTIVATION_REMINDER->value,
]);
}
$alreadyActiveId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $alreadyActiveId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should not be selected: already active',
'description' => 'test',
'status' => IntegrationStatus::Active,
'created_at' => Carbon::now()->subMonths(14),
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $alreadyActiveId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);

if ($hasContact) {
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $integrationId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);
}
$noContactsId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $noContactsId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should not be selected: No contacts',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(14),
]);

if ($onHold) {
DB::table('admin_information')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $integrationId,
'on_hold' => true,
'comment' => 'Integration is on hold',
]);
}
$createdToRecentlyId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $createdToRecentlyId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should not be selected: Created too recently',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(11),
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $createdToRecentlyId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);

$this->assertCount(
$expectedCount,
$this->integrationRepository->getDraftsByTypeAndBetweenMonthsOld(
IntegrationType::SearchApi,
12,
24,
$templateName,
)
);
}
$mailAlreadySentId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $mailAlreadySentId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should not be selected: Created too recently',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(11),
]);
DB::table('integrations_mails')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $mailAlreadySentId,
'template_name' => TemplateName::INTEGRATION_ACTIVATION_REMINDER->value,
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $mailAlreadySentId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);

public static function dataProviderForGetDraftsByTypeAndBetweenMonthsOld(): array
{
return [
'Should not be selected: wrong type' => [
IntegrationType::EntryApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(14),
null,
true,
false,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
0,
],
'Should not be selected: already active' => [
IntegrationType::SearchApi,
IntegrationStatus::Active,
Carbon::now()->subMonths(14),
null,
true,
false,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
0,
],
'Should not be selected: No contacts' => [
IntegrationType::SearchApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(14),
null,
false,
false,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
0,
],
'Should not be selected: Created too recently' => [
IntegrationType::SearchApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(11),
null,
true,
false,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
0,
],
'Should not be selected: Mail already sent' => [
IntegrationType::SearchApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(14),
Carbon::now(),
true,
false,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
0,
],
'Should not be selected: Too old' => [
IntegrationType::SearchApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(50),
null,
true,
false,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
0,
],
'Should not be selected: has an admin hold state' => [
IntegrationType::SearchApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(14),
null,
true,
true,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
0,
],
'Should be selected!' => [
IntegrationType::SearchApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(14),
null,
true,
false,
TemplateName::INTEGRATION_ACTIVATION_REMINDER,
1,
],
'A different type of email has been sent, should be selected' => [
IntegrationType::SearchApi,
IntegrationStatus::Draft,
Carbon::now()->subMonths(14),
Carbon::now(),
true,
false,
TemplateName::INTEGRATION_CREATED,
1,
],
];
$tooOldId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $tooOldId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should not be selected: Too old',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(50),
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $tooOldId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);

$hasAdminHoldId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $hasAdminHoldId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should not be selected: has an admin hold state',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(14),
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $hasAdminHoldId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);
DB::table('admin_information')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $hasAdminHoldId,
'on_hold' => true,
'comment' => 'Integration is on hold',
]);

$shouldBeSelectedId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $shouldBeSelectedId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should be selected!',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(14),
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $shouldBeSelectedId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);

$shouldAlsoBeSelectedId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $shouldAlsoBeSelectedId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'Should also be selected!',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(15),
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $shouldAlsoBeSelectedId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);

$differentTypeOfMailId = Uuid::uuid4()->toString();
DB::table('integrations')->insert([
'id' => $differentTypeOfMailId,
'type' => IntegrationType::SearchApi,
'subscription_id' => Uuid::uuid4()->toString(),
'name' => 'A different type of email has been sent, should be selected',
'description' => 'test',
'status' => IntegrationStatus::Draft,
'created_at' => Carbon::now()->subMonths(15),
]);
DB::table('integrations_mails')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $differentTypeOfMailId,
'template_name' => TemplateName::INTEGRATION_CREATED->value,
]);
DB::table('contacts')->insert([
'id' => Uuid::uuid4()->toString(),
'integration_id' => $differentTypeOfMailId,
'email' => '[email protected]',
'type' => ContactType::Technical->value,
'first_name' => 'Grote',
'last_name' => 'Smurf',
]);
}

private function givenThereIsASubscription(
Expand Down
Loading